Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GitActionsControl.logic.ts

494 lines13.2 KB

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;
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;
33​
34export interface DefaultBranchActionDialogCopy {
35 title: string;
36 description: string;
37 continueLabel: string;
39​
40export interface GitActionProgressPresentation {
41 readonly status: string;
42 readonly output: string | null;
43 readonly startedAtMs: number | null;
45​
46export interface GitActionResultToastTiming {
47 readonly timeout: 0;
48 readonly dismissAfterVisibleMs: number | null;
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 };
67​
68function resolveChangeRequestTerminology(
69 gitStatus: VcsStatusResult | null,
70): ChangeRequestTerminology {
71 return gitStatus?.sourceControlProvider
72 ? getChangeRequestTerminology(gitStatus.sourceControlProvider)
73 : DEFAULT_CHANGE_REQUEST_TERMINOLOGY;
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 };
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`;
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];
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​
193 const commitItem: GitActionMenuItem = {
194 id: "commit",
195 label: "Commit",
196 disabled: !canCommit,
197 icon: "commit",
198 kind: "open_dialog",
199 dialogAction: "commit",
200 };
201​
202 if (!hasPrimaryRemote) {
203 return [commitItem];
204 }
205​
206 const pushItem: GitActionMenuItem = {
207 id: "push",
208 label: "Push",
209 disabled: !canPush,
210 icon: "push",
211 kind: "open_dialog",
212 dialogAction: "push",
213 };
214​
215 // An open change request is surfaced by the standalone attribution row, so
216 // the menu offers no change-request entry at all while one is open.
217 if (hasOpenPr) {
218 return [commitItem, pushItem];
219 }
220​
221 return [
222 commitItem,
223 pushItem,
224 {
225 id: "pr",
226 label: `Create ${terminology.shortLabel}`,
227 disabled: !canCreatePr,
228 icon: "pr",
229 kind: "open_dialog",
230 dialogAction: "create_pr",
231 },
232 ];
234​
235export function resolveQuickAction(
236 gitStatus: VcsStatusResult | null,
237 isBusy: boolean,
238 isDefaultRef = false,
239 hasPrimaryRemote = true,
240): GitQuickAction {
241 if (isBusy) {
242 return { label: "Commit", disabled: true, kind: "show_hint", hint: "Git action in progress." };
243 }
244​
245 if (!gitStatus) {
246 return {
247 label: "Commit",
248 disabled: true,
249 kind: "show_hint",
250 hint: "Git status is unavailable.",
251 };
252 }
253​
254 const hasBranch = gitStatus.refName !== null;
255 const hasChanges = gitStatus.hasWorkingTreeChanges;
256 const hasOpenPr = gitStatus.pr?.state === "open";
257 const isAhead = gitStatus.aheadCount > 0;
258 const hasDefaultBranchDelta = (gitStatus.aheadOfDefaultCount ?? gitStatus.aheadCount) > 0;
259 const isBehind = gitStatus.behindCount > 0;
260 const isDiverged = isAhead && isBehind;
261 const terminology = resolveChangeRequestTerminology(gitStatus);
262​
263 if (!hasBranch) {
264 return {
265 label: "Commit",
266 disabled: true,
267 kind: "show_hint",
268 hint: `Create and checkout a ref before pushing or opening a ${terminology.singular}.`,
269 };
270 }
271​
272 if (hasChanges) {
273 if (!gitStatus.hasUpstream && !hasPrimaryRemote) {
274 return { label: "Commit", disabled: false, kind: "run_action", action: "commit" };
275 }
276 if (hasOpenPr || isDefaultRef) {
277 return { label: "Commit & push", disabled: false, kind: "run_action", action: "commit_push" };
278 }
279 return {
280 label: `Commit, push & ${terminology.shortLabel}`,
281 disabled: false,
282 kind: "run_action",
283 action: "commit_push_pr",
284 };
285 }
286​
287 if (!gitStatus.hasUpstream) {
288 if (!hasPrimaryRemote) {
289 return {
290 label: "Publish repository",
291 disabled: false,
292 kind: "open_publish",
293 };
294 }
295 if (!isAhead) {
296 if (hasOpenPr) {
297 return {
298 label: "Commit",
299 disabled: true,
300 kind: "show_hint",
301 hint: "Branch is up to date. No action needed.",
302 };
303 }
304 return {
305 label: "Push",
306 disabled: true,
307 kind: "show_hint",
308 hint: "No local commits to push.",
309 };
310 }
311 if (hasOpenPr || isDefaultRef) {
312 return {
313 label: "Push",
314 disabled: false,
315 kind: "run_action",
316 action: isDefaultRef ? "commit_push" : "push",
317 };
318 }
319 return {
320 label: `Push & create ${terminology.shortLabel}`,
321 disabled: false,
322 kind: "run_action",
323 action: "create_pr",
324 };
325 }
326​
327 if (isDiverged) {
328 return {
329 label: "Sync ref",
330 disabled: true,
331 kind: "show_hint",
332 hint: "Branch has diverged from upstream. Rebase/merge first.",
333 };
334 }
335​
336 if (isBehind) {
337 return {
338 label: "Pull",
339 disabled: false,
340 kind: "run_pull",
341 };
342 }
343​
344 if (isAhead) {
345 if (hasOpenPr || isDefaultRef) {
346 return {
347 label: "Push",
348 disabled: false,
349 kind: "run_action",
350 action: isDefaultRef ? "commit_push" : "push",
351 };
352 }
353 return {
354 label: `Push & create ${terminology.shortLabel}`,
355 disabled: false,
356 kind: "run_action",
357 action: "create_pr",
358 };
359 }
360​
361 // An open change request is surfaced by the standalone attribution row in the
362 // details panel, so the action button rests in its disabled up-to-date state.
363 if (hasOpenPr && gitStatus.hasUpstream) {
364 return {
365 label: "Commit",
366 disabled: true,
367 kind: "show_hint",
368 hint: "Branch is up to date. No action needed.",
369 };
370 }
371​
372 if (hasDefaultBranchDelta && !isDefaultRef) {
373 return {
374 label: `Create ${terminology.shortLabel}`,
375 disabled: false,
376 kind: "run_action",
377 action: "create_pr",
378 };
379 }
380​
381 return {
382 label: "Commit",
383 disabled: true,
384 kind: "show_hint",
385 hint: "Branch is up to date. No action needed.",
386 };
388​
389export function requiresDefaultBranchConfirmation(
390 action: GitStackedAction,
391 isDefaultRef: boolean,
392): boolean {
393 if (!isDefaultRef) return false;
394 return (
395 action === "push" ||
396 action === "create_pr" ||
397 action === "commit_push" ||
398 action === "commit_push_pr"
399 );
401​
402export function resolveDefaultBranchActionDialogCopy(input: {
403 action: DefaultBranchConfirmableAction;
404 branchName: string;
405 includesCommit: boolean;
406 terminology?: ChangeRequestTerminology;
407}): DefaultBranchActionDialogCopy {
408 const branchLabel = input.branchName;
409 const suffix = ` on "${branchLabel}". You can continue on this ref or create a feature ref and run the same action there.`;
410 const terminology = input.terminology ?? DEFAULT_CHANGE_REQUEST_TERMINOLOGY;
411​
412 if (input.action === "push" || input.action === "commit_push") {
413 if (input.includesCommit) {
414 return {
415 title: "Commit & push to default ref?",
416 description: `This action will commit and push changes${suffix}`,
417 continueLabel: `Commit & push to ${branchLabel}`,
418 };
419 }
420 return {
421 title: "Push to default ref?",
422 description: `This action will push local commits${suffix}`,
423 continueLabel: `Push to ${branchLabel}`,
424 };
425 }
426​
427 if (input.includesCommit) {
428 return {
429 title: `Commit, push & create ${terminology.shortLabel} from default ref?`,
430 description: `This action will commit, push, and create a ${terminology.singular}${suffix}`,
431 continueLabel: `Commit, push & create ${terminology.shortLabel}`,
432 };
433 }
434 return {
435 title: `Push & create ${terminology.shortLabel} from default ref?`,
436 description: `This action will push local commits and create a ${terminology.singular}${suffix}`,
437 continueLabel: `Push & create ${terminology.shortLabel}`,
438 };
440​
441export function resolveThreadBranchUpdate(
442 result: GitRunStackedActionResult,
443): { branch: string } | null {
444 if (result.branch.status !== "created" || !result.branch.name) {
445 return null;
446 }
447​
448 return {
449 branch: result.branch.name,
450 };
452​
453export function resolveThreadBranchMetadataPatch(
454 branch: string | null,
455 expectedBranch: string | null,
456): {
457 branch: string | null;
458 expectedBranch: string | null;
459} {
460 return { branch, expectedBranch };
462​
463export function resolveLiveThreadBranchUpdate(input: {
464 threadBranch: string | null;
465 gitStatus: VcsStatusResult | null;
466}): { branch: string | null } | null {
467 if (!input.gitStatus) {
468 return null;
469 }
470​
471 if (input.gitStatus.refName === null && input.threadBranch !== null) {
472 return null;
473 }
474​
475 if (input.threadBranch === input.gitStatus.refName) {
476 return null;
477 }
478​
479 if (
480 input.threadBranch !== null &&
481 input.gitStatus.refName !== null &&
482 !isTemporaryWorktreeBranch(input.threadBranch) &&
483 isTemporaryWorktreeBranch(input.gitStatus.refName)
484 ) {
485 return null;
486 }
487​
488 return {
489 branch: input.gitStatus.refName,
490 };
492​
493// Re-export from shared for backwards compatibility in this module's exports
494export { resolveAutoFeatureBranchName } from "@t3tools/shared/git";