forked from pvorb/node-git-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.js
More file actions
61 lines (50 loc) · 1.38 KB
/
git.js
File metadata and controls
61 lines (50 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// imports
var fs = require('fs');
var path = require('path');
var exec = require('child_process').exec;
// Class Git
var Git = module.exports = function (options) {
this.binary = 'git';
if (typeof options == 'undefined')
options = {};
this.args = Git.optionsToString(options);
};
// git.exec(command [[, options], args ], callback)
Git.prototype.exec = function (command, options, args, callback) {
callback = arguments[arguments.length - 1];
if (arguments.length == 2) {
options = {};
args = [];
} else if (arguments.length == 3) {
args = arguments[1];
options = [];
}
args = args.join(' ');
options = Git.optionsToString(options)
var cmd = this.binary + ' ' + this.args + ' ' + command + ' ' + options + ' '
+ args;
exec(cmd, function (err, stdout, stderr) {
callback(err, stdout);
});
};
// converts an object that contains key value pairs to a argv string
Git.optionsToString = function (options) {
var args = [];
for (var k in options) {
var val = options[k];
if (k.length == 1) {
// val is true, add '-k'
if (val === true)
args.push('-'+k);
// if val is not false, add '-k val'
else if (val !== false)
args.push('-'+k+' '+val);
} else {
if (val === true)
args.push('--'+k);
else if (val !== false)
args.push('--'+k+'='+val);
}
}
return args.join(' ');
};