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
+97 -46

No files matched your search

+66 -37
View File
@@ -3,6 +3,7 @@ var gitane = require('gitane')
, exec = require('child_process').exec
, shellescape = require('shell-escape')
, path = require('path')
, fs = require('fs')
, _ = require('underscore')
module.exports = {
@@ -11,9 +12,29 @@ module.exports = {
httpUrl: httpUrl,
gitCmd: gitCmd,
gitaneCmd: gitaneCmd,
processBranches: processBranches,
getBranches: getBranches,
shellEscape: shellEscape
shellEscape: shellEscape,
Config: Config,
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) {
@@ -51,8 +72,6 @@ function gitCmd(cmd, cwd, auth, context, done) {
}, done)
}
// run a strider command with gitane
function gitaneCmd(cmd, dest, privkey, context, done) {
var start = new Date()
@@ -77,44 +96,54 @@ function gitaneCmd(cmd, dest, privkey, context, done) {
})
}
function processBranches(data, done) {
done(null, data.trim().split(/\n+/).map(function (line) {
return line.split(/\s+/)[1].split('/').slice(-1)[0]
}))
}
function getBranches(config, privkey, done) {
if (config.auth.type === 'ssh') {
function Hg(){
this.getMercurialBranchesCommandLine = function(config){
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));
}
})
}
this.getMercurialBranchesForSshBasedReposetories = function(config, privkey, done){
var cmd = this.getMercurialBranchesCommandLine(config);
gitane.run({
cmd: 'git ls-remote -h ' + gitUrl(config)[0],
cmd: cmd,
baseDir: '/',
privKey: config.auth.privkey || privkey,
detached: true
}, function (err, stdout, stderr, exitCode) {
if (err || exitCode !== 0) return done(err || new Error(stderr))
processBranches(stdout, done)
})
} else {
exec('git ls-remote -h ' + httpUrl(config)[0], function (err, stdout, stderr) {
if (err) return done(err)
processBranches(stdout, done)
},
function (err, stdout, stderr, exitCode) {
if (err || exitCode !== 0){
return done(err || new Error(stderr))
}else{
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);
}
}
}
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{
return console.log(_.filter(stdout.split("\n"),function(v){return !!v} ))
}
})
}
getMercurialBranches('ssh://hg@bitbucket.org/ustraca/crypto')
console.log("ok?")