git-check-assertions/bin/git-check-assertions
Sven van Heugten 210dd4d72d
Fix build
```git-check-assertions
run nix build
assert_success

git checkout HEAD~
run nix build
assert_failure
assert_output --partial "done <.git-check-assertions-cache"
```
2026-03-10 17:39:25 +01:00

132 lines
3.8 KiB
Bash
Executable file

#!/usr/bin/env bash
# Copyright (c) 2026 Sven van Heugten
#
# Repository: https://codeberg.org/svenvanheugten/git-check-assertions
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
set -euo pipefail
# helper functions inspired by bats/bats-assert
run() {
set +e
output="$("$@" 2>&1)"
status=$?
set -e
printf '%s\n' "$output"
return 0
}
assert_success() {
if [ "$status" -ne 0 ]; then
echo "Expected command to succeed, but it failed."
exit 1
fi
}
assert_failure() {
if [ "$status" -eq 0 ]; then
echo "Expected command to fail, but it succeeded."
exit 1
fi
}
assert_output() {
if [ "${1:-}" = "--partial" ]; then
local expected="$2"
if [[ "$output" != *"$expected"* ]]; then
echo "Expected output to contain: $expected"
echo "Actual output: $output"
exit 1
fi
return 0
fi
if [ "$output" != "$1" ]; then
echo "Expected output to equal: $1"
echo "Actual output: $output"
exit 1
fi
}
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
elif git show-ref --verify --quiet refs/heads/master; then
base_branch=master
else
echo "No main or master branch found." >&2
exit 1
fi
restore() {
git restore -q --staged --worktree .
}
base="$(git merge-base "$base_branch" HEAD)"
mapfile -t commits < <(git rev-list --reverse "${base}..HEAD")
echo "Base: $base"
echo "Commits to visit: ${#commits[@]}"
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
if [ -n "${cached_commits[$commit_hash]+x}" ]; then
echo "Skipping $commit_hash (cached)"
echo
continue
fi
echo "Checking out $commit_hash"
git -c advice.detachedHead=false checkout -q "$commit_hash"
commit_msg="$(git log -1 --format=%B "$commit_hash")"
# shellcheck disable=SC2016
block="$(printf '%s\n' "$commit_msg" |
sed -n '/^```git-check-assertions[[:space:]]*$/,/^```[[:space:]]*$/p' |
sed '1d;$d')"
if [ -n "$block" ]; then
echo "git-check-assertions block in $commit_hash:"
printf '%s\n' "$block" | sed 's/^/> /'
if ! bash -euo pipefail -c "$block"; then
echo "git-check-assertions block failed in $commit_hash" >&2
restore
echo "Returning to $orig_ref"
git checkout -q "$orig_ref"
exit 1
fi
restore
fi
printf '%s\n' "$commit_hash" >>.git-check-assertions-cache
echo
done
echo
echo "Success!"
echo "Returning to $orig_ref"
git checkout -q "$orig_ref"