-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcommitters.svc.ts
272 lines (217 loc) · 6.78 KB
/
committers.svc.ts
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
export interface CommitEntry {
month: string;
author: string;
}
export interface AuthorCommitCounts {
[author: string]: number;
}
export interface MonthlyData {
[month: string]: AuthorCommitCounts;
}
export interface ReportData {
monthly: {
[month: string]: {
[author: string]: number;
total: number;
};
};
overall: {
[author: string]: number;
total: number;
};
}
/**
* Parses git log output into structured data
* @param output - Git log command output
* @returns Parsed commit entries
*/
export function parseGitLogOutput(output: string): CommitEntry[] {
return output
.split('\n')
.filter(Boolean)
.map((line) => {
// Remove surrounding double quotes if present (e.g. "March|John Doe" → March|John Doe)
const [month, author] = line.replace(/^"(.*)"$/, '$1').split('|');
return { month, author };
});
}
/**
* Groups commit data by month
* @param entries - Commit entries
* @returns Object with months as keys and author commit counts as values
*/
export function groupCommitsByMonth(entries: CommitEntry[]): MonthlyData {
const result: MonthlyData = {};
// Group commits by month
const commitsByMonth = entries.reduce<Record<string, CommitEntry[]>>((acc, entry) => {
const monthKey = entry.month;
if (!acc[monthKey]) {
acc[monthKey] = [];
}
acc[monthKey].push(entry);
return acc;
}, {});
// Process each month
for (const [month, commits] of Object.entries(commitsByMonth)) {
if (!commits) {
result[month] = {};
continue;
}
// Count commits per author for this month
const commitsByAuthor = commits.reduce<Record<string, CommitEntry[]>>((acc, entry) => {
const authorKey = entry.author;
if (!acc[authorKey]) {
acc[authorKey] = [];
}
acc[authorKey].push(entry);
return acc;
}, {});
const authorCounts: AuthorCommitCounts = {};
for (const [author, authorCommits] of Object.entries(commitsByAuthor)) {
authorCounts[author] = authorCommits?.length ?? 0;
}
result[month] = authorCounts;
}
return result;
}
/**
* Calculates overall commit statistics by author
* @param entries - Commit entries
* @returns Object with authors as keys and total commit counts as values
*/
export function calculateOverallStats(entries: CommitEntry[]): AuthorCommitCounts {
const commitsByAuthor = entries.reduce<Record<string, CommitEntry[]>>((acc, entry) => {
const authorKey = entry.author;
if (!acc[authorKey]) {
acc[authorKey] = [];
}
acc[authorKey].push(entry);
return acc;
}, {});
const result: AuthorCommitCounts = {};
// Count commits for each author
for (const author in commitsByAuthor) {
result[author] = commitsByAuthor[author]?.length ?? 0;
}
return result;
}
/**
* Formats monthly report sections
* @param monthlyData - Grouped commit data by month
* @returns Formatted monthly report sections
*/
export function formatMonthlyReport(monthlyData: MonthlyData): string {
const sortedMonths = Object.keys(monthlyData).sort();
let report = '';
for (const month of sortedMonths) {
report += `\n## ${month}\n`;
const authors = Object.entries(monthlyData[month]).sort((a, b) => b[1] - a[1]);
for (const [author, count] of authors) {
report += `${count.toString().padStart(6)} ${author}\n`;
}
const monthTotal = authors.reduce((sum, [_, count]) => sum + count, 0);
report += `${monthTotal.toString().padStart(6)} TOTAL\n`;
}
return report;
}
/**
* Formats overall statistics section
* @param overallStats - Overall commit counts by author
* @param grandTotal - Total number of commits
* @returns Formatted overall statistics section
*/
export function formatOverallStats(overallStats: AuthorCommitCounts, grandTotal: number): string {
let report = '\n## Overall Statistics\n';
const sortedStats = Object.entries(overallStats).sort((a, b) => b[1] - a[1]);
for (const [author, count] of sortedStats) {
report += `${count.toString().padStart(6)} ${author}\n`;
}
report += `${grandTotal.toString().padStart(6)} GRAND TOTAL\n`;
return report;
}
/**
* Formats the report data as CSV
* @param data - The structured report data
*/
export function formatAsCsv(data: ReportData): string {
// First prepare all author names (for columns)
const allAuthors = new Set<string>();
// Collect all unique author names
for (const monthData of Object.values(data.monthly)) {
for (const author of Object.keys(monthData)) {
if (author !== 'total') allAuthors.add(author);
}
}
const authors = Array.from(allAuthors).sort();
// Create CSV header
let csv = `Month,${authors.join(',')},Total\n`;
// Add monthly data rows
const sortedMonths = Object.keys(data.monthly).sort();
for (const month of sortedMonths) {
csv += month;
// Add data for each author
for (const author of authors) {
const count = data.monthly[month][author] || 0;
csv += `,${count}`;
}
// Add monthly total
csv += `,${`${data.monthly[month].total}\n`}`;
}
// Add overall totals row
csv += 'Overall';
for (const author of authors) {
const count = data.overall[author] || 0;
csv += `,${count}`;
}
csv += `,${data.overall.total}\n`;
return csv;
}
/**
* Formats the report data as text
* @param data - The structured report data
*/
export function formatAsText(data: ReportData): string {
let report = 'Monthly Commit Report\n';
// Monthly sections
const sortedMonths = Object.keys(data.monthly).sort();
for (const month of sortedMonths) {
report += `\n## ${month}\n`;
const authors = Object.entries(data.monthly[month])
.filter(([author]) => author !== 'total')
.sort((a, b) => b[1] - a[1]);
for (const [author, count] of authors) {
report += `${count.toString().padStart(6)} ${author}\n`;
}
report += `${data.monthly[month].total.toString().padStart(6)} TOTAL\n`;
}
// Overall statistics
report += '\n## Overall Statistics\n';
const sortedEntries = Object.entries(data.overall)
.filter(([author]) => author !== 'total')
.sort((a, b) => b[1] - a[1]);
for (const [author, count] of sortedEntries) {
report += `${count.toString().padStart(6)} ${author}\n`;
}
report += `${data.overall.total.toString().padStart(6)} GRAND TOTAL\n`;
return report;
}
/**
* Format output based on user preference
* @param output
* @param reportData
* @returns
*/
export function formatOutputBasedOnFlag(output: string, reportData: ReportData) {
let formattedOutput: string;
switch (output) {
case 'json':
formattedOutput = JSON.stringify(reportData, null, 2);
break;
case 'csv':
formattedOutput = formatAsCsv(reportData);
break;
default:
formattedOutput = formatAsText(reportData);
}
return formattedOutput;
}