From 6f37a9b0a3abc2e63ee966058d7e9cd82d569b9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mate=20Mijolovi=C4=87?= <mate.mijolovic@gmail.com> Date: Tue, 23 Jan 2024 19:10:50 +0100 Subject: [PATCH] fix: consider 'defaultBranch' setting when doing git clone (#26810) --- lib/util/git/index.spec.ts | 17 +++++++++++++++++ lib/util/git/index.ts | 8 +++++++- lib/util/git/types.ts | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/util/git/index.spec.ts b/lib/util/git/index.spec.ts index 6b2c85027b..b97aba5402 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 36c6c12675..0cad95457e 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 6f4beb1c0d..0dd48c4274 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; -- GitLab