Skip to content

Commit

Permalink
Split up the cloning functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalrymple committed Jul 24, 2017
1 parent a8c401c commit b2ca83e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},
"extends": "airbnb",
rules:{
arrow-body-style: [2, "as-needed"]
arrow-body-style: [2, "as-needed"],
no-await-in-loop: [1]
}
}
96 changes: 47 additions & 49 deletions src/Hg.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function moveFiles(source, destination, files) {
async function cloneMultipleAndMerge(fromRepos, combinedRepo) {
const mergedRepos = [];

for (repo of fromRepos) {
for (let fromRepo 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');
}
Expand All @@ -33,28 +33,28 @@ async function cloneMultipleAndMerge(fromRepos, combinedRepo) {
name += `-${ShortID.generate()}`;
}

await combinedRepo.pull({ source: fromRepo, force: true })
await combinedRepo.update({ clean: true, revision: 'default' })
await combinedRepo.pull({ source: fromRepo, force: true });
await combinedRepo.update({ clean: true, revision: 'default' });

let files = await Globby(['*', '!.hg'], { dot: true, cwd: combinedRepo.path })
const files = await Globby(['*', '!.hg'], { dot: true, cwd: combinedRepo.path });

await moveFiles(combinedRepo.path, Path.join(combinedRepo.path, name), files)
await combinedRepo.add()
await moveFiles(combinedRepo.path, Path.join(combinedRepo.path, name), files);
await combinedRepo.add();

try {
await combinedRepo.remove({ after: true })
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}`)
await combinedRepo.commit(`Moving repository ${name} into folder ${name}`);

if (!mergedRepos.length) return;
if (!mergedRepos.length) break;

await combinedRepo.merge()
await combinedRepo.merge();

try {
await combinedRepo.commit(`Merging ${name} into combined`)
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')) {
Expand All @@ -68,43 +68,28 @@ async function cloneMultipleAndMerge(fromRepos, combinedRepo) {
return combinedRepo;
}

async function cloneSingleOrMultiple(from, to, pythonPath) {
switch (from.constructor) {
case Array:
{
const newRepo = new HgRepo(to, pythonPath);

await newRepo.init();

return cloneMultipleAndMerge(from, newRepo);
}
case Object:
{
const newRepo = new HgRepo(to || {
url: from.url,
password: from.password,
username: from.username,
}, pythonPath);

const url = Utils.buildRepoURL(from);

await Command.run('hg clone', newRepo.path, [url, newRepo.path]);

return newRepo;
}
case String:
{
const newRepo = new HgRepo(to || {
url: from,
}, pythonPath);
async function cloneSingle(from, to, pythonPath) {
let repo;
let url;

if (from.constructor === Object) {
repo = new HgRepo(to || {
url: from.url,
password: from.password,
username: from.username,
}, pythonPath);

url = Utils.buildRepoURL(from);
} else {
url = from;
repo = new HgRepo(to || {
url: from,
}, pythonPath);
}

await Command.run('hg clone', newRepo.path, [from, newRepo.path]);
await Command.run('hg clone', repo.path, [url, repo.path]);

return newRepo;
}
default:
return new TypeError('Incorrect type of from parameter. Must be an array or an object');
}
return repo;
}

class Hg {
Expand All @@ -113,17 +98,30 @@ class Hg {
}

async clone(from, to = undefined, done = undefined) {
try {
const repo = await cloneSingleOrMultiple(from, to, this.pythonPath);
let repo;

return Utils.asCallback(repo, done);
try {
switch (from.constructor) {
case Array:
{
repo = await cloneMultipleAndMerge(from, to);
break;
}
case String || Object:
repo = await cloneSingle(from, to, this.pythonPath);
break;
default:
return new TypeError('Incorrect type of from parameter. Must be an array or an object');
}
} catch (e) {
if (e.message.includes('not found')) {
throw new TypeError('Incorrect type of from parameter. Clone source not found');
} else {
throw e;
}
}

return Utils.asCallback(repo, done);
}

async create(to, done = undefined) {
Expand Down

0 comments on commit b2ca83e

Please sign in to comment.