[git] git hook

2026 年 7 月 8 日 星期三
15

[git] git hook


#!/bin/sh
#pre-commit
set -eu

echo "Running project formatter..."

if command -v pwsh >/dev/null 2>&1; then
    pwsh -NoProfile -File "tools/format_code.ps1"
elif command -v powershell.exe >/dev/null 2>&1; then
    powershell.exe -NoProfile -ExecutionPolicy Bypass -File "tools/format_code.ps1"
elif command -v powershell >/dev/null 2>&1; then
    powershell -NoProfile -ExecutionPolicy Bypass -File "tools/format_code.ps1"
else
    echo "PowerShell was not found. Run tools/format_code.ps1 manually before committing."
    exit 1
fi

if ! git diff --quiet --; then
    echo ""
    echo "Formatting changed files or unstaged changes are present."
    echo "Review the diff, stage the formatted files, then commit again:"
    echo ""
    echo "  git diff"
    echo "  git add <files>"
    echo "  git commit"
    echo ""
    git diff --name-only --
    exit 1
fi

echo "Formatting check passed."

使用git hook可以在git操作的各个阶段进行操作。

例如我这里的pre-commit可以在commit提交之前执行脚本。

这里执行了一次clang-format,判断代码是否格式化。

使用社交账号登录

  • Loading...
  • Loading...
  • Loading...
  • Loading...
  • Loading...