原文(日本語に翻訳)
パーミッションルール構文(例: Bash(git *))を使用したフック向けの条件 if フィールドを追加。フックの実行タイミングをフィルタリングし、プロセス起動のオーバーヘッドを削減します。
原文(英語)
Added conditional if field for hooks using permission rule syntax (e.g., Bash(git *)) to filter when they run, reducing process spawning overhead
概要
フックの設定に if フィールドが追加され、パーミッションルール構文を使って特定のコマンドやツールが実行された場合にのみフックを起動できるようになりました。これにより、不要なプロセスの起動を抑制し、パフォーマンスが向上します。例えば Bash(git *) と指定すると、gitコマンドの実行時のみフックが動作します。
基本的な使い方
.claude/settings.json のフック設定に if フィールドを追加します:
json
{
"hooks": {
"PreToolUse": [
{
"if": "Bash(git *)",
"command": "echo 'git command detected'"
}
]
}
}実践例
ユースケース: gitコマンド実行時のみ通知する
json
{
"hooks": {
"PreToolUse": [
{
"if": "Bash(git commit*)",
"command": "/usr/local/bin/notify-git-commit.sh"
}
]
}
}ユースケース: 特定のファイル操作のみ監視する
json
{
"hooks": {
"PreToolUse": [
{
"if": "Write(*.env*)",
"command": "/usr/local/bin/warn-env-write.sh"
}
],
"PostToolUse": [
{
"if": "Bash(npm *)",
"command": "/usr/local/bin/log-npm-command.sh"
}
]
}
}ユースケース: 複数の条件でフックを設定する
json
{
"hooks": {
"PreToolUse": [
{
"if": "Bash(rm *)",
"command": "/usr/local/bin/warn-delete.sh"
},
{
"if": "Bash(sudo *)",
"command": "/usr/local/bin/log-sudo.sh"
}
]
}
}注意点
ifフィールドはパーミッションルールと同じ構文(ToolName(pattern))を使用します- 条件が一致しない場合、フックスクリプトは起動されないため、プロセス起動コストを節約できます
ifフィールドを省略した場合は従来通りすべてのツール使用時にフックが実行されます- ワイルドカード
*を使ったパターンマッチングが利用可能です