@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
package/src/detect.mjs
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
import { DEFAULT_PRODUCT_PATHS } from "./config.mjs";
|
|
5
|
+
import {
|
|
6
|
+
currentBranch,
|
|
7
|
+
gitConfigGet,
|
|
8
|
+
localBranchExists,
|
|
9
|
+
remoteExists,
|
|
10
|
+
remoteTrackingBranchExists,
|
|
11
|
+
runReadSoft,
|
|
12
|
+
} from "./git.mjs";
|
|
13
|
+
|
|
14
|
+
const PRODUCT_DIR_CANDIDATES = [
|
|
15
|
+
"src",
|
|
16
|
+
"app",
|
|
17
|
+
"apps",
|
|
18
|
+
"lib",
|
|
19
|
+
"packages",
|
|
20
|
+
"services",
|
|
21
|
+
"cmd",
|
|
22
|
+
"internal",
|
|
23
|
+
"pkg",
|
|
24
|
+
"crates",
|
|
25
|
+
"server",
|
|
26
|
+
"client",
|
|
27
|
+
"web",
|
|
28
|
+
"frontend",
|
|
29
|
+
"backend",
|
|
30
|
+
"api",
|
|
31
|
+
"components",
|
|
32
|
+
"pages",
|
|
33
|
+
"modules",
|
|
34
|
+
"core",
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
const IGNORED_TOP_DIRS = new Set([
|
|
38
|
+
"node_modules",
|
|
39
|
+
"dist",
|
|
40
|
+
"build",
|
|
41
|
+
"out",
|
|
42
|
+
"target",
|
|
43
|
+
"vendor",
|
|
44
|
+
"coverage",
|
|
45
|
+
"test",
|
|
46
|
+
"tests",
|
|
47
|
+
"__tests__",
|
|
48
|
+
"spec",
|
|
49
|
+
"docs",
|
|
50
|
+
"doc",
|
|
51
|
+
"scripts",
|
|
52
|
+
"features",
|
|
53
|
+
"templates",
|
|
54
|
+
"bin",
|
|
55
|
+
"venv",
|
|
56
|
+
"__pycache__",
|
|
57
|
+
"tmp",
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
function readJsonSafe(filePath) {
|
|
61
|
+
if (!existsSync(filePath)) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
return JSON.parse(readFileSync(filePath, "utf8"));
|
|
66
|
+
} catch {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function readTextSafe(filePath) {
|
|
72
|
+
if (!existsSync(filePath)) {
|
|
73
|
+
return "";
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
return readFileSync(filePath, "utf8");
|
|
77
|
+
} catch {
|
|
78
|
+
return "";
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function detectBranches(repoRoot, remote = "origin") {
|
|
83
|
+
const hasRemote = remoteExists(remote, repoRoot);
|
|
84
|
+
const remoteUrl = hasRemote ? runReadSoft("git", ["remote", "get-url", remote], repoRoot).stdout : "";
|
|
85
|
+
const isHostedGit = /github\.com|gitlab\.com|bitbucket\.org/i.test(remoteUrl);
|
|
86
|
+
|
|
87
|
+
let defaultBranch = "";
|
|
88
|
+
const originHead = runReadSoft("git", ["symbolic-ref", "--short", `refs/remotes/${remote}/HEAD`], repoRoot);
|
|
89
|
+
if (originHead.status === 0 && originHead.stdout) {
|
|
90
|
+
const prefix = `${remote}/`;
|
|
91
|
+
defaultBranch = originHead.stdout.startsWith(prefix)
|
|
92
|
+
? originHead.stdout.slice(prefix.length)
|
|
93
|
+
: originHead.stdout;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (!defaultBranch) {
|
|
97
|
+
if (localBranchExists("main", repoRoot)) {
|
|
98
|
+
defaultBranch = "main";
|
|
99
|
+
} else if (localBranchExists("master", repoRoot)) {
|
|
100
|
+
defaultBranch = "master";
|
|
101
|
+
} else if (remoteTrackingBranchExists(remote, "main", repoRoot)) {
|
|
102
|
+
defaultBranch = "main";
|
|
103
|
+
} else if (remoteTrackingBranchExists(remote, "master", repoRoot)) {
|
|
104
|
+
defaultBranch = "master";
|
|
105
|
+
} else {
|
|
106
|
+
defaultBranch = currentBranch(repoRoot) || "main";
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
let integrationBranch = "dev";
|
|
111
|
+
let hasDevBranch = false;
|
|
112
|
+
if (localBranchExists("dev", repoRoot) || remoteTrackingBranchExists(remote, "dev", repoRoot)) {
|
|
113
|
+
integrationBranch = "dev";
|
|
114
|
+
hasDevBranch = true;
|
|
115
|
+
} else if (localBranchExists("develop", repoRoot) || remoteTrackingBranchExists(remote, "develop", repoRoot)) {
|
|
116
|
+
integrationBranch = "develop";
|
|
117
|
+
hasDevBranch = true;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
defaultBranch,
|
|
122
|
+
integrationBranch,
|
|
123
|
+
hasDevBranch,
|
|
124
|
+
hasRemote,
|
|
125
|
+
remoteUrl,
|
|
126
|
+
isHostedGit,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function detectProductPaths(repoRoot) {
|
|
131
|
+
let entries = [];
|
|
132
|
+
try {
|
|
133
|
+
entries = readdirSync(repoRoot, { withFileTypes: true });
|
|
134
|
+
} catch {
|
|
135
|
+
return { productPaths: [...DEFAULT_PRODUCT_PATHS], source: "default" };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const topDirs = new Set(
|
|
139
|
+
entries
|
|
140
|
+
.filter((entry) => entry.isDirectory() && !entry.name.startsWith(".") && !IGNORED_TOP_DIRS.has(entry.name))
|
|
141
|
+
.map((entry) => entry.name),
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
const found = [];
|
|
145
|
+
for (const candidate of PRODUCT_DIR_CANDIDATES) {
|
|
146
|
+
if (topDirs.has(candidate)) {
|
|
147
|
+
found.push(candidate);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Detect top-level Python packages (directories containing __init__.py)
|
|
152
|
+
const pythonPackages = [...topDirs]
|
|
153
|
+
.filter((dir) => !found.includes(dir) && existsSync(path.join(repoRoot, dir, "__init__.py")))
|
|
154
|
+
.sort();
|
|
155
|
+
found.push(...pythonPackages);
|
|
156
|
+
|
|
157
|
+
if (found.length > 0) {
|
|
158
|
+
return {
|
|
159
|
+
productPaths: found.map((dir) => `${dir}/`),
|
|
160
|
+
source: "detected",
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
productPaths: [...DEFAULT_PRODUCT_PATHS],
|
|
166
|
+
source: "default",
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function detectPackageManagerAndVerify(repoRoot, productPathsInfo) {
|
|
171
|
+
let packageManager = "";
|
|
172
|
+
let verifyCommand = "";
|
|
173
|
+
const suggestions = [];
|
|
174
|
+
|
|
175
|
+
const pkg = readJsonSafe(path.join(repoRoot, "package.json"));
|
|
176
|
+
if (pkg) {
|
|
177
|
+
if (existsSync(path.join(repoRoot, "pnpm-lock.yaml"))) {
|
|
178
|
+
packageManager = "pnpm";
|
|
179
|
+
suggestions.push('postWorktreeCreate: ["pnpm install --frozen-lockfile --offline"]');
|
|
180
|
+
} else if (existsSync(path.join(repoRoot, "yarn.lock"))) {
|
|
181
|
+
packageManager = "yarn";
|
|
182
|
+
} else if (existsSync(path.join(repoRoot, "bun.lock")) || existsSync(path.join(repoRoot, "bun.lockb"))) {
|
|
183
|
+
packageManager = "bun";
|
|
184
|
+
} else if (existsSync(path.join(repoRoot, "package-lock.json"))) {
|
|
185
|
+
packageManager = "npm";
|
|
186
|
+
} else if (typeof pkg.packageManager === "string") {
|
|
187
|
+
const match = pkg.packageManager.match(/^(pnpm|yarn|bun|npm)\b/);
|
|
188
|
+
if (match) {
|
|
189
|
+
packageManager = match[1];
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (!packageManager) {
|
|
193
|
+
packageManager = "npm";
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const testScript = typeof pkg.scripts?.test === "string" ? pkg.scripts.test.trim() : "";
|
|
197
|
+
if (testScript && !/no test specified/i.test(testScript)) {
|
|
198
|
+
verifyCommand = packageManager === "bun" ? "bun run test" : `${packageManager} test`;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (!verifyCommand && existsSync(path.join(repoRoot, "go.mod"))) {
|
|
203
|
+
verifyCommand = "go test ./...";
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (!verifyCommand && existsSync(path.join(repoRoot, "Cargo.toml"))) {
|
|
207
|
+
verifyCommand = "cargo test";
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (!verifyCommand) {
|
|
211
|
+
const pyproject = readTextSafe(path.join(repoRoot, "pyproject.toml"));
|
|
212
|
+
const hasPytestMarkers =
|
|
213
|
+
existsSync(path.join(repoRoot, "pytest.ini")) ||
|
|
214
|
+
existsSync(path.join(repoRoot, "conftest.py")) ||
|
|
215
|
+
/\bpytest\b/i.test(pyproject) ||
|
|
216
|
+
(Boolean(pyproject) && (existsSync(path.join(repoRoot, "tests")) || existsSync(path.join(repoRoot, "test"))));
|
|
217
|
+
if (hasPytestMarkers) {
|
|
218
|
+
verifyCommand = existsSync(path.join(repoRoot, "uv.lock")) ? "uv run pytest" : "pytest";
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (existsSync(path.join(repoRoot, ".codegraph"))) {
|
|
223
|
+
suggestions.push('postWorktreeCreate: ["codegraph init"]');
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const verifyCommands = {};
|
|
227
|
+
if (verifyCommand && productPathsInfo.source === "detected") {
|
|
228
|
+
for (const dir of productPathsInfo.productPaths) {
|
|
229
|
+
verifyCommands[dir] = verifyCommand;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return {
|
|
234
|
+
packageManager,
|
|
235
|
+
verifyCommand,
|
|
236
|
+
verifyCommands,
|
|
237
|
+
suggestions,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function detectFeaturesDirConflict(repoRoot, featuresDir = "features") {
|
|
242
|
+
const target = path.join(repoRoot, featuresDir);
|
|
243
|
+
if (!existsSync(target)) {
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
if (existsSync(path.join(target, "_template.md")) || existsSync(path.join(target, "active"))) {
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
try {
|
|
250
|
+
const entries = readdirSync(target).filter((name) => name !== ".DS_Store");
|
|
251
|
+
return entries.length > 0;
|
|
252
|
+
} catch {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function detectRepoEnvironment(repoRoot, options = {}) {
|
|
258
|
+
const remote = options.remote || "origin";
|
|
259
|
+
const branches = detectBranches(repoRoot, remote);
|
|
260
|
+
const productInfo = detectProductPaths(repoRoot);
|
|
261
|
+
const verifyInfo = detectPackageManagerAndVerify(repoRoot, productInfo);
|
|
262
|
+
const featuresDirConflict = detectFeaturesDirConflict(repoRoot, options.featuresDir || "features");
|
|
263
|
+
|
|
264
|
+
return {
|
|
265
|
+
...branches,
|
|
266
|
+
productPaths: productInfo.productPaths,
|
|
267
|
+
productPathsSource: productInfo.source,
|
|
268
|
+
packageManager: verifyInfo.packageManager,
|
|
269
|
+
verifyCommand: verifyInfo.verifyCommand,
|
|
270
|
+
verifyCommands: verifyInfo.verifyCommands,
|
|
271
|
+
suggestions: verifyInfo.suggestions,
|
|
272
|
+
featuresDirConflict,
|
|
273
|
+
existingHooksPath: gitConfigGet("core.hooksPath", repoRoot),
|
|
274
|
+
hasAgentsDir: existsSync(path.join(repoRoot, ".agents")),
|
|
275
|
+
hasAntigravityHooks: existsSync(path.join(repoRoot, ".agents", "hooks.json")),
|
|
276
|
+
hasClaudeConfig: existsSync(path.join(repoRoot, ".claude", "settings.json")),
|
|
277
|
+
hasClaudeDir: existsSync(path.join(repoRoot, ".claude")),
|
|
278
|
+
hasCodexConfig: existsSync(path.join(repoRoot, ".codex", "config.toml")),
|
|
279
|
+
hasCodexDir: existsSync(path.join(repoRoot, ".codex")),
|
|
280
|
+
hasCursorHooks: existsSync(path.join(repoRoot, ".cursor", "hooks.json")),
|
|
281
|
+
hasCursorDir: existsSync(path.join(repoRoot, ".cursor")),
|
|
282
|
+
hasCodegraph: existsSync(path.join(repoRoot, ".codegraph")),
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export function applyDetectedConfig(baseConfig, detected, options = {}) {
|
|
287
|
+
const next = {
|
|
288
|
+
...baseConfig,
|
|
289
|
+
protectedBranches: [...baseConfig.protectedBranches],
|
|
290
|
+
prefixes: [...baseConfig.prefixes],
|
|
291
|
+
productPaths: [...detected.productPaths],
|
|
292
|
+
postWorktreeCreate: [...baseConfig.postWorktreeCreate],
|
|
293
|
+
verifyCommands: { ...baseConfig.verifyCommands, ...detected.verifyCommands },
|
|
294
|
+
dangerousCommands: { ...baseConfig.dangerousCommands },
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
if (next.mode === "developer-branch") {
|
|
298
|
+
next.releaseBranch = detected.defaultBranch;
|
|
299
|
+
next.integrationBranch = detected.integrationBranch;
|
|
300
|
+
next.baseBranch = detected.integrationBranch;
|
|
301
|
+
next.protectedBranches = [...new Set([detected.defaultBranch, detected.integrationBranch])];
|
|
302
|
+
} else {
|
|
303
|
+
next.baseBranch = detected.defaultBranch;
|
|
304
|
+
next.integrationBranch = detected.defaultBranch;
|
|
305
|
+
next.releaseBranch = detected.defaultBranch;
|
|
306
|
+
next.protectedBranches = [detected.defaultBranch];
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (detected.featuresDirConflict && !options.explicitFeaturesDir) {
|
|
310
|
+
next.featuresDir = options.minimal || options.localFeatures ? ".featflow/features" : "docs/features";
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return next;
|
|
314
|
+
}
|
package/src/git.mjs
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { existsSync, realpathSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
export function quoteArg(value) {
|
|
6
|
+
return /^[A-Za-z0-9_@%+=:,./-]+$/.test(value) ? value : JSON.stringify(value);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function formatCommand(command, args) {
|
|
10
|
+
return [command, ...args].map(quoteArg).join(" ");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function fail(message, usageText = "") {
|
|
14
|
+
console.error(`error: ${message}`);
|
|
15
|
+
if (usageText) {
|
|
16
|
+
console.error("");
|
|
17
|
+
console.error(usageText);
|
|
18
|
+
}
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function runRead(command, args, cwd = process.cwd(), extra = {}) {
|
|
23
|
+
const result = spawnSync(command, args, {
|
|
24
|
+
cwd,
|
|
25
|
+
encoding: "utf8",
|
|
26
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
27
|
+
env: extra.env ? { ...process.env, ...extra.env } : process.env,
|
|
28
|
+
});
|
|
29
|
+
if (result.error) {
|
|
30
|
+
throw new Error(`${formatCommand(command, args)} failed:\n${result.error.message}`);
|
|
31
|
+
}
|
|
32
|
+
if (result.status !== 0) {
|
|
33
|
+
const detail = (result.stderr ?? "").trim() || `exit code ${String(result.status)}`;
|
|
34
|
+
throw new Error(`${formatCommand(command, args)} failed:\n${detail}`);
|
|
35
|
+
}
|
|
36
|
+
return (result.stdout ?? "").trim();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function runReadSoft(command, args, cwd = process.cwd(), extra = {}) {
|
|
40
|
+
const result = spawnSync(command, args, {
|
|
41
|
+
cwd,
|
|
42
|
+
encoding: "utf8",
|
|
43
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
44
|
+
env: extra.env ? { ...process.env, ...extra.env } : process.env,
|
|
45
|
+
});
|
|
46
|
+
return {
|
|
47
|
+
status: result.status,
|
|
48
|
+
stdout: (result.stdout ?? "").trim(),
|
|
49
|
+
stderr: (result.stderr ?? "").trim(),
|
|
50
|
+
error: result.error ?? null,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function runStatus(command, args, cwd = process.cwd(), extra = {}) {
|
|
55
|
+
const result = spawnSync(command, args, {
|
|
56
|
+
cwd,
|
|
57
|
+
encoding: "utf8",
|
|
58
|
+
stdio: "ignore",
|
|
59
|
+
env: extra.env ? { ...process.env, ...extra.env } : process.env,
|
|
60
|
+
});
|
|
61
|
+
return result.error ? -1 : (result.status ?? -1);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function runWrite(command, args, cwd, options = {}) {
|
|
65
|
+
const printable = cwd ? `(cd ${quoteArg(cwd)} && ${formatCommand(command, args)})` : formatCommand(command, args);
|
|
66
|
+
if (options.dryRun) {
|
|
67
|
+
console.log(`[dry-run] ${printable}`);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const result = spawnSync(command, args, {
|
|
72
|
+
cwd,
|
|
73
|
+
stdio: "inherit",
|
|
74
|
+
shell: options.shell ?? false,
|
|
75
|
+
env: options.env ? { ...process.env, ...options.env } : process.env,
|
|
76
|
+
});
|
|
77
|
+
if (result.error) {
|
|
78
|
+
throw new Error(`${formatCommand(command, args)} failed: ${result.error.message}`);
|
|
79
|
+
}
|
|
80
|
+
if (result.status !== 0) {
|
|
81
|
+
throw new Error(`${formatCommand(command, args)} failed with exit code ${String(result.status)}.`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function commandExists(command, args = ["--version"]) {
|
|
86
|
+
const result = spawnSync(command, args, {
|
|
87
|
+
encoding: "utf8",
|
|
88
|
+
stdio: "ignore",
|
|
89
|
+
});
|
|
90
|
+
return !result.error && result.status === 0;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function canonicalPath(targetPath) {
|
|
94
|
+
const resolved = path.resolve(targetPath);
|
|
95
|
+
const nativeRealpath = realpathSync.native ?? realpathSync;
|
|
96
|
+
if (existsSync(resolved)) {
|
|
97
|
+
try {
|
|
98
|
+
return nativeRealpath(resolved);
|
|
99
|
+
} catch {
|
|
100
|
+
return resolved;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const tail = [];
|
|
104
|
+
let cursor = resolved;
|
|
105
|
+
while (true) {
|
|
106
|
+
const parent = path.dirname(cursor);
|
|
107
|
+
if (parent === cursor) {
|
|
108
|
+
return resolved;
|
|
109
|
+
}
|
|
110
|
+
tail.unshift(path.basename(cursor));
|
|
111
|
+
cursor = parent;
|
|
112
|
+
if (existsSync(cursor)) {
|
|
113
|
+
try {
|
|
114
|
+
return path.join(nativeRealpath(cursor), ...tail);
|
|
115
|
+
} catch {
|
|
116
|
+
return resolved;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function samePath(left, right) {
|
|
123
|
+
const a = canonicalPath(left);
|
|
124
|
+
const b = canonicalPath(right);
|
|
125
|
+
return process.platform === "win32" ? a.toLowerCase() === b.toLowerCase() : a === b;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function isInsidePath(child, parent) {
|
|
129
|
+
const resolvedChild = canonicalPath(child);
|
|
130
|
+
const resolvedParent = canonicalPath(parent);
|
|
131
|
+
const relative = path.relative(
|
|
132
|
+
process.platform === "win32" ? resolvedParent.toLowerCase() : resolvedParent,
|
|
133
|
+
process.platform === "win32" ? resolvedChild.toLowerCase() : resolvedChild,
|
|
134
|
+
);
|
|
135
|
+
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function findRepoRoot(startCwd = process.cwd()) {
|
|
139
|
+
const result = runReadSoft("git", ["rev-parse", "--show-toplevel"], startCwd);
|
|
140
|
+
if (result.status !== 0 || !result.stdout) {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
return canonicalPath(result.stdout);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function findGitCommonDir(startCwd = process.cwd()) {
|
|
147
|
+
const commonDirResult = runReadSoft("git", ["rev-parse", "--path-format=absolute", "--git-common-dir"], startCwd);
|
|
148
|
+
if (commonDirResult.status === 0 && commonDirResult.stdout) {
|
|
149
|
+
return canonicalPath(commonDirResult.stdout);
|
|
150
|
+
}
|
|
151
|
+
const fallback = runReadSoft("git", ["rev-parse", "--git-common-dir"], startCwd);
|
|
152
|
+
if (fallback.status === 0 && fallback.stdout) {
|
|
153
|
+
return canonicalPath(path.resolve(startCwd, fallback.stdout));
|
|
154
|
+
}
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function findMainRepoRoot(startCwd = process.cwd()) {
|
|
159
|
+
const commonDir = findGitCommonDir(startCwd);
|
|
160
|
+
if (commonDir && path.basename(commonDir) === ".git") {
|
|
161
|
+
return path.dirname(commonDir);
|
|
162
|
+
}
|
|
163
|
+
return findRepoRoot(startCwd);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function gitConfigGet(key, cwd) {
|
|
167
|
+
const result = runReadSoft("git", ["config", "--get", key], cwd);
|
|
168
|
+
return result.status === 0 ? result.stdout : "";
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function gitConfigSet(key, value, cwd, options = {}) {
|
|
172
|
+
runWrite("git", ["config", key, value], cwd, options);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function currentBranch(cwd) {
|
|
176
|
+
const result = runReadSoft("git", ["branch", "--show-current"], cwd);
|
|
177
|
+
return result.status === 0 ? result.stdout : "";
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function localBranchExists(branchName, cwd) {
|
|
181
|
+
return runStatus("git", ["show-ref", "--verify", "--quiet", `refs/heads/${branchName}`], cwd) === 0;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function remoteExists(remoteName, cwd) {
|
|
185
|
+
return runStatus("git", ["remote", "get-url", remoteName], cwd) === 0;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export function remoteTrackingBranchExists(remote, branchName, cwd) {
|
|
189
|
+
return runStatus("git", ["show-ref", "--verify", "--quiet", `refs/remotes/${remote}/${branchName}`], cwd) === 0;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function remoteBranchExists(remote, branchName, cwd) {
|
|
193
|
+
if (!remoteExists(remote, cwd)) {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
return runStatus("git", ["ls-remote", "--exit-code", "--heads", remote, branchName], cwd) === 0;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function localBranches(cwd) {
|
|
200
|
+
const output = runRead("git", ["for-each-ref", "refs/heads", "--format=%(refname:short)"], cwd);
|
|
201
|
+
return output ? output.split(/\r?\n/).filter(Boolean) : [];
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function parseWorktrees(output) {
|
|
205
|
+
const worktrees = [];
|
|
206
|
+
let current = null;
|
|
207
|
+
|
|
208
|
+
for (const rawLine of output.split(/\r?\n/)) {
|
|
209
|
+
const line = rawLine.trimEnd();
|
|
210
|
+
if (line.startsWith("worktree ")) {
|
|
211
|
+
if (current) {
|
|
212
|
+
worktrees.push(current);
|
|
213
|
+
}
|
|
214
|
+
current = { path: canonicalPath(line.slice("worktree ".length)) };
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
if (!current) {
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
if (line.startsWith("HEAD ")) {
|
|
221
|
+
current.head = line.slice("HEAD ".length);
|
|
222
|
+
} else if (line.startsWith("branch ")) {
|
|
223
|
+
current.branch = line.slice("branch ".length);
|
|
224
|
+
} else if (line === "bare") {
|
|
225
|
+
current.bare = true;
|
|
226
|
+
} else if (line === "detached") {
|
|
227
|
+
current.detached = true;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (current) {
|
|
232
|
+
worktrees.push(current);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return worktrees;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function gitWorktrees(cwd) {
|
|
239
|
+
return parseWorktrees(runRead("git", ["worktree", "list", "--porcelain"], cwd));
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export function findWorktreeForBranch(worktrees, branchName) {
|
|
243
|
+
return worktrees.find((worktree) => worktree.branch === `refs/heads/${branchName}`) ?? null;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export function findWorktreeForPath(worktrees, worktreePath) {
|
|
247
|
+
return worktrees.find((worktree) => samePath(worktree.path, worktreePath)) ?? null;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function statusShort(cwd) {
|
|
251
|
+
return runRead("git", ["status", "--short"], cwd);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export function ensureCleanWorktree(cwd, label) {
|
|
255
|
+
const status = statusShort(cwd);
|
|
256
|
+
if (status) {
|
|
257
|
+
throw new Error(`Refusing to modify dirty ${label} worktree ${cwd}:\n${status}`);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function validateGitBranchName(branchName, cwd, options = {}) {
|
|
262
|
+
const label = options.label ?? "branch";
|
|
263
|
+
if (!branchName) {
|
|
264
|
+
throw new Error(`Missing ${label} name.`);
|
|
265
|
+
}
|
|
266
|
+
if (!options.allowSlash && branchName.includes("/")) {
|
|
267
|
+
throw new Error(`${label} "${branchName}" must be a simple branch name without "/".`);
|
|
268
|
+
}
|
|
269
|
+
if (/\s/.test(branchName)) {
|
|
270
|
+
throw new Error(`${label} "${branchName}" must not contain whitespace.`);
|
|
271
|
+
}
|
|
272
|
+
if (runStatus("git", ["check-ref-format", "--branch", branchName], cwd) !== 0) {
|
|
273
|
+
throw new Error(`${label} "${branchName}" is not a valid Git branch name.`);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export function validateDeveloperBranchName(branchName, cwd, protectedBranches) {
|
|
278
|
+
validateGitBranchName(branchName, cwd, { allowSlash: false, label: "developer branch" });
|
|
279
|
+
if (protectedBranches.has(branchName)) {
|
|
280
|
+
throw new Error(`Developer branch cannot be protected branch "${branchName}".`);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export function validateTaskBranchName(branchName, cwd, protectedBranches) {
|
|
285
|
+
validateGitBranchName(branchName, cwd, { allowSlash: true, label: "task branch" });
|
|
286
|
+
if (protectedBranches.has(branchName)) {
|
|
287
|
+
throw new Error(`Task branch cannot be protected branch "${branchName}".`);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export function isAncestor(ancestorRef, descendantRef, cwd) {
|
|
292
|
+
return runStatus("git", ["merge-base", "--is-ancestor", ancestorRef, descendantRef], cwd) === 0;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export function isTaskBranch(branchName, config, developerBranch = "") {
|
|
296
|
+
const slashIndex = branchName.indexOf("/");
|
|
297
|
+
if (slashIndex === -1) {
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
const prefix = branchName.slice(0, slashIndex);
|
|
301
|
+
const rest = branchName.slice(slashIndex + 1);
|
|
302
|
+
if (!config.prefixes.includes(prefix) || !rest) {
|
|
303
|
+
return false;
|
|
304
|
+
}
|
|
305
|
+
if (config.mode === "developer-branch") {
|
|
306
|
+
return Boolean(developerBranch) && rest.startsWith(`${developerBranch}-`);
|
|
307
|
+
}
|
|
308
|
+
return true;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export function sanitizeBranchForDir(branchName) {
|
|
312
|
+
return branchName.replaceAll("/", "-").replace(/[^A-Za-z0-9._-]+/g, "-");
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export function gitChangedFiles(cwd) {
|
|
316
|
+
if (process.env.FEATFLOW_TEST_CHANGED_FILES) {
|
|
317
|
+
return process.env.FEATFLOW_TEST_CHANGED_FILES.split(/\r?\n|,/)
|
|
318
|
+
.map((entry) => entry.trim())
|
|
319
|
+
.filter(Boolean);
|
|
320
|
+
}
|
|
321
|
+
const result = runReadSoft("git", ["status", "--porcelain"], cwd);
|
|
322
|
+
if (result.status !== 0 || !result.stdout) {
|
|
323
|
+
return [];
|
|
324
|
+
}
|
|
325
|
+
return result.stdout
|
|
326
|
+
.split(/\r?\n/)
|
|
327
|
+
.map((line) => line.trimEnd())
|
|
328
|
+
.filter(Boolean)
|
|
329
|
+
.map((line) => line.slice(3).split(" -> ").pop()?.trim() ?? "")
|
|
330
|
+
.filter(Boolean);
|
|
331
|
+
}
|