Open History to see the last commit
1import type {
2 GitRunStackedActionResult,
3 GitStackedAction,
4 VcsStatusResult,
5} from "@t3tools/contracts";
6import { isTemporaryWorktreeBranch } from "@t3tools/shared/git";
7import {
8 DEFAULT_CHANGE_REQUEST_TERMINOLOGY,
9 getChangeRequestTerminology,
10 type ChangeRequestTerminology,
11} from "../sourceControlPresentation";
12
13export type GitActionIconName = "commit" | "push" | "pr";
14
15export type GitDialogAction = "commit" | "push" | "create_pr";
16
17export interface GitActionMenuItem {
18 id: "commit" | "push" | "pr";
19 label: string;
20 disabled: boolean;
21 icon: GitActionIconName;
22 kind: "open_dialog" | "open_pr";
23 dialogAction?: GitDialogAction;
24}
25
26export interface GitQuickAction {
27 label: string;
28 disabled: boolean;
29 kind: "run_action" | "run_pull" | "open_pr" | "open_publish" | "show_hint";
30 action?: GitStackedAction;
31 hint?: string;
32}
33
34export interface DefaultBranchActionDialogCopy {
35 title: string;
36 description: string;
37 continueLabel: string;
38}
39
40export interface GitActionProgressPresentation {
41 readonly status: string;
42 readonly output: string | null;
43 readonly startedAtMs: number | null;
44}
45
46export interface GitActionResultToastTiming {
47 readonly timeout: 0;
48 readonly dismissAfterVisibleMs: number | null;
49}
50
51export type DefaultBranchConfirmableAction =
52 | "push"
53 | "create_pr"
54 | "commit_push"
55 | "commit_push_pr";
56
57const GIT_ACTION_SUCCESS_TOAST_VISIBLE_MS = 10_000;
58
59export function resolveGitActionResultToastTiming(
60 type: "error" | "success",
61): GitActionResultToastTiming {
62 return {
63 timeout: 0,
64 dismissAfterVisibleMs: type === "success" ? GIT_ACTION_SUCCESS_TOAST_VISIBLE_MS : null,
65 };
66}
67
68function resolveChangeRequestTerminology(
69 gitStatus: VcsStatusResult | null,
70): ChangeRequestTerminology {
71 return gitStatus?.sourceControlProvider
72 ? getChangeRequestTerminology(gitStatus.sourceControlProvider)
73 : DEFAULT_CHANGE_REQUEST_TERMINOLOGY;
74}
75
76export function resolveGitActionProgressPresentation(input: {
77 readonly isRunning: boolean;
78 readonly operation: string | null;
79 readonly currentLabel: string | null;
80 readonly lastOutputLine: string | null;
81 readonly phaseStartedAtMs: number | null;
82 readonly hookStartedAtMs: number | null;
83}): GitActionProgressPresentation | null {
84 if (
85 !input.isRunning ||
86 (input.operation !== "run_change_request" && input.operation !== "pull")
87 ) {
88 return null;
89 }
90
91 const currentLabel = input.currentLabel?.trim();
92 const output = input.lastOutputLine?.trim();
93 const isPull = input.operation === "pull";
94 return {
95 status:
96 currentLabel && currentLabel !== "Running source control action"
97 ? currentLabel
98 : isPull
99 ? "Pulling latest changes..."
100 : "Starting source control action...",
101 output: !isPull && output ? output : null,
102 startedAtMs: isPull
103 ? input.phaseStartedAtMs
104 : (input.hookStartedAtMs ?? input.phaseStartedAtMs),
105 };
106}
107
108export function formatGitActionElapsed(startedAtMs: number | null, nowMs: number): string | null {
109 if (startedAtMs === null) {
110 return null;
111 }
112
113 const elapsedSeconds = Math.max(0, Math.floor((nowMs - startedAtMs) / 1_000));
114 if (elapsedSeconds < 60) {
115 return `${elapsedSeconds}s`;
116 }
117
118 const minutes = Math.floor(elapsedSeconds / 60);
119 const seconds = elapsedSeconds % 60;
120 return `${minutes}m ${seconds}s`;
121}
122
123export function buildGitActionProgressStages(input: {
124 action: GitStackedAction;
125 hasCustomCommitMessage: boolean;
126 hasWorkingTreeChanges: boolean;
127 pushTarget?: string;
128 featureBranch?: boolean;
129 shouldPushBeforePr?: boolean;
130 terminology?: ChangeRequestTerminology;
131}): string[] {
132 const terminology = input.terminology ?? DEFAULT_CHANGE_REQUEST_TERMINOLOGY;
133 const branchStages = input.featureBranch ? ["Preparing feature ref..."] : [];
134 const pushStage = input.pushTarget ? `Pushing to ${input.pushTarget}...` : "Pushing...";
135 const prStages = [
136 `Preparing ${terminology.shortLabel}...`,
137 `Generating ${terminology.shortLabel} content...`,
138 `Creating ${terminology.singular}...`,
139 ];
140
141 if (input.action === "push") {
142 return [pushStage];
143 }
144 if (input.action === "create_pr") {
145 return input.shouldPushBeforePr ? [pushStage, ...prStages] : prStages;
146 }
147
148 const shouldIncludeCommitStages = input.action === "commit" || input.hasWorkingTreeChanges;
149 const commitStages = !shouldIncludeCommitStages
150 ? []
151 : input.hasCustomCommitMessage
152 ? ["Committing..."]
153 : ["Generating commit message...", "Committing..."];
154 if (input.action === "commit") {
155 return [...branchStages, ...commitStages];
156 }
157 if (input.action === "commit_push") {
158 return [...branchStages, ...commitStages, pushStage];
159 }
160 return [...branchStages, ...commitStages, pushStage, ...prStages];
161}
162
163export function buildMenuItems(
164 gitStatus: VcsStatusResult | null,
165 isBusy: boolean,
166 hasPrimaryRemote = true,
167): GitActionMenuItem[] {
168 if (!gitStatus) return [];
169 const terminology = resolveChangeRequestTerminology(gitStatus);
170
171 const hasBranch = gitStatus.refName !== null;
172 const hasChanges = gitStatus.hasWorkingTreeChanges;
173 const hasOpenPr = gitStatus.pr?.state === "open";
174 const isBehind = gitStatus.behindCount > 0;
175 const hasDefaultBranchDelta = (gitStatus.aheadOfDefaultCount ?? gitStatus.aheadCount) > 0;
176 const canPushWithoutUpstream = hasPrimaryRemote && !gitStatus.hasUpstream;
177 const canCommit = !isBusy && hasChanges;
178 const canPush =
179 !isBusy &&
180 hasBranch &&
181 !isBehind &&
182 gitStatus.aheadCount > 0 &&
183 (gitStatus.hasUpstream || canPushWithoutUpstream);
184 const canCreatePr =
185 !isBusy &&
186 hasBranch &&
187 !hasChanges &&
188 !hasOpenPr &&
189 hasDefaultBranchDelta &&
190 !isBehind &&
191 (gitStatus.hasUpstream || canPushWithoutUpstream);
192 const canOpenPr = !isBusy && hasOpenPr;
193
194 const commitItem: GitActionMenuItem = {
195 id: "commit",
196 label: "Commit",
197 disabled: !canCommit,
198 icon: "commit",
199 kind: "open_dialog",
200 dialogAction: "commit",
201 };
202
203 if (!hasPrimaryRemote) {
204 return [commitItem];
205 }
206
207 return [
208 commitItem,
209 {
210 id: "push",
211 label: "Push",
212 disabled: !canPush,
213 icon: "push",
214 kind: "open_dialog",
215 dialogAction: "push",
216 },
217 hasOpenPr
218 ? {
219 id: "pr",
220 label: `View ${terminology.shortLabel}`,
221 disabled: !canOpenPr,
222 icon: "pr",
223 kind: "open_pr",
224 }
225 : {
226 id: "pr",
227 label: `Create ${terminology.shortLabel}`,
228 disabled: !canCreatePr,
229 icon: "pr",
230 kind: "open_dialog",
231 dialogAction: "create_pr",
232 },
233 ];
234}
235
236export function resolveQuickAction(
237 gitStatus: VcsStatusResult | null,
238 isBusy: boolean,
239 isDefaultRef = false,
240 hasPrimaryRemote = true,
241): GitQuickAction {
242 if (isBusy) {
243 return { label: "Commit", disabled: true, kind: "show_hint", hint: "Git action in progress." };
244 }
245
246 if (!gitStatus) {
247 return {
248 label: "Commit",
249 disabled: true,
250 kind: "show_hint",
251 hint: "Git status is unavailable.",
252 };
253 }
254
255 const hasBranch = gitStatus.refName !== null;
256 const hasChanges = gitStatus.hasWorkingTreeChanges;
257 const hasOpenPr = gitStatus.pr?.state === "open";
258 const isAhead = gitStatus.aheadCount > 0;
259 const hasDefaultBranchDelta = (gitStatus.aheadOfDefaultCount ?? gitStatus.aheadCount) > 0;
260 const isBehind = gitStatus.behindCount > 0;
261 const isDiverged = isAhead && isBehind;
262 const terminology = resolveChangeRequestTerminology(gitStatus);
263
264 if (!hasBranch) {
265 return {
266 label: "Commit",
267 disabled: true,
268 kind: "show_hint",
269 hint: `Create and checkout a ref before pushing or opening a ${terminology.singular}.`,
270 };
271 }
272
273 if (hasChanges) {
274 if (!gitStatus.hasUpstream && !hasPrimaryRemote) {
275 return { label: "Commit", disabled: false, kind: "run_action", action: "commit" };
276 }
277 if (hasOpenPr || isDefaultRef) {
278 return { label: "Commit & push", disabled: false, kind: "run_action", action: "commit_push" };
279 }
280 return {
281 label: `Commit, push & ${terminology.shortLabel}`,
282 disabled: false,
283 kind: "run_action",
284 action: "commit_push_pr",
285 };
286 }
287
288 if (!gitStatus.hasUpstream) {
289 if (!hasPrimaryRemote) {
290 if (hasOpenPr && !isAhead) {
291 return { label: `View ${terminology.shortLabel}`, disabled: false, kind: "open_pr" };
292 }
293 return {
294 label: "Publish repository",
295 disabled: false,
296 kind: "open_publish",
297 };
298 }
299 if (!isAhead) {
300 if (hasOpenPr) {
301 return { label: `View ${terminology.shortLabel}`, disabled: false, kind: "open_pr" };
302 }
303 return {
304 label: "Push",
305 disabled: true,
306 kind: "show_hint",
307 hint: "No local commits to push.",
308 };
309 }
310 if (hasOpenPr || isDefaultRef) {
311 return {
312 label: "Push",
313 disabled: false,
314 kind: "run_action",
315 action: isDefaultRef ? "commit_push" : "push",
316 };
317 }
318 return {
319 label: `Push & create ${terminology.shortLabel}`,
320 disabled: false,
321 kind: "run_action",
322 action: "create_pr",
323 };
324 }
325
326 if (isDiverged) {
327 return {
328 label: "Sync ref",
329 disabled: true,
330 kind: "show_hint",
331 hint: "Branch has diverged from upstream. Rebase/merge first.",
332 };
333 }
334
335 if (isBehind) {
336 return {
337 label: "Pull",
338 disabled: false,
339 kind: "run_pull",
340 };
341 }
342
343 if (isAhead) {
344 if (hasOpenPr || isDefaultRef) {
345 return {
346 label: "Push",
347 disabled: false,
348 kind: "run_action",
349 action: isDefaultRef ? "commit_push" : "push",
350 };
351 }
352 return {
353 label: `Push & create ${terminology.shortLabel}`,
354 disabled: false,
355 kind: "run_action",
356 action: "create_pr",
357 };
358 }
359
360 if (hasOpenPr && gitStatus.hasUpstream) {
361 return { label: `View ${terminology.shortLabel}`, disabled: false, kind: "open_pr" };
362 }
363
364 if (hasDefaultBranchDelta && !isDefaultRef) {
365 return {
366 label: `Create ${terminology.shortLabel}`,
367 disabled: false,
368 kind: "run_action",
369 action: "create_pr",
370 };
371 }
372
373 return {
374 label: "Commit",
375 disabled: true,
376 kind: "show_hint",
377 hint: "Branch is up to date. No action needed.",
378 };
379}
380
381export function requiresDefaultBranchConfirmation(
382 action: GitStackedAction,
383 isDefaultRef: boolean,
384): boolean {
385 if (!isDefaultRef) return false;
386 return (
387 action === "push" ||
388 action === "create_pr" ||
389 action === "commit_push" ||
390 action === "commit_push_pr"
391 );
392}
393
394export function resolveDefaultBranchActionDialogCopy(input: {
395 action: DefaultBranchConfirmableAction;
396 branchName: string;
397 includesCommit: boolean;
398 terminology?: ChangeRequestTerminology;
399}): DefaultBranchActionDialogCopy {
400 const branchLabel = input.branchName;
401 const suffix = ` on "${branchLabel}". You can continue on this ref or create a feature ref and run the same action there.`;
402 const terminology = input.terminology ?? DEFAULT_CHANGE_REQUEST_TERMINOLOGY;
403
404 if (input.action === "push" || input.action === "commit_push") {
405 if (input.includesCommit) {
406 return {
407 title: "Commit & push to default ref?",
408 description: `This action will commit and push changes${suffix}`,
409 continueLabel: `Commit & push to ${branchLabel}`,
410 };
411 }
412 return {
413 title: "Push to default ref?",
414 description: `This action will push local commits${suffix}`,
415 continueLabel: `Push to ${branchLabel}`,
416 };
417 }
418
419 if (input.includesCommit) {
420 return {
421 title: `Commit, push & create ${terminology.shortLabel} from default ref?`,
422 description: `This action will commit, push, and create a ${terminology.singular}${suffix}`,
423 continueLabel: `Commit, push & create ${terminology.shortLabel}`,
424 };
425 }
426 return {
427 title: `Push & create ${terminology.shortLabel} from default ref?`,
428 description: `This action will push local commits and create a ${terminology.singular}${suffix}`,
429 continueLabel: `Push & create ${terminology.shortLabel}`,
430 };
431}
432
433export function resolveThreadBranchUpdate(
434 result: GitRunStackedActionResult,
435): { branch: string } | null {
436 if (result.branch.status !== "created" || !result.branch.name) {
437 return null;
438 }
439
440 return {
441 branch: result.branch.name,
442 };
443}
444
445export function resolveThreadBranchMetadataPatch(
446 branch: string | null,
447 expectedBranch: string | null,
448): {
449 branch: string | null;
450 expectedBranch: string | null;
451} {
452 return { branch, expectedBranch };
453}
454
455export function resolveLiveThreadBranchUpdate(input: {
456 threadBranch: string | null;
457 gitStatus: VcsStatusResult | null;
458}): { branch: string | null } | null {
459 if (!input.gitStatus) {
460 return null;
461 }
462
463 if (input.gitStatus.refName === null && input.threadBranch !== null) {
464 return null;
465 }
466
467 if (input.threadBranch === input.gitStatus.refName) {
468 return null;
469 }
470
471 if (
472 input.threadBranch !== null &&
473 input.gitStatus.refName !== null &&
474 !isTemporaryWorktreeBranch(input.threadBranch) &&
475 isTemporaryWorktreeBranch(input.gitStatus.refName)
476 ) {
477 return null;
478 }
479
480 return {
481 branch: input.gitStatus.refName,
482 };
483}
484
485// Re-export from shared for backwards compatibility in this module's exports
486export { resolveAutoFeatureBranchName } from "@t3tools/shared/git";