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