Skip to content

Fix checking out a branch on in existing repo#2114

Open
heewa wants to merge 1 commit into
saltstack:developfrom
heewa:fix-branch-checkout-in-existing-repo
Open

Fix checking out a branch on in existing repo#2114
heewa wants to merge 1 commit into
saltstack:developfrom
heewa:fix-branch-checkout-in-existing-repo

Conversation

@heewa

@heewa heewa commented Jun 1, 2026

Copy link
Copy Markdown

What does this PR do?

In the case where a git repo already exists:

  • if a branch is intended to be checked out, reset to origin/<branch>
    • this mirrors the behavior of the repo not existing on these lines, a little further down
    • it avoids problems stemming from a local branch by that name already existing, which may not even be related, or could have diverged from origin
  • remove the unnecessary code that rebases from origin, since we just directly check it out (which happens after a fetch updates it a few lines up)

What issues does this PR fix or reference?

When a user first runs bootstrap-salt.sh with a branch specified (without a checked out repo) works fine, but sets up HEAD in a detached state to origin/<branch>. Subsequent runs of the bootstrap script fail when it tries to git reset --hard <branch> , with err:

fatal: ambiguous argument '': unknown revision or path not in the working tree.

Previous Behavior

Remove this section if not relevant

New Behavior

Remove this section if not relevant

@twangboy twangboy self-assigned this Jul 9, 2026
Comment thread bootstrap-salt.sh
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants