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

GitActionsControl.logic.ts

455 lines11.9 KB

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;
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;
30​
31export interface DefaultBranchActionDialogCopy {
32 title: string;
33 description: string;
34 continueLabel: string;
36​
37export interface GitActionProgressPresentation {
38 readonly status: string;
39 readonly output: string | null;
40 readonly startedAtMs: number | null;
42​
43export interface GitActionResultToastTiming {
44 readonly timeout: 0;
45 readonly dismissAfterVisibleMs: number | null;
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 };
64​
65function resolveChangeRequestTerminology(
66 gitStatus: VcsStatusResult | null,
67): ChangeRequestTerminology {
68 return sourceControlClients.get(gitStatus?.sourceControlProvider?.kind).changeRequest;
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 };
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`;
117​
118export function buildMenuItems(
119 gitStatus: VcsStatusResult | null,
120 isBusy: boolean,
121 hasPrimaryRemote = true,
122): GitActionMenuItem[] {
123 if (!gitStatus) return [];
124 const terminology = resolveChangeRequestTerminology(gitStatus);
125​
126 const hasBranch = gitStatus.refName !== null;
127 const hasChanges = gitStatus.hasWorkingTreeChanges;
128 const hasOpenPr = gitStatus.pr?.state === "open";
129 const isBehind = gitStatus.behindCount > 0;
130 const hasDefaultBranchDelta = (gitStatus.aheadOfDefaultCount ?? gitStatus.aheadCount) > 0;
131 const canPushWithoutUpstream = hasPrimaryRemote && !gitStatus.hasUpstream;
132 const canCommit = !isBusy && hasChanges;
133 const canPush =
134 !isBusy &&
135 hasBranch &&
136 !isBehind &&
137 gitStatus.aheadCount > 0 &&
138 (gitStatus.hasUpstream || canPushWithoutUpstream);
139 const canCreatePr =
140 !isBusy &&
141 hasBranch &&
142 !hasOpenPr &&
143 hasDefaultBranchDelta &&
144 !isBehind &&
145 (gitStatus.hasUpstream || canPushWithoutUpstream);
146​
147 const commitItem: GitActionMenuItem = {
148 id: "commit",
149 label: "Commit",
150 disabled: !canCommit,
151 icon: "commit",
152 kind: "open_dialog",
153 dialogAction: "commit",
154 };
155​
156 if (!hasPrimaryRemote) {
157 return [commitItem];
158 }
159​
160 const pushItem: GitActionMenuItem = {
161 id: "push",
162 label: "Push",
163 disabled: !canPush,
164 icon: "push",
165 kind: "open_dialog",
166 dialogAction: "push",
167 };
168​
169 if (hasOpenPr) {
170 return [commitItem, pushItem];
171 }
172​
173 return [
174 commitItem,
175 pushItem,
176 {
177 id: "pr",
178 label: `Create ${terminology.shortLabel}`,
179 disabled: !canCreatePr,
180 icon: "pr",
181 kind: "open_dialog",
182 dialogAction: "create_pr",
183 },
184 ];
186​
187export function resolveQuickAction(
188 gitStatus: VcsStatusResult | null,
189 isBusy: boolean,
190 isDefaultRef = false,
191 hasPrimaryRemote = true,
192): GitQuickAction {
193 if (isBusy) {
194 return { label: "Commit", disabled: true, kind: "show_hint", hint: "Git action in progress." };
195 }
196​
197 if (!gitStatus) {
198 return {
199 label: "Commit",
200 disabled: true,
201 kind: "show_hint",
202 hint: "Git status is unavailable.",
203 };
204 }
205​
206 const hasBranch = gitStatus.refName !== null;
207 const hasChanges = gitStatus.hasWorkingTreeChanges;
208 const hasOpenPr = gitStatus.pr?.state === "open";
209 const isAhead = gitStatus.aheadCount > 0;
210 const hasDefaultBranchDelta = (gitStatus.aheadOfDefaultCount ?? gitStatus.aheadCount) > 0;
211 const isBehind = gitStatus.behindCount > 0;
212 const isDiverged = isAhead && isBehind;
213 const terminology = resolveChangeRequestTerminology(gitStatus);
214​
215 if (!hasBranch) {
216 return {
217 label: "Commit",
218 disabled: true,
219 kind: "show_hint",
220 hint: `Create and checkout a ref before pushing or opening a ${terminology.singular}.`,
221 };
222 }
223​
224 if (hasChanges) {
225 if (!gitStatus.hasUpstream && !hasPrimaryRemote) {
226 return { label: "Commit", disabled: false, kind: "run_action", action: "commit" };
227 }
228 if (hasOpenPr || isDefaultRef) {
229 return { label: "Commit & push", disabled: false, kind: "run_action", action: "commit_push" };
230 }
231 return {
232 label: `Commit, push & ${terminology.shortLabel}`,
233 disabled: false,
234 kind: "run_action",
235 action: "commit_push_pr",
236 };
237 }
238​
239 if (!gitStatus.hasUpstream) {
240 if (!hasPrimaryRemote) {
241 return {
242 label: "Publish repository",
243 disabled: false,
244 kind: "open_publish",
245 };
246 }
247 if (!isAhead) {
248 if (hasOpenPr) {
249 return {
250 label: "Commit",
251 disabled: true,
252 kind: "show_hint",
253 hint: "Branch is up to date. No action needed.",
254 };
255 }
256 return {
257 label: "Push",
258 disabled: true,
259 kind: "show_hint",
260 hint: "No local commits to push.",
261 };
262 }
263 if (hasOpenPr || isDefaultRef) {
264 return {
265 label: "Push",
266 disabled: false,
267 kind: "run_action",
268 action: isDefaultRef ? "commit_push" : "push",
269 };
270 }
271 return {
272 label: `Push & create ${terminology.shortLabel}`,
273 disabled: false,
274 kind: "run_action",
275 action: "create_pr",
276 };
277 }
278​
279 if (isDiverged) {
280 return {
281 label: "Sync ref",
282 disabled: true,
283 kind: "show_hint",
284 hint: "Branch has diverged from upstream. Rebase/merge first.",
285 };
286 }
287​
288 if (isBehind) {
289 return {
290 label: "Pull",
291 disabled: false,
292 kind: "run_pull",
293 };
294 }
295​
296 if (isAhead) {
297 if (hasOpenPr || isDefaultRef) {
298 return {
299 label: "Push",
300 disabled: false,
301 kind: "run_action",
302 action: isDefaultRef ? "commit_push" : "push",
303 };
304 }
305 return {
306 label: `Push & create ${terminology.shortLabel}`,
307 disabled: false,
308 kind: "run_action",
309 action: "create_pr",
310 };
311 }
312​
313 if (hasOpenPr && gitStatus.hasUpstream) {
314 return {
315 label: "Commit",
316 disabled: true,
317 kind: "show_hint",
318 hint: "Branch is up to date. No action needed.",
319 };
320 }
321​
322 if (hasDefaultBranchDelta && !isDefaultRef) {
323 return {
324 label: `Create ${terminology.shortLabel}`,
325 disabled: false,
326 kind: "run_action",
327 action: "create_pr",
328 };
329 }
330​
331 return {
332 label: "Commit",
333 disabled: true,
334 kind: "show_hint",
335 hint: "Branch is up to date. No action needed.",
336 };
338​
339export function requiresDefaultBranchConfirmation(
340 action: GitStackedAction,
341 isDefaultRef: boolean,
342): boolean {
343 if (!isDefaultRef) return false;
344 return (
345 action === "push" ||
346 action === "create_pr" ||
347 action === "commit_push" ||
348 action === "commit_push_pr"
349 );
351​
352export function resolveDefaultBranchActionDialogCopy(input: {
353 action: DefaultBranchConfirmableAction;
354 branchName: string;
355 includesCommit: boolean;
356 terminology?: ChangeRequestTerminology;
357}): DefaultBranchActionDialogCopy {
358 const branchLabel = input.branchName;
359 const suffix = ` on "${branchLabel}". You can continue on this ref or create a feature ref and run the same action there.`;
360 const terminology = input.terminology ?? sourceControlClients.get(undefined).changeRequest;
361​
362 if (input.action === "push" || input.action === "commit_push") {
363 if (input.includesCommit) {
364 return {
365 title: "Commit & push to default ref?",
366 description: `This action will commit and push changes${suffix}`,
367 continueLabel: `Commit & push to ${branchLabel}`,
368 };
369 }
370 return {
371 title: "Push to default ref?",
372 description: `This action will push local commits${suffix}`,
373 continueLabel: `Push to ${branchLabel}`,
374 };
375 }
376​
377 if (input.includesCommit) {
378 return {
379 title: `Commit, push & create ${terminology.shortLabel} from default ref?`,
380 description: `This action will commit, push, and create a ${terminology.singular}${suffix}`,
381 continueLabel: `Commit, push & create ${terminology.shortLabel}`,
382 };
383 }
384 return {
385 title: `Push & create ${terminology.shortLabel} from default ref?`,
386 description: `This action will push local commits and create a ${terminology.singular}${suffix}`,
387 continueLabel: `Push & create ${terminology.shortLabel}`,
388 };
390​
391/**
392 * Why the default-ref dialog cannot check out a feature branch, or null when it can.
393 * The server names that branch from the uncommitted changes, so a clean tree cannot use it.
394 */
395export function resolveFeatureBranchUnavailableReason(input: {
396 branchName: string;
397 hasWorkingTreeChanges: boolean;
398}): string | null {
399 if (input.hasWorkingTreeChanges) return null;
400 return `There are no changes to commit, so a feature branch can't be named. Create a branch yourself or continue on ${input.branchName}.`;
402​
403export function resolveThreadBranchUpdate(
404 result: GitRunStackedActionResult,
405): { branch: string } | null {
406 if (result.branch.status !== "created" || !result.branch.name) {
407 return null;
408 }
409​
410 return {
411 branch: result.branch.name,
412 };
414​
415export function resolveThreadBranchMetadataPatch(
416 branch: string | null,
417 expectedBranch: string | null,
418): {
419 branch: string | null;
420 expectedBranch: string | null;
421} {
422 return { branch, expectedBranch };
424​
425export function resolveLiveThreadBranchUpdate(input: {
426 threadBranch: string | null;
427 gitStatus: VcsStatusResult | null;
428}): { branch: string | null } | null {
429 if (!input.gitStatus) {
430 return null;
431 }
432​
433 if (input.gitStatus.refName === null && input.threadBranch !== null) {
434 return null;
435 }
436​
437 if (input.threadBranch === input.gitStatus.refName) {
438 return null;
439 }
440​
441 if (
442 input.threadBranch !== null &&
443 input.gitStatus.refName !== null &&
444 !isTemporaryWorktreeBranch(input.threadBranch) &&
445 isTemporaryWorktreeBranch(input.gitStatus.refName)
446 ) {
447 return null;
448 }
449​
450 return {
451 branch: input.gitStatus.refName,
452 };
454​
455export { resolveAutoFeatureBranchName } from "@t3tools/shared/git";