Skip to content

Commit

Permalink
cmd/update: on git init, match remote branch name
Browse files Browse the repository at this point in the history
- always default to master (apple git defaults to `main`)
- rename if this turns out not no match the remote
- set upstream tracking branch
  • Loading branch information
rrotter committed Feb 23, 2025
1 parent b3f5d9a commit 6846822
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions Library/Homebrew/cmd/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ git() {
}

git_init_if_necessary() {
local branch_name

safe_cd "${HOMEBREW_REPOSITORY}"
if [[ ! -d ".git" ]]
then
set -e
trap '{ rm -rf .git; exit 1; }' EXIT
git init
git -c init.defaultBranch=master init --quiet
git config --bool core.autocrlf false
git config --bool core.symlinks true
if [[ "${HOMEBREW_BREW_DEFAULT_GIT_REMOTE}" != "${HOMEBREW_BREW_GIT_REMOTE}" ]]
Expand All @@ -56,7 +58,13 @@ git_init_if_necessary() {
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch --force --tags origin
git remote set-head origin --auto >/dev/null
git reset --hard origin/master
branch_name="$(upstream_branch)"
git reset --hard "origin/${branch_name}"
if [[ "${branch_name}" != "master" ]]
then
git branch -m master "${branch_name}"
fi
git branch -u "origin/${branch_name}" "${branch_name}"
SKIP_FETCH_BREW_REPOSITORY=1
set +e
trap - EXIT
Expand All @@ -68,7 +76,7 @@ git_init_if_necessary() {
then
set -e
trap '{ rm -rf .git; exit 1; }' EXIT
git init
git -c init.defaultBranch=master init --quiet
git config --bool core.autocrlf false
git config --bool core.symlinks true
if [[ "${HOMEBREW_CORE_DEFAULT_GIT_REMOTE}" != "${HOMEBREW_CORE_GIT_REMOTE}" ]]
Expand All @@ -77,9 +85,15 @@ git_init_if_necessary() {
fi
git config remote.origin.url "${HOMEBREW_CORE_GIT_REMOTE}"
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch --force origin refs/heads/master:refs/remotes/origin/master
git fetch --force origin
git remote set-head origin --auto >/dev/null
git reset --hard origin/master
branch_name="$(upstream_branch)"
git reset --hard "origin/${branch_name}"
if [[ "${branch_name}" != "master" ]]
then
git branch -m master "${branch_name}"
fi
git branch -u "origin/${branch_name}" "${branch_name}"
SKIP_FETCH_CORE_REPOSITORY=1
set +e
trap - EXIT
Expand Down

0 comments on commit 6846822

Please sign in to comment.