From 553eed3b5d4caadda3e5ac82a460fc69468c7821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=20=E9=A7=BF=E7=94=AB=20=28Shunsuke=20Hayashi=29?= Date: Fri, 10 Apr 2026 07:04:07 +0900 Subject: [PATCH] =?UTF-8?q?[=E8=BF=BD=E5=8A=A0]=20Git=20hooks:=20pre-commi?= =?UTF-8?q?t=20=E3=83=AD=E3=83=83=E3=82=AF=E6=A4=9C=E8=A8=BC=20+=20post-co?= =?UTF-8?q?mmit=20Issue=20=E8=87=AA=E5=8B=95=E6=9B=B4=E6=96=B0=20(#66)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit scripts/pre-commit-lock-check.sh: - コミット前にファイルロックを検証 - POLARIS_AGENT_ID 環境変数でエージェント識別 - ロック保持者以外の変更を exit 1 で拒否 scripts/post-commit-issue-update.sh: - コミットメッセージから #N を抽出 - gh issue comment で自動更新 - cargo test 結果も添付 - バックグラウンド実行(コミットをブロックしない) Co-Authored-By: Claude Opus 4.6 (1M context) --- scripts/post-commit-issue-update.sh | 36 ++++++++++++++++ scripts/pre-commit-lock-check.sh | 64 +++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100755 scripts/post-commit-issue-update.sh create mode 100755 scripts/pre-commit-lock-check.sh diff --git a/scripts/post-commit-issue-update.sh b/scripts/post-commit-issue-update.sh new file mode 100755 index 0000000..4e8068e --- /dev/null +++ b/scripts/post-commit-issue-update.sh @@ -0,0 +1,36 @@ +#!/bin/bash +# Polaris: コミット後に関連 Issue を自動更新する post-commit hook +# +# コミットメッセージに (#52, #53) のような Issue 番号があれば、 +# 自動で gh issue comment を実行する。 + +REPO="Miyabi-G-K/miyabi-cli-standalone" + +# 直前のコミット情報を取得 +COMMIT_HASH=$(git rev-parse --short HEAD) +COMMIT_MSG=$(git log -1 --pretty=%B) +AUTHOR=$(git log -1 --pretty=%an) + +# コミットメッセージから Issue 番号を抽出 (#52, #53 等) +ISSUE_NUMS=$(echo "$COMMIT_MSG" | grep -oE '#[0-9]+' | sed 's/#//') + +if [ -z "$ISSUE_NUMS" ]; then + exit 0 # Issue 番号がなければ何もしない +fi + +# 各 Issue にコメントを投稿 +for NUM in $ISSUE_NUMS; do + gh issue comment "$NUM" \ + --repo "$REPO" \ + --body "✅ **コミット反映**: \`$COMMIT_HASH\` by $AUTHOR + +\`\`\` +$COMMIT_MSG +\`\`\` + +cargo test: $(cargo test 2>&1 | grep 'test result' | tail -1)" \ + 2>/dev/null & +done + +# バックグラウンドで実行(コミットをブロックしない) +wait diff --git a/scripts/pre-commit-lock-check.sh b/scripts/pre-commit-lock-check.sh new file mode 100755 index 0000000..b544b8c --- /dev/null +++ b/scripts/pre-commit-lock-check.sh @@ -0,0 +1,64 @@ +#!/bin/bash +# Polaris: コミット前にファイルロックを検証する pre-commit hook +# +# ロックされているファイルを、ロック保持者以外が変更しようとしたら拒否する。 + +STORE="$(git rev-parse --show-toplevel)/project_memory/tasks.json" +MIYABI_BIN="$(git rev-parse --show-toplevel)/target/release/miyabi" + +# miyabi バイナリがなければスキップ(ビルド前) +if [ ! -x "$MIYABI_BIN" ]; then + exit 0 +fi + +# 変更されたファイル一覧 +CHANGED_FILES=$(git diff --cached --name-only) + +if [ -z "$CHANGED_FILES" ]; then + exit 0 +fi + +# ロック一覧を取得 +LOCKS=$("$MIYABI_BIN" gate --store-path "$STORE" --format json locks 2>/dev/null) + +if [ -z "$LOCKS" ] || [ "$LOCKS" = "[]" ] || [ "$LOCKS" = "{}" ]; then + exit 0 # ロックなし +fi + +# 現在のエージェント ID(環境変数から取得、なければチェックしない) +AGENT_ID="${POLARIS_AGENT_ID:-}" + +if [ -z "$AGENT_ID" ]; then + exit 0 # エージェント ID 未設定ならチェックスキップ +fi + +# 各変更ファイルのロック状態を確認 +BLOCKED=0 +for FILE in $CHANGED_FILES; do + LOCK_OWNER=$(echo "$LOCKS" | python3 -c " +import sys, json +try: + data = json.load(sys.stdin) + if isinstance(data, dict): + entry = data.get('$FILE', {}) + if entry: + print(entry.get('agent', '') + '@' + entry.get('node', '')) +except: pass +" 2>/dev/null) + + if [ -n "$LOCK_OWNER" ] && [ "$LOCK_OWNER" != "$AGENT_ID" ]; then + echo "❌ BLOCKED: $FILE is locked by $LOCK_OWNER" + echo " You ($AGENT_ID) cannot modify this file." + echo " Run: miyabi gate assign --files \"$FILE\" to acquire lock" + BLOCKED=1 + fi +done + +if [ "$BLOCKED" -eq 1 ]; then + echo "" + echo "🔒 Commit rejected by Polaris file lock enforcement." + echo " Set POLARIS_AGENT_ID to identify yourself." + exit 1 +fi + +exit 0