diff --git a/lib/platform/git/storage.js b/lib/platform/git/storage.js index 73e8f019e4954c0a9681b55f82daabe32f442e17..b865d279ac2e354c25ad48f1cbb14b76175bc1c6 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 );