@danielstyle/featflow 0.0.0-stage → 0.6.3
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/LICENSE +21 -0
- package/README.md +206 -2
- package/README.zh-CN.md +163 -0
- package/bin/featflow.mjs +113 -0
- package/package.json +62 -4
- package/skills/featflow/SKILL.md +127 -0
- package/skills/featflow-issues/SKILL.md +104 -0
- package/skills/featflow-regression/SKILL.md +63 -0
- package/src/agent-docs.mjs +143 -0
- package/src/changed-files.mjs +204 -0
- package/src/commands/cleanup.mjs +221 -0
- package/src/commands/codegraph.mjs +110 -0
- package/src/commands/doctor.mjs +265 -0
- package/src/commands/finish.mjs +249 -0
- package/src/commands/hook-cmd.mjs +78 -0
- package/src/commands/hooks-cmd.mjs +238 -0
- package/src/commands/init.mjs +382 -0
- package/src/commands/issue.mjs +434 -0
- package/src/commands/new.mjs +311 -0
- package/src/commands/plan-check.mjs +75 -0
- package/src/commands/policy-cmd.mjs +51 -0
- package/src/commands/pr-cleanup.mjs +194 -0
- package/src/commands/regression.mjs +206 -0
- package/src/commands/setup.mjs +343 -0
- package/src/commands/size-check.mjs +137 -0
- package/src/commands/status.mjs +100 -0
- package/src/commands/sync.mjs +407 -0
- package/src/commands/verify.mjs +206 -0
- package/src/commands/worktree.mjs +205 -0
- package/src/config.mjs +295 -0
- package/src/detect.mjs +314 -0
- package/src/git.mjs +331 -0
- package/src/hook-adapters.mjs +218 -0
- package/src/hooks-core.mjs +420 -0
- package/src/index.mjs +13 -0
- package/src/init-args.mjs +282 -0
- package/src/init-hooks.mjs +312 -0
- package/src/init-prompts.mjs +191 -0
- package/src/issues-helpers.mjs +463 -0
- package/src/issues.mjs +521 -0
- package/src/plan.mjs +202 -0
- package/src/policy.mjs +143 -0
- package/src/regression.mjs +338 -0
- package/src/size-check.mjs +203 -0
- package/src/tui-select.mjs +378 -0
- package/templates/adapters/antigravity/hooks.json +42 -0
- package/templates/adapters/claude-code/settings.json +46 -0
- package/templates/adapters/codex/config.toml +19 -0
- package/templates/adapters/cursor/hooks.json +27 -0
- package/templates/agents/codex/codebase-locator.toml +28 -0
- package/templates/agents/codex/implementation-scout.toml +28 -0
- package/templates/agents/codex/regression-guard-planner.toml +20 -0
- package/templates/features/README.md +56 -0
- package/templates/features/_program_template.md +79 -0
- package/templates/features/_template.md +120 -0
- package/templates/features/current-feature.md +18 -0
- package/templates/githooks/post-merge +7 -0
- package/templates/githooks/pre-commit +11 -0
- package/templates/githooks/pre-push +10 -0
- package/templates/regression/regression-ledger.yml +23 -0
- package/templates/snippets/AGENTS-workflow.md +21 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
import { loadConfig, resolveWorktreeParent } from "../config.mjs";
|
|
4
|
+
import {
|
|
5
|
+
ensureCleanWorktree,
|
|
6
|
+
fail,
|
|
7
|
+
findWorktreeForBranch,
|
|
8
|
+
gitConfigGet,
|
|
9
|
+
gitWorktrees,
|
|
10
|
+
isInsidePath,
|
|
11
|
+
localBranchExists,
|
|
12
|
+
remoteExists,
|
|
13
|
+
remoteTrackingBranchExists,
|
|
14
|
+
runWrite,
|
|
15
|
+
sanitizeBranchForDir,
|
|
16
|
+
validateTaskBranchName,
|
|
17
|
+
} from "../git.mjs";
|
|
18
|
+
import { setupInstructions } from "../policy.mjs";
|
|
19
|
+
import { cleanupMergedTaskBranches } from "./cleanup.mjs";
|
|
20
|
+
|
|
21
|
+
const usage = `
|
|
22
|
+
Usage:
|
|
23
|
+
featflow finish <task-branch> [options]
|
|
24
|
+
|
|
25
|
+
Completes a verified task branch:
|
|
26
|
+
- In developer-branch mode: merges into your personal developer branch,
|
|
27
|
+
pushes it (unless --no-push or offline), and removes the clean task worktree & branch.
|
|
28
|
+
- In trunk mode (finishTarget=merge): merges into baseBranch, pushes if remote exists
|
|
29
|
+
(unless --no-push), and removes the clean task worktree & branch.
|
|
30
|
+
- In trunk mode (finishTarget=pr or --pr): pushes the task branch so you can open a PR,
|
|
31
|
+
keeping the worktree until "featflow pr-cleanup <pr>" runs after merge.
|
|
32
|
+
|
|
33
|
+
Options:
|
|
34
|
+
--developer <branch> Override configured developer branch.
|
|
35
|
+
--remote <name> Remote to fetch/push. Defaults to config.remote.
|
|
36
|
+
--worktree <path> Override the task worktree path.
|
|
37
|
+
--pr Push task branch for a pull request instead of merging locally.
|
|
38
|
+
--no-push Skip fetching from and pushing to the remote.
|
|
39
|
+
--dry-run Print mutating commands without running them.
|
|
40
|
+
--keep-worktree Do not remove the task worktree.
|
|
41
|
+
--keep-branch Do not delete the local task branch.
|
|
42
|
+
-h, --help Show this help.
|
|
43
|
+
`.trim();
|
|
44
|
+
|
|
45
|
+
function parseArgs(argv) {
|
|
46
|
+
const options = {
|
|
47
|
+
developer: "",
|
|
48
|
+
dryRun: false,
|
|
49
|
+
keepBranch: false,
|
|
50
|
+
keepWorktree: false,
|
|
51
|
+
noPush: false,
|
|
52
|
+
pr: false,
|
|
53
|
+
remote: "",
|
|
54
|
+
taskBranch: "",
|
|
55
|
+
worktree: "",
|
|
56
|
+
};
|
|
57
|
+
const positional = [];
|
|
58
|
+
|
|
59
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
60
|
+
const arg = argv[i];
|
|
61
|
+
if (arg === "-h" || arg === "--help") {
|
|
62
|
+
console.log(usage);
|
|
63
|
+
process.exit(0);
|
|
64
|
+
}
|
|
65
|
+
if (arg === "--dry-run") {
|
|
66
|
+
options.dryRun = true;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (arg === "--no-push") {
|
|
70
|
+
options.noPush = true;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (arg === "--pr") {
|
|
74
|
+
options.pr = true;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (arg === "--keep-worktree") {
|
|
78
|
+
options.keepWorktree = true;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (arg === "--keep-branch") {
|
|
82
|
+
options.keepBranch = true;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (arg === "--developer" || arg === "--remote" || arg === "--worktree") {
|
|
86
|
+
options[arg.slice(2)] = argv[++i] ?? "";
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (arg.startsWith("--developer=") || arg.startsWith("--remote=") || arg.startsWith("--worktree=")) {
|
|
90
|
+
const idx = arg.indexOf("=");
|
|
91
|
+
options[arg.slice(2, idx)] = arg.slice(idx + 1);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (arg.startsWith("-")) {
|
|
95
|
+
fail(`Unknown option: ${arg}`, usage);
|
|
96
|
+
}
|
|
97
|
+
positional.push(arg);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (positional.length !== 1) {
|
|
101
|
+
fail("Expected exactly one task branch name.", usage);
|
|
102
|
+
}
|
|
103
|
+
options.taskBranch = positional[0];
|
|
104
|
+
return options;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function ensureTargetWorktree(repoRoot, config, targetBranch, options) {
|
|
108
|
+
let worktrees = gitWorktrees(repoRoot);
|
|
109
|
+
let targetWorktree = findWorktreeForBranch(worktrees, targetBranch);
|
|
110
|
+
if (targetWorktree) {
|
|
111
|
+
return targetWorktree;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const parent = resolveWorktreeParent(repoRoot, config);
|
|
115
|
+
const worktreePath = path.join(parent, sanitizeBranchForDir(targetBranch));
|
|
116
|
+
runWrite("git", ["worktree", "add", worktreePath, targetBranch], repoRoot, options);
|
|
117
|
+
if (options.dryRun) {
|
|
118
|
+
return { path: worktreePath, dryRunMissing: true };
|
|
119
|
+
}
|
|
120
|
+
worktrees = gitWorktrees(repoRoot);
|
|
121
|
+
targetWorktree = findWorktreeForBranch(worktrees, targetBranch);
|
|
122
|
+
if (!targetWorktree) {
|
|
123
|
+
throw new Error(`Could not find worktree for ${targetBranch} after creating ${worktreePath}.`);
|
|
124
|
+
}
|
|
125
|
+
return targetWorktree;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export async function runFinish(argv) {
|
|
129
|
+
const options = parseArgs(argv);
|
|
130
|
+
const { config, repoRoot } = loadConfig(process.cwd());
|
|
131
|
+
const remote = options.remote || config.remote;
|
|
132
|
+
|
|
133
|
+
try {
|
|
134
|
+
validateTaskBranchName(options.taskBranch, repoRoot, new Set(config.protectedBranches));
|
|
135
|
+
} catch (error) {
|
|
136
|
+
fail(error instanceof Error ? error.message : String(error));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (!localBranchExists(options.taskBranch, repoRoot)) {
|
|
140
|
+
fail(`Local task branch "${options.taskBranch}" does not exist.`);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const taskWorktree = options.worktree
|
|
144
|
+
? { path: path.resolve(options.worktree) }
|
|
145
|
+
: findWorktreeForBranch(gitWorktrees(repoRoot), options.taskBranch);
|
|
146
|
+
|
|
147
|
+
if (taskWorktree) {
|
|
148
|
+
ensureCleanWorktree(taskWorktree.path, "task");
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const hasRemote = !options.noPush && remoteExists(remote, repoRoot);
|
|
152
|
+
|
|
153
|
+
// PR-based finish (trunk + pr strategy or explicit --pr)
|
|
154
|
+
if (options.pr || (config.mode === "trunk" && config.finishTarget === "pr")) {
|
|
155
|
+
const pushCwd = taskWorktree?.path ?? repoRoot;
|
|
156
|
+
if (hasRemote) {
|
|
157
|
+
runWrite("git", ["push", "-u", remote, options.taskBranch], pushCwd, { dryRun: options.dryRun });
|
|
158
|
+
}
|
|
159
|
+
const base = config.mode === "developer-branch" ? config.integrationBranch : config.baseBranch;
|
|
160
|
+
console.log(`Task branch ready for pull request: ${options.taskBranch}`);
|
|
161
|
+
console.log(`Open PR: gh pr create --base ${base} --head ${options.taskBranch}`);
|
|
162
|
+
console.log("After merge, clean up locally with: featflow pr-cleanup <pr-number>");
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
let targetBranch = config.baseBranch;
|
|
167
|
+
let developerBranch = "";
|
|
168
|
+
|
|
169
|
+
if (config.mode === "developer-branch") {
|
|
170
|
+
developerBranch = options.developer || gitConfigGet(config.developerBranchConfigKey, repoRoot);
|
|
171
|
+
if (!developerBranch) {
|
|
172
|
+
fail(setupInstructions(config));
|
|
173
|
+
}
|
|
174
|
+
targetBranch = developerBranch;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (options.taskBranch === targetBranch) {
|
|
178
|
+
fail(`Task branch and target branch (${targetBranch}) must be different.`);
|
|
179
|
+
}
|
|
180
|
+
if (!localBranchExists(targetBranch, repoRoot)) {
|
|
181
|
+
fail(`Local target branch "${targetBranch}" does not exist.`);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const targetWorktree = ensureTargetWorktree(repoRoot, config, targetBranch, { dryRun: options.dryRun });
|
|
185
|
+
if (!targetWorktree.dryRunMissing) {
|
|
186
|
+
ensureCleanWorktree(targetWorktree.path, "target");
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const internalEnv = config.mode === "trunk" ? { FEATFLOW_INTERNAL_FINISH: "1" } : undefined;
|
|
190
|
+
|
|
191
|
+
if (hasRemote) {
|
|
192
|
+
runWrite("git", ["fetch", remote, targetBranch], targetWorktree.path, { dryRun: options.dryRun });
|
|
193
|
+
if (remoteTrackingBranchExists(remote, targetBranch, targetWorktree.path)) {
|
|
194
|
+
runWrite("git", ["merge", "--ff-only", `${remote}/${targetBranch}`], targetWorktree.path, {
|
|
195
|
+
dryRun: options.dryRun,
|
|
196
|
+
env: internalEnv,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Move out of task worktree BEFORE merging so post-merge hooks & worktree removal succeed
|
|
202
|
+
if (taskWorktree && !options.keepWorktree && isInsidePath(process.cwd(), taskWorktree.path)) {
|
|
203
|
+
process.chdir(targetWorktree.path);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
runWrite("git", ["merge", "--no-edit", options.taskBranch], targetWorktree.path, {
|
|
207
|
+
dryRun: options.dryRun,
|
|
208
|
+
env: internalEnv,
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
if (hasRemote) {
|
|
212
|
+
runWrite("git", ["push", remote, targetBranch], targetWorktree.path, {
|
|
213
|
+
dryRun: options.dryRun,
|
|
214
|
+
env: internalEnv,
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const remainingTaskWorktree = findWorktreeForBranch(gitWorktrees(targetWorktree.path), options.taskBranch);
|
|
219
|
+
if (remainingTaskWorktree && !options.keepWorktree) {
|
|
220
|
+
if (isInsidePath(process.cwd(), remainingTaskWorktree.path)) {
|
|
221
|
+
process.chdir(targetWorktree.path);
|
|
222
|
+
}
|
|
223
|
+
runWrite("git", ["worktree", "remove", remainingTaskWorktree.path], targetWorktree.path, {
|
|
224
|
+
dryRun: options.dryRun,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (!options.keepBranch && localBranchExists(options.taskBranch, targetWorktree.path)) {
|
|
229
|
+
runWrite("git", ["branch", "-d", options.taskBranch], targetWorktree.path, { dryRun: options.dryRun });
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
cleanupMergedTaskBranches({
|
|
233
|
+
cwd: targetWorktree.path,
|
|
234
|
+
config,
|
|
235
|
+
targetBranch,
|
|
236
|
+
developerBranch,
|
|
237
|
+
dryRun: options.dryRun,
|
|
238
|
+
excludeBranches: options.keepBranch || options.keepWorktree ? [options.taskBranch] : [],
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
const prefix = options.dryRun ? "Dry run complete:" : "Target branch updated:";
|
|
242
|
+
const suffix = hasRemote ? "" : " (local only)";
|
|
243
|
+
console.log(`${prefix} ${targetBranch}${suffix}`);
|
|
244
|
+
if (config.mode === "developer-branch") {
|
|
245
|
+
console.log(
|
|
246
|
+
`No PR was opened. Create a PR to ${config.integrationBranch} only when ${targetBranch} is ready for team integration.`,
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { loadConfig } from "../config.mjs";
|
|
2
|
+
import { fail } from "../git.mjs";
|
|
3
|
+
import {
|
|
4
|
+
dispatchHookAdapter,
|
|
5
|
+
resolveHookCwd,
|
|
6
|
+
SUPPORTED_HOOK_ADAPTERS,
|
|
7
|
+
SUPPORTED_HOOK_EVENTS,
|
|
8
|
+
} from "../hook-adapters.mjs";
|
|
9
|
+
|
|
10
|
+
const usage = `
|
|
11
|
+
Usage:
|
|
12
|
+
featflow hook <codex|claude-code|antigravity|cursor> <event>
|
|
13
|
+
|
|
14
|
+
Supported events:
|
|
15
|
+
codex / claude-code: pre-tool-use | post-tool-use | user-prompt | stop
|
|
16
|
+
antigravity: pre-invocation | pre-tool-use | post-tool-use | stop
|
|
17
|
+
cursor: before-submit-prompt | pre-tool-use | post-tool-use | stop
|
|
18
|
+
|
|
19
|
+
Reads hook JSON from stdin and emits the adapter-appropriate response and exit code.
|
|
20
|
+
`.trim();
|
|
21
|
+
|
|
22
|
+
async function readStdinJson() {
|
|
23
|
+
const chunks = [];
|
|
24
|
+
for await (const chunk of process.stdin) {
|
|
25
|
+
chunks.push(chunk);
|
|
26
|
+
}
|
|
27
|
+
const raw = Buffer.concat(chunks).toString("utf8").trim();
|
|
28
|
+
if (!raw) {
|
|
29
|
+
return {};
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
return JSON.parse(raw);
|
|
33
|
+
} catch {
|
|
34
|
+
return { _raw: raw };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function runHook(argv) {
|
|
39
|
+
const [adapter, event] = argv;
|
|
40
|
+
if (!adapter || adapter === "-h" || adapter === "--help") {
|
|
41
|
+
console.log(usage);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (!SUPPORTED_HOOK_ADAPTERS.includes(adapter)) {
|
|
46
|
+
fail(
|
|
47
|
+
`Unsupported hook adapter "${adapter}". Expected one of: ${SUPPORTED_HOOK_ADAPTERS.join(", ")}.`,
|
|
48
|
+
usage,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
const validEvents = SUPPORTED_HOOK_EVENTS[adapter] ?? [];
|
|
52
|
+
if (!validEvents.includes(event ?? "")) {
|
|
53
|
+
fail(
|
|
54
|
+
`Unsupported hook event "${event ?? ""}" for adapter "${adapter}". Expected one of: ${validEvents.join(", ")}.`,
|
|
55
|
+
usage,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const rawInput = await readStdinJson();
|
|
60
|
+
const startCwd = resolveHookCwd(rawInput, process.cwd());
|
|
61
|
+
const { config, repoRoot } = loadConfig(startCwd);
|
|
62
|
+
|
|
63
|
+
const outcome = dispatchHookAdapter(adapter, event, rawInput, {
|
|
64
|
+
repoRoot,
|
|
65
|
+
config,
|
|
66
|
+
env: process.env,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
if (outcome.stdout) {
|
|
70
|
+
process.stdout.write(outcome.stdout);
|
|
71
|
+
}
|
|
72
|
+
if (outcome.stderr) {
|
|
73
|
+
process.stderr.write(outcome.stderr);
|
|
74
|
+
}
|
|
75
|
+
if (outcome.exitCode && outcome.exitCode !== 0) {
|
|
76
|
+
process.exit(outcome.exitCode);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { chmodSync, copyFileSync, existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
import { packageRoot } from "../config.mjs";
|
|
5
|
+
import { fail, findGitCommonDir, findRepoRoot, gitConfigGet, gitConfigSet } from "../git.mjs";
|
|
6
|
+
|
|
7
|
+
const BLOCK_START = "# >>> featflow managed hook >>>";
|
|
8
|
+
const BLOCK_END = "# <<< featflow managed hook <<<";
|
|
9
|
+
|
|
10
|
+
const HOOK_SNIPPETS = {
|
|
11
|
+
"pre-commit": [
|
|
12
|
+
BLOCK_START,
|
|
13
|
+
'if [ -x "./node_modules/.bin/featflow" ]; then',
|
|
14
|
+
" ./node_modules/.bin/featflow policy pre-commit || exit $?",
|
|
15
|
+
"elif command -v featflow >/dev/null 2>&1; then",
|
|
16
|
+
" featflow policy pre-commit || exit $?",
|
|
17
|
+
"fi",
|
|
18
|
+
BLOCK_END,
|
|
19
|
+
].join("\n"),
|
|
20
|
+
"pre-push": [
|
|
21
|
+
BLOCK_START,
|
|
22
|
+
'if [ -x "./node_modules/.bin/featflow" ]; then',
|
|
23
|
+
" ./node_modules/.bin/featflow policy pre-push || exit $?",
|
|
24
|
+
"elif command -v featflow >/dev/null 2>&1; then",
|
|
25
|
+
" featflow policy pre-push || exit $?",
|
|
26
|
+
"fi",
|
|
27
|
+
BLOCK_END,
|
|
28
|
+
].join("\n"),
|
|
29
|
+
"post-merge": [
|
|
30
|
+
BLOCK_START,
|
|
31
|
+
'if [ -x "./node_modules/.bin/featflow" ]; then',
|
|
32
|
+
" ./node_modules/.bin/featflow cleanup --post-merge || true",
|
|
33
|
+
"elif command -v featflow >/dev/null 2>&1; then",
|
|
34
|
+
" featflow cleanup --post-merge || true",
|
|
35
|
+
"fi",
|
|
36
|
+
BLOCK_END,
|
|
37
|
+
].join("\n"),
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const usage = `
|
|
41
|
+
Usage:
|
|
42
|
+
featflow hooks install [--local|--repo]
|
|
43
|
+
featflow hooks uninstall [--local]
|
|
44
|
+
featflow hooks status
|
|
45
|
+
|
|
46
|
+
Manages Git hooks for featflow without polluting the target repository.
|
|
47
|
+
|
|
48
|
+
Modes:
|
|
49
|
+
--local (default) Install/remove managed blocks inside .git/hooks/ (zero tracked
|
|
50
|
+
files, preserves existing local hooks, works across worktrees).
|
|
51
|
+
--repo Copy tracked hooks to .githooks/ and safely configure core.hooksPath.
|
|
52
|
+
`.trim();
|
|
53
|
+
|
|
54
|
+
export function configureGitHooksSafely(repoRoot) {
|
|
55
|
+
const existingHooksPath = gitConfigGet("core.hooksPath", repoRoot);
|
|
56
|
+
if (!existingHooksPath || existingHooksPath === ".githooks") {
|
|
57
|
+
gitConfigSet("core.hooksPath", ".githooks", repoRoot);
|
|
58
|
+
return { configured: true, existingHooksPath: ".githooks" };
|
|
59
|
+
}
|
|
60
|
+
return { configured: false, existingHooksPath };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function escapeRegex(value) {
|
|
64
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function stripManagedBlock(content) {
|
|
68
|
+
const regex = new RegExp(`(?:\\r?\\n)*${escapeRegex(BLOCK_START)}[\\s\\S]*?${escapeRegex(BLOCK_END)}(?:\\r?\\n)?`, "g");
|
|
69
|
+
return content.replace(regex, "\n").replace(/\n{3,}/g, "\n\n").trimEnd();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function hasManagedBlock(filePath) {
|
|
73
|
+
if (!existsSync(filePath)) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
return readFileSync(filePath, "utf8").includes(BLOCK_START);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function installLocalHooks(repoRoot, options = {}) {
|
|
80
|
+
const quiet = Boolean(options.quiet);
|
|
81
|
+
const commonDir = findGitCommonDir(repoRoot);
|
|
82
|
+
if (!commonDir) {
|
|
83
|
+
fail("Could not resolve .git directory.");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const hooksPath = gitConfigGet("core.hooksPath", repoRoot);
|
|
87
|
+
if (hooksPath) {
|
|
88
|
+
if (!quiet) {
|
|
89
|
+
console.warn(`Warning: core.hooksPath is set to "${hooksPath}", so Git ignores .git/hooks/.`);
|
|
90
|
+
console.warn("Add these calls to your existing hook manager instead:");
|
|
91
|
+
console.warn(" pre-commit: featflow policy pre-commit");
|
|
92
|
+
console.warn(" pre-push: featflow policy pre-push");
|
|
93
|
+
console.warn(" post-merge: featflow cleanup --post-merge || true");
|
|
94
|
+
}
|
|
95
|
+
return { installed: false, existingHooksPath: hooksPath, hooks: [], hooksDir: "" };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const hooksDir = path.join(commonDir, "hooks");
|
|
99
|
+
mkdirSync(hooksDir, { recursive: true });
|
|
100
|
+
|
|
101
|
+
const installed = [];
|
|
102
|
+
for (const [hookName, snippet] of Object.entries(HOOK_SNIPPETS)) {
|
|
103
|
+
const hookFile = path.join(hooksDir, hookName);
|
|
104
|
+
let nextContent;
|
|
105
|
+
if (existsSync(hookFile)) {
|
|
106
|
+
const existing = readFileSync(hookFile, "utf8");
|
|
107
|
+
const cleaned = stripManagedBlock(existing);
|
|
108
|
+
const base = cleaned.length > 0 ? cleaned : "#!/usr/bin/env sh";
|
|
109
|
+
nextContent = `${base}\n\n${snippet}\n`;
|
|
110
|
+
} else {
|
|
111
|
+
nextContent = `#!/usr/bin/env sh\n\n${snippet}\n`;
|
|
112
|
+
}
|
|
113
|
+
writeFileSync(hookFile, nextContent, "utf8");
|
|
114
|
+
chmodSync(hookFile, 0o755);
|
|
115
|
+
installed.push(hookName);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (!quiet) {
|
|
119
|
+
console.log(`Installed local featflow Git hooks in ${hooksDir}: ${installed.join(", ")}`);
|
|
120
|
+
console.log("Zero repository files were modified (existing custom hooks were preserved).");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return { installed: true, existingHooksPath: "", hooks: installed, hooksDir };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function uninstallLocalHooks(repoRoot) {
|
|
127
|
+
const commonDir = findGitCommonDir(repoRoot);
|
|
128
|
+
if (!commonDir) {
|
|
129
|
+
fail("Could not resolve .git directory.");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const hooksDir = path.join(commonDir, "hooks");
|
|
133
|
+
const removed = [];
|
|
134
|
+
const preserved = [];
|
|
135
|
+
|
|
136
|
+
for (const hookName of Object.keys(HOOK_SNIPPETS)) {
|
|
137
|
+
const hookFile = path.join(hooksDir, hookName);
|
|
138
|
+
if (!hasManagedBlock(hookFile)) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
const existing = readFileSync(hookFile, "utf8");
|
|
142
|
+
const cleaned = stripManagedBlock(existing).trim();
|
|
143
|
+
const isOnlyShebang = !cleaned || /^#!\/[^\r\n]+$/.test(cleaned);
|
|
144
|
+
if (isOnlyShebang) {
|
|
145
|
+
unlinkSync(hookFile);
|
|
146
|
+
removed.push(hookName);
|
|
147
|
+
} else {
|
|
148
|
+
writeFileSync(hookFile, `${cleaned}\n`, "utf8");
|
|
149
|
+
preserved.push(hookName);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (removed.length === 0 && preserved.length === 0) {
|
|
154
|
+
console.log("No local featflow Git hooks found in .git/hooks/.");
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (removed.length > 0) {
|
|
158
|
+
console.log(`Removed featflow-only local hooks: ${removed.join(", ")}`);
|
|
159
|
+
}
|
|
160
|
+
if (preserved.length > 0) {
|
|
161
|
+
console.log(`Removed featflow block while preserving custom hooks: ${preserved.join(", ")}`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function installRepoHooks(repoRoot) {
|
|
166
|
+
const srcDir = path.join(packageRoot, "templates", "githooks");
|
|
167
|
+
const destDir = path.join(repoRoot, ".githooks");
|
|
168
|
+
mkdirSync(destDir, { recursive: true });
|
|
169
|
+
for (const hookName of Object.keys(HOOK_SNIPPETS)) {
|
|
170
|
+
const dest = path.join(destDir, hookName);
|
|
171
|
+
copyFileSync(path.join(srcDir, hookName), dest);
|
|
172
|
+
chmodSync(dest, 0o755);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const result = configureGitHooksSafely(repoRoot);
|
|
176
|
+
if (result.configured) {
|
|
177
|
+
console.log("Installed tracked hooks in .githooks/ and set core.hooksPath=.githooks.");
|
|
178
|
+
} else {
|
|
179
|
+
console.log(`Copied hooks to .githooks/, but kept existing core.hooksPath=${result.existingHooksPath}.`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function showHookStatus(repoRoot) {
|
|
184
|
+
const commonDir = findGitCommonDir(repoRoot);
|
|
185
|
+
const hooksPath = gitConfigGet("core.hooksPath", repoRoot);
|
|
186
|
+
const localHooksDir = commonDir ? path.join(commonDir, "hooks") : "";
|
|
187
|
+
const localActive = localHooksDir
|
|
188
|
+
? Object.keys(HOOK_SNIPPETS).filter((name) => hasManagedBlock(path.join(localHooksDir, name)))
|
|
189
|
+
: [];
|
|
190
|
+
const repoHooksExist = existsSync(path.join(repoRoot, ".githooks", "pre-commit"));
|
|
191
|
+
|
|
192
|
+
console.log("featflow hooks status");
|
|
193
|
+
console.log(`core.hooksPath: ${hooksPath || "(default .git/hooks)"}`);
|
|
194
|
+
console.log(`Local .git/hooks managed blocks: ${localActive.length > 0 ? localActive.join(", ") : "(none)"}`);
|
|
195
|
+
console.log(`Tracked .githooks/ present: ${repoHooksExist ? "yes" : "no"}`);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export async function runHooks(argv) {
|
|
199
|
+
const action = argv[0] ?? "";
|
|
200
|
+
if (!action || action === "-h" || action === "--help") {
|
|
201
|
+
console.log(usage);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const rest = argv.slice(1);
|
|
206
|
+
if (rest.includes("-h") || rest.includes("--help")) {
|
|
207
|
+
console.log(usage);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const repoRoot = findRepoRoot(process.cwd());
|
|
212
|
+
if (!repoRoot) {
|
|
213
|
+
fail("Not inside a Git repository.");
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const useRepo = rest.includes("--repo");
|
|
217
|
+
|
|
218
|
+
if (action === "install") {
|
|
219
|
+
if (useRepo) {
|
|
220
|
+
installRepoHooks(repoRoot);
|
|
221
|
+
} else {
|
|
222
|
+
installLocalHooks(repoRoot);
|
|
223
|
+
}
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (action === "uninstall") {
|
|
228
|
+
uninstallLocalHooks(repoRoot);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (action === "status") {
|
|
233
|
+
showHookStatus(repoRoot);
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
fail(`Unknown hooks subcommand "${action}". Expected "install", "uninstall", or "status".`, usage);
|
|
238
|
+
}
|