#!/usr/bin/env bats load helpers/setup setup() { setup_repo } @test "commit -p on a branch that is not on the remote pushes it with tracking" { git switch --quiet -c feature/new echo "change" > file.txt run gb commit -p add file [ "$status" -eq 0 ] remote_has_branch feature/new [ "$(git rev-parse --abbrev-ref '@{upstream}')" = "origin/feature/new" ] } @test "nothing to commit fails before asking for a message" { run gb commit [ "$status" -eq 1 ] [[ "$output" == *"Nothing to commit"* ]] || false [[ "$output" != *"Commit message"* ]] } @test "an empty message cancels the commit" { echo "change" >> README.md run gb_input '\n' commit [ "$status" -eq 1 ] [[ "$output" == *"Empty commit message"* ]] || false [ "$(git rev-list --count HEAD)" -eq 1 ] } @test "with staged and unstaged changes, Enter commits only the staged ones" { echo "staged" > staged.txt git add staged.txt echo "unstaged" >> README.md run gb_input '\n' commit my message [ "$status" -eq 0 ] [ "$(git show --name-only --format= HEAD)" = "staged.txt" ] [ -n "$(git diff --name-only)" ] } @test "new files are listed before they are added" { echo "secret" > .env run gb_input 'n\n' commit add stuff [ "$status" -eq 1 ] [[ "$output" == *"+ .env"* ]] || false [ "$(git rev-list --count HEAD)" -eq 1 ] } @test "new files outside the current directory are listed too" { mkdir src commit_file src/a.txt "a" "add a" echo "change" >> src/a.txt echo "SECRET" > .env cd src run gb_input 'n\n' commit msg [ "$status" -eq 1 ] [[ "$output" == *"+ .env"* ]] || false [ "$(git log -1 --format=%s)" = "add a" ] } @test "a new file outside the current directory is not 'nothing to commit'" { mkdir src echo "new" > root.txt cd src run gb commit add root file [ "$status" -eq 0 ] [ "$(git show --name-only --format= HEAD)" = "root.txt" ] } @test "--yes adds new files without asking" { echo "new" > new.txt run gb commit --yes add new file [ "$status" -eq 0 ] [ "$(git show --name-only --format= HEAD)" = "new.txt" ] } @test "--amend without a message keeps the old message" { echo "change" >> README.md git commit --quiet -am "original message" echo "more" >> README.md run gb commit --amend [ "$status" -eq 0 ] [ "$(git log -1 --format=%s)" = "original message" ] [ "$(git rev-list --count HEAD)" -eq 2 ] } @test "-- lets the message start with a dash" { echo "change" >> README.md run gb commit -- -1 is not an index [ "$status" -eq 0 ] [ "$(git log -1 --format=%s)" = "-1 is not an index" ] } @test "--amend -p asks before force-pushing and never rebases silently" { git switch --quiet -c feature/amend commit_file a.txt "a" "first version" git push --quiet -u origin feature/amend 2>/dev/null local pushed pushed=$(git rev-parse HEAD) # Default answer (EOF) is no: nothing is pushed or rewritten run gb commit --amend -p "second version" [ "$status" -eq 1 ] [ "$(git ls-remote "$REMOTE" refs/heads/feature/amend | cut -f1)" = "$pushed" ] [ "$(git log -1 --format=%s)" = "second version" ] [ "$(git rev-list --count HEAD)" -eq 2 ] # Saying yes force-pushes with lease run gb_input 'y\n' commit --amend -p "third version" [ "$status" -eq 0 ] [ "$(git ls-remote "$REMOTE" refs/heads/feature/amend | cut -f1)" = "$(git rev-parse HEAD)" ] } @test "commit -p asks what to do when the remote diverged" { git switch --quiet -c feature/div git push --quiet -u origin feature/div 2>/dev/null remote_commit feature/div other.txt "theirs" "teammate change" echo "mine" > mine.txt # Default is abort: committed locally, nothing pushed run gb commit --yes -p mine [ "$status" -eq 1 ] [[ "$output" == *"diverged"* ]] || false [ "$(git log -1 --format=%s)" = "mine" ] [ "$(git ls-remote "$REMOTE" refs/heads/feature/div | cut -f1)" = "$(git rev-parse origin/feature/div)" ] # Rebase when asked: linear history on top of the teammate's commit echo "more" > more.txt run gb_input 'r\n' commit --yes -p more [ "$status" -eq 0 ] [ "$(git ls-remote "$REMOTE" refs/heads/feature/div | cut -f1)" = "$(git rev-parse HEAD)" ] [ "$(git rev-list --merges --count HEAD)" -eq 0 ] [ "$(git log -3 --format=%s | tr '\n' ',')" = "more,mine,teammate change," ] } @test "commit -p merges when asked after divergence" { git switch --quiet -c feature/merge git push --quiet -u origin feature/merge 2>/dev/null remote_commit feature/merge other.txt "theirs" "teammate change" echo "mine" > mine.txt run gb_input 'm\n' commit --yes -p mine [ "$status" -eq 0 ] [ "$(git ls-remote "$REMOTE" refs/heads/feature/merge | cut -f1)" = "$(git rev-parse HEAD)" ] git merge-base --is-ancestor origin/feature/merge HEAD } @test "commit -t prefixes the conventional commit type" { echo "change" >> README.md export FZF_STUB_STEP="fix " run gb commit -t broken tests [ "$status" -eq 0 ] [ "$(git log -1 --format=%s)" = "fix: broken tests" ] } @test "git hooks don't see gitbash's internal variables" { local seen="$BATS_TEST_TMPDIR/hook-env" printf '#!/bin/sh\necho "VERSION=$VERSION SCRIPT_DIR=$SCRIPT_DIR GITBASH_BIN=$GITBASH_BIN" > "%s"\n' "$seen" > .git/hooks/pre-commit chmod +x .git/hooks/pre-commit echo "change" > file.txt VERSION= SCRIPT_DIR= GITBASH_BIN= run gb commit -y add file [ "$status" -eq 0 ] [ "$(cat "$seen")" = "VERSION= SCRIPT_DIR= GITBASH_BIN=" ] } @test "commit -p pushes to the branch's upstream on another remote" { local fork="$BATS_TEST_TMPDIR/fork.git" git init --quiet --bare "$fork" git remote add fork "$fork" git switch --quiet -c feature/forked git push --quiet -u fork feature/forked 2>/dev/null echo "fork work" > fork.txt run gb commit -y -p fork work [ "$status" -eq 0 ] [ "$(git ls-remote "$fork" refs/heads/feature/forked | cut -f1)" = "$(git rev-parse HEAD)" ] ! remote_has_branch feature/forked } @test "commit -p pushes to an upstream with a different name" { git switch --quiet -c feature/long-name git push --quiet -u origin feature/long-name 2>/dev/null git branch --quiet -m short echo "work" > w.txt run gb commit -y -p work [ "$status" -eq 0 ] [ "$(git ls-remote "$REMOTE" refs/heads/feature/long-name | cut -f1)" = "$(git rev-parse HEAD)" ] ! remote_has_branch short } @test "commit -p never pushes a branch started from origin/main to main" { git switch --quiet -c feature/from-main --track origin/main echo "work" > w.txt run gb commit -y -p work [ "$status" -eq 0 ] [ "$(git ls-remote "$REMOTE" refs/heads/main | cut -f1)" != "$(git rev-parse HEAD)" ] remote_has_branch feature/from-main [ "$(git rev-parse --abbrev-ref '@{upstream}')" = "origin/feature/from-main" ] } @test "git hooks don't see -y or gitbash's nesting and color variables" { local seen="$BATS_TEST_TMPDIR/hook-env" printf '#!/bin/sh\necho "yes=$GITBASH_ASSUME_YES nested=$GITBASH_NESTED color=$GB_COLOR" > "%s"\n' "$seen" > .git/hooks/pre-commit chmod +x .git/hooks/pre-commit echo "change" > file.txt run gb commit -y add file [ "$status" -eq 0 ] [ "$(cat "$seen")" = "yes= nested= color=" ] # Also when they come from the environment echo "more" >> file.txt GITBASH_ASSUME_YES=1 GITBASH_NESTED=1 GB_COLOR=always run gb commit more [ "$status" -eq 0 ] [ "$(cat "$seen")" = "yes= nested= color=" ] }