]> git.scottworley.com Git - tablify/commitdiff
Include a git pre-commit hook
authorScott Worley <scottworley@scottworley.com>
Wed, 16 Jul 2025 10:19:57 +0000 (03:19 -0700)
committerScott Worley <scottworley@scottworley.com>
Wed, 16 Jul 2025 10:19:57 +0000 (03:19 -0700)
Changelog
git-pre-commit-hook [new file with mode: 0755]

index d81a4562cbe3b7a5707a073a8425a76d39e83987..f36eb826e1cfb318cb7e0c4a172a12eea8be4811 100644 (file)
--- a/Changelog
+++ b/Changelog
@@ -1,4 +1,5 @@
 ## [Unreleased]
 ## [Unreleased]
+- Include a git pre-commit hook
 
 ## [0.6.0] - 2024-10-24
 - Lighten background shading
 
 ## [0.6.0] - 2024-10-24
 - Lighten background shading
diff --git a/git-pre-commit-hook b/git-pre-commit-hook
new file mode 100755 (executable)
index 0000000..e40d283
--- /dev/null
@@ -0,0 +1,52 @@
+#!/usr/bin/env bash
+
+# Copy me to .git/hooks/pre-commit
+
+set -euo pipefail
+
+if [[ "${GIT_REFLOG_ACTION:-}" == 'rebase (reword)' ]];then
+  exit 0
+fi
+
+tmpdir=
+cleanup_tmpdir() {
+  if [[ "$tmpdir" && -e "$tmpdir" ]];then
+    rm -rf "$tmpdir"
+  fi
+}
+trap cleanup_tmpdir EXIT
+
+# Check out the git index (what's about to be committed) in a temporary
+# directory & run the supplied command there.
+in_git_index_in_tmpdir() {
+  tmpdir=$(mktemp -d)
+  [[ "$tmpdir" && -d "$tmpdir" ]]
+  start_index=$(sha256sum "${GIT_INDEX_FILE:-.git/index}")
+  git checkout-index --prefix="$tmpdir/" -a
+  pushd "$tmpdir"
+  "$@"
+  popd
+  end_index=$(sha256sum "${GIT_INDEX_FILE:-.git/index}")
+  if [[ "$start_index" != "$end_index" ]];then
+    echo "Index changed while pre-commit tests were running.  Aborting!"
+    exit 1
+  fi
+}
+
+verify() {
+  cargo test --frozen
+  cargo clippy --frozen -- -D warnings \
+    -W clippy::pedantic \
+    -W clippy::clone_on_ref_ptr \
+    -W clippy::if_then_some_else_none \
+    -W clippy::impl_trait_in_params \
+    -W clippy::pattern_type_mismatch \
+    -W clippy::shadow_reuse \
+    -W clippy::shadow_unrelated \
+    -W clippy::str_to_string \
+    -W clippy::try_err \
+    -A clippy::missing_errors_doc
+  find . -name target -prune -o -name '*.rs' -exec rustfmt --check -- {} +
+}
+
+in_git_index_in_tmpdir verify