From a4dcb1eb8bfab9aee624f56f5ecb3f5b119e2c90 Mon Sep 17 00:00:00 2001 From: Zedhugh Chen Date: Mon, 23 Mar 2026 15:59:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20git=20=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E5=89=8D=E8=87=AA=E5=8A=A8=E6=A0=BC=E5=BC=8F=E5=8C=96=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .githooks/pre-commit | 51 ++++++++++++++++++++++++++++++++++++++++++++ Makefile | 6 +++++- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100755 .githooks/pre-commit diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..a75cecd --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,51 @@ +#!/usr/bin/env bash + +set -euo pipefail + +repo_root="$(git rev-parse --show-toplevel)" +cd "$repo_root" + +if ! command -v clang-format >/dev/null 2>&1; then + echo "pre-commit: clang-format not found in PATH" >&2 + exit 1 +fi + +if ! git clang-format -h >/dev/null 2>&1; then + echo "pre-commit: git clang-format is not available" >&2 + exit 1 +fi + +mapfile -d '' staged_files < <( + git diff --cached --name-only --diff-filter=ACMR -z -- \ + '*.c' '*.h' '*.cc' '*.cpp' '*.cxx' '*.hh' '*.hpp' '*.hxx' +) + +if ((${#staged_files[@]} == 0)); then + exit 0 +fi + +if git rev-parse --verify HEAD >/dev/null 2>&1; then + base_commit=HEAD +else + empty_tree="$(git hash-object -t tree /dev/null)" + base_commit="$( + printf 'pre-commit empty base\n' | \ + GIT_AUTHOR_NAME=pre-commit \ + GIT_AUTHOR_EMAIL=pre-commit@example.invalid \ + GIT_COMMITTER_NAME=pre-commit \ + GIT_COMMITTER_EMAIL=pre-commit@example.invalid \ + git commit-tree "$empty_tree" + )" +fi + +set +e +git clang-format --staged --binary clang-format --quiet "$base_commit" -- \ + "${staged_files[@]}" +status=$? +set -e + +if [[ $status -ne 0 && $status -ne 1 ]]; then + exit "$status" +fi + +git add -- "${staged_files[@]}" diff --git a/Makefile b/Makefile index 4d4c323..cdc72b4 100644 --- a/Makefile +++ b/Makefile @@ -44,4 +44,8 @@ test: cmake --build $(BUILD_DIR) ctest --test-dir $(BUILD_DIR) -.PHONY: clean run wm prepare build install uninstall reinstall test +install-hooks: + chmod +x $(MAKEFILE_DIR).githooks/pre-commit + git -C $(MAKEFILE_DIR) config core.hooksPath $(MAKEFILE_DIR).githooks + +.PHONY: clean run wm prepare build install uninstall reinstall test install-hooks