Skip to content

Instantly share code, notes, and snippets.

@bmatusiak
Created November 24, 2012 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmatusiak/4141110 to your computer and use it in GitHub Desktop.
Save bmatusiak/4141110 to your computer and use it in GitHub Desktop.
Git CLI to NodeJS
var procman = require("./procman.js");
module.exports = function(uri){
return new git(uri);
};
function git(uri){
this.uri(uri);
this.RunOptions = {};
}
git.prototype.uri = function (uri){
this._uri = uri;
};
git.prototype.cliGit = function (args,callback){
procman.run("git "+args,this.RunOptions,callback);
};
git.prototype.cd = function (cwd){
this.RunOptions.cwd = cwd;
};
git.prototype.clone = function (localPath,callback){
this.cliGit("clone "+this._uri+" "+localPath,callback);
};
git.prototype.log = function (args,callback){
this.cliGit("log "+args,callback);
};
git.prototype.commit = function (args,callback){
this.cliGit("commit "+args,callback);
};
git.prototype.push = function (args,callback){
this.cliGit("push "+args,callback);
};
git.prototype.add = function (args,callback){
this.cliGit("add "+args,callback);
};
git.prototype.remote = function (args,callback){
this.cliGit("remote "+args,callback);
};
/*
add Add file contents to the index
bisect Find by binary search the change that introduced a bug
branch List, create, or delete branches
checkout Checkout a branch or paths to the working tree
clone Clone a repository into a new directory
commit Record changes to the repository
diff Show changes between commits, commit and working tree, etc
fetch Download objects and refs from another repository
grep Print lines matching a pattern
init Create an empty git repository or reinitialize an existing one
log Show commit logs
merge Join two or more development histories together
mv Move or rename a file, a directory, or a symlink
pull Fetch from and merge with another repository or a local branch
push Update remote refs along with associated objects
rebase Forward-port local commits to the updated upstream head
reset Reset current HEAD to the specified state
rm Remove files from the working tree and from the index
show Show various types of objects
status Show the working tree status
*/
var spawn = require('child_process').spawn;
module.exports = (function() {
var command = {};
command.run = function(commandLine, _options, callback) {
var options,__undefined__;
if(!callback && typeof _options === "function"){
callback = _options;
options = __undefined__;
}else if(typeof _options === "object"){
options = _options;
}
var args = commandLine.split(" ");
var cmd = args[0];
args.shift();
var oneoff = spawn(cmd, args, options);
var stderr,stdout;
oneoff.stdout.on('data', function(data) {
stdout = data.toString();
});
oneoff.stderr.on('data', function(data) {
stderr = data.toString();
});
oneoff.on('exit', function(code, signal) {
if(typeof callback === "function")callback(stderr, stdout, code, signal);
});
};
return command;
})();
var Git = require("./git.js");
var git = Git("https://user:pass@bitbucket.org/user/project.git");
var HOME_PATH = process.env.HOME;
var LOCALGIT_PATH = HOME_PATH+"gitTest";
git.cd(LOCALGIT_PATH);
git.log("-1"
,function(stderr, stdout, code, signal){
var commitID;
var data = stdout.split("\n");
for(var i in data){
var split = data[i].split(" ");
if(split[0] && split[0] === "commit"){
commitID = split[1].substr(0,8);
break;
}
}
if(commitID){
console.log(commitID);
}
});
/*
git.clone(LOCALGIT_PATH
,function(stderr, stdout, code, signal){
console.log(stderr, stdout, code, signal);
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment