-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
278 lines (261 loc) · 7.16 KB
/
index.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
275
276
277
278
const child_process = require("child_process");
const crypto = require("crypto");
const commander = require("commander");
const fs = require("fs");
const os = require("os");
const path = require("path");
const table = require("tty-table");
const zlib = require("zlib");
const ENGINE_OPTIONS = { quiet: false };
const LOG = (s) => ENGINE_OPTIONS.quiet || process.stderr.write(s);
async function analyze(options) {
const changedPaths = getChangedPaths(options);
const gitLog = await getGitLog(options, changedPaths);
const analysis = await analyzeCoupling(
groupPathsByCommit(gitLog),
changedPaths
);
return generateReport(analysis, options);
}
const formatConfidence = (x) => Math.round(x * 100) / 100;
function printTable(result) {
const header = [
{ value: "Changed file", align: "left" },
{ value: "Coupled file", align: "left" },
{ value: "Shared commits", align: "right" },
{ value: "Confidence", align: "right" },
];
const rows = result.map(
({ path, coupledPath, sharedCommits, confidence }) => [
path,
coupledPath,
sharedCommits,
formatConfidence(confidence),
]
);
const options = {};
process.stdout.write(table(header, rows, options).render());
process.stdout.write("\n");
}
function printCSV(result) {
process.stdout.write("Changed file,Coupled file,Shared commits,Confidence\n");
for (const { path, coupledPath, sharedCommits, confidence } of result) {
process.stdout.write(
`${path},${coupledPath},${sharedCommits},${formatConfidence(
confidence
)}\n`
);
}
}
/** Generate CSV report with top untouched coupled files above threshold.
*/
function generateReport({ commitCount, frequencies }, { threshold, ntop }) {
LOG("Generating report...");
const result = [];
for (const [path, coupled] of Object.entries(frequencies)) {
Object.entries(coupled)
.map(([coupledPath, sharedCommits]) => [
coupledPath,
sharedCommits,
sharedCommits / commitCount[coupledPath],
])
// [2] is confidence
.filter((x) => x[2] >= threshold)
.sort((a, b) => b[2] - a[2])
.slice(0, ntop)
.forEach(([coupledPath, sharedCommits, confidence]) =>
result.push({ path, coupledPath, sharedCommits, confidence })
);
}
LOG(" Enjoy!\n");
return result;
}
/** Stream of paths changed in the same commit.
*
* PEG:
*
* output <- (commit '\n')*
* commit <- '\n' (path '\n')*
* path <- [^\n]+
*/
async function getGitLog(options, changedPaths) {
if (!options.cache) {
return getLogFromGit(options, changedPaths);
}
LOG("Is cache valid?");
const masterHash = getMasterHash(options);
if (fs.existsSync(options.cacheFile)) {
const f = await splitLines(
fs.createReadStream(options.cacheFile).pipe(zlib.createGunzip())
);
const cacheHash = (await f.next()).value;
if (cacheHash === masterHash) {
LOG(" Yes!\n");
return f;
}
}
LOG(" Nope.\n");
return withCache(
getLogFromGit(options, changedPaths),
options.cacheFile,
masterHash
);
}
function getChangedPaths({ master }) {
LOG("Getting changed paths...");
const masterDiff = child_process
.spawnSync("git", ["diff", "--name-only", "--no-renames", `${master}...`], {
encoding: "utf8",
})
.stdout.split("\n");
const uncommittedDiff = child_process
.spawnSync("git", ["diff", "--name-only", "--no-renames", "HEAD"], {
encoding: "utf8",
})
.stdout.split("\n");
const result = masterDiff.concat(uncommittedDiff).filter((x) => x.length > 0);
LOG(` Done.\n`);
LOG(`${result.length} files changed against the ${master}\n`);
return result;
}
async function* getLogFromGit({ master }, changedPaths) {
const lines = splitLines(
spawnOut("git", [
"log",
"--all",
"--name-only",
"--pretty=format:",
"--no-renames",
"--no-merges",
master,
])
);
const fsCache = {};
// Even if some files in changedPaths are deleted in the current changeset,
// we still want to treat them as existing in the log.
for (const path of changedPaths) {
fsCache[path] = true;
}
const fileExists = (path) => {
if (!fsCache.hasOwnProperty(path)) {
fsCache[path] = fs.existsSync(path);
}
return fsCache[path];
};
for await (const line of lines) {
if (line.length === 0 || fileExists(line)) {
yield line;
}
}
}
function getMasterHash({ master }) {
return child_process
.spawnSync("git", ["rev-parse", master], { encoding: "utf8" })
.stdout.trim();
}
async function* groupPathsByCommit(lines) {
let paths = [];
for await (const line of lines) {
if (line.length > 0) {
paths.push(line);
} else if (paths.length > 0) {
yield paths;
paths = [];
}
}
}
async function* withCache(lines, cacheFile, key) {
// This is not secure (ref. https://en.wikipedia.org/wiki/Symlink_race)
// but good enough for our purposes.
const tmpCacheFile = path.join(
os.tmpdir(),
`did-i-forget-cache-${crypto.randomBytes(8).toString("hex")}`
);
const f = fs.createWriteStream(tmpCacheFile);
let cacheFailed = false;
f.on("error", () => {
cacheFailed = true;
});
f.on("close", () =>
cacheFailed
? fs.unlinkSync(tmpCacheFile)
: fs.renameSync(tmpCacheFile, cacheFile)
);
const cache = zlib.createGzip();
cache.pipe(f);
cache.write(key);
cache.write("\n");
for await (const line of lines) {
cache.write(line);
cache.write("\n");
yield line;
}
cache.end();
}
async function* splitLines(chunks, maxBuffer = 1024) {
const NEWLINE = "\n".charCodeAt(0);
const line = Buffer.alloc(maxBuffer);
let end = 0;
for await (const chunk of chunks) {
for (const byte of chunk) {
if (byte === NEWLINE) {
yield line.slice(0, end).toString();
end = 0;
} else {
line[end] = byte;
end += 1;
}
}
}
}
function partition(xs, p) {
const t = [];
const f = [];
for (const x of xs) {
(p(x) ? t : f).push(x);
}
return [t, f];
}
async function analyzeCoupling(commits, changedPaths) {
const commitCount = {};
const frequencies = {};
for (const p of changedPaths) {
frequencies[p] = {};
}
const changedPathsSet = new Set(changedPaths);
let progress = 0;
for await (const paths of commits) {
const [changedPathsInCommit, unchangedPaths] = partition(paths, (p) =>
changedPathsSet.has(p)
);
for (const path of paths) {
commitCount[path] = (commitCount[path] || 0) + 1;
}
for (const changedPath of changedPathsInCommit) {
for (const unchangedPath of unchangedPaths) {
frequencies[changedPath][unchangedPath] =
(frequencies[changedPath][unchangedPath] || 0) + 1;
}
}
progress += 1;
if (progress < 1000 || progress % 1000 === 0) {
LOG(`\rProcessed ${progress} commits...`);
}
}
LOG(`\rProcessed ${progress} commits. \n`);
return { commitCount, frequencies };
}
async function* spawnOut(cmd, args = [], options = {}) {
for await (const chunk of child_process.spawn(cmd, args, {
...options,
stdio: ["ignore", "pipe", "ignore"],
}).stdout) {
yield chunk;
}
}
module.exports = {
analyze,
printCSV,
printTable,
ENGINE_OPTIONS,
};