Skip to content

Commit 9f51df0

Browse files
hsbtclaude
andcommitted
Check git state before prepare_release
Add a check_git_state! method that runs at the start of prepare! to detect stale state from a previous release attempt: leftover index.lock, interrupted cherry-pick or rebase, and existing release branches. When any of these are found the task aborts with actionable error messages so the operator can resolve them before retrying. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 78ed27e commit 9f51df0

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tool/release.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ def set_rubygems_as_current_library
174174
def prepare!
175175
initial_branch = `git rev-parse --abbrev-ref HEAD`.strip
176176

177+
check_git_state!
178+
177179
unless @prerelease
178180
create_if_not_exist_and_switch_to(@stable_branch, from: "master")
179181
system("git", "push", "origin", @stable_branch, exception: true) if @level == :minor_or_major && !ENV["DRYRUN"]
@@ -228,6 +230,32 @@ def prepare!
228230
end
229231
end
230232

233+
def check_git_state!
234+
git_dir = `git rev-parse --git-dir`.strip
235+
errors = []
236+
237+
if File.exist?(File.join(git_dir, "index.lock"))
238+
errors << "#{git_dir}/index.lock exists. A previous git process may have crashed. Remove it if no git process is running."
239+
end
240+
241+
if File.exist?(File.join(git_dir, "CHERRY_PICK_HEAD"))
242+
errors << "A cherry-pick is in progress. Run `git cherry-pick --abort` to cancel it."
243+
end
244+
245+
if File.exist?(File.join(git_dir, "rebase-merge")) || File.exist?(File.join(git_dir, "rebase-apply"))
246+
errors << "A rebase is in progress. Run `git rebase --abort` to cancel it."
247+
end
248+
249+
branches = [@release_branch]
250+
branches << "cherry_pick_changelogs" unless @prerelease
251+
existing = branches.select {|b| system("git", "rev-parse", "--verify", b, out: IO::NULL, err: IO::NULL) }
252+
unless existing.empty?
253+
errors << "Release branches already exist: #{existing.join(", ")}. Please delete them before running this task."
254+
end
255+
256+
raise errors.join("\n") unless errors.empty?
257+
end
258+
231259
def create_if_not_exist_and_switch_to(branch, from:)
232260
system("git", "checkout", branch, exception: true, err: IO::NULL)
233261
rescue StandardError

0 commit comments

Comments
 (0)