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

gitActions.ts

440 lines11.3 KB

1import type {
2 GitRunStackedActionInput,
3 GitRunStackedActionResult,
4 GitStackedAction,
5 VcsStatusResult,
6} from "@t3tools/contracts";
7import { isTemporaryWorktreeBranch } from "@t3tools/shared/git";
8​
9export type GitActionIconName = "commit" | "push" | "pr";
10​
11export type GitDialogAction = "commit" | "push" | "create_pr";
12​
13export interface GitActionMenuItem {
14 id: "commit" | "push" | "pr";
15 label: string;
16 disabled: boolean;
17 icon: GitActionIconName;
18 kind: "open_dialog" | "open_pr";
19 dialogAction?: GitDialogAction;
21​
22export interface GitQuickAction {
23 label: string;
24 disabled: boolean;
25 kind: "run_action" | "run_pull" | "open_pr" | "show_hint";
26 action?: GitStackedAction;
27 hint?: string;
29​
30export interface DefaultBranchActionDialogCopy {
31 title: string;
32 description: string;
33 continueLabel: string;
35​
36export type DefaultBranchConfirmableAction =
37 | "push"
38 | "create_pr"
39 | "commit_push"
40 | "commit_push_pr";
41​
42export type GitActionRequestInput = Pick<
43 GitRunStackedActionInput,
44 "action" | "commitMessage" | "featureBranch" | "filePaths"
45>;
46​
47export function buildGitActionProgressStages(input: {
48 action: GitStackedAction;
49 hasCustomCommitMessage: boolean;
50 hasWorkingTreeChanges: boolean;
51 pushTarget?: string;
52 featureBranch?: boolean;
53 shouldPushBeforePr?: boolean;
54}): string[] {
55 const branchStages = input.featureBranch ? ["Preparing feature branch..."] : [];
56 const pushStage = input.pushTarget ? `Pushing to ${input.pushTarget}...` : "Pushing...";
57 const prStages = [
58 "Preparing PR...",
59 "Generating PR content...",
60 "Creating GitHub pull request...",
61 ];
62​
63 if (input.action === "push") {
64 return [pushStage];
65 }
66 if (input.action === "create_pr") {
67 return input.shouldPushBeforePr ? [pushStage, ...prStages] : prStages;
68 }
69​
70 const shouldIncludeCommitStages = input.action === "commit" || input.hasWorkingTreeChanges;
71 const commitStages = !shouldIncludeCommitStages
72 ? []
73 : input.hasCustomCommitMessage
74 ? ["Committing..."]
75 : ["Generating commit message...", "Committing..."];
76 if (input.action === "commit") {
77 return [...branchStages, ...commitStages];
78 }
79 if (input.action === "commit_push") {
80 return [...branchStages, ...commitStages, pushStage];
81 }
82 return [...branchStages, ...commitStages, pushStage, ...prStages];
84​
85export function buildMenuItems(
86 gitStatus: VcsStatusResult | null,
87 isBusy: boolean,
88 hasOriginRemote = true,
89): GitActionMenuItem[] {
90 if (!gitStatus) return [];
91​
92 const hasBranch = gitStatus.refName !== null;
93 const hasChanges = gitStatus.hasWorkingTreeChanges;
94 const hasOpenPr = gitStatus.pr?.state === "open";
95 const isBehind = gitStatus.behindCount > 0;
96 const canPushWithoutUpstream = hasOriginRemote && !gitStatus.hasUpstream;
97 const canCommit = !isBusy && hasChanges;
98 const canPush =
99 !isBusy &&
100 hasBranch &&
101 !hasChanges &&
102 !isBehind &&
103 gitStatus.aheadCount > 0 &&
104 (gitStatus.hasUpstream || canPushWithoutUpstream);
105 const canCreatePr =
106 !isBusy &&
107 hasBranch &&
108 !hasChanges &&
109 !hasOpenPr &&
110 gitStatus.aheadCount > 0 &&
111 !isBehind &&
112 (gitStatus.hasUpstream || canPushWithoutUpstream);
113 const canOpenPr = !isBusy && hasOpenPr;
114​
115 return [
116 {
117 id: "commit",
118 label: "Commit",
119 disabled: !canCommit,
120 icon: "commit",
121 kind: "open_dialog",
122 dialogAction: "commit",
123 },
124 {
125 id: "push",
126 label: "Push",
127 disabled: !canPush,
128 icon: "push",
129 kind: "open_dialog",
130 dialogAction: "push",
131 },
132 hasOpenPr
133 ? {
134 id: "pr",
135 label: "View PR",
136 disabled: !canOpenPr,
137 icon: "pr",
138 kind: "open_pr",
139 }
140 : {
141 id: "pr",
142 label: "Create PR",
143 disabled: !canCreatePr,
144 icon: "pr",
145 kind: "open_dialog",
146 dialogAction: "create_pr",
147 },
148 ];
150​
151export function resolveQuickAction(
152 gitStatus: VcsStatusResult | null,
153 isBusy: boolean,
154 isDefaultBranch = false,
155 hasOriginRemote = true,
156): GitQuickAction {
157 if (isBusy) {
158 return { label: "Commit", disabled: true, kind: "show_hint", hint: "Git action in progress." };
159 }
160​
161 if (!gitStatus) {
162 return {
163 label: "Commit",
164 disabled: true,
165 kind: "show_hint",
166 hint: "Git status is unavailable.",
167 };
168 }
169​
170 const hasBranch = gitStatus.refName !== null;
171 const hasChanges = gitStatus.hasWorkingTreeChanges;
172 const hasOpenPr = gitStatus.pr?.state === "open";
173 const isAhead = gitStatus.aheadCount > 0;
174 const isBehind = gitStatus.behindCount > 0;
175 const isDiverged = isAhead && isBehind;
176​
177 if (!hasBranch) {
178 return {
179 label: "Commit",
180 disabled: true,
181 kind: "show_hint",
182 hint: "Create and checkout a branch before pushing or opening a PR.",
183 };
184 }
185​
186 if (hasChanges) {
187 if (!gitStatus.hasUpstream && !hasOriginRemote) {
188 return { label: "Commit", disabled: false, kind: "run_action", action: "commit" };
189 }
190 if (hasOpenPr || isDefaultBranch) {
191 return { label: "Commit & push", disabled: false, kind: "run_action", action: "commit_push" };
192 }
193 return {
194 label: "Commit, push & PR",
195 disabled: false,
196 kind: "run_action",
197 action: "commit_push_pr",
198 };
199 }
200​
201 if (!gitStatus.hasUpstream) {
202 if (!hasOriginRemote) {
203 if (hasOpenPr && !isAhead) {
204 return { label: "View PR", disabled: false, kind: "open_pr" };
205 }
206 return {
207 label: "Push",
208 disabled: true,
209 kind: "show_hint",
210 hint: 'Add an "origin" remote before pushing or creating a PR.',
211 };
212 }
213 if (!isAhead) {
214 if (hasOpenPr) {
215 return { label: "View PR", disabled: false, kind: "open_pr" };
216 }
217 return {
218 label: "Push",
219 disabled: true,
220 kind: "show_hint",
221 hint: "No local commits to push.",
222 };
223 }
224 if (hasOpenPr || isDefaultBranch) {
225 return {
226 label: "Push",
227 disabled: false,
228 kind: "run_action",
229 action: isDefaultBranch ? "commit_push" : "push",
230 };
231 }
232 return {
233 label: "Push & create PR",
234 disabled: false,
235 kind: "run_action",
236 action: "create_pr",
237 };
238 }
239​
240 if (isDiverged) {
241 return {
242 label: "Sync branch",
243 disabled: true,
244 kind: "show_hint",
245 hint: "Branch has diverged from upstream. Rebase/merge first.",
246 };
247 }
248​
249 if (isBehind) {
250 return {
251 label: "Pull",
252 disabled: false,
253 kind: "run_pull",
254 };
255 }
256​
257 if (isAhead) {
258 if (hasOpenPr || isDefaultBranch) {
259 return {
260 label: "Push",
261 disabled: false,
262 kind: "run_action",
263 action: isDefaultBranch ? "commit_push" : "push",
264 };
265 }
266 return {
267 label: "Push & create PR",
268 disabled: false,
269 kind: "run_action",
270 action: "create_pr",
271 };
272 }
273​
274 if (hasOpenPr && gitStatus.hasUpstream) {
275 return { label: "View PR", disabled: false, kind: "open_pr" };
276 }
277​
278 return {
279 label: "Commit",
280 disabled: true,
281 kind: "show_hint",
282 hint: "Branch is up to date. No action needed.",
283 };
285​
286export function getGitActionDisabledReason(input: {
287 item: GitActionMenuItem;
288 gitStatus: VcsStatusResult | null;
289 isBusy: boolean;
290 hasOriginRemote: boolean;
291}): string | null {
292 const { item, gitStatus, isBusy, hasOriginRemote } = input;
293 if (!item.disabled) return null;
294 if (isBusy) return "Git action in progress.";
295 if (!gitStatus) return "Git status is unavailable.";
296​
297 const hasBranch = gitStatus.refName !== null;
298 const hasChanges = gitStatus.hasWorkingTreeChanges;
299 const hasOpenPr = gitStatus.pr?.state === "open";
300 const isAhead = gitStatus.aheadCount > 0;
301 const isBehind = gitStatus.behindCount > 0;
302​
303 if (item.id === "commit") {
304 if (!hasChanges) {
305 return "Worktree is clean. Make changes before committing.";
306 }
307 return "Commit is currently unavailable.";
308 }
309​
310 if (item.id === "push") {
311 if (!hasBranch) {
312 return "Detached HEAD: checkout a branch before pushing.";
313 }
314 if (hasChanges) {
315 return "Commit or stash local changes before pushing.";
316 }
317 if (isBehind) {
318 return "Branch is behind upstream. Pull/rebase before pushing.";
319 }
320 if (!gitStatus.hasUpstream && !hasOriginRemote) {
321 return 'Add an "origin" remote before pushing.';
322 }
323 if (!isAhead) {
324 return "No local commits to push.";
325 }
326 return "Push is currently unavailable.";
327 }
328​
329 if (hasOpenPr) {
330 return "View PR is currently unavailable.";
331 }
332 if (!hasBranch) {
333 return "Detached HEAD: checkout a branch before creating a PR.";
334 }
335 if (hasChanges) {
336 return "Commit local changes before creating a PR.";
337 }
338 if (!gitStatus.hasUpstream && !hasOriginRemote) {
339 return 'Add an "origin" remote before creating a PR.';
340 }
341 if (!isAhead) {
342 return "No local commits to include in a PR.";
343 }
344 if (isBehind) {
345 return "Branch is behind upstream. Pull/rebase before creating a PR.";
346 }
347 return "Create PR is currently unavailable.";
349​
350export function requiresDefaultBranchConfirmation(
351 action: GitStackedAction,
352 isDefaultBranch: boolean,
353): boolean {
354 if (!isDefaultBranch) return false;
355 return (
356 action === "push" ||
357 action === "create_pr" ||
358 action === "commit_push" ||
359 action === "commit_push_pr"
360 );
362​
363export function resolveDefaultBranchActionDialogCopy(input: {
364 action: DefaultBranchConfirmableAction;
365 branchName: string;
366 includesCommit: boolean;
367}): DefaultBranchActionDialogCopy {
368 const branchLabel = input.branchName;
369 const suffix = ` on "${branchLabel}". You can continue on this branch or create a feature branch and run the same action there.`;
370​
371 if (input.action === "push" || input.action === "commit_push") {
372 if (input.includesCommit) {
373 return {
374 title: "Commit & push to default branch?",
375 description: `This action will commit and push changes${suffix}`,
376 continueLabel: `Commit & push to ${branchLabel}`,
377 };
378 }
379 return {
380 title: "Push to default branch?",
381 description: `This action will push local commits${suffix}`,
382 continueLabel: `Push to ${branchLabel}`,
383 };
384 }
385​
386 if (input.includesCommit) {
387 return {
388 title: "Commit, push & create PR from default branch?",
389 description: `This action will commit, push, and create a PR${suffix}`,
390 continueLabel: "Commit, push & create PR",
391 };
392 }
393 return {
394 title: "Push & create PR from default branch?",
395 description: `This action will push local commits and create a PR${suffix}`,
396 continueLabel: "Push & create PR",
397 };
399​
400export function resolveThreadBranchUpdate(
401 result: GitRunStackedActionResult,
402): { branch: string } | null {
403 if (result.branch.status !== "created" || !result.branch.name) {
404 return null;
405 }
406​
407 return {
408 branch: result.branch.name,
409 };
411​
412export function resolveLiveThreadBranchUpdate(input: {
413 threadBranch: string | null;
414 gitStatus: VcsStatusResult | null;
415}): { branch: string | null } | null {
416 if (!input.gitStatus) {
417 return null;
418 }
419​
420 if (input.gitStatus.refName === null && input.threadBranch !== null) {
421 return null;
422 }
423​
424 if (input.threadBranch === input.gitStatus.refName) {
425 return null;
426 }
427​
428 if (
429 input.threadBranch !== null &&
430 input.gitStatus.refName !== null &&
431 !isTemporaryWorktreeBranch(input.threadBranch) &&
432 isTemporaryWorktreeBranch(input.gitStatus.refName)
433 ) {
434 return null;
435 }
436​
437 return {
438 branch: input.gitStatus.refName,
439 };