diff --git a/Library/Homebrew/dev-cmd/bump-cask-pr.rb b/Library/Homebrew/dev-cmd/bump-cask-pr.rb index 1a70fed47f082cd80c0170fccb51e864319ac0a0..7a4b74e6bbd85cbc5cd349ec62797fe903513093 100644 --- a/Library/Homebrew/dev-cmd/bump-cask-pr.rb +++ b/Library/Homebrew/dev-cmd/bump-cask-pr.rb @@ -77,8 +77,8 @@ module Homebrew old_hash = cask.sha256.to_s tap_full_name = cask.tap&.full_name - origin_branch = Utils::Git.origin_branch(cask.tap.path) if cask.tap - origin_branch ||= "origin/master" + default_remote_branch = cask.tap.path.git_origin_branch if cask.tap + default_remote_branch ||= "master" previous_branch = "-" check_open_pull_requests(cask, tap_full_name, args: args) @@ -200,7 +200,7 @@ module Homebrew pr_info = { sourcefile_path: cask.sourcefile_path, old_contents: old_contents, - origin_branch: origin_branch, + remote_branch: default_remote_branch, branch_name: "bump-#{cask.token}-#{new_version.tr(",:", "-")}", commit_message: "Update #{cask.token} from #{old_version} to #{new_version}", previous_branch: previous_branch, diff --git a/Library/Homebrew/dev-cmd/bump-formula-pr.rb b/Library/Homebrew/dev-cmd/bump-formula-pr.rb index dffc4dcdf35fce1c1857e6923180a2b33c97afd1..19e28f8f3298bcf9699c11ca9b9258381b23b980 100644 --- a/Library/Homebrew/dev-cmd/bump-formula-pr.rb +++ b/Library/Homebrew/dev-cmd/bump-formula-pr.rb @@ -84,44 +84,38 @@ module Homebrew end def use_correct_linux_tap(formula, args:) - if OS.linux? && formula.tap.core_tap? - tap_full_name = formula.tap.full_name.gsub("linuxbrew", "homebrew") - homebrew_core_url = "https://github.com/#{tap_full_name}" - homebrew_core_remote = "homebrew" - homebrew_core_branch = "master" - origin_branch = "#{homebrew_core_remote}/#{homebrew_core_branch}" - previous_branch = Utils.popen_read("git -C \"#{formula.tap.path}\" symbolic-ref -q --short HEAD").chomp - previous_branch = "master" if previous_branch.empty? - formula_path = formula.path.to_s[%r{(Formula/.*)}, 1] - - if args.dry_run? || args.write? - ohai "git remote add #{homebrew_core_remote} #{homebrew_core_url}" - ohai "git fetch #{homebrew_core_remote} #{homebrew_core_branch}" - ohai "git cat-file -e #{origin_branch}:#{formula_path}" - ohai "git checkout #{origin_branch}" - return tap_full_name, origin_branch, previous_branch - else - formula.path.parent.cd do - unless Utils.popen_read("git remote -v").match?(%r{^homebrew.*Homebrew/homebrew-core.*$}) - ohai "Adding #{homebrew_core_remote} remote" - safe_system "git", "remote", "add", homebrew_core_remote, homebrew_core_url - end - ohai "Fetching #{origin_branch}" - safe_system "git", "fetch", homebrew_core_remote, homebrew_core_branch - if quiet_system "git", "cat-file", "-e", "#{origin_branch}:#{formula_path}" - ohai "#{formula.full_name} exists in #{origin_branch}" - safe_system "git", "checkout", origin_branch - return tap_full_name, origin_branch, previous_branch - end - end - end + default_origin_branch = formula.tap.path.git_origin_branch if formula.tap + + return formula.tap&.full_name, "origin", default_origin_branch, "-" if !OS.linux? || !formula.tap.core_tap? + + tap_full_name = formula.tap.full_name.gsub("linuxbrew", "homebrew") + homebrew_core_url = "https://github.com/#{tap_full_name}" + homebrew_core_remote = "homebrew" + previous_branch = formula.tap.path.git_branch || "master" + formula_path = formula.path.relative_path_from(formula.tap.path) + full_origin_branch = "#{homebrew_core_remote}/#{default_origin_branch}" + + if args.dry_run? || args.write? + ohai "git remote add #{homebrew_core_remote} #{homebrew_core_url}" + ohai "git fetch #{homebrew_core_remote} HEAD #{default_origin_branch}" + ohai "git cat-file -e #{full_origin_branch}:#{formula_path}" + ohai "git checkout #{full_origin_branch}" + return tap_full_name, homebrew_core_remote, default_origin_branch, previous_branch end - if formula.tap - origin_branch = Utils.popen_read("git", "-C", formula.tap.path.to_s, "symbolic-ref", "-q", "--short", - "refs/remotes/origin/HEAD").chomp.presence + + formula.tap.path.cd do + unless Utils.popen_read("git remote -v").match?(%r{^homebrew.*Homebrew/homebrew-core.*$}) + ohai "Adding #{homebrew_core_remote} remote" + safe_system "git", "remote", "add", homebrew_core_remote, homebrew_core_url + end + ohai "Fetching remote #{homebrew_core_remote}" + safe_system "git", "fetch", homebrew_core_remote, "HEAD", default_origin_branch + if quiet_system "git", "cat-file", "-e", "#{full_origin_branch}:#{formula_path}" + ohai "#{formula.full_name} exists in #{full_origin_branch}" + safe_system "git", "checkout", full_origin_branch + return tap_full_name, homebrew_core_remote, default_origin_branch, previous_branch + end end - origin_branch ||= "origin/master" - [formula.tap&.full_name, origin_branch, "-"] end def bump_formula_pr @@ -142,7 +136,7 @@ module Homebrew odie "This formula is disabled!" if formula.disabled? - tap_full_name, origin_branch, previous_branch = use_correct_linux_tap(formula, args: args) + tap_full_name, remote, remote_branch, previous_branch = use_correct_linux_tap(formula, args: args) check_open_pull_requests(formula, tap_full_name, args: args) new_version = args.version @@ -355,7 +349,8 @@ module Homebrew sourcefile_path: formula.path, old_contents: old_contents, additional_files: alias_rename, - origin_branch: origin_branch, + remote: remote, + remote_branch: remote_branch, branch_name: "bump-#{formula.name}-#{new_formula_version}", commit_message: "#{formula.name} #{new_formula_version}", previous_branch: previous_branch, diff --git a/Library/Homebrew/dev-cmd/pr-pull.rb b/Library/Homebrew/dev-cmd/pr-pull.rb index 8a0e93f946f15212fcdd53df7154c69c9edc9b5d..f866da3b9d0e4c514b3a3be317ad7dbece538970 100644 --- a/Library/Homebrew/dev-cmd/pr-pull.rb +++ b/Library/Homebrew/dev-cmd/pr-pull.rb @@ -385,11 +385,8 @@ module Homebrew _, user, repo, pr = *url_match odie "Not a GitHub pull request: #{arg}" unless pr - current_branch = tap.path.git_branch - origin_branch = Utils::Git.origin_branch(tap.path).split("/").last - - if current_branch != origin_branch || args.branch_okay? || args.clean? - opoo "Current branch is #{current_branch}: do you need to pull inside #{origin_branch}?" + if !tap.path.git_default_origin_branch? || args.branch_okay? || args.clean? + opoo "Current branch is #{tap.path.git_branch}: do you need to pull inside #{tap.path.git_origin_branch}?" end ohai "Fetching #{tap} pull request ##{pr}" diff --git a/Library/Homebrew/diagnostic.rb b/Library/Homebrew/diagnostic.rb index 74c9aca8cd58b2f3d7a2a459822083abd516286c..3199df2ac5f87f2063e3f8d42456a145410410a3 100644 --- a/Library/Homebrew/diagnostic.rb +++ b/Library/Homebrew/diagnostic.rb @@ -572,16 +572,9 @@ module Homebrew return unless Utils::Git.available? commands = Tap.map do |tap| - next unless tap.path.git? - next if tap.path.git_origin.blank? + next if tap.path.git_default_origin_branch? - branch = tap.path.git_branch - next if branch.blank? - - origin_branch = Utils::Git.origin_branch(tap.path)&.split("/")&.last - next if origin_branch == branch - - "git -C $(brew --repo #{tap.name}) checkout #{origin_branch}" + "git -C $(brew --repo #{tap.name}) checkout #{tap.path.git_origin_branch}" end.compact return if commands.blank? diff --git a/Library/Homebrew/extend/git_repository.rb b/Library/Homebrew/extend/git_repository.rb index 067f518bbfcaa5dfae393d8a1672aeecad658ee9..09c18ae1f28e189cf080ca521d2dcaee75827781 100644 --- a/Library/Homebrew/extend/git_repository.rb +++ b/Library/Homebrew/extend/git_repository.rb @@ -63,6 +63,21 @@ module GitRepositoryExtension Utils.popen_read("git", "rev-parse", "--abbrev-ref", "HEAD", chdir: self).chomp.presence end + # Gets the name of the default origin HEAD branch. + sig { returns(T.nilable(String)) } + def git_origin_branch + return unless git? && Utils::Git.available? + + Utils.popen_read("git", "symbolic-ref", "-q", "--short", "refs/remotes/origin/HEAD", chdir: self) + .chomp.presence&.split("/")&.last + end + + # Returns true if the repository's current branch matches the default origin branch. + sig { returns(T.nilable(T::Boolean)) } + def git_default_origin_branch? + git_origin_branch == git_branch + end + # Returns the date of the last commit, in YYYY-MM-DD format. sig { returns(T.nilable(String)) } def git_last_commit_date diff --git a/Library/Homebrew/utils/git.rb b/Library/Homebrew/utils/git.rb index bb8deabe5578d1533c759b7b6014ca90588cc149..875bfea3542a90e1d44edf05673ce659f92ac0b4 100644 --- a/Library/Homebrew/utils/git.rb +++ b/Library/Homebrew/utils/git.rb @@ -135,8 +135,8 @@ module Utils end def origin_branch(repo) - Utils.popen_read(git, "-C", repo, "symbolic-ref", "-q", "--short", - "refs/remotes/origin/HEAD").chomp.presence + odeprecated "Utils::Git.origin_branch(repo)", "Pathname(repo).git_origin_branch" + Pathname(repo).extend(GitRepositoryExtension).git_origin_branch end def current_branch(repo) diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 8aeb60a21a7e57fff65b347970ae677829a773b1..c58974816834b55624ca00b05b284cb7457dcc78 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -679,7 +679,8 @@ module GitHub sourcefile_path = info[:sourcefile_path] old_contents = info[:old_contents] additional_files = info[:additional_files] || [] - origin_branch = info[:origin_branch] + remote = info[:remote] || "origin" + remote_branch = info[:remote_branch] branch = info[:branch_name] commit_message = info[:commit_message] previous_branch = info[:previous_branch] @@ -688,7 +689,6 @@ module GitHub pr_message = info[:pr_message] sourcefile_path.parent.cd do - _, base_branch = origin_branch.split("/") git_dir = Utils.popen_read("git rev-parse --git-dir").chomp shallow = !git_dir.empty? && File.exist?("#{git_dir}/shallow") changed_files = [sourcefile_path] @@ -698,12 +698,12 @@ module GitHub ohai "try to fork repository with GitHub API" unless args.no_fork? ohai "git fetch --unshallow origin" if shallow ohai "git add #{changed_files.join(" ")}" - ohai "git checkout --no-track -b #{branch} #{origin_branch}" + ohai "git checkout --no-track -b #{branch} #{remote}/#{remote_branch}" ohai "git commit --no-edit --verbose --message='#{commit_message}'" \ " -- #{changed_files.join(" ")}" ohai "git push --set-upstream $HUB_REMOTE #{branch}:#{branch}" ohai "git checkout --quiet #{previous_branch}" - ohai "create pull request with GitHub API (base branch: #{base_branch})" + ohai "create pull request with GitHub API (base branch: #{remote_branch})" else unless args.commit? @@ -723,7 +723,7 @@ module GitHub end safe_system "git", "add", *changed_files - safe_system "git", "checkout", "--no-track", "-b", branch, origin_branch unless args.commit? + safe_system "git", "checkout", "--no-track", "-b", branch, "#{remote}/#{remote_branch}" unless args.commit? safe_system "git", "commit", "--no-edit", "--verbose", "--message=#{commit_message}", "--", *changed_files @@ -746,7 +746,7 @@ module GitHub begin url = GitHub.create_pull_request(tap_full_name, commit_message, - "#{username}:#{branch}", base_branch, pr_message)["html_url"] + "#{username}:#{branch}", remote_branch, pr_message)["html_url"] if args.no_browse? puts url else