Merge pull request 'Cache successful commits' (#14) from cache into main

Reviewed-on: https://codeberg.org/svenvanheugten/git-check-assertions/pulls/14
This commit is contained in:
Sven van Heugten 2026-03-10 06:50:28 +01:00
commit 1080fc60fd
4 changed files with 80 additions and 1 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/result /result
.git-check-assertions-cache

View file

@ -77,6 +77,12 @@ You can technically assert that a command fails by writing `! ... || exit 1`, or
I'm considering taking `bats-assert` as a dependency, but for now, this very minimal set of functions with a similar interface should get you on your way. I'm considering taking `bats-assert` as a dependency, but for now, this very minimal set of functions with a similar interface should get you on your way.
## Cache of successful commits
After a commit's assertions run successfully, `git-check-assertions` appends the commit hash to `.git-check-assertions-cache` in the repo root. On the next run, any commit listed there is skipped.
Be sure to add `.git-check-assertions-cache` to your `.gitignore`.
## Examples of commit messages ## Examples of commit messages
Assert that a commit builds: Assert that a commit builds:

View file

@ -87,7 +87,22 @@ echo "Base: $base"
echo "Commits to visit: ${#commits[@]}" echo "Commits to visit: ${#commits[@]}"
echo echo
declare -A cached_commits=()
if [ -f .git-check-assertions-cache ]; then
while IFS= read -r cached_commit; do
if [ -n "$cached_commit" ]; then
cached_commits["$cached_commit"]=1
fi
done < .git-check-assertions-cache
fi
for commit_hash in "${commits[@]}"; do for commit_hash in "${commits[@]}"; do
if [ -n "${cached_commits[$commit_hash]+x}" ]; then
echo "Skipping $commit_hash (cached)"
echo
continue
fi
echo "Checking out $commit_hash" echo "Checking out $commit_hash"
git -c advice.detachedHead=false checkout -q "$commit_hash" git -c advice.detachedHead=false checkout -q "$commit_hash"
commit_msg="$(git log -1 --format=%B "$commit_hash")" commit_msg="$(git log -1 --format=%B "$commit_hash")"
@ -107,6 +122,7 @@ for commit_hash in "${commits[@]}"; do
fi fi
restore restore
fi fi
printf '%s\n' "$commit_hash" >>.git-check-assertions-cache
echo echo
done done

View file

@ -9,9 +9,14 @@ init_repo() {
git init -b "$main_branch" git init -b "$main_branch"
git config user.email "john.doe@example.com" git config user.email "john.doe@example.com"
git config user.name "John Doe" git config user.name "John Doe"
echo ".git-check-assertions-cache" >>.gitignore
git add .gitignore
git commit -m "add .git-check-assertions-cache to gitignore"
touch readme touch readme
git add readme git add readme
git commit -m "initial" git commit -m "add readme"
} }
setup() { setup() {
@ -195,6 +200,57 @@ commit_with_assertion() {
assert_size_zero readme assert_size_zero readme
} }
@test "should skip cached commits on subsequent runs" {
git checkout -b feature
commit_with_assertion "touch ../test"
run git-check-assertions
assert_success
assert_file_exists ../test
rm ../test
run git-check-assertions
assert_success
assert_file_not_exists ../test
}
@test "should not cache failed commits" {
git checkout -b feature
commit_with_assertion "touch ../test && exit 3"
run git-check-assertions
assert_failure
assert_file_exists ../test
rm ../test
run git-check-assertions
assert_failure
assert_file_exists ../test
}
@test "should skip cached commits when multiple commits are cached" {
git checkout -b feature
commit_with_assertion "touch ../test1"
commit_with_assertion "touch ../test2"
run git-check-assertions
assert_success
assert_file_exists ../test1
assert_file_exists ../test2
rm ../test1 ../test2
run git-check-assertions
assert_success
assert_file_not_exists ../test1
assert_file_not_exists ../test2
}
@test "assertions should run against the version of the code inside of the commit" { @test "assertions should run against the version of the code inside of the commit" {
git checkout -b feature git checkout -b feature
echo commit1 >readme echo commit1 >readme