fix(gitfs): reset local branches after fetch

This commit is contained in:
Rhys Arkins 2018-09-10 11:59:17 +02:00
parent 085ce43a47
commit a4107b44c6

View file

@ -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
);