@wipcomputer/wip-branch-guard 1.9.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/INSTALL.md +41 -0
- package/guard.mjs +259 -0
- package/package.json +11 -0
package/INSTALL.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# wip-branch-guard Installation
|
|
2
|
+
|
|
3
|
+
Add this hook to `~/.claude/settings.json` under `hooks.PreToolUse`:
|
|
4
|
+
|
|
5
|
+
```json
|
|
6
|
+
{
|
|
7
|
+
"matcher": "Write|Edit|NotebookEdit|Bash",
|
|
8
|
+
"hooks": [
|
|
9
|
+
{
|
|
10
|
+
"type": "command",
|
|
11
|
+
"command": "node /Users/lesa/.ldm/extensions/wip-branch-guard/guard.mjs",
|
|
12
|
+
"timeout": 5
|
|
13
|
+
}
|
|
14
|
+
]
|
|
15
|
+
}
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Then copy the guard to the extensions directory:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
mkdir -p ~/.ldm/extensions/wip-branch-guard
|
|
22
|
+
cp guard.mjs package.json ~/.ldm/extensions/wip-branch-guard/
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## What it does
|
|
26
|
+
|
|
27
|
+
Blocks ALL file writes and git commits when Claude Code is on main branch.
|
|
28
|
+
Agents must create a branch or use a worktree before editing anything.
|
|
29
|
+
|
|
30
|
+
## What it allows on main
|
|
31
|
+
|
|
32
|
+
- Read, Glob, Grep (read-only tools)
|
|
33
|
+
- git status, git log, git diff, git branch, git checkout, git pull, git merge, git push
|
|
34
|
+
- gh commands (issues, PRs, releases)
|
|
35
|
+
- Opening files in browser/mdview
|
|
36
|
+
|
|
37
|
+
## Test
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
node ~/.ldm/extensions/wip-branch-guard/guard.mjs --check
|
|
41
|
+
```
|
package/guard.mjs
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// wip-branch-guard/guard.mjs
|
|
3
|
+
// PreToolUse hook for Claude Code.
|
|
4
|
+
// Blocks ALL file writes and git commits when on main branch.
|
|
5
|
+
// Agents must work on branches or worktrees. Never on main.
|
|
6
|
+
|
|
7
|
+
import { execSync } from 'node:child_process';
|
|
8
|
+
import { dirname } from 'node:path';
|
|
9
|
+
import { statSync } from 'node:fs';
|
|
10
|
+
|
|
11
|
+
// Tools that modify files or git state
|
|
12
|
+
const WRITE_TOOLS = new Set(['Write', 'Edit', 'NotebookEdit']);
|
|
13
|
+
const BASH_TOOL = 'Bash';
|
|
14
|
+
|
|
15
|
+
// Git commands that should be blocked on main
|
|
16
|
+
const BLOCKED_GIT_PATTERNS = [
|
|
17
|
+
/\bgit\s+commit\b/,
|
|
18
|
+
/\bgit\s+add\b/,
|
|
19
|
+
/\bgit\s+stash\b/,
|
|
20
|
+
/\bgit\s+reset\b/,
|
|
21
|
+
/\bgit\s+revert\b/,
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
// Git commands that are ALLOWED on main (read-only or merge operations)
|
|
25
|
+
const ALLOWED_GIT_PATTERNS = [
|
|
26
|
+
/\bgit\s+merge\b/,
|
|
27
|
+
/\bgit\s+pull\b/,
|
|
28
|
+
/\bgit\s+fetch\b/,
|
|
29
|
+
/\bgit\s+push\b/,
|
|
30
|
+
/\bgit\s+status\b/,
|
|
31
|
+
/\bgit\s+log\b/,
|
|
32
|
+
/\bgit\s+diff\b/,
|
|
33
|
+
/\bgit\s+branch\b/,
|
|
34
|
+
/\bgit\s+checkout\b/,
|
|
35
|
+
/\bgit\s+worktree\b/,
|
|
36
|
+
/\bgit\s+stash\s+drop\b/,
|
|
37
|
+
/\bgit\s+stash\s+list\b/,
|
|
38
|
+
/\bgit\s+remote\b/,
|
|
39
|
+
/\bgit\s+describe\b/,
|
|
40
|
+
/\bgit\s+tag\b/,
|
|
41
|
+
/\bgit\s+rev-parse\b/,
|
|
42
|
+
/\bgit\s+show\b/,
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
// Non-git bash commands that write files (common patterns)
|
|
46
|
+
const BLOCKED_BASH_PATTERNS = [
|
|
47
|
+
/\bcp\s+/,
|
|
48
|
+
/\bmv\s+/,
|
|
49
|
+
/\brm\s+/,
|
|
50
|
+
/\bmkdir\s+/,
|
|
51
|
+
/\btouch\s+/,
|
|
52
|
+
/\bnpm\s+link\b/,
|
|
53
|
+
/\bnpm\s+install\s+-g\b/,
|
|
54
|
+
/>\s/, // redirects
|
|
55
|
+
/\btee\s+/,
|
|
56
|
+
/\bsed\s+-i/,
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
// Allowed bash patterns (read-only operations, even though they match blocked patterns)
|
|
60
|
+
const ALLOWED_BASH_PATTERNS = [
|
|
61
|
+
/\bls\b/,
|
|
62
|
+
/\bcat\b/,
|
|
63
|
+
/\bhead\b/,
|
|
64
|
+
/\btail\b/,
|
|
65
|
+
/\bgrep\b/,
|
|
66
|
+
/\brg\b/,
|
|
67
|
+
/\bfind\b/,
|
|
68
|
+
/\bwc\b/,
|
|
69
|
+
/\becho\b/,
|
|
70
|
+
/\bcurl\b/,
|
|
71
|
+
/\bgh\s+(issue|pr|release|api)\b/,
|
|
72
|
+
/\bgh\s+pr\s+merge\b/,
|
|
73
|
+
/\bnode\s+-e\b/,
|
|
74
|
+
/\blsof\b/,
|
|
75
|
+
/\bopen\s+-a\b/,
|
|
76
|
+
/\bpwd\b/,
|
|
77
|
+
/--dry-run/,
|
|
78
|
+
/--help/,
|
|
79
|
+
/\bwip-release\b.*--dry-run/,
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
function deny(reason) {
|
|
83
|
+
const output = {
|
|
84
|
+
hookSpecificOutput: {
|
|
85
|
+
hookEventName: 'PreToolUse',
|
|
86
|
+
permissionDecision: 'deny',
|
|
87
|
+
permissionDecisionReason: reason,
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
process.stdout.write(JSON.stringify(output));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function findRepoRoot(filePath) {
|
|
94
|
+
// Walk up from a file path to find the git repo root
|
|
95
|
+
try {
|
|
96
|
+
let dir = filePath;
|
|
97
|
+
// If it's a file, start from its directory
|
|
98
|
+
try {
|
|
99
|
+
if (statSync(dir).isFile()) dir = dirname(dir);
|
|
100
|
+
} catch {
|
|
101
|
+
dir = dirname(dir); // File might not exist yet
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Use git rev-parse from the directory
|
|
105
|
+
const result = execSync('git rev-parse --show-toplevel 2>/dev/null', {
|
|
106
|
+
cwd: dir,
|
|
107
|
+
encoding: 'utf8',
|
|
108
|
+
timeout: 3000,
|
|
109
|
+
}).trim();
|
|
110
|
+
return result;
|
|
111
|
+
} catch {}
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function getCurrentBranch(cwd) {
|
|
116
|
+
try {
|
|
117
|
+
return execSync('git branch --show-current 2>/dev/null', {
|
|
118
|
+
cwd: cwd || process.cwd(),
|
|
119
|
+
encoding: 'utf8',
|
|
120
|
+
timeout: 3000,
|
|
121
|
+
}).trim();
|
|
122
|
+
} catch {
|
|
123
|
+
return null; // Not in a git repo
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function isInWorktree(cwd) {
|
|
128
|
+
try {
|
|
129
|
+
const gitDir = execSync('git rev-parse --git-dir 2>/dev/null', {
|
|
130
|
+
cwd: cwd || process.cwd(),
|
|
131
|
+
encoding: 'utf8',
|
|
132
|
+
timeout: 3000,
|
|
133
|
+
}).trim();
|
|
134
|
+
return gitDir.includes('/worktrees/');
|
|
135
|
+
} catch {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// CLI mode
|
|
141
|
+
if (process.argv.includes('--check')) {
|
|
142
|
+
const branch = getCurrentBranch();
|
|
143
|
+
const worktree = isInWorktree();
|
|
144
|
+
console.log(`Branch: ${branch || '(not in git repo)'}`);
|
|
145
|
+
console.log(`Worktree: ${worktree ? 'yes' : 'no'}`);
|
|
146
|
+
console.log(`Status: ${branch === 'main' || branch === 'master' ? 'BLOCKED (on main)' : 'OK'}`);
|
|
147
|
+
process.exit(branch === 'main' || branch === 'master' ? 1 : 0);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async function main() {
|
|
151
|
+
let raw = '';
|
|
152
|
+
for await (const chunk of process.stdin) {
|
|
153
|
+
raw += chunk;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
let input;
|
|
157
|
+
try {
|
|
158
|
+
input = JSON.parse(raw);
|
|
159
|
+
} catch {
|
|
160
|
+
process.exit(0);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const toolName = input.tool_name || '';
|
|
164
|
+
const toolInput = input.tool_input || {};
|
|
165
|
+
|
|
166
|
+
// Determine which repo to check.
|
|
167
|
+
// Claude Code always opens in .openclaw, but edits files in other repos.
|
|
168
|
+
// We need to check the branch of THE REPO THE FILE LIVES IN, not the CWD.
|
|
169
|
+
const filePath = toolInput.file_path || toolInput.filePath || '';
|
|
170
|
+
const command = toolInput.command || '';
|
|
171
|
+
|
|
172
|
+
// For Write/Edit: derive repo from the file path
|
|
173
|
+
// For Bash: try to extract repo path from the command (cd, or file paths in args)
|
|
174
|
+
let repoDir = null;
|
|
175
|
+
|
|
176
|
+
if (filePath) {
|
|
177
|
+
// Walk up from file path to find .git directory
|
|
178
|
+
repoDir = findRepoRoot(filePath);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (!repoDir && command) {
|
|
182
|
+
// Try to extract a path from the bash command
|
|
183
|
+
// Common patterns: cd "/path/to/repo" && ..., or paths in arguments
|
|
184
|
+
const cdMatch = command.match(/cd\s+["']?([^"'&|;]+?)["']?\s*(?:&&|;|$)/);
|
|
185
|
+
if (cdMatch) {
|
|
186
|
+
repoDir = findRepoRoot(cdMatch[1].trim());
|
|
187
|
+
}
|
|
188
|
+
// Also check for git -C /path/to/repo
|
|
189
|
+
const gitCMatch = command.match(/git\s+-C\s+["']?([^"'&|;]+?)["']?\s/);
|
|
190
|
+
if (!repoDir && gitCMatch) {
|
|
191
|
+
repoDir = findRepoRoot(gitCMatch[1].trim());
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Fall back to CWD
|
|
196
|
+
if (!repoDir) {
|
|
197
|
+
repoDir = process.env.CWD || process.cwd();
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Check if the target repo is on main
|
|
201
|
+
const branch = getCurrentBranch(repoDir);
|
|
202
|
+
if (!branch || (branch !== 'main' && branch !== 'master')) {
|
|
203
|
+
// Not on main, allow everything
|
|
204
|
+
process.exit(0);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// We're on main. Check if this is a write operation.
|
|
208
|
+
|
|
209
|
+
// Block Write/Edit tools entirely on main
|
|
210
|
+
if (WRITE_TOOLS.has(toolName)) {
|
|
211
|
+
deny(`BLOCKED: Cannot ${toolName} while on main branch. Create a branch first: git checkout -b cc-mini/your-feature. Or use a worktree.`);
|
|
212
|
+
process.exit(0);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// For Bash, check the command
|
|
216
|
+
if (toolName === BASH_TOOL && command) {
|
|
217
|
+
// First check if it's explicitly allowed (read-only)
|
|
218
|
+
for (const pattern of ALLOWED_BASH_PATTERNS) {
|
|
219
|
+
if (pattern.test(command)) {
|
|
220
|
+
process.exit(0);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Check for blocked git commands
|
|
225
|
+
for (const pattern of BLOCKED_GIT_PATTERNS) {
|
|
226
|
+
if (pattern.test(command)) {
|
|
227
|
+
// Make sure it's not an allowed git operation
|
|
228
|
+
let isAllowed = false;
|
|
229
|
+
for (const ap of ALLOWED_GIT_PATTERNS) {
|
|
230
|
+
if (ap.test(command)) { isAllowed = true; break; }
|
|
231
|
+
}
|
|
232
|
+
if (!isAllowed) {
|
|
233
|
+
deny(`BLOCKED: Cannot run "${command.substring(0, 60)}..." on main branch. Create a branch first.`);
|
|
234
|
+
process.exit(0);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Check for file-writing bash commands
|
|
240
|
+
for (const pattern of BLOCKED_BASH_PATTERNS) {
|
|
241
|
+
if (pattern.test(command)) {
|
|
242
|
+
// Check it's not a read-only context
|
|
243
|
+
let isAllowed = false;
|
|
244
|
+
for (const ap of ALLOWED_BASH_PATTERNS) {
|
|
245
|
+
if (ap.test(command)) { isAllowed = true; break; }
|
|
246
|
+
}
|
|
247
|
+
if (!isAllowed) {
|
|
248
|
+
deny(`BLOCKED: Cannot run file-modifying command on main branch. Create a branch first.`);
|
|
249
|
+
process.exit(0);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Allow everything else (Read, Glob, Grep, Agent, etc.)
|
|
256
|
+
process.exit(0);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
main().catch(() => process.exit(0));
|
package/package.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wipcomputer/wip-branch-guard",
|
|
3
|
+
"version": "1.9.17",
|
|
4
|
+
"description": "PreToolUse hook that blocks all writes on main branch. Forces agents to work on branches or worktrees.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "guard.mjs",
|
|
7
|
+
"bin": {
|
|
8
|
+
"wip-branch-guard": "guard.mjs"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT"
|
|
11
|
+
}
|