cte 10 месяцев назад
Родитель
Сommit
37c2a1760b

+ 12 - 4
src/services/checkpoints/CheckpointService.ts

@@ -218,7 +218,7 @@ export class CheckpointService {
 			this.log(
 				`[saveCheckpoint] failed in stage stash phase: ${err instanceof Error ? err.message : String(err)}`,
 			)
-			await this.git.checkout(["-f", this.mainBranch])
+			await this.restoreMain({ branch: stashBranch, stashSha, force: true })
 			await this.git.branch(["-D", stashBranch]).catch(() => {})
 			throw err
 		}
@@ -232,19 +232,27 @@ export class CheckpointService {
 		 *   - UNDO: Create branch
 		 *   - UNDO: Change branch
 		 */
+		let stashCommit
+
 		try {
-			// TODO: Add a test to see if empty commits break this.
-			const stashCommit = await this.git.commit(message, undefined, { "--no-verify": null })
+			stashCommit = await this.git.commit(message, undefined, { "--no-verify": null })
 			this.log(`[saveCheckpoint] stashCommit: ${message} -> ${JSON.stringify(stashCommit)}`)
 		} catch (err) {
 			this.log(
 				`[saveCheckpoint] failed in stash commit phase: ${err instanceof Error ? err.message : String(err)}`,
 			)
-			await this.git.checkout(["-f", this.mainBranch])
+			await this.restoreMain({ branch: stashBranch, stashSha, force: true })
 			await this.git.branch(["-D", stashBranch]).catch(() => {})
 			throw err
 		}
 
+		if (!stashCommit) {
+			this.log("[saveCheckpoint] no stash commit")
+			await this.restoreMain({ branch: stashBranch, stashSha })
+			await this.git.branch(["-D", stashBranch])
+			return undefined
+		}
+
 		/**
 		 * PHASE: Diff
 		 * Mutations:

+ 5 - 2
src/services/checkpoints/__tests__/CheckpointService.test.ts

@@ -210,9 +210,12 @@ describe("CheckpointService", () => {
 		})
 
 		it("does not create a checkpoint if there are no pending changes", async () => {
+			const commit0 = await service.saveCheckpoint("Zeroth checkpoint")
+			expect(commit0?.commit).toBeFalsy()
+
 			await fs.writeFile(testFile, "Ahoy, world!")
-			const commit = await service.saveCheckpoint("First checkpoint")
-			expect(commit?.commit).toBeTruthy()
+			const commit1 = await service.saveCheckpoint("First checkpoint")
+			expect(commit1?.commit).toBeTruthy()
 
 			const commit2 = await service.saveCheckpoint("Second checkpoint")
 			expect(commit2?.commit).toBeFalsy()