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

GitActionsControl.logic.ts

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