Skip to content

Instantly share code, notes, and snippets.

@x-yuri
Last active December 13, 2024 13:31
Show Gist options
  • Save x-yuri/020d2c271fa10238264769865392a244 to your computer and use it in GitHub Desktop.
Save x-yuri/020d2c271fa10238264769865392a244 to your computer and use it in GitHub Desktop.
git checkout: -t, --track, --no-track

git checkout: -t, --track, --no-track

These options override the branch.autoSetupMerge setting:

  • -t, --track, --track=direct make the starting point the upstream of the branch that's being created
  • --track=inherit copy the upstream configuration (if such exists) from the starting point to the branch that's being created

a.bats:

strict() { set -euo pipefail; shopt -s inherit_errexit; "$@"; }

setup() {
    [ "$BATS_LIB_PATH" = /usr/lib/bats ] && BATS_LIB_PATH=$HOME/.bats/lib:$BATS_LIB_PATH
    bats_load_library bats-support
    bats_load_library bats-assert
    strict
}

# the state of the cloned repository (2 commits, 1 local branch dev):
# a (origin/master) --- b (HEAD -> dev, origin/dev)

@test "-t makes it set an upstream" {
    mk_cloned_repo
    git config branch.autoSetupMerge false

    git checkout -b master -t origin/master

    assert_upstream master origin/master
}

@test "--no-track makes it not set an upstream" {
    mk_cloned_repo
    git config branch.autoSetupMerge always

    git checkout -b master --no-track origin/master

    refute_upstream master
}

@test "--track=inherit makes it copy the upstream configuration" {
    mk_cloned_repo
    git config branch.autoSetupMerge always

    git checkout -b master --track=inherit dev

    assert_upstream master origin/dev
}

assert_branch() {
    run git branch --list "$1"
    assert_output -p "$1"
}

assert_upstream() {
    run git rev-parse --abbrev-ref "$1@{upstream}"
    assert_output "$2"
}

refute_upstream() {
    run git rev-parse --abbrev-ref "$1@{upstream}"
    if [ "${2-}" ]; then
        refute_output "$2"
    else
        assert_output "fatal: no upstream configured for branch '$1'"
    fi
}

mk_cloned_repo() {
    (mkrepo)
    cd "$BATS_TEST_TMPDIR"
    git clone a b
    cd b
}

mkrepo() {
    cd "$BATS_TEST_TMPDIR"
    mkdir a
    cd a
    git init
    git config user.email [email protected]
    git config user.name "Your Name"
    git commit --allow-empty -m a
    git checkout -b dev
    git commit --allow-empty -m b
}
$ docker run --rm -itv "$PWD":/app -w /app alpine:3.21
/ # apk add git bash ncurses
/ # git clone https://github.com/bats-core/bats-core ~/.bats
/ # git clone https://github.com/bats-core/bats-support ~/.bats/lib/bats-support
/ # git clone https://github.com/bats-core/bats-assert ~/.bats/lib/bats-assert
/ # ~/.bats/bin/bats a.bats
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment