-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollector.ts
More file actions
482 lines (430 loc) · 16.9 KB
/
Copy pathcollector.ts
File metadata and controls
482 lines (430 loc) · 16.9 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import { CommitData, FileChange, GitSparkOptions, ProgressCallback } from '../types/index.js';
import { GitExecutor } from '../utils/git.js';
import { validateGitOptions } from '../utils/input-validation.js';
import { createLogger } from '../utils/logger.js';
// import { validateCommitMessage } from '../utils/validation.js';
const logger = createLogger('collector');
/**
* Git repository data collection engine
*
* The DataCollector is responsible for efficiently extracting raw data from Git repositories
* using git commands. It handles commit history traversal, file change analysis, and progress
* tracking for large repositories.
*
* Key features:
* - Efficient batch processing of git log data
* - Comprehensive file change tracking with diff statistics
* - Memory-optimized streaming for large repositories
* - Robust error handling and validation
* - Progress reporting for long-running operations
* - Flexible filtering by date, author, branch, and file patterns
*
* @example
* ```typescript
* const collector = new DataCollector('/path/to/repo', (progress) => {
* console.log(`${progress.stage}: ${progress.percentage}%`);
* });
*
* const commits = await collector.collectCommits({
* since: '2024-01-01',
* author: 'john@example.com',
* maxCommits: 1000
* });
*
* console.log(`Collected ${commits.length} commits`);
* ```
*/
export class DataCollector {
private git: GitExecutor;
private progressCallback?: ProgressCallback | undefined;
private lastWarnings: string[] = [];
private repoPath: string;
/**
* Create a new DataCollector instance
*
* @param repoPath - Absolute path to the Git repository
* @param progressCallback - Optional callback for progress updates during collection
* @throws {Error} When repository path is invalid or git is not accessible
*
* @example
* ```typescript
* // Basic usage
* const collector = new DataCollector('/path/to/repo');
*
* // With progress tracking
* const collector = new DataCollector('/path/to/repo', (progress) => {
* console.log(`${progress.stage}: ${progress.current}/${progress.total}`);
* });
* ```
*/
constructor(repoPath: string, progressCallback?: ProgressCallback | undefined) {
this.repoPath = repoPath;
this.git = new GitExecutor(repoPath);
this.progressCallback = progressCallback;
}
/**
* Get the repository path
* @returns The absolute path to the Git repository
*/
getRepositoryPath(): string {
return this.repoPath;
}
/**
* Collect commit data from Git repository with comprehensive metadata
*
* Efficiently processes git log to extract commit information including:
* - Basic commit metadata (hash, author, date, message)
* - File change statistics (additions, deletions, modifications)
* - Diff analysis for each changed file
* - Merge commit handling and parent tracking
*
* @param options - Collection options and filters
* @param options.since - Start date for commit range (ISO string or git date format)
* @param options.until - End date for commit range (ISO string or git date format)
* @param options.branch - Specific branch to analyze (default: current branch)
* @param options.author - Filter commits by author email or name
* @param options.maxCommits - Maximum number of commits to collect (for performance)
* @param options.excludePatterns - File patterns to exclude from analysis
*
* @returns Promise resolving to array of processed commit data
* @throws {Error} When git operations fail or repository is inaccessible
*
* @example
* ```typescript
* // Collect all commits from last 6 months
* const commits = await collector.collectCommits({
* since: '2024-06-01',
* maxCommits: 5000
* });
*
* // Collect commits from specific author
* const commits = await collector.collectCommits({
* author: 'developer@company.com',
* branch: 'main'
* });
* ```
*/
async collectCommits(options: GitSparkOptions): Promise<CommitData[]> {
this.reportProgress('Collecting commit data', 0, 100);
const warnings: string[] = [];
// Get the first commit date in the repository to cap the analysis range
const firstCommitDate = await this.git.getFirstCommitDate(options.branch);
logger.debug(`Repository first commit date: ${firstCommitDate?.toISOString() || 'null'}`);
// Derive since date from --days if user supplied days but not an explicit since
if (!options.since && options.days && options.days > 0) {
const d = new Date();
d.setDate(d.getDate() - options.days);
// Use ISO date (no time) to let git interpret midnight boundary
options.since = d.toISOString().split('T')[0];
logger.debug(`Calculated since date from --days=${options.days}: ${options.since}`);
}
// Cap the analysis range to repository lifetime
if (options.since && firstCommitDate) {
const requestedStartDate = new Date(options.since);
logger.debug(
`Comparing dates: requested=${requestedStartDate.toISOString()}, firstCommit=${firstCommitDate.toISOString()}`
);
if (requestedStartDate < firstCommitDate) {
// Convert to local date at start of day to ensure we include the first commit
// Subtract one day to ensure we capture commits on the first commit date
const adjustedDate = new Date(firstCommitDate);
adjustedDate.setDate(adjustedDate.getDate() - 1);
options.since = adjustedDate.toISOString().split('T')[0];
logger.info(
`Analysis start date is before repository first commit. Adjusting range to start from ${options.since}.`
);
}
}
const gitOptions: { since?: string; until?: string; branch?: string; author?: string } = {};
if (options.since) gitOptions.since = options.since;
if (options.until) gitOptions.until = options.until;
if (options.branch) gitOptions.branch = options.branch;
if (options.author) gitOptions.author = options.author;
const totalCommits = await this.git.getCommitCount(gitOptions);
logger.info(`Collecting up to ${totalCommits} commits (streaming)`, { options });
// Build streaming git log command with safe delimiters
// Use unit separator (0x1F) for fields and record separator (0x1E) for commits
// Validate and sanitize all input parameters to prevent command injection
const gitParams: Record<string, string> = {};
if (gitOptions.since) gitParams.since = gitOptions.since;
if (gitOptions.until) gitParams.until = gitOptions.until;
if (gitOptions.author) gitParams.author = gitOptions.author;
if (gitOptions.branch) gitParams.branch = gitOptions.branch;
if (options.path) gitParams.path = options.path;
const validation = validateGitOptions(gitParams);
if (!validation.isValid) {
throw new Error(`Invalid Git parameters: ${validation.errors.join(', ')}`);
}
const safeOptions = validation.sanitized!;
// We prefix each commit with a record separator so we can split reliably even for large bodies.
// Format: \x1e<fields...> then numstat lines, then the next commit begins with \x1e
const args: string[] = [
'log',
'--no-merges',
'--numstat',
`--pretty=format:%x1e%H%x1f%h%x1f%an%x1f%ae%x1f%ai%x1f%s%x1f%b%x1f%P`,
];
if (safeOptions.since) args.push('--since', safeOptions.since);
if (safeOptions.until) args.push('--until', safeOptions.until);
if (safeOptions.author) args.push('--author', safeOptions.author);
if (safeOptions.path) args.push('--', safeOptions.path);
if (safeOptions.branch) args.push(safeOptions.branch);
const { spawn } = await import('child_process');
const child = spawn('git', args, { cwd: (this.git as any).repoPath });
const commits: CommitData[] = [];
let buffer = '';
let processed = 0;
let currentCommit: Partial<CommitData> | null = null;
const finalizeCurrent = () => {
if (currentCommit && currentCommit.hash) {
const finalized = this.finalizeCommit(currentCommit);
commits.push(finalized);
currentCommit = null;
}
};
child.stdout.on('data', (chunk: Buffer) => {
buffer += chunk.toString();
// Split on record separator (each commit starts with \x1e)
const records = buffer.split('\x1e');
buffer = records.pop() || '';
for (const record of records) {
if (!record.trim()) continue;
// We need 7 field separators (\x1f) to yield 8 header fields (hash..parents)
// The commit body (%b) may contain newlines, so the header can span multiple lines
// until the parents field is encountered. Only after the parents field do file
// change (numstat) lines begin on subsequent lines.
// Count field separators (0x1F) without regex to satisfy lint (no-control-regex)
let fieldSepCount = 0;
for (let i = 0; i < record.length; i++) {
if (record.charCodeAt(i) === 0x1f) fieldSepCount++;
}
if (fieldSepCount < 7) {
// Incomplete header (body truncated across chunk) - re-buffer
buffer = '\x1e' + record + buffer;
continue;
}
// Find the index of the 7th separator to know where parents field starts
let sepCount = 0;
let idx = -1;
for (let i = 0; i < record.length; i++) {
if (record[i] === '\x1f') {
sepCount++;
if (sepCount === 7) {
// after this comes the parents field value
// Find newline ending the parents field
const newlineAfterParents = record.indexOf('\n', i + 1);
if (newlineAfterParents === -1) {
// Parents field not terminated yet; wait for more data
buffer = '\x1e' + record + buffer;
idx = -1;
} else {
idx = newlineAfterParents; // header ends here
}
break;
}
}
}
if (idx === -1) {
continue; // wait for more data
}
const headerLine = record.slice(0, idx);
const parts = headerLine.split('\x1f');
if (parts.length < 8) {
warnings.push('Malformed commit header encountered (insufficient fields)');
continue;
}
currentCommit = this.parseCommitHeader(parts);
// Remainder after header newline are numstat lines (may contain blank line at end)
const remainder = record.slice(idx + 1);
if (remainder) {
const lines = remainder.split('\n');
for (const l of lines) {
if (!l.trim()) continue;
const fileChange = this.parseFileStats(l.replace(/\r$/, ''));
if (fileChange) {
currentCommit.files = currentCommit.files || [];
currentCommit.files.push(fileChange);
}
}
}
finalizeCurrent();
processed++;
if (processed % 200 === 0 || processed === totalCommits) {
const pct = totalCommits ? Math.min(100, Math.round((processed / totalCommits) * 70)) : 0;
this.reportProgress('Streaming commit collection', pct, 100);
}
}
});
await new Promise<void>((resolve, reject) => {
child.on('error', (e: Error) => reject(e));
child.on('close', () => {
// Process any remaining buffered (partial) record
if (buffer.trim()) {
const newlineIdx = buffer.indexOf('\n');
if (newlineIdx !== -1) {
const headerLine = buffer.slice(0, newlineIdx);
let fieldSepCount = 0;
for (let i = 0; i < headerLine.length; i++) {
if (headerLine.charCodeAt(i) === 0x1f) fieldSepCount++;
}
if (fieldSepCount >= 7) {
const parts = headerLine.split('\x1f');
if (parts.length >= 8) {
currentCommit = this.parseCommitHeader(parts);
const remainder = buffer.slice(newlineIdx + 1);
const lines = remainder ? remainder.split('\n') : [];
for (const l of lines) {
if (!l.trim()) continue;
const fc = this.parseFileStats(l);
if (fc) {
currentCommit.files = currentCommit.files || [];
currentCommit.files.push(fc);
}
}
finalizeCurrent();
}
}
}
}
resolve();
});
});
this.reportProgress('Enhancing commits', 80, 100);
for (let i = 0; i < commits.length; i++) {
try {
commits[i] = await this.enhanceCommit(commits[i]);
} catch (e: any) {
warnings.push(`Enhancement failed for ${commits[i].hash}: ${e.message || 'unknown'}`);
}
if (i % 250 === 0) {
const pct = 80 + Math.min(15, Math.round((i / commits.length) * 15));
this.reportProgress('Enhancing commits', pct, 100);
}
}
this.reportProgress('Commit collection complete', 100, 100);
if (warnings.length) {
logger.warn(`Completed with ${warnings.length} warnings`);
}
this.lastWarnings = warnings.slice();
return commits;
}
// Legacy parseCommitLog removed in favor of streaming parser above
private parseCommitHeader(parts: string[]): Partial<CommitData> {
const [hash, shortHash, author, authorEmail, date, subject, body, parents] = parts;
return {
hash: hash.trim(),
shortHash: shortHash.trim(),
author: author.trim(),
authorEmail: authorEmail.trim(),
date: new Date(date.trim()),
subject: subject.trim(),
body: body.trim(),
message: `${subject.trim()}\n${body.trim()}`.trim(),
isMerge: parents ? parents.trim().split(' ').length > 1 : false,
files: [],
insertions: 0,
deletions: 0,
filesChanged: 0,
isCoAuthored: false,
coAuthors: [],
};
}
private parseFileStats(line: string): FileChange | null {
const match = line.match(/^(\d+|\-)\s+(\d+|\-)\s+(.+)$/);
if (!match) return null;
const [, insertions, deletions, path] = match;
// Handle binary files
const ins = insertions === '-' ? 0 : parseInt(insertions, 10);
const dels = deletions === '-' ? 0 : parseInt(deletions, 10);
// Determine file status and handle renames
let status: FileChange['status'] = 'modified';
let filePath = path;
let oldPath: string | undefined;
if (path.includes(' => ')) {
status = 'renamed';
const parts = path.split(' => ');
oldPath = parts[0].trim();
filePath = parts[1].trim();
} else if (ins > 0 && dels === 0) {
status = 'added';
} else if (ins === 0 && dels > 0) {
status = 'deleted';
}
const result: FileChange = {
path: filePath,
insertions: ins,
deletions: dels,
status,
};
if (oldPath) {
result.oldPath = oldPath;
}
return result;
}
private finalizeCommit(commit: Partial<CommitData>): CommitData {
// Calculate totals from file changes
const insertions = commit.files?.reduce((sum, f) => sum + f.insertions, 0) || 0;
const deletions = commit.files?.reduce((sum, f) => sum + f.deletions, 0) || 0;
const filesChanged = commit.files?.length || 0;
// Detect co-authored commits
const coAuthorMatches = commit.body?.match(/Co-authored-by: (.+) <(.+)>/g) || [];
const coAuthors = coAuthorMatches
.map(match => {
const authorMatch = match.match(/Co-authored-by: (.+) <(.+)>/);
return authorMatch ? authorMatch[1] : '';
})
.filter(Boolean);
return {
hash: commit.hash!,
shortHash: commit.shortHash!,
author: commit.author!,
authorEmail: commit.authorEmail!,
date: commit.date!,
message: commit.message!,
subject: commit.subject!,
body: commit.body || '',
insertions,
deletions,
filesChanged,
isMerge: commit.isMerge!,
isCoAuthored: coAuthors.length > 0,
coAuthors,
files: commit.files || [],
};
}
private async enhanceCommit(commit: CommitData): Promise<CommitData> {
// Add commit message analysis
// const messageAnalysis = validateCommitMessage(commit.message);
// You could add more enhancements here:
// - Language detection for files
// - File size information
// - Complexity metrics
return commit;
}
async getBranches(): Promise<string[]> {
return this.git.getBranches();
}
async getCurrentBranch(): Promise<string> {
return this.git.getCurrentBranch();
}
async getRemoteUrl(): Promise<string> {
return this.git.getRemoteUrl();
}
async getLanguageStats(): Promise<{ [language: string]: number }> {
return this.git.getLanguageStats();
}
async validateRepository(): Promise<boolean> {
return this.git.validateRepository();
}
private reportProgress(phase: string, current: number, total: number): void {
if (this.progressCallback) {
this.progressCallback(phase, current, total);
}
}
/**
* Get warnings from the most recent collection run
*/
getWarnings(): string[] {
return this.lastWarnings.slice();
}
}