Open History to see the last commit
1import type { GitStackedAction, VcsRefKind, VcsStatusResult } from "@t3tools/contracts";
2import { mutationOptions, queryOptions, type QueryClient } from "@tanstack/react-query";
3import { ensureNativeApi } from "../nativeApi";
4
5const GIT_STATUS_STALE_TIME_MS = 5_000;
6const GIT_STATUS_REFETCH_INTERVAL_MS = 15_000;
7const GIT_BRANCHES_STALE_TIME_MS = 15_000;
8const GIT_BRANCHES_REFETCH_INTERVAL_MS = 60_000;
9
10export const gitQueryKeys = {
11 all: ["git"] as const,
12 status: (cwd: string | null) => ["git", "status", cwd] as const,
13 branches: (cwd: string | null) => ["git", "branches", cwd] as const,
14};
15
16export const gitMutationKeys = {
17 init: (cwd: string | null) => ["git", "mutation", "init", cwd] as const,
18 checkout: (cwd: string | null) => ["git", "mutation", "checkout", cwd] as const,
19 runStackedAction: (cwd: string | null) => ["git", "mutation", "run-stacked-action", cwd] as const,
20 pull: (cwd: string | null) => ["git", "mutation", "pull", cwd] as const,
21};
22
23export function invalidateGitQueries(queryClient: QueryClient) {
24 return queryClient.invalidateQueries({ queryKey: gitQueryKeys.all });
25}
26
27export function gitStatusQueryOptions(cwd: string | null) {
28 return queryOptions({
29 queryKey: gitQueryKeys.status(cwd),
30 queryFn: async () => {
31 const api = ensureNativeApi();
32 if (!cwd) throw new Error("VCS status is unavailable.");
33 const result = await api.vcs.status({ cwd });
34 return {
35 ...result,
36 branch: result.refName,
37 pr: result.pr
38 ? {
39 ...result.pr,
40 baseBranch: result.pr.baseRef,
41 headBranch: result.pr.headRef,
42 }
43 : null,
44 };
45 },
46 enabled: cwd !== null,
47 staleTime: GIT_STATUS_STALE_TIME_MS,
48 refetchOnWindowFocus: "always",
49 refetchOnReconnect: "always",
50 refetchInterval: GIT_STATUS_REFETCH_INTERVAL_MS,
51 });
52}
53
54export function gitBranchesQueryOptions(cwd: string | null) {
55 return queryOptions({
56 queryKey: gitQueryKeys.branches(cwd),
57 queryFn: async () => {
58 const api = ensureNativeApi();
59 if (!cwd) throw new Error("VCS refs are unavailable.");
60 const result = await api.vcs.listRefs({ cwd });
61 return {
62 ...result,
63 branches: result.refs,
64 };
65 },
66 enabled: cwd !== null,
67 staleTime: GIT_BRANCHES_STALE_TIME_MS,
68 refetchOnWindowFocus: true,
69 refetchOnReconnect: true,
70 refetchInterval: GIT_BRANCHES_REFETCH_INTERVAL_MS,
71 });
72}
73
74export function gitInitMutationOptions(input: { cwd: string | null; queryClient: QueryClient }) {
75 return mutationOptions({
76 mutationKey: gitMutationKeys.init(input.cwd),
77 mutationFn: async () => {
78 const api = ensureNativeApi();
79 if (!input.cwd) throw new Error("VCS init is unavailable.");
80 return api.vcs.init({ cwd: input.cwd });
81 },
82 onSuccess: async () => {
83 await invalidateGitQueries(input.queryClient);
84 },
85 });
86}
87
88export function gitCheckoutMutationOptions(input: {
89 cwd: string | null;
90 queryClient: QueryClient;
91}) {
92 return mutationOptions({
93 mutationKey: gitMutationKeys.checkout(input.cwd),
94 mutationFn: async (branch: string) => {
95 const api = ensureNativeApi();
96 if (!input.cwd) throw new Error("VCS checkout is unavailable.");
97 return api.vcs.checkoutRef({ cwd: input.cwd, refName: branch, refKind: "branch" });
98 },
99 onSuccess: async () => {
100 await invalidateGitQueries(input.queryClient);
101 },
102 });
103}
104
105export function gitRunStackedActionMutationOptions(input: {
106 cwd: string | null;
107 queryClient: QueryClient;
108 gitStatus?: VcsStatusResult | null;
109}) {
110 return mutationOptions({
111 mutationKey: gitMutationKeys.runStackedAction(input.cwd),
112 mutationFn: async ({
113 action,
114 commitMessage,
115 featureBranch,
116 }: {
117 action: GitStackedAction;
118 commitMessage?: string;
119 featureBranch?: boolean;
120 }) => {
121 const api = ensureNativeApi();
122 if (!input.cwd) throw new Error("Git action is unavailable.");
123 if (!input.gitStatus?.capabilities.supportsRunStackedAction) {
124 throw new Error("Git actions are unavailable for this repository backend.");
125 }
126 return api.git.runStackedAction({
127 cwd: input.cwd,
128 action,
129 ...(commitMessage ? { commitMessage } : {}),
130 ...(featureBranch ? { featureBranch } : {}),
131 });
132 },
133 onSettled: async () => {
134 await invalidateGitQueries(input.queryClient);
135 },
136 });
137}
138
139export function gitPullMutationOptions(input: { cwd: string | null; queryClient: QueryClient }) {
140 return mutationOptions({
141 mutationKey: gitMutationKeys.pull(input.cwd),
142 mutationFn: async () => {
143 const api = ensureNativeApi();
144 if (!input.cwd) throw new Error("Git pull is unavailable.");
145 return api.git.pull({ cwd: input.cwd });
146 },
147 onSettled: async () => {
148 await invalidateGitQueries(input.queryClient);
149 },
150 });
151}
152
153export function gitCreateWorktreeMutationOptions(input: { queryClient: QueryClient }) {
154 return mutationOptions({
155 mutationFn: async ({
156 cwd,
157 branch,
158 refKind,
159 newBranch,
160 path,
161 }: {
162 cwd: string;
163 branch: string;
164 refKind?: VcsRefKind;
165 newBranch?: string;
166 path?: string | null;
167 }) => {
168 const api = ensureNativeApi();
169 if (!cwd) throw new Error("Workspace creation is unavailable.");
170 const result = await api.vcs.createWorkspace({
171 cwd,
172 refName: branch,
173 refKind: refKind ?? "branch",
174 ...(newBranch ? { newRefName: newBranch } : {}),
175 path: path ?? null,
176 });
177 return {
178 ...result,
179 worktree: {
180 path: result.workspace.path,
181 branch: result.workspace.refName,
182 },
183 };
184 },
185 mutationKey: ["git", "mutation", "create-worktree"] as const,
186 onSettled: async () => {
187 await invalidateGitQueries(input.queryClient);
188 },
189 });
190}
191
192export function gitRemoveWorktreeMutationOptions(input: { queryClient: QueryClient }) {
193 return mutationOptions({
194 mutationFn: async ({ cwd, path, force }: { cwd: string; path: string; force?: boolean }) => {
195 const api = ensureNativeApi();
196 if (!cwd) throw new Error("Workspace removal is unavailable.");
197 return api.vcs.removeWorkspace({ cwd, path, force });
198 },
199 mutationKey: ["git", "mutation", "remove-worktree"] as const,
200 onSettled: async () => {
201 await invalidateGitQueries(input.queryClient);
202 },
203 });
204}