Skip to content

Commit 3802791

Browse files
authored
Merge pull request #909 from RooVetGit/cte/delete-local-git-config-if-necessary
Delete local git config when appropriate
2 parents f3a8f82 + ffd0e97 commit 3802791

File tree

2 files changed

+71
-8
lines changed

2 files changed

+71
-8
lines changed

src/services/checkpoints/CheckpointService.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ export type CheckpointServiceOptions = {
5050
*/
5151

5252
export class CheckpointService {
53+
private static readonly USER_NAME = "Roo Code"
54+
private static readonly USER_EMAIL = "[email protected]"
55+
5356
private _currentCheckpoint?: string
5457

5558
public get currentCheckpoint() {
@@ -273,6 +276,7 @@ export class CheckpointService {
273276
log(
274277
`[CheckpointService] taskId = ${taskId}, baseDir = ${baseDir}, currentBranch = ${currentBranch}, currentSha = ${currentSha}, hiddenBranch = ${hiddenBranch}`,
275278
)
279+
276280
return new CheckpointService(taskId, git, baseDir, currentBranch, currentSha, hiddenBranch, log)
277281
}
278282

@@ -284,16 +288,33 @@ export class CheckpointService {
284288
log(`[initRepo] Initialized new Git repository at ${baseDir}`)
285289
}
286290

287-
// Only set user config if not already configured
288-
const userName = await git.getConfig("user.name")
289-
const userEmail = await git.getConfig("user.email")
291+
const globalUserName = await git.getConfig("user.name", "global")
292+
const localUserName = await git.getConfig("user.name", "local")
293+
const userName = localUserName.value || globalUserName.value
294+
295+
const globalUserEmail = await git.getConfig("user.email", "global")
296+
const localUserEmail = await git.getConfig("user.email", "local")
297+
const userEmail = localUserEmail.value || globalUserEmail.value
298+
299+
// Prior versions of this service indiscriminately set the local user
300+
// config, and it should not override the global config. To address
301+
// this we remove the local user config if it matches the default
302+
// user name and email and there's a global config.
303+
if (globalUserName.value && localUserName.value === CheckpointService.USER_NAME) {
304+
await git.raw(["config", "--unset", "--local", "user.name"])
305+
}
306+
307+
if (globalUserEmail.value && localUserEmail.value === CheckpointService.USER_EMAIL) {
308+
await git.raw(["config", "--unset", "--local", "user.email"])
309+
}
290310

291-
if (!userName.value) {
292-
await git.addConfig("user.name", "Roo Code")
311+
// Only set user config if not already configured.
312+
if (!userName) {
313+
await git.addConfig("user.name", CheckpointService.USER_NAME)
293314
}
294315

295-
if (!userEmail.value) {
296-
await git.addConfig("user.email", "[email protected]")
316+
if (!userEmail) {
317+
await git.addConfig("user.email", CheckpointService.USER_EMAIL)
297318
}
298319

299320
if (!isExistingRepo) {

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import fs from "fs/promises"
44
import path from "path"
55
import os from "os"
66

7-
import { simpleGit, SimpleGit } from "simple-git"
7+
import { simpleGit, SimpleGit, SimpleGitTaskCallback } from "simple-git"
88

99
import { CheckpointService } from "../CheckpointService"
1010

@@ -367,5 +367,47 @@ describe("CheckpointService", () => {
367367

368368
await fs.rm(baseDir, { recursive: true, force: true })
369369
})
370+
371+
it("removes local git config if it matches default and global exists", async () => {
372+
const baseDir = path.join(os.tmpdir(), `checkpoint-service-test-config2-${Date.now()}`)
373+
const repo = await initRepo({ baseDir })
374+
const newGit = repo.git
375+
376+
const originalGetConfig = newGit.getConfig.bind(newGit)
377+
378+
jest.spyOn(newGit, "getConfig").mockImplementation(
379+
(
380+
key: string,
381+
scope?: "system" | "global" | "local" | "worktree",
382+
callback?: SimpleGitTaskCallback<string>,
383+
) => {
384+
if (scope === "global") {
385+
if (key === "user.email") {
386+
return Promise.resolve({ value: "[email protected]" }) as any
387+
}
388+
if (key === "user.name") {
389+
return Promise.resolve({ value: "Global User" }) as any
390+
}
391+
}
392+
393+
return originalGetConfig(key, scope, callback)
394+
},
395+
)
396+
397+
await CheckpointService.create({ taskId, git: newGit, baseDir, log: () => {} })
398+
399+
// Verify local config was removed and global config is used.
400+
const localName = await newGit.getConfig("user.name", "local")
401+
const localEmail = await newGit.getConfig("user.email", "local")
402+
const globalName = await newGit.getConfig("user.name", "global")
403+
const globalEmail = await newGit.getConfig("user.email", "global")
404+
405+
expect(localName.value).toBeNull() // Local config should be removed.
406+
expect(localEmail.value).toBeNull()
407+
expect(globalName.value).toBe("Global User") // Global config should remain.
408+
expect(globalEmail.value).toBe("[email protected]")
409+
410+
await fs.rm(baseDir, { recursive: true, force: true })
411+
})
370412
})
371413
})

0 commit comments

Comments
 (0)