-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit.js
275 lines (240 loc) · 5.87 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
var fs = require('fs');
var path = require('path');
var childProcess = require('child_process');
var escapeQuotes = /'/g;
// ------------------------------------------------------------------
// The Repo constructor
function Repo(repoPath) {
this.repoPath = path.resolve(repoPath);
}
// git ...
Repo.prototype.run = function(cmd, args, callback) {
if (arguments.length === 2) {
callback = args;
args = [ ];
}
runGitCommand(exports.parseHolders(cmd, args), this.repoPath, callback);
};
// git add files
Repo.prototype.add = function(files, callback) {
if (typeof files === 'object') {
files = files.join(' ');
}
this.run('add ?', [files], callback);
};
// git commit -v -m message
Repo.prototype.commit = function(message, callback) {
this.run("commit -v -m '?'", [message.replace(escapeQuotes, "\\'")], callback);
};
// git commit -av -m message
Repo.prototype.commitAll = function(message, callback) {
this.run("commit -av -m '?'", [message.replace(escapeQuotes, "\\'")], callback);
};
// git clone --local /repo/path target
Repo.prototype.cloneTo = function(target, callback) {
this.run('clone --local ? ?', [this.repoPath, target], callback);
};
// git clean (dirs ? -d)
Repo.prototype.clean = function(dirs, callback) {
this.run('clean' + (dirs ? ' -d' : ''), [ ], callback);
};
// Helper for branch related functions
Repo.prototype._withBranches = function(callback) {
this.run('branch', function(err, stdout, stderr) {
if (err) {
return callback({
err: err,
stdout: stdout,
stderr: stderr
});
}
callback(null, String(stdout).split('\n'));
});
};
// git branch
Repo.prototype.listBranches = function(giveObjs, callback) {
if (arguments.length === 1) {
callback = giveObjs;
giveObjs = false;
}
this._withBranches(function(err, result) {
if (err) {
return callback(err);
}
var branches = [ ];
result.forEach(function(branch) {
branch = branch.trim();
var isCurrent = false;
if (branch[0] === '*') {
branch = branch.slice(2);
isCurrent = true;
}
if (giveObjs) {
branches.push({
name: branch,
isCurrent: isCurrent
});
} else {
branches.push(branch);
}
});
callback(null, branches);
});
};
// git branch
Repo.prototype.currentBranch = function(callback) {
this._withBranches(function(err, result) {
if (err) {
return callback(err);
}
var current;
for (var i = 0, c = result.length; i < c; i++) {
branch = branch.trim();
if (branch[0] === '*') {
current = branch.slice(2);
break;
}
}
callback(null, current);
});
};
// git remote ...
Repo.prototype._withRemotes = function(callback) {
this.run('remote', function(err, stdout, stderr) {
if (err) {
return callback({
err: err,
stdout: stdout,
stderr: stderr
});
}
callback(null, String(stdout).split('\n'));
});
};
// git remote
Repo.prototype.listRemotes = function(callback) {
this._withRemotes(callback);
};
// git remote
Repo.prototype.remoteExists = function(remote, callback) {
this._withRemotes(function(err, remotes) {
if (err) {
return callback(err);
}
callback(null, (remotes.indexOf(remote) >= 0));
});
};
// git branch name
Repo.prototype.createBranch = function(name, callback) {
this.run('branch ?', [name], callback);
};
// git branch (force ? -D : -d) name
Repo.prototype.deleteBranch = function(name, force, callback) {
if (arguments.length === 2) {
callback = force;
force = false;
}
this.run('branch -? ?', [(force ? 'D' : 'd'), name], callback);
};
// git checkout name
Repo.prototype.checkout = function(name, callback) {
this.run('checkout ?', [name], callback);
};
// git status --porcelain
Repo.prototype.status = function(callback) {
this.run('status --porcelain -z', function(err, stdout, stderr) {
// Handle errors
if (err) {
return callback({
err: err,
stdout: stdout,
stderr: stderr
});
}
var status = [ ];
String(stdout).trim().split('\0').forEach(function(item) {
if (item) {
status.push({
x: item[0],
y: item[1],
file: item.slice(3)
});
}
});
callback(null, status);
});
};
// git push
Repo.prototype.push = function(callback) {
this.run("push --all", callback);
};
// ------------------------------------------------------------------
// Publics
exports.Repo = Repo;
/**
* Parse holders in a string
*/
exports.parseHolders = function(str, holders) {
str = str.split('?');
var result = str.shift();
while (str.length) {
result += (holders.shift() || '?') + str.shift();
}
return result;
};
/**
* Get a Repo object for a given path
*/
exports.open = function(repoPath, autoCreate, callback) {
if (arguments.length === 2) {
callback = autoCreate;
autoCreate = false;
}
isRepo(repoPath, function(exists) {
if (! exists) {
if (autoCreate) {
exports.init(repoPath, callback, true);
} else {
callback(new Error('repoPath is not a git repository'));
}
} else {
callback(null, new Repo(repoPath));
}
});
};
/**
* Create a repo in the given directory
*/
exports.init = function(repoPath, callback, autoCreate) {
var repo = new Repo(repoPath);
repo.run('init', function(err, stdout, stderr) {
if (err) {
callback(new Error(String(stderr)), repo, true);
} else {
callback(null, repo, true);
}
});
};
// ------------------------------------------------------------------
// Testing functions
exports.test = function(repoPath) {
exports.open(repoPath, function(err, repo) {
global.repo = repo;
});
};
exports.results = function(err, stdout, stderr) {
console.log('err: ', err);
console.log('stdout: ', stdout);
console.log('stderr: ', stderr);
};
// ------------------------------------------------------------------
// Helpers
function isRepo(repoPath, callback) {
var git = path.join(repoPath, '.git');
path.exists(git, function(exists) {
callback(exists);
});
}
function runGitCommand(cmd, cwd, callback) {
childProcess.exec('git ' + cmd, { cwd: cwd }, callback);
}