-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_stats.js
More file actions
187 lines (163 loc) · 4.83 KB
/
git_stats.js
File metadata and controls
187 lines (163 loc) · 4.83 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
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
const path = require("path");
const exec = require("child_process").exec
function create_stats(input) {
const lines = input.split('\n');
let state = 0;
let author = null;
const files = new Set();
const aliases = {};
const authors = new Set();
const by_author_and_file = {};
const by_file_and_author = {};
for (const line of lines) {
if (line.startsWith("commit")) {
state = 0;
const parts = line.split(' ');
author = parts[2];
authors.add(author);
}
if (line.length == 0) {
state = 1;
}
if (state == 1 && line.length > 0) {
state = 2;
}
if (state == 2) {
by_author_and_file[author] = by_author_and_file[author] || {};
const parts = line.split('\t');
const insertions = +parts[0];
const deletions = +parts[1];
const file = (() => {
const oldName = parts[2].replace(/{([^}]+)}/g, (_, inner) => {
return inner.split(" => ")[0];
});
const newName = parts[2].replace(/{([^}]+)}/g, (_, inner) => {
return inner.split(" => ")[1];
});
if (newName !== oldName) {
aliases[oldName] = newName;
}
return aliases[newName] || newName;
})();
files.add(file);
if (Number.isNaN(insertions) || Number.isNaN(insertions) || !file) {
console.error(`Could not read stat: ${line}`);
continue;
}
by_file_and_author[file] = by_file_and_author[file] || {};
by_author_and_file[author][file] = by_author_and_file[author][file] || [0,0];
by_author_and_file[author][file][0] += insertions;
by_author_and_file[author][file][1] += deletions;
by_file_and_author[file][author] = by_file_and_author[file][author] || [0,0];
by_file_and_author[file][author][0] += insertions;
by_file_and_author[file][author][1] += deletions;
}
}
return { files, authors, by_author_and_file, by_file_and_author };
}
function print_stats_by_author({ files, authors, by_author_and_file, by_file_and_author }) {
const authorNames = (() => {
const authorNames = [...authors];
authorNames.sort();
return authorNames;
})();
const fileNames = (() => {
const fileNames = [...files];
fileNames.sort();
return fileNames.map(name => {
const shortName = name.split('/').map((part, index, arr) => {
if (index < arr.length - 1) {
return part[0];
} else {
return part;
}
}).join('/')
return [shortName, name];
});
})();
console.log(`author,${fileNames.map(names => names[0]).join(',')}`);
for (const author of authorNames) {
let line = `${author}`;
for ([_, fileName] of fileNames) {
const [insertions, deletions] = (by_author_and_file[author] || {})[fileName] || [0,0];
line += `,${insertions + deletions}`;
}
console.log(line);
}
}
function print_stats_by_file({ files, authors, by_author_and_file, by_file_and_author }) {
const authorNames = (() => {
const authorNames = [...authors];
authorNames.sort();
return authorNames;
})();
const fileNames = (() => {
const fileNames = [...files];
fileNames.sort();
return fileNames.map(name => {
const shortName = name.split('/').map((part, index, arr) => {
if (index < arr.length - 1) {
return part[0];
} else {
return part;
}
}).join('/')
return [shortName, name];
});
})();
console.log(`filename,${authorNames.join(',')}`);
for (const [shortFileName, fileName] of fileNames) {
let line = `${shortFileName}`;
for (const authorName of authorNames) {
const [insertions, deletions] = (by_file_and_author[fileName] || {})[authorName] || [0,0];
line += `,${insertions + deletions}`;
}
console.log(line);
}
}
function printUsage() {
const scriptName = path.basename(process.argv[1]);
console.log("####################");
console.log("# Git change stats #");
console.log("####################");
console.log();
console.log("Show accumulated changes for all files and authors.");
console.log("Print stats in CSV format to stdout.");
console.log();
console.log("USAGE");
console.log(`\tnode ${scriptName} --by-author|--by-file [-- additional git args]`);
console.log();
console.log("Example");
console.log(`\tnode ${scriptName} --by-file -- --since=01.01.2022 > by_file.csv`);
}
const extraGitArgs = (() => {
const index = process.argv.indexOf("--");
if (index !== -1) {
return process.argv.slice(index + 1).join(' ');
} else {
return "";
}
})();
exec(`git log --numstat --format="commit %H %an" ${extraGitArgs}`, (err, stdout, stderr) => {
if (!err) {
let mode = null;
if (new Set(process.argv).has("--by-author")) {
mode = "by-author";
} else if (new Set(process.argv).has("--by-file")) {
mode = "by-file";
} else {
printUsage();
process.exit(0);
}
const output = stdout.toString();
const stats = create_stats(output);
if (mode == "by-author") {
print_stats_by_author(stats);
} else if (mode == "by-file") {
print_stats_by_file(stats);
}
} else {
console.log(`ERR: ${err}`);
console.log(stderr.toString());
}
})