Open History to see the last commit
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;
18}
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;
26}
27
28export interface DefaultBranchActionDialogCopy {
29 title: string;
30 description: string;
31 continueLabel: string;
32}
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);
42}
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()}...`;
52}
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..."];
78}
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" };
111}
112
113export function buildMenuItems(
114 gitStatus: GitStatusResult | null,
115 isBusy: boolean,
116 hasOriginRemote = true,
117): GitActionMenuItem[] {
118 if (!gitStatus) return [];
119
120 const hasBranch = gitStatus.branch !== null;
121 const hasChanges = gitStatus.hasWorkingTreeChanges;
122 const hasOpenPr = gitStatus.pr?.state === "open";
123 const isBehind = gitStatus.behindCount > 0;
124 const canPushWithoutUpstream = hasOriginRemote && !gitStatus.hasUpstream;
125 const canCommit = !isBusy && hasChanges;
126 const canPush =
127 !isBusy &&
128 hasBranch &&
129 !hasChanges &&
130 !isBehind &&
131 gitStatus.aheadCount > 0 &&
132 (gitStatus.hasUpstream || canPushWithoutUpstream);
133 const canCreatePr =
134 !isBusy &&
135 hasBranch &&
136 !hasChanges &&
137 !hasOpenPr &&
138 gitStatus.aheadCount > 0 &&
139 !isBehind &&
140 (gitStatus.hasUpstream || canPushWithoutUpstream);
141 const canOpenPr = !isBusy && hasOpenPr;
142
143 return [
144 {
145 id: "commit",
146 label: "Commit",
147 disabled: !canCommit,
148 icon: "commit",
149 kind: "open_dialog",
150 dialogAction: "commit",
151 },
152 {
153 id: "push",
154 label: "Push",
155 disabled: !canPush,
156 icon: "push",
157 kind: "open_dialog",
158 dialogAction: "push",
159 },
160 hasOpenPr
161 ? {
162 id: "pr",
163 label: "View PR",
164 disabled: !canOpenPr,
165 icon: "pr",
166 kind: "open_pr",
167 }
168 : {
169 id: "pr",
170 label: "Create PR",
171 disabled: !canCreatePr,
172 icon: "pr",
173 kind: "open_dialog",
174 dialogAction: "create_pr",
175 },
176 ];
177}
178
179export function resolveQuickAction(
180 gitStatus: GitStatusResult | null,
181 isBusy: boolean,
182 isDefaultBranch = false,
183 hasOriginRemote = true,
184): GitQuickAction {
185 if (isBusy) {
186 return { label: "Commit", disabled: true, kind: "show_hint", hint: "Git action in progress." };
187 }
188
189 if (!gitStatus) {
190 return {
191 label: "Commit",
192 disabled: true,
193 kind: "show_hint",
194 hint: "Git status is unavailable.",
195 };
196 }
197
198 const hasBranch = gitStatus.branch !== null;
199 const hasChanges = gitStatus.hasWorkingTreeChanges;
200 const hasOpenPr = gitStatus.pr?.state === "open";
201 const isAhead = gitStatus.aheadCount > 0;
202 const isBehind = gitStatus.behindCount > 0;
203 const isDiverged = isAhead && isBehind;
204
205 if (!hasBranch) {
206 return {
207 label: "Commit",
208 disabled: true,
209 kind: "show_hint",
210 hint: "Create and checkout a branch before pushing or opening a PR.",
211 };
212 }
213
214 if (hasChanges) {
215 if (!gitStatus.hasUpstream && !hasOriginRemote) {
216 return { label: "Commit", disabled: false, kind: "run_action", action: "commit" };
217 }
218 if (hasOpenPr || isDefaultBranch) {
219 return { label: "Commit & push", disabled: false, kind: "run_action", action: "commit_push" };
220 }
221 return {
222 label: "Commit, push & PR",
223 disabled: false,
224 kind: "run_action",
225 action: "commit_push_pr",
226 };
227 }
228
229 if (!gitStatus.hasUpstream) {
230 if (!hasOriginRemote) {
231 if (hasOpenPr && !isAhead) {
232 return { label: "View PR", disabled: false, kind: "open_pr" };
233 }
234 return {
235 label: "Push",
236 disabled: true,
237 kind: "show_hint",
238 hint: 'Add an "origin" remote before pushing or creating a PR.',
239 };
240 }
241 if (!isAhead) {
242 if (hasOpenPr) {
243 return { label: "View PR", disabled: false, kind: "open_pr" };
244 }
245 return {
246 label: "Push",
247 disabled: true,
248 kind: "show_hint",
249 hint: "No local commits to push.",
250 };
251 }
252 if (hasOpenPr || isDefaultBranch) {
253 return { label: "Push", disabled: false, kind: "run_action", action: "commit_push" };
254 }
255 return {
256 label: "Push & create PR",
257 disabled: false,
258 kind: "run_action",
259 action: "commit_push_pr",
260 };
261 }
262
263 if (isDiverged) {
264 return {
265 label: "Sync branch",
266 disabled: true,
267 kind: "show_hint",
268 hint: "Branch has diverged from upstream. Rebase/merge first.",
269 };
270 }
271
272 if (isBehind) {
273 return {
274 label: "Pull",
275 disabled: false,
276 kind: "run_pull",
277 };
278 }
279
280 if (isAhead) {
281 if (hasOpenPr || isDefaultBranch) {
282 return { label: "Push", disabled: false, kind: "run_action", action: "commit_push" };
283 }
284 return {
285 label: "Push & create PR",
286 disabled: false,
287 kind: "run_action",
288 action: "commit_push_pr",
289 };
290 }
291
292 if (hasOpenPr && gitStatus.hasUpstream) {
293 return { label: "View PR", disabled: false, kind: "open_pr" };
294 }
295
296 return {
297 label: "Commit",
298 disabled: true,
299 kind: "show_hint",
300 hint: "Branch is up to date. No action needed.",
301 };
302}
303
304export function requiresDefaultBranchConfirmation(
305 action: GitStackedAction,
306 isDefaultBranch: boolean,
307): boolean {
308 if (!isDefaultBranch) return false;
309 return action === "commit_push" || action === "commit_push_pr";
310}
311
312export function resolveDefaultBranchActionDialogCopy(input: {
313 action: DefaultBranchConfirmableAction;
314 branchName: string;
315 includesCommit: boolean;
316}): DefaultBranchActionDialogCopy {
317 const branchLabel = input.branchName;
318 const suffix = ` on "${branchLabel}". You can continue on this branch or create a feature branch and run the same action there.`;
319
320 if (input.action === "commit_push") {
321 if (input.includesCommit) {
322 return {
323 title: "Commit & push to default branch?",
324 description: `This action will commit and push changes${suffix}`,
325 continueLabel: `Commit & push to ${branchLabel}`,
326 };
327 }
328 return {
329 title: "Push to default branch?",
330 description: `This action will push local commits${suffix}`,
331 continueLabel: `Push to ${branchLabel}`,
332 };
333 }
334
335 if (input.includesCommit) {
336 return {
337 title: "Commit, push & create PR from default branch?",
338 description: `This action will commit, push, and create a PR${suffix}`,
339 continueLabel: `Commit, push & create PR`,
340 };
341 }
342 return {
343 title: "Push & create PR from default branch?",
344 description: `This action will push local commits and create a PR${suffix}`,
345 continueLabel: "Push & create PR",
346 };
347}
348
349// Re-export from shared for backwards compatibility in this module's exports
350export { resolveAutoFeatureBranchName } from "@t3tools/shared/git";