Open History to see the last commit
1import type { GitStackedAction } 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 preparePullRequestThread: (cwd: string | null) =>
22 ["git", "mutation", "prepare-pull-request-thread", cwd] as const,
23};
24
25export function invalidateGitQueries(queryClient: QueryClient) {
26 return queryClient.invalidateQueries({ queryKey: gitQueryKeys.all });
27}
28
29export function gitStatusQueryOptions(cwd: string | null) {
30 return queryOptions({
31 queryKey: gitQueryKeys.status(cwd),
32 queryFn: async () => {
33 const api = ensureNativeApi();
34 if (!cwd) throw new Error("Git status is unavailable.");
35 return api.git.status({ cwd });
36 },
37 enabled: cwd !== null,
38 staleTime: GIT_STATUS_STALE_TIME_MS,
39 refetchOnWindowFocus: "always",
40 refetchOnReconnect: "always",
41 refetchInterval: GIT_STATUS_REFETCH_INTERVAL_MS,
42 });
43}
44
45export function gitBranchesQueryOptions(cwd: string | null) {
46 return queryOptions({
47 queryKey: gitQueryKeys.branches(cwd),
48 queryFn: async () => {
49 const api = ensureNativeApi();
50 if (!cwd) throw new Error("Git branches are unavailable.");
51 return api.git.listBranches({ cwd });
52 },
53 enabled: cwd !== null,
54 staleTime: GIT_BRANCHES_STALE_TIME_MS,
55 refetchOnWindowFocus: true,
56 refetchOnReconnect: true,
57 refetchInterval: GIT_BRANCHES_REFETCH_INTERVAL_MS,
58 });
59}
60
61export function gitResolvePullRequestQueryOptions(input: {
62 cwd: string | null;
63 reference: string | null;
64}) {
65 return queryOptions({
66 queryKey: ["git", "pull-request", input.cwd, input.reference] as const,
67 queryFn: async () => {
68 const api = ensureNativeApi();
69 if (!input.cwd || !input.reference) {
70 throw new Error("Pull request lookup is unavailable.");
71 }
72 return api.git.resolvePullRequest({ cwd: input.cwd, reference: input.reference });
73 },
74 enabled: input.cwd !== null && input.reference !== null,
75 staleTime: 30_000,
76 refetchOnWindowFocus: false,
77 refetchOnReconnect: false,
78 });
79}
80
81export function gitInitMutationOptions(input: { cwd: string | null; queryClient: QueryClient }) {
82 return mutationOptions({
83 mutationKey: gitMutationKeys.init(input.cwd),
84 mutationFn: async () => {
85 const api = ensureNativeApi();
86 if (!input.cwd) throw new Error("Git init is unavailable.");
87 return api.git.init({ cwd: input.cwd });
88 },
89 onSuccess: async () => {
90 await invalidateGitQueries(input.queryClient);
91 },
92 });
93}
94
95export function gitCheckoutMutationOptions(input: {
96 cwd: string | null;
97 queryClient: QueryClient;
98}) {
99 return mutationOptions({
100 mutationKey: gitMutationKeys.checkout(input.cwd),
101 mutationFn: async (branch: string) => {
102 const api = ensureNativeApi();
103 if (!input.cwd) throw new Error("Git checkout is unavailable.");
104 return api.git.checkout({ cwd: input.cwd, branch });
105 },
106 onSuccess: async () => {
107 await invalidateGitQueries(input.queryClient);
108 },
109 });
110}
111
112export function gitRunStackedActionMutationOptions(input: {
113 cwd: string | null;
114 queryClient: QueryClient;
115}) {
116 return mutationOptions({
117 mutationKey: gitMutationKeys.runStackedAction(input.cwd),
118 mutationFn: async ({
119 action,
120 commitMessage,
121 featureBranch,
122 }: {
123 action: GitStackedAction;
124 commitMessage?: string;
125 featureBranch?: boolean;
126 }) => {
127 const api = ensureNativeApi();
128 if (!input.cwd) throw new Error("Git action is unavailable.");
129 return api.git.runStackedAction({
130 cwd: input.cwd,
131 action,
132 ...(commitMessage ? { commitMessage } : {}),
133 ...(featureBranch ? { featureBranch } : {}),
134 });
135 },
136 onSettled: async () => {
137 await invalidateGitQueries(input.queryClient);
138 },
139 });
140}
141
142export function gitPullMutationOptions(input: { cwd: string | null; queryClient: QueryClient }) {
143 return mutationOptions({
144 mutationKey: gitMutationKeys.pull(input.cwd),
145 mutationFn: async () => {
146 const api = ensureNativeApi();
147 if (!input.cwd) throw new Error("Git pull is unavailable.");
148 return api.git.pull({ cwd: input.cwd });
149 },
150 onSettled: async () => {
151 await invalidateGitQueries(input.queryClient);
152 },
153 });
154}
155
156export function gitCreateWorktreeMutationOptions(input: { queryClient: QueryClient }) {
157 return mutationOptions({
158 mutationFn: async ({
159 cwd,
160 branch,
161 newBranch,
162 path,
163 }: {
164 cwd: string;
165 branch: string;
166 newBranch: string;
167 path?: string | null;
168 }) => {
169 const api = ensureNativeApi();
170 if (!cwd) throw new Error("Git worktree creation is unavailable.");
171 return api.git.createWorktree({ cwd, branch, newBranch, path: path ?? null });
172 },
173 mutationKey: ["git", "mutation", "create-worktree"] as const,
174 onSettled: async () => {
175 await invalidateGitQueries(input.queryClient);
176 },
177 });
178}
179
180export function gitRemoveWorktreeMutationOptions(input: { queryClient: QueryClient }) {
181 return mutationOptions({
182 mutationFn: async ({ cwd, path, force }: { cwd: string; path: string; force?: boolean }) => {
183 const api = ensureNativeApi();
184 if (!cwd) throw new Error("Git worktree removal is unavailable.");
185 return api.git.removeWorktree({ cwd, path, force });
186 },
187 mutationKey: ["git", "mutation", "remove-worktree"] as const,
188 onSettled: async () => {
189 await invalidateGitQueries(input.queryClient);
190 },
191 });
192}
193
194export function gitPreparePullRequestThreadMutationOptions(input: {
195 cwd: string | null;
196 queryClient: QueryClient;
197}) {
198 return mutationOptions({
199 mutationFn: async ({ reference, mode }: { reference: string; mode: "local" | "worktree" }) => {
200 const api = ensureNativeApi();
201 if (!input.cwd) throw new Error("Pull request thread preparation is unavailable.");
202 return api.git.preparePullRequestThread({
203 cwd: input.cwd,
204 reference,
205 mode,
206 });
207 },
208 mutationKey: gitMutationKeys.preparePullRequestThread(input.cwd),
209 onSettled: async () => {
210 await invalidateGitQueries(input.queryClient);
211 },
212 });
213}