Provide a .gitconfig for shadow repos (#1294)

This commit is contained in:
Louis Jimenez 2025-06-22 00:13:03 -04:00 committed by GitHub
parent 0779697da6
commit 383212034c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 5 deletions

View File

@ -188,6 +188,18 @@ describe('GitService', () => {
const repoDir = path.join(mockHomedir, '.gemini', 'history', mockHash);
const hiddenGitIgnorePath = path.join(repoDir, '.gitignore');
const visibleGitIgnorePath = path.join(mockProjectRoot, '.gitignore');
const gitConfigPath = path.join(repoDir, '.gitconfig');
it('should create a .gitconfig file with the correct content', async () => {
const service = new GitService(mockProjectRoot);
await service.setupShadowGitRepository();
const expectedConfigContent =
'[user]\n name = Gemini CLI\n email = gemini-cli@google.com\n[commit]\n gpgsign = false\n';
expect(hoistedMockWriteFile).toHaveBeenCalledWith(
gitConfigPath,
expectedConfigContent,
);
});
it('should create history and repository directories', async () => {
const service = new GitService(mockProjectRoot);

View File

@ -54,18 +54,24 @@ export class GitService {
*/
async setupShadowGitRepository() {
const repoDir = this.getHistoryDir();
const gitConfigPath = path.join(repoDir, '.gitconfig');
await fs.mkdir(repoDir, { recursive: true });
const isRepoDefined = await simpleGit(repoDir).checkIsRepo(
CheckRepoActions.IS_REPO_ROOT,
);
// We don't want to inherit the user's name, email, or gpg signing
// preferences for the shadow repository, so we create a dedicated gitconfig.
const gitConfigContent =
'[user]\n name = Gemini CLI\n email = gemini-cli@google.com\n[commit]\n gpgsign = false\n';
await fs.writeFile(gitConfigPath, gitConfigContent);
const repo = simpleGit(repoDir);
const isRepoDefined = await repo.checkIsRepo(CheckRepoActions.IS_REPO_ROOT);
if (!isRepoDefined) {
await simpleGit(repoDir).init(false, {
await repo.init(false, {
'--initial-branch': 'main',
});
const repo = simpleGit(repoDir);
await repo.commit('Initial commit', { '--allow-empty': null });
}
@ -89,6 +95,9 @@ export class GitService {
return simpleGit(this.projectRoot).env({
GIT_DIR: path.join(repoDir, '.git'),
GIT_WORK_TREE: this.projectRoot,
// Prevent git from using the user's global git config.
HOME: repoDir,
XDG_CONFIG_HOME: repoDir,
});
}