Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: consider 'defaultBranch' setting when doing git clone #26810

Merged
17 changes: 17 additions & 0 deletions lib/util/git/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
8 changes: 7 additions & 1 deletion lib/util/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions lib/util/git/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type LongCommitSha = string & { __longCommitSha: never };

export interface StorageConfig {
currentBranch?: string;
defaultBranch?: string;
url: string;
extraCloneOpts?: GitOptions;
cloneSubmodules?: boolean;
Expand Down