git_extensions: move origin_branch from utils/git
This commit is contained in:
parent
d802b3755a
commit
92d3eda914
@ -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,
|
||||
|
||||
@ -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]
|
||||
default_origin_branch = formula.tap.path.git_origin_branch if formula.tap
|
||||
|
||||
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
|
||||
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
|
||||
|
||||
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
|
||||
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
|
||||
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,
|
||||
|
||||
@ -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}"
|
||||
|
||||
@ -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?
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user