diff --git a/lib/Hg.js b/lib/Hg.js index 87b9d791..9bc87885 100644 --- a/lib/Hg.js +++ b/lib/Hg.js @@ -22,44 +22,49 @@ function moveFiles(source, destination, files) { function cloneMultipleAndMerge(fromRepos, combinedRepo) { const mergedRepos = []; - return Promise.each(fromRepos, (fromRepo) => { - if (fromRepo.constructor !== String || fromRepo.constructor !== Object) { - throw new TypeError('Incorrect type of from parameter. Clone source in array is an invalid type. Must be an String or an Object'); - } + for (repo of fromRepos) { + if (fromRepo.constructor !== String || fromRepo.constructor !== Object) { + throw new TypeError('Incorrect type of from parameter. Clone source in array is an invalid type. Must be an String or an Object'); + } + + let name = Path.basename(fromRepo); + + if (mergedRepos.includes(name)) { + name += `-${ShortID.generate()}`; + } + + await combinedRepo.pull({ source: fromRepo, force: true }) + await combinedRepo.update({ clean: true, revision: 'default' }) + + let files = await Globby(['*', '!.hg'], { dot: true, cwd: combinedRepo.path }) + + await moveFiles(combinedRepo.path, Path.join(combinedRepo.path, name), files) + await combinedRepo.add() + try { + await combinedRepo.remove({ after: true }) + } catch (errorInfo) { + if (!errorInfo.error.message.includes('still exists')) throw errorInfo.error; + } + + await combinedRepo.commit(`Moving repository ${name} into folder ${name}`) - let name = Path.basename(fromRepo); + if (!mergedRepos.length) return; - if (mergedRepos.includes(name)) { - name += `-${ShortID.generate()}`; + await combinedRepo.merge() + try { + await combinedRepo.commit(`Merging ${name} into combined`) + + } catch (errorInfo) { + if (!errorInfo.error.message.includes('nothing to merge') && + !errorInfo.error.message.includes('merging with a working directory ancestor')) { + throw errorInfo.error; } + } + + mergedRepos.push(name); + } - return combinedRepo.pull({ source: fromRepo, force: true }) - .then(() => combinedRepo.update({ clean: true, revision: 'default' })) - .then(() => Globby(['*', '!.hg'], { dot: true, cwd: combinedRepo.path })) - .then(files => moveFiles(combinedRepo.path, Path.join(combinedRepo.path, name), files)) - .then(() => combinedRepo.add()) - .then(() => combinedRepo.remove({ after: true })) - .catch((errorInfo) => { - if (!errorInfo.error.message.includes('still exists')) throw errorInfo.error; - }) - .then(() => combinedRepo.commit(`Moving repository ${name} into folder ${name}`)) - .then(() => { - if (!mergedRepos.length) return Promise.resolve(); - - return combinedRepo.merge() - .then(() => combinedRepo.commit(`Merging ${name} into combined`)) - .catch((errorInfo) => { - if (!errorInfo.error.message.includes('nothing to merge') && - !errorInfo.error.message.includes('merging with a working directory ancestor')) { - throw errorInfo.error; - } - }); - }) - .then(() => { - mergedRepos.push(name); - }); - }) - .then(() => combinedRepo); + return combinedRepo; } async function cloneSingleOrMultiple(from, to, pythonPath) { @@ -70,9 +75,9 @@ async function cloneSingleOrMultiple(from, to, pythonPath) { try { await Command.run('hg clone', newRepo.path, [from, newRepo.path]) } catch (error) { - if (results.error.message.includes('not found')) { - throw new TypeError('Incorrect type of from parameter. Clone source not found'); - } + if (results.error.message.includes('not found')) { + throw new TypeError('Incorrect type of from parameter. Clone source not found'); + } } return newRepo; @@ -126,7 +131,7 @@ class Hg { gitify({ gitRepoPath = undefined } = {}, done = undefined) { const repo = new HgRepo(undefined, this.pythonPath); - console.log(gitRepoPath) + return repo.gitify({ gitRepoPath }) .asCallback(done); }