From a4107b44c6c94a31297dadb4764bb2eabcfd8cdf Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@arkins.net> Date: Mon, 10 Sep 2018 11:59:17 +0200 Subject: [PATCH] fix(gitfs): reset local branches after fetch --- lib/platform/git/storage.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/platform/git/storage.js b/lib/platform/git/storage.js index 73e8f019e4..b865d279ac 100644 --- a/lib/platform/git/storage.js +++ b/lib/platform/git/storage.js @@ -29,6 +29,26 @@ class Storage { mergeBranch, }); + // istanbul ignore next + async function resetToBranch(branchName) { + await git.raw(['reset', '--hard']); + await git.checkout(branchName); + await git.raw(['reset', '--hard', 'origin/' + branchName]); + } + + // istanbul ignore next + async function cleanLocalBranches() { + const existingBranches = (await git.raw(['branch'])) + .split('\n') + .map(branch => branch.trim()) + .filter(branch => branch.length) + .filter(branch => !branch.startsWith('* ')); + logger.debug({ existingBranches }); + for (const branchName of existingBranches) { + await deleteLocalBranch(branchName); + } + } + async function initRepo(args) { cleanRepo(); logger.info('Initialising git repository'); @@ -55,7 +75,9 @@ class Storage { await git.raw(['remote', 'set-url', 'origin', config.url]); const fetchStart = process.hrtime(); await git.fetch(config.url, ['--depth=2', '--no-single-branch']); - await git.raw(['remote', 'prune', 'origin']); + await determineBaseBranch(); + await resetToBranch(config.baseBranch); + await cleanLocalBranches(); const fetchSeconds = Math.round( convertHrtime(process.hrtime(fetchStart)).seconds ); -- GitLab