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

gitActions.ts

358 lines9.1 KB

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