Skip to content

Commit 5aaae6c

Browse files
git actions
1 parent 8b94f00 commit 5aaae6c

12 files changed

Lines changed: 1164 additions & 195 deletions

File tree

‎apps/server/src/git.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
createGitWorktree,
1212
initGitRepo,
1313
listGitBranches,
14+
pullGitBranch,
1415
removeGitWorktree,
1516
runTerminalCommand,
1617
} from "./git";
@@ -604,6 +605,42 @@ describe("git integration", () => {
604605
expect(skipped.status).toBe("skipped_up_to_date");
605606
});
606607

608+
it("pulls behind branch and then reports up-to-date", async () => {
609+
await using remote = await makeTmpDir();
610+
await using source = await makeTmpDir();
611+
await using clone = await makeTmpDir();
612+
await git(remote.path, "init --bare");
613+
614+
await initRepoWithCommit(source.path);
615+
const initialBranch = (await listGitBranches({ cwd: source.path })).branches.find(
616+
(branch) => branch.current,
617+
)!.name;
618+
await git(source.path, `remote add origin ${JSON.stringify(remote.path)}`);
619+
await git(source.path, `push -u origin ${initialBranch}`);
620+
621+
await git(clone.path, `clone ${JSON.stringify(remote.path)} .`);
622+
await git(clone.path, "config user.email '[email protected]'");
623+
await git(clone.path, "config user.name 'Test'");
624+
await writeFile(path.join(clone.path, "CHANGELOG.md"), "remote change\n");
625+
await git(clone.path, "add CHANGELOG.md");
626+
await git(clone.path, "commit -m 'remote update'");
627+
await git(clone.path, `push origin ${initialBranch}`);
628+
629+
const core = new GitCoreService();
630+
const pulled = await core.pullCurrentBranch(source.path);
631+
expect(pulled.status).toBe("pulled");
632+
expect((await core.statusDetails(source.path)).behindCount).toBe(0);
633+
634+
const skipped = await core.pullCurrentBranch(source.path);
635+
expect(skipped.status).toBe("skipped_up_to_date");
636+
});
637+
638+
it("top-level pullGitBranch rejects when no upstream exists", async () => {
639+
await using tmp = await makeTmpDir();
640+
await initRepoWithCommit(tmp.path);
641+
await expect(pullGitBranch({ cwd: tmp.path })).rejects.toThrow("no upstream");
642+
});
643+
607644
it("lists branches when recency lookup fails", async () => {
608645
await using tmp = await makeTmpDir();
609646
await initRepoWithCommit(tmp.path);

‎apps/server/src/git.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import os from "node:os";
33
import path from "node:path";
44

55
import {
6+
gitPullInputSchema,
7+
gitPullResultSchema,
68
gitStatusInputSchema,
79
gitStatusResultSchema,
810
type GitCheckoutInput,
@@ -12,6 +14,8 @@ import {
1214
type GitInitInput,
1315
type GitListBranchesInput,
1416
type GitListBranchesResult,
17+
type GitPullInput,
18+
type GitPullResult,
1519
type GitRemoveWorktreeInput,
1620
type GitStatusInput,
1721
type GitStatusResult,
@@ -291,6 +295,29 @@ export class GitCoreService {
291295
};
292296
}
293297

298+
async pullCurrentBranch(cwd: string): Promise<GitPullResult> {
299+
const details = await this.statusDetails(cwd);
300+
const branch = details.branch;
301+
if (!branch) {
302+
throw new Error("Cannot pull from detached HEAD.");
303+
}
304+
if (!details.hasUpstream) {
305+
throw new Error("Current branch has no upstream configured. Push with upstream first.");
306+
}
307+
const beforeSha = trimStdout(await this.gitStdout(cwd, ["rev-parse", "HEAD"], true));
308+
await executeGit(cwd, ["pull", "--ff-only"], {
309+
timeoutMs: 30_000,
310+
fallbackErrorMessage: "git pull failed",
311+
});
312+
const afterSha = trimStdout(await this.gitStdout(cwd, ["rev-parse", "HEAD"], true));
313+
const refreshed = await this.statusDetails(cwd);
314+
return gitPullResultSchema.parse({
315+
status: beforeSha.length > 0 && beforeSha === afterSha ? "skipped_up_to_date" : "pulled",
316+
branch,
317+
...(refreshed.upstreamRef ? { upstreamBranch: refreshed.upstreamRef } : {}),
318+
});
319+
}
320+
294321
async readRangeContext(cwd: string, baseBranch: string): Promise<GitRangeContext> {
295322
const range = `${baseBranch}..HEAD`;
296323
const [commitSummary, diffSummary, diffPatch] = await Promise.all([
@@ -511,6 +538,11 @@ export async function listGitBranches(input: GitListBranchesInput): Promise<GitL
511538
return defaultGitCoreService.listBranches(input);
512539
}
513540

541+
export async function pullGitBranch(raw: GitPullInput): Promise<GitPullResult> {
542+
const input = gitPullInputSchema.parse(raw);
543+
return defaultGitCoreService.pullCurrentBranch(input.cwd);
544+
}
545+
514546
export async function createGitWorktree(
515547
input: GitCreateWorktreeInput,
516548
): Promise<GitCreateWorktreeResult> {

‎apps/server/src/wsServer.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,11 @@ describe("WebSocket Server", () => {
545545
});
546546
expect(afterInit.error).toBeUndefined();
547547
expect((afterInit.result as { isRepo: boolean }).isRepo).toBe(true);
548+
549+
const pullResponse = await sendRequest(ws, WS_METHODS.gitPull, { cwd: repoCwd });
550+
expect(pullResponse.result).toBeUndefined();
551+
expect(pullResponse.error?.message).toBeDefined();
552+
expect(pullResponse.error?.message).not.toContain("Unknown method");
548553
});
549554

550555
it("responds to git.status via the git manager", async () => {

‎apps/server/src/wsServer.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
createGitWorktree,
2828
initGitRepo,
2929
listGitBranches,
30+
pullGitBranch,
3031
removeGitWorktree,
3132
} from "./git";
3233
import { TerminalManager } from "./terminalManager";
@@ -363,6 +364,9 @@ export function createServer(options: ServerOptions) {
363364
case WS_METHODS.gitStatus:
364365
return gitManager.status(request.params as never);
365366

367+
case WS_METHODS.gitPull:
368+
return pullGitBranch(request.params as never);
369+
366370
case WS_METHODS.gitRunStackedAction:
367371
return gitManager.runStackedAction(request.params as never);
368372
case WS_METHODS.gitListBranches:

0 commit comments

Comments
 (0)