Skip to content

Commit 37c2a17

Browse files
committed
Check for empty stash commits
1 parent 6b35187 commit 37c2a17

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/services/checkpoints/CheckpointService.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export class CheckpointService {
218218
this.log(
219219
`[saveCheckpoint] failed in stage stash phase: ${err instanceof Error ? err.message : String(err)}`,
220220
)
221-
await this.git.checkout(["-f", this.mainBranch])
221+
await this.restoreMain({ branch: stashBranch, stashSha, force: true })
222222
await this.git.branch(["-D", stashBranch]).catch(() => {})
223223
throw err
224224
}
@@ -232,19 +232,27 @@ export class CheckpointService {
232232
* - UNDO: Create branch
233233
* - UNDO: Change branch
234234
*/
235+
let stashCommit
236+
235237
try {
236-
// TODO: Add a test to see if empty commits break this.
237-
const stashCommit = await this.git.commit(message, undefined, { "--no-verify": null })
238+
stashCommit = await this.git.commit(message, undefined, { "--no-verify": null })
238239
this.log(`[saveCheckpoint] stashCommit: ${message} -> ${JSON.stringify(stashCommit)}`)
239240
} catch (err) {
240241
this.log(
241242
`[saveCheckpoint] failed in stash commit phase: ${err instanceof Error ? err.message : String(err)}`,
242243
)
243-
await this.git.checkout(["-f", this.mainBranch])
244+
await this.restoreMain({ branch: stashBranch, stashSha, force: true })
244245
await this.git.branch(["-D", stashBranch]).catch(() => {})
245246
throw err
246247
}
247248

249+
if (!stashCommit) {
250+
this.log("[saveCheckpoint] no stash commit")
251+
await this.restoreMain({ branch: stashBranch, stashSha })
252+
await this.git.branch(["-D", stashBranch])
253+
return undefined
254+
}
255+
248256
/**
249257
* PHASE: Diff
250258
* Mutations:

src/services/checkpoints/__tests__/CheckpointService.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,12 @@ describe("CheckpointService", () => {
210210
})
211211

212212
it("does not create a checkpoint if there are no pending changes", async () => {
213+
const commit0 = await service.saveCheckpoint("Zeroth checkpoint")
214+
expect(commit0?.commit).toBeFalsy()
215+
213216
await fs.writeFile(testFile, "Ahoy, world!")
214-
const commit = await service.saveCheckpoint("First checkpoint")
215-
expect(commit?.commit).toBeTruthy()
217+
const commit1 = await service.saveCheckpoint("First checkpoint")
218+
expect(commit1?.commit).toBeTruthy()
216219

217220
const commit2 = await service.saveCheckpoint("Second checkpoint")
218221
expect(commit2?.commit).toBeFalsy()

0 commit comments

Comments
 (0)