cmd/update: attempt migration from master to main branch.

This should allow any repositories that change from `master` to `main`
to be automatically migrated by `brew update` without requiring manual
intervention or displaying errors to the user.
This commit is contained in:
Mike McQuaid 2025-06-11 08:38:32 +01:00
parent 144113318e
commit 837b28ba79
No known key found for this signature in database

View File

@ -725,17 +725,40 @@ EOS
local tmp_failure_file="${DIR}/.git/TMP_FETCH_FAILURES" local tmp_failure_file="${DIR}/.git/TMP_FETCH_FAILURES"
rm -f "${tmp_failure_file}" rm -f "${tmp_failure_file}"
if [[ -n "${HOMEBREW_UPDATE_AUTO}" ]]
then
git fetch --tags --force "${QUIET_ARGS[@]}" origin \
"refs/heads/${UPSTREAM_BRANCH_DIR}:refs/remotes/origin/${UPSTREAM_BRANCH_DIR}" 2>/dev/null
else
# Capture stderr to tmp_failure_file
if ! git fetch --tags --force "${QUIET_ARGS[@]}" origin \ if ! git fetch --tags --force "${QUIET_ARGS[@]}" origin \
"refs/heads/${UPSTREAM_BRANCH_DIR}:refs/remotes/origin/${UPSTREAM_BRANCH_DIR}" 2>>"${tmp_failure_file}" "refs/heads/${UPSTREAM_BRANCH_DIR}:refs/remotes/origin/${UPSTREAM_BRANCH_DIR}" 2>>"${tmp_failure_file}"
then then
if [[ -f "${tmp_failure_file}" ]]
then
local git_errors
git_errors="$(cat "${tmp_failure_file}")"
if [[ "${git_errors}" == "fatal: couldn't find remote ref refs/heads/master" ]]
then
# Attempt migration from master to main branch.
if git fetch --tags --force "${QUIET_ARGS[@]}" origin \
"refs/heads/main:refs/remotes/origin/main" 2>>"${tmp_failure_file}"
then
rm -f "${DIR}/.git/refs/remotes/origin/HEAD" "${DIR}/.git/refs/remotes/origin/master"
UPSTREAM_BRANCH_DIR="$(upstream_branch)"
declare UPSTREAM_BRANCH"${TAP_VAR}"="${UPSTREAM_BRANCH_DIR}"
git branch -m master main "${QUIET_ARGS[@]}"
git branch -u origin/main main "${QUIET_ARGS[@]}"
rm -f "${tmp_failure_file}"
exit
fi
fi
rm -f "${tmp_failure_file}"
fi
# Don't output errors if HOMEBREW_UPDATE_AUTO is set.
if [[ -n "${HOMEBREW_UPDATE_AUTO}" ]]
then
exit
fi
# Reprint fetch errors to stderr # Reprint fetch errors to stderr
[[ -f "${tmp_failure_file}" ]] && cat "${tmp_failure_file}" 1>&2 [[ -n "${git_errors}" ]] && echo "${git_errors}" 1>&2
if [[ "${UPSTREAM_SHA_HTTP_CODE}" == "404" ]] if [[ "${UPSTREAM_SHA_HTTP_CODE}" == "404" ]]
then then
@ -751,7 +774,6 @@ EOS
fi fi
fi fi
fi fi
fi
rm -f "${tmp_failure_file}" rm -f "${tmp_failure_file}"
) & ) &