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
28const SHORT_SHA_LENGTH = 7;
29const TOAST_DESCRIPTION_MAX = 72;
30
31function shortenSha(sha: string | undefined): string | null {
32 if (!sha) return null;
33 return sha.slice(0, SHORT_SHA_LENGTH);
34}
35
36function truncateText(
37 value: string | undefined,
38 maxLength = TOAST_DESCRIPTION_MAX,
39): string | undefined {
40 if (!value) return undefined;
41 if (value.length <= maxLength) return value;
42 if (maxLength <= 3) return "...".slice(0, maxLength);
43 return `${value.slice(0, Math.max(0, maxLength - 3)).trimEnd()}...`;
44}
45
46export function buildGitActionProgressStages(input: {
47 action: GitStackedAction;
48 hasCustomCommitMessage: boolean;
49 hasWorkingTreeChanges: boolean;
50 forcePushOnly?: boolean;
51 pushTarget?: string;
52}): string[] {
53 const shouldIncludeCommitStages =
54 !input.forcePushOnly && (input.action === "commit" || input.hasWorkingTreeChanges);
55 const commitStages = !shouldIncludeCommitStages
56 ? []
57 : input.hasCustomCommitMessage
58 ? ["Committing..."]
59 : ["Generating commit message...", "Committing..."];
60 const pushStage = input.pushTarget ? `Pushing to ${input.pushTarget}...` : "Pushing...";
61 if (input.action === "commit") {
62 return commitStages;
63 }
64 if (input.action === "commit_push") {
65 return [...commitStages, pushStage];
66 }
67 return [...commitStages, pushStage, "Creating PR..."];
68}
69
70export function summarizeGitResult(result: GitRunStackedActionResult): {
71 title: string;
72 description?: string;
73} {
74 const withDescription = (title: string, description: string | undefined) =>
75 description ? { title, description } : { title };
76
77 if (result.pr.status === "created" || result.pr.status === "opened_existing") {
78 const prNumber = result.pr.number ? ` #${result.pr.number}` : "";
79 const title = `${result.pr.status === "created" ? "Created PR" : "Opened PR"}${prNumber}`;
80 return withDescription(title, truncateText(result.pr.title));
81 }
82
83 if (result.push.status === "pushed") {
84 const shortSha = shortenSha(result.commit.commitSha);
85 const branch = result.push.upstreamBranch ?? result.push.branch;
86 const pushedCommitPart = shortSha ? ` ${shortSha}` : "";
87 const branchPart = branch ? ` to ${branch}` : "";
88 return withDescription(
89 `Pushed${pushedCommitPart}${branchPart}`,
90 truncateText(result.commit.subject),
91 );
92 }
93
94 if (result.commit.status === "created") {
95 const shortSha = shortenSha(result.commit.commitSha);
96 const title = shortSha ? `Committed ${shortSha}` : "Committed changes";
97 return withDescription(title, truncateText(result.commit.subject));
98 }
99
100 return { title: "Done" };
101}
102
103export function buildMenuItems(
104 gitStatus: GitStatusResult | null,
105 isBusy: boolean,
106): GitActionMenuItem[] {
107 if (!gitStatus) return [];
108
109 const hasBranch = gitStatus.branch !== null;
110 const hasChanges = gitStatus.hasWorkingTreeChanges;
111 const hasOpenPr = gitStatus.openPr !== null;
112 const isBehind = gitStatus.behindCount > 0;
113 const canCommit = !isBusy && hasChanges;
114 const canPush = !isBusy && hasBranch && !hasChanges && !isBehind && gitStatus.aheadCount > 0;
115 const canCreatePr =
116 !isBusy &&
117 hasBranch &&
118 !hasChanges &&
119 !hasOpenPr &&
120 gitStatus.aheadCount > 0 &&
121 gitStatus.hasUpstream &&
122 !isBehind;
123 const canOpenPr = !isBusy && hasOpenPr;
124
125 return [
126 {
127 id: "commit",
128 label: "Commit",
129 disabled: !canCommit,
130 icon: "commit",
131 kind: "open_dialog",
132 dialogAction: "commit",
133 },
134 {
135 id: "push",
136 label: "Push",
137 disabled: !canPush,
138 icon: "push",
139 kind: "open_dialog",
140 dialogAction: "push",
141 },
142 hasOpenPr
143 ? {
144 id: "pr",
145 label: "Open PR",
146 disabled: !canOpenPr,
147 icon: "pr",
148 kind: "open_pr",
149 }
150 : {
151 id: "pr",
152 label: "Create PR",
153 disabled: !canCreatePr,
154 icon: "pr",
155 kind: "open_dialog",
156 dialogAction: "create_pr",
157 },
158 ];
159}
160
161export function resolveQuickAction(
162 gitStatus: GitStatusResult | null,
163 isBusy: boolean,
164 isDefaultBranch = false,
165): GitQuickAction {
166 if (isBusy) {
167 return { label: "Commit", disabled: true, kind: "show_hint", hint: "Git action in progress." };
168 }
169
170 if (!gitStatus) {
171 return {
172 label: "Commit",
173 disabled: true,
174 kind: "show_hint",
175 hint: "Git status is unavailable.",
176 };
177 }
178
179 const hasBranch = gitStatus.branch !== null;
180 const hasChanges = gitStatus.hasWorkingTreeChanges;
181 const hasOpenPr = gitStatus.openPr !== null;
182 const isAhead = gitStatus.aheadCount > 0;
183 const isBehind = gitStatus.behindCount > 0;
184 const isDiverged = isAhead && isBehind;
185
186 if (!hasBranch) {
187 return {
188 label: "Commit",
189 disabled: true,
190 kind: "show_hint",
191 hint: "Create and checkout a branch before pushing or opening a PR.",
192 };
193 }
194
195 if (hasChanges) {
196 if (hasOpenPr || isDefaultBranch) {
197 return { label: "Commit & push", disabled: false, kind: "run_action", action: "commit_push" };
198 }
199 return {
200 label: "Commit, push & create PR",
201 disabled: false,
202 kind: "run_action",
203 action: "commit_push_pr",
204 };
205 }
206
207 if (isDiverged) {
208 return {
209 label: "Sync branch",
210 disabled: true,
211 kind: "show_hint",
212 hint: "Branch has diverged from upstream. Rebase/merge first.",
213 };
214 }
215
216 if (isBehind) {
217 return {
218 label: "Pull",
219 disabled: false,
220 kind: "run_pull",
221 };
222 }
223
224 if (isAhead) {
225 if (hasOpenPr || isDefaultBranch) {
226 return { label: "Push", disabled: false, kind: "run_action", action: "commit_push" };
227 }
228 return {
229 label: "Push & create PR",
230 disabled: false,
231 kind: "run_action",
232 action: "commit_push_pr",
233 };
234 }
235
236 if (hasOpenPr && gitStatus.hasUpstream) {
237 return { label: "Open PR", disabled: false, kind: "open_pr" };
238 }
239
240 return {
241 label: "Commit",
242 disabled: true,
243 kind: "show_hint",
244 hint: "Branch is up to date. No action needed.",
245 };
246}
247
248export function requiresDefaultBranchConfirmation(
249 action: GitStackedAction,
250 isDefaultBranch: boolean,
251): boolean {
252 if (!isDefaultBranch) return false;
253 return action === "commit_push" || action === "commit_push_pr";
254}