]> git.scottworley.com Git - rfc1751/blob - git-pre-commit-hook
Rename rfc1751 → encode
[rfc1751] / git-pre-commit-hook
1 #! /usr/bin/env nix-shell
2 #! nix-shell -i bash -p shfmt
3 # shellcheck shell=bash
4
5 # Copy me to .git/hooks/pre-commit
6
7 set -euo pipefail
8
9 tmpdir=
10 cleanup_tmpdir() {
11 if [[ "$tmpdir" && -e "$tmpdir" ]];then
12 rm -rf "$tmpdir"
13 fi
14 }
15 trap cleanup_tmpdir EXIT
16
17 # Check out the git index (what's about to be committed) in a temporary
18 # directory & run the supplied command there.
19 in_git_index_in_tmpdir() {
20 tmpdir=$(mktemp -d)
21 [[ "$tmpdir" && -d "$tmpdir" ]]
22 start_index=$(sha256sum "${GIT_INDEX_FILE:-.git/index}")
23 git checkout-index --prefix="$tmpdir/" -a
24 pushd "$tmpdir"
25 "$@"
26 popd
27 end_index=$(sha256sum "${GIT_INDEX_FILE:-.git/index}")
28 if [[ "$start_index" != "$end_index" ]];then
29 echo "Index changed while pre-commit tests were running. Aborting!"
30 exit 1
31 fi
32 }
33
34 verify() {
35 nix-shell --arg lint true --run './test.sh lint'
36 }
37
38 in_git_index_in_tmpdir verify