diff --git a/lib/util/git/index.spec.ts b/lib/util/git/index.spec.ts index be08b2763b2..e8792ade287 100644 --- a/lib/util/git/index.spec.ts +++ b/lib/util/git/index.spec.ts @@ -1046,6 +1046,26 @@ describe('util/git/index', { timeout: 10000 }, () => { const res = (await repo.raw(['config', 'extra.clone.config'])).trim(); expect(res).toBe('test-extra-config-value'); }); + + it('should not pass extraCloneOpts to ls-remote when local repo exists', async () => { + const extraCloneOpts = { + '-c': 'extra.clone.config=test-extra-config-value', + }; + + await fs.emptyDir(tmpDir.path); + await git.initRepo({ url: origin.path, extraCloneOpts, fullClone: true }); + await git.syncGit(); + + const rawSpy = vi.spyOn(SimpleGit.prototype, 'raw'); + + await git.initRepo({ url: origin.path, extraCloneOpts, fullClone: true }); + + expect(rawSpy).toHaveBeenCalledWith([ + 'ls-remote', + '--heads', + origin.path, + ]); + }); }); describe('setGitAuthor()', () => { diff --git a/lib/util/git/index.ts b/lib/util/git/index.ts index 982c404eec1..81ae7ce34e7 100644 --- a/lib/util/git/index.ts +++ b/lib/util/git/index.ts @@ -208,7 +208,9 @@ async function fetchBranchCommits(preferUpstream = true): Promise { preferUpstream && config.upstreamUrl ? config.upstreamUrl : config.url; logger.debug(`fetchBranchCommits(): url=${url}`); const opts = ['ls-remote', '--heads', url]; - if (config.extraCloneOpts) { + const localDir = GlobalConfig.get('localDir')!; + const repoExists = await fs.pathExists(upath.join(localDir, '.git/HEAD')); + if (config.extraCloneOpts && !repoExists) { Object.entries(config.extraCloneOpts).forEach((e) => // TODO: types (#22198) opts.unshift(e[0], `${e[1]!}`),