tap: fix renamed branches with brew tap --repair

This commit is contained in:
Rylan Polster 2021-01-25 23:57:01 -05:00
parent b4800dc63b
commit 4bdc11ddc9
No known key found for this signature in database
GPG Key ID: 46A744940CFF4D64
3 changed files with 40 additions and 0 deletions

View File

@ -50,6 +50,7 @@ module Homebrew
if args.repair?
Tap.each(&:link_completions_and_manpages)
Tap.each(&:fix_remote_configuration)
elsif args.list_pinned?
puts Tap.select(&:pinned?).map(&:name)
elsif args.no_named?

View File

@ -54,6 +54,18 @@ module GitRepositoryExtension
popen_git("rev-parse", "--abbrev-ref", "HEAD", safe: safe)
end
# Change the name of a local branch
sig { params(old: String, new: String).void }
def git_rename_branch(old:, new:)
popen_git("branch", "-m", old, new)
end
# Set an upstream branch for a local branch to track
sig { params(local: String, origin: String).void }
def git_branch_set_upstream(local:, origin:)
popen_git("branch", "-u", "origin/#{origin}", local)
end
# Gets the name of the default origin HEAD branch.
sig { returns(T.nilable(String)) }
def git_origin_branch
@ -72,6 +84,17 @@ module GitRepositoryExtension
popen_git("show", "-s", "--format=%cd", "--date=short", "HEAD")
end
# Returns true if the given branch exists on origin
sig { params(branch: String).returns(T::Boolean) }
def git_origin_has_branch?(branch)
popen_git("ls-remote", "--heads", "origin", branch).present?
end
sig { void }
def git_origin_set_head_auto
popen_git("remote", "set-head", "origin", "--auto")
end
# Gets the full commit message of the specified commit, or of the HEAD commit if unspecified.
sig { params(commit: String, safe: T::Boolean).returns(T.nilable(String)) }
def git_commit_message(commit = "HEAD", safe: false)

View File

@ -362,6 +362,22 @@ class Tap
end
end
def fix_remote_configuration
return unless remote.include? "github.com"
current_upstream_head = path.git_origin_branch
return if path.git_origin_has_branch? current_upstream_head
safe_system "git", "-C", path, "fetch", "origin"
path.git_origin_set_head_auto
new_upstream_head = path.git_origin_branch
path.git_rename_branch old: current_upstream_head, new: new_upstream_head
path.git_branch_set_upstream local: new_upstream_head, origin: new_upstream_head
ohai "#{name}: changed default branch name from #{current_upstream_head} to #{new_upstream_head}"
end
# Uninstall this {Tap}.
def uninstall(manual: false)
require "descriptions"