From 4bdc11ddc97ccf2c7b822e7485bab65f940a69e9 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Mon, 25 Jan 2021 23:57:01 -0500 Subject: [PATCH] tap: fix renamed branches with `brew tap --repair` --- Library/Homebrew/cmd/tap.rb | 1 + Library/Homebrew/extend/git_repository.rb | 23 +++++++++++++++++++++++ Library/Homebrew/tap.rb | 16 ++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/Library/Homebrew/cmd/tap.rb b/Library/Homebrew/cmd/tap.rb index 2868cabbcb..d352143c7a 100644 --- a/Library/Homebrew/cmd/tap.rb +++ b/Library/Homebrew/cmd/tap.rb @@ -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? diff --git a/Library/Homebrew/extend/git_repository.rb b/Library/Homebrew/extend/git_repository.rb index e490e12eee..b5a54742f8 100644 --- a/Library/Homebrew/extend/git_repository.rb +++ b/Library/Homebrew/extend/git_repository.rb @@ -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) diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index 986e35dc13..9a49c29424 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -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"