@devries-cameron20 / t3code Mirror · read-only / commits / f145a453b5
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 25, 18:30 UTC

Refresh branch upstream after checkout and invalidate git queries

- fetch tracked remote after checkout to update ahead/behind counts - add integration test for behind count when remote branch advances - invalidate git React Query data on `turn/completed` events in the web app

Julius Marminge committed Feb 15, 2026 at 00:25 UTC f145a453b56828af4ad61263ca267ebaeec5a141
3 files changed +75 -2
apps/server/src/git.test.ts
+40
@@ -239,6 +239,46 @@ describe("git integration", () => {
239 expect(current!.name).toBe("feature");
240 });
241
242 + it("refreshes upstream behind count after checkout when remote branch advanced", async () => {
243 + await using remote = await makeTmpDir();
244 + await using source = await makeTmpDir();
245 + await using clone = await makeTmpDir();
246 + await git(remote.path, "init --bare");
247 +
248 + await initRepoWithCommit(source.path);
249 + const defaultBranch = (await listGitBranches({ cwd: source.path })).branches.find(
250 + (branch) => branch.current,
251 + )!.name;
252 + await git(source.path, `remote add origin ${JSON.stringify(remote.path)}`);
253 + await git(source.path, `push -u origin ${defaultBranch}`);
254 +
255 + const featureBranch = "feature-behind";
256 + await createGitBranch({ cwd: source.path, branch: featureBranch });
257 + await checkoutGitBranch({ cwd: source.path, branch: featureBranch });
258 + await writeFile(path.join(source.path, "feature.txt"), "feature base\n");
259 + await git(source.path, "add feature.txt");
260 + await git(source.path, "commit -m 'feature base'");
261 + await git(source.path, `push -u origin ${featureBranch}`);
262 + await checkoutGitBranch({ cwd: source.path, branch: defaultBranch });
263 +
264 + await git(clone.path, `clone ${JSON.stringify(remote.path)} .`);
265 + await git(clone.path, "config user.email 'test@test.com'");
266 + await git(clone.path, "config user.name 'Test'");
267 + await git(clone.path, `checkout -b ${featureBranch} --track origin/${featureBranch}`);
268 + await writeFile(path.join(clone.path, "feature.txt"), "feature from remote\n");
269 + await git(clone.path, "add feature.txt");
270 + await git(clone.path, "commit -m 'remote feature update'");
271 + await git(clone.path, `push origin ${featureBranch}`);
272 +
273 + await checkoutGitBranch({ cwd: source.path, branch: featureBranch });
274 + const core = new GitCoreService();
275 + const details = await core.statusDetails(source.path);
276 +
277 + expect(details.branch).toBe(featureBranch);
278 + expect(details.aheadCount).toBe(0);
279 + expect(details.behindCount).toBe(1);
280 + });
281 +
282 it("throws when branch does not exist", async () => {
283 await using tmp = await makeTmpDir();
284 await initRepoWithCommit(tmp.path);
apps/server/src/git.ts
+28
@@ -553,11 +553,39 @@ export class GitCoreService {
553 });
554 }
555
556 + private async refreshCheckedOutBranchUpstream(cwd: string): Promise<void> {
557 + const upstreamRef = trimStdout(
558 + await this.gitStdout(
559 + cwd,
560 + ["rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{upstream}"],
561 + true,
562 + ),
563 + );
564 + if (upstreamRef.length === 0 || upstreamRef === "@{upstream}") {
565 + return;
566 + }
567 +
568 + const separatorIndex = upstreamRef.indexOf("/");
569 + if (separatorIndex <= 0) {
570 + return;
571 + }
572 + const remoteName = upstreamRef.slice(0, separatorIndex);
573 + if (remoteName.length === 0) {
574 + return;
575 + }
576 +
577 + await executeGit(cwd, ["fetch", "--quiet", "--no-tags", remoteName], {
578 + timeoutMs: 20_000,
579 + allowNonZeroExit: true,
580 + });
581 + }
582 +
583 async checkoutBranch(input: GitCheckoutInput): Promise<void> {
584 await executeGit(input.cwd, ["checkout", input.branch], {
585 timeoutMs: 10_000,
586 fallbackErrorMessage: "git checkout failed",
587 });
588 + await this.refreshCheckedOutBranchUpstream(input.cwd);
589 }
590
591 async initRepo(input: GitInitInput): Promise<void> {
apps/web/src/App.tsx
+7 -2
@@ -1,4 +1,4 @@
1 -import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
1 +import { QueryClient, QueryClientProvider, useQueryClient } from "@tanstack/react-query";
2 import { useEffect, useRef } from "react";
3
4 import ChatView from "./components/ChatView";
@@ -11,22 +11,27 @@ import { DEFAULT_THREAD_TERMINAL_HEIGHT, DEFAULT_THREAD_TERMINAL_ID } from "./ty
11 import { onServerWelcome } from "./wsNativeApi";
12 import { useNativeApi } from "./hooks/useNativeApi";
13 import { AnchoredToastProvider, ToastProvider } from "./components/ui/toast";
14 +import { invalidateGitQueries } from "./lib/gitReactQuery";
15
16 function EventRouter() {
17 const api = useNativeApi();
18 const { dispatch } = useStore();
19 + const queryClient = useQueryClient();
20 const activeAssistantItemRef = useRef<string | null>(null);
21
22 useEffect(() => {
23 if (!api) return;
24 return api.providers.onEvent((event) => {
25 + if (event.method === "turn/completed") {
26 + void invalidateGitQueries(queryClient);
27 + }
28 dispatch({
29 type: "APPLY_EVENT",
30 event,
31 activeAssistantItemRef,
32 });
33 });
29 - }, [api, dispatch]);
34 + }, [api, dispatch, queryClient]);
35
36 useEffect(() => {
37 if (!api) return;