forked from claudioc/jingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitmech.js
249 lines (209 loc) · 6.98 KB
/
gitmech.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
var Path = require("path")
, ChildProcess = require('child_process')
, Fs = require("fs");
module.exports = Gitmech = (function() {
var Repo;
var gitCommands, gitDir, workTree, docSubdir;
var gitENOENT = /fatal: (Path '([^']+)' does not exist in '([0-9a-f]{40})'|ambiguous argument '([^']+)': unknown revision or path not in the working tree.)/;
// Internal helper to talk to the git subprocess
function gitExec(commands, callback) {
commands = gitCommands.concat(commands);
var child = ChildProcess.spawn("git", commands, { cwd: workTree });
var stdout = [], stderr = [];
child.stdout.addListener('data', function (text) {
stdout[stdout.length] = text;
});
child.stderr.addListener('data', function (text) {
stderr[stderr.length] = text;
});
var exitCode;
child.addListener('exit', function (code) {
exitCode = code;
});
child.addListener('close', function () {
if (exitCode > 0) {
var err = new Error("git " + commands.join(" ") + "\n" + join(stderr, 'utf8'));
if (gitENOENT.test(err.message)) {
err.errno = process.ENOENT;
}
callback(err);
return;
}
callback(null, join(stdout));
});
child.stdin.end();
}
function join(arr) {
var result, index = 0, length;
length = arr.reduce(function(l, b) {
return l + b.length;
}, 0);
result = new Buffer(length);
arr.forEach(function(b) {
b.copy(result, index);
index += b.length;
});
return result;
}
return {
// FIXME: shouldPush should be a method which understands if the local repo is in sync with the remote
// BY default we assume the repo is dirty and needs a push
// git rev-list master...origin/master (if the output is empty, there is no need for a push)
shouldPush: true,
setup: function(repoDir, repoDocSubdir) {
try {
Fs.statSync(repoDir);
} catch (e) {
throw new Error("Bad repository path (not exists): " + repoDir);
}
docSubdir = repoDocSubdir.trim().replace(/^\/|\/$/g, '');
if (docSubdir != "") {
docSubdir = docSubdir + "/";
}
try {
Fs.statSync(repoDir + "/" + docSubdir);
} catch (e) {
throw new Error("Bad document subdirectory (not exists): " + repoDir + "/" + docSubdir);
}
try {
var gitDir = Path.join(repoDir, ".git");
Fs.statSync(gitDir);
workTree = repoDir;
gitCommands = ["--git-dir=" + gitDir, "--work-tree=" + workTree];
} catch (e) {
throw new Error("Bad repository path (not initialized): " + repoDir);
}
},
absPath: function(path) {
return workTree + "/" + docSubdir + path;
},
readFile: function(path, version, callback) {
gitExec(["show", version + ":" + docSubdir + path], function(err, data) {
if (err) {
callback(err);
} else {
callback(null, data.toString());
}
});
},
remoteExists: function(remote, callback) {
gitExec(["remote"], function(err, data) {
var remotes = (data ? data.toString().split("\n") : []);
callback(null, remotes.indexOf(remote) !== -1);
});
},
push: function(remote, refspec, callback) {
// No commits, no push
if (!this.shouldPush) {
callback(null);
return;
}
this.remoteExists(remote, function(err, exists) {
if (!exists) {
callback("Remote does not exist " + "(" + remote + ")");
return;
}
gitExec(["push", remote, refspec], function(err) {
if (err && err.toString().match(/^Error:/)) {
var lines = err.toString().split("\n");
callback("Push unsucessfull (" + lines[1] + ")");
return;
}
this.shouldPush = false;
callback(null);
});
});
},
log: function(path, version, howMany, callback) {
if (typeof howMany == 'function') {
callback = howMany;
howMany = 1;
}
gitExec(["log", "-" + howMany, "--reverse", "--no-notes", "--pretty=format:%h%n%H%n%an%n%ae%n%aD%n%ar%n%at%n%s", version, "--", docSubdir + path], function(err, data) {
var logdata = data ? data.toString().split("\n") : []
, group
, metadata = [];
for (var i = Math.floor(logdata.length / 8); i-- > 0; ) {
group = logdata.slice(i * 8, (i + 1) * 8);
metadata.push({
name: path.replace(".md", ""),
hash: group[0],
hashRef: group[0],
fullhash: group[1],
author: group[2],
email: group[3],
date: group[4],
relDate: group[5],
timestamp: group[6],
subject: group[7]
});
}
if (metadata[0]) {
metadata[0].hashRef = ''; // This can be used linking this version, but needs to be empty for HEAD
}
if (howMany == 1) {
metadata = metadata[0];
}
callback(null, metadata);
});
},
add: function(path, message, author, callback) {
gitExec(["add", docSubdir + path], function(err) {
if (err) {
callback(err);
} else {
this.commit(path, message, author, callback);
}
}.bind(this));
},
rm: function(path, message, author, callback) {
gitExec(["rm", docSubdir + path], function(err) {
if (err) {
callback(err);
} else {
this.commit(path, message, author, callback);
}
}.bind(this));
},
commit: function(path, message, author, callback) {
gitExec(["commit", "--author=\"" + author + "\"", "-m", message, docSubdir + path], function(err) {
this.shouldPush = true;
callback(err);
}.bind(this));
},
grep: function(pattern, callback) {
// TODO decide for -w
var args = [ "grep", "--no-color", "-F", "-n", "-i", "-I", pattern ];
if (docSubdir != "") {
args.push(docSubdir);
}
gitExec(args, function(err, data) {
var result;
if (data) {
result = data.toString().split("\n");
} else {
result = [];
}
// Search in the file names
gitExec([ "ls-files", docSubdir + "*" + pattern + "*.md" ], function(err, data) {
if (data) {
data.toString().split("\n").forEach(function(name) {
result.push(Path.basename(name));
});
}
callback(err, result);
});
});
},
diff: function(path, revisions, callback) {
gitExec([ "diff", "--no-color", "-b", revisions, "--", docSubdir + path ], function(err, data) {
callback(err, data.toString());
});
},
ls: function(callback) {
gitExec([ "ls-tree", "--name-only", "-r", "HEAD", docSubdir ], function(err, data) {
callback(null, data.toString().split("\n").filter(function(v) { return v!=""}));
});
}
};
})();