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

GitActionsControl.logic.ts

363 lines9.1 KB

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