Skip to content

Commit 23cb962

Browse files
committed
Skip stash pop when stash push creates no entry
1 parent 43a8908 commit 23cb962

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

apps/server/src/git/Layers/GitCore.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,29 @@ it.layer(TestLayer)("git integration", (it) => {
16551655
}),
16561656
);
16571657

1658+
it.effect("skips stash pop when stash push creates no entry", () =>
1659+
Effect.gen(function* () {
1660+
const tmp = yield* makeTmpDir();
1661+
const { initialBranch } = yield* initRepoWithCommit(tmp);
1662+
const core = yield* GitCore;
1663+
1664+
yield* core.createBranch({ cwd: tmp, branch: "feature" });
1665+
yield* core.checkoutBranch({ cwd: tmp, branch: "feature" });
1666+
yield* writeTextFile(path.join(tmp, "feature.txt"), "feature content\n");
1667+
yield* git(tmp, ["add", "."]);
1668+
yield* git(tmp, ["commit", "-m", "add feature file"]);
1669+
yield* core.checkoutBranch({ cwd: tmp, branch: initialBranch });
1670+
1671+
yield* core.stashAndCheckout({ cwd: tmp, branch: "feature" });
1672+
1673+
const branches = yield* core.listBranches({ cwd: tmp });
1674+
expect(branches.branches.find((b) => b.current)!.name).toBe("feature");
1675+
1676+
const stashList = yield* git(tmp, ["stash", "list"]);
1677+
expect(stashList.trim()).toBe("");
1678+
}),
1679+
);
1680+
16581681
it.effect("includes descriptive stash message", () =>
16591682
Effect.gen(function* () {
16601683
const tmp = yield* makeTmpDir();

apps/server/src/git/Layers/GitCore.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,13 +2243,20 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
22432243
{ timeoutMs: 10_000, allowNonZeroExit: true },
22442244
);
22452245
});
2246+
const readStashList = () =>
2247+
runGitStdout("GitCore.stashAndCheckout.stashList", input.cwd, ["stash", "list"]).pipe(
2248+
Effect.map((stdout) => stdout.trim()),
2249+
);
2250+
2251+
const stashListBefore = yield* readStashList();
22462252

22472253
yield* executeGit(
22482254
"GitCore.stashAndCheckout.stash",
22492255
input.cwd,
22502256
["stash", "push", "-u", "-m", `t3code: stash before switching to ${input.branch}`],
22512257
{ timeoutMs: 15_000, fallbackErrorMessage: "git stash failed" },
22522258
);
2259+
const stashEntryCreated = (yield* readStashList()) !== stashListBefore;
22532260

22542261
const checkoutExit = yield* Effect.exit(
22552262
checkoutBranch(input).pipe(
@@ -2266,6 +2273,10 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
22662273
),
22672274
);
22682275
if (Exit.isFailure(checkoutExit)) {
2276+
if (!stashEntryCreated) {
2277+
return yield* Effect.failCause(checkoutExit.cause);
2278+
}
2279+
22692280
const restoreResult = yield* executeGit(
22702281
"GitCore.stashAndCheckout.restoreOriginalBranch",
22712282
input.cwd,
@@ -2286,6 +2297,10 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
22862297
return yield* Effect.failCause(checkoutExit.cause);
22872298
}
22882299

2300+
if (!stashEntryCreated) {
2301+
return;
2302+
}
2303+
22892304
const popResult = yield* executeGit(
22902305
"GitCore.stashAndCheckout.stashPop",
22912306
input.cwd,

0 commit comments

Comments
 (0)