Make checkout tolerate upstream refresh failures
- treat post-checkout upstream fetch as best effort so checkout still succeeds - route refresh fetch through `this.git(...)` for consistent behavior - add regression test for fetch-timeout during branch checkout
This commit is contained in:
1 parent
4c7720565c
commit
ca2acb078e
2 files changed
+51
-5
No files matched your search
@@ -279,6 +279,51 @@ describe("git integration", () => {
|
||||
expect(details.behindCount).toBe(1);
|
||||
});
|
||||
|
||||
it("keeps checkout successful when upstream refresh fails", async () => {
|
||||
await using remote = await makeTmpDir();
|
||||
await using source = await makeTmpDir();
|
||||
await git(remote.path, "init --bare");
|
||||
|
||||
await initRepoWithCommit(source.path);
|
||||
const defaultBranch = (await listGitBranches({ cwd: source.path })).branches.find(
|
||||
(branch) => branch.current,
|
||||
)!.name;
|
||||
await git(source.path, `remote add origin ${JSON.stringify(remote.path)}`);
|
||||
await git(source.path, `push -u origin ${defaultBranch}`);
|
||||
|
||||
const featureBranch = "feature-refresh-failure";
|
||||
await git(source.path, `branch ${featureBranch}`);
|
||||
await git(source.path, `checkout ${featureBranch}`);
|
||||
await writeFile(path.join(source.path, "feature.txt"), "feature base\n");
|
||||
await git(source.path, "add feature.txt");
|
||||
await git(source.path, "commit -m 'feature base'");
|
||||
await git(source.path, `push -u origin ${featureBranch}`);
|
||||
await git(source.path, `checkout ${defaultBranch}`);
|
||||
|
||||
class RefreshFailingGitCoreService extends GitCoreService {
|
||||
refreshFetchAttempts = 0;
|
||||
|
||||
override async git(
|
||||
cwd: string,
|
||||
args: readonly string[],
|
||||
allowNonZeroExit = false,
|
||||
): Promise<void> {
|
||||
if (args[0] === "fetch") {
|
||||
this.refreshFetchAttempts += 1;
|
||||
throw new Error("simulated fetch timeout");
|
||||
}
|
||||
await super.git(cwd, args, allowNonZeroExit);
|
||||
}
|
||||
}
|
||||
|
||||
const core = new RefreshFailingGitCoreService();
|
||||
await expect(core.checkoutBranch({ cwd: source.path, branch: featureBranch })).resolves.toBe(
|
||||
undefined,
|
||||
);
|
||||
expect(core.refreshFetchAttempts).toBe(1);
|
||||
expect(await git(source.path, "branch --show-current")).toBe(featureBranch);
|
||||
});
|
||||
|
||||
it("throws when branch does not exist", async () => {
|
||||
await using tmp = await makeTmpDir();
|
||||
await initRepoWithCommit(tmp.path);
|
||||
|
||||
@@ -574,10 +574,7 @@ export class GitCoreService {
|
||||
return;
|
||||
}
|
||||
|
||||
await executeGit(cwd, ["fetch", "--quiet", "--no-tags", remoteName], {
|
||||
timeoutMs: 20_000,
|
||||
allowNonZeroExit: true,
|
||||
});
|
||||
await this.git(cwd, ["fetch", "--quiet", "--no-tags", remoteName], true);
|
||||
}
|
||||
|
||||
async checkoutBranch(input: GitCheckoutInput): Promise<void> {
|
||||
@@ -585,7 +582,11 @@ export class GitCoreService {
|
||||
timeoutMs: 10_000,
|
||||
fallbackErrorMessage: "git checkout failed",
|
||||
});
|
||||
await this.refreshCheckedOutBranchUpstream(input.cwd);
|
||||
try {
|
||||
await this.refreshCheckedOutBranchUpstream(input.cwd);
|
||||
} catch {
|
||||
// Best effort: checkout already succeeded, so avoid surfacing refresh failures.
|
||||
}
|
||||
}
|
||||
|
||||
async initRepo(input: GitInitInput): Promise<void> {
|
||||
|
||||
Reference in new issue
Block a user