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

GitActionsControl.logic.ts

349 lines9.4 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 = "commit_push" | "commit_push_pr";
35​
36const SHORT_SHA_LENGTH = 7;
37const TOAST_DESCRIPTION_MAX = 72;
38const AUTO_FEATURE_BRANCH_PREFIX = "feature/stacked";
39​
40function shortenSha(sha: string | undefined): string | null {
41 if (!sha) return null;
42 return sha.slice(0, SHORT_SHA_LENGTH);
44​
45function truncateText(
46 value: string | undefined,
47 maxLength = TOAST_DESCRIPTION_MAX,
48): string | undefined {
49 if (!value) return undefined;
50 if (value.length <= maxLength) return value;
51 if (maxLength <= 3) return "...".slice(0, maxLength);
52 return `${value.slice(0, Math.max(0, maxLength - 3)).trimEnd()}...`;
54​
55export function buildGitActionProgressStages(input: {
56 action: GitStackedAction;
57 hasCustomCommitMessage: boolean;
58 hasWorkingTreeChanges: boolean;
59 forcePushOnly?: boolean;
60 pushTarget?: string;
61}): string[] {
62 const shouldIncludeCommitStages =
63 !input.forcePushOnly && (input.action === "commit" || input.hasWorkingTreeChanges);
64 const commitStages = !shouldIncludeCommitStages
65 ? []
66 : input.hasCustomCommitMessage
67 ? ["Committing..."]
68 : ["Generating commit message...", "Committing..."];
69 const pushStage = input.pushTarget ? `Pushing to ${input.pushTarget}...` : "Pushing...";
70 if (input.action === "commit") {
71 return commitStages;
72 }
73 if (input.action === "commit_push") {
74 return [...commitStages, pushStage];
75 }
76 return [...commitStages, pushStage, "Creating PR..."];
78​
79const withDescription = (title: string, description: string | undefined) =>
80 description ? { title, description } : { title };
81​
82export function summarizeGitResult(result: GitRunStackedActionResult): {
83 title: string;
84 description?: string;
85} {
86 if (result.pr.status === "created" || result.pr.status === "opened_existing") {
87 const prNumber = result.pr.number ? ` #${result.pr.number}` : "";
88 const title = `${result.pr.status === "created" ? "Created PR" : "Opened PR"}${prNumber}`;
89 return withDescription(title, truncateText(result.pr.title));
90 }
91​
92 if (result.push.status === "pushed") {
93 const shortSha = shortenSha(result.commit.commitSha);
94 const branch = result.push.upstreamBranch ?? result.push.branch;
95 const pushedCommitPart = shortSha ? ` ${shortSha}` : "";
96 const branchPart = branch ? ` to ${branch}` : "";
97 return withDescription(
98 `Pushed${pushedCommitPart}${branchPart}`,
99 truncateText(result.commit.subject),
100 );
101 }
102​
103 if (result.commit.status === "created") {
104 const shortSha = shortenSha(result.commit.commitSha);
105 const title = shortSha ? `Committed ${shortSha}` : "Committed changes";
106 return withDescription(title, truncateText(result.commit.subject));
107 }
108​
109 return { title: "Done" };
111​
112export function buildMenuItems(
113 gitStatus: GitStatusResult | null,
114 isBusy: boolean,
115): GitActionMenuItem[] {
116 if (!gitStatus) return [];
117​
118 const hasBranch = gitStatus.branch !== null;
119 const hasChanges = gitStatus.hasWorkingTreeChanges;
120 const hasOpenPr = gitStatus.pr?.state === "open";
121 const isBehind = gitStatus.behindCount > 0;
122 const canCommit = !isBusy && hasChanges;
123 const canPush = !isBusy && hasBranch && !hasChanges && !isBehind && gitStatus.aheadCount > 0;
124 const canCreatePr =
125 !isBusy &&
126 hasBranch &&
127 !hasChanges &&
128 !hasOpenPr &&
129 gitStatus.aheadCount > 0 &&
130 !isBehind;
131 const canOpenPr = !isBusy && hasOpenPr;
132​
133 return [
134 {
135 id: "commit",
136 label: "Commit",
137 disabled: !canCommit,
138 icon: "commit",
139 kind: "open_dialog",
140 dialogAction: "commit",
141 },
142 {
143 id: "push",
144 label: "Push",
145 disabled: !canPush,
146 icon: "push",
147 kind: "open_dialog",
148 dialogAction: "push",
149 },
150 hasOpenPr
151 ? {
152 id: "pr",
153 label: "Open PR",
154 disabled: !canOpenPr,
155 icon: "pr",
156 kind: "open_pr",
157 }
158 : {
159 id: "pr",
160 label: "Create PR",
161 disabled: !canCreatePr,
162 icon: "pr",
163 kind: "open_dialog",
164 dialogAction: "create_pr",
165 },
166 ];
168​
169export function resolveQuickAction(
170 gitStatus: GitStatusResult | null,
171 isBusy: boolean,
172 isDefaultBranch = false,
173): GitQuickAction {
174 if (isBusy) {
175 return { label: "Commit", disabled: true, kind: "show_hint", hint: "Git action in progress." };
176 }
177​
178 if (!gitStatus) {
179 return {
180 label: "Commit",
181 disabled: true,
182 kind: "show_hint",
183 hint: "Git status is unavailable.",
184 };
185 }
186​
187 const hasBranch = gitStatus.branch !== null;
188 const hasChanges = gitStatus.hasWorkingTreeChanges;
189 const hasOpenPr = gitStatus.pr?.state === "open";
190 const isAhead = gitStatus.aheadCount > 0;
191 const isBehind = gitStatus.behindCount > 0;
192 const isDiverged = isAhead && isBehind;
193​
194 if (!hasBranch) {
195 return {
196 label: "Commit",
197 disabled: true,
198 kind: "show_hint",
199 hint: "Create and checkout a branch before pushing or opening a PR.",
200 };
201 }
202​
203 if (hasChanges) {
204 if (hasOpenPr || isDefaultBranch) {
205 return { label: "Commit & push", disabled: false, kind: "run_action", action: "commit_push" };
206 }
207 return {
208 label: "Commit, push & create PR",
209 disabled: false,
210 kind: "run_action",
211 action: "commit_push_pr",
212 };
213 }
214​
215 if (!gitStatus.hasUpstream) {
216 if (!isAhead) {
217 if (hasOpenPr) {
218 return { label: "Open PR", disabled: false, kind: "open_pr" };
219 }
220 return {
221 label: "Push",
222 disabled: true,
223 kind: "show_hint",
224 hint: "No local commits to push.",
225 };
226 }
227 if (hasOpenPr || isDefaultBranch) {
228 return { label: "Push", disabled: false, kind: "run_action", action: "commit_push" };
229 }
230 return {
231 label: "Push & create PR",
232 disabled: false,
233 kind: "run_action",
234 action: "commit_push_pr",
235 };
236 }
237​
238 if (isDiverged) {
239 return {
240 label: "Sync branch",
241 disabled: true,
242 kind: "show_hint",
243 hint: "Branch has diverged from upstream. Rebase/merge first.",
244 };
245 }
246​
247 if (isBehind) {
248 return {
249 label: "Pull",
250 disabled: false,
251 kind: "run_pull",
252 };
253 }
254​
255 if (isAhead) {
256 if (hasOpenPr || isDefaultBranch) {
257 return { label: "Push", disabled: false, kind: "run_action", action: "commit_push" };
258 }
259 return {
260 label: "Push & create PR",
261 disabled: false,
262 kind: "run_action",
263 action: "commit_push_pr",
264 };
265 }
266​
267 if (hasOpenPr && gitStatus.hasUpstream) {
268 return { label: "Open 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 action === "commit_push" || action === "commit_push_pr";
286​
287export function resolveDefaultBranchActionDialogCopy(input: {
288 action: DefaultBranchConfirmableAction;
289 branchName: string;
290 includesCommit: boolean;
291}): DefaultBranchActionDialogCopy {
292 const branchLabel = input.branchName;
293 const suffix = ` on "${branchLabel}". You can continue on this branch or create a feature branch and run the same action there.`;
294​
295 if (input.action === "commit_push") {
296 if (input.includesCommit) {
297 return {
298 title: "Commit & push to default branch?",
299 description: `This action will commit and push changes${suffix}`,
300 continueLabel: `Commit & push to ${branchLabel}`,
301 };
302 }
303 return {
304 title: "Push to default branch?",
305 description: `This action will push local commits${suffix}`,
306 continueLabel: `Push to ${branchLabel}`,
307 };
308 }
309​
310 if (input.includesCommit) {
311 return {
312 title: "Commit, push & create PR from default branch?",
313 description: `This action will commit, push, and create a PR${suffix}`,
314 continueLabel: `Commit, push & create PR`,
315 };
316 }
317 return {
318 title: "Push & create PR from default branch?",
319 description: `This action will push local commits and create a PR${suffix}`,
320 continueLabel: "Push & create PR",
321 };
323​
324function formatDateForBranch(now: Date): string {
325 const year = now.getUTCFullYear();
326 const month = String(now.getUTCMonth() + 1).padStart(2, "0");
327 const day = String(now.getUTCDate()).padStart(2, "0");
328 return `${year}${month}${day}`;
330​
331export function resolveAutoFeatureBranchName(
332 existingBranchNames: readonly string[],
333 now = new Date(),
334): string {
335 const daySlug = formatDateForBranch(now);
336 const base = `${AUTO_FEATURE_BRANCH_PREFIX}-${daySlug}`;
337 const existingNames = new Set(existingBranchNames);
338​
339 if (!existingNames.has(base)) {
340 return base;
341 }
342​
343 let suffix = 2;
344 while (existingNames.has(`${base}-${suffix}`)) {
345 suffix += 1;
346 }
347​
348 return `${base}-${suffix}`;