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";
23 dialogAction?: GitDialogAction;
24}
25
26export interface GitQuickAction {
27 label: string;
28 disabled: boolean;
29 kind: "run_action" | "run_pull" | "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
57export const GIT_ACTION_SUCCESS_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_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 !hasOpenPr &&
188 hasDefaultBranchDelta &&
189 !isBehind &&
190 (gitStatus.hasUpstream || canPushWithoutUpstream);
191
192 const commitItem: GitActionMenuItem = {
193 id: "commit",
194 label: "Commit",
195 disabled: !canCommit,
196 icon: "commit",
197 kind: "open_dialog",
198 dialogAction: "commit",
199 };
200
201 if (!hasPrimaryRemote) {
202 return [commitItem];
203 }
204
205 const pushItem: GitActionMenuItem = {
206 id: "push",
207 label: "Push",
208 disabled: !canPush,
209 icon: "push",
210 kind: "open_dialog",
211 dialogAction: "push",
212 };
213
214 // An open change request is surfaced by the standalone attribution row, so
215 // the menu offers no change-request entry at all while one is open.
216 if (hasOpenPr) {
217 return [commitItem, pushItem];
218 }
219
220 return [
221 commitItem,
222 pushItem,
223 {
224 id: "pr",
225 label: `Create ${terminology.shortLabel}`,
226 disabled: !canCreatePr,
227 icon: "pr",
228 kind: "open_dialog",
229 dialogAction: "create_pr",
230 },
231 ];
232}
233
234export function resolveQuickAction(
235 gitStatus: VcsStatusResult | null,
236 isBusy: boolean,
237 isDefaultRef = false,
238 hasPrimaryRemote = true,
239): GitQuickAction {
240 if (isBusy) {
241 return { label: "Commit", disabled: true, kind: "show_hint", hint: "Git action in progress." };
242 }
243
244 if (!gitStatus) {
245 return {
246 label: "Commit",
247 disabled: true,
248 kind: "show_hint",
249 hint: "Git status is unavailable.",
250 };
251 }
252
253 const hasBranch = gitStatus.refName !== null;
254 const hasChanges = gitStatus.hasWorkingTreeChanges;
255 const hasOpenPr = gitStatus.pr?.state === "open";
256 const isAhead = gitStatus.aheadCount > 0;
257 const hasDefaultBranchDelta = (gitStatus.aheadOfDefaultCount ?? gitStatus.aheadCount) > 0;
258 const isBehind = gitStatus.behindCount > 0;
259 const isDiverged = isAhead && isBehind;
260 const terminology = resolveChangeRequestTerminology(gitStatus);
261
262 if (!hasBranch) {
263 return {
264 label: "Commit",
265 disabled: true,
266 kind: "show_hint",
267 hint: `Create and checkout a ref before pushing or opening a ${terminology.singular}.`,
268 };
269 }
270
271 if (hasChanges) {
272 if (!gitStatus.hasUpstream && !hasPrimaryRemote) {
273 return { label: "Commit", disabled: false, kind: "run_action", action: "commit" };
274 }
275 if (hasOpenPr || isDefaultRef) {
276 return { label: "Commit & push", disabled: false, kind: "run_action", action: "commit_push" };
277 }
278 return {
279 label: `Commit, push & ${terminology.shortLabel}`,
280 disabled: false,
281 kind: "run_action",
282 action: "commit_push_pr",
283 };
284 }
285
286 if (!gitStatus.hasUpstream) {
287 if (!hasPrimaryRemote) {
288 return {
289 label: "Publish repository",
290 disabled: false,
291 kind: "open_publish",
292 };
293 }
294 if (!isAhead) {
295 if (hasOpenPr) {
296 return {
297 label: "Commit",
298 disabled: true,
299 kind: "show_hint",
300 hint: "Branch is up to date. No action needed.",
301 };
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 // An open change request is surfaced by the standalone attribution row in the
361 // details panel, so the action button rests in its disabled up-to-date state.
362 if (hasOpenPr && gitStatus.hasUpstream) {
363 return {
364 label: "Commit",
365 disabled: true,
366 kind: "show_hint",
367 hint: "Branch is up to date. No action needed.",
368 };
369 }
370
371 if (hasDefaultBranchDelta && !isDefaultRef) {
372 return {
373 label: `Create ${terminology.shortLabel}`,
374 disabled: false,
375 kind: "run_action",
376 action: "create_pr",
377 };
378 }
379
380 return {
381 label: "Commit",
382 disabled: true,
383 kind: "show_hint",
384 hint: "Branch is up to date. No action needed.",
385 };
386}
387
388export function requiresDefaultBranchConfirmation(
389 action: GitStackedAction,
390 isDefaultRef: boolean,
391): boolean {
392 if (!isDefaultRef) return false;
393 return (
394 action === "push" ||
395 action === "create_pr" ||
396 action === "commit_push" ||
397 action === "commit_push_pr"
398 );
399}
400
401export function resolveDefaultBranchActionDialogCopy(input: {
402 action: DefaultBranchConfirmableAction;
403 branchName: string;
404 includesCommit: boolean;
405 terminology?: ChangeRequestTerminology;
406}): DefaultBranchActionDialogCopy {
407 const branchLabel = input.branchName;
408 const suffix = ` on "${branchLabel}". You can continue on this ref or create a feature ref and run the same action there.`;
409 const terminology = input.terminology ?? DEFAULT_CHANGE_REQUEST_TERMINOLOGY;
410
411 if (input.action === "push" || input.action === "commit_push") {
412 if (input.includesCommit) {
413 return {
414 title: "Commit & push to default ref?",
415 description: `This action will commit and push changes${suffix}`,
416 continueLabel: `Commit & push to ${branchLabel}`,
417 };
418 }
419 return {
420 title: "Push to default ref?",
421 description: `This action will push local commits${suffix}`,
422 continueLabel: `Push to ${branchLabel}`,
423 };
424 }
425
426 if (input.includesCommit) {
427 return {
428 title: `Commit, push & create ${terminology.shortLabel} from default ref?`,
429 description: `This action will commit, push, and create a ${terminology.singular}${suffix}`,
430 continueLabel: `Commit, push & create ${terminology.shortLabel}`,
431 };
432 }
433 return {
434 title: `Push & create ${terminology.shortLabel} from default ref?`,
435 description: `This action will push local commits and create a ${terminology.singular}${suffix}`,
436 continueLabel: `Push & create ${terminology.shortLabel}`,
437 };
438}
439
440export function resolveThreadBranchUpdate(
441 result: GitRunStackedActionResult,
442): { branch: string } | null {
443 if (result.branch.status !== "created" || !result.branch.name) {
444 return null;
445 }
446
447 return {
448 branch: result.branch.name,
449 };
450}
451
452export function resolveThreadBranchMetadataPatch(
453 branch: string | null,
454 expectedBranch: string | null,
455): {
456 branch: string | null;
457 expectedBranch: string | null;
458} {
459 return { branch, expectedBranch };
460}
461
462export function resolveLiveThreadBranchUpdate(input: {
463 threadBranch: string | null;
464 gitStatus: VcsStatusResult | null;
465}): { branch: string | null } | null {
466 if (!input.gitStatus) {
467 return null;
468 }
469
470 if (input.gitStatus.refName === null && input.threadBranch !== null) {
471 return null;
472 }
473
474 if (input.threadBranch === input.gitStatus.refName) {
475 return null;
476 }
477
478 if (
479 input.threadBranch !== null &&
480 input.gitStatus.refName !== null &&
481 !isTemporaryWorktreeBranch(input.threadBranch) &&
482 isTemporaryWorktreeBranch(input.gitStatus.refName)
483 ) {
484 return null;
485 }
486
487 return {
488 branch: input.gitStatus.refName,
489 };
490}
491
492// Re-export from shared for backwards compatibility in this module's exports
493export { resolveAutoFeatureBranchName } from "@t3tools/shared/git";