#!/usr/bin/env bash set -u -o pipefail TEST_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" source "${TEST_ROOT}/tests/testlib.sh" source "${TEST_ROOT}/inc/git-update.inc" TEST_TEMP_DIR="$(mktemp -d /tmp/tapm-git-tests.XXXXXX)" TEST_REPOSITORY="${TEST_TEMP_DIR}/repository" cleanup_git_tests() { if [[ "$TEST_TEMP_DIR" == /tmp/tapm-git-tests.* && -d "$TEST_TEMP_DIR" ]]; then rm -rf -- "$TEST_TEMP_DIR" fi } trap cleanup_git_tests EXIT git init -q -b main "$TEST_REPOSITORY" git -C "$TEST_REPOSITORY" config user.name 'TA-ProxMenu Tests' git -C "$TEST_REPOSITORY" config user.email 'tapm-tests@example.invalid' printf 'first\n' >"${TEST_REPOSITORY}/fixture.txt" git -C "$TEST_REPOSITORY" add fixture.txt git -C "$TEST_REPOSITORY" commit -q -m first commit_a="$(git -C "$TEST_REPOSITORY" rev-parse HEAD)" printf 'second\n' >>"${TEST_REPOSITORY}/fixture.txt" git -C "$TEST_REPOSITORY" commit -q -am second commit_b="$(git -C "$TEST_REPOSITORY" rev-parse HEAD)" git -C "$TEST_REPOSITORY" switch -q --detach "$commit_a" printf 'alternate\n' >"${TEST_REPOSITORY}/alternate.txt" git -C "$TEST_REPOSITORY" add alternate.txt git -C "$TEST_REPOSITORY" commit -q -m alternate commit_c="$(git -C "$TEST_REPOSITORY" rev-parse HEAD)" git -C "$TEST_REPOSITORY" update-ref refs/tapm/a "$commit_a" git -C "$TEST_REPOSITORY" update-ref refs/tapm/b "$commit_b" git -C "$TEST_REPOSITORY" update-ref refs/tapm/c "$commit_c" assert_equal current \ "$(TAPM_GIT_RELATION "$TEST_REPOSITORY" refs/tapm/a refs/tapm/a)" \ "equal commits" assert_equal behind \ "$(TAPM_GIT_RELATION "$TEST_REPOSITORY" refs/tapm/a refs/tapm/b)" \ "local commit behind remote" assert_equal ahead \ "$(TAPM_GIT_RELATION "$TEST_REPOSITORY" refs/tapm/b refs/tapm/a)" \ "local commit ahead of remote" assert_equal diverged \ "$(TAPM_GIT_RELATION "$TEST_REPOSITORY" refs/tapm/b refs/tapm/c)" \ "diverged commits" git -C "$TEST_REPOSITORY" switch -q -C main "$commit_a" git -C "$TEST_REPOSITORY" update-ref refs/remotes/origin/main "$commit_a" assert_equal current \ "$(TAPM_GIT_BRANCH_STATE "$TEST_REPOSITORY" main)" \ "clean current branch" printf 'untracked\n' >"${TEST_REPOSITORY}/untracked.txt" assert_equal dirty \ "$(TAPM_GIT_BRANCH_STATE "$TEST_REPOSITORY" main)" \ "dirty working tree" rm -f -- "${TEST_REPOSITORY}/untracked.txt" git -C "$TEST_REPOSITORY" switch -q -c feature assert_equal wrong-branch \ "$(TAPM_GIT_BRANCH_STATE "$TEST_REPOSITORY" main)" \ "wrong checked-out branch" git -C "$TEST_REPOSITORY" switch -q main git -C "$TEST_REPOSITORY" update-ref refs/remotes/origin/main "$commit_b" assert_success "fast-forward merge" \ TAPM_GIT_FAST_FORWARD "$TEST_REPOSITORY" main assert_equal "$commit_b" \ "$(git -C "$TEST_REPOSITORY" rev-parse HEAD)" \ "fast-forward destination" finish_tests