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