Skip to content

Commit 96de0c2

Browse files
committed
Fix tests
1 parent 4fb86d7 commit 96de0c2

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

src/core/Cline.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3411,8 +3411,15 @@ export class Cline {
34113411

34123412
service.on("initialize", () => {
34133413
try {
3414+
const isCheckpointNeeded =
3415+
typeof this.clineMessages.find(({ say }) => say === "checkpoint_saved") === "undefined"
3416+
34143417
this.checkpointService = service
3415-
this.checkpointSave()
3418+
3419+
if (isCheckpointNeeded) {
3420+
log("[Cline#initializeCheckpoints] no checkpoints found, saving initial checkpoint")
3421+
this.checkpointSave()
3422+
}
34163423
} catch (err) {
34173424
log("[Cline#initializeCheckpoints] caught error in on('initialize'), disabling checkpoints")
34183425
this.enableCheckpoints = false

src/services/checkpoints/RepoPerWorkspaceCheckpointService.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,26 @@ export class RepoPerWorkspaceCheckpointService extends ShadowCheckpointService {
3131
}
3232

3333
override async initShadowGit() {
34-
await super.initShadowGit()
34+
const result = await super.initShadowGit()
3535
await this.checkoutTaskBranch()
36+
return result
37+
}
38+
39+
override async saveCheckpoint(message: string) {
40+
await this.checkoutTaskBranch()
41+
return super.saveCheckpoint(message)
3642
}
3743

3844
override async restoreCheckpoint(commitHash: string) {
3945
await this.checkoutTaskBranch()
4046
await super.restoreCheckpoint(commitHash)
4147
}
4248

49+
override async getDiff({ from, to }: { from?: string; to?: string }) {
50+
await this.checkoutTaskBranch()
51+
return super.getDiff({ from, to })
52+
}
53+
4354
public static create({ taskId, workspaceDir, shadowDir, log = console.log }: CheckpointServiceOptions) {
4455
const workspaceHash = crypto.createHash("sha256").update(workspaceDir).digest("hex").toString()
4556

src/services/checkpoints/ShadowCheckpointService.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export abstract class ShadowCheckpointService extends EventEmitter {
5757

5858
public async initShadowGit() {
5959
if (this.git) {
60-
return
60+
throw new Error("Shadow git repo already initialized")
6161
}
6262

6363
await fs.mkdir(this.checkpointsDir, { recursive: true })
@@ -119,7 +119,7 @@ export abstract class ShadowCheckpointService extends EventEmitter {
119119
await fs.mkdir(path.join(this.dotGitDir, "info"), { recursive: true })
120120
const excludesPath = path.join(this.dotGitDir, "info", "exclude")
121121
await fs.writeFile(excludesPath, [...GIT_EXCLUDES, ...lfsPatterns].join("\n"))
122-
await this.stageAll()
122+
await this.stageAll(git)
123123
const { commit } = await git.commit("initial commit", { "--allow-empty": null })
124124
this.baseHash = commit
125125
this.log(`[${this.constructor.name}#initShadowGit] base commit is ${commit}`)
@@ -139,18 +139,16 @@ export abstract class ShadowCheckpointService extends EventEmitter {
139139
created,
140140
duration,
141141
})
142-
}
143142

144-
private async stageAll() {
145-
if (!this.git) {
146-
throw new Error("Shadow git repo not initialized")
147-
}
143+
return { created, duration }
144+
}
148145

146+
private async stageAll(git: SimpleGit) {
149147
// await writeExcludesFile(gitPath, await getLfsPatterns(this.cwd)).
150148
await this.renameNestedGitRepos(true)
151149

152150
try {
153-
await this.git.add(".")
151+
await git.add(".")
154152
} catch (error) {
155153
this.log(
156154
`[${this.constructor.name}#stageAll] failed to add files to git: ${error instanceof Error ? error.message : String(error)}`,
@@ -222,7 +220,7 @@ export abstract class ShadowCheckpointService extends EventEmitter {
222220
}
223221

224222
const startTime = Date.now()
225-
await this.stageAll()
223+
await this.stageAll(this.git)
226224
const result = await this.git.commit(message)
227225
const isFirst = this._checkpoints.length === 0
228226
const fromHash = this._checkpoints[this._checkpoints.length - 1] ?? this.baseHash!
@@ -282,7 +280,7 @@ export abstract class ShadowCheckpointService extends EventEmitter {
282280
}
283281

284282
// Stage all changes so that untracked files appear in diff summary.
285-
await this.stageAll()
283+
await this.stageAll(this.git)
286284

287285
const { files } = to ? await this.git.diffSummary([`${from}..${to}`]) : await this.git.diffSummary([from])
288286

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jest.mock("globby", () => ({
1414
globby: jest.fn().mockResolvedValue([]),
1515
}))
1616

17-
const tmpDir = path.join(os.tmpdir(), "test-ShadowCheckpointService")
17+
const tmpDir = path.join(os.tmpdir(), "CheckpointService")
1818

1919
const initRepo = async ({
2020
workspaceDir,
@@ -311,10 +311,11 @@ describe.each([
311311
expect(await fs.readFile(newTestFile, "utf-8")).toBe("Hello, world!")
312312

313313
// Ensure the git repository was initialized.
314-
const gitDir = path.join(shadowDir, "tasks", taskId, "checkpoints", ".git")
315-
await expect(fs.stat(gitDir)).rejects.toThrow()
316314
const newService = await klass.create({ taskId, shadowDir, workspaceDir, log: () => {} })
317-
await newService.initShadowGit()
315+
const { created } = await newService.initShadowGit()
316+
expect(created).toBeTruthy()
317+
318+
const gitDir = path.join(newService.checkpointsDir, ".git")
318319
expect(await fs.stat(gitDir)).toBeTruthy()
319320

320321
// Save a new checkpoint: Ahoy, world!

0 commit comments

Comments
 (0)