-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit.js
More file actions
85 lines (70 loc) · 2.01 KB
/
git.js
File metadata and controls
85 lines (70 loc) · 2.01 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// imports
var fs = require('fs');
var path = require('path');
var exec = require('child-process-promise').exec;
var spawn = require('child-process-promise').spawn;
var commands = require('./commands');
var inherits = require('util').inherits;
// Class Git
var Git = module.exports = function (options) {
this.binary = 'git';
options = options || {};
// If a gitDir is provided, make sure it is to the .git
// directory and a working tree is provided (or set to
// base directory)
var gitDir = options['git-dir'];
if(gitDir) {
var sep = path.sep === '/' ? '\\/' : path.sep;
var isGitRegex = new RegExp(sep + '\\.git$', 'g');
if(! gitDir.match(isGitRegex)) {
options['git-dir'] = path.join(gitDir, '.git');
}
if(! options['work-tree']) {
options['work-tree'] = path.join(options['git-dir'], '..');
}
}
this.args = Git.optionsToArray(options);
};
Git.prototype.exec = function(command, args) {
args = args || [];
return this.spawn(command, args, {
capture: ['stdout', 'stderr']
});
};
// git.spawn(command [, args])
Git.prototype.spawn = function(command, args){
args = args || [];
command = Array.isArray(command) ? command : [command];
var rawargs = this.args.concat(command).concat(args);
args = [];
// Get rid of nulls and undefineds.
for(var i = 0, len = rawargs.length; i < len; i++) {
if(rawargs[i] != null) args.push(rawargs[i]+"");
}
return spawn(this.binary, args, {
capture: ['stdout', 'stderr']
});
};
Git.optionsToArray = function(options){
var args = [];
Object.keys(options).forEach(function(k){
var val = options[k];
if(k.length == 1) {
if(val === true) {
args.push('-' + k);
} 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;
};
Object.keys(commands).forEach(function(key) {
Git.prototype[key] = commands[key];
});