started to changed the code. Added tests.

This commit is contained in:
didier.amyot committed 2014-01-20 10:02:22 -05:00
1 parent 3c8aafcbc2
commit cfbf2aa42f
4 files changed
+94 -43

No files matched your search

-6
View File
@@ -1,6 +0,0 @@
from mercurial import ui, hg, node
from sys import argv
peer = hg.peer(ui.ui(), {}, argv[1])
for name, rev in peer.branchmap().items():
print(name)
+63 -34
View File
@@ -3,6 +3,7 @@ var gitane = require('gitane')
, exec = require('child_process').exec , exec = require('child_process').exec
, shellescape = require('shell-escape') , shellescape = require('shell-escape')
, path = require('path') , path = require('path')
, fs = require('fs')
, _ = require('underscore') , _ = require('underscore')
module.exports = { module.exports = {
@@ -11,9 +12,29 @@ module.exports = {
httpUrl: httpUrl, httpUrl: httpUrl,
gitCmd: gitCmd, gitCmd: gitCmd,
gitaneCmd: gitaneCmd, gitaneCmd: gitaneCmd,
processBranches: processBranches, shellEscape: shellEscape,
getBranches: getBranches, Config: Config,
shellEscape: shellEscape Hg : Hg
}
function Config(basedConfig){
this.sanitizeConfig = function(config) {
config = config || {};
config.auth = config.auth || {};
this.url= config.url || "",
this.display_url= config.display_url || "",
this.auth = {
type: config.auth.type || "",
privkey: config.auth.privkey || "",
pubkey: config.auth.pubkey || "",
username: config.auth.username || "",
password: config.auth.password || ""
}
}
this.sanitizeConfig(basedConfig);
return this
} }
function shellEscape(one) { function shellEscape(one) {
@@ -51,8 +72,6 @@ function gitCmd(cmd, cwd, auth, context, done) {
}, done) }, done)
} }
// run a strider command with gitane // run a strider command with gitane
function gitaneCmd(cmd, dest, privkey, context, done) { function gitaneCmd(cmd, dest, privkey, context, done) {
var start = new Date() var start = new Date()
@@ -77,44 +96,54 @@ function gitaneCmd(cmd, dest, privkey, context, done) {
}) })
} }
function processBranches(data, done) { function Hg(){
done(null, data.trim().split(/\n+/).map(function (line) { this.getMercurialBranchesCommandLine = function(config){
return line.split(/\s+/)[1].split('/').slice(-1)[0] var repoLocation = config.url;
})) var pythonScript = fs.realpathSync(path.join(__dirname, 'get_branches.py'));
} var cmd = 'python ' + pythonScript + " " + repoLocation;
}
this.getNameOfMercurialBranchesOutOfScriptStdout = function(stdout){
return _.filter(stdout.split("\n"),function(v){return !!v})
}
this.getMercurialBranchesForHttpBasedReposetories = function(config, done){
var cmd = this.getMercurialBranchesCommandLine(config);
exec(cmd, function (err, stdout, stderr) {
if (err){
return done(err);
}else{
return done(this.getNameOfMercurialBranchesOutOfScriptStdout(stdout));
}
})
}
function getBranches(config, privkey, done) { this.getMercurialBranchesForSshBasedReposetories = function(config, privkey, done){
if (config.auth.type === 'ssh') { var cmd = this.getMercurialBranchesCommandLine(config);
gitane.run({ gitane.run({
cmd: 'git ls-remote -h ' + gitUrl(config)[0], cmd: cmd,
baseDir: '/', baseDir: '/',
privKey: config.auth.privkey || privkey, privKey: config.auth.privkey || privkey,
detached: true detached: true
}, function (err, stdout, stderr, exitCode) { },
if (err || exitCode !== 0) return done(err || new Error(stderr)) function (err, stdout, stderr, exitCode) {
processBranches(stdout, done) if (err || exitCode !== 0){
}) return done(err || new Error(stderr))
} else {
exec('git ls-remote -h ' + httpUrl(config)[0], function (err, stdout, stderr) {
if (err) return done(err)
processBranches(stdout, done)
})
}
}
function getMercurialBranches(repoLocation){
var pythonScript = path.join(__dirname, 'get_branches.py');
var cmd = 'python ' + pythonScript + " " + repoLocation;
exec(cmd, function (err, stdout, stderr) {
if (err){
return console.log(err)
}else{ }else{
return console.log(_.filter(stdout.split("\n"),function(v){return !!v} )) return done(this.getNameOfMercurialBranchesOutOfScriptStdout(stdout));
} }
}) })
}
this.getMercurialBranches = function(config, privkey, done){
if (config.auth.type === 'ssh') {
privkey = config.auth.privkey || privkey;
return this.getMercurialBranchesForSshBasedReposetories(config, privkey, done);
}else{
return this.getMercurialBranchesForHttpBasedReposetories(config, done);
}
}
} }
getMercurialBranches('ssh://hg@bitbucket.org/ustraca/crypto')
console.log("ok?")
+4 -3
View File
@@ -20,8 +20,8 @@
"license": "MIT", "license": "MIT",
"readmeFilename": "Readme.md", "readmeFilename": "Readme.md",
"strider": { "strider": {
"id": "git", "id": "hg",
"title": "Git", "title": "Hg",
"type": "provider", "type": "provider",
"webapp": "webapp.js", "webapp": "webapp.js",
"worker": "worker.js", "worker": "worker.js",
@@ -32,7 +32,8 @@
"gitane": "~0.3.1", "gitane": "~0.3.1",
"shell-escape": "0.0.1", "shell-escape": "0.0.1",
"mkdirp": "~0.3.5", "mkdirp": "~0.3.5",
"underscore": "~1.5.2" "underscore": "~1.5.2",
"expect.js": "~0.2.0"
}, },
"devDependencies": { "devDependencies": {
"jshint": "~2.3.0", "jshint": "~2.3.0",
+27
View File
@@ -0,0 +1,27 @@
var expect = require('expect.js')
, utils = require('../lib/index.js')
describe('The lib util interface', function () {
describe('the Hg object', function () {
it('should always pass', function(){
expect(1).to.be(1);
});
var hg = new utils.Hg();
it('should get the branched name from the script stdout', function () {
var stdout = "a\nb\nc\n";
expect(hg.getNameOfMercurialBranchesOutOfScriptStdout(stdout)).to.eql(["a", "b", "c"]);
})
})
describe('the config object', function(){
it('should be able to initiate properly', function () {
var config = new utils.Config();
expect(config.url).to.eql("");
expect(config.auth.username).to.eql("");
config = new utils.Config({url: "1", auth: {username: "2"}});
expect(config.url).to.eql("1");
expect(config.auth.username).to.eql("2");
})
})
})