summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2025-10-28 21:34:41 +0900
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2025-11-05 15:01:16 +0900
commit348adb8fb46c815b1ec16f05d1beceef270ef0ec (patch)
tree5db6ac0774349361ee8514b7a10720a60e2dcd56
parent26cb69f7d173fe5c9c5e6e4dddfd135212987701 (diff)
sync_default_gems.rb: simplify rewriting commit message
Use "git commit --amend" instead of "git filter-branch" since we only need to handle one commit at HEAD.
-rwxr-xr-xtool/sync_default_gems.rb54
-rwxr-xr-xtool/test/test_sync_default_gems.rb10
2 files changed, 24 insertions, 40 deletions
diff --git a/tool/sync_default_gems.rb b/tool/sync_default_gems.rb
index d927086d32..620539bcc9 100755
--- a/tool/sync_default_gems.rb
+++ b/tool/sync_default_gems.rb
@@ -415,18 +415,19 @@ module SyncDefaultGems
Regexp.union(*patterns)
end
- def message_filter(repo, sha, input: ARGF)
+ def message_filter(repo, sha, log)
unless repo.count("/") == 1 and /\A\S+\z/ =~ repo
raise ArgumentError, "invalid repository: #{repo}"
end
unless /\A\h{10,40}\z/ =~ sha
raise ArgumentError, "invalid commit-hash: #{sha}"
end
- log = input.read
- log.delete!("\r")
- log << "\n" if !log.end_with?("\n")
repo_url = "https://github.com/#{repo}"
+ # Log messages generated by GitHub web UI have inconsistent line endings
+ log = log.delete("\r")
+ log << "\n" if !log.end_with?("\n")
+
# Split the subject from the log message according to git conventions.
# SPECIAL TREAT: when the first line ends with a dot `.` (which is not
# obeying the conventions too), takes only that line.
@@ -457,7 +458,7 @@ module SyncDefaultGems
else
log = commit_url
end
- puts subject, "\n", log
+ "#{subject}\n\n#{log}"
end
def log_format(format, args, &block)
@@ -648,6 +649,21 @@ module SyncDefaultGems
`git commit --amend --no-edit --all`
end
+ # Update commit message to include links to the original commit
+ puts "Update commit message: #{sha}"
+ repo, = REPOSITORIES[gem]
+ headers, orig = IO.popen(%W[git cat-file commit #{sha}], "rb", &:read).split("\n\n", 2)
+ message = message_filter(repo, sha, orig)
+ IO.popen(%W[git commit --amend --no-edit -F -], "r+b") {|io|
+ io.write(message)
+ io.close_write
+ io.read
+ }
+ unless $?.success?
+ puts "Failed to modify commit message of #{sha}"
+ return nil
+ end
+
return true
end
@@ -684,32 +700,13 @@ module SyncDefaultGems
puts "----"
failed_commits = []
-
- require 'shellwords'
- filter = [
- ENV.fetch('RUBY', 'ruby').shellescape,
- File.realpath(__FILE__).shellescape,
- "--message-filter",
- ]
commits.each do |sha, subject|
puts "Pick #{sha} from #{repo}."
case pickup_commit(gem, sha, edit)
when false
- next
+ # skipped
when nil
failed_commits << sha
- next
- end
-
- puts "Update commit message: #{sha}"
-
- # Run this script itself (tool/sync_default_gems.rb --message-filter) as a message filter
- IO.popen({"FILTER_BRANCH_SQUELCH_WARNING" => "1"},
- %W[git filter-branch -f --msg-filter #{[filter, repo, sha].join(' ')} -- HEAD~1..HEAD],
- &:read)
- unless $?.success?
- puts "Failed to modify commit message of #{sha}"
- break
end
end
@@ -799,13 +796,6 @@ module SyncDefaultGems
next unless pattern =~ name or pattern =~ gem
printf "%-15s https://github.com/%s\n", name, gem
end
- when "--message-filter"
- ARGV.shift
- if ARGV.size < 2
- abort "usage: #{$0} --message-filter repository commit-hash [input...]"
- end
- message_filter(*ARGV.shift(2))
- exit
when "rdoc-ref"
ARGV.shift
pattern = ARGV.empty? ? %w[*.c *.rb *.rdoc] : ARGV
diff --git a/tool/test/test_sync_default_gems.rb b/tool/test/test_sync_default_gems.rb
index adbde66fbf..b0c9638862 100755
--- a/tool/test/test_sync_default_gems.rb
+++ b/tool/test/test_sync_default_gems.rb
@@ -19,14 +19,8 @@ module Test_SyncDefaultGems
expected.concat(trailers.map {_1+"\n"})
end
- out, err = capture_output do
- SyncDefaultGems.message_filter(repo, sha, input: StringIO.new(input, "r"))
- end
-
- all_assertions do |a|
- a.for("error") {assert_empty err}
- a.for("result") {assert_pattern_list(expected, out)}
- end
+ out = SyncDefaultGems.message_filter(repo, sha, input)
+ assert_pattern_list(expected, out)
end
def test_subject_only