contribute-now 0.1.2-staging.c209cc7 → 0.2.0-dev.09dfc24
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/dist/index.js +2754 -712
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
2
4
|
|
|
3
5
|
// src/index.ts
|
|
4
|
-
import { defineCommand as
|
|
6
|
+
import { defineCommand as defineCommand14, runMain } from "citty";
|
|
5
7
|
|
|
6
|
-
// src/commands/
|
|
8
|
+
// src/commands/branch.ts
|
|
7
9
|
import { defineCommand } from "citty";
|
|
8
|
-
import
|
|
10
|
+
import pc2 from "picocolors";
|
|
9
11
|
|
|
10
12
|
// src/utils/config.ts
|
|
11
13
|
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
@@ -14,13 +16,20 @@ var CONFIG_FILENAME = ".contributerc.json";
|
|
|
14
16
|
function getConfigPath(cwd = process.cwd()) {
|
|
15
17
|
return join(cwd, CONFIG_FILENAME);
|
|
16
18
|
}
|
|
19
|
+
function configExists(cwd = process.cwd()) {
|
|
20
|
+
return existsSync(getConfigPath(cwd));
|
|
21
|
+
}
|
|
17
22
|
function readConfig(cwd = process.cwd()) {
|
|
18
23
|
const path = getConfigPath(cwd);
|
|
19
24
|
if (!existsSync(path))
|
|
20
25
|
return null;
|
|
21
26
|
try {
|
|
22
27
|
const raw = readFileSync(path, "utf-8");
|
|
23
|
-
|
|
28
|
+
const parsed = JSON.parse(raw);
|
|
29
|
+
if (typeof parsed !== "object" || parsed === null || typeof parsed.workflow !== "string" || typeof parsed.role !== "string" || typeof parsed.mainBranch !== "string" || typeof parsed.upstream !== "string" || typeof parsed.origin !== "string" || !Array.isArray(parsed.branchPrefixes) || typeof parsed.commitConvention !== "string") {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
return parsed;
|
|
24
33
|
} catch {
|
|
25
34
|
return null;
|
|
26
35
|
}
|
|
@@ -55,69 +64,15 @@ function getDefaultConfig() {
|
|
|
55
64
|
};
|
|
56
65
|
}
|
|
57
66
|
|
|
58
|
-
// src/utils/confirm.ts
|
|
59
|
-
import pc from "picocolors";
|
|
60
|
-
async function confirmPrompt(message) {
|
|
61
|
-
console.log(`
|
|
62
|
-
${message}`);
|
|
63
|
-
process.stdout.write(`${pc.dim("Continue? [y/N] ")}`);
|
|
64
|
-
const response = await new Promise((resolve) => {
|
|
65
|
-
process.stdin.setEncoding("utf-8");
|
|
66
|
-
process.stdin.once("data", (data) => {
|
|
67
|
-
process.stdin.pause();
|
|
68
|
-
resolve(data.toString().trim());
|
|
69
|
-
});
|
|
70
|
-
process.stdin.resume();
|
|
71
|
-
});
|
|
72
|
-
if (response.toLowerCase() !== "y") {
|
|
73
|
-
console.log(pc.yellow("Aborted."));
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
return true;
|
|
77
|
-
}
|
|
78
|
-
async function selectPrompt(message, choices) {
|
|
79
|
-
console.log(`
|
|
80
|
-
${message}`);
|
|
81
|
-
choices.forEach((choice, i) => {
|
|
82
|
-
console.log(` ${pc.dim(`${i + 1}.`)} ${choice}`);
|
|
83
|
-
});
|
|
84
|
-
process.stdout.write(pc.dim(`Enter number [1-${choices.length}]: `));
|
|
85
|
-
const response = await new Promise((resolve) => {
|
|
86
|
-
process.stdin.setEncoding("utf-8");
|
|
87
|
-
process.stdin.once("data", (data) => {
|
|
88
|
-
process.stdin.pause();
|
|
89
|
-
resolve(data.toString().trim());
|
|
90
|
-
});
|
|
91
|
-
process.stdin.resume();
|
|
92
|
-
});
|
|
93
|
-
const index = Number.parseInt(response, 10) - 1;
|
|
94
|
-
if (index >= 0 && index < choices.length) {
|
|
95
|
-
return choices[index];
|
|
96
|
-
}
|
|
97
|
-
return choices[0];
|
|
98
|
-
}
|
|
99
|
-
async function inputPrompt(message, defaultValue) {
|
|
100
|
-
const hint = defaultValue ? ` ${pc.dim(`[${defaultValue}]`)}` : "";
|
|
101
|
-
process.stdout.write(`
|
|
102
|
-
${message}${hint}: `);
|
|
103
|
-
const response = await new Promise((resolve) => {
|
|
104
|
-
process.stdin.setEncoding("utf-8");
|
|
105
|
-
process.stdin.once("data", (data) => {
|
|
106
|
-
process.stdin.pause();
|
|
107
|
-
resolve(data.toString().trim());
|
|
108
|
-
});
|
|
109
|
-
process.stdin.resume();
|
|
110
|
-
});
|
|
111
|
-
return response || defaultValue || "";
|
|
112
|
-
}
|
|
113
|
-
|
|
114
67
|
// src/utils/git.ts
|
|
115
68
|
import { execFile as execFileCb } from "node:child_process";
|
|
69
|
+
import { readFileSync as readFileSync2 } from "node:fs";
|
|
70
|
+
import { join as join2 } from "node:path";
|
|
116
71
|
function run(args) {
|
|
117
72
|
return new Promise((resolve) => {
|
|
118
73
|
execFileCb("git", args, (error, stdout, stderr) => {
|
|
119
74
|
resolve({
|
|
120
|
-
exitCode: error ? error.code === "ENOENT" ? 127 : error.
|
|
75
|
+
exitCode: error ? error.code === "ENOENT" ? 127 : error.status ?? 1 : 0,
|
|
121
76
|
stdout: stdout ?? "",
|
|
122
77
|
stderr: stderr ?? ""
|
|
123
78
|
});
|
|
@@ -153,13 +108,17 @@ async function hasUncommittedChanges() {
|
|
|
153
108
|
return false;
|
|
154
109
|
return stdout.trim().length > 0;
|
|
155
110
|
}
|
|
111
|
+
async function branchExists(branch) {
|
|
112
|
+
const { exitCode } = await run(["rev-parse", "--verify", branch]);
|
|
113
|
+
return exitCode === 0;
|
|
114
|
+
}
|
|
156
115
|
async function fetchRemote(remote) {
|
|
157
116
|
return run(["fetch", remote]);
|
|
158
117
|
}
|
|
159
118
|
async function fetchAll() {
|
|
160
119
|
return run(["fetch", "--all", "--quiet"]);
|
|
161
120
|
}
|
|
162
|
-
async function
|
|
121
|
+
async function checkoutBranch2(branch) {
|
|
163
122
|
return run(["checkout", branch]);
|
|
164
123
|
}
|
|
165
124
|
async function createBranch(branch, from) {
|
|
@@ -169,12 +128,74 @@ async function createBranch(branch, from) {
|
|
|
169
128
|
async function resetHard(ref) {
|
|
170
129
|
return run(["reset", "--hard", ref]);
|
|
171
130
|
}
|
|
131
|
+
async function updateLocalBranch(branch, target) {
|
|
132
|
+
const current = await getCurrentBranch();
|
|
133
|
+
if (current === branch) {
|
|
134
|
+
return resetHard(target);
|
|
135
|
+
}
|
|
136
|
+
return run(["branch", "-f", branch, target]);
|
|
137
|
+
}
|
|
172
138
|
async function pushSetUpstream(remote, branch) {
|
|
173
139
|
return run(["push", "-u", remote, branch]);
|
|
174
140
|
}
|
|
175
141
|
async function rebase(branch) {
|
|
176
142
|
return run(["rebase", branch]);
|
|
177
143
|
}
|
|
144
|
+
async function getUpstreamRef() {
|
|
145
|
+
const { exitCode, stdout } = await run([
|
|
146
|
+
"rev-parse",
|
|
147
|
+
"--abbrev-ref",
|
|
148
|
+
"--symbolic-full-name",
|
|
149
|
+
"@{u}"
|
|
150
|
+
]);
|
|
151
|
+
if (exitCode !== 0)
|
|
152
|
+
return null;
|
|
153
|
+
return stdout.trim() || null;
|
|
154
|
+
}
|
|
155
|
+
async function unsetUpstream() {
|
|
156
|
+
return run(["branch", "--unset-upstream"]);
|
|
157
|
+
}
|
|
158
|
+
async function rebaseOnto(newBase, oldBase) {
|
|
159
|
+
return run(["rebase", "--onto", newBase, oldBase]);
|
|
160
|
+
}
|
|
161
|
+
async function getMergeBase(ref1, ref2) {
|
|
162
|
+
const { exitCode, stdout } = await run(["merge-base", ref1, ref2]);
|
|
163
|
+
if (exitCode !== 0)
|
|
164
|
+
return null;
|
|
165
|
+
return stdout.trim() || null;
|
|
166
|
+
}
|
|
167
|
+
async function getCommitHash(ref) {
|
|
168
|
+
const { exitCode, stdout } = await run(["rev-parse", ref]);
|
|
169
|
+
if (exitCode !== 0)
|
|
170
|
+
return null;
|
|
171
|
+
return stdout.trim() || null;
|
|
172
|
+
}
|
|
173
|
+
async function determineRebaseStrategy(currentBranch, syncRef) {
|
|
174
|
+
const upstreamRef = await getUpstreamRef();
|
|
175
|
+
if (!upstreamRef) {
|
|
176
|
+
return { strategy: "plain" };
|
|
177
|
+
}
|
|
178
|
+
const upstreamHash = await getCommitHash(upstreamRef);
|
|
179
|
+
if (!upstreamHash) {
|
|
180
|
+
return { strategy: "plain" };
|
|
181
|
+
}
|
|
182
|
+
const slashIdx = upstreamRef.indexOf("/");
|
|
183
|
+
const upstreamBranchName = slashIdx !== -1 ? upstreamRef.slice(slashIdx + 1) : upstreamRef;
|
|
184
|
+
if (upstreamBranchName === currentBranch) {
|
|
185
|
+
return { strategy: "plain" };
|
|
186
|
+
}
|
|
187
|
+
const [forkFromUpstream, forkFromSync] = await Promise.all([
|
|
188
|
+
getMergeBase("HEAD", upstreamRef),
|
|
189
|
+
getMergeBase("HEAD", syncRef)
|
|
190
|
+
]);
|
|
191
|
+
if (forkFromUpstream && forkFromSync && forkFromUpstream === forkFromSync) {
|
|
192
|
+
return { strategy: "plain" };
|
|
193
|
+
}
|
|
194
|
+
if (forkFromUpstream) {
|
|
195
|
+
return { strategy: "onto", ontoOldBase: forkFromUpstream };
|
|
196
|
+
}
|
|
197
|
+
return { strategy: "plain" };
|
|
198
|
+
}
|
|
178
199
|
async function getStagedDiff() {
|
|
179
200
|
const { stdout } = await run(["diff", "--cached"]);
|
|
180
201
|
return stdout;
|
|
@@ -190,8 +211,16 @@ async function getChangedFiles() {
|
|
|
190
211
|
const { exitCode, stdout } = await run(["status", "--porcelain"]);
|
|
191
212
|
if (exitCode !== 0)
|
|
192
213
|
return [];
|
|
193
|
-
return stdout.
|
|
194
|
-
`).filter(Boolean).map((l) =>
|
|
214
|
+
return stdout.trimEnd().split(`
|
|
215
|
+
`).filter(Boolean).map((l) => {
|
|
216
|
+
const line = l.replace(/\r$/, "");
|
|
217
|
+
const match = line.match(/^..\s+(.*)/);
|
|
218
|
+
if (!match)
|
|
219
|
+
return "";
|
|
220
|
+
const file = match[1];
|
|
221
|
+
const renameIdx = file.indexOf(" -> ");
|
|
222
|
+
return renameIdx !== -1 ? file.slice(renameIdx + 4) : file;
|
|
223
|
+
}).filter(Boolean);
|
|
195
224
|
}
|
|
196
225
|
async function getDivergence(branch, base) {
|
|
197
226
|
const { exitCode, stdout } = await run([
|
|
@@ -215,9 +244,38 @@ async function getMergedBranches(base) {
|
|
|
215
244
|
return stdout.trim().split(`
|
|
216
245
|
`).map((b) => b.replace(/^\*?\s+/, "").trim()).filter(Boolean);
|
|
217
246
|
}
|
|
247
|
+
async function getGoneBranches() {
|
|
248
|
+
const { exitCode, stdout } = await run(["branch", "-vv"]);
|
|
249
|
+
if (exitCode !== 0)
|
|
250
|
+
return [];
|
|
251
|
+
return stdout.trimEnd().split(`
|
|
252
|
+
`).filter((line) => line.includes(": gone]")).map((line) => line.replace(/^\*?\s+/, "").split(/\s+/)[0]).filter(Boolean);
|
|
253
|
+
}
|
|
218
254
|
async function deleteBranch(branch) {
|
|
219
255
|
return run(["branch", "-d", branch]);
|
|
220
256
|
}
|
|
257
|
+
async function forceDeleteBranch(branch) {
|
|
258
|
+
return run(["branch", "-D", branch]);
|
|
259
|
+
}
|
|
260
|
+
async function renameBranch(oldName, newName) {
|
|
261
|
+
return run(["branch", "-m", oldName, newName]);
|
|
262
|
+
}
|
|
263
|
+
async function hasLocalWork(remote, branch) {
|
|
264
|
+
const uncommitted = await hasUncommittedChanges();
|
|
265
|
+
const trackingRef = `${remote}/${branch}`;
|
|
266
|
+
const { exitCode, stdout } = await run(["rev-list", "--count", `${trackingRef}..${branch}`]);
|
|
267
|
+
const unpushedCommits = exitCode === 0 ? Number.parseInt(stdout.trim(), 10) || 0 : 0;
|
|
268
|
+
return { uncommitted, unpushedCommits };
|
|
269
|
+
}
|
|
270
|
+
async function deleteRemoteBranch(remote, branch) {
|
|
271
|
+
return run(["push", remote, "--delete", branch]);
|
|
272
|
+
}
|
|
273
|
+
async function mergeSquash(branch) {
|
|
274
|
+
return run(["merge", "--squash", branch]);
|
|
275
|
+
}
|
|
276
|
+
async function pushBranch(remote, branch) {
|
|
277
|
+
return run(["push", remote, branch]);
|
|
278
|
+
}
|
|
221
279
|
async function pruneRemote(remote) {
|
|
222
280
|
return run(["remote", "prune", remote]);
|
|
223
281
|
}
|
|
@@ -238,10 +296,161 @@ async function getLog(base, head) {
|
|
|
238
296
|
async function pullBranch(remote, branch) {
|
|
239
297
|
return run(["pull", remote, branch]);
|
|
240
298
|
}
|
|
299
|
+
async function stageFiles(files) {
|
|
300
|
+
return run(["add", "--", ...files]);
|
|
301
|
+
}
|
|
302
|
+
async function unstageFiles(files) {
|
|
303
|
+
return run(["reset", "HEAD", "--", ...files]);
|
|
304
|
+
}
|
|
305
|
+
async function stageAll() {
|
|
306
|
+
return run(["add", "-A"]);
|
|
307
|
+
}
|
|
308
|
+
async function getFullDiffForFiles(files) {
|
|
309
|
+
const [unstaged, staged, untracked] = await Promise.all([
|
|
310
|
+
run(["diff", "--", ...files]),
|
|
311
|
+
run(["diff", "--cached", "--", ...files]),
|
|
312
|
+
getUntrackedFiles()
|
|
313
|
+
]);
|
|
314
|
+
const parts = [staged.stdout, unstaged.stdout].filter(Boolean);
|
|
315
|
+
const untrackedSet = new Set(untracked);
|
|
316
|
+
const MAX_FILE_CONTENT = 2000;
|
|
317
|
+
for (const file of files) {
|
|
318
|
+
if (untrackedSet.has(file)) {
|
|
319
|
+
try {
|
|
320
|
+
const content = readFileSync2(join2(process.cwd(), file), "utf-8");
|
|
321
|
+
const truncated = content.length > MAX_FILE_CONTENT ? `${content.slice(0, MAX_FILE_CONTENT)}
|
|
322
|
+
... (truncated)` : content;
|
|
323
|
+
const lines = truncated.split(`
|
|
324
|
+
`).map((l) => `+${l}`);
|
|
325
|
+
parts.push(`diff --git a/${file} b/${file}
|
|
326
|
+
new file
|
|
327
|
+
--- /dev/null
|
|
328
|
+
+++ b/${file}
|
|
329
|
+
${lines.join(`
|
|
330
|
+
`)}`);
|
|
331
|
+
} catch {}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return parts.join(`
|
|
335
|
+
`);
|
|
336
|
+
}
|
|
337
|
+
async function getUntrackedFiles() {
|
|
338
|
+
const { exitCode, stdout } = await run(["ls-files", "--others", "--exclude-standard"]);
|
|
339
|
+
if (exitCode !== 0)
|
|
340
|
+
return [];
|
|
341
|
+
return stdout.trim().split(`
|
|
342
|
+
`).filter(Boolean);
|
|
343
|
+
}
|
|
344
|
+
async function getFileStatus() {
|
|
345
|
+
const { exitCode, stdout } = await run(["status", "--porcelain"]);
|
|
346
|
+
if (exitCode !== 0)
|
|
347
|
+
return { staged: [], modified: [], untracked: [] };
|
|
348
|
+
const result = { staged: [], modified: [], untracked: [] };
|
|
349
|
+
const STATUS_LABELS = {
|
|
350
|
+
A: "new file",
|
|
351
|
+
M: "modified",
|
|
352
|
+
D: "deleted",
|
|
353
|
+
R: "renamed",
|
|
354
|
+
C: "copied",
|
|
355
|
+
T: "type changed"
|
|
356
|
+
};
|
|
357
|
+
for (const raw of stdout.trimEnd().split(`
|
|
358
|
+
`).filter(Boolean)) {
|
|
359
|
+
const line = raw.replace(/\r$/, "");
|
|
360
|
+
const indexStatus = line[0];
|
|
361
|
+
const workTreeStatus = line[1];
|
|
362
|
+
const pathPart = line.slice(3);
|
|
363
|
+
const renameIdx = pathPart.indexOf(" -> ");
|
|
364
|
+
const file = renameIdx !== -1 ? pathPart.slice(renameIdx + 4) : pathPart;
|
|
365
|
+
if (indexStatus === "?" && workTreeStatus === "?") {
|
|
366
|
+
result.untracked.push(file);
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
if (indexStatus && indexStatus !== " " && indexStatus !== "?") {
|
|
370
|
+
result.staged.push({ file, status: STATUS_LABELS[indexStatus] ?? indexStatus });
|
|
371
|
+
}
|
|
372
|
+
if (workTreeStatus && workTreeStatus !== " " && workTreeStatus !== "?") {
|
|
373
|
+
result.modified.push({ file, status: STATUS_LABELS[workTreeStatus] ?? workTreeStatus });
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
return result;
|
|
377
|
+
}
|
|
378
|
+
async function getLogGraph(options) {
|
|
379
|
+
const count = options?.count ?? 20;
|
|
380
|
+
const args = [
|
|
381
|
+
"log",
|
|
382
|
+
"--oneline",
|
|
383
|
+
"--graph",
|
|
384
|
+
"--decorate",
|
|
385
|
+
`--max-count=${count}`,
|
|
386
|
+
"--color=never"
|
|
387
|
+
];
|
|
388
|
+
if (options?.all) {
|
|
389
|
+
args.push("--all");
|
|
390
|
+
}
|
|
391
|
+
if (options?.branch) {
|
|
392
|
+
args.push(options.branch);
|
|
393
|
+
}
|
|
394
|
+
const { exitCode, stdout } = await run(args);
|
|
395
|
+
if (exitCode !== 0)
|
|
396
|
+
return [];
|
|
397
|
+
return stdout.trimEnd().split(`
|
|
398
|
+
`);
|
|
399
|
+
}
|
|
400
|
+
async function getLogEntries(options) {
|
|
401
|
+
const count = options?.count ?? 20;
|
|
402
|
+
const args = [
|
|
403
|
+
"log",
|
|
404
|
+
`--format=%h||%s||%D`,
|
|
405
|
+
`--max-count=${count}`
|
|
406
|
+
];
|
|
407
|
+
if (options?.all) {
|
|
408
|
+
args.push("--all");
|
|
409
|
+
}
|
|
410
|
+
if (options?.branch) {
|
|
411
|
+
args.push(options.branch);
|
|
412
|
+
}
|
|
413
|
+
const { exitCode, stdout } = await run(args);
|
|
414
|
+
if (exitCode !== 0)
|
|
415
|
+
return [];
|
|
416
|
+
return stdout.trimEnd().split(`
|
|
417
|
+
`).filter(Boolean).map((line) => {
|
|
418
|
+
const [hash = "", subject = "", refs = ""] = line.split("||");
|
|
419
|
+
return { hash: hash.trim(), subject: subject.trim(), refs: refs.trim() };
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
async function getLocalBranches() {
|
|
423
|
+
const { exitCode, stdout } = await run(["branch", "-vv", "--no-color"]);
|
|
424
|
+
if (exitCode !== 0)
|
|
425
|
+
return [];
|
|
426
|
+
return stdout.trimEnd().split(`
|
|
427
|
+
`).filter(Boolean).map((line) => {
|
|
428
|
+
const isCurrent = line.startsWith("*");
|
|
429
|
+
const trimmed = line.slice(2);
|
|
430
|
+
const nameMatch = trimmed.match(/^(\S+)/);
|
|
431
|
+
const name = nameMatch?.[1] ?? "";
|
|
432
|
+
const upstreamMatch = trimmed.match(/\[([^\]]+)\]/);
|
|
433
|
+
let upstream = null;
|
|
434
|
+
let gone = false;
|
|
435
|
+
if (upstreamMatch) {
|
|
436
|
+
const bracketContent = upstreamMatch[1];
|
|
437
|
+
gone = bracketContent.includes(": gone");
|
|
438
|
+
upstream = bracketContent.split(":")[0].trim();
|
|
439
|
+
}
|
|
440
|
+
return { name, isCurrent, upstream, gone };
|
|
441
|
+
}).filter((b) => b.name.length > 0);
|
|
442
|
+
}
|
|
443
|
+
async function getRemoteBranches() {
|
|
444
|
+
const { exitCode, stdout } = await run(["branch", "-r", "--no-color"]);
|
|
445
|
+
if (exitCode !== 0)
|
|
446
|
+
return [];
|
|
447
|
+
return stdout.trimEnd().split(`
|
|
448
|
+
`).map((line) => line.trim()).filter((line) => line.length > 0 && !line.includes(" -> "));
|
|
449
|
+
}
|
|
241
450
|
|
|
242
451
|
// src/utils/logger.ts
|
|
243
452
|
import { LogEngine, LogMode } from "@wgtechlabs/log-engine";
|
|
244
|
-
import
|
|
453
|
+
import pc from "picocolors";
|
|
245
454
|
LogEngine.configure({
|
|
246
455
|
mode: LogMode.INFO,
|
|
247
456
|
format: {
|
|
@@ -264,7 +473,7 @@ function info(msg) {
|
|
|
264
473
|
}
|
|
265
474
|
function heading(msg) {
|
|
266
475
|
console.log(`
|
|
267
|
-
${
|
|
476
|
+
${pc.bold(msg)}`);
|
|
268
477
|
}
|
|
269
478
|
|
|
270
479
|
// src/utils/workflow.ts
|
|
@@ -314,17 +523,23 @@ function getProtectedBranches(config) {
|
|
|
314
523
|
return branches;
|
|
315
524
|
}
|
|
316
525
|
|
|
317
|
-
// src/commands/
|
|
318
|
-
var
|
|
526
|
+
// src/commands/branch.ts
|
|
527
|
+
var branch_default = defineCommand({
|
|
319
528
|
meta: {
|
|
320
|
-
name: "
|
|
321
|
-
description: "
|
|
529
|
+
name: "branch",
|
|
530
|
+
description: "List branches with workflow-aware labels and status"
|
|
322
531
|
},
|
|
323
532
|
args: {
|
|
324
|
-
|
|
533
|
+
all: {
|
|
325
534
|
type: "boolean",
|
|
326
|
-
alias: "
|
|
327
|
-
description: "
|
|
535
|
+
alias: "a",
|
|
536
|
+
description: "Show both local and remote branches",
|
|
537
|
+
default: false
|
|
538
|
+
},
|
|
539
|
+
remote: {
|
|
540
|
+
type: "boolean",
|
|
541
|
+
alias: "r",
|
|
542
|
+
description: "Show only remote branches",
|
|
328
543
|
default: false
|
|
329
544
|
}
|
|
330
545
|
},
|
|
@@ -334,189 +549,277 @@ var clean_default = defineCommand({
|
|
|
334
549
|
process.exit(1);
|
|
335
550
|
}
|
|
336
551
|
const config = readConfig();
|
|
337
|
-
|
|
338
|
-
error("No .contributerc.json found. Run `contrib setup` first.");
|
|
339
|
-
process.exit(1);
|
|
340
|
-
}
|
|
341
|
-
const { origin } = config;
|
|
342
|
-
const baseBranch = getBaseBranch(config);
|
|
552
|
+
const protectedBranches = config ? getProtectedBranches(config) : ["main", "master"];
|
|
343
553
|
const currentBranch = await getCurrentBranch();
|
|
344
|
-
|
|
345
|
-
const
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
if (
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
${pc3.bold("Branches to delete:")}`);
|
|
353
|
-
for (const b of candidates) {
|
|
354
|
-
console.log(` ${pc3.dim("•")} ${b}`);
|
|
355
|
-
}
|
|
356
|
-
console.log();
|
|
357
|
-
const ok = args.yes || await confirmPrompt(`Delete ${pc3.bold(String(candidates.length))} merged branch${candidates.length !== 1 ? "es" : ""}?`);
|
|
358
|
-
if (!ok) {
|
|
359
|
-
info("Skipped branch deletion.");
|
|
554
|
+
const showRemoteOnly = args.remote;
|
|
555
|
+
const showAll = args.all;
|
|
556
|
+
heading("\uD83C\uDF3F branches");
|
|
557
|
+
console.log();
|
|
558
|
+
if (!showRemoteOnly) {
|
|
559
|
+
const localBranches = await getLocalBranches();
|
|
560
|
+
if (localBranches.length === 0) {
|
|
561
|
+
console.log(pc2.dim(" No local branches found."));
|
|
360
562
|
} else {
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
563
|
+
console.log(` ${pc2.bold("Local")}`);
|
|
564
|
+
console.log();
|
|
565
|
+
for (const branch of localBranches) {
|
|
566
|
+
const parts = [];
|
|
567
|
+
if (branch.isCurrent) {
|
|
568
|
+
parts.push(pc2.green("* "));
|
|
365
569
|
} else {
|
|
366
|
-
|
|
570
|
+
parts.push(" ");
|
|
367
571
|
}
|
|
572
|
+
const nameStr = colorBranchName(branch.name, protectedBranches, currentBranch);
|
|
573
|
+
parts.push(nameStr.padEnd(30));
|
|
574
|
+
if (branch.gone) {
|
|
575
|
+
parts.push(pc2.red(" ✗ remote gone"));
|
|
576
|
+
} else if (branch.upstream) {
|
|
577
|
+
parts.push(pc2.dim(` → ${branch.upstream}`));
|
|
578
|
+
} else {
|
|
579
|
+
parts.push(pc2.dim(" (no remote)"));
|
|
580
|
+
}
|
|
581
|
+
const labels = getBranchLabels(branch.name, protectedBranches, config);
|
|
582
|
+
if (labels.length > 0) {
|
|
583
|
+
parts.push(` ${labels.join(" ")}`);
|
|
584
|
+
}
|
|
585
|
+
console.log(` ${parts.join("")}`);
|
|
368
586
|
}
|
|
369
587
|
}
|
|
370
588
|
}
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
589
|
+
if (showRemoteOnly || showAll) {
|
|
590
|
+
const remoteBranches = await getRemoteBranches();
|
|
591
|
+
if (!showRemoteOnly) {
|
|
592
|
+
console.log();
|
|
593
|
+
}
|
|
594
|
+
if (remoteBranches.length === 0) {
|
|
595
|
+
console.log(pc2.dim(" No remote branches found."));
|
|
596
|
+
} else {
|
|
597
|
+
const grouped = groupByRemote(remoteBranches);
|
|
598
|
+
for (const [remote, branches] of Object.entries(grouped)) {
|
|
599
|
+
console.log(` ${pc2.bold(`Remote: ${remote}`)}`);
|
|
600
|
+
console.log();
|
|
601
|
+
for (const fullRef of branches) {
|
|
602
|
+
const branchName = fullRef.slice(remote.length + 1);
|
|
603
|
+
const nameStr = colorBranchName(branchName, protectedBranches, currentBranch);
|
|
604
|
+
const remotePrefix = pc2.dim(`${remote}/`);
|
|
605
|
+
console.log(` ${remotePrefix}${nameStr}`);
|
|
606
|
+
}
|
|
607
|
+
console.log();
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
const tips = [];
|
|
612
|
+
if (!showAll && !showRemoteOnly) {
|
|
613
|
+
tips.push(`Use ${pc2.bold("contrib branch -a")} to include remote branches`);
|
|
614
|
+
}
|
|
615
|
+
if (!showRemoteOnly) {
|
|
616
|
+
tips.push(`Use ${pc2.bold("contrib start")} to create a new feature branch`);
|
|
617
|
+
tips.push(`Use ${pc2.bold("contrib clean")} to remove merged/stale branches`);
|
|
377
618
|
}
|
|
619
|
+
if (tips.length > 0) {
|
|
620
|
+
console.log(` ${pc2.dim("\uD83D\uDCA1 Tip:")}`);
|
|
621
|
+
for (const tip of tips) {
|
|
622
|
+
console.log(` ${pc2.dim(tip)}`);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
console.log();
|
|
378
626
|
}
|
|
379
627
|
});
|
|
628
|
+
function colorBranchName(name, protectedBranches, currentBranch) {
|
|
629
|
+
if (name === currentBranch) {
|
|
630
|
+
return pc2.bold(pc2.green(name));
|
|
631
|
+
}
|
|
632
|
+
if (protectedBranches.includes(name)) {
|
|
633
|
+
return pc2.bold(pc2.red(name));
|
|
634
|
+
}
|
|
635
|
+
return name;
|
|
636
|
+
}
|
|
637
|
+
function getBranchLabels(name, protectedBranches, config) {
|
|
638
|
+
const labels = [];
|
|
639
|
+
if (protectedBranches.includes(name)) {
|
|
640
|
+
labels.push(pc2.dim(pc2.red("[protected]")));
|
|
641
|
+
}
|
|
642
|
+
if (config) {
|
|
643
|
+
if (name === config.mainBranch) {
|
|
644
|
+
labels.push(pc2.dim(pc2.cyan("[main]")));
|
|
645
|
+
}
|
|
646
|
+
if (config.devBranch && name === config.devBranch) {
|
|
647
|
+
labels.push(pc2.dim(pc2.cyan("[dev]")));
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
return labels;
|
|
651
|
+
}
|
|
652
|
+
function groupByRemote(branches) {
|
|
653
|
+
const grouped = {};
|
|
654
|
+
for (const ref of branches) {
|
|
655
|
+
const slashIdx = ref.indexOf("/");
|
|
656
|
+
const remote = slashIdx !== -1 ? ref.slice(0, slashIdx) : "unknown";
|
|
657
|
+
if (!grouped[remote]) {
|
|
658
|
+
grouped[remote] = [];
|
|
659
|
+
}
|
|
660
|
+
grouped[remote].push(ref);
|
|
661
|
+
}
|
|
662
|
+
return grouped;
|
|
663
|
+
}
|
|
380
664
|
|
|
381
|
-
// src/commands/
|
|
665
|
+
// src/commands/clean.ts
|
|
382
666
|
import { defineCommand as defineCommand2 } from "citty";
|
|
383
|
-
import
|
|
667
|
+
import pc5 from "picocolors";
|
|
384
668
|
|
|
385
|
-
// src/utils/
|
|
386
|
-
var
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
conventional: "Conventional Commits",
|
|
390
|
-
"clean-commit": "Clean Commit (by WGTech Labs)",
|
|
391
|
-
none: "No convention"
|
|
392
|
-
};
|
|
393
|
-
var CONVENTION_DESCRIPTIONS = {
|
|
394
|
-
conventional: "Conventional Commits — feat: | fix: | docs: | chore: etc. (conventionalcommits.org)",
|
|
395
|
-
"clean-commit": "Clean Commit — \uD83D\uDCE6 new: | \uD83D\uDD27 update: | \uD83D\uDDD1️ remove: etc. (by WGTech Labs)",
|
|
396
|
-
none: "No commit convention enforcement"
|
|
397
|
-
};
|
|
398
|
-
var CONVENTION_FORMAT_HINTS = {
|
|
399
|
-
conventional: [
|
|
400
|
-
"Format: <type>[!][(<scope>)]: <description>",
|
|
401
|
-
"Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert",
|
|
402
|
-
"Examples: feat: add login page | fix(auth): resolve token expiry | docs: update README"
|
|
403
|
-
],
|
|
404
|
-
"clean-commit": [
|
|
405
|
-
"Format: <emoji> <type>[!][(<scope>)]: <description>",
|
|
406
|
-
"Types: \uD83D\uDCE6 new | \uD83D\uDD27 update | \uD83D\uDDD1️ remove | \uD83D\uDD12 security | ⚙️ setup | ☕ chore | \uD83E\uDDEA test | \uD83D\uDCD6 docs | \uD83D\uDE80 release",
|
|
407
|
-
"Examples: \uD83D\uDCE6 new: user auth | \uD83D\uDD27 update (api): improve errors | ⚙️ setup (ci): add workflow"
|
|
408
|
-
]
|
|
409
|
-
};
|
|
410
|
-
function validateCommitMessage(message, convention) {
|
|
411
|
-
if (convention === "none")
|
|
412
|
-
return true;
|
|
413
|
-
if (convention === "clean-commit")
|
|
414
|
-
return CLEAN_COMMIT_PATTERN.test(message);
|
|
415
|
-
if (convention === "conventional")
|
|
416
|
-
return CONVENTIONAL_COMMIT_PATTERN.test(message);
|
|
417
|
-
return true;
|
|
669
|
+
// src/utils/branch.ts
|
|
670
|
+
var DEFAULT_PREFIXES = ["feature", "fix", "docs", "chore", "test", "refactor"];
|
|
671
|
+
function hasPrefix(branchName, prefixes = DEFAULT_PREFIXES) {
|
|
672
|
+
return prefixes.some((p) => branchName.startsWith(`${p}/`));
|
|
418
673
|
}
|
|
419
|
-
function
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
674
|
+
function formatBranchName(prefix, name) {
|
|
675
|
+
const sanitized = name.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
|
676
|
+
return `${prefix}/${sanitized}`;
|
|
677
|
+
}
|
|
678
|
+
function isValidBranchName(name) {
|
|
679
|
+
return /^[a-zA-Z0-9._/-]+$/.test(name) && !name.startsWith("/") && !name.endsWith("/");
|
|
680
|
+
}
|
|
681
|
+
function looksLikeNaturalLanguage(input) {
|
|
682
|
+
return input.includes(" ") && !input.includes("/");
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
// src/utils/confirm.ts
|
|
686
|
+
import * as clack from "@clack/prompts";
|
|
687
|
+
import pc3 from "picocolors";
|
|
688
|
+
function handleCancel(value) {
|
|
689
|
+
if (clack.isCancel(value)) {
|
|
690
|
+
clack.cancel("Cancelled.");
|
|
691
|
+
process.exit(0);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
async function confirmPrompt(message) {
|
|
695
|
+
const result = await clack.confirm({ message });
|
|
696
|
+
handleCancel(result);
|
|
697
|
+
return result;
|
|
698
|
+
}
|
|
699
|
+
async function selectPrompt(message, choices) {
|
|
700
|
+
const result = await clack.select({
|
|
701
|
+
message,
|
|
702
|
+
options: choices.map((choice) => ({ value: choice, label: choice }))
|
|
703
|
+
});
|
|
704
|
+
handleCancel(result);
|
|
705
|
+
return result;
|
|
706
|
+
}
|
|
707
|
+
async function inputPrompt(message, defaultValue) {
|
|
708
|
+
const result = await clack.text({
|
|
709
|
+
message,
|
|
710
|
+
placeholder: defaultValue,
|
|
711
|
+
defaultValue
|
|
712
|
+
});
|
|
713
|
+
handleCancel(result);
|
|
714
|
+
return result || defaultValue || "";
|
|
715
|
+
}
|
|
716
|
+
async function multiSelectPrompt(message, choices) {
|
|
717
|
+
const result = await clack.multiselect({
|
|
718
|
+
message: `${message} ${pc3.dim("(space to toggle, enter to confirm)")}`,
|
|
719
|
+
options: choices.map((choice) => ({ value: choice, label: choice })),
|
|
720
|
+
required: false
|
|
721
|
+
});
|
|
722
|
+
handleCancel(result);
|
|
723
|
+
return result;
|
|
426
724
|
}
|
|
427
725
|
|
|
428
726
|
// src/utils/copilot.ts
|
|
429
727
|
import { CopilotClient } from "@github/copilot-sdk";
|
|
430
|
-
var CONVENTIONAL_COMMIT_SYSTEM_PROMPT = `
|
|
431
|
-
|
|
728
|
+
var CONVENTIONAL_COMMIT_SYSTEM_PROMPT = `Git commit message generator. Format: <type>[!][(<scope>)]: <description>
|
|
729
|
+
Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert
|
|
730
|
+
Rules: breaking (!) only for feat/fix/refactor/perf; imperative mood; max 72 chars; lowercase start; scope optional camelCase/kebab-case. Return ONLY the message line.
|
|
731
|
+
Examples: feat: add user auth | fix(auth): resolve token expiry | feat!: redesign auth API`;
|
|
732
|
+
var CLEAN_COMMIT_SYSTEM_PROMPT = `Git commit message generator. EXACT format: <emoji> <type>[!][ (<scope>)]: <description>
|
|
733
|
+
Spacing: EMOJI SPACE TYPE [SPACE OPENPAREN SCOPE CLOSEPAREN] COLON SPACE DESCRIPTION
|
|
734
|
+
Types: \uD83D\uDCE6 new, \uD83D\uDD27 update, \uD83D\uDDD1️ remove, \uD83D\uDD12 security, ⚙️ setup, ☕ chore, \uD83E\uDDEA test, \uD83D\uDCD6 docs, \uD83D\uDE80 release
|
|
735
|
+
Rules: breaking (!) only for new/update/remove/security; imperative mood; max 72 chars; lowercase start; scope optional. Return ONLY the message line.
|
|
736
|
+
Correct: \uD83D\uDCE6 new: add user auth | \uD83D\uDD27 update (api): improve error handling | ⚙️ setup (ci): configure github actions
|
|
737
|
+
WRONG: ⚙️setup(ci): ... | \uD83D\uDD27 update(api): ... ← always space before scope parenthesis`;
|
|
738
|
+
function getGroupingSystemPrompt(convention) {
|
|
739
|
+
const conventionBlock = convention === "conventional" ? `Use Conventional Commit format: <type>[(<scope>)]: <description>
|
|
740
|
+
Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert` : `Use Clean Commit format: <emoji> <type>[!][ (<scope>)]: <description>
|
|
741
|
+
Emoji/type table:
|
|
742
|
+
\uD83D\uDCE6 new, \uD83D\uDD27 update, \uD83D\uDDD1️ remove, \uD83D\uDD12 security, ⚙️ setup, ☕ chore, \uD83E\uDDEA test, \uD83D\uDCD6 docs, \uD83D\uDE80 release`;
|
|
743
|
+
return `You are a smart commit grouping assistant. Given a list of changed files and their diffs, group related changes into logical atomic commits.
|
|
432
744
|
|
|
433
|
-
|
|
434
|
-
feat – a new feature
|
|
435
|
-
fix – a bug fix
|
|
436
|
-
docs – documentation only changes
|
|
437
|
-
style – changes that do not affect code meaning (whitespace, formatting)
|
|
438
|
-
refactor – code change that neither fixes a bug nor adds a feature
|
|
439
|
-
perf – performance improvement
|
|
440
|
-
test – adding or correcting tests
|
|
441
|
-
build – changes to the build system or external dependencies
|
|
442
|
-
ci – changes to CI configuration files and scripts
|
|
443
|
-
chore – other changes that don't modify src or test files
|
|
444
|
-
revert – reverts a previous commit
|
|
745
|
+
${conventionBlock}
|
|
445
746
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
feat: add user authentication system
|
|
454
|
-
fix(auth): resolve token expiry issue
|
|
455
|
-
docs: update contributing guidelines
|
|
456
|
-
feat!: redesign authentication API`;
|
|
457
|
-
var CLEAN_COMMIT_SYSTEM_PROMPT = `You are a git commit message generator. Generate a Clean Commit message following this exact format:
|
|
458
|
-
<emoji> <type>[!][(<scope>)]: <description>
|
|
459
|
-
|
|
460
|
-
Emoji and type table:
|
|
461
|
-
\uD83D\uDCE6 new – new features, files, or capabilities
|
|
462
|
-
\uD83D\uDD27 update – changes, refactoring, improvements
|
|
463
|
-
\uD83D\uDDD1️ remove – removing code, files, or dependencies
|
|
464
|
-
\uD83D\uDD12 security – security fixes or patches
|
|
465
|
-
⚙️ setup – configs, CI/CD, tooling, build systems
|
|
466
|
-
☕ chore – maintenance, dependency updates
|
|
467
|
-
\uD83E\uDDEA test – adding or updating tests
|
|
468
|
-
\uD83D\uDCD6 docs – documentation changes
|
|
469
|
-
\uD83D\uDE80 release – version releases
|
|
470
|
-
|
|
471
|
-
Rules:
|
|
472
|
-
- Breaking change (!) only for: new, update, remove, security
|
|
473
|
-
- Description: concise, imperative mood, max 72 chars
|
|
474
|
-
- Scope: optional, camelCase or kebab-case component name
|
|
475
|
-
- Return ONLY the commit message line, nothing else
|
|
476
|
-
|
|
477
|
-
Examples:
|
|
478
|
-
\uD83D\uDCE6 new: user authentication system
|
|
479
|
-
\uD83D\uDD27 update (api): improve error handling
|
|
480
|
-
⚙️ setup (ci): configure github actions workflow
|
|
481
|
-
\uD83D\uDCE6 new!: completely redesign authentication system`;
|
|
482
|
-
var BRANCH_NAME_SYSTEM_PROMPT = `You are a git branch name generator. Convert natural language descriptions into proper git branch names.
|
|
483
|
-
|
|
484
|
-
Format: <prefix>/<kebab-case-name>
|
|
485
|
-
Prefixes: feature, fix, docs, chore, test, refactor
|
|
747
|
+
Return a JSON array of commit groups with this EXACT structure (no markdown fences, no explanation):
|
|
748
|
+
[
|
|
749
|
+
{
|
|
750
|
+
"files": ["path/to/file1.ts", "path/to/file2.ts"],
|
|
751
|
+
"message": "<commit message following the convention above>"
|
|
752
|
+
}
|
|
753
|
+
]
|
|
486
754
|
|
|
487
755
|
Rules:
|
|
488
|
-
-
|
|
489
|
-
-
|
|
490
|
-
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
756
|
+
- Group files that are logically related (e.g. a utility and its tests, a feature and its types)
|
|
757
|
+
- Each group should represent ONE logical change
|
|
758
|
+
- Every file must appear in exactly one group
|
|
759
|
+
- Commit messages must follow the convention, be concise, imperative, max 72 chars
|
|
760
|
+
- Order groups so foundational changes come first (types, utils) and consumers come after
|
|
761
|
+
- Return ONLY the JSON array, nothing else`;
|
|
762
|
+
}
|
|
763
|
+
var BRANCH_NAME_SYSTEM_PROMPT = `You are a git branch name generator. Your ONLY job is to output a single git branch name. NOTHING ELSE.
|
|
764
|
+
Output format: <prefix>/<kebab-case-name>
|
|
765
|
+
Valid prefixes: feature, fix, docs, chore, test, refactor
|
|
766
|
+
Rules: lowercase, kebab-case, 2-5 words after the prefix, no punctuation.
|
|
767
|
+
CRITICAL: Output ONLY the branch name on a single line. No explanation. No markdown. No questions. No other text.
|
|
768
|
+
Examples: fix/login-timeout | feature/user-profile-page | docs/update-readme | chore/update-pr-title`;
|
|
769
|
+
var PR_DESCRIPTION_SYSTEM_PROMPT_BASE = `GitHub PR description generator. Return JSON: {"title":"<72 chars>","body":"## Summary\\n...\\n\\n## Changes\\n- ...\\n\\n## Test Plan\\n..."}
|
|
770
|
+
IMPORTANT: The title must capture the overall theme or goal of the PR — NOT enumerate individual changes. Think: what problem does this PR solve or what capability does it add? Keep it focused and specific but high-level.`;
|
|
771
|
+
function getPRDescriptionSystemPrompt(convention) {
|
|
772
|
+
if (convention === "clean-commit") {
|
|
773
|
+
return `${PR_DESCRIPTION_SYSTEM_PROMPT_BASE}
|
|
774
|
+
CRITICAL: The PR title MUST follow the Clean Commit format exactly: <emoji> <type>: <description>
|
|
775
|
+
Emoji/type table: \uD83D\uDCE6 new, \uD83D\uDD27 update, \uD83D\uDDD1️ remove, \uD83D\uDD12 security, ⚙️ setup, ☕ chore, \uD83E\uDDEA test, \uD83D\uDCD6 docs, \uD83D\uDE80 release
|
|
776
|
+
Title examples: \uD83D\uDCE6 new: add user authentication | \uD83D\uDD27 update: improve error handling | \uD83D\uDDD1️ remove: drop legacy API
|
|
777
|
+
Rules: title follows convention, present tense, max 72 chars, describes the PR theme not individual commits; body has Summary, Changes (bullets), Test Plan sections. Return ONLY the JSON object, no fences.`;
|
|
778
|
+
}
|
|
779
|
+
if (convention === "conventional") {
|
|
780
|
+
return `${PR_DESCRIPTION_SYSTEM_PROMPT_BASE}
|
|
781
|
+
CRITICAL: The PR title MUST follow Conventional Commits format: <type>[(<scope>)]: <description>
|
|
782
|
+
Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert
|
|
783
|
+
Title examples: feat: add user authentication | fix(auth): resolve token expiry | docs: update contributing guide
|
|
784
|
+
Rules: title follows convention, present tense, max 72 chars, describes the PR theme not individual commits; body has Summary, Changes (bullets), Test Plan sections. Return ONLY the JSON object, no fences.`;
|
|
785
|
+
}
|
|
786
|
+
return `${PR_DESCRIPTION_SYSTEM_PROMPT_BASE}
|
|
787
|
+
Rules: title concise present tense, describes the PR theme not individual commits; body has Summary, Changes (bullets), Test Plan sections. Return ONLY the JSON object, no fences.`;
|
|
788
|
+
}
|
|
789
|
+
var CONFLICT_RESOLUTION_SYSTEM_PROMPT = `Git merge conflict advisor. Explain each side, suggest resolution strategy. Never auto-resolve — guidance only. Be concise and actionable.`;
|
|
790
|
+
function suppressSubprocessWarnings() {
|
|
791
|
+
process.env.NODE_NO_WARNINGS = "1";
|
|
792
|
+
}
|
|
793
|
+
function withTimeout(promise, ms) {
|
|
794
|
+
return new Promise((resolve, reject) => {
|
|
795
|
+
const timer = setTimeout(() => reject(new Error(`Copilot request timed out after ${ms / 1000}s`)), ms);
|
|
796
|
+
promise.then((val) => {
|
|
797
|
+
clearTimeout(timer);
|
|
798
|
+
resolve(val);
|
|
799
|
+
}, (err) => {
|
|
800
|
+
clearTimeout(timer);
|
|
801
|
+
reject(err);
|
|
802
|
+
});
|
|
803
|
+
});
|
|
502
804
|
}
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
- title: concise, present tense, describes what the PR does
|
|
506
|
-
- body: markdown with Summary, Changes (bullet list), and Test Plan sections
|
|
507
|
-
- Return ONLY the JSON object, no markdown fences, no extra text`;
|
|
508
|
-
var CONFLICT_RESOLUTION_SYSTEM_PROMPT = `You are a git merge conflict resolution advisor. Analyze the conflict markers and provide guidance.
|
|
509
|
-
|
|
510
|
-
Rules:
|
|
511
|
-
- Explain what each side of the conflict contains
|
|
512
|
-
- Suggest the most likely correct resolution strategy
|
|
513
|
-
- Never auto-resolve — provide guidance only
|
|
514
|
-
- Be concise and actionable`;
|
|
805
|
+
var COPILOT_TIMEOUT_MS = 30000;
|
|
806
|
+
var COPILOT_LONG_TIMEOUT_MS = 90000;
|
|
515
807
|
async function checkCopilotAvailable() {
|
|
516
|
-
let client = null;
|
|
517
808
|
try {
|
|
518
|
-
client =
|
|
519
|
-
|
|
809
|
+
const client = await getManagedClient();
|
|
810
|
+
try {
|
|
811
|
+
await client.ping();
|
|
812
|
+
} catch (err) {
|
|
813
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
814
|
+
if (msg.includes("auth") || msg.includes("token") || msg.includes("401") || msg.includes("403")) {
|
|
815
|
+
return "Copilot authentication failed. Run `gh auth login` to refresh your token.";
|
|
816
|
+
}
|
|
817
|
+
if (msg.includes("ECONNREFUSED") || msg.includes("timeout") || msg.includes("network")) {
|
|
818
|
+
return "Could not reach GitHub Copilot service. Check your internet connection.";
|
|
819
|
+
}
|
|
820
|
+
return `Copilot health check failed: ${msg}`;
|
|
821
|
+
}
|
|
822
|
+
return null;
|
|
520
823
|
} catch (err) {
|
|
521
824
|
const msg = err instanceof Error ? err.message : String(err);
|
|
522
825
|
if (msg.includes("ENOENT") || msg.includes("not found")) {
|
|
@@ -524,44 +827,45 @@ async function checkCopilotAvailable() {
|
|
|
524
827
|
}
|
|
525
828
|
return `Failed to start Copilot service: ${msg}`;
|
|
526
829
|
}
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
830
|
+
}
|
|
831
|
+
var _managedClient = null;
|
|
832
|
+
var _clientStarted = false;
|
|
833
|
+
async function getManagedClient() {
|
|
834
|
+
if (!_managedClient || !_clientStarted) {
|
|
835
|
+
suppressSubprocessWarnings();
|
|
836
|
+
_managedClient = new CopilotClient;
|
|
837
|
+
await _managedClient.start();
|
|
838
|
+
_clientStarted = true;
|
|
839
|
+
const cleanup = () => {
|
|
840
|
+
if (_managedClient && _clientStarted) {
|
|
841
|
+
try {
|
|
842
|
+
_managedClient.stop();
|
|
843
|
+
} catch {}
|
|
844
|
+
_clientStarted = false;
|
|
845
|
+
_managedClient = null;
|
|
846
|
+
}
|
|
847
|
+
};
|
|
848
|
+
process.once("exit", cleanup);
|
|
849
|
+
process.once("SIGINT", cleanup);
|
|
850
|
+
process.once("SIGTERM", cleanup);
|
|
542
851
|
}
|
|
543
|
-
return
|
|
852
|
+
return _managedClient;
|
|
544
853
|
}
|
|
545
|
-
async function callCopilot(systemMessage, userMessage, model) {
|
|
546
|
-
const client =
|
|
547
|
-
|
|
854
|
+
async function callCopilot(systemMessage, userMessage, model, timeoutMs = COPILOT_TIMEOUT_MS) {
|
|
855
|
+
const client = await getManagedClient();
|
|
856
|
+
const sessionConfig = {
|
|
857
|
+
systemMessage: { mode: "replace", content: systemMessage }
|
|
858
|
+
};
|
|
859
|
+
if (model)
|
|
860
|
+
sessionConfig.model = model;
|
|
861
|
+
const session = await client.createSession(sessionConfig);
|
|
548
862
|
try {
|
|
549
|
-
const
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
sessionConfig.model = model;
|
|
554
|
-
const session = await client.createSession(sessionConfig);
|
|
555
|
-
try {
|
|
556
|
-
const response = await session.sendAndWait({ content: userMessage });
|
|
557
|
-
if (!response?.data?.content)
|
|
558
|
-
return null;
|
|
559
|
-
return response.data.content;
|
|
560
|
-
} finally {
|
|
561
|
-
await session.destroy();
|
|
562
|
-
}
|
|
863
|
+
const response = await withTimeout(session.sendAndWait({ prompt: userMessage }), timeoutMs);
|
|
864
|
+
if (!response?.data?.content)
|
|
865
|
+
return null;
|
|
866
|
+
return response.data.content;
|
|
563
867
|
} finally {
|
|
564
|
-
await
|
|
868
|
+
await session.destroy();
|
|
565
869
|
}
|
|
566
870
|
}
|
|
567
871
|
function getCommitSystemPrompt(convention) {
|
|
@@ -569,21 +873,53 @@ function getCommitSystemPrompt(convention) {
|
|
|
569
873
|
return CONVENTIONAL_COMMIT_SYSTEM_PROMPT;
|
|
570
874
|
return CLEAN_COMMIT_SYSTEM_PROMPT;
|
|
571
875
|
}
|
|
876
|
+
function extractJson(raw) {
|
|
877
|
+
let text2 = raw.trim().replace(/^```(?:json)?\s*/i, "").replace(/\s*```$/i, "").trim();
|
|
878
|
+
if (text2.startsWith("[") || text2.startsWith("{"))
|
|
879
|
+
return text2;
|
|
880
|
+
const arrayStart = text2.indexOf("[");
|
|
881
|
+
const objStart = text2.indexOf("{");
|
|
882
|
+
let start;
|
|
883
|
+
let closeChar;
|
|
884
|
+
if (arrayStart === -1 && objStart === -1)
|
|
885
|
+
return text2;
|
|
886
|
+
if (arrayStart === -1) {
|
|
887
|
+
start = objStart;
|
|
888
|
+
closeChar = "}";
|
|
889
|
+
} else if (objStart === -1) {
|
|
890
|
+
start = arrayStart;
|
|
891
|
+
closeChar = "]";
|
|
892
|
+
} else if (arrayStart < objStart) {
|
|
893
|
+
start = arrayStart;
|
|
894
|
+
closeChar = "]";
|
|
895
|
+
} else {
|
|
896
|
+
start = objStart;
|
|
897
|
+
closeChar = "}";
|
|
898
|
+
}
|
|
899
|
+
const end = text2.lastIndexOf(closeChar);
|
|
900
|
+
if (end > start) {
|
|
901
|
+
text2 = text2.slice(start, end + 1);
|
|
902
|
+
}
|
|
903
|
+
return text2;
|
|
904
|
+
}
|
|
572
905
|
async function generateCommitMessage(diff, stagedFiles, model, convention = "clean-commit") {
|
|
573
906
|
try {
|
|
907
|
+
const multiFileHint = stagedFiles.length > 1 ? `
|
|
908
|
+
|
|
909
|
+
IMPORTANT: Multiple files are staged. Generate ONE commit message that captures the high-level purpose of ALL changes together. Focus on the overall intent, not individual file changes. Be specific but concise — do not list every file.` : "";
|
|
574
910
|
const userMessage = `Generate a commit message for these staged changes:
|
|
575
911
|
|
|
576
912
|
Files: ${stagedFiles.join(", ")}
|
|
577
913
|
|
|
578
914
|
Diff:
|
|
579
|
-
${diff.slice(0, 4000)}`;
|
|
915
|
+
${diff.slice(0, 4000)}${multiFileHint}`;
|
|
580
916
|
const result = await callCopilot(getCommitSystemPrompt(convention), userMessage, model);
|
|
581
917
|
return result?.trim() ?? null;
|
|
582
918
|
} catch {
|
|
583
919
|
return null;
|
|
584
920
|
}
|
|
585
921
|
}
|
|
586
|
-
async function generatePRDescription(commits, diff, model) {
|
|
922
|
+
async function generatePRDescription(commits, diff, model, convention = "clean-commit") {
|
|
587
923
|
try {
|
|
588
924
|
const userMessage = `Generate a PR description for these changes:
|
|
589
925
|
|
|
@@ -593,10 +929,10 @@ ${commits.join(`
|
|
|
593
929
|
|
|
594
930
|
Diff (truncated):
|
|
595
931
|
${diff.slice(0, 4000)}`;
|
|
596
|
-
const result = await callCopilot(
|
|
932
|
+
const result = await callCopilot(getPRDescriptionSystemPrompt(convention), userMessage, model);
|
|
597
933
|
if (!result)
|
|
598
934
|
return null;
|
|
599
|
-
const cleaned = result
|
|
935
|
+
const cleaned = extractJson(result);
|
|
600
936
|
return JSON.parse(cleaned);
|
|
601
937
|
} catch {
|
|
602
938
|
return null;
|
|
@@ -605,7 +941,11 @@ ${diff.slice(0, 4000)}`;
|
|
|
605
941
|
async function suggestBranchName(description, model) {
|
|
606
942
|
try {
|
|
607
943
|
const result = await callCopilot(BRANCH_NAME_SYSTEM_PROMPT, description, model);
|
|
608
|
-
|
|
944
|
+
const trimmed = result?.trim() ?? null;
|
|
945
|
+
if (trimmed && /^[a-z]+\/[a-z0-9-]+$/.test(trimmed)) {
|
|
946
|
+
return trimmed;
|
|
947
|
+
}
|
|
948
|
+
return null;
|
|
609
949
|
} catch {
|
|
610
950
|
return null;
|
|
611
951
|
}
|
|
@@ -621,21 +961,359 @@ ${conflictDiff.slice(0, 4000)}`;
|
|
|
621
961
|
return null;
|
|
622
962
|
}
|
|
623
963
|
}
|
|
964
|
+
async function generateCommitGroups(files, diffs, model, convention = "clean-commit") {
|
|
965
|
+
const userMessage = `Group these changed files into logical atomic commits:
|
|
624
966
|
|
|
625
|
-
|
|
626
|
-
|
|
967
|
+
Files:
|
|
968
|
+
${files.join(`
|
|
969
|
+
`)}
|
|
970
|
+
|
|
971
|
+
Diffs (truncated):
|
|
972
|
+
${diffs.slice(0, 6000)}`;
|
|
973
|
+
const result = await callCopilot(getGroupingSystemPrompt(convention), userMessage, model, COPILOT_LONG_TIMEOUT_MS);
|
|
974
|
+
if (!result) {
|
|
975
|
+
throw new Error("AI returned an empty response");
|
|
976
|
+
}
|
|
977
|
+
const cleaned = extractJson(result);
|
|
978
|
+
let parsed;
|
|
979
|
+
try {
|
|
980
|
+
parsed = JSON.parse(cleaned);
|
|
981
|
+
} catch {
|
|
982
|
+
throw new Error(`AI response is not valid JSON. Raw start: "${result.slice(0, 120)}..."`);
|
|
983
|
+
}
|
|
984
|
+
const groups = parsed;
|
|
985
|
+
if (!Array.isArray(groups) || groups.length === 0) {
|
|
986
|
+
throw new Error("AI response was not a valid JSON array of commit groups");
|
|
987
|
+
}
|
|
988
|
+
for (const group of groups) {
|
|
989
|
+
if (!Array.isArray(group.files) || typeof group.message !== "string") {
|
|
990
|
+
throw new Error("AI returned groups with invalid structure (missing files or message)");
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
return groups;
|
|
994
|
+
}
|
|
995
|
+
async function regenerateAllGroupMessages(groups, diffs, model, convention = "clean-commit") {
|
|
996
|
+
const groupSummary = groups.map((g, i) => `Group ${i + 1}: [${g.files.join(", ")}]`).join(`
|
|
997
|
+
`);
|
|
998
|
+
const userMessage = `Regenerate ONLY the commit messages for these pre-defined file groups. Do NOT change the file groupings.
|
|
999
|
+
|
|
1000
|
+
Groups:
|
|
1001
|
+
${groupSummary}
|
|
1002
|
+
|
|
1003
|
+
Diffs (truncated):
|
|
1004
|
+
${diffs.slice(0, 6000)}`;
|
|
1005
|
+
const result = await callCopilot(getGroupingSystemPrompt(convention), userMessage, model, COPILOT_LONG_TIMEOUT_MS);
|
|
1006
|
+
if (!result)
|
|
1007
|
+
return groups;
|
|
1008
|
+
try {
|
|
1009
|
+
const cleaned = extractJson(result);
|
|
1010
|
+
const parsed = JSON.parse(cleaned);
|
|
1011
|
+
if (!Array.isArray(parsed) || parsed.length !== groups.length)
|
|
1012
|
+
return groups;
|
|
1013
|
+
return groups.map((g, i) => ({
|
|
1014
|
+
files: g.files,
|
|
1015
|
+
message: typeof parsed[i]?.message === "string" ? parsed[i].message : g.message
|
|
1016
|
+
}));
|
|
1017
|
+
} catch {
|
|
1018
|
+
return groups;
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
async function regenerateGroupMessage(files, diffs, model, convention = "clean-commit") {
|
|
1022
|
+
try {
|
|
1023
|
+
const userMessage = `Generate a single commit message for these files:
|
|
1024
|
+
|
|
1025
|
+
Files: ${files.join(", ")}
|
|
1026
|
+
|
|
1027
|
+
Diff:
|
|
1028
|
+
${diffs.slice(0, 4000)}`;
|
|
1029
|
+
const result = await callCopilot(getCommitSystemPrompt(convention), userMessage, model);
|
|
1030
|
+
return result?.trim() ?? null;
|
|
1031
|
+
} catch {
|
|
1032
|
+
return null;
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
// src/utils/gh.ts
|
|
1037
|
+
import { execFile as execFileCb2 } from "node:child_process";
|
|
1038
|
+
function run2(args) {
|
|
1039
|
+
return new Promise((resolve) => {
|
|
1040
|
+
execFileCb2("gh", args, (error2, stdout, stderr) => {
|
|
1041
|
+
resolve({
|
|
1042
|
+
exitCode: error2 ? error2.code === "ENOENT" ? 127 : error2.status ?? 1 : 0,
|
|
1043
|
+
stdout: stdout ?? "",
|
|
1044
|
+
stderr: stderr ?? ""
|
|
1045
|
+
});
|
|
1046
|
+
});
|
|
1047
|
+
});
|
|
1048
|
+
}
|
|
1049
|
+
async function checkGhInstalled() {
|
|
1050
|
+
try {
|
|
1051
|
+
const { exitCode } = await run2(["--version"]);
|
|
1052
|
+
return exitCode === 0;
|
|
1053
|
+
} catch {
|
|
1054
|
+
return false;
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
async function checkGhAuth() {
|
|
1058
|
+
try {
|
|
1059
|
+
const { exitCode } = await run2(["auth", "status"]);
|
|
1060
|
+
return exitCode === 0;
|
|
1061
|
+
} catch {
|
|
1062
|
+
return false;
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
var SAFE_SLUG = /^[\w.-]+$/;
|
|
1066
|
+
async function checkRepoPermissions(owner, repo) {
|
|
1067
|
+
if (!SAFE_SLUG.test(owner) || !SAFE_SLUG.test(repo))
|
|
1068
|
+
return null;
|
|
1069
|
+
const { exitCode, stdout } = await run2(["api", `repos/${owner}/${repo}`, "--jq", ".permissions"]);
|
|
1070
|
+
if (exitCode !== 0)
|
|
1071
|
+
return null;
|
|
1072
|
+
try {
|
|
1073
|
+
return JSON.parse(stdout.trim());
|
|
1074
|
+
} catch {
|
|
1075
|
+
return null;
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
async function isRepoFork() {
|
|
1079
|
+
const { exitCode, stdout } = await run2(["repo", "view", "--json", "isFork", "-q", ".isFork"]);
|
|
1080
|
+
if (exitCode !== 0)
|
|
1081
|
+
return null;
|
|
1082
|
+
const val = stdout.trim();
|
|
1083
|
+
if (val === "true")
|
|
1084
|
+
return true;
|
|
1085
|
+
if (val === "false")
|
|
1086
|
+
return false;
|
|
1087
|
+
return null;
|
|
1088
|
+
}
|
|
1089
|
+
async function getCurrentRepoInfo() {
|
|
1090
|
+
const { exitCode, stdout } = await run2([
|
|
1091
|
+
"repo",
|
|
1092
|
+
"view",
|
|
1093
|
+
"--json",
|
|
1094
|
+
"nameWithOwner",
|
|
1095
|
+
"-q",
|
|
1096
|
+
".nameWithOwner"
|
|
1097
|
+
]);
|
|
1098
|
+
if (exitCode !== 0)
|
|
1099
|
+
return null;
|
|
1100
|
+
const nameWithOwner = stdout.trim();
|
|
1101
|
+
if (!nameWithOwner)
|
|
1102
|
+
return null;
|
|
1103
|
+
const [owner, repo] = nameWithOwner.split("/");
|
|
1104
|
+
if (!owner || !repo)
|
|
1105
|
+
return null;
|
|
1106
|
+
return { owner, repo };
|
|
1107
|
+
}
|
|
1108
|
+
async function createPR(options) {
|
|
1109
|
+
const args = [
|
|
1110
|
+
"pr",
|
|
1111
|
+
"create",
|
|
1112
|
+
"--base",
|
|
1113
|
+
options.base,
|
|
1114
|
+
"--title",
|
|
1115
|
+
options.title,
|
|
1116
|
+
"--body",
|
|
1117
|
+
options.body
|
|
1118
|
+
];
|
|
1119
|
+
if (options.draft)
|
|
1120
|
+
args.push("--draft");
|
|
1121
|
+
return run2(args);
|
|
1122
|
+
}
|
|
1123
|
+
async function createPRFill(base, draft) {
|
|
1124
|
+
const args = ["pr", "create", "--base", base, "--fill"];
|
|
1125
|
+
if (draft)
|
|
1126
|
+
args.push("--draft");
|
|
1127
|
+
return run2(args);
|
|
1128
|
+
}
|
|
1129
|
+
async function getPRForBranch(headBranch) {
|
|
1130
|
+
const { exitCode, stdout } = await run2([
|
|
1131
|
+
"pr",
|
|
1132
|
+
"list",
|
|
1133
|
+
"--head",
|
|
1134
|
+
headBranch,
|
|
1135
|
+
"--state",
|
|
1136
|
+
"open",
|
|
1137
|
+
"--json",
|
|
1138
|
+
"number,url,title,state",
|
|
1139
|
+
"--limit",
|
|
1140
|
+
"1"
|
|
1141
|
+
]);
|
|
1142
|
+
if (exitCode !== 0)
|
|
1143
|
+
return null;
|
|
1144
|
+
try {
|
|
1145
|
+
const prs = JSON.parse(stdout.trim());
|
|
1146
|
+
return prs.length > 0 ? prs[0] : null;
|
|
1147
|
+
} catch {
|
|
1148
|
+
return null;
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
async function getMergedPRForBranch(headBranch) {
|
|
1152
|
+
const { exitCode, stdout } = await run2([
|
|
1153
|
+
"pr",
|
|
1154
|
+
"list",
|
|
1155
|
+
"--head",
|
|
1156
|
+
headBranch,
|
|
1157
|
+
"--state",
|
|
1158
|
+
"merged",
|
|
1159
|
+
"--json",
|
|
1160
|
+
"number,url,title,state",
|
|
1161
|
+
"--limit",
|
|
1162
|
+
"1"
|
|
1163
|
+
]);
|
|
1164
|
+
if (exitCode !== 0)
|
|
1165
|
+
return null;
|
|
1166
|
+
try {
|
|
1167
|
+
const prs = JSON.parse(stdout.trim());
|
|
1168
|
+
return prs.length > 0 ? prs[0] : null;
|
|
1169
|
+
} catch {
|
|
1170
|
+
return null;
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
// src/utils/spinner.ts
|
|
1175
|
+
import pc4 from "picocolors";
|
|
1176
|
+
var FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
1177
|
+
function createSpinner(text2) {
|
|
1178
|
+
let frameIdx = 0;
|
|
1179
|
+
let currentText = text2;
|
|
1180
|
+
let stopped = false;
|
|
1181
|
+
const clearLine = () => {
|
|
1182
|
+
process.stderr.write("\r\x1B[K");
|
|
1183
|
+
};
|
|
1184
|
+
const render = () => {
|
|
1185
|
+
if (stopped)
|
|
1186
|
+
return;
|
|
1187
|
+
const frame = pc4.cyan(FRAMES[frameIdx % FRAMES.length]);
|
|
1188
|
+
clearLine();
|
|
1189
|
+
process.stderr.write(`${frame} ${currentText}`);
|
|
1190
|
+
frameIdx++;
|
|
1191
|
+
};
|
|
1192
|
+
const timer = setInterval(render, 80);
|
|
1193
|
+
render();
|
|
1194
|
+
const stop = () => {
|
|
1195
|
+
if (stopped)
|
|
1196
|
+
return;
|
|
1197
|
+
stopped = true;
|
|
1198
|
+
clearInterval(timer);
|
|
1199
|
+
clearLine();
|
|
1200
|
+
};
|
|
1201
|
+
return {
|
|
1202
|
+
update(newText) {
|
|
1203
|
+
currentText = newText;
|
|
1204
|
+
},
|
|
1205
|
+
success(msg) {
|
|
1206
|
+
stop();
|
|
1207
|
+
process.stderr.write(`${pc4.green("✔")} ${msg}
|
|
1208
|
+
`);
|
|
1209
|
+
},
|
|
1210
|
+
fail(msg) {
|
|
1211
|
+
stop();
|
|
1212
|
+
process.stderr.write(`${pc4.red("✖")} ${msg}
|
|
1213
|
+
`);
|
|
1214
|
+
},
|
|
1215
|
+
stop() {
|
|
1216
|
+
stop();
|
|
1217
|
+
}
|
|
1218
|
+
};
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
// src/commands/clean.ts
|
|
1222
|
+
async function handleCurrentBranchDeletion(currentBranch, baseBranch, config) {
|
|
1223
|
+
if (!config)
|
|
1224
|
+
return "skipped";
|
|
1225
|
+
const { origin } = config;
|
|
1226
|
+
const localWork = await hasLocalWork(origin, currentBranch);
|
|
1227
|
+
const hasWork = localWork.uncommitted || localWork.unpushedCommits > 0;
|
|
1228
|
+
if (hasWork) {
|
|
1229
|
+
if (localWork.uncommitted) {
|
|
1230
|
+
warn("You have uncommitted changes in your working tree.");
|
|
1231
|
+
}
|
|
1232
|
+
if (localWork.unpushedCommits > 0) {
|
|
1233
|
+
warn(`You have ${pc5.bold(String(localWork.unpushedCommits))} local commit${localWork.unpushedCommits !== 1 ? "s" : ""} not pushed.`);
|
|
1234
|
+
}
|
|
1235
|
+
const SAVE_NEW_BRANCH = "Save changes to a new branch";
|
|
1236
|
+
const DISCARD = "Discard all changes and clean up";
|
|
1237
|
+
const CANCEL = "Skip this branch";
|
|
1238
|
+
const action = await selectPrompt(`${pc5.bold(currentBranch)} has local changes. What would you like to do?`, [SAVE_NEW_BRANCH, DISCARD, CANCEL]);
|
|
1239
|
+
if (action === CANCEL)
|
|
1240
|
+
return "skipped";
|
|
1241
|
+
if (action === SAVE_NEW_BRANCH) {
|
|
1242
|
+
if (!config)
|
|
1243
|
+
return "skipped";
|
|
1244
|
+
info(pc5.dim("Tip: Describe what you're going to work on in plain English and we'll generate a branch name."));
|
|
1245
|
+
const description = await inputPrompt("What are you going to work on?");
|
|
1246
|
+
let newBranchName = description;
|
|
1247
|
+
if (looksLikeNaturalLanguage(description)) {
|
|
1248
|
+
const spinner = createSpinner("Generating branch name suggestion...");
|
|
1249
|
+
const suggested = await suggestBranchName(description);
|
|
1250
|
+
if (suggested) {
|
|
1251
|
+
spinner.success("Branch name suggestion ready.");
|
|
1252
|
+
console.log(`
|
|
1253
|
+
${pc5.dim("AI suggestion:")} ${pc5.bold(pc5.cyan(suggested))}`);
|
|
1254
|
+
const accepted = await confirmPrompt(`Use ${pc5.bold(suggested)} as your branch name?`);
|
|
1255
|
+
newBranchName = accepted ? suggested : await inputPrompt("Enter branch name", description);
|
|
1256
|
+
} else {
|
|
1257
|
+
spinner.fail("AI did not return a suggestion.");
|
|
1258
|
+
newBranchName = await inputPrompt("Enter branch name", description);
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
if (!hasPrefix(newBranchName, config.branchPrefixes)) {
|
|
1262
|
+
const prefix = await selectPrompt(`Choose a branch type for ${pc5.bold(newBranchName)}:`, config.branchPrefixes);
|
|
1263
|
+
newBranchName = formatBranchName(prefix, newBranchName);
|
|
1264
|
+
}
|
|
1265
|
+
if (!isValidBranchName(newBranchName)) {
|
|
1266
|
+
error("Invalid branch name. Use only alphanumeric characters, dots, hyphens, underscores, and slashes.");
|
|
1267
|
+
return "skipped";
|
|
1268
|
+
}
|
|
1269
|
+
const renameResult = await renameBranch(currentBranch, newBranchName);
|
|
1270
|
+
if (renameResult.exitCode !== 0) {
|
|
1271
|
+
error(`Failed to rename branch: ${renameResult.stderr}`);
|
|
1272
|
+
return "skipped";
|
|
1273
|
+
}
|
|
1274
|
+
success(`Renamed ${pc5.bold(currentBranch)} → ${pc5.bold(newBranchName)}`);
|
|
1275
|
+
const syncSource2 = getSyncSource(config);
|
|
1276
|
+
await fetchRemote(syncSource2.remote);
|
|
1277
|
+
const savedUpstreamRef = await getUpstreamRef();
|
|
1278
|
+
const rebaseResult = savedUpstreamRef && savedUpstreamRef !== syncSource2.ref ? await rebaseOnto(syncSource2.ref, savedUpstreamRef) : await rebase(syncSource2.ref);
|
|
1279
|
+
if (rebaseResult.exitCode !== 0) {
|
|
1280
|
+
warn("Rebase encountered conflicts. Resolve them after cleanup:");
|
|
1281
|
+
info(` ${pc5.bold(`git checkout ${newBranchName} && git rebase --continue`)}`);
|
|
1282
|
+
} else {
|
|
1283
|
+
success(`Rebased ${pc5.bold(newBranchName)} onto ${pc5.bold(syncSource2.ref)}.`);
|
|
1284
|
+
}
|
|
1285
|
+
const coResult2 = await checkoutBranch(baseBranch);
|
|
1286
|
+
if (coResult2.exitCode !== 0) {
|
|
1287
|
+
error(`Failed to checkout ${baseBranch}: ${coResult2.stderr}`);
|
|
1288
|
+
return "saved";
|
|
1289
|
+
}
|
|
1290
|
+
await updateLocalBranch(baseBranch, syncSource2.ref);
|
|
1291
|
+
success(`Synced ${pc5.bold(baseBranch)} with ${pc5.bold(syncSource2.ref)}.`);
|
|
1292
|
+
return "saved";
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
const syncSource = getSyncSource(config);
|
|
1296
|
+
info(`Switching to ${pc5.bold(baseBranch)} and syncing...`);
|
|
1297
|
+
await fetchRemote(syncSource.remote);
|
|
1298
|
+
const coResult = await checkoutBranch(baseBranch);
|
|
1299
|
+
if (coResult.exitCode !== 0) {
|
|
1300
|
+
error(`Failed to checkout ${baseBranch}: ${coResult.stderr}`);
|
|
1301
|
+
return "skipped";
|
|
1302
|
+
}
|
|
1303
|
+
await updateLocalBranch(baseBranch, syncSource.ref);
|
|
1304
|
+
success(`Synced ${pc5.bold(baseBranch)} with ${pc5.bold(syncSource.ref)}.`);
|
|
1305
|
+
return "switched";
|
|
1306
|
+
}
|
|
1307
|
+
var clean_default = defineCommand2({
|
|
627
1308
|
meta: {
|
|
628
|
-
name: "
|
|
629
|
-
description: "
|
|
1309
|
+
name: "clean",
|
|
1310
|
+
description: "Delete merged branches and prune remote refs"
|
|
630
1311
|
},
|
|
631
1312
|
args: {
|
|
632
|
-
|
|
633
|
-
type: "string",
|
|
634
|
-
description: "AI model to use for commit message generation"
|
|
635
|
-
},
|
|
636
|
-
"no-ai": {
|
|
1313
|
+
yes: {
|
|
637
1314
|
type: "boolean",
|
|
638
|
-
|
|
1315
|
+
alias: "y",
|
|
1316
|
+
description: "Skip confirmation prompt",
|
|
639
1317
|
default: false
|
|
640
1318
|
}
|
|
641
1319
|
},
|
|
@@ -649,115 +1327,888 @@ var commit_default = defineCommand2({
|
|
|
649
1327
|
error("No .contributerc.json found. Run `contrib setup` first.");
|
|
650
1328
|
process.exit(1);
|
|
651
1329
|
}
|
|
652
|
-
|
|
653
|
-
const
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
1330
|
+
const { origin } = config;
|
|
1331
|
+
const baseBranch = getBaseBranch(config);
|
|
1332
|
+
let currentBranch = await getCurrentBranch();
|
|
1333
|
+
heading("\uD83E\uDDF9 contrib clean");
|
|
1334
|
+
info(`Pruning ${origin} remote refs...`);
|
|
1335
|
+
const pruneResult = await pruneRemote(origin);
|
|
1336
|
+
if (pruneResult.exitCode === 0) {
|
|
1337
|
+
success(`Pruned ${origin} remote refs.`);
|
|
1338
|
+
} else {
|
|
1339
|
+
warn(`Could not prune remote: ${pruneResult.stderr.trim()}`);
|
|
1340
|
+
}
|
|
1341
|
+
const protectedBranches = new Set(getProtectedBranches(config));
|
|
1342
|
+
const mergedBranches = await getMergedBranches(baseBranch);
|
|
1343
|
+
const mergedCandidates = mergedBranches.filter((b) => !protectedBranches.has(b));
|
|
1344
|
+
const goneBranches = await getGoneBranches();
|
|
1345
|
+
const goneCandidates = goneBranches.filter((b) => !protectedBranches.has(b) && !mergedCandidates.includes(b));
|
|
1346
|
+
if (currentBranch && !protectedBranches.has(currentBranch) && !mergedCandidates.includes(currentBranch) && !goneCandidates.includes(currentBranch)) {
|
|
1347
|
+
const ghInstalled = await checkGhInstalled();
|
|
1348
|
+
const ghAuthed = ghInstalled && await checkGhAuth();
|
|
1349
|
+
if (ghInstalled && ghAuthed) {
|
|
1350
|
+
const mergedPR = await getMergedPRForBranch(currentBranch);
|
|
1351
|
+
if (mergedPR) {
|
|
1352
|
+
warn(`PR #${mergedPR.number} (${pc5.bold(mergedPR.title)}) has already been merged.`);
|
|
1353
|
+
info(`Link: ${pc5.underline(mergedPR.url)}`);
|
|
1354
|
+
goneCandidates.push(currentBranch);
|
|
1355
|
+
}
|
|
659
1356
|
}
|
|
1357
|
+
}
|
|
1358
|
+
if (mergedCandidates.length > 0) {
|
|
660
1359
|
console.log(`
|
|
661
|
-
${
|
|
662
|
-
for (const
|
|
663
|
-
|
|
1360
|
+
${pc5.bold("Merged branches to delete:")}`);
|
|
1361
|
+
for (const b of mergedCandidates) {
|
|
1362
|
+
const marker = b === currentBranch ? pc5.yellow(" (current)") : "";
|
|
1363
|
+
console.log(` ${pc5.dim("•")} ${b}${marker}`);
|
|
664
1364
|
}
|
|
665
1365
|
console.log();
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
1366
|
+
const ok = args.yes || await confirmPrompt(`Delete ${pc5.bold(String(mergedCandidates.length))} merged branch${mergedCandidates.length !== 1 ? "es" : ""}?`);
|
|
1367
|
+
if (ok) {
|
|
1368
|
+
for (const branch of mergedCandidates) {
|
|
1369
|
+
if (branch === currentBranch) {
|
|
1370
|
+
const result2 = await handleCurrentBranchDeletion(currentBranch, baseBranch, config);
|
|
1371
|
+
if (result2 === "skipped") {
|
|
1372
|
+
warn(` Skipped ${branch}.`);
|
|
1373
|
+
continue;
|
|
1374
|
+
}
|
|
1375
|
+
if (result2 === "saved") {
|
|
1376
|
+
currentBranch = baseBranch;
|
|
1377
|
+
continue;
|
|
1378
|
+
}
|
|
1379
|
+
currentBranch = baseBranch;
|
|
1380
|
+
}
|
|
1381
|
+
const result = await deleteBranch(branch);
|
|
1382
|
+
if (result.exitCode === 0) {
|
|
1383
|
+
success(` Deleted ${pc5.bold(branch)}`);
|
|
1384
|
+
} else {
|
|
1385
|
+
warn(` Failed to delete ${branch}: ${result.stderr.trim()}`);
|
|
1386
|
+
}
|
|
686
1387
|
}
|
|
1388
|
+
} else {
|
|
1389
|
+
info("Skipped merged branch deletion.");
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
if (goneCandidates.length > 0) {
|
|
1393
|
+
console.log(`
|
|
1394
|
+
${pc5.bold("Stale branches (remote deleted, likely squash-merged):")}`);
|
|
1395
|
+
for (const b of goneCandidates) {
|
|
1396
|
+
const marker = b === currentBranch ? pc5.yellow(" (current)") : "";
|
|
1397
|
+
console.log(` ${pc5.dim("•")} ${b}${marker}`);
|
|
687
1398
|
}
|
|
1399
|
+
console.log();
|
|
1400
|
+
const ok = args.yes || await confirmPrompt(`Delete ${pc5.bold(String(goneCandidates.length))} stale branch${goneCandidates.length !== 1 ? "es" : ""}?`);
|
|
1401
|
+
if (ok) {
|
|
1402
|
+
for (const branch of goneCandidates) {
|
|
1403
|
+
if (branch === currentBranch) {
|
|
1404
|
+
const result2 = await handleCurrentBranchDeletion(currentBranch, baseBranch, config);
|
|
1405
|
+
if (result2 === "skipped") {
|
|
1406
|
+
warn(` Skipped ${branch}.`);
|
|
1407
|
+
continue;
|
|
1408
|
+
}
|
|
1409
|
+
if (result2 === "saved") {
|
|
1410
|
+
currentBranch = baseBranch;
|
|
1411
|
+
continue;
|
|
1412
|
+
}
|
|
1413
|
+
currentBranch = baseBranch;
|
|
1414
|
+
}
|
|
1415
|
+
const result = await forceDeleteBranch(branch);
|
|
1416
|
+
if (result.exitCode === 0) {
|
|
1417
|
+
success(` Deleted ${pc5.bold(branch)}`);
|
|
1418
|
+
} else {
|
|
1419
|
+
warn(` Failed to delete ${branch}: ${result.stderr.trim()}`);
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
} else {
|
|
1423
|
+
info("Skipped stale branch deletion.");
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
if (mergedCandidates.length === 0 && goneCandidates.length === 0) {
|
|
1427
|
+
info("No branches to clean up. Everything is tidy! \uD83E\uDDF9");
|
|
1428
|
+
}
|
|
1429
|
+
const finalBranch = await getCurrentBranch();
|
|
1430
|
+
if (finalBranch && protectedBranches.has(finalBranch)) {
|
|
1431
|
+
console.log();
|
|
1432
|
+
info(`You're on ${pc5.bold(finalBranch)}. Run ${pc5.bold("contrib start")} to begin a new feature.`);
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
});
|
|
1436
|
+
|
|
1437
|
+
// src/commands/commit.ts
|
|
1438
|
+
import { defineCommand as defineCommand3 } from "citty";
|
|
1439
|
+
import pc6 from "picocolors";
|
|
1440
|
+
|
|
1441
|
+
// src/utils/convention.ts
|
|
1442
|
+
var CLEAN_COMMIT_PATTERN = /^(📦|🔧|🗑\uFE0F?|🔒|⚙\uFE0F?|☕|🧪|📖|🚀) (new|update|remove|security|setup|chore|test|docs|release)(!?)( \([a-zA-Z0-9][a-zA-Z0-9-]*\))?: .{1,72}$/u;
|
|
1443
|
+
var CONVENTIONAL_COMMIT_PATTERN = /^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(!?)(\([a-zA-Z0-9][a-zA-Z0-9._-]*\))?: .{1,72}$/;
|
|
1444
|
+
var CONVENTION_LABELS = {
|
|
1445
|
+
conventional: "Conventional Commits",
|
|
1446
|
+
"clean-commit": "Clean Commit (by WGTech Labs)",
|
|
1447
|
+
none: "No convention"
|
|
1448
|
+
};
|
|
1449
|
+
var CONVENTION_DESCRIPTIONS = {
|
|
1450
|
+
conventional: "Conventional Commits — feat: | fix: | docs: | chore: etc. (conventionalcommits.org)",
|
|
1451
|
+
"clean-commit": "Clean Commit — \uD83D\uDCE6 new: | \uD83D\uDD27 update: | \uD83D\uDDD1️ remove: etc. (by WGTech Labs)",
|
|
1452
|
+
none: "No commit convention enforcement"
|
|
1453
|
+
};
|
|
1454
|
+
var CONVENTION_FORMAT_HINTS = {
|
|
1455
|
+
conventional: [
|
|
1456
|
+
"Format: <type>[!][(<scope>)]: <description>",
|
|
1457
|
+
"Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert",
|
|
1458
|
+
"Examples: feat: add login page | fix(auth): resolve token expiry | docs: update README"
|
|
1459
|
+
],
|
|
1460
|
+
"clean-commit": [
|
|
1461
|
+
"Format: <emoji> <type>[!][(<scope>)]: <description>",
|
|
1462
|
+
"Types: \uD83D\uDCE6 new | \uD83D\uDD27 update | \uD83D\uDDD1️ remove | \uD83D\uDD12 security | ⚙️ setup | ☕ chore | \uD83E\uDDEA test | \uD83D\uDCD6 docs | \uD83D\uDE80 release",
|
|
1463
|
+
"Examples: \uD83D\uDCE6 new: user auth | \uD83D\uDD27 update (api): improve errors | ⚙️ setup (ci): add workflow"
|
|
1464
|
+
]
|
|
1465
|
+
};
|
|
1466
|
+
function validateCommitMessage(message, convention) {
|
|
1467
|
+
if (convention === "none")
|
|
1468
|
+
return true;
|
|
1469
|
+
if (convention === "clean-commit")
|
|
1470
|
+
return CLEAN_COMMIT_PATTERN.test(message);
|
|
1471
|
+
if (convention === "conventional")
|
|
1472
|
+
return CONVENTIONAL_COMMIT_PATTERN.test(message);
|
|
1473
|
+
return true;
|
|
1474
|
+
}
|
|
1475
|
+
function getValidationError(convention) {
|
|
1476
|
+
if (convention === "none")
|
|
1477
|
+
return [];
|
|
1478
|
+
return [
|
|
1479
|
+
`Commit message does not follow ${CONVENTION_LABELS[convention]} format.`,
|
|
1480
|
+
...CONVENTION_FORMAT_HINTS[convention]
|
|
1481
|
+
];
|
|
1482
|
+
}
|
|
1483
|
+
|
|
1484
|
+
// src/commands/commit.ts
|
|
1485
|
+
var commit_default = defineCommand3({
|
|
1486
|
+
meta: {
|
|
1487
|
+
name: "commit",
|
|
1488
|
+
description: "Stage changes and create a commit message (AI-powered)"
|
|
1489
|
+
},
|
|
1490
|
+
args: {
|
|
1491
|
+
model: {
|
|
1492
|
+
type: "string",
|
|
1493
|
+
description: "AI model to use for commit message generation"
|
|
1494
|
+
},
|
|
1495
|
+
"no-ai": {
|
|
1496
|
+
type: "boolean",
|
|
1497
|
+
description: "Skip AI and write commit message manually",
|
|
1498
|
+
default: false
|
|
1499
|
+
},
|
|
1500
|
+
group: {
|
|
1501
|
+
type: "boolean",
|
|
1502
|
+
description: "AI groups related changes into separate atomic commits",
|
|
1503
|
+
default: false
|
|
1504
|
+
}
|
|
1505
|
+
},
|
|
1506
|
+
async run({ args }) {
|
|
1507
|
+
if (!await isGitRepo()) {
|
|
1508
|
+
error("Not inside a git repository.");
|
|
1509
|
+
process.exit(1);
|
|
1510
|
+
}
|
|
1511
|
+
const config = readConfig();
|
|
1512
|
+
if (!config) {
|
|
1513
|
+
error("No .contributerc.json found. Run `contrib setup` first.");
|
|
1514
|
+
process.exit(1);
|
|
1515
|
+
}
|
|
1516
|
+
heading("\uD83D\uDCBE contrib commit");
|
|
1517
|
+
if (args.group) {
|
|
1518
|
+
await runGroupCommit(args.model, config);
|
|
1519
|
+
return;
|
|
1520
|
+
}
|
|
1521
|
+
let stagedFiles = await getStagedFiles();
|
|
1522
|
+
if (stagedFiles.length === 0) {
|
|
1523
|
+
const changedFiles = await getChangedFiles();
|
|
1524
|
+
if (changedFiles.length === 0) {
|
|
1525
|
+
error("No changes to commit.");
|
|
1526
|
+
process.exit(1);
|
|
1527
|
+
}
|
|
1528
|
+
console.log(`
|
|
1529
|
+
${pc6.bold("Changed files:")}`);
|
|
1530
|
+
for (const f of changedFiles) {
|
|
1531
|
+
console.log(` ${pc6.dim("•")} ${f}`);
|
|
1532
|
+
}
|
|
1533
|
+
const stageAction = await selectPrompt("No staged changes. How would you like to stage?", [
|
|
1534
|
+
"Stage all changes",
|
|
1535
|
+
"Select files to stage",
|
|
1536
|
+
"Cancel"
|
|
1537
|
+
]);
|
|
1538
|
+
if (stageAction === "Cancel") {
|
|
1539
|
+
process.exit(0);
|
|
1540
|
+
}
|
|
1541
|
+
if (stageAction === "Stage all changes") {
|
|
1542
|
+
const result2 = await stageAll();
|
|
1543
|
+
if (result2.exitCode !== 0) {
|
|
1544
|
+
error(`Failed to stage files: ${result2.stderr}`);
|
|
1545
|
+
process.exit(1);
|
|
1546
|
+
}
|
|
1547
|
+
success("Staged all changes.");
|
|
1548
|
+
} else {
|
|
1549
|
+
const selected = await multiSelectPrompt("Select files to stage:", changedFiles);
|
|
1550
|
+
if (selected.length === 0) {
|
|
1551
|
+
error("No files selected.");
|
|
1552
|
+
process.exit(1);
|
|
1553
|
+
}
|
|
1554
|
+
const result2 = await stageFiles(selected);
|
|
1555
|
+
if (result2.exitCode !== 0) {
|
|
1556
|
+
error(`Failed to stage files: ${result2.stderr}`);
|
|
1557
|
+
process.exit(1);
|
|
1558
|
+
}
|
|
1559
|
+
success(`Staged ${selected.length} file(s).`);
|
|
1560
|
+
}
|
|
1561
|
+
stagedFiles = await getStagedFiles();
|
|
1562
|
+
if (stagedFiles.length === 0) {
|
|
1563
|
+
error("No staged changes after staging attempt.");
|
|
1564
|
+
process.exit(1);
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
info(`Staged files: ${stagedFiles.join(", ")}`);
|
|
1568
|
+
let commitMessage = null;
|
|
1569
|
+
const useAI = !args["no-ai"];
|
|
1570
|
+
if (useAI) {
|
|
1571
|
+
const [copilotError, diff] = await Promise.all([checkCopilotAvailable(), getStagedDiff()]);
|
|
1572
|
+
if (copilotError) {
|
|
1573
|
+
warn(`AI unavailable: ${copilotError}`);
|
|
1574
|
+
warn("Falling back to manual commit message entry.");
|
|
1575
|
+
} else {
|
|
1576
|
+
const spinner = createSpinner("Generating commit message with AI...");
|
|
1577
|
+
commitMessage = await generateCommitMessage(diff, stagedFiles, args.model, config.commitConvention);
|
|
1578
|
+
if (commitMessage) {
|
|
1579
|
+
spinner.success("AI commit message generated.");
|
|
1580
|
+
console.log(`
|
|
1581
|
+
${pc6.dim("AI suggestion:")} ${pc6.bold(pc6.cyan(commitMessage))}`);
|
|
1582
|
+
} else {
|
|
1583
|
+
spinner.fail("AI did not return a commit message.");
|
|
1584
|
+
warn("Falling back to manual entry.");
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
let finalMessage = null;
|
|
1589
|
+
if (commitMessage) {
|
|
1590
|
+
const action = await selectPrompt("What would you like to do?", [
|
|
1591
|
+
"Accept this message",
|
|
1592
|
+
"Edit this message",
|
|
1593
|
+
"Regenerate",
|
|
1594
|
+
"Write manually"
|
|
1595
|
+
]);
|
|
1596
|
+
if (action === "Accept this message") {
|
|
1597
|
+
finalMessage = commitMessage;
|
|
1598
|
+
} else if (action === "Edit this message") {
|
|
1599
|
+
finalMessage = await inputPrompt("Edit commit message", commitMessage);
|
|
1600
|
+
} else if (action === "Regenerate") {
|
|
1601
|
+
const spinner = createSpinner("Regenerating commit message...");
|
|
1602
|
+
const diff = await getStagedDiff();
|
|
1603
|
+
const regen = await generateCommitMessage(diff, stagedFiles, args.model, config.commitConvention);
|
|
1604
|
+
if (regen) {
|
|
1605
|
+
spinner.success("Commit message regenerated.");
|
|
1606
|
+
console.log(`
|
|
1607
|
+
${pc6.dim("AI suggestion:")} ${pc6.bold(pc6.cyan(regen))}`);
|
|
1608
|
+
const ok = await confirmPrompt("Use this message?");
|
|
1609
|
+
finalMessage = ok ? regen : await inputPrompt("Enter commit message manually");
|
|
1610
|
+
} else {
|
|
1611
|
+
spinner.fail("Regeneration failed.");
|
|
1612
|
+
finalMessage = await inputPrompt("Enter commit message");
|
|
1613
|
+
}
|
|
1614
|
+
} else {
|
|
1615
|
+
finalMessage = await inputPrompt("Enter commit message");
|
|
1616
|
+
}
|
|
1617
|
+
} else {
|
|
1618
|
+
const convention2 = config.commitConvention;
|
|
1619
|
+
if (convention2 !== "none") {
|
|
1620
|
+
console.log();
|
|
1621
|
+
for (const hint of CONVENTION_FORMAT_HINTS[convention2]) {
|
|
1622
|
+
console.log(pc6.dim(hint));
|
|
1623
|
+
}
|
|
1624
|
+
console.log();
|
|
1625
|
+
}
|
|
1626
|
+
finalMessage = await inputPrompt("Enter commit message");
|
|
1627
|
+
}
|
|
1628
|
+
if (!finalMessage) {
|
|
1629
|
+
error("No commit message provided.");
|
|
1630
|
+
process.exit(1);
|
|
1631
|
+
}
|
|
1632
|
+
const convention = config.commitConvention;
|
|
1633
|
+
if (!validateCommitMessage(finalMessage, convention)) {
|
|
1634
|
+
for (const line of getValidationError(convention)) {
|
|
1635
|
+
warn(line);
|
|
1636
|
+
}
|
|
1637
|
+
const proceed = await confirmPrompt("Commit anyway?");
|
|
1638
|
+
if (!proceed)
|
|
1639
|
+
process.exit(1);
|
|
1640
|
+
}
|
|
1641
|
+
const result = await commitWithMessage(finalMessage);
|
|
1642
|
+
if (result.exitCode !== 0) {
|
|
1643
|
+
error(`Failed to commit: ${result.stderr}`);
|
|
1644
|
+
process.exit(1);
|
|
1645
|
+
}
|
|
1646
|
+
success(`✅ Committed: ${pc6.bold(finalMessage)}`);
|
|
1647
|
+
}
|
|
1648
|
+
});
|
|
1649
|
+
async function runGroupCommit(model, config) {
|
|
1650
|
+
const [copilotError, changedFiles] = await Promise.all([
|
|
1651
|
+
checkCopilotAvailable(),
|
|
1652
|
+
getChangedFiles()
|
|
1653
|
+
]);
|
|
1654
|
+
if (copilotError) {
|
|
1655
|
+
error(`AI is required for --group mode but unavailable: ${copilotError}`);
|
|
1656
|
+
process.exit(1);
|
|
1657
|
+
}
|
|
1658
|
+
if (changedFiles.length === 0) {
|
|
1659
|
+
error("No changes to group-commit.");
|
|
1660
|
+
process.exit(1);
|
|
1661
|
+
}
|
|
1662
|
+
console.log(`
|
|
1663
|
+
${pc6.bold("Changed files:")}`);
|
|
1664
|
+
for (const f of changedFiles) {
|
|
1665
|
+
console.log(` ${pc6.dim("•")} ${f}`);
|
|
1666
|
+
}
|
|
1667
|
+
const spinner = createSpinner(`Asking AI to group ${changedFiles.length} file(s) into logical commits...`);
|
|
1668
|
+
const diffs = await getFullDiffForFiles(changedFiles);
|
|
1669
|
+
if (!diffs.trim()) {
|
|
1670
|
+
spinner.stop();
|
|
1671
|
+
warn("Could not retrieve diff context for any files. AI needs diffs to produce groups.");
|
|
1672
|
+
}
|
|
1673
|
+
let groups;
|
|
1674
|
+
try {
|
|
1675
|
+
groups = await generateCommitGroups(changedFiles, diffs, model, config.commitConvention);
|
|
1676
|
+
spinner.success(`AI generated ${groups.length} commit group(s).`);
|
|
1677
|
+
} catch (err) {
|
|
1678
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
1679
|
+
spinner.fail(`AI grouping failed: ${reason}`);
|
|
1680
|
+
process.exit(1);
|
|
1681
|
+
}
|
|
1682
|
+
if (groups.length === 0) {
|
|
1683
|
+
error("AI could not produce commit groups. Try committing files manually.");
|
|
1684
|
+
process.exit(1);
|
|
1685
|
+
}
|
|
1686
|
+
const changedSet = new Set(changedFiles);
|
|
1687
|
+
for (const group of groups) {
|
|
1688
|
+
const invalid = group.files.filter((f) => !changedSet.has(f));
|
|
1689
|
+
if (invalid.length > 0) {
|
|
1690
|
+
warn(`AI suggested unknown file(s): ${invalid.join(", ")} — removed from group.`);
|
|
1691
|
+
}
|
|
1692
|
+
group.files = group.files.filter((f) => changedSet.has(f));
|
|
1693
|
+
}
|
|
1694
|
+
let validGroups = groups.filter((g) => g.files.length > 0);
|
|
1695
|
+
if (validGroups.length === 0) {
|
|
1696
|
+
error("No valid groups remain after validation. Try committing files manually.");
|
|
1697
|
+
process.exit(1);
|
|
1698
|
+
}
|
|
1699
|
+
let proceedToCommit = false;
|
|
1700
|
+
let commitAll = false;
|
|
1701
|
+
while (!proceedToCommit) {
|
|
1702
|
+
console.log(`
|
|
1703
|
+
${pc6.bold(`AI suggested ${validGroups.length} commit group(s):`)}
|
|
1704
|
+
`);
|
|
1705
|
+
for (let i = 0;i < validGroups.length; i++) {
|
|
1706
|
+
const g = validGroups[i];
|
|
1707
|
+
console.log(` ${pc6.cyan(`Group ${i + 1}:`)} ${pc6.bold(g.message)}`);
|
|
1708
|
+
for (const f of g.files) {
|
|
1709
|
+
console.log(` ${pc6.dim("•")} ${f}`);
|
|
1710
|
+
}
|
|
1711
|
+
console.log();
|
|
1712
|
+
}
|
|
1713
|
+
const summaryAction = await selectPrompt("What would you like to do?", [
|
|
1714
|
+
"Commit all",
|
|
1715
|
+
"Review each group",
|
|
1716
|
+
"Regenerate all messages",
|
|
1717
|
+
"Cancel"
|
|
1718
|
+
]);
|
|
1719
|
+
if (summaryAction === "Cancel") {
|
|
1720
|
+
warn("Group commit cancelled.");
|
|
1721
|
+
process.exit(0);
|
|
1722
|
+
}
|
|
1723
|
+
if (summaryAction === "Regenerate all messages") {
|
|
1724
|
+
const regenSpinner = createSpinner("Regenerating all commit messages...");
|
|
1725
|
+
try {
|
|
1726
|
+
validGroups = await regenerateAllGroupMessages(validGroups, diffs, model, config.commitConvention);
|
|
1727
|
+
regenSpinner.success("All commit messages regenerated.");
|
|
1728
|
+
} catch {
|
|
1729
|
+
regenSpinner.fail("Failed to regenerate messages. Keeping current ones.");
|
|
1730
|
+
}
|
|
1731
|
+
continue;
|
|
1732
|
+
}
|
|
1733
|
+
proceedToCommit = true;
|
|
1734
|
+
commitAll = summaryAction === "Commit all";
|
|
1735
|
+
}
|
|
1736
|
+
let committed = 0;
|
|
1737
|
+
if (commitAll) {
|
|
1738
|
+
for (let i = 0;i < validGroups.length; i++) {
|
|
1739
|
+
const group = validGroups[i];
|
|
1740
|
+
const stageResult = await stageFiles(group.files);
|
|
1741
|
+
if (stageResult.exitCode !== 0) {
|
|
1742
|
+
error(`Failed to stage group ${i + 1}: ${stageResult.stderr}`);
|
|
1743
|
+
continue;
|
|
1744
|
+
}
|
|
1745
|
+
const commitResult = await commitWithMessage(group.message);
|
|
1746
|
+
if (commitResult.exitCode !== 0) {
|
|
1747
|
+
const detail = (commitResult.stderr || commitResult.stdout).trim();
|
|
1748
|
+
error(`Failed to commit group ${i + 1}: ${detail}`);
|
|
1749
|
+
await unstageFiles(group.files);
|
|
1750
|
+
continue;
|
|
1751
|
+
}
|
|
1752
|
+
committed++;
|
|
1753
|
+
success(`✅ Committed group ${i + 1}: ${pc6.bold(group.message)}`);
|
|
1754
|
+
}
|
|
1755
|
+
} else {
|
|
1756
|
+
for (let i = 0;i < validGroups.length; i++) {
|
|
1757
|
+
const group = validGroups[i];
|
|
1758
|
+
console.log(pc6.bold(`
|
|
1759
|
+
── Group ${i + 1}/${validGroups.length} ──`));
|
|
1760
|
+
console.log(` ${pc6.cyan(group.message)}`);
|
|
1761
|
+
for (const f of group.files) {
|
|
1762
|
+
console.log(` ${pc6.dim("•")} ${f}`);
|
|
1763
|
+
}
|
|
1764
|
+
let message = group.message;
|
|
1765
|
+
let actionDone = false;
|
|
1766
|
+
while (!actionDone) {
|
|
1767
|
+
const action = await selectPrompt("Action for this group:", [
|
|
1768
|
+
"Commit as-is",
|
|
1769
|
+
"Edit message and commit",
|
|
1770
|
+
"Regenerate message",
|
|
1771
|
+
"Skip this group"
|
|
1772
|
+
]);
|
|
1773
|
+
if (action === "Skip this group") {
|
|
1774
|
+
warn(`Skipped group ${i + 1}.`);
|
|
1775
|
+
actionDone = true;
|
|
1776
|
+
continue;
|
|
1777
|
+
}
|
|
1778
|
+
if (action === "Regenerate message") {
|
|
1779
|
+
const regenSpinner = createSpinner("Regenerating commit message for this group...");
|
|
1780
|
+
const newMsg = await regenerateGroupMessage(group.files, diffs, model, config.commitConvention);
|
|
1781
|
+
if (newMsg) {
|
|
1782
|
+
message = newMsg;
|
|
1783
|
+
group.message = newMsg;
|
|
1784
|
+
regenSpinner.success(`New message: ${pc6.bold(message)}`);
|
|
1785
|
+
} else {
|
|
1786
|
+
regenSpinner.fail("AI could not generate a new message. Keeping current one.");
|
|
1787
|
+
}
|
|
1788
|
+
continue;
|
|
1789
|
+
}
|
|
1790
|
+
if (action === "Edit message and commit") {
|
|
1791
|
+
message = await inputPrompt("Edit commit message", message);
|
|
1792
|
+
if (!message) {
|
|
1793
|
+
warn(`Skipped group ${i + 1} (empty message).`);
|
|
1794
|
+
actionDone = true;
|
|
1795
|
+
continue;
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1798
|
+
if (!validateCommitMessage(message, config.commitConvention)) {
|
|
1799
|
+
for (const line of getValidationError(config.commitConvention)) {
|
|
1800
|
+
warn(line);
|
|
1801
|
+
}
|
|
1802
|
+
const proceed = await confirmPrompt("Commit anyway?");
|
|
1803
|
+
if (!proceed) {
|
|
1804
|
+
warn(`Skipped group ${i + 1}.`);
|
|
1805
|
+
actionDone = true;
|
|
1806
|
+
continue;
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1809
|
+
const stageResult = await stageFiles(group.files);
|
|
1810
|
+
if (stageResult.exitCode !== 0) {
|
|
1811
|
+
error(`Failed to stage group ${i + 1}: ${stageResult.stderr}`);
|
|
1812
|
+
actionDone = true;
|
|
1813
|
+
continue;
|
|
1814
|
+
}
|
|
1815
|
+
const commitResult = await commitWithMessage(message);
|
|
1816
|
+
if (commitResult.exitCode !== 0) {
|
|
1817
|
+
const detail = (commitResult.stderr || commitResult.stdout).trim();
|
|
1818
|
+
error(`Failed to commit group ${i + 1}: ${detail}`);
|
|
1819
|
+
await unstageFiles(group.files);
|
|
1820
|
+
actionDone = true;
|
|
1821
|
+
continue;
|
|
1822
|
+
}
|
|
1823
|
+
committed++;
|
|
1824
|
+
success(`✅ Committed group ${i + 1}: ${pc6.bold(message)}`);
|
|
1825
|
+
actionDone = true;
|
|
1826
|
+
}
|
|
1827
|
+
}
|
|
1828
|
+
}
|
|
1829
|
+
if (committed === 0) {
|
|
1830
|
+
warn("No groups were committed.");
|
|
1831
|
+
} else {
|
|
1832
|
+
success(`
|
|
1833
|
+
\uD83C\uDF89 ${committed} of ${validGroups.length} group(s) committed successfully.`);
|
|
1834
|
+
}
|
|
1835
|
+
process.exit(0);
|
|
1836
|
+
}
|
|
1837
|
+
|
|
1838
|
+
// src/commands/doctor.ts
|
|
1839
|
+
import { execFile as execFileCb3 } from "node:child_process";
|
|
1840
|
+
import { defineCommand as defineCommand4 } from "citty";
|
|
1841
|
+
import pc7 from "picocolors";
|
|
1842
|
+
// package.json
|
|
1843
|
+
var package_default = {
|
|
1844
|
+
name: "contribute-now",
|
|
1845
|
+
version: "0.2.0-dev.09dfc24",
|
|
1846
|
+
description: "Git workflow CLI for squash-merge two-branch models. Keeps dev in sync with main after squash merges.",
|
|
1847
|
+
type: "module",
|
|
1848
|
+
bin: {
|
|
1849
|
+
contrib: "dist/index.js",
|
|
1850
|
+
contribute: "dist/index.js"
|
|
1851
|
+
},
|
|
1852
|
+
files: [
|
|
1853
|
+
"dist"
|
|
1854
|
+
],
|
|
1855
|
+
scripts: {
|
|
1856
|
+
build: "bun build src/index.ts --outfile dist/index.js --target node --packages external",
|
|
1857
|
+
cli: "bun run src/index.ts --",
|
|
1858
|
+
dev: "bun src/index.ts",
|
|
1859
|
+
test: "bun test",
|
|
1860
|
+
lint: "biome check .",
|
|
1861
|
+
"lint:fix": "biome check --write .",
|
|
1862
|
+
format: "biome format --write .",
|
|
1863
|
+
"www:dev": "bun run --cwd www dev",
|
|
1864
|
+
"www:build": "bun run --cwd www build",
|
|
1865
|
+
"www:preview": "bun run --cwd www preview"
|
|
1866
|
+
},
|
|
1867
|
+
engines: {
|
|
1868
|
+
node: ">=18",
|
|
1869
|
+
bun: ">=1.0"
|
|
1870
|
+
},
|
|
1871
|
+
keywords: [
|
|
1872
|
+
"git",
|
|
1873
|
+
"workflow",
|
|
1874
|
+
"squash-merge",
|
|
1875
|
+
"sync",
|
|
1876
|
+
"cli",
|
|
1877
|
+
"contribute",
|
|
1878
|
+
"fork",
|
|
1879
|
+
"dev-branch",
|
|
1880
|
+
"clean-commit"
|
|
1881
|
+
],
|
|
1882
|
+
author: "Waren Gonzaga",
|
|
1883
|
+
license: "GPL-3.0",
|
|
1884
|
+
repository: {
|
|
1885
|
+
type: "git",
|
|
1886
|
+
url: "git+https://github.com/warengonzaga/contribute-now.git"
|
|
1887
|
+
},
|
|
1888
|
+
dependencies: {
|
|
1889
|
+
"@clack/prompts": "^1.0.1",
|
|
1890
|
+
"@github/copilot-sdk": "^0.1.25",
|
|
1891
|
+
"@wgtechlabs/log-engine": "^2.3.1",
|
|
1892
|
+
citty: "^0.1.6",
|
|
1893
|
+
figlet: "^1.10.0",
|
|
1894
|
+
picocolors: "^1.1.1"
|
|
1895
|
+
},
|
|
1896
|
+
devDependencies: {
|
|
1897
|
+
"@biomejs/biome": "^2.4.4",
|
|
1898
|
+
"@types/bun": "latest",
|
|
1899
|
+
"@types/figlet": "^1.7.0",
|
|
1900
|
+
typescript: "^5.7.0"
|
|
1901
|
+
}
|
|
1902
|
+
};
|
|
1903
|
+
|
|
1904
|
+
// src/utils/remote.ts
|
|
1905
|
+
function parseRepoFromUrl(url) {
|
|
1906
|
+
const httpsMatch = url.match(/https?:\/\/github\.com\/([^/]+)\/([^/.]+?)(?:\.git)?$/);
|
|
1907
|
+
if (httpsMatch) {
|
|
1908
|
+
return { owner: httpsMatch[1], repo: httpsMatch[2] };
|
|
1909
|
+
}
|
|
1910
|
+
const sshMatch = url.match(/git@github\.com:([^/]+)\/([^/.]+?)(?:\.git)?$/);
|
|
1911
|
+
if (sshMatch) {
|
|
1912
|
+
return { owner: sshMatch[1], repo: sshMatch[2] };
|
|
1913
|
+
}
|
|
1914
|
+
return null;
|
|
1915
|
+
}
|
|
1916
|
+
async function detectForkSetup() {
|
|
1917
|
+
const remotes = await getRemotes();
|
|
1918
|
+
const hasOrigin = remotes.includes("origin");
|
|
1919
|
+
const hasUpstream = remotes.includes("upstream");
|
|
1920
|
+
return {
|
|
1921
|
+
isFork: hasUpstream,
|
|
1922
|
+
originRemote: hasOrigin ? "origin" : null,
|
|
1923
|
+
upstreamRemote: hasUpstream ? "upstream" : null
|
|
1924
|
+
};
|
|
1925
|
+
}
|
|
1926
|
+
async function getRepoInfoFromRemote(remote = "origin") {
|
|
1927
|
+
const url = await getRemoteUrl(remote);
|
|
1928
|
+
if (!url)
|
|
1929
|
+
return null;
|
|
1930
|
+
return parseRepoFromUrl(url);
|
|
1931
|
+
}
|
|
1932
|
+
|
|
1933
|
+
// src/commands/doctor.ts
|
|
1934
|
+
var PASS = ` ${pc7.green("✔")} `;
|
|
1935
|
+
var FAIL = ` ${pc7.red("✗")} `;
|
|
1936
|
+
var WARN = ` ${pc7.yellow("⚠")} `;
|
|
1937
|
+
function printReport(report) {
|
|
1938
|
+
for (const section of report.sections) {
|
|
1939
|
+
console.log(`
|
|
1940
|
+
${pc7.bold(pc7.underline(section.title))}`);
|
|
1941
|
+
for (const check of section.checks) {
|
|
1942
|
+
const prefix = check.ok ? check.warning ? WARN : PASS : FAIL;
|
|
1943
|
+
const text2 = check.detail ? `${check.label} ${pc7.dim(`— ${check.detail}`)}` : check.label;
|
|
1944
|
+
console.log(`${prefix}${text2}`);
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
console.log();
|
|
1948
|
+
}
|
|
1949
|
+
function toJson(report) {
|
|
1950
|
+
return JSON.stringify(report.sections.map((s) => ({
|
|
1951
|
+
section: s.title,
|
|
1952
|
+
checks: s.checks.map((c) => ({
|
|
1953
|
+
label: c.label,
|
|
1954
|
+
ok: c.ok,
|
|
1955
|
+
warning: c.warning ?? false,
|
|
1956
|
+
detail: c.detail ?? null
|
|
1957
|
+
}))
|
|
1958
|
+
})), null, 2);
|
|
1959
|
+
}
|
|
1960
|
+
function runCmd(cmd, args) {
|
|
1961
|
+
return new Promise((resolve) => {
|
|
1962
|
+
execFileCb3(cmd, args, (error2, stdout) => {
|
|
1963
|
+
resolve({
|
|
1964
|
+
ok: !error2,
|
|
1965
|
+
stdout: (stdout ?? "").trim()
|
|
1966
|
+
});
|
|
1967
|
+
});
|
|
1968
|
+
});
|
|
1969
|
+
}
|
|
1970
|
+
async function toolSection() {
|
|
1971
|
+
const checks = [];
|
|
1972
|
+
checks.push({
|
|
1973
|
+
label: `contrib v${package_default.version ?? "unknown"}`,
|
|
1974
|
+
ok: true
|
|
1975
|
+
});
|
|
1976
|
+
const runtime = typeof globalThis.Bun !== "undefined" ? `Bun ${globalThis.Bun.version ?? "?"}` : `Node ${process.version}`;
|
|
1977
|
+
checks.push({ label: runtime, ok: true, detail: `${process.platform}-${process.arch}` });
|
|
1978
|
+
return { title: "Tool", checks };
|
|
1979
|
+
}
|
|
1980
|
+
async function depsSection() {
|
|
1981
|
+
const checks = [];
|
|
1982
|
+
const git = await runCmd("git", ["--version"]);
|
|
1983
|
+
checks.push({
|
|
1984
|
+
label: git.ok ? git.stdout.replace("git version ", "git ") : "git not found",
|
|
1985
|
+
ok: git.ok
|
|
1986
|
+
});
|
|
1987
|
+
const ghInstalled = await checkGhInstalled();
|
|
1988
|
+
if (ghInstalled) {
|
|
1989
|
+
const ghVer = await runCmd("gh", ["--version"]);
|
|
1990
|
+
const ver = ghVer.stdout.split(`
|
|
1991
|
+
`)[0] ?? "gh";
|
|
1992
|
+
checks.push({ label: ver, ok: true });
|
|
1993
|
+
const ghAuth = await checkGhAuth();
|
|
1994
|
+
checks.push({
|
|
1995
|
+
label: ghAuth ? "gh authenticated" : "gh not authenticated",
|
|
1996
|
+
ok: ghAuth,
|
|
1997
|
+
warning: !ghAuth,
|
|
1998
|
+
detail: ghAuth ? undefined : "run `gh auth login`"
|
|
1999
|
+
});
|
|
2000
|
+
} else {
|
|
2001
|
+
checks.push({
|
|
2002
|
+
label: "gh CLI not installed",
|
|
2003
|
+
ok: false,
|
|
2004
|
+
detail: "install from https://cli.github.com"
|
|
2005
|
+
});
|
|
2006
|
+
}
|
|
2007
|
+
try {
|
|
2008
|
+
await import("@github/copilot-sdk");
|
|
2009
|
+
checks.push({ label: "Copilot SDK importable", ok: true });
|
|
2010
|
+
} catch {
|
|
2011
|
+
checks.push({
|
|
2012
|
+
label: "Copilot SDK not loadable",
|
|
2013
|
+
ok: false,
|
|
2014
|
+
warning: true,
|
|
2015
|
+
detail: "AI features will be unavailable"
|
|
2016
|
+
});
|
|
2017
|
+
}
|
|
2018
|
+
return { title: "Dependencies", checks };
|
|
2019
|
+
}
|
|
2020
|
+
async function configSection() {
|
|
2021
|
+
const checks = [];
|
|
2022
|
+
const exists = configExists();
|
|
2023
|
+
if (!exists) {
|
|
2024
|
+
checks.push({
|
|
2025
|
+
label: ".contributerc.json not found",
|
|
2026
|
+
ok: false,
|
|
2027
|
+
detail: "run `contrib setup` to create it"
|
|
2028
|
+
});
|
|
2029
|
+
return { title: "Config", checks };
|
|
2030
|
+
}
|
|
2031
|
+
const config = readConfig();
|
|
2032
|
+
if (!config) {
|
|
2033
|
+
checks.push({ label: ".contributerc.json found but invalid", ok: false });
|
|
2034
|
+
return { title: "Config", checks };
|
|
2035
|
+
}
|
|
2036
|
+
checks.push({ label: ".contributerc.json found and valid", ok: true });
|
|
2037
|
+
const desc = WORKFLOW_DESCRIPTIONS[config.workflow] ?? config.workflow;
|
|
2038
|
+
checks.push({
|
|
2039
|
+
label: `Workflow: ${config.workflow}`,
|
|
2040
|
+
ok: true,
|
|
2041
|
+
detail: desc
|
|
2042
|
+
});
|
|
2043
|
+
checks.push({ label: `Role: ${config.role}`, ok: true });
|
|
2044
|
+
checks.push({ label: `Commit convention: ${config.commitConvention}`, ok: true });
|
|
2045
|
+
if (hasDevBranch(config.workflow)) {
|
|
2046
|
+
checks.push({
|
|
2047
|
+
label: `Dev branch: ${config.devBranch ?? "(not set)"}`,
|
|
2048
|
+
ok: !!config.devBranch
|
|
2049
|
+
});
|
|
2050
|
+
}
|
|
2051
|
+
const ignored = isGitignored();
|
|
2052
|
+
checks.push({
|
|
2053
|
+
label: ignored ? ".contributerc.json in .gitignore" : ".contributerc.json NOT in .gitignore",
|
|
2054
|
+
ok: true,
|
|
2055
|
+
warning: !ignored,
|
|
2056
|
+
detail: ignored ? undefined : "consider adding it to .gitignore"
|
|
2057
|
+
});
|
|
2058
|
+
return { title: "Config", checks };
|
|
2059
|
+
}
|
|
2060
|
+
async function gitSection() {
|
|
2061
|
+
const checks = [];
|
|
2062
|
+
const inRepo = await isGitRepo();
|
|
2063
|
+
checks.push({
|
|
2064
|
+
label: inRepo ? "Inside a git repository" : "Not inside a git repository",
|
|
2065
|
+
ok: inRepo
|
|
2066
|
+
});
|
|
2067
|
+
if (!inRepo)
|
|
2068
|
+
return { title: "Git Environment", checks };
|
|
2069
|
+
const branch = await getCurrentBranch();
|
|
2070
|
+
const head = await runCmd("git", ["rev-parse", "--short", "HEAD"]);
|
|
2071
|
+
checks.push({
|
|
2072
|
+
label: `Branch: ${branch ?? "(detached)"}`,
|
|
2073
|
+
ok: !!branch,
|
|
2074
|
+
detail: head.ok ? `HEAD ${head.stdout}` : undefined
|
|
2075
|
+
});
|
|
2076
|
+
const remotes = await getRemotes();
|
|
2077
|
+
if (remotes.length === 0) {
|
|
2078
|
+
checks.push({ label: "No remotes configured", ok: false, warning: true });
|
|
2079
|
+
} else {
|
|
2080
|
+
for (const remote of remotes) {
|
|
2081
|
+
const url = await getRemoteUrl(remote);
|
|
2082
|
+
const repoInfo = url ? parseRepoFromUrl(url) : null;
|
|
2083
|
+
const detail = repoInfo ? `${repoInfo.owner}/${repoInfo.repo}` : url ?? "unknown URL";
|
|
2084
|
+
checks.push({ label: `Remote: ${remote}`, ok: true, detail });
|
|
2085
|
+
}
|
|
2086
|
+
}
|
|
2087
|
+
const dirty = await hasUncommittedChanges();
|
|
2088
|
+
checks.push({
|
|
2089
|
+
label: dirty ? "Uncommitted changes detected" : "Working tree clean",
|
|
2090
|
+
ok: true,
|
|
2091
|
+
warning: dirty
|
|
2092
|
+
});
|
|
2093
|
+
return { title: "Git Environment", checks };
|
|
2094
|
+
}
|
|
2095
|
+
async function forkSection() {
|
|
2096
|
+
const checks = [];
|
|
2097
|
+
const fork = await detectForkSetup();
|
|
2098
|
+
checks.push({
|
|
2099
|
+
label: fork.isFork ? "Fork detected (upstream remote exists)" : "Not a fork (no upstream remote)",
|
|
2100
|
+
ok: true
|
|
2101
|
+
});
|
|
2102
|
+
if (fork.originRemote) {
|
|
2103
|
+
checks.push({ label: `Origin remote: ${fork.originRemote}`, ok: true });
|
|
2104
|
+
}
|
|
2105
|
+
if (fork.upstreamRemote) {
|
|
2106
|
+
checks.push({ label: `Upstream remote: ${fork.upstreamRemote}`, ok: true });
|
|
2107
|
+
}
|
|
2108
|
+
return { title: "Fork Detection", checks };
|
|
2109
|
+
}
|
|
2110
|
+
async function workflowSection() {
|
|
2111
|
+
const checks = [];
|
|
2112
|
+
const config = readConfig();
|
|
2113
|
+
if (!config) {
|
|
2114
|
+
checks.push({
|
|
2115
|
+
label: "Cannot resolve workflow (no config)",
|
|
2116
|
+
ok: false,
|
|
2117
|
+
detail: "run `contrib setup` first"
|
|
2118
|
+
});
|
|
2119
|
+
return { title: "Workflow Resolution", checks };
|
|
2120
|
+
}
|
|
2121
|
+
const baseBranch = getBaseBranch(config);
|
|
2122
|
+
checks.push({ label: `Base branch: ${baseBranch}`, ok: true });
|
|
2123
|
+
const sync = getSyncSource(config);
|
|
2124
|
+
checks.push({
|
|
2125
|
+
label: `Sync source: ${sync.ref}`,
|
|
2126
|
+
ok: true,
|
|
2127
|
+
detail: `strategy: ${sync.strategy}`
|
|
2128
|
+
});
|
|
2129
|
+
checks.push({
|
|
2130
|
+
label: `Branch prefixes: ${config.branchPrefixes.join(", ")}`,
|
|
2131
|
+
ok: config.branchPrefixes.length > 0
|
|
2132
|
+
});
|
|
2133
|
+
return { title: "Workflow Resolution", checks };
|
|
2134
|
+
}
|
|
2135
|
+
function envSection() {
|
|
2136
|
+
const checks = [];
|
|
2137
|
+
const vars = ["GITHUB_TOKEN", "GH_TOKEN", "COPILOT_AGENT_TOKEN", "NO_COLOR", "FORCE_COLOR", "CI"];
|
|
2138
|
+
for (const name of vars) {
|
|
2139
|
+
const val = process.env[name];
|
|
2140
|
+
if (val !== undefined) {
|
|
2141
|
+
const isSecret = name.toLowerCase().includes("token");
|
|
2142
|
+
const display = isSecret ? `${val.slice(0, 4)}${"*".repeat(Math.min(val.length - 4, 12))}` : val;
|
|
2143
|
+
checks.push({ label: `${name} = ${display}`, ok: true });
|
|
2144
|
+
}
|
|
2145
|
+
}
|
|
2146
|
+
if (checks.length === 0) {
|
|
2147
|
+
checks.push({ label: "No relevant environment variables set", ok: true });
|
|
2148
|
+
}
|
|
2149
|
+
return { title: "Environment", checks };
|
|
2150
|
+
}
|
|
2151
|
+
var doctor_default = defineCommand4({
|
|
2152
|
+
meta: {
|
|
2153
|
+
name: "doctor",
|
|
2154
|
+
description: "Diagnose the contribute-now CLI environment and configuration"
|
|
2155
|
+
},
|
|
2156
|
+
args: {
|
|
2157
|
+
json: {
|
|
2158
|
+
type: "boolean",
|
|
2159
|
+
description: "Output report as JSON",
|
|
2160
|
+
default: false
|
|
688
2161
|
}
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
}
|
|
2162
|
+
},
|
|
2163
|
+
async run({ args }) {
|
|
2164
|
+
const isJson = args.json;
|
|
2165
|
+
const [tool, deps, config, git, fork, workflow] = await Promise.all([
|
|
2166
|
+
toolSection(),
|
|
2167
|
+
depsSection(),
|
|
2168
|
+
configSection(),
|
|
2169
|
+
gitSection(),
|
|
2170
|
+
forkSection(),
|
|
2171
|
+
workflowSection()
|
|
2172
|
+
]);
|
|
2173
|
+
const env = envSection();
|
|
2174
|
+
const report = {
|
|
2175
|
+
sections: [tool, deps, config, git, fork, workflow, env]
|
|
2176
|
+
};
|
|
2177
|
+
if (isJson) {
|
|
2178
|
+
console.log(toJson(report));
|
|
2179
|
+
return;
|
|
2180
|
+
}
|
|
2181
|
+
heading("\uD83E\uDE7A contribute-now doctor");
|
|
2182
|
+
printReport(report);
|
|
2183
|
+
const total = report.sections.flatMap((s) => s.checks);
|
|
2184
|
+
const failures = total.filter((c) => !c.ok);
|
|
2185
|
+
const warnings = total.filter((c) => c.ok && c.warning);
|
|
2186
|
+
if (failures.length === 0 && warnings.length === 0) {
|
|
2187
|
+
console.log(` ${pc7.green("All checks passed!")} No issues detected.
|
|
2188
|
+
`);
|
|
717
2189
|
} else {
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
console.log();
|
|
721
|
-
for (const hint of CONVENTION_FORMAT_HINTS[convention2]) {
|
|
722
|
-
console.log(pc4.dim(hint));
|
|
723
|
-
}
|
|
724
|
-
console.log();
|
|
2190
|
+
if (failures.length > 0) {
|
|
2191
|
+
console.log(` ${pc7.red(`${failures.length} issue${failures.length !== 1 ? "s" : ""} found.`)}`);
|
|
725
2192
|
}
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
if (!finalMessage) {
|
|
729
|
-
error("No commit message provided.");
|
|
730
|
-
process.exit(1);
|
|
731
|
-
}
|
|
732
|
-
const convention = config.commitConvention;
|
|
733
|
-
if (!validateCommitMessage(finalMessage, convention)) {
|
|
734
|
-
for (const line of getValidationError(convention)) {
|
|
735
|
-
warn(line);
|
|
2193
|
+
if (warnings.length > 0) {
|
|
2194
|
+
console.log(` ${pc7.yellow(`${warnings.length} warning${warnings.length !== 1 ? "s" : ""}.`)}`);
|
|
736
2195
|
}
|
|
737
|
-
|
|
738
|
-
if (!proceed)
|
|
739
|
-
process.exit(1);
|
|
740
|
-
}
|
|
741
|
-
const result = await commitWithMessage(finalMessage);
|
|
742
|
-
if (result.exitCode !== 0) {
|
|
743
|
-
error(`Failed to commit: ${result.stderr}`);
|
|
744
|
-
process.exit(1);
|
|
2196
|
+
console.log();
|
|
745
2197
|
}
|
|
746
|
-
success(`✅ Committed: ${pc4.bold(finalMessage)}`);
|
|
747
2198
|
}
|
|
748
2199
|
});
|
|
749
2200
|
|
|
750
2201
|
// src/commands/hook.ts
|
|
751
|
-
import { existsSync as existsSync2, mkdirSync, readFileSync as
|
|
752
|
-
import { join as
|
|
753
|
-
import { defineCommand as
|
|
754
|
-
import
|
|
2202
|
+
import { existsSync as existsSync2, mkdirSync, readFileSync as readFileSync3, rmSync, writeFileSync as writeFileSync2 } from "node:fs";
|
|
2203
|
+
import { join as join3 } from "node:path";
|
|
2204
|
+
import { defineCommand as defineCommand5 } from "citty";
|
|
2205
|
+
import pc8 from "picocolors";
|
|
755
2206
|
var HOOK_MARKER = "# managed by contribute-now";
|
|
756
2207
|
function getHooksDir(cwd = process.cwd()) {
|
|
757
|
-
return
|
|
2208
|
+
return join3(cwd, ".git", "hooks");
|
|
758
2209
|
}
|
|
759
2210
|
function getHookPath(cwd = process.cwd()) {
|
|
760
|
-
return
|
|
2211
|
+
return join3(getHooksDir(cwd), "commit-msg");
|
|
761
2212
|
}
|
|
762
2213
|
function generateHookScript() {
|
|
763
2214
|
return `#!/bin/sh
|
|
@@ -774,11 +2225,22 @@ case "$commit_msg" in
|
|
|
774
2225
|
Merge\\ *|fixup!*|squash!*|amend!*) exit 0 ;;
|
|
775
2226
|
esac
|
|
776
2227
|
|
|
777
|
-
#
|
|
778
|
-
|
|
2228
|
+
# Detect available package runner
|
|
2229
|
+
if command -v contrib >/dev/null 2>&1; then
|
|
2230
|
+
contrib validate "$commit_msg"
|
|
2231
|
+
elif command -v bunx >/dev/null 2>&1; then
|
|
2232
|
+
bunx contrib validate "$commit_msg"
|
|
2233
|
+
elif command -v pnpx >/dev/null 2>&1; then
|
|
2234
|
+
pnpx contrib validate "$commit_msg"
|
|
2235
|
+
elif command -v npx >/dev/null 2>&1; then
|
|
2236
|
+
npx contrib validate "$commit_msg"
|
|
2237
|
+
else
|
|
2238
|
+
echo "Warning: No package runner found. Skipping commit message validation."
|
|
2239
|
+
exit 0
|
|
2240
|
+
fi
|
|
779
2241
|
`;
|
|
780
2242
|
}
|
|
781
|
-
var hook_default =
|
|
2243
|
+
var hook_default = defineCommand5({
|
|
782
2244
|
meta: {
|
|
783
2245
|
name: "hook",
|
|
784
2246
|
description: "Install or uninstall the commit-msg git hook"
|
|
@@ -822,7 +2284,7 @@ async function installHook() {
|
|
|
822
2284
|
const hookPath = getHookPath();
|
|
823
2285
|
const hooksDir = getHooksDir();
|
|
824
2286
|
if (existsSync2(hookPath)) {
|
|
825
|
-
const existing =
|
|
2287
|
+
const existing = readFileSync3(hookPath, "utf-8");
|
|
826
2288
|
if (!existing.includes(HOOK_MARKER)) {
|
|
827
2289
|
error("A commit-msg hook already exists and was not installed by contribute-now.");
|
|
828
2290
|
warn(`Path: ${hookPath}`);
|
|
@@ -836,8 +2298,8 @@ async function installHook() {
|
|
|
836
2298
|
}
|
|
837
2299
|
writeFileSync2(hookPath, generateHookScript(), { mode: 493 });
|
|
838
2300
|
success(`commit-msg hook installed.`);
|
|
839
|
-
info(`Convention: ${
|
|
840
|
-
info(`Path: ${
|
|
2301
|
+
info(`Convention: ${pc8.bold(CONVENTION_LABELS[config.commitConvention])}`);
|
|
2302
|
+
info(`Path: ${pc8.dim(hookPath)}`);
|
|
841
2303
|
}
|
|
842
2304
|
async function uninstallHook() {
|
|
843
2305
|
heading("\uD83E\uDE9D hook uninstall");
|
|
@@ -846,7 +2308,7 @@ async function uninstallHook() {
|
|
|
846
2308
|
info("No commit-msg hook found. Nothing to uninstall.");
|
|
847
2309
|
return;
|
|
848
2310
|
}
|
|
849
|
-
const content =
|
|
2311
|
+
const content = readFileSync3(hookPath, "utf-8");
|
|
850
2312
|
if (!content.includes(HOOK_MARKER)) {
|
|
851
2313
|
error("The commit-msg hook was not installed by contribute-now. Leaving it untouched.");
|
|
852
2314
|
process.exit(1);
|
|
@@ -855,122 +2317,165 @@ async function uninstallHook() {
|
|
|
855
2317
|
success("commit-msg hook removed.");
|
|
856
2318
|
}
|
|
857
2319
|
|
|
858
|
-
// src/commands/
|
|
859
|
-
import { defineCommand as
|
|
860
|
-
import
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
2320
|
+
// src/commands/log.ts
|
|
2321
|
+
import { defineCommand as defineCommand6 } from "citty";
|
|
2322
|
+
import pc9 from "picocolors";
|
|
2323
|
+
var log_default = defineCommand6({
|
|
2324
|
+
meta: {
|
|
2325
|
+
name: "log",
|
|
2326
|
+
description: "Show a colorized, workflow-aware commit log with graph"
|
|
2327
|
+
},
|
|
2328
|
+
args: {
|
|
2329
|
+
count: {
|
|
2330
|
+
type: "string",
|
|
2331
|
+
alias: "n",
|
|
2332
|
+
description: "Number of commits to show (default: 20)"
|
|
2333
|
+
},
|
|
2334
|
+
all: {
|
|
2335
|
+
type: "boolean",
|
|
2336
|
+
alias: "a",
|
|
2337
|
+
description: "Show all branches, not just current",
|
|
2338
|
+
default: false
|
|
2339
|
+
},
|
|
2340
|
+
graph: {
|
|
2341
|
+
type: "boolean",
|
|
2342
|
+
alias: "g",
|
|
2343
|
+
description: "Show graph view with branch lines",
|
|
2344
|
+
default: true
|
|
2345
|
+
},
|
|
2346
|
+
branch: {
|
|
2347
|
+
type: "string",
|
|
2348
|
+
alias: "b",
|
|
2349
|
+
description: "Show log for a specific branch"
|
|
2350
|
+
}
|
|
2351
|
+
},
|
|
2352
|
+
async run({ args }) {
|
|
2353
|
+
if (!await isGitRepo()) {
|
|
2354
|
+
error("Not inside a git repository.");
|
|
2355
|
+
process.exit(1);
|
|
2356
|
+
}
|
|
2357
|
+
const config = readConfig();
|
|
2358
|
+
const count = args.count ? Number.parseInt(args.count, 10) : 20;
|
|
2359
|
+
const showAll = args.all;
|
|
2360
|
+
const showGraph = args.graph;
|
|
2361
|
+
const targetBranch = args.branch;
|
|
2362
|
+
const protectedBranches = config ? getProtectedBranches(config) : ["main", "master"];
|
|
2363
|
+
const currentBranch = await getCurrentBranch();
|
|
2364
|
+
heading("\uD83D\uDCDC commit log");
|
|
2365
|
+
if (showGraph) {
|
|
2366
|
+
const lines = await getLogGraph({ count, all: showAll, branch: targetBranch });
|
|
2367
|
+
if (lines.length === 0) {
|
|
2368
|
+
console.log(pc9.dim(" No commits found."));
|
|
2369
|
+
console.log();
|
|
2370
|
+
return;
|
|
2371
|
+
}
|
|
2372
|
+
console.log();
|
|
2373
|
+
for (const line of lines) {
|
|
2374
|
+
console.log(` ${colorizeGraphLine(line, protectedBranches, currentBranch)}`);
|
|
2375
|
+
}
|
|
2376
|
+
} else {
|
|
2377
|
+
const entries = await getLogEntries({ count, all: showAll, branch: targetBranch });
|
|
2378
|
+
if (entries.length === 0) {
|
|
2379
|
+
console.log(pc9.dim(" No commits found."));
|
|
2380
|
+
console.log();
|
|
2381
|
+
return;
|
|
2382
|
+
}
|
|
2383
|
+
console.log();
|
|
2384
|
+
for (const entry of entries) {
|
|
2385
|
+
const hashStr = pc9.yellow(entry.hash);
|
|
2386
|
+
const refsStr = entry.refs ? ` ${colorizeRefs(entry.refs, protectedBranches, currentBranch)}` : "";
|
|
2387
|
+
const subjectStr = colorizeSubject(entry.subject);
|
|
2388
|
+
console.log(` ${hashStr}${refsStr} ${subjectStr}`);
|
|
2389
|
+
}
|
|
2390
|
+
}
|
|
2391
|
+
console.log();
|
|
2392
|
+
console.log(pc9.dim(` Showing ${count} most recent commits${showAll ? " (all branches)" : targetBranch ? ` (${targetBranch})` : ""}`));
|
|
2393
|
+
console.log(pc9.dim(` Use ${pc9.bold("contrib log -n 50")} for more, or ${pc9.bold("contrib log --all")} for all branches`));
|
|
2394
|
+
console.log();
|
|
881
2395
|
}
|
|
882
|
-
}
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
return
|
|
887
|
-
} catch {
|
|
888
|
-
return false;
|
|
2396
|
+
});
|
|
2397
|
+
function colorizeGraphLine(line, protectedBranches, currentBranch) {
|
|
2398
|
+
const match = line.match(/^([|/\\*\s_.-]*)([a-f0-9]{7,12})(\s+\(([^)]+)\))?\s*(.*)/);
|
|
2399
|
+
if (!match) {
|
|
2400
|
+
return pc9.cyan(line);
|
|
889
2401
|
}
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
return null;
|
|
895
|
-
try {
|
|
896
|
-
return JSON.parse(stdout.trim());
|
|
897
|
-
} catch {
|
|
898
|
-
return null;
|
|
2402
|
+
const [, graphPart = "", hash, , refs, subject = ""] = match;
|
|
2403
|
+
const parts = [];
|
|
2404
|
+
if (graphPart) {
|
|
2405
|
+
parts.push(colorizeGraphChars(graphPart));
|
|
899
2406
|
}
|
|
2407
|
+
parts.push(pc9.yellow(hash));
|
|
2408
|
+
if (refs) {
|
|
2409
|
+
parts.push(` (${colorizeRefs(refs, protectedBranches, currentBranch)})`);
|
|
2410
|
+
}
|
|
2411
|
+
parts.push(` ${colorizeSubject(subject)}`);
|
|
2412
|
+
return parts.join("");
|
|
2413
|
+
}
|
|
2414
|
+
function colorizeGraphChars(graphPart) {
|
|
2415
|
+
return graphPart.split("").map((ch) => {
|
|
2416
|
+
switch (ch) {
|
|
2417
|
+
case "*":
|
|
2418
|
+
return pc9.green(ch);
|
|
2419
|
+
case "|":
|
|
2420
|
+
return pc9.cyan(ch);
|
|
2421
|
+
case "/":
|
|
2422
|
+
case "\\":
|
|
2423
|
+
return pc9.cyan(ch);
|
|
2424
|
+
case "-":
|
|
2425
|
+
case "_":
|
|
2426
|
+
return pc9.cyan(ch);
|
|
2427
|
+
default:
|
|
2428
|
+
return ch;
|
|
2429
|
+
}
|
|
2430
|
+
}).join("");
|
|
2431
|
+
}
|
|
2432
|
+
function colorizeRefs(refs, protectedBranches, currentBranch) {
|
|
2433
|
+
return refs.split(",").map((ref) => {
|
|
2434
|
+
const trimmed = ref.trim();
|
|
2435
|
+
if (trimmed.startsWith("HEAD ->") || trimmed === "HEAD") {
|
|
2436
|
+
const branchName = trimmed.replace("HEAD -> ", "");
|
|
2437
|
+
if (trimmed === "HEAD") {
|
|
2438
|
+
return pc9.bold(pc9.cyan("HEAD"));
|
|
2439
|
+
}
|
|
2440
|
+
return `${pc9.bold(pc9.cyan("HEAD"))} ${pc9.dim("->")} ${colorizeRefName(branchName, protectedBranches, currentBranch)}`;
|
|
2441
|
+
}
|
|
2442
|
+
if (trimmed.startsWith("tag:")) {
|
|
2443
|
+
return pc9.bold(pc9.magenta(trimmed));
|
|
2444
|
+
}
|
|
2445
|
+
return colorizeRefName(trimmed, protectedBranches, currentBranch);
|
|
2446
|
+
}).join(pc9.dim(", "));
|
|
900
2447
|
}
|
|
901
|
-
|
|
902
|
-
const
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
if (val === "true")
|
|
907
|
-
return true;
|
|
908
|
-
if (val === "false")
|
|
909
|
-
return false;
|
|
910
|
-
return null;
|
|
911
|
-
}
|
|
912
|
-
async function getCurrentRepoInfo() {
|
|
913
|
-
const { exitCode, stdout } = await run2([
|
|
914
|
-
"repo",
|
|
915
|
-
"view",
|
|
916
|
-
"--json",
|
|
917
|
-
"nameWithOwner",
|
|
918
|
-
"-q",
|
|
919
|
-
".nameWithOwner"
|
|
920
|
-
]);
|
|
921
|
-
if (exitCode !== 0)
|
|
922
|
-
return null;
|
|
923
|
-
const nameWithOwner = stdout.trim();
|
|
924
|
-
if (!nameWithOwner)
|
|
925
|
-
return null;
|
|
926
|
-
const [owner, repo] = nameWithOwner.split("/");
|
|
927
|
-
if (!owner || !repo)
|
|
928
|
-
return null;
|
|
929
|
-
return { owner, repo };
|
|
930
|
-
}
|
|
931
|
-
async function createPR(options) {
|
|
932
|
-
const args = [
|
|
933
|
-
"pr",
|
|
934
|
-
"create",
|
|
935
|
-
"--base",
|
|
936
|
-
options.base,
|
|
937
|
-
"--title",
|
|
938
|
-
options.title,
|
|
939
|
-
"--body",
|
|
940
|
-
options.body
|
|
941
|
-
];
|
|
942
|
-
if (options.draft)
|
|
943
|
-
args.push("--draft");
|
|
944
|
-
return run2(args);
|
|
945
|
-
}
|
|
946
|
-
async function createPRFill(base, draft) {
|
|
947
|
-
const args = ["pr", "create", "--base", base, "--fill"];
|
|
948
|
-
if (draft)
|
|
949
|
-
args.push("--draft");
|
|
950
|
-
return run2(args);
|
|
951
|
-
}
|
|
952
|
-
|
|
953
|
-
// src/utils/remote.ts
|
|
954
|
-
function parseRepoFromUrl(url) {
|
|
955
|
-
const httpsMatch = url.match(/https?:\/\/github\.com\/([^/]+)\/([^/.]+?)(?:\.git)?$/);
|
|
956
|
-
if (httpsMatch) {
|
|
957
|
-
return { owner: httpsMatch[1], repo: httpsMatch[2] };
|
|
2448
|
+
function colorizeRefName(name, protectedBranches, currentBranch) {
|
|
2449
|
+
const isRemote = name.includes("/");
|
|
2450
|
+
const localName = isRemote ? name.split("/").slice(1).join("/") : name;
|
|
2451
|
+
if (protectedBranches.includes(localName)) {
|
|
2452
|
+
return isRemote ? pc9.bold(pc9.red(name)) : pc9.bold(pc9.red(name));
|
|
958
2453
|
}
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
return { owner: sshMatch[1], repo: sshMatch[2] };
|
|
2454
|
+
if (localName === currentBranch) {
|
|
2455
|
+
return pc9.bold(pc9.green(name));
|
|
962
2456
|
}
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
2457
|
+
if (isRemote) {
|
|
2458
|
+
return pc9.blue(name);
|
|
2459
|
+
}
|
|
2460
|
+
return pc9.green(name);
|
|
2461
|
+
}
|
|
2462
|
+
function colorizeSubject(subject) {
|
|
2463
|
+
const emojiMatch = subject.match(/^([\p{Emoji_Presentation}\p{Emoji}\uFE0F]+\s*)/u);
|
|
2464
|
+
if (emojiMatch) {
|
|
2465
|
+
const emoji = emojiMatch[1];
|
|
2466
|
+
const rest = subject.slice(emoji.length);
|
|
2467
|
+
return `${emoji}${pc9.white(rest)}`;
|
|
2468
|
+
}
|
|
2469
|
+
if (subject.startsWith("Merge ")) {
|
|
2470
|
+
return pc9.dim(subject);
|
|
2471
|
+
}
|
|
2472
|
+
return pc9.white(subject);
|
|
970
2473
|
}
|
|
971
2474
|
|
|
972
2475
|
// src/commands/setup.ts
|
|
973
|
-
|
|
2476
|
+
import { defineCommand as defineCommand7 } from "citty";
|
|
2477
|
+
import pc10 from "picocolors";
|
|
2478
|
+
var setup_default = defineCommand7({
|
|
974
2479
|
meta: {
|
|
975
2480
|
name: "setup",
|
|
976
2481
|
description: "Initialize contribute-now config for this repo (.contributerc.json)"
|
|
@@ -991,7 +2496,7 @@ var setup_default = defineCommand4({
|
|
|
991
2496
|
workflow = "github-flow";
|
|
992
2497
|
else if (workflowChoice.startsWith("Git Flow"))
|
|
993
2498
|
workflow = "git-flow";
|
|
994
|
-
info(`Workflow: ${
|
|
2499
|
+
info(`Workflow: ${pc10.bold(WORKFLOW_DESCRIPTIONS[workflow])}`);
|
|
995
2500
|
const conventionChoice = await selectPrompt("Which commit convention should this project use?", [
|
|
996
2501
|
`${CONVENTION_DESCRIPTIONS["clean-commit"]} (recommended)`,
|
|
997
2502
|
CONVENTION_DESCRIPTIONS.conventional,
|
|
@@ -1044,8 +2549,8 @@ var setup_default = defineCommand4({
|
|
|
1044
2549
|
detectedRole = roleChoice;
|
|
1045
2550
|
detectionSource = "user selection";
|
|
1046
2551
|
} else {
|
|
1047
|
-
info(`Detected role: ${
|
|
1048
|
-
const confirmed = await confirmPrompt(`Role detected as ${
|
|
2552
|
+
info(`Detected role: ${pc10.bold(detectedRole)} (via ${detectionSource})`);
|
|
2553
|
+
const confirmed = await confirmPrompt(`Role detected as ${pc10.bold(detectedRole)}. Is this correct?`);
|
|
1049
2554
|
if (!confirmed) {
|
|
1050
2555
|
const roleChoice = await selectPrompt("Select your role:", ["maintainer", "contributor"]);
|
|
1051
2556
|
detectedRole = roleChoice;
|
|
@@ -1090,37 +2595,22 @@ var setup_default = defineCommand4({
|
|
|
1090
2595
|
warn(' echo ".contributerc.json" >> .gitignore');
|
|
1091
2596
|
}
|
|
1092
2597
|
console.log();
|
|
1093
|
-
info(`Workflow: ${
|
|
1094
|
-
info(`Convention: ${
|
|
1095
|
-
info(`Role: ${
|
|
2598
|
+
info(`Workflow: ${pc10.bold(WORKFLOW_DESCRIPTIONS[config.workflow])}`);
|
|
2599
|
+
info(`Convention: ${pc10.bold(CONVENTION_DESCRIPTIONS[config.commitConvention])}`);
|
|
2600
|
+
info(`Role: ${pc10.bold(config.role)}`);
|
|
1096
2601
|
if (config.devBranch) {
|
|
1097
|
-
info(`Main: ${
|
|
2602
|
+
info(`Main: ${pc10.bold(config.mainBranch)} | Dev: ${pc10.bold(config.devBranch)}`);
|
|
1098
2603
|
} else {
|
|
1099
|
-
info(`Main: ${
|
|
2604
|
+
info(`Main: ${pc10.bold(config.mainBranch)}`);
|
|
1100
2605
|
}
|
|
1101
|
-
info(`Origin: ${
|
|
2606
|
+
info(`Origin: ${pc10.bold(config.origin)}${config.role === "contributor" ? ` | Upstream: ${pc10.bold(config.upstream)}` : ""}`);
|
|
1102
2607
|
}
|
|
1103
2608
|
});
|
|
1104
2609
|
|
|
1105
2610
|
// src/commands/start.ts
|
|
1106
|
-
import { defineCommand as
|
|
1107
|
-
import
|
|
1108
|
-
|
|
1109
|
-
// src/utils/branch.ts
|
|
1110
|
-
var DEFAULT_PREFIXES = ["feature", "fix", "docs", "chore", "test", "refactor"];
|
|
1111
|
-
function hasPrefix(branchName, prefixes = DEFAULT_PREFIXES) {
|
|
1112
|
-
return prefixes.some((p) => branchName.startsWith(`${p}/`));
|
|
1113
|
-
}
|
|
1114
|
-
function formatBranchName(prefix, name) {
|
|
1115
|
-
const sanitized = name.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
|
1116
|
-
return `${prefix}/${sanitized}`;
|
|
1117
|
-
}
|
|
1118
|
-
function looksLikeNaturalLanguage(input) {
|
|
1119
|
-
return input.includes(" ") && !input.includes("/");
|
|
1120
|
-
}
|
|
1121
|
-
|
|
1122
|
-
// src/commands/start.ts
|
|
1123
|
-
var start_default = defineCommand5({
|
|
2611
|
+
import { defineCommand as defineCommand8 } from "citty";
|
|
2612
|
+
import pc11 from "picocolors";
|
|
2613
|
+
var start_default = defineCommand8({
|
|
1124
2614
|
meta: {
|
|
1125
2615
|
name: "start",
|
|
1126
2616
|
description: "Create a new feature branch from the latest base branch"
|
|
@@ -1128,8 +2618,8 @@ var start_default = defineCommand5({
|
|
|
1128
2618
|
args: {
|
|
1129
2619
|
name: {
|
|
1130
2620
|
type: "positional",
|
|
1131
|
-
description: "Branch name or description",
|
|
1132
|
-
required:
|
|
2621
|
+
description: "Branch name or description (prompted if omitted)",
|
|
2622
|
+
required: false
|
|
1133
2623
|
},
|
|
1134
2624
|
model: {
|
|
1135
2625
|
type: "string",
|
|
@@ -1160,42 +2650,57 @@ var start_default = defineCommand5({
|
|
|
1160
2650
|
const syncSource = getSyncSource(config);
|
|
1161
2651
|
let branchName = args.name;
|
|
1162
2652
|
heading("\uD83C\uDF3F contrib start");
|
|
2653
|
+
if (!branchName) {
|
|
2654
|
+
branchName = await inputPrompt("What are you going to work on?");
|
|
2655
|
+
if (!branchName || branchName.trim().length === 0) {
|
|
2656
|
+
error("A branch name or description is required.");
|
|
2657
|
+
process.exit(1);
|
|
2658
|
+
}
|
|
2659
|
+
branchName = branchName.trim();
|
|
2660
|
+
}
|
|
1163
2661
|
const useAI = !args["no-ai"] && looksLikeNaturalLanguage(branchName);
|
|
1164
2662
|
if (useAI) {
|
|
1165
|
-
|
|
2663
|
+
const spinner = createSpinner("Generating branch name suggestion...");
|
|
1166
2664
|
const suggested = await suggestBranchName(branchName, args.model);
|
|
1167
2665
|
if (suggested) {
|
|
2666
|
+
spinner.success("Branch name suggestion ready.");
|
|
1168
2667
|
console.log(`
|
|
1169
|
-
${
|
|
1170
|
-
const accepted = await confirmPrompt(`Use ${
|
|
2668
|
+
${pc11.dim("AI suggestion:")} ${pc11.bold(pc11.cyan(suggested))}`);
|
|
2669
|
+
const accepted = await confirmPrompt(`Use ${pc11.bold(suggested)} as your branch name?`);
|
|
1171
2670
|
if (accepted) {
|
|
1172
2671
|
branchName = suggested;
|
|
1173
2672
|
} else {
|
|
1174
2673
|
branchName = await inputPrompt("Enter branch name", branchName);
|
|
1175
2674
|
}
|
|
2675
|
+
} else {
|
|
2676
|
+
spinner.fail("AI did not return a branch name suggestion.");
|
|
1176
2677
|
}
|
|
1177
2678
|
}
|
|
1178
2679
|
if (!hasPrefix(branchName, branchPrefixes)) {
|
|
1179
|
-
const prefix = await selectPrompt(`Choose a branch type for ${
|
|
2680
|
+
const prefix = await selectPrompt(`Choose a branch type for ${pc11.bold(branchName)}:`, branchPrefixes);
|
|
1180
2681
|
branchName = formatBranchName(prefix, branchName);
|
|
1181
2682
|
}
|
|
1182
|
-
|
|
2683
|
+
if (!isValidBranchName(branchName)) {
|
|
2684
|
+
error("Invalid branch name. Use only alphanumeric characters, dots, hyphens, underscores, and slashes.");
|
|
2685
|
+
process.exit(1);
|
|
2686
|
+
}
|
|
2687
|
+
info(`Creating branch: ${pc11.bold(branchName)}`);
|
|
1183
2688
|
await fetchRemote(syncSource.remote);
|
|
1184
|
-
const
|
|
1185
|
-
if (
|
|
2689
|
+
const updateResult = await updateLocalBranch(baseBranch, syncSource.ref);
|
|
2690
|
+
if (updateResult.exitCode !== 0) {}
|
|
1186
2691
|
const result = await createBranch(branchName, baseBranch);
|
|
1187
2692
|
if (result.exitCode !== 0) {
|
|
1188
2693
|
error(`Failed to create branch: ${result.stderr}`);
|
|
1189
2694
|
process.exit(1);
|
|
1190
2695
|
}
|
|
1191
|
-
success(`✅ Created ${
|
|
2696
|
+
success(`✅ Created ${pc11.bold(branchName)} from latest ${pc11.bold(baseBranch)}`);
|
|
1192
2697
|
}
|
|
1193
2698
|
});
|
|
1194
2699
|
|
|
1195
2700
|
// src/commands/status.ts
|
|
1196
|
-
import { defineCommand as
|
|
1197
|
-
import
|
|
1198
|
-
var status_default =
|
|
2701
|
+
import { defineCommand as defineCommand9 } from "citty";
|
|
2702
|
+
import pc12 from "picocolors";
|
|
2703
|
+
var status_default = defineCommand9({
|
|
1199
2704
|
meta: {
|
|
1200
2705
|
name: "status",
|
|
1201
2706
|
description: "Show sync status of branches"
|
|
@@ -1211,17 +2716,17 @@ var status_default = defineCommand6({
|
|
|
1211
2716
|
process.exit(1);
|
|
1212
2717
|
}
|
|
1213
2718
|
heading("\uD83D\uDCCA contribute-now status");
|
|
1214
|
-
console.log(` ${
|
|
1215
|
-
console.log(` ${
|
|
2719
|
+
console.log(` ${pc12.dim("Workflow:")} ${pc12.bold(WORKFLOW_DESCRIPTIONS[config.workflow])}`);
|
|
2720
|
+
console.log(` ${pc12.dim("Role:")} ${pc12.bold(config.role)}`);
|
|
1216
2721
|
console.log();
|
|
1217
2722
|
await fetchAll();
|
|
1218
2723
|
const currentBranch = await getCurrentBranch();
|
|
1219
2724
|
const { mainBranch, origin, upstream, workflow } = config;
|
|
1220
2725
|
const baseBranch = getBaseBranch(config);
|
|
1221
2726
|
const isContributor = config.role === "contributor";
|
|
1222
|
-
const dirty = await hasUncommittedChanges();
|
|
2727
|
+
const [dirty, fileStatus] = await Promise.all([hasUncommittedChanges(), getFileStatus()]);
|
|
1223
2728
|
if (dirty) {
|
|
1224
|
-
console.log(` ${
|
|
2729
|
+
console.log(` ${pc12.yellow("⚠")} ${pc12.yellow("Uncommitted changes in working tree")}`);
|
|
1225
2730
|
console.log();
|
|
1226
2731
|
}
|
|
1227
2732
|
const mainRemote = `${origin}/${mainBranch}`;
|
|
@@ -1234,34 +2739,144 @@ var status_default = defineCommand6({
|
|
|
1234
2739
|
const devLine = formatStatus(config.devBranch, devRemoteRef, devDiv.ahead, devDiv.behind);
|
|
1235
2740
|
console.log(devLine);
|
|
1236
2741
|
}
|
|
1237
|
-
if (currentBranch && currentBranch !== mainBranch && currentBranch !== config.devBranch) {
|
|
1238
|
-
const branchDiv = await getDivergence(currentBranch, baseBranch);
|
|
1239
|
-
const branchLine = formatStatus(currentBranch, baseBranch, branchDiv.ahead, branchDiv.behind);
|
|
1240
|
-
console.log(branchLine +
|
|
1241
|
-
} else if (currentBranch) {
|
|
1242
|
-
console.log(
|
|
2742
|
+
if (currentBranch && currentBranch !== mainBranch && currentBranch !== config.devBranch) {
|
|
2743
|
+
const branchDiv = await getDivergence(currentBranch, baseBranch);
|
|
2744
|
+
const branchLine = formatStatus(currentBranch, baseBranch, branchDiv.ahead, branchDiv.behind);
|
|
2745
|
+
console.log(branchLine + pc12.dim(` (current ${pc12.green("*")})`));
|
|
2746
|
+
} else if (currentBranch) {
|
|
2747
|
+
console.log(pc12.dim(` (on ${pc12.bold(currentBranch)} branch)`));
|
|
2748
|
+
}
|
|
2749
|
+
const hasFiles = fileStatus.staged.length > 0 || fileStatus.modified.length > 0 || fileStatus.untracked.length > 0;
|
|
2750
|
+
if (hasFiles) {
|
|
2751
|
+
console.log();
|
|
2752
|
+
if (fileStatus.staged.length > 0) {
|
|
2753
|
+
console.log(` ${pc12.green("Staged for commit:")}`);
|
|
2754
|
+
for (const { file, status } of fileStatus.staged) {
|
|
2755
|
+
console.log(` ${pc12.green("+")} ${pc12.dim(`${status}:`)} ${file}`);
|
|
2756
|
+
}
|
|
2757
|
+
}
|
|
2758
|
+
if (fileStatus.modified.length > 0) {
|
|
2759
|
+
console.log(` ${pc12.yellow("Unstaged changes:")}`);
|
|
2760
|
+
for (const { file, status } of fileStatus.modified) {
|
|
2761
|
+
console.log(` ${pc12.yellow("~")} ${pc12.dim(`${status}:`)} ${file}`);
|
|
2762
|
+
}
|
|
2763
|
+
}
|
|
2764
|
+
if (fileStatus.untracked.length > 0) {
|
|
2765
|
+
console.log(` ${pc12.red("Untracked files:")}`);
|
|
2766
|
+
for (const file of fileStatus.untracked) {
|
|
2767
|
+
console.log(` ${pc12.red("?")} ${file}`);
|
|
2768
|
+
}
|
|
2769
|
+
}
|
|
2770
|
+
} else if (!dirty) {
|
|
2771
|
+
console.log(` ${pc12.green("✓")} ${pc12.dim("Working tree clean")}`);
|
|
2772
|
+
}
|
|
2773
|
+
const tips = [];
|
|
2774
|
+
if (fileStatus.staged.length > 0) {
|
|
2775
|
+
tips.push(`Run ${pc12.bold("contrib commit")} to commit staged changes`);
|
|
2776
|
+
}
|
|
2777
|
+
if (fileStatus.modified.length > 0 || fileStatus.untracked.length > 0) {
|
|
2778
|
+
tips.push(`Run ${pc12.bold("contrib commit")} to stage and commit changes`);
|
|
2779
|
+
}
|
|
2780
|
+
if (fileStatus.staged.length === 0 && fileStatus.modified.length === 0 && fileStatus.untracked.length === 0 && currentBranch && currentBranch !== mainBranch && currentBranch !== config.devBranch) {
|
|
2781
|
+
const branchDiv = await getDivergence(currentBranch, `${origin}/${currentBranch}`);
|
|
2782
|
+
if (branchDiv.ahead > 0) {
|
|
2783
|
+
tips.push(`Run ${pc12.bold("contrib submit")} to push and create/update your PR`);
|
|
2784
|
+
}
|
|
2785
|
+
}
|
|
2786
|
+
if (tips.length > 0) {
|
|
2787
|
+
console.log();
|
|
2788
|
+
console.log(` ${pc12.dim("\uD83D\uDCA1 Tip:")}`);
|
|
2789
|
+
for (const tip of tips) {
|
|
2790
|
+
console.log(` ${pc12.dim(tip)}`);
|
|
2791
|
+
}
|
|
1243
2792
|
}
|
|
1244
2793
|
console.log();
|
|
1245
2794
|
}
|
|
1246
2795
|
});
|
|
1247
2796
|
function formatStatus(branch, base, ahead, behind) {
|
|
1248
|
-
const label =
|
|
2797
|
+
const label = pc12.bold(branch.padEnd(20));
|
|
1249
2798
|
if (ahead === 0 && behind === 0) {
|
|
1250
|
-
return ` ${
|
|
2799
|
+
return ` ${pc12.green("✓")} ${label} ${pc12.dim(`in sync with ${base}`)}`;
|
|
1251
2800
|
}
|
|
1252
2801
|
if (ahead > 0 && behind === 0) {
|
|
1253
|
-
return ` ${
|
|
2802
|
+
return ` ${pc12.yellow("↑")} ${label} ${pc12.yellow(`${ahead} commit${ahead !== 1 ? "s" : ""} ahead of ${base}`)}`;
|
|
1254
2803
|
}
|
|
1255
2804
|
if (behind > 0 && ahead === 0) {
|
|
1256
|
-
return ` ${
|
|
2805
|
+
return ` ${pc12.red("↓")} ${label} ${pc12.red(`${behind} commit${behind !== 1 ? "s" : ""} behind ${base}`)}`;
|
|
1257
2806
|
}
|
|
1258
|
-
return ` ${
|
|
2807
|
+
return ` ${pc12.red("⚡")} ${label} ${pc12.yellow(`${ahead} ahead`)}${pc12.dim(", ")}${pc12.red(`${behind} behind`)} ${pc12.dim(base)}`;
|
|
1259
2808
|
}
|
|
1260
2809
|
|
|
1261
2810
|
// src/commands/submit.ts
|
|
1262
|
-
import { defineCommand as
|
|
1263
|
-
import
|
|
1264
|
-
|
|
2811
|
+
import { defineCommand as defineCommand10 } from "citty";
|
|
2812
|
+
import pc13 from "picocolors";
|
|
2813
|
+
async function performSquashMerge(origin, baseBranch, featureBranch, options) {
|
|
2814
|
+
info(`Checking out ${pc13.bold(baseBranch)}...`);
|
|
2815
|
+
const coResult = await checkoutBranch2(baseBranch);
|
|
2816
|
+
if (coResult.exitCode !== 0) {
|
|
2817
|
+
error(`Failed to checkout ${baseBranch}: ${coResult.stderr}`);
|
|
2818
|
+
process.exit(1);
|
|
2819
|
+
}
|
|
2820
|
+
info(`Squash merging ${pc13.bold(featureBranch)} into ${pc13.bold(baseBranch)}...`);
|
|
2821
|
+
const mergeResult = await mergeSquash(featureBranch);
|
|
2822
|
+
if (mergeResult.exitCode !== 0) {
|
|
2823
|
+
error(`Squash merge failed: ${mergeResult.stderr}`);
|
|
2824
|
+
process.exit(1);
|
|
2825
|
+
}
|
|
2826
|
+
let message = options?.defaultMsg;
|
|
2827
|
+
if (!message) {
|
|
2828
|
+
const copilotError = await checkCopilotAvailable();
|
|
2829
|
+
if (!copilotError) {
|
|
2830
|
+
const spinner = createSpinner("Generating AI commit message for squash merge...");
|
|
2831
|
+
const [stagedDiff, stagedFiles] = await Promise.all([getStagedDiff(), getStagedFiles()]);
|
|
2832
|
+
const aiMsg = await generateCommitMessage(stagedDiff, stagedFiles, options?.model, options?.convention ?? "clean-commit");
|
|
2833
|
+
if (aiMsg) {
|
|
2834
|
+
message = aiMsg;
|
|
2835
|
+
spinner.success("AI commit message generated.");
|
|
2836
|
+
} else {
|
|
2837
|
+
spinner.fail("AI did not return a commit message.");
|
|
2838
|
+
}
|
|
2839
|
+
} else {
|
|
2840
|
+
warn(`AI unavailable: ${copilotError}`);
|
|
2841
|
+
}
|
|
2842
|
+
}
|
|
2843
|
+
const fallback = message || `squash merge ${featureBranch}`;
|
|
2844
|
+
let finalMsg;
|
|
2845
|
+
if (message) {
|
|
2846
|
+
console.log(` ${pc13.dim("Commit message:")} ${pc13.bold(message)}`);
|
|
2847
|
+
finalMsg = message;
|
|
2848
|
+
} else {
|
|
2849
|
+
finalMsg = await inputPrompt("Commit message", fallback);
|
|
2850
|
+
}
|
|
2851
|
+
const commitResult = await commitWithMessage(finalMsg);
|
|
2852
|
+
if (commitResult.exitCode !== 0) {
|
|
2853
|
+
error(`Commit failed: ${commitResult.stderr}`);
|
|
2854
|
+
process.exit(1);
|
|
2855
|
+
}
|
|
2856
|
+
info(`Pushing ${pc13.bold(baseBranch)} to ${origin}...`);
|
|
2857
|
+
const pushResult = await pushBranch(origin, baseBranch);
|
|
2858
|
+
if (pushResult.exitCode !== 0) {
|
|
2859
|
+
error(`Failed to push ${baseBranch}: ${pushResult.stderr}`);
|
|
2860
|
+
process.exit(1);
|
|
2861
|
+
}
|
|
2862
|
+
info(`Deleting local branch ${pc13.bold(featureBranch)}...`);
|
|
2863
|
+
const delLocal = await forceDeleteBranch(featureBranch);
|
|
2864
|
+
if (delLocal.exitCode !== 0) {
|
|
2865
|
+
warn(`Could not delete local branch: ${delLocal.stderr.trim()}`);
|
|
2866
|
+
}
|
|
2867
|
+
const remoteBranchRef = `${origin}/${featureBranch}`;
|
|
2868
|
+
const remoteExists = await branchExists(remoteBranchRef);
|
|
2869
|
+
if (remoteExists) {
|
|
2870
|
+
info(`Deleting remote branch ${pc13.bold(featureBranch)}...`);
|
|
2871
|
+
const delRemote = await deleteRemoteBranch(origin, featureBranch);
|
|
2872
|
+
if (delRemote.exitCode !== 0) {
|
|
2873
|
+
warn(`Could not delete remote branch: ${delRemote.stderr.trim()}`);
|
|
2874
|
+
}
|
|
2875
|
+
}
|
|
2876
|
+
success(`✅ Squash merged ${pc13.bold(featureBranch)} into ${pc13.bold(baseBranch)} and pushed.`);
|
|
2877
|
+
info(`Run ${pc13.bold("contrib start")} to begin a new feature.`);
|
|
2878
|
+
}
|
|
2879
|
+
var submit_default = defineCommand10({
|
|
1265
2880
|
meta: {
|
|
1266
2881
|
name: "submit",
|
|
1267
2882
|
description: "Push current branch and create a pull request"
|
|
@@ -1301,89 +2916,316 @@ var submit_default = defineCommand7({
|
|
|
1301
2916
|
process.exit(1);
|
|
1302
2917
|
}
|
|
1303
2918
|
if (protectedBranches.includes(currentBranch)) {
|
|
1304
|
-
|
|
1305
|
-
|
|
2919
|
+
heading("\uD83D\uDE80 contrib submit");
|
|
2920
|
+
warn(`You're on ${pc13.bold(currentBranch)}, which is a protected branch. PRs should come from feature branches.`);
|
|
2921
|
+
await fetchAll();
|
|
2922
|
+
const remoteRef = `${origin}/${currentBranch}`;
|
|
2923
|
+
const localWork = await hasLocalWork(origin, currentBranch);
|
|
2924
|
+
const dirty = await hasUncommittedChanges();
|
|
2925
|
+
const hasCommits = localWork.unpushedCommits > 0;
|
|
2926
|
+
const hasAnything = hasCommits || dirty;
|
|
2927
|
+
if (!hasAnything) {
|
|
2928
|
+
error("No local changes or commits to move. Switch to a feature branch first.");
|
|
2929
|
+
info(` Run ${pc13.bold("contrib start")} to create a new feature branch.`);
|
|
2930
|
+
process.exit(1);
|
|
2931
|
+
}
|
|
2932
|
+
if (hasCommits) {
|
|
2933
|
+
info(`Found ${pc13.bold(String(localWork.unpushedCommits))} unpushed commit${localWork.unpushedCommits !== 1 ? "s" : ""} on ${pc13.bold(currentBranch)}.`);
|
|
2934
|
+
}
|
|
2935
|
+
if (dirty) {
|
|
2936
|
+
info("You also have uncommitted changes in the working tree.");
|
|
2937
|
+
}
|
|
2938
|
+
console.log();
|
|
2939
|
+
const MOVE_BRANCH = "Move my changes to a new feature branch";
|
|
2940
|
+
const CANCEL2 = "Cancel (stay on this branch)";
|
|
2941
|
+
const action = await selectPrompt("Let's get you back on track. What would you like to do?", [MOVE_BRANCH, CANCEL2]);
|
|
2942
|
+
if (action === CANCEL2) {
|
|
2943
|
+
info("No changes made. You are still on your current branch.");
|
|
2944
|
+
return;
|
|
2945
|
+
}
|
|
2946
|
+
info(pc13.dim("Tip: Describe what you're going to work on in plain English and we'll generate a branch name."));
|
|
2947
|
+
const description = await inputPrompt("What are you going to work on?");
|
|
2948
|
+
let newBranchName = description;
|
|
2949
|
+
if (looksLikeNaturalLanguage(description)) {
|
|
2950
|
+
const copilotError = await checkCopilotAvailable();
|
|
2951
|
+
if (!copilotError) {
|
|
2952
|
+
const spinner = createSpinner("Generating branch name suggestion...");
|
|
2953
|
+
const suggested = await suggestBranchName(description, args.model);
|
|
2954
|
+
if (suggested) {
|
|
2955
|
+
spinner.success("Branch name suggestion ready.");
|
|
2956
|
+
console.log(`
|
|
2957
|
+
${pc13.dim("AI suggestion:")} ${pc13.bold(pc13.cyan(suggested))}`);
|
|
2958
|
+
const accepted = await confirmPrompt(`Use ${pc13.bold(suggested)} as your branch name?`);
|
|
2959
|
+
newBranchName = accepted ? suggested : await inputPrompt("Enter branch name", description);
|
|
2960
|
+
} else {
|
|
2961
|
+
spinner.fail("AI did not return a suggestion.");
|
|
2962
|
+
newBranchName = await inputPrompt("Enter branch name", description);
|
|
2963
|
+
}
|
|
2964
|
+
}
|
|
2965
|
+
}
|
|
2966
|
+
if (!hasPrefix(newBranchName, config.branchPrefixes)) {
|
|
2967
|
+
const prefix = await selectPrompt(`Choose a branch type for ${pc13.bold(newBranchName)}:`, config.branchPrefixes);
|
|
2968
|
+
newBranchName = formatBranchName(prefix, newBranchName);
|
|
2969
|
+
}
|
|
2970
|
+
if (!isValidBranchName(newBranchName)) {
|
|
2971
|
+
error("Invalid branch name. Use only alphanumeric characters, dots, hyphens, underscores, and slashes.");
|
|
2972
|
+
process.exit(1);
|
|
2973
|
+
}
|
|
2974
|
+
const branchResult = await createBranch(newBranchName);
|
|
2975
|
+
if (branchResult.exitCode !== 0) {
|
|
2976
|
+
error(`Failed to create branch: ${branchResult.stderr}`);
|
|
2977
|
+
process.exit(1);
|
|
2978
|
+
}
|
|
2979
|
+
success(`Created ${pc13.bold(newBranchName)} with your changes.`);
|
|
2980
|
+
await updateLocalBranch(currentBranch, remoteRef);
|
|
2981
|
+
info(`Reset ${pc13.bold(currentBranch)} back to ${pc13.bold(remoteRef)} — no damage done.`);
|
|
2982
|
+
console.log();
|
|
2983
|
+
success(`You're now on ${pc13.bold(newBranchName)} with all your work intact.`);
|
|
2984
|
+
info(`Run ${pc13.bold("contrib submit")} again to push and create your PR.`);
|
|
2985
|
+
return;
|
|
1306
2986
|
}
|
|
1307
2987
|
heading("\uD83D\uDE80 contrib submit");
|
|
1308
|
-
info(`Pushing ${pc9.bold(currentBranch)} to ${origin}...`);
|
|
1309
|
-
const pushResult = await pushSetUpstream(origin, currentBranch);
|
|
1310
|
-
if (pushResult.exitCode !== 0) {
|
|
1311
|
-
error(`Failed to push: ${pushResult.stderr}`);
|
|
1312
|
-
process.exit(1);
|
|
1313
|
-
}
|
|
1314
2988
|
const ghInstalled = await checkGhInstalled();
|
|
1315
2989
|
const ghAuthed = ghInstalled && await checkGhAuth();
|
|
1316
|
-
if (
|
|
1317
|
-
const
|
|
1318
|
-
if (
|
|
1319
|
-
|
|
2990
|
+
if (ghInstalled && ghAuthed) {
|
|
2991
|
+
const mergedPR = await getMergedPRForBranch(currentBranch);
|
|
2992
|
+
if (mergedPR) {
|
|
2993
|
+
warn(`PR #${mergedPR.number} (${pc13.bold(mergedPR.title)}) was already merged.`);
|
|
2994
|
+
const localWork = await hasLocalWork(origin, currentBranch);
|
|
2995
|
+
const hasWork = localWork.uncommitted || localWork.unpushedCommits > 0;
|
|
2996
|
+
if (hasWork) {
|
|
2997
|
+
if (localWork.uncommitted) {
|
|
2998
|
+
warn("You have uncommitted changes in your working tree.");
|
|
2999
|
+
}
|
|
3000
|
+
if (localWork.unpushedCommits > 0) {
|
|
3001
|
+
warn(`You have ${pc13.bold(String(localWork.unpushedCommits))} local commit${localWork.unpushedCommits !== 1 ? "s" : ""} not in the merged PR.`);
|
|
3002
|
+
}
|
|
3003
|
+
const SAVE_NEW_BRANCH = "Save changes to a new branch";
|
|
3004
|
+
const DISCARD = "Discard all changes and clean up";
|
|
3005
|
+
const CANCEL2 = "Cancel";
|
|
3006
|
+
const action = await selectPrompt("This branch was merged but you have local changes. What would you like to do?", [SAVE_NEW_BRANCH, DISCARD, CANCEL2]);
|
|
3007
|
+
if (action === CANCEL2) {
|
|
3008
|
+
info("No changes made. You are still on your current branch.");
|
|
3009
|
+
return;
|
|
3010
|
+
}
|
|
3011
|
+
if (action === SAVE_NEW_BRANCH) {
|
|
3012
|
+
info(pc13.dim("Tip: Describe what you're going to work on in plain English and we'll generate a branch name."));
|
|
3013
|
+
const description = await inputPrompt("What are you going to work on?");
|
|
3014
|
+
let newBranchName = description;
|
|
3015
|
+
if (!args["no-ai"] && looksLikeNaturalLanguage(description)) {
|
|
3016
|
+
const spinner = createSpinner("Generating branch name suggestion...");
|
|
3017
|
+
const suggested = await suggestBranchName(description, args.model);
|
|
3018
|
+
if (suggested) {
|
|
3019
|
+
spinner.success("Branch name suggestion ready.");
|
|
3020
|
+
console.log(`
|
|
3021
|
+
${pc13.dim("AI suggestion:")} ${pc13.bold(pc13.cyan(suggested))}`);
|
|
3022
|
+
const accepted = await confirmPrompt(`Use ${pc13.bold(suggested)} as your branch name?`);
|
|
3023
|
+
newBranchName = accepted ? suggested : await inputPrompt("Enter branch name", description);
|
|
3024
|
+
} else {
|
|
3025
|
+
spinner.fail("AI did not return a suggestion.");
|
|
3026
|
+
newBranchName = await inputPrompt("Enter branch name", description);
|
|
3027
|
+
}
|
|
3028
|
+
}
|
|
3029
|
+
if (!hasPrefix(newBranchName, config.branchPrefixes)) {
|
|
3030
|
+
const prefix = await selectPrompt(`Choose a branch type for ${pc13.bold(newBranchName)}:`, config.branchPrefixes);
|
|
3031
|
+
newBranchName = formatBranchName(prefix, newBranchName);
|
|
3032
|
+
}
|
|
3033
|
+
if (!isValidBranchName(newBranchName)) {
|
|
3034
|
+
error("Invalid branch name. Use only alphanumeric characters, dots, hyphens, underscores, and slashes.");
|
|
3035
|
+
process.exit(1);
|
|
3036
|
+
}
|
|
3037
|
+
const staleUpstream = await getUpstreamRef();
|
|
3038
|
+
const staleUpstreamHash = staleUpstream ? await getCommitHash(staleUpstream) : null;
|
|
3039
|
+
const renameResult = await renameBranch(currentBranch, newBranchName);
|
|
3040
|
+
if (renameResult.exitCode !== 0) {
|
|
3041
|
+
error(`Failed to rename branch: ${renameResult.stderr}`);
|
|
3042
|
+
process.exit(1);
|
|
3043
|
+
}
|
|
3044
|
+
success(`Renamed ${pc13.bold(currentBranch)} → ${pc13.bold(newBranchName)}`);
|
|
3045
|
+
await unsetUpstream();
|
|
3046
|
+
const syncSource2 = getSyncSource(config);
|
|
3047
|
+
info(`Syncing ${pc13.bold(newBranchName)} with latest ${pc13.bold(baseBranch)}...`);
|
|
3048
|
+
await fetchRemote(syncSource2.remote);
|
|
3049
|
+
let rebaseResult;
|
|
3050
|
+
if (staleUpstreamHash) {
|
|
3051
|
+
rebaseResult = await rebaseOnto(syncSource2.ref, staleUpstreamHash);
|
|
3052
|
+
} else {
|
|
3053
|
+
const savedStrategy = await determineRebaseStrategy(newBranchName, syncSource2.ref);
|
|
3054
|
+
rebaseResult = savedStrategy.strategy === "onto" && savedStrategy.ontoOldBase ? await rebaseOnto(syncSource2.ref, savedStrategy.ontoOldBase) : await rebase(syncSource2.ref);
|
|
3055
|
+
}
|
|
3056
|
+
if (rebaseResult.exitCode !== 0) {
|
|
3057
|
+
warn("Rebase encountered conflicts. Resolve them manually, then run:");
|
|
3058
|
+
info(` ${pc13.bold("git rebase --continue")}`);
|
|
3059
|
+
} else {
|
|
3060
|
+
success(`Rebased ${pc13.bold(newBranchName)} onto ${pc13.bold(syncSource2.ref)}.`);
|
|
3061
|
+
}
|
|
3062
|
+
info(`All your changes are preserved. Run ${pc13.bold("contrib submit")} when ready to create a new PR.`);
|
|
3063
|
+
return;
|
|
3064
|
+
}
|
|
3065
|
+
warn("Discarding local changes...");
|
|
3066
|
+
}
|
|
3067
|
+
const syncSource = getSyncSource(config);
|
|
3068
|
+
info(`Switching to ${pc13.bold(baseBranch)} and syncing...`);
|
|
3069
|
+
await fetchRemote(syncSource.remote);
|
|
3070
|
+
const coResult = await checkoutBranch2(baseBranch);
|
|
3071
|
+
if (coResult.exitCode !== 0) {
|
|
3072
|
+
error(`Failed to checkout ${baseBranch}: ${coResult.stderr}`);
|
|
3073
|
+
process.exit(1);
|
|
3074
|
+
}
|
|
3075
|
+
await updateLocalBranch(baseBranch, syncSource.ref);
|
|
3076
|
+
success(`Synced ${pc13.bold(baseBranch)} with ${pc13.bold(syncSource.ref)}.`);
|
|
3077
|
+
info(`Deleting stale branch ${pc13.bold(currentBranch)}...`);
|
|
3078
|
+
const delResult = await forceDeleteBranch(currentBranch);
|
|
3079
|
+
if (delResult.exitCode === 0) {
|
|
3080
|
+
success(`Deleted ${pc13.bold(currentBranch)}.`);
|
|
3081
|
+
} else {
|
|
3082
|
+
warn(`Could not delete branch: ${delResult.stderr.trim()}`);
|
|
3083
|
+
}
|
|
1320
3084
|
console.log();
|
|
1321
|
-
info(
|
|
1322
|
-
|
|
1323
|
-
} else {
|
|
1324
|
-
info("gh CLI not available. Create your PR manually on GitHub.");
|
|
3085
|
+
info(`You're now on ${pc13.bold(baseBranch)}. Run ${pc13.bold("contrib start")} to begin a new feature.`);
|
|
3086
|
+
return;
|
|
1325
3087
|
}
|
|
1326
|
-
return;
|
|
1327
3088
|
}
|
|
1328
3089
|
let prTitle = null;
|
|
1329
3090
|
let prBody = null;
|
|
1330
|
-
|
|
1331
|
-
const copilotError = await
|
|
3091
|
+
async function tryGenerateAI() {
|
|
3092
|
+
const [copilotError, commits, diff] = await Promise.all([
|
|
3093
|
+
checkCopilotAvailable(),
|
|
3094
|
+
getLog(baseBranch, "HEAD"),
|
|
3095
|
+
getLogDiff(baseBranch, "HEAD")
|
|
3096
|
+
]);
|
|
1332
3097
|
if (!copilotError) {
|
|
1333
|
-
|
|
1334
|
-
const
|
|
1335
|
-
const diff = await getLogDiff(baseBranch, "HEAD");
|
|
1336
|
-
const result = await generatePRDescription(commits, diff, args.model);
|
|
3098
|
+
const spinner = createSpinner("Generating AI PR description...");
|
|
3099
|
+
const result = await generatePRDescription(commits, diff, args.model, config.commitConvention);
|
|
1337
3100
|
if (result) {
|
|
1338
3101
|
prTitle = result.title;
|
|
1339
3102
|
prBody = result.body;
|
|
3103
|
+
spinner.success("PR description generated.");
|
|
1340
3104
|
console.log(`
|
|
1341
|
-
${
|
|
3105
|
+
${pc13.dim("AI title:")} ${pc13.bold(pc13.cyan(prTitle))}`);
|
|
1342
3106
|
console.log(`
|
|
1343
|
-
${
|
|
1344
|
-
console.log(
|
|
3107
|
+
${pc13.dim("AI body preview:")}`);
|
|
3108
|
+
console.log(pc13.dim(prBody.slice(0, 300) + (prBody.length > 300 ? "..." : "")));
|
|
1345
3109
|
} else {
|
|
1346
|
-
|
|
3110
|
+
spinner.fail("AI did not return a PR description.");
|
|
1347
3111
|
}
|
|
1348
3112
|
} else {
|
|
1349
3113
|
warn(`AI unavailable: ${copilotError}`);
|
|
1350
3114
|
}
|
|
1351
3115
|
}
|
|
1352
|
-
if (
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
3116
|
+
if (!args["no-ai"]) {
|
|
3117
|
+
await tryGenerateAI();
|
|
3118
|
+
}
|
|
3119
|
+
const CANCEL = "Cancel";
|
|
3120
|
+
const SQUASH_LOCAL = `Squash merge to ${baseBranch} locally (no PR)`;
|
|
3121
|
+
const REGENERATE = "Regenerate AI description";
|
|
3122
|
+
let submitAction = "cancel";
|
|
3123
|
+
const isMaintainer = config.role === "maintainer";
|
|
3124
|
+
let actionResolved = false;
|
|
3125
|
+
while (!actionResolved) {
|
|
3126
|
+
if (prTitle && prBody) {
|
|
3127
|
+
const choices = ["Use AI description"];
|
|
3128
|
+
if (isMaintainer)
|
|
3129
|
+
choices.push(SQUASH_LOCAL);
|
|
3130
|
+
choices.push("Edit title", "Write manually", "Use gh --fill (auto-fill from commits)", REGENERATE, CANCEL);
|
|
3131
|
+
const action = await selectPrompt("What would you like to do with the PR description?", choices);
|
|
3132
|
+
if (action === CANCEL) {
|
|
3133
|
+
submitAction = "cancel";
|
|
3134
|
+
actionResolved = true;
|
|
3135
|
+
} else if (action === REGENERATE) {
|
|
3136
|
+
prTitle = null;
|
|
3137
|
+
prBody = null;
|
|
3138
|
+
await tryGenerateAI();
|
|
3139
|
+
} else if (action === SQUASH_LOCAL) {
|
|
3140
|
+
submitAction = "squash";
|
|
3141
|
+
actionResolved = true;
|
|
3142
|
+
} else if (action === "Use AI description") {
|
|
3143
|
+
submitAction = "create-pr";
|
|
3144
|
+
actionResolved = true;
|
|
3145
|
+
} else if (action === "Edit title") {
|
|
3146
|
+
prTitle = await inputPrompt("PR title", prTitle);
|
|
3147
|
+
submitAction = "create-pr";
|
|
3148
|
+
actionResolved = true;
|
|
3149
|
+
} else if (action === "Write manually") {
|
|
3150
|
+
prTitle = await inputPrompt("PR title");
|
|
3151
|
+
prBody = await inputPrompt("PR body (markdown)");
|
|
3152
|
+
submitAction = "create-pr";
|
|
3153
|
+
actionResolved = true;
|
|
3154
|
+
} else {
|
|
3155
|
+
submitAction = "fill";
|
|
3156
|
+
actionResolved = true;
|
|
3157
|
+
}
|
|
1364
3158
|
} else {
|
|
1365
|
-
const
|
|
1366
|
-
if (
|
|
1367
|
-
|
|
1368
|
-
|
|
3159
|
+
const choices = [];
|
|
3160
|
+
if (isMaintainer)
|
|
3161
|
+
choices.push(SQUASH_LOCAL);
|
|
3162
|
+
if (!args["no-ai"])
|
|
3163
|
+
choices.push(REGENERATE);
|
|
3164
|
+
choices.push("Write title & body manually", "Use gh --fill (auto-fill from commits)", CANCEL);
|
|
3165
|
+
const action = await selectPrompt("How would you like to create the PR?", choices);
|
|
3166
|
+
if (action === CANCEL) {
|
|
3167
|
+
submitAction = "cancel";
|
|
3168
|
+
actionResolved = true;
|
|
3169
|
+
} else if (action === REGENERATE) {
|
|
3170
|
+
await tryGenerateAI();
|
|
3171
|
+
} else if (action === SQUASH_LOCAL) {
|
|
3172
|
+
submitAction = "squash";
|
|
3173
|
+
actionResolved = true;
|
|
3174
|
+
} else if (action === "Write title & body manually") {
|
|
3175
|
+
prTitle = await inputPrompt("PR title");
|
|
3176
|
+
prBody = await inputPrompt("PR body (markdown)");
|
|
3177
|
+
submitAction = "create-pr";
|
|
3178
|
+
actionResolved = true;
|
|
3179
|
+
} else {
|
|
3180
|
+
submitAction = "fill";
|
|
3181
|
+
actionResolved = true;
|
|
1369
3182
|
}
|
|
1370
|
-
success(`✅ PR created: ${fillResult.stdout.trim()}`);
|
|
1371
|
-
return;
|
|
1372
3183
|
}
|
|
1373
|
-
}
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
3184
|
+
}
|
|
3185
|
+
if (submitAction === "cancel") {
|
|
3186
|
+
warn("Submit cancelled.");
|
|
3187
|
+
return;
|
|
3188
|
+
}
|
|
3189
|
+
if (submitAction === "squash") {
|
|
3190
|
+
await performSquashMerge(origin, baseBranch, currentBranch, {
|
|
3191
|
+
defaultMsg: prTitle ?? undefined,
|
|
3192
|
+
model: args.model,
|
|
3193
|
+
convention: config.commitConvention
|
|
3194
|
+
});
|
|
3195
|
+
return;
|
|
3196
|
+
}
|
|
3197
|
+
info(`Pushing ${pc13.bold(currentBranch)} to ${origin}...`);
|
|
3198
|
+
const pushResult = await pushSetUpstream(origin, currentBranch);
|
|
3199
|
+
if (pushResult.exitCode !== 0) {
|
|
3200
|
+
error(`Failed to push: ${pushResult.stderr}`);
|
|
3201
|
+
process.exit(1);
|
|
3202
|
+
}
|
|
3203
|
+
if (!ghInstalled || !ghAuthed) {
|
|
3204
|
+
const repoInfo = await getRepoInfoFromRemote(origin);
|
|
3205
|
+
if (repoInfo) {
|
|
3206
|
+
const prUrl = `https://github.com/${repoInfo.owner}/${repoInfo.repo}/compare/${baseBranch}...${currentBranch}?expand=1`;
|
|
3207
|
+
console.log();
|
|
3208
|
+
info("Create your PR manually:");
|
|
3209
|
+
console.log(` ${pc13.cyan(prUrl)}`);
|
|
1378
3210
|
} else {
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
3211
|
+
info("gh CLI not available. Create your PR manually on GitHub.");
|
|
3212
|
+
}
|
|
3213
|
+
return;
|
|
3214
|
+
}
|
|
3215
|
+
const existingPR = await getPRForBranch(currentBranch);
|
|
3216
|
+
if (existingPR) {
|
|
3217
|
+
success(`Pushed changes to existing PR #${existingPR.number}: ${pc13.bold(existingPR.title)}`);
|
|
3218
|
+
console.log(` ${pc13.cyan(existingPR.url)}`);
|
|
3219
|
+
return;
|
|
3220
|
+
}
|
|
3221
|
+
if (submitAction === "fill") {
|
|
3222
|
+
const fillResult = await createPRFill(baseBranch, args.draft);
|
|
3223
|
+
if (fillResult.exitCode !== 0) {
|
|
3224
|
+
error(`Failed to create PR: ${fillResult.stderr}`);
|
|
3225
|
+
process.exit(1);
|
|
1386
3226
|
}
|
|
3227
|
+
success(`✅ PR created: ${fillResult.stdout.trim()}`);
|
|
3228
|
+
return;
|
|
1387
3229
|
}
|
|
1388
3230
|
if (!prTitle) {
|
|
1389
3231
|
error("No PR title provided.");
|
|
@@ -1404,9 +3246,9 @@ ${pc9.dim("AI body preview:")}`);
|
|
|
1404
3246
|
});
|
|
1405
3247
|
|
|
1406
3248
|
// src/commands/sync.ts
|
|
1407
|
-
import { defineCommand as
|
|
1408
|
-
import
|
|
1409
|
-
var sync_default =
|
|
3249
|
+
import { defineCommand as defineCommand11 } from "citty";
|
|
3250
|
+
import pc14 from "picocolors";
|
|
3251
|
+
var sync_default = defineCommand11({
|
|
1410
3252
|
meta: {
|
|
1411
3253
|
name: "sync",
|
|
1412
3254
|
description: "Sync your local branches with the remote"
|
|
@@ -1417,6 +3259,15 @@ var sync_default = defineCommand8({
|
|
|
1417
3259
|
alias: "y",
|
|
1418
3260
|
description: "Skip confirmation prompt",
|
|
1419
3261
|
default: false
|
|
3262
|
+
},
|
|
3263
|
+
model: {
|
|
3264
|
+
type: "string",
|
|
3265
|
+
description: "AI model to use for branch name suggestion"
|
|
3266
|
+
},
|
|
3267
|
+
"no-ai": {
|
|
3268
|
+
type: "boolean",
|
|
3269
|
+
description: "Skip AI branch name suggestion",
|
|
3270
|
+
default: false
|
|
1420
3271
|
}
|
|
1421
3272
|
},
|
|
1422
3273
|
async run({ args }) {
|
|
@@ -1448,16 +3299,89 @@ var sync_default = defineCommand8({
|
|
|
1448
3299
|
}
|
|
1449
3300
|
const div = await getDivergence(baseBranch, syncSource.ref);
|
|
1450
3301
|
if (div.ahead > 0 || div.behind > 0) {
|
|
1451
|
-
info(`${
|
|
3302
|
+
info(`${pc14.bold(baseBranch)} is ${pc14.yellow(`${div.ahead} ahead`)} and ${pc14.red(`${div.behind} behind`)} ${syncSource.ref}`);
|
|
1452
3303
|
} else {
|
|
1453
|
-
info(`${
|
|
3304
|
+
info(`${pc14.bold(baseBranch)} is already in sync with ${syncSource.ref}`);
|
|
3305
|
+
}
|
|
3306
|
+
if (div.ahead > 0) {
|
|
3307
|
+
const currentBranch = await getCurrentBranch();
|
|
3308
|
+
const protectedBranches = getProtectedBranches(config);
|
|
3309
|
+
const isOnProtected = currentBranch && protectedBranches.includes(currentBranch);
|
|
3310
|
+
if (isOnProtected) {
|
|
3311
|
+
warn(`You have ${pc14.bold(String(div.ahead))} local commit${div.ahead !== 1 ? "s" : ""} on ${pc14.bold(baseBranch)} that aren't on the remote.`);
|
|
3312
|
+
info("Pulling now could create a merge commit, which breaks clean history.");
|
|
3313
|
+
console.log();
|
|
3314
|
+
const MOVE_BRANCH = "Move my commits to a new feature branch, then sync";
|
|
3315
|
+
const PULL_ANYWAY = "Pull anyway (may create a merge commit)";
|
|
3316
|
+
const CANCEL = "Cancel";
|
|
3317
|
+
const action = await selectPrompt("How would you like to handle this?", [MOVE_BRANCH, PULL_ANYWAY, CANCEL]);
|
|
3318
|
+
if (action === CANCEL) {
|
|
3319
|
+
info("No changes made.");
|
|
3320
|
+
return;
|
|
3321
|
+
}
|
|
3322
|
+
if (action === MOVE_BRANCH) {
|
|
3323
|
+
info(pc14.dim("Tip: Describe what you're going to work on in plain English and we'll generate a branch name."));
|
|
3324
|
+
const description = await inputPrompt("What are you going to work on?");
|
|
3325
|
+
let newBranchName = description;
|
|
3326
|
+
if (!args["no-ai"] && looksLikeNaturalLanguage(description)) {
|
|
3327
|
+
const copilotError = await checkCopilotAvailable();
|
|
3328
|
+
if (!copilotError) {
|
|
3329
|
+
const spinner = createSpinner("Generating branch name suggestion...");
|
|
3330
|
+
const suggested = await suggestBranchName(description, args.model);
|
|
3331
|
+
if (suggested) {
|
|
3332
|
+
spinner.success("Branch name suggestion ready.");
|
|
3333
|
+
console.log(`
|
|
3334
|
+
${pc14.dim("AI suggestion:")} ${pc14.bold(pc14.cyan(suggested))}`);
|
|
3335
|
+
const accepted = await confirmPrompt(`Use ${pc14.bold(suggested)} as your branch name?`);
|
|
3336
|
+
newBranchName = accepted ? suggested : await inputPrompt("Enter branch name", description);
|
|
3337
|
+
} else {
|
|
3338
|
+
spinner.fail("AI did not return a suggestion.");
|
|
3339
|
+
newBranchName = await inputPrompt("Enter branch name", description);
|
|
3340
|
+
}
|
|
3341
|
+
}
|
|
3342
|
+
}
|
|
3343
|
+
if (!hasPrefix(newBranchName, config.branchPrefixes)) {
|
|
3344
|
+
const prefix = await selectPrompt(`Choose a branch type for ${pc14.bold(newBranchName)}:`, config.branchPrefixes);
|
|
3345
|
+
newBranchName = formatBranchName(prefix, newBranchName);
|
|
3346
|
+
}
|
|
3347
|
+
if (!isValidBranchName(newBranchName)) {
|
|
3348
|
+
error("Invalid branch name. Use only alphanumeric characters, dots, hyphens, underscores, and slashes.");
|
|
3349
|
+
process.exit(1);
|
|
3350
|
+
}
|
|
3351
|
+
const branchResult = await createBranch(newBranchName);
|
|
3352
|
+
if (branchResult.exitCode !== 0) {
|
|
3353
|
+
error(`Failed to create branch: ${branchResult.stderr}`);
|
|
3354
|
+
process.exit(1);
|
|
3355
|
+
}
|
|
3356
|
+
success(`Created ${pc14.bold(newBranchName)} with your commits.`);
|
|
3357
|
+
const coResult2 = await checkoutBranch2(baseBranch);
|
|
3358
|
+
if (coResult2.exitCode !== 0) {
|
|
3359
|
+
error(`Failed to checkout ${baseBranch}: ${coResult2.stderr}`);
|
|
3360
|
+
process.exit(1);
|
|
3361
|
+
}
|
|
3362
|
+
const remoteRef = syncSource.ref;
|
|
3363
|
+
await updateLocalBranch(baseBranch, remoteRef);
|
|
3364
|
+
success(`Reset ${pc14.bold(baseBranch)} to ${pc14.bold(remoteRef)}.`);
|
|
3365
|
+
const pullResult2 = await pullBranch(syncSource.remote, baseBranch);
|
|
3366
|
+
if (pullResult2.exitCode !== 0) {
|
|
3367
|
+
error(`Failed to pull: ${pullResult2.stderr}`);
|
|
3368
|
+
process.exit(1);
|
|
3369
|
+
}
|
|
3370
|
+
success(`✅ ${pc14.bold(baseBranch)} is now in sync with ${syncSource.ref}`);
|
|
3371
|
+
console.log();
|
|
3372
|
+
info(`Your commits are safe on ${pc14.bold(newBranchName)}.`);
|
|
3373
|
+
info(`Run ${pc14.bold(`git checkout ${newBranchName}`)} then ${pc14.bold("contrib update")} to rebase onto the synced ${pc14.bold(baseBranch)}.`);
|
|
3374
|
+
return;
|
|
3375
|
+
}
|
|
3376
|
+
warn("Proceeding with pull — a merge commit may be created.");
|
|
3377
|
+
}
|
|
1454
3378
|
}
|
|
1455
3379
|
if (!args.yes) {
|
|
1456
|
-
const ok = await confirmPrompt(`This will pull ${
|
|
3380
|
+
const ok = await confirmPrompt(`This will pull ${pc14.bold(syncSource.ref)} into local ${pc14.bold(baseBranch)}.`);
|
|
1457
3381
|
if (!ok)
|
|
1458
3382
|
process.exit(0);
|
|
1459
3383
|
}
|
|
1460
|
-
const coResult = await
|
|
3384
|
+
const coResult = await checkoutBranch2(baseBranch);
|
|
1461
3385
|
if (coResult.exitCode !== 0) {
|
|
1462
3386
|
error(`Failed to checkout ${baseBranch}: ${coResult.stderr}`);
|
|
1463
3387
|
process.exit(1);
|
|
@@ -1471,25 +3395,25 @@ var sync_default = defineCommand8({
|
|
|
1471
3395
|
if (hasDevBranch(workflow) && role === "maintainer") {
|
|
1472
3396
|
const mainDiv = await getDivergence(config.mainBranch, `${origin}/${config.mainBranch}`);
|
|
1473
3397
|
if (mainDiv.behind > 0) {
|
|
1474
|
-
info(`Also syncing ${
|
|
1475
|
-
const mainCoResult = await
|
|
3398
|
+
info(`Also syncing ${pc14.bold(config.mainBranch)}...`);
|
|
3399
|
+
const mainCoResult = await checkoutBranch2(config.mainBranch);
|
|
1476
3400
|
if (mainCoResult.exitCode === 0) {
|
|
1477
3401
|
const mainPullResult = await pullBranch(origin, config.mainBranch);
|
|
1478
3402
|
if (mainPullResult.exitCode === 0) {
|
|
1479
3403
|
success(`✅ ${config.mainBranch} is now in sync with ${origin}/${config.mainBranch}`);
|
|
1480
3404
|
}
|
|
1481
3405
|
}
|
|
1482
|
-
await
|
|
3406
|
+
await checkoutBranch2(baseBranch);
|
|
1483
3407
|
}
|
|
1484
3408
|
}
|
|
1485
3409
|
}
|
|
1486
3410
|
});
|
|
1487
3411
|
|
|
1488
3412
|
// src/commands/update.ts
|
|
1489
|
-
import { readFileSync as
|
|
1490
|
-
import { defineCommand as
|
|
1491
|
-
import
|
|
1492
|
-
var update_default =
|
|
3413
|
+
import { readFileSync as readFileSync4 } from "node:fs";
|
|
3414
|
+
import { defineCommand as defineCommand12 } from "citty";
|
|
3415
|
+
import pc15 from "picocolors";
|
|
3416
|
+
var update_default = defineCommand12({
|
|
1493
3417
|
meta: {
|
|
1494
3418
|
name: "update",
|
|
1495
3419
|
description: "Rebase current branch onto the latest base branch"
|
|
@@ -1524,18 +3448,177 @@ var update_default = defineCommand9({
|
|
|
1524
3448
|
process.exit(1);
|
|
1525
3449
|
}
|
|
1526
3450
|
if (protectedBranches.includes(currentBranch)) {
|
|
1527
|
-
|
|
1528
|
-
|
|
3451
|
+
heading("\uD83D\uDD03 contrib update");
|
|
3452
|
+
warn(`You're on ${pc15.bold(currentBranch)}, which is a protected branch. Updates (rebase) apply to feature branches.`);
|
|
3453
|
+
await fetchAll();
|
|
3454
|
+
const { origin } = config;
|
|
3455
|
+
const remoteRef = `${origin}/${currentBranch}`;
|
|
3456
|
+
const localWork = await hasLocalWork(origin, currentBranch);
|
|
3457
|
+
const dirty = await hasUncommittedChanges();
|
|
3458
|
+
const hasCommits = localWork.unpushedCommits > 0;
|
|
3459
|
+
const hasAnything = hasCommits || dirty;
|
|
3460
|
+
if (!hasAnything) {
|
|
3461
|
+
info(`No local changes found on ${pc15.bold(currentBranch)}.`);
|
|
3462
|
+
info(`Use ${pc15.bold("contrib sync")} to sync protected branches, or ${pc15.bold("contrib start")} to create a feature branch.`);
|
|
3463
|
+
process.exit(1);
|
|
3464
|
+
}
|
|
3465
|
+
if (hasCommits) {
|
|
3466
|
+
info(`Found ${pc15.bold(String(localWork.unpushedCommits))} unpushed commit${localWork.unpushedCommits !== 1 ? "s" : ""} on ${pc15.bold(currentBranch)}.`);
|
|
3467
|
+
}
|
|
3468
|
+
if (dirty) {
|
|
3469
|
+
info("You also have uncommitted changes in the working tree.");
|
|
3470
|
+
}
|
|
3471
|
+
console.log();
|
|
3472
|
+
const MOVE_BRANCH = "Move my changes to a new feature branch";
|
|
3473
|
+
const CANCEL = "Cancel (stay on this branch)";
|
|
3474
|
+
const action = await selectPrompt("Let's get you back on track. What would you like to do?", [MOVE_BRANCH, CANCEL]);
|
|
3475
|
+
if (action === CANCEL) {
|
|
3476
|
+
info("No changes made. You are still on your current branch.");
|
|
3477
|
+
return;
|
|
3478
|
+
}
|
|
3479
|
+
info(pc15.dim("Tip: Describe what you're going to work on in plain English and we'll generate a branch name."));
|
|
3480
|
+
const description = await inputPrompt("What are you going to work on?");
|
|
3481
|
+
let newBranchName = description;
|
|
3482
|
+
if (!args["no-ai"] && looksLikeNaturalLanguage(description)) {
|
|
3483
|
+
const copilotError = await checkCopilotAvailable();
|
|
3484
|
+
if (!copilotError) {
|
|
3485
|
+
const spinner = createSpinner("Generating branch name suggestion...");
|
|
3486
|
+
const suggested = await suggestBranchName(description, args.model);
|
|
3487
|
+
if (suggested) {
|
|
3488
|
+
spinner.success("Branch name suggestion ready.");
|
|
3489
|
+
console.log(`
|
|
3490
|
+
${pc15.dim("AI suggestion:")} ${pc15.bold(pc15.cyan(suggested))}`);
|
|
3491
|
+
const accepted = await confirmPrompt(`Use ${pc15.bold(suggested)} as your branch name?`);
|
|
3492
|
+
newBranchName = accepted ? suggested : await inputPrompt("Enter branch name", description);
|
|
3493
|
+
} else {
|
|
3494
|
+
spinner.fail("AI did not return a suggestion.");
|
|
3495
|
+
newBranchName = await inputPrompt("Enter branch name", description);
|
|
3496
|
+
}
|
|
3497
|
+
}
|
|
3498
|
+
}
|
|
3499
|
+
if (!hasPrefix(newBranchName, config.branchPrefixes)) {
|
|
3500
|
+
const prefix = await selectPrompt(`Choose a branch type for ${pc15.bold(newBranchName)}:`, config.branchPrefixes);
|
|
3501
|
+
newBranchName = formatBranchName(prefix, newBranchName);
|
|
3502
|
+
}
|
|
3503
|
+
if (!isValidBranchName(newBranchName)) {
|
|
3504
|
+
error("Invalid branch name. Use only alphanumeric characters, dots, hyphens, underscores, and slashes.");
|
|
3505
|
+
process.exit(1);
|
|
3506
|
+
}
|
|
3507
|
+
const branchResult = await createBranch(newBranchName);
|
|
3508
|
+
if (branchResult.exitCode !== 0) {
|
|
3509
|
+
error(`Failed to create branch: ${branchResult.stderr}`);
|
|
3510
|
+
process.exit(1);
|
|
3511
|
+
}
|
|
3512
|
+
success(`Created ${pc15.bold(newBranchName)} with your changes.`);
|
|
3513
|
+
await updateLocalBranch(currentBranch, remoteRef);
|
|
3514
|
+
info(`Reset ${pc15.bold(currentBranch)} back to ${pc15.bold(remoteRef)} — no damage done.`);
|
|
3515
|
+
console.log();
|
|
3516
|
+
success(`You're now on ${pc15.bold(newBranchName)} with all your work intact.`);
|
|
3517
|
+
info(`Run ${pc15.bold("contrib update")} again to rebase onto latest ${pc15.bold(baseBranch)}.`);
|
|
3518
|
+
return;
|
|
1529
3519
|
}
|
|
1530
3520
|
if (await hasUncommittedChanges()) {
|
|
1531
3521
|
error("You have uncommitted changes. Please commit or stash them first.");
|
|
1532
3522
|
process.exit(1);
|
|
1533
3523
|
}
|
|
1534
3524
|
heading("\uD83D\uDD03 contrib update");
|
|
1535
|
-
|
|
3525
|
+
const mergedPR = await getMergedPRForBranch(currentBranch);
|
|
3526
|
+
if (mergedPR) {
|
|
3527
|
+
warn(`PR #${mergedPR.number} (${pc15.bold(mergedPR.title)}) has already been merged.`);
|
|
3528
|
+
info(`Link: ${pc15.underline(mergedPR.url)}`);
|
|
3529
|
+
const localWork = await hasLocalWork(syncSource.remote, currentBranch);
|
|
3530
|
+
const hasWork = localWork.uncommitted || localWork.unpushedCommits > 0;
|
|
3531
|
+
if (hasWork) {
|
|
3532
|
+
if (localWork.uncommitted) {
|
|
3533
|
+
info("You have uncommitted local changes.");
|
|
3534
|
+
}
|
|
3535
|
+
if (localWork.unpushedCommits > 0) {
|
|
3536
|
+
info(`You have ${localWork.unpushedCommits} unpushed commit(s).`);
|
|
3537
|
+
}
|
|
3538
|
+
const SAVE_NEW_BRANCH = "Save changes to a new branch";
|
|
3539
|
+
const DISCARD = "Discard all changes and clean up";
|
|
3540
|
+
const CANCEL = "Cancel";
|
|
3541
|
+
const action = await selectPrompt(`${pc15.bold(currentBranch)} is stale but has local work. What would you like to do?`, [SAVE_NEW_BRANCH, DISCARD, CANCEL]);
|
|
3542
|
+
if (action === CANCEL) {
|
|
3543
|
+
info("No changes made. You are still on your current branch.");
|
|
3544
|
+
return;
|
|
3545
|
+
}
|
|
3546
|
+
if (action === SAVE_NEW_BRANCH) {
|
|
3547
|
+
info(pc15.dim("Tip: Describe what you're going to work on in plain English and we'll generate a branch name."));
|
|
3548
|
+
const description = await inputPrompt("What are you going to work on?");
|
|
3549
|
+
let newBranchName = description;
|
|
3550
|
+
if (!args["no-ai"] && looksLikeNaturalLanguage(description)) {
|
|
3551
|
+
const spinner = createSpinner("Generating branch name suggestion...");
|
|
3552
|
+
const suggested = await suggestBranchName(description, args.model);
|
|
3553
|
+
if (suggested) {
|
|
3554
|
+
spinner.success("Branch name suggestion ready.");
|
|
3555
|
+
console.log(`
|
|
3556
|
+
${pc15.dim("AI suggestion:")} ${pc15.bold(pc15.cyan(suggested))}`);
|
|
3557
|
+
const accepted = await confirmPrompt(`Use ${pc15.bold(suggested)} as your branch name?`);
|
|
3558
|
+
newBranchName = accepted ? suggested : await inputPrompt("Enter branch name", description);
|
|
3559
|
+
} else {
|
|
3560
|
+
spinner.fail("AI did not return a suggestion.");
|
|
3561
|
+
newBranchName = await inputPrompt("Enter branch name", description);
|
|
3562
|
+
}
|
|
3563
|
+
}
|
|
3564
|
+
if (!hasPrefix(newBranchName, config.branchPrefixes)) {
|
|
3565
|
+
const prefix = await selectPrompt(`Choose a branch type for ${pc15.bold(newBranchName)}:`, config.branchPrefixes);
|
|
3566
|
+
newBranchName = formatBranchName(prefix, newBranchName);
|
|
3567
|
+
}
|
|
3568
|
+
if (!isValidBranchName(newBranchName)) {
|
|
3569
|
+
error("Invalid branch name. Use only alphanumeric characters, dots, hyphens, underscores, and slashes.");
|
|
3570
|
+
process.exit(1);
|
|
3571
|
+
}
|
|
3572
|
+
const staleUpstream = await getUpstreamRef();
|
|
3573
|
+
const staleUpstreamHash = staleUpstream ? await getCommitHash(staleUpstream) : null;
|
|
3574
|
+
const renameResult = await renameBranch(currentBranch, newBranchName);
|
|
3575
|
+
if (renameResult.exitCode !== 0) {
|
|
3576
|
+
error(`Failed to rename branch: ${renameResult.stderr}`);
|
|
3577
|
+
process.exit(1);
|
|
3578
|
+
}
|
|
3579
|
+
success(`Renamed ${pc15.bold(currentBranch)} → ${pc15.bold(newBranchName)}`);
|
|
3580
|
+
await unsetUpstream();
|
|
3581
|
+
await fetchRemote(syncSource.remote);
|
|
3582
|
+
let rebaseResult2;
|
|
3583
|
+
if (staleUpstreamHash) {
|
|
3584
|
+
rebaseResult2 = await rebaseOnto(syncSource.ref, staleUpstreamHash);
|
|
3585
|
+
} else {
|
|
3586
|
+
const savedStrategy = await determineRebaseStrategy(newBranchName, syncSource.ref);
|
|
3587
|
+
rebaseResult2 = savedStrategy.strategy === "onto" && savedStrategy.ontoOldBase ? await rebaseOnto(syncSource.ref, savedStrategy.ontoOldBase) : await rebase(syncSource.ref);
|
|
3588
|
+
}
|
|
3589
|
+
if (rebaseResult2.exitCode !== 0) {
|
|
3590
|
+
warn("Rebase encountered conflicts. Resolve them manually, then run:");
|
|
3591
|
+
info(` ${pc15.bold("git rebase --continue")}`);
|
|
3592
|
+
} else {
|
|
3593
|
+
success(`Rebased ${pc15.bold(newBranchName)} onto ${pc15.bold(syncSource.ref)}.`);
|
|
3594
|
+
}
|
|
3595
|
+
info(`All your changes are preserved. Run ${pc15.bold("contrib submit")} when ready to create a new PR.`);
|
|
3596
|
+
return;
|
|
3597
|
+
}
|
|
3598
|
+
warn("Discarding local changes...");
|
|
3599
|
+
}
|
|
3600
|
+
await fetchRemote(syncSource.remote);
|
|
3601
|
+
const coResult = await checkoutBranch2(baseBranch);
|
|
3602
|
+
if (coResult.exitCode !== 0) {
|
|
3603
|
+
error(`Failed to checkout ${baseBranch}: ${coResult.stderr}`);
|
|
3604
|
+
process.exit(1);
|
|
3605
|
+
}
|
|
3606
|
+
await updateLocalBranch(baseBranch, syncSource.ref);
|
|
3607
|
+
success(`Synced ${pc15.bold(baseBranch)} with ${pc15.bold(syncSource.ref)}.`);
|
|
3608
|
+
info(`Deleting stale branch ${pc15.bold(currentBranch)}...`);
|
|
3609
|
+
await forceDeleteBranch(currentBranch);
|
|
3610
|
+
success(`Deleted ${pc15.bold(currentBranch)}.`);
|
|
3611
|
+
info(`Run ${pc15.bold("contrib start")} to begin a new feature branch.`);
|
|
3612
|
+
return;
|
|
3613
|
+
}
|
|
3614
|
+
info(`Updating ${pc15.bold(currentBranch)} with latest ${pc15.bold(baseBranch)}...`);
|
|
1536
3615
|
await fetchRemote(syncSource.remote);
|
|
1537
|
-
await
|
|
1538
|
-
const
|
|
3616
|
+
await updateLocalBranch(baseBranch, syncSource.ref);
|
|
3617
|
+
const rebaseStrategy = await determineRebaseStrategy(currentBranch, syncSource.ref);
|
|
3618
|
+
if (rebaseStrategy.strategy === "onto" && rebaseStrategy.ontoOldBase) {
|
|
3619
|
+
info(pc15.dim(`Using --onto rebase (branch was based on a different ref)`));
|
|
3620
|
+
}
|
|
3621
|
+
const rebaseResult = rebaseStrategy.strategy === "onto" && rebaseStrategy.ontoOldBase ? await rebaseOnto(syncSource.ref, rebaseStrategy.ontoOldBase) : await rebase(syncSource.ref);
|
|
1539
3622
|
if (rebaseResult.exitCode !== 0) {
|
|
1540
3623
|
warn("Rebase hit conflicts. Resolve them manually.");
|
|
1541
3624
|
console.log();
|
|
@@ -1547,7 +3630,7 @@ var update_default = defineCommand9({
|
|
|
1547
3630
|
let conflictDiff = "";
|
|
1548
3631
|
for (const file of conflictFiles.slice(0, 3)) {
|
|
1549
3632
|
try {
|
|
1550
|
-
const content =
|
|
3633
|
+
const content = readFileSync4(file, "utf-8");
|
|
1551
3634
|
if (content.includes("<<<<<<<")) {
|
|
1552
3635
|
conflictDiff += `
|
|
1553
3636
|
--- ${file} ---
|
|
@@ -1557,34 +3640,38 @@ ${content.slice(0, 2000)}
|
|
|
1557
3640
|
} catch {}
|
|
1558
3641
|
}
|
|
1559
3642
|
if (conflictDiff) {
|
|
3643
|
+
const spinner = createSpinner("Analyzing conflicts with AI...");
|
|
1560
3644
|
const suggestion = await suggestConflictResolution(conflictDiff, args.model);
|
|
1561
3645
|
if (suggestion) {
|
|
3646
|
+
spinner.success("AI conflict guidance ready.");
|
|
1562
3647
|
console.log(`
|
|
1563
|
-
${
|
|
1564
|
-
console.log(
|
|
3648
|
+
${pc15.bold("\uD83D\uDCA1 AI Conflict Resolution Guidance:")}`);
|
|
3649
|
+
console.log(pc15.dim("─".repeat(60)));
|
|
1565
3650
|
console.log(suggestion);
|
|
1566
|
-
console.log(
|
|
3651
|
+
console.log(pc15.dim("─".repeat(60)));
|
|
1567
3652
|
console.log();
|
|
3653
|
+
} else {
|
|
3654
|
+
spinner.fail("AI could not analyze the conflicts.");
|
|
1568
3655
|
}
|
|
1569
3656
|
}
|
|
1570
3657
|
}
|
|
1571
3658
|
}
|
|
1572
|
-
console.log(
|
|
3659
|
+
console.log(pc15.bold("To resolve:"));
|
|
1573
3660
|
console.log(` 1. Fix conflicts in the affected files`);
|
|
1574
|
-
console.log(` 2. ${
|
|
1575
|
-
console.log(` 3. ${
|
|
3661
|
+
console.log(` 2. ${pc15.cyan("git add <resolved-files>")}`);
|
|
3662
|
+
console.log(` 3. ${pc15.cyan("git rebase --continue")}`);
|
|
1576
3663
|
console.log();
|
|
1577
|
-
console.log(` Or abort: ${
|
|
3664
|
+
console.log(` Or abort: ${pc15.cyan("git rebase --abort")}`);
|
|
1578
3665
|
process.exit(1);
|
|
1579
3666
|
}
|
|
1580
|
-
success(`✅ ${
|
|
3667
|
+
success(`✅ ${pc15.bold(currentBranch)} has been rebased onto latest ${pc15.bold(baseBranch)}`);
|
|
1581
3668
|
}
|
|
1582
3669
|
});
|
|
1583
3670
|
|
|
1584
3671
|
// src/commands/validate.ts
|
|
1585
|
-
import { defineCommand as
|
|
1586
|
-
import
|
|
1587
|
-
var validate_default =
|
|
3672
|
+
import { defineCommand as defineCommand13 } from "citty";
|
|
3673
|
+
import pc16 from "picocolors";
|
|
3674
|
+
var validate_default = defineCommand13({
|
|
1588
3675
|
meta: {
|
|
1589
3676
|
name: "validate",
|
|
1590
3677
|
description: "Validate a commit message against the configured convention"
|
|
@@ -1614,7 +3701,7 @@ var validate_default = defineCommand10({
|
|
|
1614
3701
|
}
|
|
1615
3702
|
const errors = getValidationError(convention);
|
|
1616
3703
|
for (const line of errors) {
|
|
1617
|
-
console.error(
|
|
3704
|
+
console.error(pc16.red(` ✗ ${line}`));
|
|
1618
3705
|
}
|
|
1619
3706
|
process.exit(1);
|
|
1620
3707
|
}
|
|
@@ -1622,75 +3709,19 @@ var validate_default = defineCommand10({
|
|
|
1622
3709
|
|
|
1623
3710
|
// src/ui/banner.ts
|
|
1624
3711
|
import figlet from "figlet";
|
|
1625
|
-
import
|
|
1626
|
-
|
|
1627
|
-
var package_default = {
|
|
1628
|
-
name: "contribute-now",
|
|
1629
|
-
version: "0.1.2-staging.c209cc7",
|
|
1630
|
-
description: "Git workflow CLI for squash-merge two-branch models. Keeps dev in sync with main after squash merges.",
|
|
1631
|
-
type: "module",
|
|
1632
|
-
bin: {
|
|
1633
|
-
contrib: "dist/index.js",
|
|
1634
|
-
contribute: "dist/index.js"
|
|
1635
|
-
},
|
|
1636
|
-
files: [
|
|
1637
|
-
"dist"
|
|
1638
|
-
],
|
|
1639
|
-
scripts: {
|
|
1640
|
-
build: "bun build src/index.ts --outfile dist/index.js --target node --packages external",
|
|
1641
|
-
dev: "bun src/index.ts",
|
|
1642
|
-
test: "bun test",
|
|
1643
|
-
lint: "biome check .",
|
|
1644
|
-
"lint:fix": "biome check --write .",
|
|
1645
|
-
format: "biome format --write .",
|
|
1646
|
-
prepare: "husky || true",
|
|
1647
|
-
"www:dev": "bun run --cwd www dev",
|
|
1648
|
-
"www:build": "bun run --cwd www build",
|
|
1649
|
-
"www:preview": "bun run --cwd www preview"
|
|
1650
|
-
},
|
|
1651
|
-
engines: {
|
|
1652
|
-
node: ">=18",
|
|
1653
|
-
bun: ">=1.0"
|
|
1654
|
-
},
|
|
1655
|
-
keywords: [
|
|
1656
|
-
"git",
|
|
1657
|
-
"workflow",
|
|
1658
|
-
"squash-merge",
|
|
1659
|
-
"sync",
|
|
1660
|
-
"cli",
|
|
1661
|
-
"contribute",
|
|
1662
|
-
"fork",
|
|
1663
|
-
"dev-branch",
|
|
1664
|
-
"clean-commit"
|
|
1665
|
-
],
|
|
1666
|
-
author: "Waren Gonzaga",
|
|
1667
|
-
license: "GPL-3.0",
|
|
1668
|
-
repository: {
|
|
1669
|
-
type: "git",
|
|
1670
|
-
url: "git+https://github.com/warengonzaga/contribute-now.git"
|
|
1671
|
-
},
|
|
1672
|
-
dependencies: {
|
|
1673
|
-
"@github/copilot-sdk": "^0.1.25",
|
|
1674
|
-
"@wgtechlabs/log-engine": "^2.3.1",
|
|
1675
|
-
citty: "^0.1.6",
|
|
1676
|
-
figlet: "^1.10.0",
|
|
1677
|
-
picocolors: "^1.1.1"
|
|
1678
|
-
},
|
|
1679
|
-
devDependencies: {
|
|
1680
|
-
"@biomejs/biome": "^2.4.4",
|
|
1681
|
-
"@types/bun": "latest",
|
|
1682
|
-
"@types/figlet": "^1.7.0",
|
|
1683
|
-
husky: "^9.1.7",
|
|
1684
|
-
typescript: "^5.7.0"
|
|
1685
|
-
}
|
|
1686
|
-
};
|
|
1687
|
-
|
|
1688
|
-
// src/ui/banner.ts
|
|
1689
|
-
var LOGO;
|
|
3712
|
+
import pc17 from "picocolors";
|
|
3713
|
+
var LOGO_BIG;
|
|
1690
3714
|
try {
|
|
1691
|
-
|
|
3715
|
+
LOGO_BIG = figlet.textSync(`Contribute
|
|
3716
|
+
Now`, { font: "ANSI Shadow" });
|
|
1692
3717
|
} catch {
|
|
1693
|
-
|
|
3718
|
+
LOGO_BIG = "Contribute Now";
|
|
3719
|
+
}
|
|
3720
|
+
var LOGO_SMALL;
|
|
3721
|
+
try {
|
|
3722
|
+
LOGO_SMALL = figlet.textSync("Contribute Now", { font: "Slant" });
|
|
3723
|
+
} catch {
|
|
3724
|
+
LOGO_SMALL = "Contribute Now";
|
|
1694
3725
|
}
|
|
1695
3726
|
function getVersion() {
|
|
1696
3727
|
return package_default.version ?? "unknown";
|
|
@@ -1698,24 +3729,30 @@ function getVersion() {
|
|
|
1698
3729
|
function getAuthor() {
|
|
1699
3730
|
return typeof package_default.author === "string" ? package_default.author : "unknown";
|
|
1700
3731
|
}
|
|
1701
|
-
function showBanner(
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
3732
|
+
function showBanner(variant = "small") {
|
|
3733
|
+
const logo = variant === "big" ? LOGO_BIG : LOGO_SMALL;
|
|
3734
|
+
console.log(pc17.cyan(`
|
|
3735
|
+
${logo}`));
|
|
3736
|
+
console.log(` ${pc17.dim(`v${getVersion()}`)} ${pc17.dim("—")} ${pc17.dim(`Built by ${getAuthor()}`)}`);
|
|
3737
|
+
if (variant === "big") {
|
|
1707
3738
|
console.log();
|
|
1708
|
-
console.log(` ${
|
|
1709
|
-
console.log(` ${
|
|
1710
|
-
console.log(` ${
|
|
3739
|
+
console.log(` ${pc17.yellow("Star")} ${pc17.cyan("https://github.com/warengonzaga/contribute-now")}`);
|
|
3740
|
+
console.log(` ${pc17.green("Contribute")} ${pc17.cyan("https://github.com/warengonzaga/contribute-now/blob/main/CONTRIBUTING.md")}`);
|
|
3741
|
+
console.log(` ${pc17.magenta("Sponsor")} ${pc17.cyan("https://warengonzaga.com/sponsor")}`);
|
|
1711
3742
|
}
|
|
1712
3743
|
console.log();
|
|
1713
3744
|
}
|
|
1714
3745
|
|
|
1715
3746
|
// src/index.ts
|
|
1716
|
-
var
|
|
1717
|
-
|
|
1718
|
-
|
|
3747
|
+
var isVersion = process.argv.includes("--version") || process.argv.includes("-v");
|
|
3748
|
+
if (!isVersion) {
|
|
3749
|
+
const subCommands = ["setup", "sync", "start", "commit", "update", "submit", "clean", "status", "log", "branch", "hook", "validate", "doctor"];
|
|
3750
|
+
const isHelp = process.argv.includes("--help") || process.argv.includes("-h");
|
|
3751
|
+
const hasSubCommand = subCommands.some((cmd) => process.argv.includes(cmd));
|
|
3752
|
+
const useBigBanner = isHelp || !hasSubCommand;
|
|
3753
|
+
showBanner(useBigBanner ? "big" : "small");
|
|
3754
|
+
}
|
|
3755
|
+
var main = defineCommand14({
|
|
1719
3756
|
meta: {
|
|
1720
3757
|
name: "contrib",
|
|
1721
3758
|
version: getVersion(),
|
|
@@ -1735,10 +3772,13 @@ var main = defineCommand11({
|
|
|
1735
3772
|
commit: commit_default,
|
|
1736
3773
|
update: update_default,
|
|
1737
3774
|
submit: submit_default,
|
|
3775
|
+
branch: branch_default,
|
|
1738
3776
|
clean: clean_default,
|
|
1739
3777
|
status: status_default,
|
|
3778
|
+
log: log_default,
|
|
1740
3779
|
hook: hook_default,
|
|
1741
|
-
validate: validate_default
|
|
3780
|
+
validate: validate_default,
|
|
3781
|
+
doctor: doctor_default
|
|
1742
3782
|
},
|
|
1743
3783
|
run({ args }) {
|
|
1744
3784
|
if (args.version) {
|
|
@@ -1746,4 +3786,6 @@ var main = defineCommand11({
|
|
|
1746
3786
|
}
|
|
1747
3787
|
}
|
|
1748
3788
|
});
|
|
1749
|
-
runMain(main)
|
|
3789
|
+
runMain(main).then(() => {
|
|
3790
|
+
process.exit(0);
|
|
3791
|
+
});
|