From b10ecbab771d3069ee23b6731c84a93f1ba40563 Mon Sep 17 00:00:00 2001 From: core Date: Mon, 27 Nov 2023 10:58:26 -0500 Subject: [PATCH] add rustfmt pre-commit hook --- .git-hooks/pre-commit | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .git-hooks/pre-commit diff --git a/.git-hooks/pre-commit b/.git-hooks/pre-commit new file mode 100644 index 0000000000000000000000000000000000000000..b2e81fdb920aee210075f6422b90b26202ee1698 --- /dev/null +++ b/.git-hooks/pre-commit @@ -0,0 +1,23 @@ +#!/bin/bash + +HAS_ISSUES=0 +FIRST_FILE=1 + +for file in $(git diff --name-only --staged); do + FMT_RESULT="$(rustfmt --skip-children --force --write-mode diff $file 2>/dev/null || true)" + if [ "$FMT_RESULT" != "" ]; then + if [ $FIRST_FILE -eq 0 ]; then + echo -n ", " + fi + echo -n "$file" + HAS_ISSUES=1 + FIRST_FILE=0 + fi +done + +if [ $HAS_ISSUES -eq 0 ]; then + exit 0 +fi + +echo ". Your code has formatting issues in files listed above. Format each file with `rustfmt path_to_file.rs`" +exit 1