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

GitActionsControl.logic.ts

407 lines10.9 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" | "open_pr";
23 dialogAction?: GitDialogAction;
25​
26export interface GitQuickAction {
27 label: string;
28 disabled: boolean;
29 kind: "run_action" | "run_pull" | "open_pr" | "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 type DefaultBranchConfirmableAction =
41 | "push"
42 | "create_pr"
43 | "commit_push"
44 | "commit_push_pr";
45​
46function resolveChangeRequestTerminology(
47 gitStatus: VcsStatusResult | null,
48): ChangeRequestTerminology {
49 return gitStatus?.sourceControlProvider
50 ? getChangeRequestTerminology(gitStatus.sourceControlProvider)
51 : DEFAULT_CHANGE_REQUEST_TERMINOLOGY;
53​
54export function buildGitActionProgressStages(input: {
55 action: GitStackedAction;
56 hasCustomCommitMessage: boolean;
57 hasWorkingTreeChanges: boolean;
58 pushTarget?: string;
59 featureBranch?: boolean;
60 shouldPushBeforePr?: boolean;
61 terminology?: ChangeRequestTerminology;
62}): string[] {
63 const terminology = input.terminology ?? DEFAULT_CHANGE_REQUEST_TERMINOLOGY;
64 const branchStages = input.featureBranch ? ["Preparing feature ref..."] : [];
65 const pushStage = input.pushTarget ? `Pushing to ${input.pushTarget}...` : "Pushing...";
66 const prStages = [
67 `Preparing ${terminology.shortLabel}...`,
68 `Generating ${terminology.shortLabel} content...`,
69 `Creating ${terminology.singular}...`,
70 ];
71​
72 if (input.action === "push") {
73 return [pushStage];
74 }
75 if (input.action === "create_pr") {
76 return input.shouldPushBeforePr ? [pushStage, ...prStages] : prStages;
77 }
78​
79 const shouldIncludeCommitStages = input.action === "commit" || input.hasWorkingTreeChanges;
80 const commitStages = !shouldIncludeCommitStages
81 ? []
82 : input.hasCustomCommitMessage
83 ? ["Committing..."]
84 : ["Generating commit message...", "Committing..."];
85 if (input.action === "commit") {
86 return [...branchStages, ...commitStages];
87 }
88 if (input.action === "commit_push") {
89 return [...branchStages, ...commitStages, pushStage];
90 }
91 return [...branchStages, ...commitStages, pushStage, ...prStages];
93​
94export function buildMenuItems(
95 gitStatus: VcsStatusResult | null,
96 isBusy: boolean,
97 hasPrimaryRemote = true,
98): GitActionMenuItem[] {
99 if (!gitStatus) return [];
100 const terminology = resolveChangeRequestTerminology(gitStatus);
101​
102 const hasBranch = gitStatus.refName !== null;
103 const hasChanges = gitStatus.hasWorkingTreeChanges;
104 const hasOpenPr = gitStatus.pr?.state === "open";
105 const isBehind = gitStatus.behindCount > 0;
106 const hasDefaultBranchDelta = (gitStatus.aheadOfDefaultCount ?? gitStatus.aheadCount) > 0;
107 const canPushWithoutUpstream = hasPrimaryRemote && !gitStatus.hasUpstream;
108 const canCommit = !isBusy && hasChanges;
109 const canPush =
110 !isBusy &&
111 hasBranch &&
112 !isBehind &&
113 gitStatus.aheadCount > 0 &&
114 (gitStatus.hasUpstream || canPushWithoutUpstream);
115 const canCreatePr =
116 !isBusy &&
117 hasBranch &&
118 !hasChanges &&
119 !hasOpenPr &&
120 hasDefaultBranchDelta &&
121 !isBehind &&
122 (gitStatus.hasUpstream || canPushWithoutUpstream);
123 const canOpenPr = !isBusy && hasOpenPr;
124​
125 const commitItem: GitActionMenuItem = {
126 id: "commit",
127 label: "Commit",
128 disabled: !canCommit,
129 icon: "commit",
130 kind: "open_dialog",
131 dialogAction: "commit",
132 };
133​
134 if (!hasPrimaryRemote) {
135 return [commitItem];
136 }
137​
138 return [
139 commitItem,
140 {
141 id: "push",
142 label: "Push",
143 disabled: !canPush,
144 icon: "push",
145 kind: "open_dialog",
146 dialogAction: "push",
147 },
148 hasOpenPr
149 ? {
150 id: "pr",
151 label: `View ${terminology.shortLabel}`,
152 disabled: !canOpenPr,
153 icon: "pr",
154 kind: "open_pr",
155 }
156 : {
157 id: "pr",
158 label: `Create ${terminology.shortLabel}`,
159 disabled: !canCreatePr,
160 icon: "pr",
161 kind: "open_dialog",
162 dialogAction: "create_pr",
163 },
164 ];
166​
167export function resolveQuickAction(
168 gitStatus: VcsStatusResult | null,
169 isBusy: boolean,
170 isDefaultRef = false,
171 hasPrimaryRemote = true,
172): GitQuickAction {
173 if (isBusy) {
174 return { label: "Commit", disabled: true, kind: "show_hint", hint: "Git action in progress." };
175 }
176​
177 if (!gitStatus) {
178 return {
179 label: "Commit",
180 disabled: true,
181 kind: "show_hint",
182 hint: "Git status is unavailable.",
183 };
184 }
185​
186 const hasBranch = gitStatus.refName !== null;
187 const hasChanges = gitStatus.hasWorkingTreeChanges;
188 const hasOpenPr = gitStatus.pr?.state === "open";
189 const isAhead = gitStatus.aheadCount > 0;
190 const hasDefaultBranchDelta = (gitStatus.aheadOfDefaultCount ?? gitStatus.aheadCount) > 0;
191 const isBehind = gitStatus.behindCount > 0;
192 const isDiverged = isAhead && isBehind;
193 const terminology = resolveChangeRequestTerminology(gitStatus);
194​
195 if (!hasBranch) {
196 return {
197 label: "Commit",
198 disabled: true,
199 kind: "show_hint",
200 hint: `Create and checkout a ref before pushing or opening a ${terminology.singular}.`,
201 };
202 }
203​
204 if (hasChanges) {
205 if (!gitStatus.hasUpstream && !hasPrimaryRemote) {
206 return { label: "Commit", disabled: false, kind: "run_action", action: "commit" };
207 }
208 if (hasOpenPr || isDefaultRef) {
209 return { label: "Commit & push", disabled: false, kind: "run_action", action: "commit_push" };
210 }
211 return {
212 label: `Commit, push & ${terminology.shortLabel}`,
213 disabled: false,
214 kind: "run_action",
215 action: "commit_push_pr",
216 };
217 }
218​
219 if (!gitStatus.hasUpstream) {
220 if (!hasPrimaryRemote) {
221 if (hasOpenPr && !isAhead) {
222 return { label: `View ${terminology.shortLabel}`, disabled: false, kind: "open_pr" };
223 }
224 return {
225 label: "Publish repository",
226 disabled: false,
227 kind: "open_publish",
228 };
229 }
230 if (!isAhead) {
231 if (hasOpenPr) {
232 return { label: `View ${terminology.shortLabel}`, disabled: false, kind: "open_pr" };
233 }
234 return {
235 label: "Push",
236 disabled: true,
237 kind: "show_hint",
238 hint: "No local commits to push.",
239 };
240 }
241 if (hasOpenPr || isDefaultRef) {
242 return {
243 label: "Push",
244 disabled: false,
245 kind: "run_action",
246 action: isDefaultRef ? "commit_push" : "push",
247 };
248 }
249 return {
250 label: `Push & create ${terminology.shortLabel}`,
251 disabled: false,
252 kind: "run_action",
253 action: "create_pr",
254 };
255 }
256​
257 if (isDiverged) {
258 return {
259 label: "Sync ref",
260 disabled: true,
261 kind: "show_hint",
262 hint: "Branch has diverged from upstream. Rebase/merge first.",
263 };
264 }
265​
266 if (isBehind) {
267 return {
268 label: "Pull",
269 disabled: false,
270 kind: "run_pull",
271 };
272 }
273​
274 if (isAhead) {
275 if (hasOpenPr || isDefaultRef) {
276 return {
277 label: "Push",
278 disabled: false,
279 kind: "run_action",
280 action: isDefaultRef ? "commit_push" : "push",
281 };
282 }
283 return {
284 label: `Push & create ${terminology.shortLabel}`,
285 disabled: false,
286 kind: "run_action",
287 action: "create_pr",
288 };
289 }
290​
291 if (hasOpenPr && gitStatus.hasUpstream) {
292 return { label: `View ${terminology.shortLabel}`, disabled: false, kind: "open_pr" };
293 }
294​
295 if (hasDefaultBranchDelta && !isDefaultRef) {
296 return {
297 label: `Create ${terminology.shortLabel}`,
298 disabled: false,
299 kind: "run_action",
300 action: "create_pr",
301 };
302 }
303​
304 return {
305 label: "Commit",
306 disabled: true,
307 kind: "show_hint",
308 hint: "Branch is up to date. No action needed.",
309 };
311​
312export function requiresDefaultBranchConfirmation(
313 action: GitStackedAction,
314 isDefaultRef: boolean,
315): boolean {
316 if (!isDefaultRef) return false;
317 return (
318 action === "push" ||
319 action === "create_pr" ||
320 action === "commit_push" ||
321 action === "commit_push_pr"
322 );
324​
325export function resolveDefaultBranchActionDialogCopy(input: {
326 action: DefaultBranchConfirmableAction;
327 branchName: string;
328 includesCommit: boolean;
329 terminology?: ChangeRequestTerminology;
330}): DefaultBranchActionDialogCopy {
331 const branchLabel = input.branchName;
332 const suffix = ` on "${branchLabel}". You can continue on this ref or create a feature ref and run the same action there.`;
333 const terminology = input.terminology ?? DEFAULT_CHANGE_REQUEST_TERMINOLOGY;
334​
335 if (input.action === "push" || input.action === "commit_push") {
336 if (input.includesCommit) {
337 return {
338 title: "Commit & push to default ref?",
339 description: `This action will commit and push changes${suffix}`,
340 continueLabel: `Commit & push to ${branchLabel}`,
341 };
342 }
343 return {
344 title: "Push to default ref?",
345 description: `This action will push local commits${suffix}`,
346 continueLabel: `Push to ${branchLabel}`,
347 };
348 }
349​
350 if (input.includesCommit) {
351 return {
352 title: `Commit, push & create ${terminology.shortLabel} from default ref?`,
353 description: `This action will commit, push, and create a ${terminology.singular}${suffix}`,
354 continueLabel: `Commit, push & create ${terminology.shortLabel}`,
355 };
356 }
357 return {
358 title: `Push & create ${terminology.shortLabel} from default ref?`,
359 description: `This action will push local commits and create a ${terminology.singular}${suffix}`,
360 continueLabel: `Push & create ${terminology.shortLabel}`,
361 };
363​
364export function resolveThreadBranchUpdate(
365 result: GitRunStackedActionResult,
366): { branch: string } | null {
367 if (result.branch.status !== "created" || !result.branch.name) {
368 return null;
369 }
370​
371 return {
372 branch: result.branch.name,
373 };
375​
376export function resolveLiveThreadBranchUpdate(input: {
377 threadBranch: string | null;
378 gitStatus: VcsStatusResult | null;
379}): { branch: string | null } | null {
380 if (!input.gitStatus) {
381 return null;
382 }
383​
384 if (input.gitStatus.refName === null && input.threadBranch !== null) {
385 return null;
386 }
387​
388 if (input.threadBranch === input.gitStatus.refName) {
389 return null;
390 }
391​
392 if (
393 input.threadBranch !== null &&
394 input.gitStatus.refName !== null &&
395 !isTemporaryWorktreeBranch(input.threadBranch) &&
396 isTemporaryWorktreeBranch(input.gitStatus.refName)
397 ) {
398 return null;
399 }
400​
401 return {
402 branch: input.gitStatus.refName,
403 };
405​
406// Re-export from shared for backwards compatibility in this module's exports
407export { resolveAutoFeatureBranchName } from "@t3tools/shared/git";