-
Notifications
You must be signed in to change notification settings - Fork 13
/
git.js
60 lines (49 loc) · 1.36 KB
/
git.js
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
// imports
var execFile = require('child_process').execFile;
// Class Git
var Git = module.exports = function (options) {
this.binary = 'git';
if (typeof options == 'undefined')
options = {};
this.cwd = options.cwd || process.cwd();
delete options.cwd;
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 = [];
}
options = Git.optionsToString(options)
execFile(this.binary, [...this.args, command, ...options, ...args], {
cwd: this.cwd
}, 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
};