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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
const { spawn } = require("child_process");
const { formatDistance } = require('date-fns');
const fs = require('fs');
function execGit(path, action, args = [""], error)
{
const git = spawn("git", ["-C", path, action, args].concat(args));
git.on("error", (err) =>
{
const no_such_file_or_dir = new RegExp(`cannot change to '${path.replace('/', "\/")}': No such file or directory\\n$`);
if(no_such_file_or_dir.test(err.toString())) {
error({ "error": 404 })
return;
}
error({ "error": err });
});
git.stderr.on("data", (err) => error({ "error": "Failed to communicate with git!", "message": err.toString() }));
return git;
}
function getLog(base_dir, path)
{
return new Promise((resolve) =>
{
let log = [];
const log_format='{"hash":"%H","author":"%an","author_email":"%ae","date":"%at","subject":"%s"}';
const git = execGit(`${base_dir}/${path}`, "log", [`--format=format:${log_format}`, "--shortstat"], (err) => resolve(err));
git.stdout.on("data", (data) =>
{
data = data.toString().split('\n').filter((item) => item != "");
data[0] = JSON.parse(data[0]);
["files changed", "insertions", "deletions"].forEach((stat) =>
{
const stat_nr = new RegExp(`(\\d+)\\ ${stat.replaceAll(/s(?=(\ |$))/g, "s?")}`).exec(data[1]);
data[0][stat.replaceAll(" ", "_")] = stat_nr ? stat_nr[1] : 0;
});
log.push(data[0]);
});
git.on("close", (code) =>
{
if(code === 0) {
resolve({ "data": log });
return;
}
resolve({ "error": "Failed to communicate with git!" });
});
})
}
function getTimeSinceLatestCommit(path)
{
return new Promise((resolve) =>
{
const git = execGit(path, "log", [`--format=format:%at`, "-n 1"], (err) => resolve(err));
const commit = [];
git.stdout.on("data", (data) => commit.push(data));
git.on("close", (code) =>
{
resolve(formatDistance(new Date(), new Date(Number(Buffer.concat(commit).toString()) * 1000)));
});
});
}
function getRepoFile(base_dir, repo, file)
{
return new Promise(resolve =>
{
fs.readFile(`${base_dir}/${repo}/${file}`, async (err, content) =>
{
if(!err) {
resolve(content.toString().replaceAll('\n', ''));
return;
}
resolve("");
});
});
}
function getBasicRepoInfo(base_dir, repo_dirs)
{
return new Promise((resolve) =>
{
let repos = {};
repo_dirs.forEach(async (repo, index, arr) =>
{
const desc = await getRepoFile(base_dir, repo, "description");
const owner = await getRepoFile(base_dir, repo, "owner");
const last_commit_date = await getTimeSinceLatestCommit(`${base_dir}/${repo}`);
let repo_name = "";
repo_name = repo.slice(0, -4);
repos[repo_name] = { "description": desc, "owner": owner, "last_updated": last_commit_date };
if(index === 0) resolve(repos);
});
});
}
function getRepos(base_dir)
{
return new Promise((resolve) =>
{
fs.readdir(base_dir, async (err, content) =>
{
if(err) {
resolve({ "error": err });
return;
}
resolve({ "data": content });
});
});
}
function parseCommitFilePart(part)
{
let new_lines = [];
let deleted_lines = [];
let old_from;
let old_to;
let from;
let to;
part.forEach((line, index) =>
{
if(line.charAt(0) === '+') {
line = line.slice(1);
new_lines.push(index);
}
else if(line.charAt(0) === '-') {
line = line.slice(1);
deleted_lines.push(index);
}
else {
["+", "-"].forEach((char) =>
{
const find_char = new RegExp(`(?<=^<span.*>)\\${char}(?=.*<\/span>)`);
if(find_char.test(line)) {
console.log(`${char} ${line}`);
const char_index = find_char.exec(line)["index"];
line = line.slice(0, char_index) + line.slice(char_index + 1)
if(char === "+") {
new_lines.push(index);
}
else if(char === "-") {
deleted_lines.push(index);
}
}
})
}
part[index] = line;
});
if(/^@@\ -[0-9,]+\ \+[0-9,]+\ @@/.test(part[0])) {
const from_to = /^@@\ (-[0-9,]+)\ (\+[0-9,]+)\ @@(?:\ (.*))?/.exec(part[0]);
old_from = from_to[1].split(',')[0].slice(1);
old_to = from_to[1].split(',')[1];
from = from_to[2].split(',')[0].slice(1);
to = from_to[2].split(',')[1];
}
else {
old_from = 1;
old_to = part.length - new_lines.length;
from = 1;
to = part.length - deleted_lines.length;
}
return { "new_lines": new_lines, "deleted_lines": deleted_lines, "old_from": old_from, "old_to": old_to, "from": from, "to": to, "part": part.join("\n") };
}
function getCommit(base_dir, repo, hash)
{
return new Promise((resolve) =>
{
const git = execGit(`${base_dir}/${repo}`, "show", ['--format=format:{\"hash\": \"%H\", \"author\": \"%an <%ae>\", \"date\": \"%at\", \"message\": \"%s\"}', hash], (err) => resolve(err));
let commit = [];
git.stdout.on("data", (data) =>
{
commit.push(data);
});
git.on("close", () =>
{
let diff = commit.toString().split('\n').slice(1);
var result = [];
let start;
diff.forEach((line, index) =>
{
if(/^diff\ --git a\/[^\ ]+\ b\/[^\ ]+$/.test(line) || index === diff.length - 1) {
if(start != undefined) {
let file_diff = diff.slice(start, index - 1);
let chunk_header_index = file_diff.findIndex((line) => /^@@\ -[0-9,]+\ \+[0-9,]+\ @@/.test(line));
if(chunk_header_index === -1) {
chunk_header_index = file_diff.length;
}
let file_info = {};
let header;
if(chunk_header_index != file_diff.length) {
const from_to = file_diff.slice(chunk_header_index - 2, chunk_header_index);
file_info["from"] = from_to[0].slice(4);
file_info["to"] = from_to[1].slice(4);
const chunk_header = /^@@\ (-[0-9,]+)\ (\+[0-9,]+)\ @@(?:\ (.*))?/.exec(file_diff[chunk_header_index]);
file_info["from_file_range"] = chunk_header[1];
file_info["to_file_range"] = chunk_header[2];
let raw_diff = file_diff.slice(chunk_header_index + 1);
let parsed_diff = [];
let last_diff_start = 0;
raw_diff.forEach((diff_line, diff_index) =>
{
if(/^@@\ -[0-9,]+\ \+[0-9,]+\ @@/.test(diff_line)) {
let part = parseCommitFilePart(raw_diff.slice(last_diff_start, diff_index));
parsed_diff.push(part);
last_diff_start = diff_index;
}
else if(diff_index === raw_diff.length - 1) {
let part = parseCommitFilePart(raw_diff.slice(last_diff_start, diff_index));
parsed_diff.push(part);
}
});
console.log(parsed_diff);
file_info["diff"] = parsed_diff;
if(chunk_header[3]) {
file_info["diff"][0]["part"] = chunk_header[3] + parsed_diff[0]["part"];
}
header = file_diff.slice(1, chunk_header_index - 2);
}
else {
const from_to = /^diff\ --git (a\/[^\ ]+)\ (b\/[^\ ]+)$/.exec(file_diff[0]);
file_info["from"] = from_to[1];
file_info["to"] = from_to[2];
header = file_diff.slice(1, chunk_header_index);
}
header.forEach((line) =>
{
if(line.includes("old mode") || line.includes("new mode") || line.includes("deleted file mode") || line.includes("new file mode")) {
const data = /^(.*mode)\ (\d{6})$/.exec(line);
file_info[data[1].replaceAll(' ', "_")] = data[2];
}
else if(line.includes("copy from") || line.includes("copy to")) {
const data = /^(copy\ from|to)\ (.*)/.exec(line);
file_info[data[1].replaceAll(' ', "_")] = data[2];
}
else if(line.includes("rename from") || line.includes("rename to")) {
const data = /^(rename\ from|to)\ (.*)/.exec(line);
file_info[data[1].replaceAll(' ', "_")] = data[2];
}
else if(line.includes("similarity index") || line.includes("dissimilarity index")) {
const data = /^((?:dis)?similarity\ index)\ (\d+%)$/.exec(line);
file_info[data[1].replaceAll(' ', "_")] = data[2];
}
else if(line.includes("index")) {
const data = /^index\ ([0-9a-f,]+)\.\.([0-9a-f,]+)(?:\ ([0-9,]+))?$/.exec(line).slice(1);
file_info["index"] = { "before": data[0], "after": data[1] };
if(data[2]) {
file_info["index"]["mode"] = data[2];
}
}
});
result.push(file_info);
}
start = index;
}
if(index === diff.length - 1) {
let data = JSON.parse(commit.toString().split('\n').slice(0,1)[0]);
data["files"] = result;
resolve({ "data": data });
}
});
});
})
}
module.exports.getLog = getLog;
module.exports.getBasicRepoInfo = getBasicRepoInfo;
module.exports.getRepos = getRepos;
module.exports.getRepoFile = getRepoFile;
module.exports.getCommit = getCommit;
|