Open History to see the last commit
1import type {
2 GitRunStackedActionResult,
3 GitStackedAction,
4 GitStatusResult,
5} from "@t3tools/contracts";
6import { isTemporaryWorktreeBranch } from "@t3tools/shared/git";
7
8export type GitActionIconName = "commit" | "push" | "pr";
9
10export type GitDialogAction = "commit" | "push" | "create_pr";
11
12export interface GitActionMenuItem {
13 id: "commit" | "push" | "pr";
14 label: string;
15 disabled: boolean;
16 icon: GitActionIconName;
17 kind: "open_dialog" | "open_pr";
18 dialogAction?: GitDialogAction;
19}
20
21export interface GitQuickAction {
22 label: string;
23 disabled: boolean;
24 kind: "run_action" | "run_pull" | "open_pr" | "show_hint";
25 action?: GitStackedAction;
26 hint?: string;
27}
28
29export interface DefaultBranchActionDialogCopy {
30 title: string;
31 description: string;
32 continueLabel: string;
33}
34
35export type DefaultBranchConfirmableAction =
36 | "push"
37 | "create_pr"
38 | "commit_push"
39 | "commit_push_pr";
40
41export function buildGitActionProgressStages(input: {
42 action: GitStackedAction;
43 hasCustomCommitMessage: boolean;
44 hasWorkingTreeChanges: boolean;
45 pushTarget?: string;
46 featureBranch?: boolean;
47 shouldPushBeforePr?: boolean;
48}): string[] {
49 const branchStages = input.featureBranch ? ["Preparing feature branch..."] : [];
50 const pushStage = input.pushTarget ? `Pushing to ${input.pushTarget}...` : "Pushing...";
51 const prStages = [
52 "Preparing PR...",
53 "Generating PR content...",
54 "Creating GitHub pull request...",
55 ];
56
57 if (input.action === "push") {
58 return [pushStage];
59 }
60 if (input.action === "create_pr") {
61 return input.shouldPushBeforePr ? [pushStage, ...prStages] : prStages;
62 }
63
64 const shouldIncludeCommitStages = input.action === "commit" || input.hasWorkingTreeChanges;
65 const commitStages = !shouldIncludeCommitStages
66 ? []
67 : input.hasCustomCommitMessage
68 ? ["Committing..."]
69 : ["Generating commit message...", "Committing..."];
70 if (input.action === "commit") {
71 return [...branchStages, ...commitStages];
72 }
73 if (input.action === "commit_push") {
74 return [...branchStages, ...commitStages, pushStage];
75 }
76 return [...branchStages, ...commitStages, pushStage, ...prStages];
77}
78
79export function buildMenuItems(
80 gitStatus: GitStatusResult | null,
81 isBusy: boolean,
82 hasOriginRemote = true,
83): GitActionMenuItem[] {
84 if (!gitStatus) return [];
85
86 const hasBranch = gitStatus.branch !== null;
87 const hasChanges = gitStatus.hasWorkingTreeChanges;
88 const hasOpenPr = gitStatus.pr?.state === "open";
89 const isBehind = gitStatus.behindCount > 0;
90 const canPushWithoutUpstream = hasOriginRemote && !gitStatus.hasUpstream;
91 const canCommit = !isBusy && hasChanges;
92 const canPush =
93 !isBusy &&
94 hasBranch &&
95 !hasChanges &&
96 !isBehind &&
97 gitStatus.aheadCount > 0 &&
98 (gitStatus.hasUpstream || canPushWithoutUpstream);
99 const canCreatePr =
100 !isBusy &&
101 hasBranch &&
102 !hasChanges &&
103 !hasOpenPr &&
104 gitStatus.aheadCount > 0 &&
105 !isBehind &&
106 (gitStatus.hasUpstream || canPushWithoutUpstream);
107 const canOpenPr = !isBusy && hasOpenPr;
108
109 return [
110 {
111 id: "commit",
112 label: "Commit",
113 disabled: !canCommit,
114 icon: "commit",
115 kind: "open_dialog",
116 dialogAction: "commit",
117 },
118 {
119 id: "push",
120 label: "Push",
121 disabled: !canPush,
122 icon: "push",
123 kind: "open_dialog",
124 dialogAction: "push",
125 },
126 hasOpenPr
127 ? {
128 id: "pr",
129 label: "View PR",
130 disabled: !canOpenPr,
131 icon: "pr",
132 kind: "open_pr",
133 }
134 : {
135 id: "pr",
136 label: "Create PR",
137 disabled: !canCreatePr,
138 icon: "pr",
139 kind: "open_dialog",
140 dialogAction: "create_pr",
141 },
142 ];
143}
144
145export function resolveQuickAction(
146 gitStatus: GitStatusResult | null,
147 isBusy: boolean,
148 isDefaultBranch = false,
149 hasOriginRemote = true,
150): GitQuickAction {
151 if (isBusy) {
152 return { label: "Commit", disabled: true, kind: "show_hint", hint: "Git action in progress." };
153 }
154
155 if (!gitStatus) {
156 return {
157 label: "Commit",
158 disabled: true,
159 kind: "show_hint",
160 hint: "Git status is unavailable.",
161 };
162 }
163
164 const hasBranch = gitStatus.branch !== null;
165 const hasChanges = gitStatus.hasWorkingTreeChanges;
166 const hasOpenPr = gitStatus.pr?.state === "open";
167 const isAhead = gitStatus.aheadCount > 0;
168 const isBehind = gitStatus.behindCount > 0;
169 const isDiverged = isAhead && isBehind;
170
171 if (!hasBranch) {
172 return {
173 label: "Commit",
174 disabled: true,
175 kind: "show_hint",
176 hint: "Create and checkout a branch before pushing or opening a PR.",
177 };
178 }
179
180 if (hasChanges) {
181 if (!gitStatus.hasUpstream && !hasOriginRemote) {
182 return { label: "Commit", disabled: false, kind: "run_action", action: "commit" };
183 }
184 if (hasOpenPr || isDefaultBranch) {
185 return { label: "Commit & push", disabled: false, kind: "run_action", action: "commit_push" };
186 }
187 return {
188 label: "Commit, push & PR",
189 disabled: false,
190 kind: "run_action",
191 action: "commit_push_pr",
192 };
193 }
194
195 if (!gitStatus.hasUpstream) {
196 if (!hasOriginRemote) {
197 if (hasOpenPr && !isAhead) {
198 return { label: "View PR", disabled: false, kind: "open_pr" };
199 }
200 return {
201 label: "Push",
202 disabled: true,
203 kind: "show_hint",
204 hint: 'Add an "origin" remote before pushing or creating a PR.',
205 };
206 }
207 if (!isAhead) {
208 if (hasOpenPr) {
209 return { label: "View PR", disabled: false, kind: "open_pr" };
210 }
211 return {
212 label: "Push",
213 disabled: true,
214 kind: "show_hint",
215 hint: "No local commits to push.",
216 };
217 }
218 if (hasOpenPr || isDefaultBranch) {
219 return {
220 label: "Push",
221 disabled: false,
222 kind: "run_action",
223 action: isDefaultBranch ? "commit_push" : "push",
224 };
225 }
226 return {
227 label: "Push & create PR",
228 disabled: false,
229 kind: "run_action",
230 action: "create_pr",
231 };
232 }
233
234 if (isDiverged) {
235 return {
236 label: "Sync branch",
237 disabled: true,
238 kind: "show_hint",
239 hint: "Branch has diverged from upstream. Rebase/merge first.",
240 };
241 }
242
243 if (isBehind) {
244 return {
245 label: "Pull",
246 disabled: false,
247 kind: "run_pull",
248 };
249 }
250
251 if (isAhead) {
252 if (hasOpenPr || isDefaultBranch) {
253 return {
254 label: "Push",
255 disabled: false,
256 kind: "run_action",
257 action: isDefaultBranch ? "commit_push" : "push",
258 };
259 }
260 return {
261 label: "Push & create PR",
262 disabled: false,
263 kind: "run_action",
264 action: "create_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 };
278}
279
280export function requiresDefaultBranchConfirmation(
281 action: GitStackedAction,
282 isDefaultBranch: boolean,
283): boolean {
284 if (!isDefaultBranch) return false;
285 return (
286 action === "push" ||
287 action === "create_pr" ||
288 action === "commit_push" ||
289 action === "commit_push_pr"
290 );
291}
292
293export function resolveDefaultBranchActionDialogCopy(input: {
294 action: DefaultBranchConfirmableAction;
295 branchName: string;
296 includesCommit: boolean;
297}): DefaultBranchActionDialogCopy {
298 const branchLabel = input.branchName;
299 const suffix = ` on "${branchLabel}". You can continue on this branch or create a feature branch and run the same action there.`;
300
301 if (input.action === "push" || input.action === "commit_push") {
302 if (input.includesCommit) {
303 return {
304 title: "Commit & push to default branch?",
305 description: `This action will commit and push changes${suffix}`,
306 continueLabel: `Commit & push to ${branchLabel}`,
307 };
308 }
309 return {
310 title: "Push to default branch?",
311 description: `This action will push local commits${suffix}`,
312 continueLabel: `Push to ${branchLabel}`,
313 };
314 }
315
316 if (input.includesCommit) {
317 return {
318 title: "Commit, push & create PR from default branch?",
319 description: `This action will commit, push, and create a PR${suffix}`,
320 continueLabel: `Commit, push & create PR`,
321 };
322 }
323 return {
324 title: "Push & create PR from default branch?",
325 description: `This action will push local commits and create a PR${suffix}`,
326 continueLabel: "Push & create PR",
327 };
328}
329
330export function resolveThreadBranchUpdate(
331 result: GitRunStackedActionResult,
332): { branch: string } | null {
333 if (result.branch.status !== "created" || !result.branch.name) {
334 return null;
335 }
336
337 return {
338 branch: result.branch.name,
339 };
340}
341
342export function resolveLiveThreadBranchUpdate(input: {
343 threadBranch: string | null;
344 gitStatus: GitStatusResult | null;
345}): { branch: string | null } | null {
346 if (!input.gitStatus) {
347 return null;
348 }
349
350 if (input.gitStatus.branch === null && input.threadBranch !== null) {
351 return null;
352 }
353
354 if (input.threadBranch === input.gitStatus.branch) {
355 return null;
356 }
357
358 if (
359 input.threadBranch !== null &&
360 input.gitStatus.branch !== null &&
361 !isTemporaryWorktreeBranch(input.threadBranch) &&
362 isTemporaryWorktreeBranch(input.gitStatus.branch)
363 ) {
364 return null;
365 }
366
367 return {
368 branch: input.gitStatus.branch,
369 };
370}
371
372// Re-export from shared for backwards compatibility in this module's exports
373export { resolveAutoFeatureBranchName } from "@t3tools/shared/git";