Skip to content

Commit

Permalink
Merge pull request #3 from boomzillawtf/master
Browse files Browse the repository at this point in the history
Hg support plus minor changes.
  • Loading branch information
yamikuronue committed Jul 16, 2015
2 parents 653956d + b6dccfc commit c59bb30
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 6 deletions.
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);
});
}
}

0 comments on commit c59bb30

Please sign in to comment.