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