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

gitActions.ts

512 lines16.7 KB

1import type {
2 GitRunStackedActionInput,
3 GitStackedAction,
4 VcsStatusResult,
5} from "@t3tools/contracts";
6import { type ChangeRequestTerminology, sourceControlClients } from "../sourceControlClients.ts";
7import { mFallback, msg, select, part } from "gt-i18n";
8import type { M } from "gt-i18n";
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" | "open_pr";
20 dialogAction?: GitDialogAction;
22​
23export interface GitQuickAction {
24 label: string;
25 disabled: boolean;
26 kind: "run_action" | "run_pull" | "open_pr" | "show_hint";
27 action?: GitStackedAction;
28 hint?: string;
30​
31export interface DefaultBranchActionDialogCopy {
32 title: string;
33 description: string;
34 continueLabel: string;
36​
37export type DefaultBranchConfirmableAction =
38 | "push"
39 | "create_pr"
40 | "commit_push"
41 | "commit_push_pr";
42​
43export type GitActionRequestInput = Pick<
44 GitRunStackedActionInput,
45 "action" | "commitMessage" | "featureBranch" | "expectedBranch" | "filePaths"
46>;
47​
48export function changeRequestTerminologyFor(
49 gitStatus: VcsStatusResult | null,
50): ChangeRequestTerminology {
51 return sourceControlClients.get(gitStatus?.sourceControlProvider?.kind).changeRequest;
53​
54export function buildMenuItems(
55 gitStatus: VcsStatusResult | null,
56 isBusy: boolean,
57 hasOriginRemote = true,
58 resolve: M = mFallback,
59): GitActionMenuItem[] {
60 if (!gitStatus) return [];
61 const terminology = changeRequestTerminologyFor(gitStatus);
62​
63 const hasBranch = gitStatus.refName !== null;
64 const hasChanges = gitStatus.hasWorkingTreeChanges;
65 const hasOpenPr = gitStatus.pr?.state === "open";
66 const isBehind = gitStatus.behindCount > 0;
67 const canPushWithoutUpstream = hasOriginRemote && !gitStatus.hasUpstream;
68 const canCommit = !isBusy && hasChanges;
69 const canPush =
70 !isBusy &&
71 hasBranch &&
72 !hasChanges &&
73 !isBehind &&
74 gitStatus.aheadCount > 0 &&
75 (gitStatus.hasUpstream || canPushWithoutUpstream);
76 const canCreatePr =
77 !isBusy &&
78 hasBranch &&
79 !hasOpenPr &&
80 (gitStatus.aheadOfDefaultCount ?? gitStatus.aheadCount) > 0 &&
81 !isBehind &&
82 (gitStatus.hasUpstream || canPushWithoutUpstream);
83 const canOpenPr = !isBusy && hasOpenPr;
84​
85 return [
86 {
87 id: "commit",
88 label: resolve(msg({ context: "verb, git action" })`Commit`),
89 disabled: !canCommit,
90 icon: "commit",
91 kind: "open_dialog",
92 dialogAction: "commit",
93 },
94 {
95 id: "push",
96 label: resolve(msg({ context: "verb, git action" })`Push`),
97 disabled: !canPush,
98 icon: "push",
99 kind: "open_dialog",
100 dialogAction: "push",
101 },
102 hasOpenPr
103 ? {
104 id: "pr",
105 label: resolve(msg`View ${{ shortLabel: terminology.shortLabel }}`),
106 disabled: !canOpenPr,
107 icon: "pr",
108 kind: "open_pr",
109 }
110 : {
111 id: "pr",
112 label: resolve(msg`Create ${{ shortLabel: terminology.shortLabel }}`),
113 disabled: !canCreatePr,
114 icon: "pr",
115 kind: "open_dialog",
116 dialogAction: "create_pr",
117 },
118 ];
120​
121export function resolveQuickAction(
122 gitStatus: VcsStatusResult | null,
123 isBusy: boolean,
124 isDefaultBranch = false,
125 hasOriginRemote = true,
126 resolve: M = mFallback,
127): GitQuickAction {
128 if (isBusy) {
129 return {
130 label: resolve(msg({ context: "verb, git action" })`Commit`),
131 disabled: true,
132 kind: "show_hint",
133 hint: resolve(msg("Git action in progress.")),
134 };
135 }
136​
137 if (!gitStatus) {
138 return {
139 label: resolve(msg({ context: "verb, git action" })`Commit`),
140 disabled: true,
141 kind: "show_hint",
142 hint: resolve(msg("Git status is unavailable.")),
143 };
144 }
145​
146 const hasBranch = gitStatus.refName !== null;
147 const hasChanges = gitStatus.hasWorkingTreeChanges;
148 const hasOpenPr = gitStatus.pr?.state === "open";
149 const isAhead = gitStatus.aheadCount > 0;
150 const hasDefaultBranchDelta = (gitStatus.aheadOfDefaultCount ?? gitStatus.aheadCount) > 0;
151 const isBehind = gitStatus.behindCount > 0;
152 const isDiverged = isAhead && isBehind;
153 const terminology = changeRequestTerminologyFor(gitStatus);
154​
155 if (!hasBranch) {
156 return {
157 label: resolve(msg({ context: "verb, git action" })`Commit`),
158 disabled: true,
159 kind: "show_hint",
160 hint: resolve(
161 msg`${select(
162 { kind: changeRequestKind(terminology) },
163 {
164 pr: "Create and checkout a branch before pushing or opening a PR.",
165 mr: "Create and checkout a branch before pushing or opening an MR.",
166 generic: "Create and checkout a branch before pushing or opening a change request.",
167 other: part`Create and checkout a branch before pushing or opening ${{ changeRequest: terminology.shortLabelWithArticle }}.`,
168 },
169 )}`,
170 ),
171 };
172 }
173​
174 if (hasChanges) {
175 if (isBehind || (!gitStatus.hasUpstream && !hasOriginRemote)) {
176 return {
177 label: resolve(msg({ context: "verb, git action" })`Commit`),
178 disabled: false,
179 kind: "run_action",
180 action: "commit",
181 };
182 }
183 if (hasOpenPr || isDefaultBranch) {
184 return {
185 label: resolve(msg("Commit & push")),
186 disabled: false,
187 kind: "run_action",
188 action: "commit_push",
189 };
190 }
191 return {
192 label: resolve(msg`Commit, push & ${{ shortLabel: terminology.shortLabel }}`),
193 disabled: false,
194 kind: "run_action",
195 action: "commit_push_pr",
196 };
197 }
198​
199 if (!gitStatus.hasUpstream) {
200 if (!hasOriginRemote) {
201 if (hasOpenPr && !isAhead) {
202 return {
203 label: resolve(msg`View ${{ shortLabel: terminology.shortLabel }}`),
204 disabled: false,
205 kind: "open_pr",
206 };
207 }
208 return {
209 label: resolve(msg({ context: "verb, git action" })`Push`),
210 disabled: true,
211 kind: "show_hint",
212 hint: resolve(
213 msg`${select(
214 { kind: changeRequestKind(terminology) },
215 {
216 pr: 'Add an "origin" remote before pushing or creating a PR.',
217 mr: 'Add an "origin" remote before pushing or creating an MR.',
218 generic: 'Add an "origin" remote before pushing or creating a change request.',
219 other: part`Add an "origin" remote before pushing or creating ${{ changeRequest: terminology.shortLabelWithArticle }}.`,
220 },
221 )}`,
222 ),
223 };
224 }
225 if (!isAhead) {
226 if (hasOpenPr) {
227 return {
228 label: resolve(msg`View ${{ shortLabel: terminology.shortLabel }}`),
229 disabled: false,
230 kind: "open_pr",
231 };
232 }
233 return {
234 label: resolve(msg({ context: "verb, git action" })`Push`),
235 disabled: true,
236 kind: "show_hint",
237 hint: resolve(msg("No local commits to push.")),
238 };
239 }
240 if (hasOpenPr || isDefaultBranch) {
241 return {
242 label: resolve(msg({ context: "verb, git action" })`Push`),
243 disabled: false,
244 kind: "run_action",
245 action: "push",
246 };
247 }
248 return {
249 label: resolve(msg`Push & create ${{ shortLabel: terminology.shortLabel }}`),
250 disabled: false,
251 kind: "run_action",
252 action: "create_pr",
253 };
254 }
255​
256 if (isDiverged) {
257 return {
258 label: resolve(msg("Sync branch")),
259 disabled: true,
260 kind: "show_hint",
261 hint: resolve(msg("Branch has diverged from upstream. Rebase/merge first.")),
262 };
263 }
264​
265 if (isBehind) {
266 return {
267 label: resolve(msg({ context: "verb, git action" })`Pull`),
268 disabled: false,
269 kind: "run_pull",
270 };
271 }
272​
273 if (isAhead) {
274 if (hasOpenPr || isDefaultBranch) {
275 return {
276 label: resolve(msg({ context: "verb, git action" })`Push`),
277 disabled: false,
278 kind: "run_action",
279 action: "push",
280 };
281 }
282 return {
283 label: resolve(msg`Push & create ${{ shortLabel: terminology.shortLabel }}`),
284 disabled: false,
285 kind: "run_action",
286 action: "create_pr",
287 };
288 }
289​
290 if (hasOpenPr && gitStatus.hasUpstream) {
291 return {
292 label: resolve(msg`View ${{ shortLabel: terminology.shortLabel }}`),
293 disabled: false,
294 kind: "open_pr",
295 };
296 }
297​
298 if (hasDefaultBranchDelta && !isDefaultBranch) {
299 return {
300 label: resolve(msg`Create ${{ shortLabel: terminology.shortLabel }}`),
301 disabled: false,
302 kind: "run_action",
303 action: "create_pr",
304 };
305 }
306​
307 return {
308 label: resolve(msg({ context: "verb, git action" })`Commit`),
309 disabled: true,
310 kind: "show_hint",
311 hint: resolve(msg("Branch is up to date. No action needed.")),
312 };
314​
315export function getGitActionDisabledReason(
316 input: {
317 item: GitActionMenuItem;
318 gitStatus: VcsStatusResult | null;
319 isBusy: boolean;
320 hasOriginRemote: boolean;
321 },
322 resolve: M = mFallback,
323): string | null {
324 const { item, gitStatus, isBusy, hasOriginRemote } = input;
325 if (!item.disabled) return null;
326 if (isBusy) return resolve(msg("Git action in progress."));
327 if (!gitStatus) return resolve(msg("Git status is unavailable."));
328​
329 const hasBranch = gitStatus.refName !== null;
330 const hasChanges = gitStatus.hasWorkingTreeChanges;
331 const hasOpenPr = gitStatus.pr?.state === "open";
332 const isAhead = gitStatus.aheadCount > 0;
333 const isBehind = gitStatus.behindCount > 0;
334 const terminology = changeRequestTerminologyFor(gitStatus);
335​
336 if (item.id === "commit") {
337 if (!hasChanges) {
338 return resolve(msg("Worktree is clean. Make changes before committing."));
339 }
340 return resolve(msg("Commit is currently unavailable."));
341 }
342​
343 if (item.id === "push") {
344 if (!hasBranch) {
345 return resolve(msg("Detached HEAD: checkout a branch before pushing."));
346 }
347 if (hasChanges) {
348 return resolve(msg("Commit or stash local changes before pushing."));
349 }
350 if (isBehind) {
351 return resolve(msg("Branch is behind upstream. Pull/rebase before pushing."));
352 }
353 if (!gitStatus.hasUpstream && !hasOriginRemote) {
354 return resolve(msg('Add an "origin" remote before pushing.'));
355 }
356 if (!isAhead) {
357 return resolve(msg("No local commits to push."));
358 }
359 return resolve(msg("Push is currently unavailable."));
360 }
361​
362 if (hasOpenPr) {
363 return resolve(msg`View ${{ shortLabel: terminology.shortLabel }} is currently unavailable.`);
364 }
365 if (!hasBranch) {
366 return resolve(
367 msg`${select(
368 { kind: changeRequestKind(terminology) },
369 {
370 pr: "Detached HEAD: checkout a branch before creating a PR.",
371 mr: "Detached HEAD: checkout a branch before creating an MR.",
372 generic: "Detached HEAD: checkout a branch before creating a change request.",
373 other: part`Detached HEAD: checkout a branch before creating ${{ changeRequest: terminology.shortLabelWithArticle }}.`,
374 },
375 )}`,
376 );
377 }
378 if (!gitStatus.hasUpstream && !hasOriginRemote) {
379 return resolve(
380 msg`${select(
381 { kind: changeRequestKind(terminology) },
382 {
383 pr: 'Add an "origin" remote before creating a PR.',
384 mr: 'Add an "origin" remote before creating an MR.',
385 generic: 'Add an "origin" remote before creating a change request.',
386 other: part`Add an "origin" remote before creating ${{ changeRequest: terminology.shortLabelWithArticle }}.`,
387 },
388 )}`,
389 );
390 }
391 if ((gitStatus.aheadOfDefaultCount ?? gitStatus.aheadCount) <= 0) {
392 return resolve(
393 msg`${select(
394 { kind: changeRequestKind(terminology) },
395 {
396 pr: "No local commits to include in a PR.",
397 mr: "No local commits to include in an MR.",
398 generic: "No local commits to include in a change request.",
399 other: part`No local commits to include in ${{ changeRequest: terminology.shortLabelWithArticle }}.`,
400 },
401 )}`,
402 );
403 }
404 if (isBehind) {
405 return resolve(
406 msg`${select(
407 { kind: changeRequestKind(terminology) },
408 {
409 pr: "Branch is behind upstream. Pull/rebase before creating a PR.",
410 mr: "Branch is behind upstream. Pull/rebase before creating an MR.",
411 generic: "Branch is behind upstream. Pull/rebase before creating a change request.",
412 other: part`Branch is behind upstream. Pull/rebase before creating ${{ changeRequest: terminology.shortLabelWithArticle }}.`,
413 },
414 )}`,
415 );
416 }
417 return resolve(msg`Create ${{ shortLabel: terminology.shortLabel }} is currently unavailable.`);
419​
420/** Picks the whole sentence for the provider's term; English keeps `shortLabelWithArticle`. */
421function changeRequestKind(
422 terminology: ChangeRequestTerminology,
423): "pr" | "mr" | "generic" | "other" {
424 switch (terminology.shortLabelWithArticle) {
425 case "a PR":
426 return "pr";
427 case "an MR":
428 return "mr";
429 case "a change request":
430 return "generic";
431 default:
432 return "other";
433 }
435​
436export function requiresDefaultBranchConfirmation(
437 action: GitStackedAction,
438 isDefaultBranch: boolean,
439): boolean {
440 if (!isDefaultBranch) return false;
441 return (
442 action === "push" ||
443 action === "create_pr" ||
444 action === "commit_push" ||
445 action === "commit_push_pr"
446 );
448​
449export function resolveDefaultBranchActionDialogCopy(
450 input: {
451 action: DefaultBranchConfirmableAction;
452 branchName: string;
453 includesCommit: boolean;
454 terminology: ChangeRequestTerminology;
455 },
456 resolve: M = mFallback,
457): DefaultBranchActionDialogCopy {
458 const branchLabel = input.branchName;
459 const { shortLabel, shortLabelWithArticle } = input.terminology;
460​
461 if (input.action === "push" || input.action === "commit_push") {
462 if (input.includesCommit) {
463 return {
464 title: resolve(msg("Commit & push to default branch?")),
465 description: resolve(
466 msg`This action will commit and push changes on "${{ branch: branchLabel }}". You can continue on this branch or create a feature branch and run the same action there.`,
467 ),
468 continueLabel: resolve(msg`Commit & push to ${{ branch: branchLabel }}`),
469 };
470 }
471 return {
472 title: resolve(msg("Push to default branch?")),
473 description: resolve(
474 msg`This action will push local commits on "${{ branch: branchLabel }}". You can continue on this branch or create a feature branch and run the same action there.`,
475 ),
476 continueLabel: resolve(msg`Push to ${{ branch: branchLabel }}`),
477 };
478 }
479​
480 if (input.includesCommit) {
481 return {
482 title: resolve(msg`Commit, push & create ${{ shortLabel }} from default branch?`),
483 description: resolve(
484 msg`${select(
485 { kind: changeRequestKind(input.terminology) },
486 {
487 pr: part`This action will commit, push, and create a PR on "${{ branch: branchLabel }}". You can continue on this branch or create a feature branch and run the same action there.`,
488 mr: part`This action will commit, push, and create an MR on "${{ branch: branchLabel }}". You can continue on this branch or create a feature branch and run the same action there.`,
489 generic: part`This action will commit, push, and create a change request on "${{ branch: branchLabel }}". You can continue on this branch or create a feature branch and run the same action there.`,
490 other: part`This action will commit, push, and create ${{ changeRequest: shortLabelWithArticle }} on "${{ branch: branchLabel }}". You can continue on this branch or create a feature branch and run the same action there.`,
491 },
492 )}`,
493 ),
494 continueLabel: resolve(msg`Commit, push & create ${{ shortLabel }}`),
495 };
496 }
497 return {
498 title: resolve(msg`Push & create ${{ shortLabel }} from default branch?`),
499 description: resolve(
500 msg`${select(
501 { kind: changeRequestKind(input.terminology) },
502 {
503 pr: part`This action will push local commits and create a PR on "${{ branch: branchLabel }}". You can continue on this branch or create a feature branch and run the same action there.`,
504 mr: part`This action will push local commits and create an MR on "${{ branch: branchLabel }}". You can continue on this branch or create a feature branch and run the same action there.`,
505 generic: part`This action will push local commits and create a change request on "${{ branch: branchLabel }}". You can continue on this branch or create a feature branch and run the same action there.`,
506 other: part`This action will push local commits and create ${{ changeRequest: shortLabelWithArticle }} on "${{ branch: branchLabel }}". You can continue on this branch or create a feature branch and run the same action there.`,
507 },
508 )}`,
509 ),
510 continueLabel: resolve(msg`Push & create ${{ shortLabel }}`),
511 };