Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions bootstrap-salt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2145,18 +2145,13 @@ __git_clone_and_checkout() {
git fetch --tags upstream
fi

# Check if GIT_REV_ADJ is a remote branch or just a commit hash
if git branch -r | grep -q -F -w "origin/$GIT_REV_ADJ"; then
GIT_REV_ADJ="origin/$GIT_REV_ADJ"
fi

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"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

echodebug "Hard reseting the cloned repository to ${GIT_REV_ADJ}"
git reset --hard "$GIT_REV_ADJ" || return 1

# Just calling `git reset --hard $GIT_REV_ADJ` on a branch name that has
# already been checked out will not update that branch to the upstream
# HEAD; instead it will simply reset to itself. Check the ref to see
# if it is a branch name, check out the branch, and pull in the
# changes.
if git branch -a | grep -q "${GIT_REV_ADJ}"; then
echodebug "Rebasing the cloned repository branch"
git pull --rebase || return 1
fi
else
if [ "$_FORCE_SHALLOW_CLONE" -eq "${BS_TRUE}" ]; then
echoinfo "Forced shallow cloning of git repository."
Expand Down