Fix checking out a branch on in existing repo#2114
Conversation
| if git branch -r | grep -q -F -w "origin/$GIT_REV_ADJ"; then | ||
| GIT_REV_ADJ="origin/$GIT_REV_ADJ" | ||
| fi | ||
|
|
There was a problem hiding this comment.
"While this fixes the detached HEAD issue for an existing repository, permanently mutating GIT_REV_ADJ by prepending origin/ introduces two side effects downstream:
Global Scope Pollution: GIT_REV_ADJ is used throughout the rest of the script. Changing it here means any subsequent steps that expect the raw branch name or commit SHA will now explicitly see the origin/ prefix.
Upstream Remote Breakage: Just above this block (lines 2138–2146), the script explicitly handles setups tracking upstream (the official SaltStack repo) instead of origin. If a user is tracking an upstream branch, hardcoding origin/ here will cause the git reset --hard on line 2154 to fail because origin/ won't exist or won't be up to date.
Instead of mutating the global variable, we should dynamically evaluate the ref check inside the reset logic using git rev-parse, checking both origin and upstream remotes safely."
if git rev-parse --verify "origin/${GIT_REV_ADJ}" >/dev/null 2>&1; then
git reset --hard "origin/${GIT_REV_ADJ}" || return 1
elif git rev-parse --verify "upstream/${GIT_REV_ADJ}" >/dev/null 2>&1; then
git reset --hard "upstream/${GIT_REV_ADJ}" || return 1
else
git reset --hard "${GIT_REV_ADJ}" || return 1
fi
What does this PR do?
In the case where a git repo already exists:
origin/<branch>originorigin, since we just directly check it out (which happens after afetchupdates it a few lines up)What issues does this PR fix or reference?
When a user first runs
bootstrap-salt.shwith a branch specified (without a checked out repo) works fine, but sets upHEADin a detached state toorigin/<branch>. Subsequent runs of the bootstrap script fail when it tries togit reset --hard <branch>, with err:Previous Behavior
Remove this section if not relevant
New Behavior
Remove this section if not relevant