diff --git a/lib/platform/azure/index.ts b/lib/platform/azure/index.ts index 6ab7e0f3a434821a6fabb20e2ba1dcf8e5bedfd1..6f24dba0b317350e60a53f07f5533b53d1701870 100644 --- a/lib/platform/azure/index.ts +++ b/lib/platform/azure/index.ts @@ -189,7 +189,7 @@ export /* istanbul ignore next */ async function setBaseBranch( logger.debug(`Setting baseBranch to ${branchName}`); config.baseBranch = branchName; delete config.baseCommitSHA; - const baseBranchSha = await git.setBaseBranch(branchName); + const baseBranchSha = await git.setBranch(branchName); return baseBranchSha; } diff --git a/lib/platform/bitbucket-server/index.ts b/lib/platform/bitbucket-server/index.ts index 933adbefbd92a3cd3e52b2f06e81a01bb3dee6e4..44cb2afb0160eb92213060c5cf6e3557a93de753 100644 --- a/lib/platform/bitbucket-server/index.ts +++ b/lib/platform/bitbucket-server/index.ts @@ -242,7 +242,7 @@ export async function setBaseBranch( branchName: string = config.defaultBranch ): Promise<string> { config.baseBranch = branchName; - const baseBranchSha = await git.setBaseBranch(branchName); + const baseBranchSha = await git.setBranch(branchName); return baseBranchSha; } diff --git a/lib/platform/bitbucket/index.ts b/lib/platform/bitbucket/index.ts index 0c6a45c9c3762cb5ea8bb77933a2c2fed5b5ccca..bb16658f8ba4e997a70a362745ac43cfe15bca55 100644 --- a/lib/platform/bitbucket/index.ts +++ b/lib/platform/bitbucket/index.ts @@ -182,7 +182,7 @@ export async function setBaseBranch( logger.debug(`Setting baseBranch to ${branchName}`); config.baseBranch = branchName; delete config.baseCommitSHA; - const baseBranchSha = await git.setBaseBranch(branchName); + const baseBranchSha = await git.setBranch(branchName); return baseBranchSha; } diff --git a/lib/platform/gitea/index.spec.ts b/lib/platform/gitea/index.spec.ts index d1810a68c2a42f7e17e7e1553f52caa01c0ebbeb..ab52ef1aaf99e71f72b13a00eee6e15b8bfa4013 100644 --- a/lib/platform/gitea/index.spec.ts +++ b/lib/platform/gitea/index.spec.ts @@ -390,18 +390,16 @@ describe('platform/gitea', () => { await initFakeRepo(); await gitea.setBaseBranch(); - expect(gitvcs.setBaseBranch).toHaveBeenCalledTimes(1); - expect(gitvcs.setBaseBranch).toHaveBeenCalledWith( - mockRepo.default_branch - ); + expect(gitvcs.setBranch).toHaveBeenCalledTimes(1); + expect(gitvcs.setBranch).toHaveBeenCalledWith(mockRepo.default_branch); }); it('should set custom base branch', async () => { await initFakeRepo(); await gitea.setBaseBranch('devel'); - expect(gitvcs.setBaseBranch).toHaveBeenCalledTimes(1); - expect(gitvcs.setBaseBranch).toHaveBeenCalledWith('devel'); + expect(gitvcs.setBranch).toHaveBeenCalledTimes(1); + expect(gitvcs.setBranch).toHaveBeenCalledWith('devel'); }); }); diff --git a/lib/platform/gitea/index.ts b/lib/platform/gitea/index.ts index 9936f238b9aa8f6dbb6b84f444e17f4481cdcafc..7cccb5419b624dd6a1c669948a72ccc02e9a12c1 100644 --- a/lib/platform/gitea/index.ts +++ b/lib/platform/gitea/index.ts @@ -449,7 +449,7 @@ const platform: Platform = { baseBranch: string = config.defaultBranch ): Promise<string> { config.baseBranch = baseBranch; - const baseBranchSha = await git.setBaseBranch(baseBranch); + const baseBranchSha = await git.setBranch(baseBranch); return baseBranchSha; }, diff --git a/lib/platform/github/index.ts b/lib/platform/github/index.ts index 652ff6b674874286b9cb221ba3a610d75826dc2e..abd2a86dcb73aa4c18be282988ddeda48f347f58 100644 --- a/lib/platform/github/index.ts +++ b/lib/platform/github/index.ts @@ -497,7 +497,7 @@ export async function setBaseBranch( ): Promise<string> { config.baseBranch = branchName; config.baseCommitSHA = null; - const baseBranchSha = await git.setBaseBranch(branchName); + const baseBranchSha = await git.setBranch(branchName); return baseBranchSha; } diff --git a/lib/platform/gitlab/index.ts b/lib/platform/gitlab/index.ts index cffbfa3c3b52166fa8a8a01430869662c832bef4..98f52c113e8c84305e5e3581d97f1520469c325c 100644 --- a/lib/platform/gitlab/index.ts +++ b/lib/platform/gitlab/index.ts @@ -270,7 +270,7 @@ export async function setBaseBranch( ): Promise<string> { logger.debug(`Setting baseBranch to ${branchName}`); config.baseBranch = branchName; - const baseBranchSha = await git.setBaseBranch(branchName); + const baseBranchSha = await git.setBranch(branchName); return baseBranchSha; } diff --git a/lib/util/git/index.spec.ts b/lib/util/git/index.spec.ts index 635fe2d2e97b5f05b4effb4a07c0b62f0db56ab4..f7c4e17687605d8ecbf4b87f66ca09fa3aaac7c8 100644 --- a/lib/util/git/index.spec.ts +++ b/lib/util/git/index.spec.ts @@ -69,13 +69,13 @@ describe('platform/git', () => { describe('setBaseBranch(branchName)', () => { it('sets the base branch as master', async () => { - await expect(git.setBaseBranch('master')).resolves.not.toThrow(); + await expect(git.setBranch('master')).resolves.not.toThrow(); }); it('sets non-master base branch', async () => { - await expect(git.setBaseBranch('develop')).resolves.not.toThrow(); + await expect(git.setBranch('develop')).resolves.not.toThrow(); }); it('should throw if branch does not exist', async () => { - await expect(git.setBaseBranch('not_found')).rejects.toMatchSnapshot(); + await expect(git.setBranch('not_found')).rejects.toMatchSnapshot(); }); }); describe('getFileList()', () => { @@ -338,7 +338,7 @@ describe('platform/git', () => { expect(await git.getCommitMessages()).toMatchSnapshot(); - await git.setBaseBranch('develop'); + await git.setBranch('develop'); await git.initRepo({ localDir: tmpDir.path, @@ -347,7 +347,7 @@ describe('platform/git', () => { expect(await git.branchExists('test')).toBeTruthy(); - await git.setBaseBranch('test'); + await git.setBranch('test'); const msg = await git.getCommitMessages(); expect(msg).toMatchSnapshot(); diff --git a/lib/util/git/index.ts b/lib/util/git/index.ts index 91bc12731dcd70ff91c7e96d992015adee013c34..d80a0b5e4adfffac22b59a30a0f59ec0eaac333e 100644 --- a/lib/util/git/index.ts +++ b/lib/util/git/index.ts @@ -26,7 +26,7 @@ export type DiffResult = Git.DiffResult; interface StorageConfig { localDir: string; - baseBranch?: string; + currentBranch?: string; url: string; extraCloneOpts?: Git.Options; gitAuthorName?: string; @@ -34,8 +34,8 @@ interface StorageConfig { } interface LocalConfig extends StorageConfig { - baseBranch: string; - baseBranchSha: string; + currentBranch: string; + currentBranchSha: string; branchExists: Record<string, boolean>; branchPrefix: string; } @@ -66,11 +66,11 @@ function localName(branchName: string): string { return branchName.replace(/^origin\//, ''); } -function throwBaseBranchValidationError(branchName: string): never { +function throwBranchValidationError(branchName: string): never { const error = new Error(CONFIG_VALIDATION); - error.validationError = 'baseBranch not found'; + error.validationError = 'branch not found'; error.validationMessage = - 'The following configured baseBranch could not be found: ' + branchName; + 'The following branch could not be found: ' + branchName; throw error; } @@ -159,8 +159,9 @@ export async function syncGit(): Promise<void> { await git.raw(['remote', 'set-url', 'origin', config.url]); const fetchStart = Date.now(); await git.fetch(['--depth=10']); - config.baseBranch = config.baseBranch || (await getDefaultBranch(git)); - await resetToBranch(config.baseBranch); + config.currentBranch = + config.currentBranch || (await getDefaultBranch(git)); + await resetToBranch(config.currentBranch); await cleanLocalBranches(); await git.raw(['remote', 'prune', 'origin']); const durationMs = Math.round(Date.now() - fetchStart); @@ -228,7 +229,7 @@ export async function syncGit(): Promise<void> { throw new Error(REPOSITORY_TEMPORARY_ERROR); } - config.baseBranch = config.baseBranch || (await getDefaultBranch(git)); + config.currentBranch = config.currentBranch || (await getDefaultBranch(git)); } export async function initRepo(args: StorageConfig): Promise<void> { @@ -300,16 +301,16 @@ export async function getCommitMessages(): Promise<string[]> { return res.all.map((commit) => commit.message); } -export async function setBaseBranch(branchName: string): Promise<string> { +export async function setBranch(branchName: string): Promise<string> { if (branchName) { if (!(await branchExists(branchName))) { - throwBaseBranchValidationError(branchName); + throwBranchValidationError(branchName); } - logger.debug(`Setting baseBranch to ${branchName}`); - config.baseBranch = branchName; + logger.debug(`Setting current branch to ${branchName}`); + config.currentBranch = branchName; try { if (branchName !== 'master') { - config.baseBranchSha = ( + config.currentBranchSha = ( await git.raw(['rev-parse', 'origin/' + branchName]) ).trim(); } @@ -325,13 +326,13 @@ export async function setBaseBranch(branchName: string): Promise<string> { ) || err.message.includes('did not match any file(s) known to git') ) { - throwBaseBranchValidationError(branchName); + throwBranchValidationError(branchName); } throw err; } } return ( - config.baseBranchSha || + config.currentBranchSha || (await git.raw(['rev-parse', 'origin/master'])).trim() ); } @@ -353,7 +354,7 @@ export async function setBranchPrefix(branchPrefix: string): Promise<void> { } export async function getFileList(): Promise<string[]> { - const branch = config.baseBranch; + const branch = config.currentBranch; const submodules = await getSubmodules(); const files: string = await git.raw(['ls-tree', '-r', branch]); // istanbul ignore if @@ -389,7 +390,7 @@ export async function isBranchStale(branchName: string): Promise<boolean> { '--remotes', '--verbose', '--contains', - config.baseBranchSha || `origin/${config.baseBranch}`, + config.currentBranchSha || `origin/${config.currentBranch}`, ]); return !branches.all.map(localName).includes(branchName); } @@ -416,9 +417,9 @@ export async function deleteBranch(branchName: string): Promise<void> { export async function mergeBranch(branchName: string): Promise<void> { await git.reset('hard'); await git.checkout(['-B', branchName, 'origin/' + branchName]); - await git.checkout(config.baseBranch); + await git.checkout(config.currentBranch); await git.merge(['--ff-only', branchName]); - await git.push('origin', config.baseBranch); + await git.push('origin', config.currentBranch); limits.incrementLimit('prCommitsPerRunLimit'); } @@ -436,7 +437,7 @@ export async function getBranchLastCommitTime( export async function getBranchFiles(branchName: string): Promise<string[]> { try { - const diff = await git.diffSummary([branchName, config.baseBranch]); + const diff = await git.diffSummary([branchName, config.currentBranch]); return diff.files.map((file) => file.file); } catch (err) /* istanbul ignore next */ { checkForPlatformFailure(err); @@ -457,7 +458,7 @@ export async function getFile( } try { const content = await git.show([ - 'origin/' + (branchName || config.baseBranch) + ':' + filePath, + 'origin/' + (branchName || config.currentBranch) + ':' + filePath, ]); return content; } catch (err) { @@ -510,7 +511,7 @@ export async function commitFiles({ try { await git.reset('hard'); await git.raw(['clean', '-fd']); - await git.checkout(['-B', branchName, 'origin/' + config.baseBranch]); + await git.checkout(['-B', branchName, 'origin/' + config.currentBranch]); const fileNames = []; const deleted = []; for (const file of files) {