From 8b206dfa335d55f3fa59f9bfd4c5701e246f851c Mon Sep 17 00:00:00 2001 From: Jonathan Chang Date: Sat, 28 Nov 2020 22:56:43 +1100 Subject: [PATCH] git_extensions: move commit_message from utils/git --- Library/Homebrew/dev-cmd/pr-pull.rb | 6 +++--- Library/Homebrew/extend/git_repository.rb | 8 ++++++++ Library/Homebrew/test/dev-cmd/pr-pull_spec.rb | 8 ++++---- Library/Homebrew/test/utils/git_spec.rb | 13 ------------- Library/Homebrew/utils/git.rb | 3 ++- 5 files changed, 17 insertions(+), 21 deletions(-) diff --git a/Library/Homebrew/dev-cmd/pr-pull.rb b/Library/Homebrew/dev-cmd/pr-pull.rb index f866da3b9d..daa2bcaf2f 100644 --- a/Library/Homebrew/dev-cmd/pr-pull.rb +++ b/Library/Homebrew/dev-cmd/pr-pull.rb @@ -89,7 +89,7 @@ module Homebrew end def signoff!(path, pr: nil, dry_run: false) - subject, body, trailers = separate_commit_message(Utils::Git.commit_message(path)) + subject, body, trailers = separate_commit_message(path.git_commit_message) if pr # This is a tap pull request and approving reviewers should also sign-off. @@ -156,7 +156,7 @@ module Homebrew new_formula = Utils::Git.file_at_commit(path, file, "HEAD") bump_subject = determine_bump_subject(old_formula, new_formula, formula_file, reason: reason).strip - subject, body, trailers = separate_commit_message(Utils::Git.commit_message(path)) + subject, body, trailers = separate_commit_message(path.git_commit_message) if subject != bump_subject && !subject.start_with?("#{formula_name}:") safe_system("git", "-C", path, "commit", "--amend", "-q", @@ -181,7 +181,7 @@ module Homebrew messages = [] trailers = [] commits.each do |commit| - subject, body, trailer = separate_commit_message(Utils::Git.commit_message(path, commit)) + subject, body, trailer = separate_commit_message(path.git_commit_message(commit)) body = body.lines.map { |line| " #{line.strip}" }.join("\n") messages << "* #{subject}\n#{body}".strip trailers << trailer diff --git a/Library/Homebrew/extend/git_repository.rb b/Library/Homebrew/extend/git_repository.rb index 09c18ae1f2..e69e6a1a29 100644 --- a/Library/Homebrew/extend/git_repository.rb +++ b/Library/Homebrew/extend/git_repository.rb @@ -85,4 +85,12 @@ module GitRepositoryExtension Utils.popen_read("git", "show", "-s", "--format=%cd", "--date=short", "HEAD", chdir: self).chomp.presence end + + # Gets the full commit message of the specified commit, or of the HEAD commit if unspecified. + sig { params(commit: String).returns(T.nilable(String)) } + def git_commit_message(commit = "HEAD") + return unless git? && Utils::Git.available? + + Utils.popen_read("git", "log", "-1", "--pretty=%B", commit, "--", chdir: self, err: :out).strip.presence + end end diff --git a/Library/Homebrew/test/dev-cmd/pr-pull_spec.rb b/Library/Homebrew/test/dev-cmd/pr-pull_spec.rb index 98a95c4ae6..47a494a016 100644 --- a/Library/Homebrew/test/dev-cmd/pr-pull_spec.rb +++ b/Library/Homebrew/test/dev-cmd/pr-pull_spec.rb @@ -38,7 +38,7 @@ describe Homebrew do EOS end let(:formula_file) { path/"Formula/foo.rb" } - let(:path) { Tap::TAP_DIRECTORY/"homebrew/homebrew-foo" } + let(:path) { (Tap::TAP_DIRECTORY/"homebrew/homebrew-foo").extend(GitRepositoryExtension) } describe "Homebrew.pr_pull_args" do it_behaves_like "parseable arguments" @@ -59,8 +59,8 @@ describe Homebrew do File.open(formula_file, "w") { |f| f.write(formula_version) } safe_system Utils::Git.git, "commit", formula_file, "-m", "version", "--author=#{secondary_author}" described_class.autosquash!(original_hash, path: path) - expect(Utils::Git.commit_message(path)).to include("foo 2.0") - expect(Utils::Git.commit_message(path)).to include("Co-authored-by: #{secondary_author}") + expect(path.git_commit_message).to include("foo 2.0") + expect(path.git_commit_message).to include("Co-authored-by: #{secondary_author}") end end end @@ -75,7 +75,7 @@ describe Homebrew do safe_system Utils::Git.git, "commit", "-m", "foo 1.0 (new formula)" end described_class.signoff!(path) - expect(Utils::Git.commit_message(path)).to include("Signed-off-by:") + expect(path.git_commit_message).to include("Signed-off-by:") end end diff --git a/Library/Homebrew/test/utils/git_spec.rb b/Library/Homebrew/test/utils/git_spec.rb index 0ec9310910..5bc2585a31 100644 --- a/Library/Homebrew/test/utils/git_spec.rb +++ b/Library/Homebrew/test/utils/git_spec.rb @@ -52,19 +52,6 @@ describe Utils::Git do let(:files_hash2) { [@h2[0..6], ["README.md"]] } let(:cherry_pick_commit) { @cherry_pick_commit[0..6] } - describe "#commit_message" do - it "returns the commit message" do - expect(described_class.commit_message(HOMEBREW_CACHE, file_hash1)).to eq("File added") - expect(described_class.commit_message(HOMEBREW_CACHE, file_hash2)).to eq("written to File") - end - - it "errors when commit doesn't exist" do - expect { - described_class.commit_message(HOMEBREW_CACHE, "bad_refspec") - }.to raise_error(ErrorDuringExecution, /bad revision/) - end - end - describe "#cherry_pick!" do it "can cherry pick a commit" do expect(described_class.cherry_pick!(HOMEBREW_CACHE, cherry_pick_commit)).to be_truthy diff --git a/Library/Homebrew/utils/git.rb b/Library/Homebrew/utils/git.rb index 875bfea354..39f0932309 100644 --- a/Library/Homebrew/utils/git.rb +++ b/Library/Homebrew/utils/git.rb @@ -89,8 +89,9 @@ module Utils end def commit_message(repo, commit = nil) + odeprecated "Utils::Git.commit_message(repo)", "Pathname(repo).git_commit_message" commit ||= "HEAD" - Utils.safe_popen_read(git, "-C", repo, "log", "-1", "--pretty=%B", commit, "--", err: :out).strip + Pathname(repo).extend(GitRepositoryExtension).git_commit_message(commit) end def ensure_installed!