diff --git a/lib/util/git/index.spec.ts b/lib/util/git/index.spec.ts index 6b2c85027bf7fe58523a2acc517ebd7a6108214e..b97aba5402c3c561929198d418b8b18154290b35 100644 --- a/lib/util/git/index.spec.ts +++ b/lib/util/git/index.spec.ts @@ -1132,4 +1132,21 @@ describe('util/git/index', () => { expect(sha).toBe(git.getBranchCommit(defaultBranch)); }); }); + + describe('syncGit()', () => { + it('should clone a specified base branch', async () => { + tmpDir = await tmp.dir({ unsafeCleanup: true }); + GlobalConfig.set({ baseBranches: ['develop'], localDir: tmpDir.path }); + await git.initRepo({ + url: origin.path, + defaultBranch: 'develop', + }); + await git.syncGit(); + const tmpGit = Git(tmpDir.path); + const branch = ( + await tmpGit.raw(['rev-parse', '--abbrev-ref', 'HEAD']) + ).trim(); + expect(branch).toBe('develop'); + }); + }); }); diff --git a/lib/util/git/index.ts b/lib/util/git/index.ts index 36c6c12675b29520317a1dc8fa33da3db42e5455..0cad95457e4b7e0ef398c932a35db4f3d8755879 100644 --- a/lib/util/git/index.ts +++ b/lib/util/git/index.ts @@ -412,6 +412,9 @@ export async function syncGit(): Promise<void> { const cloneStart = Date.now(); try { const opts: string[] = []; + if (config.defaultBranch) { + opts.push('-b', config.defaultBranch); + } if (config.fullClone) { logger.debug('Performing full clone'); } else { @@ -467,7 +470,10 @@ export async function syncGit(): Promise<void> { } logger.warn({ err }, 'Cannot retrieve latest commit'); } - config.currentBranch = config.currentBranch || (await getDefaultBranch(git)); + config.currentBranch = + config.currentBranch ?? + config.defaultBranch ?? + (await getDefaultBranch(git)); delete getCache()?.semanticCommits; } diff --git a/lib/util/git/types.ts b/lib/util/git/types.ts index 6f4beb1c0d327157a642f25e10df41a4ab7d17ec..0dd48c4274b55fa3b13fb30429f09d7a3244641a 100644 --- a/lib/util/git/types.ts +++ b/lib/util/git/types.ts @@ -16,6 +16,7 @@ export type LongCommitSha = string & { __longCommitSha: never }; export interface StorageConfig { currentBranch?: string; + defaultBranch?: string; url: string; extraCloneOpts?: GitOptions; cloneSubmodules?: boolean;