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