-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathGitActionsControl.logic.ts
More file actions
504 lines (453 loc) · 13.6 KB
/
Copy pathGitActionsControl.logic.ts
File metadata and controls
504 lines (453 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
import type {
GitActionFailure,
GitRunStackedActionResult,
GitStackedAction,
GitStatusResult,
} from "@okcode/contracts";
export type GitActionIconName = "commit" | "push" | "pr";
export type GitDialogAction = "commit" | "push" | "create_pr";
export interface GitActionMenuItem {
id: "commit" | "push" | "pr";
label: string;
disabled: boolean;
icon: GitActionIconName;
kind: "open_dialog" | "open_pr";
dialogAction?: GitDialogAction;
}
export type GitPullRequestMenuItemId = "open_in_browser" | "copy_pr_number" | "copy_pr_link";
export interface GitPullRequestMenuItem {
id: GitPullRequestMenuItemId;
label: string;
}
export interface GitQuickAction {
label: string;
disabled: boolean;
kind: "run_action" | "run_pull" | "open_pr" | "show_hint" | "resolve_conflicts";
action?: GitStackedAction;
hint?: string;
}
export interface GitSyncAction {
label: string;
disabled: boolean;
kind: "run_action" | "run_pull" | "show_hint";
action?: "commit_push";
hint?: string;
}
export interface DefaultBranchActionDialogCopy {
title: string;
description: string;
continueLabel: string;
}
export type DefaultBranchConfirmableAction = "commit_push" | "commit_push_pr";
const SHORT_SHA_LENGTH = 7;
const TOAST_DESCRIPTION_MAX = 72;
function shortenSha(sha: string | undefined): string | null {
if (!sha) return null;
return sha.slice(0, SHORT_SHA_LENGTH);
}
function truncateText(
value: string | undefined,
maxLength = TOAST_DESCRIPTION_MAX,
): string | undefined {
if (!value) return undefined;
if (value.length <= maxLength) return value;
if (maxLength <= 3) return "...".slice(0, maxLength);
return `${value.slice(0, Math.max(0, maxLength - 3)).trimEnd()}...`;
}
export function formatOpenPullRequestLabel(prNumber: number | undefined): string {
return prNumber ? `View PR #${prNumber}` : "View PR";
}
export function buildGitActionProgressStages(input: {
action: GitStackedAction;
hasCustomCommitMessage: boolean;
hasWorkingTreeChanges: boolean;
forcePushOnly?: boolean;
pushTarget?: string;
featureBranch?: boolean;
}): string[] {
const branchStages = input.featureBranch ? ["Preparing feature branch..."] : [];
const shouldIncludeCommitStages =
!input.forcePushOnly && (input.action === "commit" || input.hasWorkingTreeChanges);
const commitStages = !shouldIncludeCommitStages
? []
: input.hasCustomCommitMessage
? ["Committing..."]
: ["Generating commit message...", "Committing..."];
const pushStage = input.pushTarget ? `Pushing to ${input.pushTarget}...` : "Pushing...";
if (input.action === "commit") {
return [...branchStages, ...commitStages];
}
if (input.action === "commit_push") {
return [...branchStages, ...commitStages, pushStage];
}
return [...branchStages, ...commitStages, pushStage, "Creating PR..."];
}
const withDescription = (title: string, description: string | undefined) =>
description ? { title, description } : { title };
export function buildPullRequestMenuItems(
gitStatus: GitStatusResult | null,
): GitPullRequestMenuItem[] {
if (gitStatus?.pr?.state !== "open") return [];
return [
{ id: "open_in_browser", label: "Open in browser" },
{ id: "copy_pr_number", label: "Copy PR number" },
{ id: "copy_pr_link", label: "Copy PR link" },
];
}
export function summarizeGitResult(result: GitRunStackedActionResult): {
title: string;
description?: string;
} {
if (result.pr.status === "created" || result.pr.status === "opened_existing") {
const prNumber = result.pr.number ? ` #${result.pr.number}` : "";
const title = `${result.pr.status === "created" ? "Created PR" : "Opened PR"}${prNumber}`;
return withDescription(title, truncateText(result.pr.title));
}
if (result.push.status === "pushed") {
const shortSha = shortenSha(result.commit.commitSha);
const branch = result.push.upstreamBranch ?? result.push.branch;
const pushedCommitPart = shortSha ? ` ${shortSha}` : "";
const branchPart = branch ? ` to ${branch}` : "";
return withDescription(
`Pushed${pushedCommitPart}${branchPart}`,
truncateText(result.commit.subject),
);
}
if (result.commit.status === "created") {
const shortSha = shortenSha(result.commit.commitSha);
const title = shortSha ? `Committed ${shortSha}` : "Committed changes";
return withDescription(title, truncateText(result.commit.subject));
}
return { title: "Done" };
}
export function summarizeGitFailure(failure: GitActionFailure): {
title: string;
description?: string;
} {
return withDescription(failure.title, truncateText(failure.summary));
}
export function resolveGitFailureRetryLabel(failure: GitActionFailure): string {
if (failure.phase === "pr") {
return "Retry PR";
}
if (failure.phase === "push") {
return "Retry push";
}
if (failure.phase === "commit") {
return "Retry commit";
}
return "Retry";
}
export function buildMenuItems(
gitStatus: GitStatusResult | null,
isBusy: boolean,
hasOriginRemote = true,
): GitActionMenuItem[] {
if (!gitStatus) return [];
const hasBranch = gitStatus.branch !== null;
const hasChanges = gitStatus.hasWorkingTreeChanges;
const hasConflicts = gitStatus.hasConflicts;
const hasOpenPr = gitStatus.pr?.state === "open";
const isBehind = gitStatus.behindCount > 0;
const canPushWithoutUpstream = hasOriginRemote && !gitStatus.hasUpstream;
const canCommit = !isBusy && hasChanges && !hasConflicts;
const canPush =
!isBusy &&
hasBranch &&
!hasChanges &&
!hasConflicts &&
!isBehind &&
gitStatus.aheadCount > 0 &&
(gitStatus.hasUpstream || canPushWithoutUpstream);
const canCreatePr =
!isBusy &&
hasBranch &&
!hasChanges &&
!hasConflicts &&
!hasOpenPr &&
gitStatus.aheadCount > 0 &&
!isBehind &&
(gitStatus.hasUpstream || canPushWithoutUpstream);
const canOpenPr = !isBusy && hasOpenPr;
return [
{
id: "commit",
label: "Commit",
disabled: !canCommit,
icon: "commit",
kind: "open_dialog",
dialogAction: "commit",
},
{
id: "push",
label: "Push",
disabled: !canPush,
icon: "push",
kind: "open_dialog",
dialogAction: "push",
},
hasOpenPr
? {
id: "pr",
label: formatOpenPullRequestLabel(gitStatus.pr?.number),
disabled: !canOpenPr,
icon: "pr",
kind: "open_pr",
}
: {
id: "pr",
label: "Create PR",
disabled: !canCreatePr,
icon: "pr",
kind: "open_dialog",
dialogAction: "create_pr",
},
];
}
export function resolveQuickAction(
gitStatus: GitStatusResult | null,
isBusy: boolean,
isDefaultBranch = false,
hasOriginRemote = true,
): GitQuickAction {
if (isBusy) {
return { label: "Commit", disabled: true, kind: "show_hint", hint: "Git action in progress." };
}
if (!gitStatus) {
return {
label: "Commit",
disabled: true,
kind: "show_hint",
hint: "Git status is unavailable.",
};
}
const hasBranch = gitStatus.branch !== null;
const hasChanges = gitStatus.hasWorkingTreeChanges;
const hasOpenPr = gitStatus.pr?.state === "open";
const isAhead = gitStatus.aheadCount > 0;
const isBehind = gitStatus.behindCount > 0;
const isDiverged = isAhead && isBehind;
const hasConflicts = gitStatus.hasConflicts;
if (!hasBranch) {
return {
label: "Commit",
disabled: true,
kind: "show_hint",
hint: "Create and checkout a branch before pushing or opening a PR.",
};
}
if (hasConflicts) {
return {
label: "Resolve conflicts",
disabled: false,
kind: "resolve_conflicts",
hint:
gitStatus.conflictedFiles.length > 0
? `Open ${gitStatus.conflictedFiles.length === 1 ? "the conflicted file" : `${gitStatus.conflictedFiles.length} conflicted files`} in your editor.`
: "Resolve merge conflicts before committing, pulling, or pushing.",
};
}
if (hasChanges) {
if (!gitStatus.hasUpstream && !hasOriginRemote) {
return { label: "Commit", disabled: false, kind: "run_action", action: "commit" };
}
if (hasOpenPr || isDefaultBranch) {
return { label: "Commit & push", disabled: false, kind: "run_action", action: "commit_push" };
}
return {
label: "Commit, push & PR",
disabled: false,
kind: "run_action",
action: "commit_push_pr",
};
}
if (!gitStatus.hasUpstream) {
if (!hasOriginRemote) {
if (hasOpenPr && !isAhead) {
return {
label: formatOpenPullRequestLabel(gitStatus.pr?.number),
disabled: false,
kind: "open_pr",
};
}
return {
label: "Push",
disabled: true,
kind: "show_hint",
hint: 'Add an "origin" remote before pushing or creating a PR.',
};
}
if (!isAhead) {
if (hasOpenPr) {
return {
label: formatOpenPullRequestLabel(gitStatus.pr?.number),
disabled: false,
kind: "open_pr",
};
}
return {
label: "Push",
disabled: true,
kind: "show_hint",
hint: "No local commits to push.",
};
}
if (hasOpenPr || isDefaultBranch) {
return { label: "Push", disabled: false, kind: "run_action", action: "commit_push" };
}
return {
label: "Push & create PR",
disabled: false,
kind: "run_action",
action: "commit_push_pr",
};
}
if (isDiverged) {
return {
label: "Sync branch",
disabled: true,
kind: "show_hint",
hint: "Branch has diverged from upstream. Rebase/merge first.",
};
}
if (isBehind) {
return {
label: "Pull",
disabled: false,
kind: "run_pull",
};
}
if (isAhead) {
if (hasOpenPr || isDefaultBranch) {
return { label: "Push", disabled: false, kind: "run_action", action: "commit_push" };
}
return {
label: "Push & create PR",
disabled: false,
kind: "run_action",
action: "commit_push_pr",
};
}
if (hasOpenPr && gitStatus.hasUpstream) {
return {
label: formatOpenPullRequestLabel(gitStatus.pr?.number),
disabled: false,
kind: "open_pr",
};
}
return {
label: "Commit",
disabled: true,
kind: "show_hint",
hint: "Branch is up to date. No action needed.",
};
}
export function resolveSyncAction(
gitStatus: GitStatusResult | null,
isBusy: boolean,
): GitSyncAction | null {
if (!gitStatus) return null;
if (
gitStatus.branch === null ||
gitStatus.hasWorkingTreeChanges ||
gitStatus.hasConflicts ||
!gitStatus.hasUpstream
) {
return null;
}
const isAhead = gitStatus.aheadCount > 0;
const isBehind = gitStatus.behindCount > 0;
if (!isAhead && !isBehind) {
return null;
}
if (isBusy) {
return {
label: "Sync branch",
disabled: true,
kind: "show_hint",
hint: "Git action in progress.",
};
}
if (isAhead && isBehind) {
return {
label: "Sync branch",
disabled: true,
kind: "show_hint",
hint: "Branch has diverged from upstream. Rebase/merge first.",
};
}
if (isBehind) {
return {
label: "Sync branch",
disabled: false,
kind: "run_pull",
};
}
return {
label: "Sync branch",
disabled: false,
kind: "run_action",
action: "commit_push",
};
}
export function requiresDefaultBranchConfirmation(
action: GitStackedAction,
isDefaultBranch: boolean,
): boolean {
if (!isDefaultBranch) return false;
return action === "commit_push" || action === "commit_push_pr";
}
export function resolveDefaultBranchActionDialogCopy(input: {
action: DefaultBranchConfirmableAction;
branchName: string;
includesCommit: boolean;
}): DefaultBranchActionDialogCopy {
const branchLabel = input.branchName;
const suffix = ` on "${branchLabel}". You can continue on this branch or create a feature branch and run the same action there.`;
if (input.action === "commit_push") {
if (input.includesCommit) {
return {
title: "Commit & push to default branch?",
description: `This action will commit and push changes${suffix}`,
continueLabel: `Commit & push to ${branchLabel}`,
};
}
return {
title: "Push to default branch?",
description: `This action will push local commits${suffix}`,
continueLabel: `Push to ${branchLabel}`,
};
}
if (input.includesCommit) {
return {
title: "Commit, push & create PR from default branch?",
description: `This action will commit, push, and create a PR${suffix}`,
continueLabel: `Commit, push & create PR`,
};
}
return {
title: "Push & create PR from default branch?",
description: `This action will push local commits and create a PR${suffix}`,
continueLabel: "Push & create PR",
};
}
export function buildHookFailureAgentPrompt(failure: GitActionFailure): string {
const sections: string[] = [
"A git commit hook failed and blocked my commit. Please analyze the errors and fix them so I can retry.",
];
if (failure.command) {
sections.push(`**Command that failed:**\n\`\`\`\n${failure.command}\n\`\`\``);
}
if (failure.detail) {
sections.push(`**Error summary:**\n${failure.detail}`);
}
if (failure.rawMessage) {
sections.push(`**Full hook output:**\n\`\`\`\n${failure.rawMessage}\n\`\`\``);
}
sections.push(
"Please:\n1. Analyze each error in the hook output\n2. Fix the affected files\n3. Let me know when the fixes are ready so I can retry the commit",
);
return sections.join("\n\n");
}
// Re-export from shared for backwards compatibility in this module's exports
export { resolveAutoFeatureBranchName } from "@okcode/shared/git";