Skip to content

Commit e577d54

Browse files
labkey-martypclaude
andcommitted
Cover PowerShell tool and more branch-creation forms in git-ask hook
Adds a PreToolUse hook entry with matcher "PowerShell" pointing at the same check-dangerous-commands.py script, and mirrors every Bash(...) permissions.ask rule to PowerShell(...). On Windows the agent's primary shell is PowerShell, so without these the git/gh write protections did not apply to the most common code path. Extends the branch-creation regex to also match git checkout -B, git switch --create / --force-create, and git branch -t / --track. New tests cover each variant plus a git switch --no-track negative case to confirm the regex does not falsely match non-creating switch invocations. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 295e5df commit e577d54

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

.claude/hooks/check-dangerous-commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def command_touches_secret(command: str) -> bool:
9191
"git branch -D detected — confirm before proceeding",
9292
),
9393
(
94-
r'\bgit\s+[^\n;&|]*?(?:checkout\s+-b|switch\s+-[cC]|branch\s+(?!-)\S+)\b',
94+
r'\bgit\s+[^\n;&|]*?(?:checkout\s+-[bB]|switch\s+(?:-[cC]|--(?:force-)?create)|branch\s+(?:(?!-)\S+|-t|--track))\b',
9595
"git branch creation detected — confirm name before proceeding",
9696
),
9797
(

.claude/hooks/test-hooks.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,14 @@ def tally(result):
269269
# ASK: git branch -D (force delete)
270270
("git branch -D", "git branch -D feature/foo", "ASK"),
271271

272+
# ASK: branch creation/reset variants beyond the basic -b / -c
273+
("git checkout -B (force create/reset)", "git checkout -B foo", "ASK"),
274+
("git checkout -B with start point", "git checkout -B foo origin/foo", "ASK"),
275+
("git switch --create (long form)", "git switch --create foo", "ASK"),
276+
("git switch --force-create (long force)", "git switch --force-create foo origin/foo", "ASK"),
277+
("git branch -t (track + create)", "git branch -t newname origin/main", "ASK"),
278+
("git branch --track (long form)", "git branch --track newname origin/main", "ASK"),
279+
272280
# ASK: gh pr write actions
273281
("gh pr create", "gh pr create --title foo --body bar", "ASK"),
274282
("gh pr edit", "gh pr edit 123 --body foo", "ASK"),
@@ -300,6 +308,8 @@ def tally(result):
300308
("git reset HEAD~1 (no --hard)", "git reset HEAD~1", "ALLOW"),
301309
("git stash", "git stash", "ALLOW"),
302310
("git checkout main", "git checkout main", "ALLOW"),
311+
("git switch --no-track (no create flag)", "git switch --no-track foo", "ALLOW"),
312+
("git switch existing branch", "git switch main", "ALLOW"),
303313
("gh pr view", "gh pr view 123", "ALLOW"),
304314
("gh pr diff", "gh pr diff 123", "ALLOW"),
305315
("gh pr list", "gh pr list", "ALLOW"),

.claude/settings.json

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,29 @@
66
"Bash(git reset --hard:*)",
77
"Bash(git branch -D:*)",
88
"Bash(git checkout -b:*)",
9+
"Bash(git checkout -B:*)",
910
"Bash(git switch -c:*)",
1011
"Bash(git switch -C:*)",
12+
"Bash(git switch --create:*)",
13+
"Bash(git switch --force-create:*)",
1114
"Bash(gh pr create:*)",
1215
"Bash(gh pr edit:*)",
1316
"Bash(gh pr merge:*)",
14-
"Bash(gh pr close:*)"
17+
"Bash(gh pr close:*)",
18+
"PowerShell(git push:*)",
19+
"PowerShell(git commit:*)",
20+
"PowerShell(git reset --hard:*)",
21+
"PowerShell(git branch -D:*)",
22+
"PowerShell(git checkout -b:*)",
23+
"PowerShell(git checkout -B:*)",
24+
"PowerShell(git switch -c:*)",
25+
"PowerShell(git switch -C:*)",
26+
"PowerShell(git switch --create:*)",
27+
"PowerShell(git switch --force-create:*)",
28+
"PowerShell(gh pr create:*)",
29+
"PowerShell(gh pr edit:*)",
30+
"PowerShell(gh pr merge:*)",
31+
"PowerShell(gh pr close:*)"
1532
]
1633
},
1734
"hooks": {
@@ -25,6 +42,15 @@
2542
}
2643
]
2744
},
45+
{
46+
"matcher": "PowerShell",
47+
"hooks": [
48+
{
49+
"type": "command",
50+
"command": "cd \"$CLAUDE_PROJECT_DIR\" && if command -v python3 >/dev/null 2>&1; then python3 .claude/hooks/check-dangerous-commands.py; else python .claude/hooks/check-dangerous-commands.py; fi"
51+
}
52+
]
53+
},
2854
{
2955
"matcher": "Read|Edit|Write|MultiEdit|Grep|Glob",
3056
"hooks": [
@@ -37,4 +63,3 @@
3763
]
3864
}
3965
}
40-

0 commit comments

Comments
 (0)