From 67a7dd4a10eb57f69c5ecb67e3b9fb85825ffec4 Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@arkins.net> Date: Wed, 22 Jul 2020 16:13:24 +0200 Subject: [PATCH] refactor: remove unnecessary if from git.setBranch (#6815) Co-authored-by: Michael Kriese <michael.kriese@visualon.de> --- lib/util/git/index.spec.ts | 3 +++ lib/util/git/index.ts | 50 ++++++++++++++++++-------------------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/lib/util/git/index.spec.ts b/lib/util/git/index.spec.ts index d23dd7fd61..b2006e6d6f 100644 --- a/lib/util/git/index.spec.ts +++ b/lib/util/git/index.spec.ts @@ -114,6 +114,9 @@ describe('platform/git', () => { }); }); describe('isBranchStale()', () => { + beforeEach(async () => { + await git.setBranch('master'); + }); it('should return false if same SHA as master', async () => { expect(await git.isBranchStale('renovate/future_branch')).toBe(false); }); diff --git a/lib/util/git/index.ts b/lib/util/git/index.ts index 05f056bfed..b35c1f6e48 100644 --- a/lib/util/git/index.ts +++ b/lib/util/git/index.ts @@ -308,34 +308,30 @@ export async function getCommitMessages(): Promise<string[]> { } export async function setBranch(branchName: string): Promise<string> { - if (branchName) { - if (!(await branchExists(branchName))) { - throwBranchValidationError(branchName); - } - logger.debug(`Setting current branch to ${branchName}`); + if (!(await branchExists(branchName))) { + throwBranchValidationError(branchName); + } + logger.debug(`Setting current branch to ${branchName}`); + try { config.currentBranch = branchName; - try { - if (branchName !== 'master') { - config.currentBranchSha = ( - await git.raw(['rev-parse', 'origin/' + branchName]) - ).trim(); - } - await git.checkout([branchName, '-f']); - await git.reset(ResetMode.HARD); - const latestCommitDate = (await git.log({ n: 1 })).latest.date; - logger.debug({ branchName, latestCommitDate }, 'latest commit'); - } catch (err) /* istanbul ignore next */ { - checkForPlatformFailure(err); - if ( - err.message.includes( - 'unknown revision or path not in the working tree' - ) || - err.message.includes('did not match any file(s) known to git') - ) { - throwBranchValidationError(branchName); - } - throw err; + config.currentBranchSha = ( + await git.raw(['rev-parse', 'origin/' + branchName]) + ).trim(); + await git.checkout([branchName, '-f']); + const latestCommitDate = (await git.log({ n: 1 })).latest.date; + logger.debug({ branchName, latestCommitDate }, 'latest commit'); + await git.reset(ResetMode.HARD); + } catch (err) /* istanbul ignore next */ { + checkForPlatformFailure(err); + if ( + err.message.includes( + 'unknown revision or path not in the working tree' + ) || + err.message.includes('did not match any file(s) known to git') + ) { + throwBranchValidationError(branchName); } + throw err; } return ( config.currentBranchSha || @@ -396,7 +392,7 @@ export async function isBranchStale(branchName: string): Promise<boolean> { '--remotes', '--verbose', '--contains', - config.currentBranchSha || `origin/${config.currentBranch}`, + config.currentBranchSha, ]); return !branches.all.map(localName).includes(branchName); } -- GitLab