]> git.scottworley.com Git - pluta-lesnura/commitdiff
git pre-commit hook
authorScott Worley <scottworley@scottworley.com>
Thu, 13 Jul 2023 18:53:23 +0000 (11:53 -0700)
committerScott Worley <scottworley@scottworley.com>
Thu, 13 Jul 2023 18:58:32 +0000 (11:58 -0700)
git-pre-commit-hook [new file with mode: 0755]
src/lib.rs

diff --git a/git-pre-commit-hook b/git-pre-commit-hook
new file mode 100755 (executable)
index 0000000..1cc63cb
--- /dev/null
@@ -0,0 +1,38 @@
+#!/usr/bin/env bash
+
+# Copy me to .git/hooks/pre-commit
+
+set -euo pipefail
+
+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
+  cargo clippy -- -D warnings -W clippy::pedantic
+  rustfmt --check src/*.rs
+}
+
+in_git_index_in_tmpdir verify
index 7d12d9af8195bf5e19d10c7b592b359ccd014149..c90799e4b25adbb28473bf946d4a1a0aa094ba09 100644 (file)
@@ -1,3 +1,4 @@
+#[must_use]
 pub fn add(left: usize, right: usize) -> usize {
     left + right
 }