simple-git
Advanced tools
Comparing version 0.8.0 to 0.9.0
{ | ||
"name": "simple-git", | ||
"description": "Simple GIT interface for node.js", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"author": "Steve King <steve@mydev.co>", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
@@ -54,2 +54,5 @@ # Simple Git | ||
`.command(gitPath)` sets the command to use to reference git, allows for using a git binary not available on | ||
the path environment variable | ||
`.push(remote, branch, handlerFn)` pushes to a named remote and named branch | ||
@@ -56,0 +59,0 @@ |
(function() { | ||
var ChildProcess = require('child_process'); | ||
var merge = require('util').format; | ||
@@ -16,4 +17,21 @@ /** | ||
/** | ||
* @type {string} The command to use to reference the git binary | ||
*/ | ||
Git.prototype._command = 'git'; | ||
/** | ||
* Sets the path to a custom git binary, should either be `git` when there is an installation of git available on | ||
* the system path, or a fully qualified path to the executable. | ||
* | ||
* @param {string} command | ||
* @returns {Git} | ||
*/ | ||
Git.prototype.customBinary = function(command) { | ||
this._command = command; | ||
return this; | ||
}; | ||
/** | ||
* Initalize a git repo | ||
* Initialize a git repo | ||
* | ||
@@ -23,3 +41,3 @@ * @param {Function} [then] | ||
Git.prototype.init = function(then) { | ||
return this._run('git init', function(err) { | ||
return this._run('init', function(err) { | ||
then && then(err); | ||
@@ -37,3 +55,3 @@ }); | ||
Git.prototype.clone = function(repoPath, localPath, then ) { | ||
return this._run('git clone ' + repoPath + ' ' + localPath, function(err) { | ||
return this._run(['clone %s %s', repoPath, localPath], function(err) { | ||
then && then(err); | ||
@@ -62,3 +80,3 @@ }); | ||
Git.prototype.add = function(files, then) { | ||
return this._run('git add "' + [].concat(files).join('" "') + '"', function(err, data) { | ||
return this._run(['add "%s"', [].concat(files).join('" "')], function(err, data) { | ||
then && then(err); | ||
@@ -85,3 +103,3 @@ }); | ||
return this._run('git commit -m "' + message.replace(/"/g, '\\"') + '"' + files, function(err, data) { | ||
return this._run(['commit -m "%s" %s', message.replace(/"/g, '\\"'), files], function(err, data) { | ||
then && then(err, !err && git._parseCommit(data)); | ||
@@ -98,5 +116,5 @@ }); | ||
Git.prototype.pull = function(remote, branch, then) { | ||
var command = "git pull"; | ||
var command = "pull"; | ||
if (typeof remote === 'string' && typeof branch === 'string') { | ||
command += ' "' + remote + '" "' + branch + '"'; | ||
command = ['pull "%s" "%s"', remote, branch]; | ||
} | ||
@@ -124,5 +142,5 @@ if (typeof arguments[arguments.length - 1] === 'function') { | ||
Git.prototype.fetch = function(remote, branch, then) { | ||
var command = "git fetch"; | ||
var command = "fetch"; | ||
if (typeof remote === 'string' && typeof branch === 'string') { | ||
command += ' "' + remote + '" "' + branch + '"'; | ||
command = ['fetch "%s" "%s"', remote, branch]; | ||
} | ||
@@ -144,3 +162,3 @@ if (typeof arguments[arguments.length - 1] === 'function') { | ||
Git.prototype.tags = function(then) { | ||
return this._run('git tag -l', function(err, data) { | ||
return this._run('tag -l', function(err, data) { | ||
then && then(err, !err && this._parseListTags(data)); | ||
@@ -157,17 +175,17 @@ }); | ||
Git.prototype.checkout = function(what, then) { | ||
return this._run('git checkout "' + what + '"', function(err, data) { | ||
return this._run(['checkout "%s"', what], function(err, data) { | ||
then && then(err, !err && this._parseCheckout(data)); | ||
}); | ||
}; | ||
/** | ||
* Check out a remote branch | ||
* | ||
* @param {String} name of branch | ||
* @param {String} start point(e.g origin/development) | ||
* @param {String} branchName name of branch | ||
* @param {String} startPoint (e.g origin/development) | ||
* @param {Function} [then] | ||
*/ | ||
Git.prototype.checkoutBranch = function(branchName, startPoint, then) { | ||
return this._run('git checkout -b "' + branchName + '" "' + startPoint + '"', function(err, data) { | ||
return this._run(['checkout -b "%s" "%s"', branchName, startPoint], function(err, data) { | ||
then && then(err, !err && this._parseCheckout(data)); | ||
@@ -180,7 +198,7 @@ }); | ||
* | ||
* @param {String} name of branch | ||
* @param {String} branchName of branch | ||
* @param {Function} [then] | ||
*/ | ||
Git.prototype.checkoutLocalBranch = function(branchName, then) { | ||
return this._run('git checkout -b "' + branchName + '"', function(err, data) { | ||
return this._run(['checkout -b "%s"', branchName], function(err, data) { | ||
then && then(err, !err && this._parseCheckout(data)); | ||
@@ -198,3 +216,3 @@ }); | ||
Git.prototype.submoduleAdd = function(repo, path, then) { | ||
return this._run('git submodule add "' + repo + '" "' + path + '"', function(err) { | ||
return this._run(['submodule add "%s" "%s"', repo, path], function(err) { | ||
then && then(err); | ||
@@ -215,3 +233,3 @@ }); | ||
} | ||
return this._run('git ls-remote ' + args, function(err, data) { | ||
return this._run('ls-remote ' + args, function(err, data) { | ||
then && then(err, data); | ||
@@ -230,3 +248,3 @@ }); | ||
Git.prototype.addRemote = function(remoteName, remoteRepo, then) { | ||
return this._run('git remote add "' + remoteName + '" "' + remoteRepo + '"', function (err) { | ||
return this._run(['remote add "%s" "%s"', remoteName, remoteRepo], function (err) { | ||
then && then(err); | ||
@@ -244,3 +262,3 @@ }); | ||
Git.prototype.removeRemote = function(remoteName, then) { | ||
return this._run('git remote remove "' + remoteName + '"', function (err) { | ||
return this._run(['remote remove "%s"', remoteName], function (err) { | ||
then && then(err); | ||
@@ -259,5 +277,5 @@ }); | ||
Git.prototype.push = function(remote, branch, then) { | ||
var command = "git push"; | ||
var command = "push"; | ||
if (typeof remote === 'string' && typeof branch === 'string') { | ||
command += ' "' + remote + '" "' + branch + '"'; | ||
command = ['push "%s" "%s"', remote, branch]; | ||
} | ||
@@ -269,3 +287,3 @@ if (typeof arguments[arguments.length - 1] === 'function') { | ||
return this._run(command, function(err, data) { | ||
then && then(err, !err && this._parsePush(data)); | ||
then && then(err, !err && data); | ||
}); | ||
@@ -302,5 +320,5 @@ }; | ||
Git.prototype.diff = function(options, then) { | ||
var command = 'git diff'; | ||
var command = 'diff'; | ||
if (typeof options === 'string' && typeof then === 'function') { | ||
if (typeof options === 'string') { | ||
command += ' ' + options; | ||
@@ -319,3 +337,3 @@ } | ||
Git.prototype._rm = function(files, options, then) { | ||
return this._run('git rm ' + options + ' "' + [].concat(files).join('" "') + '"', function(err) { | ||
return this._run(['rm %s "%s"', options, [].concat(files).join('" "')], function(err) { | ||
then && then(err); | ||
@@ -411,4 +429,18 @@ }); | ||
/** | ||
* Schedules the supplied command to be run, the command should not include the name of the git binary and should | ||
* either be a string, or an array where the first argument is a formatted string accepted by `format` in the util | ||
* module that uses the other entities in the array as the template data. | ||
* | ||
* @param {string|string[]} command | ||
* @param {Function} [then] | ||
* @see http://nodejs.org/api/util.html#util_util_format_format | ||
* @returns {Git} | ||
*/ | ||
Git.prototype._run = function(command, then) { | ||
this._runCache.push([command, then]); | ||
if (Array.isArray(command)) { | ||
command = merge.apply(merge, command); | ||
} | ||
this._runCache.push([this._command + ' ' + command, then]); | ||
this._schedule(); | ||
@@ -415,0 +447,0 @@ |
18534
427
99