Skip to content

Commit ee56d5b

Browse files
Update cached branch list after branch checkout
- add `projectGitBranchesToCurrent` helper to promote the checked-out branch and clear previous current flags - optimistically update branches query cache in checkout mutations before invalidation - add tests covering reorder, missing-branch insertion, and undefined cache behavior
1 parent 6708fe9 commit ee56d5b

2 files changed

Lines changed: 99 additions & 3 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { projectGitBranchesToCurrent } from "./gitReactQuery";
4+
5+
describe("projectGitBranchesToCurrent", () => {
6+
it("moves the checked out branch to the top and marks it current", () => {
7+
const result = projectGitBranchesToCurrent(
8+
{
9+
isRepo: true,
10+
branches: [
11+
{ name: "main", current: true, isDefault: true, worktreePath: null },
12+
{ name: "feature/a", current: false, isDefault: false, worktreePath: null },
13+
{ name: "feature/b", current: false, isDefault: false, worktreePath: null },
14+
],
15+
},
16+
"feature/a",
17+
);
18+
19+
expect(result).toEqual({
20+
isRepo: true,
21+
branches: [
22+
{ name: "feature/a", current: true, isDefault: false, worktreePath: null },
23+
{ name: "main", current: false, isDefault: true, worktreePath: null },
24+
{ name: "feature/b", current: false, isDefault: false, worktreePath: null },
25+
],
26+
});
27+
});
28+
29+
it("inserts a missing branch as current", () => {
30+
const result = projectGitBranchesToCurrent(
31+
{
32+
isRepo: true,
33+
branches: [{ name: "main", current: true, isDefault: true, worktreePath: null }],
34+
},
35+
"feature/new",
36+
);
37+
38+
expect(result).toEqual({
39+
isRepo: true,
40+
branches: [
41+
{ name: "feature/new", current: true, isDefault: false, worktreePath: null },
42+
{ name: "main", current: false, isDefault: true, worktreePath: null },
43+
],
44+
});
45+
});
46+
47+
it("returns undefined when no cached branch list exists yet", () => {
48+
expect(projectGitBranchesToCurrent(undefined, "feature/new")).toBeUndefined();
49+
});
50+
});

‎apps/web/src/lib/gitReactQuery.ts‎

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { GitStackedAction, NativeApi } from "@t3tools/contracts";
1+
import type { GitListBranchesResult, GitStackedAction, NativeApi } from "@t3tools/contracts";
22
import { mutationOptions, queryOptions, type QueryClient } from "@tanstack/react-query";
33

44
export const gitQueryKeys = {
@@ -11,6 +11,50 @@ export function invalidateGitQueries(queryClient: QueryClient) {
1111
return queryClient.invalidateQueries({ queryKey: gitQueryKeys.all });
1212
}
1313

14+
export function projectGitBranchesToCurrent(
15+
existing: GitListBranchesResult | undefined,
16+
currentBranch: string,
17+
): GitListBranchesResult | undefined {
18+
if (!existing) return existing;
19+
20+
let nextCurrent: GitListBranchesResult["branches"][number] | null = null;
21+
const remaining: GitListBranchesResult["branches"] = [];
22+
23+
for (const branch of existing.branches) {
24+
if (branch.name === currentBranch) {
25+
nextCurrent = branch.current ? branch : { ...branch, current: true };
26+
continue;
27+
}
28+
remaining.push(branch.current ? { ...branch, current: false } : branch);
29+
}
30+
31+
if (!nextCurrent) {
32+
nextCurrent = {
33+
name: currentBranch,
34+
current: true,
35+
isDefault: false,
36+
worktreePath: null,
37+
};
38+
}
39+
40+
return {
41+
...existing,
42+
branches: [nextCurrent, ...remaining],
43+
};
44+
}
45+
46+
export function setGitBranchesCurrentBranch(
47+
queryClient: QueryClient,
48+
cwd: string | null,
49+
currentBranch: string,
50+
) {
51+
if (!cwd) return;
52+
53+
queryClient.setQueryData<GitListBranchesResult>(gitQueryKeys.branches(cwd), (existing) =>
54+
projectGitBranchesToCurrent(existing, currentBranch),
55+
);
56+
}
57+
1458
export function gitStatusQueryOptions(api: NativeApi | undefined, cwd: string | null) {
1559
return queryOptions({
1660
queryKey: gitQueryKeys.status(cwd),
@@ -63,7 +107,8 @@ export function gitCheckoutMutationOptions(input: {
63107
if (!input.api || !input.cwd) throw new Error("Git checkout is unavailable.");
64108
return input.api.git.checkout({ cwd: input.cwd, branch });
65109
},
66-
onSuccess: async () => {
110+
onSuccess: async (_result, branch) => {
111+
setGitBranchesCurrentBranch(input.queryClient, input.cwd, branch);
67112
await invalidateGitQueries(input.queryClient);
68113
},
69114
});
@@ -80,7 +125,8 @@ export function gitCreateBranchAndCheckoutMutationOptions(input: {
80125
await input.api.git.createBranch({ cwd: input.cwd, branch });
81126
return input.api.git.checkout({ cwd: input.cwd, branch });
82127
},
83-
onSuccess: async () => {
128+
onSuccess: async (_result, branch) => {
129+
setGitBranchesCurrentBranch(input.queryClient, input.cwd, branch);
84130
await invalidateGitQueries(input.queryClient);
85131
},
86132
});

0 commit comments

Comments
 (0)