diff --git a/Library/Homebrew/dev-cmd/pr-pull.rb b/Library/Homebrew/dev-cmd/pr-pull.rb index 61eeae1ac683be053a0f9e6d6c60537b5167ae22..28218da84c60d6546607dbb0362b962a5eecc98e 100644 --- a/Library/Homebrew/dev-cmd/pr-pull.rb +++ b/Library/Homebrew/dev-cmd/pr-pull.rb @@ -275,22 +275,20 @@ module Homebrew raise end - def cherry_pick_pr!(pr, args:, path: ".") + def cherry_pick_pr!(user, repo, pr, args:, path: ".") if args.dry_run? puts <<~EOS git fetch --force origin +refs/pull/#{pr}/head git merge-base HEAD FETCH_HEAD git cherry-pick --ff --allow-empty $merge_base..FETCH_HEAD EOS - else - safe_system "git", "-C", path, "fetch", "--quiet", "--force", "origin", "+refs/pull/#{pr}/head" - merge_base = Utils.popen_read("git", "-C", path, "merge-base", "HEAD", "FETCH_HEAD").strip - commit_count = Utils.popen_read("git", "-C", path, "rev-list", "#{merge_base}..FETCH_HEAD").lines.count - - ohai "Using #{commit_count} commit#{"s" unless commit_count == 1} from ##{pr}" - Utils::Git.cherry_pick!(path, "--ff", "--allow-empty", "#{merge_base}..FETCH_HEAD", - verbose: args.verbose?, resolve: args.resolve?) + return end + + commits = GitHub.pull_request_commits(user, repo, pr) + safe_system "git", "-C", path, "fetch", "--quiet", "--force", "origin", commits.last + ohai "Using #{commits.count} commit#{"s" unless commits.count == 1} from ##{pr}" + Utils::Git.cherry_pick!(path, "--ff", "--allow-empty", *commits, verbose: args.verbose?, resolve: args.resolve?) end def check_branch(path, ref, args:) @@ -391,7 +389,7 @@ module Homebrew Dir.mktmpdir pr do |dir| cd dir do original_commit = Utils.popen_read("git", "-C", tap.path, "rev-parse", "HEAD").chomp - cherry_pick_pr!(pr, path: tap.path, args: args) + cherry_pick_pr!(user, repo, pr, path: tap.path, args: args) autosquash!(original_commit, path: tap.path, args: args) if args.autosquash? signoff!(pr, tap: tap, args: args) unless args.clean? diff --git a/Library/Homebrew/test/utils/github_spec.rb b/Library/Homebrew/test/utils/github_spec.rb index 37f2fd04839551a36aaa6b2ab178009d16ffcaba..6c7739ff7100d89a6e0c7b1b9e8688f2aeb2a1d3 100644 --- a/Library/Homebrew/test/utils/github_spec.rb +++ b/Library/Homebrew/test/utils/github_spec.rb @@ -75,4 +75,11 @@ describe GitHub do expect(url).to eq("https://api.github.com/repos/Homebrew/homebrew-core/actions/artifacts/3557392/zip") end end + + describe "::pull_request_commits", :needs_network do + it "gets the correct commits hashes for a pull request" do + hashes = %w[188606a4a9587365d930b02c98ad6857b1d00150 25a71fe1ea1558415d6496d23834dc70778ddee5] + expect(subject.pull_request_commits("Homebrew", "legacy-homebrew", 50678)).to eq(hashes) + end + end end diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 2ca8dc31985e13b6256bab78b5a0b779570d6581..ab34ee32d7169b00abf5c6a5db021ccb8c8e3347 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -747,4 +747,8 @@ module GitHub end end end + + def pull_request_commits(user, repo, pr) + open_api(url_to("repos", user, repo, "pulls", pr, "commits?per_page=100")).map { |c| c["sha"] } + end end