Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hg support plus minor changes #3

Merged
merged 2 commits into from
Jul 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var argv = require('yargs')
alias : 'type',
demand: false,
default: 'auto',
describe: 'Type of repository. Accepts "git", "svn", or "auto"',
describe: 'Type of repository. Accepts "git", "hg", "svn", or "auto"',
type: 'string',
nargs: 1
})
Expand All @@ -30,7 +30,10 @@ if (argv.type === 'auto') {
console.log("Auto-detected Git repository");
} else if (argv.repo.indexOf('svn') > -1) {
argv.type = 'svn';
console.log("Auto-detected SVN repository");
console.log("Auto-detected Hg repository");
} else if (argv.repo.indexOf('hg') > -1) {
argv.type = 'hg';
console.log("Auto-detected Hg repository");
} else {
console.log("Could not detect repository type, please be explicit!");
return;
Expand All @@ -45,8 +48,8 @@ repoAdapter.init(argv.repo, location, function(err) {
output.reportErr(err);
return;
}
recursive(location, [Path.join("**",".git","*"),".gitignore",Path.join("**",".svn","*")], function (err, lsFiles) {
async.each(lsFiles,function(file, cb) {
recursive(location, [Path.join("**",".git","*"),".gitignore",Path.join("**",".svn","*"),".hg*"], function (err, lsFiles) {
async.eachSeries(lsFiles,function(file, cb) {
file = file.replace(location + Path.sep,"");
repoAdapter.getOwner(file,function(err, owner) {
if (!err) {
Expand All @@ -59,7 +62,7 @@ repoAdapter.init(argv.repo, location, function(err) {
})
}, function(err) {
if (err) {
output.reportErr(err);
output.reportError(err);
return;
}

Expand Down Expand Up @@ -106,4 +109,4 @@ repoAdapter.init(argv.repo, location, function(err) {
});
});

})
})
99 changes: 99 additions & 0 deletions lib/hg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
var spawn = require("child_process").spawn;
var fs = require("fs");
var Path = require("path");
var async = require("async");
var rimraf = require("rimraf");
var mkdirp = require('mkdirp');
var wc;

module.exports = {
init: function(url, locallocation, callback) {
rimraf(locallocation, function() {
wc = Path.resolve(locallocation);

mkdirp(locallocation, function(err) {

if (err) {
callback(err); return;
}

child = spawn("hg", [
//Command-line arguments, in order
"clone",
url,
locallocation
] );

err = null;

child.stdout.on("data", function (data) {
//console.log(data.toString("utf-8"));
});

child.stderr.on("data", function (data) {
err = new Error(data.toString("utf-8"));
});

child.on("close", function (code) {
callback(err);
});

});

})
},

getOwner: function(file, callback) {
var fileAuthors = {};
var err = null;
var linepatt = /^\s*[^:]+:/i;
child = spawn("hg", [
//Command-line arguments, in order
"blame",
file,
"-w",
"-b", //Ignore whitespace-only changes
"-u" // show the user
],
{
cwd: wc
} );

child.stdout.on("data", function (data) {
lines = data.toString("utf-8").split("\n");

async.each(lines,function(line, cb) {
var match = linepatt.exec(line);
if (!match) {
return;
}
var author = match[0].trim();
if (!fileAuthors[author]) {
fileAuthors[author] = 0;
};
fileAuthors[author]++;
});
});

child.stderr.on("data", function (data) {
err = new Error(data.toString("utf-8"));
});

child.on("close", function (code) {
if(err) {
callback(err); return;
}

var max = 0;
var winner = "";
for(auth in fileAuthors) {
if (fileAuthors[auth] > max) {
max = fileAuthors[auth];
winner = auth;
}
}

callback(err, winner);
});
}
}