From a31bc62046b82e2b1b2965ec38943477f83a8013 Mon Sep 17 00:00:00 2001 From: Sven van Heugten Date: Fri, 6 Mar 2026 05:02:57 +0100 Subject: [PATCH] Check initial state before running ```git-check-assertions run test/git-check-assertions.bats assert_success git checkout HEAD~1 bin run test/git-check-assertions.bats assert_failure assert_output --partial "not ok 2 should refuse to run when the index has changes" assert_output --partial "not ok 3 should refuse to run when the working tree has changes" assert_output --partial "not ok 4 should refuse to run when there are untracked files" ``` --- bin/git-check-assertions | 4 ++++ test/git-check-assertions.bats | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/bin/git-check-assertions b/bin/git-check-assertions index fec0805..b634c09 100755 --- a/bin/git-check-assertions +++ b/bin/git-check-assertions @@ -63,6 +63,10 @@ assert_output() { export -f run assert_success assert_failure assert_output # main flow +if [ ! -z "$(git status --porcelain)" ]; then + echo "Uncommitted changes. Refusing to run." >&2 + exit 1 +fi orig_ref="$(git symbolic-ref --quiet --short HEAD || git rev-parse HEAD)" if git show-ref --verify --quiet refs/heads/main; then base_branch=main diff --git a/test/git-check-assertions.bats b/test/git-check-assertions.bats index ee5097b..6a152bd 100755 --- a/test/git-check-assertions.bats +++ b/test/git-check-assertions.bats @@ -46,6 +46,34 @@ commit_with_assertion() { assert_file_not_exists ../test } +@test "should refuse to run when the index has changes" { + echo "staged" >>readme + git add readme + + run git-check-assertions + + assert_failure + assert_output --partial "Uncommitted changes. Refusing to run." +} + +@test "should refuse to run when the working tree has changes" { + echo "unstaged" >>readme + + run git-check-assertions + + assert_failure + assert_output --partial "Uncommitted changes. Refusing to run." +} + +@test "should refuse to run when there are untracked files" { + echo "new" >untracked + + run git-check-assertions + + assert_failure + assert_output --partial "Uncommitted changes. Refusing to run." +} + @test "should not run any assertion blocks from main when on a feature branch" { commit_with_assertion "touch ../test" git checkout -b feature