@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,311 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
import { loadConfig, packageRoot } from "../config.mjs";
|
|
5
|
+
import {
|
|
6
|
+
currentBranch,
|
|
7
|
+
fail,
|
|
8
|
+
findGitCommonDir,
|
|
9
|
+
findRepoRoot,
|
|
10
|
+
gitConfigGet,
|
|
11
|
+
} from "../git.mjs";
|
|
12
|
+
|
|
13
|
+
const LOCAL_FEATURES_DIR = ".featflow/features";
|
|
14
|
+
|
|
15
|
+
const usage = `
|
|
16
|
+
Usage:
|
|
17
|
+
featflow new <slug> [options]
|
|
18
|
+
|
|
19
|
+
Creates a new feature (or program) record using the repository's template if
|
|
20
|
+
present, or featflow's built-in template — works even in repositories without
|
|
21
|
+
running "featflow init".
|
|
22
|
+
|
|
23
|
+
Options:
|
|
24
|
+
--title <text> Human-readable title (defaults to Title Case of <slug>).
|
|
25
|
+
--branch <name> Override tracked branch recorded in frontmatter & Branch Plan.
|
|
26
|
+
--status <status> Initial status (default: planned).
|
|
27
|
+
--program Create a multi-phase program record under programs/ instead of active/.
|
|
28
|
+
--local Write to .featflow/features/ and add to .git/info/exclude (zero tracked files).
|
|
29
|
+
--dir <path> Override features base directory.
|
|
30
|
+
--update-pointer Update current-feature.md pointer file when creating a feature record.
|
|
31
|
+
--template Print the resolved template to stdout without writing any file.
|
|
32
|
+
--dry-run Print target path without writing.
|
|
33
|
+
-h, --help Show this help.
|
|
34
|
+
`.trim();
|
|
35
|
+
|
|
36
|
+
function parseArgs(argv) {
|
|
37
|
+
const options = {
|
|
38
|
+
slug: "",
|
|
39
|
+
title: "",
|
|
40
|
+
branch: "",
|
|
41
|
+
status: "planned",
|
|
42
|
+
program: false,
|
|
43
|
+
local: false,
|
|
44
|
+
dir: "",
|
|
45
|
+
updatePointer: false,
|
|
46
|
+
templateOnly: false,
|
|
47
|
+
dryRun: false,
|
|
48
|
+
};
|
|
49
|
+
const positional = [];
|
|
50
|
+
|
|
51
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
52
|
+
const arg = argv[i];
|
|
53
|
+
if (arg === "-h" || arg === "--help") {
|
|
54
|
+
console.log(usage);
|
|
55
|
+
process.exit(0);
|
|
56
|
+
}
|
|
57
|
+
if (arg === "--program") {
|
|
58
|
+
options.program = true;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (arg === "--local") {
|
|
62
|
+
options.local = true;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (arg === "--update-pointer") {
|
|
66
|
+
options.updatePointer = true;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (arg === "--template") {
|
|
70
|
+
options.templateOnly = true;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (arg === "--dry-run") {
|
|
74
|
+
options.dryRun = true;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (["--title", "--branch", "--status", "--dir"].includes(arg)) {
|
|
78
|
+
const value = argv[++i] ?? "";
|
|
79
|
+
if (!value) {
|
|
80
|
+
fail(`Missing value for ${arg}.`, usage);
|
|
81
|
+
}
|
|
82
|
+
options[arg.slice(2)] = value;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (/^--(?:title|branch|status|dir)=/.test(arg)) {
|
|
86
|
+
const idx = arg.indexOf("=");
|
|
87
|
+
options[arg.slice(2, idx)] = arg.slice(idx + 1);
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (arg.startsWith("-")) {
|
|
91
|
+
fail(`Unknown option: ${arg}`, usage);
|
|
92
|
+
}
|
|
93
|
+
positional.push(arg);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (!options.templateOnly && positional.length === 0) {
|
|
97
|
+
fail("Missing feature slug (e.g. `featflow new oauth-pkce-login`).", usage);
|
|
98
|
+
}
|
|
99
|
+
options.slug = positional.join("-");
|
|
100
|
+
return options;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function slugify(raw) {
|
|
104
|
+
return String(raw)
|
|
105
|
+
.trim()
|
|
106
|
+
.toLowerCase()
|
|
107
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
108
|
+
.replace(/^-+|-+$/g, "");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function titleFromSlug(slug) {
|
|
112
|
+
return slug
|
|
113
|
+
.split("-")
|
|
114
|
+
.filter(Boolean)
|
|
115
|
+
.map((word) => word[0].toUpperCase() + word.slice(1))
|
|
116
|
+
.join(" ");
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function formatLocalTimestamps(date = new Date()) {
|
|
120
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
121
|
+
const year = date.getFullYear();
|
|
122
|
+
const month = pad(date.getMonth() + 1);
|
|
123
|
+
const day = pad(date.getDate());
|
|
124
|
+
const hour = pad(date.getHours());
|
|
125
|
+
const minute = pad(date.getMinutes());
|
|
126
|
+
const second = pad(date.getSeconds());
|
|
127
|
+
|
|
128
|
+
const offsetMinutes = -date.getTimezoneOffset();
|
|
129
|
+
const sign = offsetMinutes >= 0 ? "+" : "-";
|
|
130
|
+
const absOffset = Math.abs(offsetMinutes);
|
|
131
|
+
const offH = pad(Math.floor(absOffset / 60));
|
|
132
|
+
const offM = pad(absOffset % 60);
|
|
133
|
+
const tz = offsetMinutes === 0 ? "Z" : `${sign}${offH}:${offM}`;
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
datePart: `${year}-${month}-${day}`,
|
|
137
|
+
dateTimePart: `${year}-${month}-${day}-${hour}${minute}`,
|
|
138
|
+
logStamp: `${year}-${month}-${day} ${hour}:${minute}`,
|
|
139
|
+
iso: `${year}-${month}-${day}T${hour}:${minute}:${second}${tz}`,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function ensureGitInfoExclude(repoRoot, entries) {
|
|
144
|
+
const commonDir = findGitCommonDir(repoRoot);
|
|
145
|
+
if (!commonDir) {
|
|
146
|
+
return [];
|
|
147
|
+
}
|
|
148
|
+
const excludePath = path.join(commonDir, "info", "exclude");
|
|
149
|
+
mkdirSync(path.dirname(excludePath), { recursive: true });
|
|
150
|
+
const existing = existsSync(excludePath) ? readFileSync(excludePath, "utf8") : "";
|
|
151
|
+
const lines = existing.split(/\r?\n/);
|
|
152
|
+
const toAdd = entries.filter((entry) => !lines.includes(entry));
|
|
153
|
+
if (toAdd.length === 0) {
|
|
154
|
+
return [];
|
|
155
|
+
}
|
|
156
|
+
const block = [
|
|
157
|
+
existing && !existing.endsWith("\n") ? "\n" : "",
|
|
158
|
+
existing ? "\n" : "",
|
|
159
|
+
"# featflow local-only state\n",
|
|
160
|
+
...toAdd.map((entry) => `${entry}\n`),
|
|
161
|
+
].join("");
|
|
162
|
+
writeFileSync(excludePath, `${existing}${block}`, "utf8");
|
|
163
|
+
return toAdd;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function loadTemplate(repoRoot, featuresBaseDir, isProgram) {
|
|
167
|
+
const filename = isProgram ? "_program_template.md" : "_template.md";
|
|
168
|
+
const repoTemplate = path.join(repoRoot, featuresBaseDir, filename);
|
|
169
|
+
if (existsSync(repoTemplate)) {
|
|
170
|
+
return readFileSync(repoTemplate, "utf8");
|
|
171
|
+
}
|
|
172
|
+
return readFileSync(path.join(packageRoot, "templates", "features", filename), "utf8");
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function populateTemplate(template, vars) {
|
|
176
|
+
let content = template;
|
|
177
|
+
if (vars.isProgram) {
|
|
178
|
+
content = content
|
|
179
|
+
.replace(/^id:\s*.*$/m, `id: ${vars.id}`)
|
|
180
|
+
.replace(/^title:\s*.*$/m, `title: ${vars.title}`)
|
|
181
|
+
.replace(/^status:\s*.*$/m, `status: ${vars.status}`)
|
|
182
|
+
.replace(/^created:\s*.*$/m, `created: ${vars.iso}`)
|
|
183
|
+
.replace(/^updated:\s*.*$/m, `updated: ${vars.iso}`)
|
|
184
|
+
.replace(/^owner:\s*.*$/m, `owner: ${vars.owner}`)
|
|
185
|
+
.replace(/^- YYYY-MM-DD HH:MM: Program record created\.$/m, `- ${vars.logStamp}: Program record created.`);
|
|
186
|
+
return content;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
content = content
|
|
190
|
+
.replace(/^id:\s*.*$/m, `id: ${vars.id}`)
|
|
191
|
+
.replace(/^title:\s*.*$/m, `title: ${vars.title}`)
|
|
192
|
+
.replace(/^status:\s*.*$/m, `status: ${vars.status}`)
|
|
193
|
+
.replace(/^branch:\s*.*$/m, `branch: ${vars.branch}`)
|
|
194
|
+
.replace(/^created:\s*.*$/m, `created: ${vars.iso}`)
|
|
195
|
+
.replace(/^updated:\s*.*$/m, `updated: ${vars.iso}`)
|
|
196
|
+
.replace(/^owner:\s*.*$/m, `owner: ${vars.owner}`)
|
|
197
|
+
.replace(/^- Workflow mode:.*$/m, `- Workflow mode: \`${vars.mode}\``)
|
|
198
|
+
.replace(/^- Base branch:.*$/m, `- Base branch: \`${vars.baseBranch}\``)
|
|
199
|
+
.replace(/^- Tracked branch:.*$/m, `- Tracked branch: \`${vars.branch}\``)
|
|
200
|
+
.replace(/^- Worktree path:.*$/m, `- Worktree path: \`${vars.worktreePath}\``)
|
|
201
|
+
.replace(/^- YYYY-MM-DD HH:MM: Started planning\.$/m, `- ${vars.logStamp}: Started planning.`);
|
|
202
|
+
|
|
203
|
+
return content;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export async function runNew(argv) {
|
|
207
|
+
const options = parseArgs(argv);
|
|
208
|
+
const repoRoot = findRepoRoot(process.cwd());
|
|
209
|
+
if (!repoRoot) {
|
|
210
|
+
fail("Not inside a Git repository.");
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const { config } = loadConfig(repoRoot);
|
|
214
|
+
const featuresBaseDir = options.dir || (options.local ? LOCAL_FEATURES_DIR : config.featuresDir);
|
|
215
|
+
const rawTemplate = loadTemplate(repoRoot, featuresBaseDir, options.program);
|
|
216
|
+
|
|
217
|
+
if (options.templateOnly) {
|
|
218
|
+
process.stdout.write(rawTemplate);
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const cleanSlug = slugify(options.slug);
|
|
223
|
+
if (!cleanSlug) {
|
|
224
|
+
fail(`Invalid slug "${options.slug}". Provide alphanumeric words such as "oauth-login".`, usage);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const stamps = formatLocalTimestamps();
|
|
228
|
+
const id = options.program ? `${stamps.datePart}-${cleanSlug}` : `${stamps.dateTimePart}-${cleanSlug}`;
|
|
229
|
+
const title = options.title || titleFromSlug(cleanSlug);
|
|
230
|
+
const owner = gitConfigGet("user.name", repoRoot) || "Unknown";
|
|
231
|
+
const branchNow = currentBranch(repoRoot);
|
|
232
|
+
const devBranch = config.mode === "developer-branch" ? gitConfigGet(config.developerBranchConfigKey, repoRoot) : "";
|
|
233
|
+
const baseBranch = config.mode === "developer-branch" ? (devBranch || config.integrationBranch) : config.baseBranch;
|
|
234
|
+
const protectedSet = new Set(config.protectedBranches);
|
|
235
|
+
const inferredBranch =
|
|
236
|
+
branchNow && !protectedSet.has(branchNow) && branchNow !== devBranch
|
|
237
|
+
? branchNow
|
|
238
|
+
: config.mode === "developer-branch" && devBranch
|
|
239
|
+
? `feat/${devBranch}-${cleanSlug}`
|
|
240
|
+
: `feat/${cleanSlug}`;
|
|
241
|
+
const branch = options.branch || inferredBranch;
|
|
242
|
+
|
|
243
|
+
const subdir = options.program ? "programs" : "active";
|
|
244
|
+
const relPath = path.posix.join(featuresBaseDir.replaceAll("\\", "/"), subdir, `${id}.md`);
|
|
245
|
+
const absPath = path.resolve(repoRoot, relPath);
|
|
246
|
+
|
|
247
|
+
if (existsSync(absPath)) {
|
|
248
|
+
fail(`Record already exists: ${relPath}`);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const rendered = populateTemplate(rawTemplate, {
|
|
252
|
+
isProgram: options.program,
|
|
253
|
+
id,
|
|
254
|
+
title,
|
|
255
|
+
status: options.status,
|
|
256
|
+
branch,
|
|
257
|
+
iso: stamps.iso,
|
|
258
|
+
logStamp: stamps.logStamp,
|
|
259
|
+
owner,
|
|
260
|
+
mode: config.mode,
|
|
261
|
+
baseBranch,
|
|
262
|
+
worktreePath: repoRoot,
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
if (options.dryRun) {
|
|
266
|
+
console.log(`[dry-run] Would create ${relPath}`);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
mkdirSync(path.dirname(absPath), { recursive: true });
|
|
271
|
+
writeFileSync(absPath, rendered, "utf8");
|
|
272
|
+
|
|
273
|
+
if (options.local) {
|
|
274
|
+
ensureGitInfoExclude(repoRoot, [".featflow/"]);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (!options.program && (options.updatePointer || existsSync(path.resolve(repoRoot, featuresBaseDir, "current-feature.md")))) {
|
|
278
|
+
const pointerPath = path.resolve(repoRoot, featuresBaseDir, "current-feature.md");
|
|
279
|
+
const existingPointer = existsSync(pointerPath) ? readFileSync(pointerPath, "utf8") : "";
|
|
280
|
+
const lastCompletedMatch = existingPointer.match(/^last_completed:[ \t]*([^\r\n]*)$/m);
|
|
281
|
+
const lastCompleted = lastCompletedMatch ? lastCompletedMatch[1].trim() : "";
|
|
282
|
+
const pointerContent = [
|
|
283
|
+
"---",
|
|
284
|
+
`status: ${options.status}`,
|
|
285
|
+
`updated: ${stamps.iso}`,
|
|
286
|
+
`active_feature: active/${id}.md`,
|
|
287
|
+
lastCompleted ? `last_completed: ${lastCompleted}` : "last_completed:",
|
|
288
|
+
"---",
|
|
289
|
+
"",
|
|
290
|
+
"# Current Feature",
|
|
291
|
+
"",
|
|
292
|
+
`${title} (${branch}).`,
|
|
293
|
+
"",
|
|
294
|
+
"# Next Step",
|
|
295
|
+
"",
|
|
296
|
+
"- Complete `Plan Details` and wait for approval before editing product code.",
|
|
297
|
+
"",
|
|
298
|
+
"# Blockers",
|
|
299
|
+
"",
|
|
300
|
+
"- None.",
|
|
301
|
+
"",
|
|
302
|
+
].join("\n");
|
|
303
|
+
writeFileSync(pointerPath, pointerContent, "utf8");
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
console.log(`Created ${options.program ? "program" : "feature"} record: ${relPath}`);
|
|
307
|
+
if (options.local) {
|
|
308
|
+
console.log("Stored locally under .featflow/features/ (excluded via .git/info/exclude; zero tracked files added).");
|
|
309
|
+
}
|
|
310
|
+
console.log(`Next: fill Plan Details and run \`featflow plan-check ${relPath}\`.`);
|
|
311
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { loadConfig } from "../config.mjs";
|
|
2
|
+
import { fail } from "../git.mjs";
|
|
3
|
+
import { validateActiveFeaturePlan } from "../plan.mjs";
|
|
4
|
+
|
|
5
|
+
const usage = `
|
|
6
|
+
Usage:
|
|
7
|
+
featflow plan-check [feature-record-path] [options]
|
|
8
|
+
|
|
9
|
+
Validates that the active (or specified) feature record has all required Plan Details
|
|
10
|
+
headings, no unresolved template placeholders, and recorded pre-implementation approval.
|
|
11
|
+
|
|
12
|
+
Options:
|
|
13
|
+
--allow-pending Do not fail if Approval status is still "pending".
|
|
14
|
+
--json Output machine-readable JSON.
|
|
15
|
+
-h, --help Show this help.
|
|
16
|
+
`.trim();
|
|
17
|
+
|
|
18
|
+
function parseArgs(argv) {
|
|
19
|
+
const options = {
|
|
20
|
+
allowPending: false,
|
|
21
|
+
filePath: null,
|
|
22
|
+
json: false,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
for (const arg of argv) {
|
|
26
|
+
if (arg === "-h" || arg === "--help") {
|
|
27
|
+
console.log(usage);
|
|
28
|
+
process.exit(0);
|
|
29
|
+
}
|
|
30
|
+
if (arg === "--allow-pending") {
|
|
31
|
+
options.allowPending = true;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (arg === "--json") {
|
|
35
|
+
options.json = true;
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
if (arg.startsWith("-")) {
|
|
39
|
+
fail(`Unknown option: ${arg}`, usage);
|
|
40
|
+
}
|
|
41
|
+
if (options.filePath) {
|
|
42
|
+
fail(`Unexpected extra argument: ${arg}`, usage);
|
|
43
|
+
}
|
|
44
|
+
options.filePath = arg;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return options;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function runPlanCheck(argv) {
|
|
51
|
+
const options = parseArgs(argv);
|
|
52
|
+
const { config, repoRoot } = loadConfig(process.cwd());
|
|
53
|
+
const result = validateActiveFeaturePlan(repoRoot, {
|
|
54
|
+
featuresDir: config.featuresDir,
|
|
55
|
+
filePath: options.filePath,
|
|
56
|
+
requireApproved: !options.allowPending,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
if (options.json) {
|
|
60
|
+
console.log(JSON.stringify(result, null, 2));
|
|
61
|
+
} else if (result.skipped) {
|
|
62
|
+
console.log(result.reason);
|
|
63
|
+
} else if (result.ok) {
|
|
64
|
+
console.log(`Feature plan OK: ${result.activeFeature}`);
|
|
65
|
+
} else {
|
|
66
|
+
console.error(`Feature plan check failed (${result.activeFeature}):`);
|
|
67
|
+
for (const err of result.errors) {
|
|
68
|
+
console.error(` - ${err}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (!result.ok) {
|
|
73
|
+
process.exit(1);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
|
|
3
|
+
import { loadConfig } from "../config.mjs";
|
|
4
|
+
import { fail } from "../git.mjs";
|
|
5
|
+
import { evaluatePreCommit, evaluatePrePush } from "../policy.mjs";
|
|
6
|
+
|
|
7
|
+
const usage = `
|
|
8
|
+
Usage:
|
|
9
|
+
featflow policy <pre-commit|pre-push>
|
|
10
|
+
|
|
11
|
+
Enforces branch protection and workflow setup guards for Git hooks.
|
|
12
|
+
Set FEATFLOW_BOOTSTRAP=1 to bypass for one-off repository bootstrap tasks.
|
|
13
|
+
`.trim();
|
|
14
|
+
|
|
15
|
+
function readStdin() {
|
|
16
|
+
try {
|
|
17
|
+
return readFileSync(0, "utf8");
|
|
18
|
+
} catch {
|
|
19
|
+
return "";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function runPolicy(argv) {
|
|
24
|
+
const mode = argv[0] ?? "";
|
|
25
|
+
if (!mode || mode === "-h" || mode === "--help") {
|
|
26
|
+
console.log(usage);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const { config, repoRoot } = loadConfig(process.cwd());
|
|
31
|
+
|
|
32
|
+
if (mode === "pre-commit") {
|
|
33
|
+
const result = evaluatePreCommit(repoRoot, config, process.env);
|
|
34
|
+
if (!result.ok) {
|
|
35
|
+
console.error(result.message);
|
|
36
|
+
process.exit(1);
|
|
37
|
+
}
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (mode === "pre-push") {
|
|
42
|
+
const result = evaluatePrePush(repoRoot, config, readStdin(), process.env);
|
|
43
|
+
if (!result.ok) {
|
|
44
|
+
console.error(result.message);
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
fail(`Unknown policy mode "${mode}". Expected "pre-commit" or "pre-push".`, usage);
|
|
51
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
import { loadConfig } from "../config.mjs";
|
|
4
|
+
import {
|
|
5
|
+
ensureCleanWorktree,
|
|
6
|
+
fail,
|
|
7
|
+
findWorktreeForBranch,
|
|
8
|
+
findWorktreeForPath,
|
|
9
|
+
gitWorktrees,
|
|
10
|
+
isAncestor,
|
|
11
|
+
isInsidePath,
|
|
12
|
+
localBranchExists,
|
|
13
|
+
remoteBranchExists,
|
|
14
|
+
runRead,
|
|
15
|
+
runWrite,
|
|
16
|
+
} from "../git.mjs";
|
|
17
|
+
|
|
18
|
+
const usage = `
|
|
19
|
+
Usage:
|
|
20
|
+
featflow pr-cleanup <pr-number|pr-url|branch> [options]
|
|
21
|
+
|
|
22
|
+
For pull requests already merged on GitHub (via "gh" CLI). Syncs the local
|
|
23
|
+
base branch, removes the clean PR worktree, deletes the local branch (checking
|
|
24
|
+
HEAD matches the merged PR head), deletes the remote branch if still present,
|
|
25
|
+
and runs "git fetch --prune".
|
|
26
|
+
|
|
27
|
+
Options:
|
|
28
|
+
--base <branch> Require PR to target this specific base branch.
|
|
29
|
+
--worktree <path> Override the feature worktree path.
|
|
30
|
+
--branch <name> Override the PR head branch name.
|
|
31
|
+
--remote <name> Remote to prune/delete from. Defaults to config.remote.
|
|
32
|
+
--dry-run Print mutating commands without running them.
|
|
33
|
+
--skip-remote-delete Do not delete the remote branch if it still exists.
|
|
34
|
+
--force-local-branch-delete Delete local branch even if HEAD differs from PR head.
|
|
35
|
+
-h, --help Show this help.
|
|
36
|
+
`.trim();
|
|
37
|
+
|
|
38
|
+
function parseArgs(argv) {
|
|
39
|
+
const options = {
|
|
40
|
+
base: "",
|
|
41
|
+
branch: "",
|
|
42
|
+
dryRun: false,
|
|
43
|
+
forceLocalBranchDelete: false,
|
|
44
|
+
prRef: "",
|
|
45
|
+
remote: "",
|
|
46
|
+
skipRemoteDelete: false,
|
|
47
|
+
worktree: "",
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
51
|
+
const arg = argv[i];
|
|
52
|
+
if (arg === "-h" || arg === "--help") {
|
|
53
|
+
console.log(usage);
|
|
54
|
+
process.exit(0);
|
|
55
|
+
}
|
|
56
|
+
if (arg === "--dry-run") {
|
|
57
|
+
options.dryRun = true;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (arg === "--skip-remote-delete") {
|
|
61
|
+
options.skipRemoteDelete = true;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (arg === "--force-local-branch-delete") {
|
|
65
|
+
options.forceLocalBranchDelete = true;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (["--base", "--worktree", "--branch", "--remote"].includes(arg)) {
|
|
69
|
+
const value = argv[++i] ?? "";
|
|
70
|
+
if (!value) {
|
|
71
|
+
fail(`Missing value for ${arg}.`, usage);
|
|
72
|
+
}
|
|
73
|
+
options[arg.slice(2)] = value;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (/^--(?:base|worktree|branch|remote)=/.test(arg)) {
|
|
77
|
+
const idx = arg.indexOf("=");
|
|
78
|
+
options[arg.slice(2, idx)] = arg.slice(idx + 1);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (arg.startsWith("-")) {
|
|
82
|
+
fail(`Unknown option: ${arg}`, usage);
|
|
83
|
+
}
|
|
84
|
+
if (options.prRef) {
|
|
85
|
+
fail(`Unexpected extra argument: ${arg}`, usage);
|
|
86
|
+
}
|
|
87
|
+
options.prRef = arg;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (!options.prRef) {
|
|
91
|
+
fail("Missing PR number, PR URL, or branch.", usage);
|
|
92
|
+
}
|
|
93
|
+
return options;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function shortOid(oid) {
|
|
97
|
+
return oid ? oid.slice(0, 12) : "unknown";
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export async function runPrCleanup(argv) {
|
|
101
|
+
const options = parseArgs(argv);
|
|
102
|
+
const { config, repoRoot } = loadConfig(process.cwd());
|
|
103
|
+
const remote = options.remote || config.remote;
|
|
104
|
+
const startCwd = process.cwd();
|
|
105
|
+
const initialWorktrees = gitWorktrees(repoRoot);
|
|
106
|
+
|
|
107
|
+
const pr = JSON.parse(
|
|
108
|
+
runRead(
|
|
109
|
+
"gh",
|
|
110
|
+
[
|
|
111
|
+
"pr",
|
|
112
|
+
"view",
|
|
113
|
+
options.prRef,
|
|
114
|
+
"--json",
|
|
115
|
+
"number,state,mergedAt,mergeCommit,headRefName,headRefOid,baseRefName,url",
|
|
116
|
+
],
|
|
117
|
+
repoRoot,
|
|
118
|
+
),
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
if (pr.state !== "MERGED") {
|
|
122
|
+
fail(`PR ${options.prRef} is ${pr.state}, not MERGED. Cleanup is only safe after merge.`);
|
|
123
|
+
}
|
|
124
|
+
if (options.base && pr.baseRefName !== options.base) {
|
|
125
|
+
fail(`PR #${pr.number} targets ${pr.baseRefName}, expected ${options.base}.`);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const baseBranch = pr.baseRefName;
|
|
129
|
+
const baseWorktree = findWorktreeForBranch(initialWorktrees, baseBranch);
|
|
130
|
+
if (!baseWorktree) {
|
|
131
|
+
fail(`Could not find a local worktree checked out on "${baseBranch}".`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
ensureCleanWorktree(baseWorktree.path, baseBranch);
|
|
135
|
+
|
|
136
|
+
const branchName = options.branch || pr.headRefName;
|
|
137
|
+
const expectedHeadOid = pr.headRefOid;
|
|
138
|
+
const mergeCommitOid = pr.mergeCommit?.oid;
|
|
139
|
+
const targetWorktree = options.worktree
|
|
140
|
+
? (findWorktreeForPath(initialWorktrees, options.worktree) ?? { path: path.resolve(options.worktree) })
|
|
141
|
+
: findWorktreeForBranch(initialWorktrees, branchName);
|
|
142
|
+
|
|
143
|
+
console.log(`Cleaning local state for merged PR #${pr.number}: ${pr.url}`);
|
|
144
|
+
console.log(`Head branch: ${branchName} -> Base branch: ${baseBranch}`);
|
|
145
|
+
console.log(`Merged at: ${pr.mergedAt}`);
|
|
146
|
+
|
|
147
|
+
if (targetWorktree && isInsidePath(startCwd, targetWorktree.path)) {
|
|
148
|
+
process.chdir(baseWorktree.path);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
runWrite("git", ["switch", baseBranch], baseWorktree.path, { dryRun: options.dryRun });
|
|
152
|
+
runWrite("git", ["pull", "--ff-only", remote, baseBranch], baseWorktree.path, { dryRun: options.dryRun });
|
|
153
|
+
|
|
154
|
+
if (!options.dryRun && mergeCommitOid && !isAncestor(mergeCommitOid, "HEAD", baseWorktree.path)) {
|
|
155
|
+
fail(`Local ${baseBranch} does not contain merge commit ${mergeCommitOid}. Pull before cleanup.`);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (targetWorktree) {
|
|
159
|
+
ensureCleanWorktree(targetWorktree.path, "PR");
|
|
160
|
+
runWrite("git", ["worktree", "remove", targetWorktree.path], baseWorktree.path, { dryRun: options.dryRun });
|
|
161
|
+
} else {
|
|
162
|
+
console.log(`No local worktree found for branch ${branchName}; skipping worktree removal.`);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (localBranchExists(branchName, baseWorktree.path)) {
|
|
166
|
+
const localBranchOid = runRead("git", ["rev-parse", branchName], baseWorktree.path);
|
|
167
|
+
if (expectedHeadOid && localBranchOid !== expectedHeadOid && !options.forceLocalBranchDelete) {
|
|
168
|
+
fail(
|
|
169
|
+
[
|
|
170
|
+
`Refusing to delete local branch ${branchName} because it no longer matches PR head ${shortOid(expectedHeadOid)}.`,
|
|
171
|
+
`Local branch is at ${shortOid(localBranchOid)}.`,
|
|
172
|
+
"Pass --force-local-branch-delete only after confirming those local commits are disposable.",
|
|
173
|
+
].join("\n"),
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
runWrite("git", ["branch", "-D", branchName], baseWorktree.path, { dryRun: options.dryRun });
|
|
177
|
+
} else {
|
|
178
|
+
console.log(`No local branch ${branchName} found; skipping local branch deletion.`);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (!options.skipRemoteDelete) {
|
|
182
|
+
if (remoteBranchExists(remote, branchName, baseWorktree.path)) {
|
|
183
|
+
runWrite("git", ["push", remote, "--delete", branchName], baseWorktree.path, { dryRun: options.dryRun });
|
|
184
|
+
} else {
|
|
185
|
+
console.log(`Remote branch ${remote}/${branchName} is already absent.`);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
runWrite("git", ["fetch", "--prune", remote], baseWorktree.path, { dryRun: options.dryRun });
|
|
190
|
+
|
|
191
|
+
const currentTip = runRead("git", ["rev-parse", "--short", "HEAD"], baseWorktree.path);
|
|
192
|
+
const prefix = options.dryRun ? "Dry run complete" : "Cleanup complete";
|
|
193
|
+
console.log(`${prefix}: local ${baseBranch} is at ${currentTip}.`);
|
|
194
|
+
}
|