@statewalker/vcs-commands 0.1.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/commit-command.d.ts.map +1 -1
- package/dist/commands/init-command.d.ts.map +1 -1
- package/dist/commands/push-command.d.ts.map +1 -1
- package/dist/core-commands/checkout.command.impl.d.ts +1 -1
- package/dist/core-commands/checkout.command.impl.d.ts.map +1 -1
- package/dist/git-command.d.ts.map +1 -1
- package/package.json +19 -14
- package/src/commands/add-command.ts +494 -0
- package/src/commands/blame-command.ts +1241 -0
- package/src/commands/branch-command.ts +487 -0
- package/src/commands/checkout-command.ts +681 -0
- package/src/commands/cherry-pick-command.ts +544 -0
- package/src/commands/clean-command.ts +218 -0
- package/src/commands/clone-command.ts +532 -0
- package/src/commands/commit-command.ts +529 -0
- package/src/commands/describe-command.ts +454 -0
- package/src/commands/diff-command.ts +257 -0
- package/src/commands/fetch-command.ts +615 -0
- package/src/commands/gc-command.ts +155 -0
- package/src/commands/index.ts +31 -0
- package/src/commands/init-command.ts +365 -0
- package/src/commands/log-command.ts +549 -0
- package/src/commands/ls-remote-command.ts +237 -0
- package/src/commands/merge-command.ts +750 -0
- package/src/commands/pack-refs-command.ts +101 -0
- package/src/commands/pull-command.ts +258 -0
- package/src/commands/push-command.ts +682 -0
- package/src/commands/rebase-command.ts +665 -0
- package/src/commands/reflog-command.ts +68 -0
- package/src/commands/remote-command.ts +385 -0
- package/src/commands/reset-command.ts +292 -0
- package/src/commands/revert-command.ts +552 -0
- package/src/commands/rm-command.ts +178 -0
- package/src/commands/stash-apply-command.ts +402 -0
- package/src/commands/stash-create-command.ts +339 -0
- package/src/commands/stash-drop-command.ts +115 -0
- package/src/commands/stash-list-command.ts +116 -0
- package/src/commands/status-command.ts +173 -0
- package/src/commands/tag-command.ts +378 -0
- package/src/commands/tree-restore.ts +126 -0
- package/src/core-commands/add.command.impl.ts +274 -0
- package/src/core-commands/add.command.ts +81 -0
- package/src/core-commands/checkout.command.impl.ts +586 -0
- package/src/core-commands/checkout.command.ts +125 -0
- package/src/core-commands/index.ts +13 -0
- package/src/errors/add-errors.ts +20 -0
- package/src/errors/checkout-errors.ts +42 -0
- package/src/errors/command-errors.ts +68 -0
- package/src/errors/commit-errors.ts +40 -0
- package/src/errors/git-api-error.ts +11 -0
- package/src/errors/index.ts +12 -0
- package/src/errors/merge-errors.ts +94 -0
- package/src/errors/rebase-errors.ts +34 -0
- package/src/errors/ref-errors.ts +100 -0
- package/src/errors/remote-errors.ts +27 -0
- package/src/errors/stash-errors.ts +42 -0
- package/src/errors/tag-errors.ts +16 -0
- package/src/errors/transport-errors.ts +68 -0
- package/src/git-command.ts +421 -0
- package/src/git.ts +910 -0
- package/src/index.ts +18 -0
- package/src/pack-import/index.ts +8 -0
- package/src/pack-import/pack-import.ts +24 -0
- package/src/remote-config/index.ts +12 -0
- package/src/remote-config/remote-config-store.ts +285 -0
- package/src/rename/index.ts +5 -0
- package/src/rename/similarity-index.ts +185 -0
- package/src/results/cherry-pick-result.ts +35 -0
- package/src/results/clone-result.ts +25 -0
- package/src/results/diff-entry.ts +162 -0
- package/src/results/diff-formatter.ts +416 -0
- package/src/results/fetch-result.ts +91 -0
- package/src/results/index.ts +13 -0
- package/src/results/init-result.ts +29 -0
- package/src/results/merge-result.ts +135 -0
- package/src/results/pull-result.ts +27 -0
- package/src/results/push-result.ts +86 -0
- package/src/results/rebase-result.ts +140 -0
- package/src/results/revert-result.ts +35 -0
- package/src/results/stash-result.ts +60 -0
- package/src/results/status-result.ts +65 -0
- package/src/transport-command.ts +125 -0
- package/src/tree-merge/index.ts +8 -0
- package/src/tree-merge/tree-merge.ts +249 -0
- package/src/types.ts +70 -0
|
@@ -0,0 +1,1241 @@
|
|
|
1
|
+
import type { Commit, ObjectId, PersonIdent, TreeEntry } from "@statewalker/vcs-core";
|
|
2
|
+
import type { Edit } from "@statewalker/vcs-utils";
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_ALGORITHM,
|
|
5
|
+
type DiffAlgorithm,
|
|
6
|
+
getAlgorithm,
|
|
7
|
+
RawText,
|
|
8
|
+
RawTextComparator,
|
|
9
|
+
type SupportedAlgorithm,
|
|
10
|
+
} from "@statewalker/vcs-utils";
|
|
11
|
+
|
|
12
|
+
import { RefNotFoundError } from "../errors/ref-errors.js";
|
|
13
|
+
import { GitCommand } from "../git-command.js";
|
|
14
|
+
import { DEFAULT_RENAME_SCORE, SimilarityIndex } from "../rename/index.js";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A single blame entry - represents authorship of one or more consecutive lines.
|
|
18
|
+
*/
|
|
19
|
+
export interface BlameEntry {
|
|
20
|
+
/** The commit that introduced these lines */
|
|
21
|
+
commit: Commit;
|
|
22
|
+
/** The commit ID */
|
|
23
|
+
commitId: ObjectId;
|
|
24
|
+
/** Original path in the source commit (may differ due to renames) */
|
|
25
|
+
sourcePath: string;
|
|
26
|
+
/** Starting line number in the original file (1-based) */
|
|
27
|
+
sourceStart: number;
|
|
28
|
+
/** Starting line number in the result file (1-based) */
|
|
29
|
+
resultStart: number;
|
|
30
|
+
/** Number of lines in this region */
|
|
31
|
+
lineCount: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Detailed tracking information for a single line.
|
|
36
|
+
*
|
|
37
|
+
* Based on JGit's per-line tracking arrays in BlameResult.
|
|
38
|
+
* This provides complete origin information for each line in the result file.
|
|
39
|
+
*/
|
|
40
|
+
export interface LineTracking {
|
|
41
|
+
/** Line number in the result file (1-based) */
|
|
42
|
+
resultLine: number;
|
|
43
|
+
/** The commit that introduced this line */
|
|
44
|
+
commit: Commit;
|
|
45
|
+
/** The commit ID */
|
|
46
|
+
commitId: ObjectId;
|
|
47
|
+
/** Path in the source commit where this line originated (may differ due to renames) */
|
|
48
|
+
sourcePath: string;
|
|
49
|
+
/** Line number in the source file (1-based) */
|
|
50
|
+
sourceLine: number;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Result of a blame operation.
|
|
55
|
+
*
|
|
56
|
+
* Contains per-line authorship information for a file.
|
|
57
|
+
*/
|
|
58
|
+
export interface BlameResult {
|
|
59
|
+
/** Path of the blamed file */
|
|
60
|
+
path: string;
|
|
61
|
+
/** Total number of lines in the file */
|
|
62
|
+
lineCount: number;
|
|
63
|
+
/** Blame entries covering all lines */
|
|
64
|
+
entries: BlameEntry[];
|
|
65
|
+
/**
|
|
66
|
+
* Get the blame entry for a specific line (1-based).
|
|
67
|
+
*
|
|
68
|
+
* @param line Line number (1-based)
|
|
69
|
+
* @returns Blame entry for that line, or undefined if out of range
|
|
70
|
+
*/
|
|
71
|
+
getEntry(line: number): BlameEntry | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* Get the commit that introduced a specific line.
|
|
74
|
+
*
|
|
75
|
+
* @param line Line number (1-based)
|
|
76
|
+
* @returns Commit or undefined if out of range
|
|
77
|
+
*/
|
|
78
|
+
getSourceCommit(line: number): Commit | undefined;
|
|
79
|
+
/**
|
|
80
|
+
* Get the author of a specific line.
|
|
81
|
+
*
|
|
82
|
+
* @param line Line number (1-based)
|
|
83
|
+
* @returns PersonIdent or undefined if out of range
|
|
84
|
+
*/
|
|
85
|
+
getSourceAuthor(line: number): PersonIdent | undefined;
|
|
86
|
+
/**
|
|
87
|
+
* Get the source line number in the original file.
|
|
88
|
+
*
|
|
89
|
+
* This returns the line number in the source commit where this line originated.
|
|
90
|
+
* When a file has been modified through history with insertions/deletions,
|
|
91
|
+
* the source line number may differ from the result line number.
|
|
92
|
+
*
|
|
93
|
+
* @param line Line number in the result file (1-based)
|
|
94
|
+
* @returns Source line number (1-based), or undefined if out of range
|
|
95
|
+
*/
|
|
96
|
+
getSourceLine(line: number): number | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* Get the source path for a specific line.
|
|
99
|
+
*
|
|
100
|
+
* This returns the file path in the source commit. When rename tracking
|
|
101
|
+
* is enabled, this may differ from the result path if the line originated
|
|
102
|
+
* from a file with a different name.
|
|
103
|
+
*
|
|
104
|
+
* @param line Line number in the result file (1-based)
|
|
105
|
+
* @returns Source path, or undefined if out of range
|
|
106
|
+
*/
|
|
107
|
+
getSourcePath(line: number): string | undefined;
|
|
108
|
+
/**
|
|
109
|
+
* Get detailed tracking information for all lines.
|
|
110
|
+
*
|
|
111
|
+
* Returns an array of LineTracking objects, one per line in the result file.
|
|
112
|
+
* Each object contains complete origin information including the commit,
|
|
113
|
+
* source path, and source line number.
|
|
114
|
+
*
|
|
115
|
+
* @returns Array of LineTracking objects ordered by result line number
|
|
116
|
+
*/
|
|
117
|
+
getLineTracking(): LineTracking[];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Annotate each line of a file with authorship information.
|
|
122
|
+
*
|
|
123
|
+
* Equivalent to `git blame`.
|
|
124
|
+
*
|
|
125
|
+
* Based on JGit's BlameCommand and BlameGenerator.
|
|
126
|
+
*
|
|
127
|
+
* This command tracks line-by-line history to determine which commit
|
|
128
|
+
* introduced each line in a file. It walks commit history backward,
|
|
129
|
+
* using diff algorithms to trace the origin of each line.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```typescript
|
|
133
|
+
* // Blame a file at HEAD
|
|
134
|
+
* const result = await git.blame()
|
|
135
|
+
* .setFilePath("src/main.ts")
|
|
136
|
+
* .call();
|
|
137
|
+
*
|
|
138
|
+
* // Get author of line 42
|
|
139
|
+
* const author = result.getSourceAuthor(42);
|
|
140
|
+
* console.log(`Line 42 written by: ${author?.name}`);
|
|
141
|
+
*
|
|
142
|
+
* // Blame at a specific commit
|
|
143
|
+
* const result = await git.blame()
|
|
144
|
+
* .setFilePath("README.md")
|
|
145
|
+
* .setStartCommit(commitId)
|
|
146
|
+
* .call();
|
|
147
|
+
*
|
|
148
|
+
* // Iterate over blame entries
|
|
149
|
+
* for (const entry of result.entries) {
|
|
150
|
+
* console.log(`Lines ${entry.resultStart}-${entry.resultStart + entry.lineCount - 1}: ${entry.commit.author.name}`);
|
|
151
|
+
* }
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Represents a region of the result file that still needs blame attribution.
|
|
157
|
+
* Modeled after JGit's Region class.
|
|
158
|
+
*
|
|
159
|
+
* Regions track both the position in the result file and the current source position
|
|
160
|
+
* (which may differ as we trace back through history due to insertions/deletions).
|
|
161
|
+
*/
|
|
162
|
+
interface BlameRegion {
|
|
163
|
+
/** Position in the result file (1-based) */
|
|
164
|
+
resultStart: number;
|
|
165
|
+
/** Position in the current source file being examined (1-based) */
|
|
166
|
+
sourceStart: number;
|
|
167
|
+
/** Number of lines in this region */
|
|
168
|
+
length: number;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Represents a candidate commit that may have contributed lines to the result.
|
|
173
|
+
* Modeled after JGit's Candidate class.
|
|
174
|
+
*/
|
|
175
|
+
interface BlameCandidate {
|
|
176
|
+
/** The commit being examined */
|
|
177
|
+
commit: Commit;
|
|
178
|
+
/** Commit ID */
|
|
179
|
+
commitId: ObjectId;
|
|
180
|
+
/** Current path being tracked */
|
|
181
|
+
path: string;
|
|
182
|
+
/** Blob ID of the file in this commit */
|
|
183
|
+
blobId: ObjectId;
|
|
184
|
+
/** Content of the file */
|
|
185
|
+
content: Uint8Array;
|
|
186
|
+
/** RawText for diff operations */
|
|
187
|
+
rawText: RawText;
|
|
188
|
+
/** Regions this candidate is responsible for */
|
|
189
|
+
regions: BlameRegion[];
|
|
190
|
+
/** Commit time for priority queue ordering */
|
|
191
|
+
time: number;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export class BlameCommand extends GitCommand<BlameResult> {
|
|
195
|
+
private filePath?: string;
|
|
196
|
+
private startCommit?: ObjectId;
|
|
197
|
+
private followRenames = false;
|
|
198
|
+
private renameScore = DEFAULT_RENAME_SCORE;
|
|
199
|
+
private diffAlgorithm: DiffAlgorithm = getAlgorithm(DEFAULT_ALGORITHM);
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Set the file path to blame.
|
|
203
|
+
*
|
|
204
|
+
* @param path Repository-relative path to the file
|
|
205
|
+
*/
|
|
206
|
+
setFilePath(path: string): this {
|
|
207
|
+
this.checkCallable();
|
|
208
|
+
this.filePath = path;
|
|
209
|
+
return this;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Set the starting commit for blame.
|
|
214
|
+
*
|
|
215
|
+
* If not set, defaults to HEAD.
|
|
216
|
+
*
|
|
217
|
+
* @param commit ObjectId of the starting commit
|
|
218
|
+
*/
|
|
219
|
+
setStartCommit(commit: ObjectId): this {
|
|
220
|
+
this.checkCallable();
|
|
221
|
+
this.startCommit = commit;
|
|
222
|
+
return this;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Set whether to follow file renames.
|
|
227
|
+
*
|
|
228
|
+
* When enabled, the blame will track lines through renames.
|
|
229
|
+
* This is more accurate but slower.
|
|
230
|
+
*
|
|
231
|
+
* @param follow Whether to follow renames
|
|
232
|
+
*/
|
|
233
|
+
setFollowRenames(follow: boolean): this {
|
|
234
|
+
this.checkCallable();
|
|
235
|
+
this.followRenames = follow;
|
|
236
|
+
return this;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Set the minimum similarity score for rename detection.
|
|
241
|
+
*
|
|
242
|
+
* @param score Similarity threshold (0-100, default 50)
|
|
243
|
+
*/
|
|
244
|
+
setRenameScore(score: number): this {
|
|
245
|
+
this.checkCallable();
|
|
246
|
+
this.renameScore = Math.max(0, Math.min(100, score));
|
|
247
|
+
return this;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Set the diff algorithm to use.
|
|
252
|
+
*
|
|
253
|
+
* @param algorithm The algorithm to use (default: histogram)
|
|
254
|
+
*/
|
|
255
|
+
setAlgorithm(algorithm: SupportedAlgorithm): this {
|
|
256
|
+
this.checkCallable();
|
|
257
|
+
this.diffAlgorithm = getAlgorithm(algorithm);
|
|
258
|
+
return this;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Execute the blame command.
|
|
263
|
+
*
|
|
264
|
+
* Uses a queue-based algorithm similar to JGit's BlameGenerator.
|
|
265
|
+
* For merge commits, checks all parents to correctly attribute lines.
|
|
266
|
+
*
|
|
267
|
+
* @returns BlameResult with per-line authorship
|
|
268
|
+
*/
|
|
269
|
+
async call(): Promise<BlameResult> {
|
|
270
|
+
this.checkCallable();
|
|
271
|
+
this.setCallable(false);
|
|
272
|
+
|
|
273
|
+
if (!this.filePath) {
|
|
274
|
+
throw new Error("File path must be set");
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Resolve starting commit
|
|
278
|
+
let startId = this.startCommit;
|
|
279
|
+
if (!startId) {
|
|
280
|
+
const head = await this.refsStore.resolve("HEAD");
|
|
281
|
+
if (!head?.objectId) {
|
|
282
|
+
throw new Error("No HEAD commit found");
|
|
283
|
+
}
|
|
284
|
+
startId = head.objectId;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const resultPath = this.filePath;
|
|
288
|
+
const entries: BlameEntry[] = [];
|
|
289
|
+
|
|
290
|
+
// Load the file content at the starting commit
|
|
291
|
+
const startCommit = await this.commits.load(startId);
|
|
292
|
+
if (!startCommit) {
|
|
293
|
+
throw new RefNotFoundError(`Cannot find commit: ${startId}`);
|
|
294
|
+
}
|
|
295
|
+
const blobId = await this.getFileBlob(startCommit.tree, resultPath);
|
|
296
|
+
|
|
297
|
+
if (!blobId) {
|
|
298
|
+
throw new Error(`File not found: ${resultPath}`);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Get file content and count lines
|
|
302
|
+
const content = await this.collectBlob(blobId);
|
|
303
|
+
const lineCount = this.countLines(content);
|
|
304
|
+
|
|
305
|
+
if (lineCount === 0) {
|
|
306
|
+
// Empty file
|
|
307
|
+
return this.createBlameResult(resultPath, 0, []);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// Initialize the candidate queue with the starting commit
|
|
311
|
+
// Queue is sorted by commit time descending (most recent first)
|
|
312
|
+
const queue: BlameCandidate[] = [];
|
|
313
|
+
const seen = new Set<ObjectId>();
|
|
314
|
+
|
|
315
|
+
const initialCandidate: BlameCandidate = {
|
|
316
|
+
commit: startCommit,
|
|
317
|
+
commitId: startId,
|
|
318
|
+
path: resultPath,
|
|
319
|
+
blobId,
|
|
320
|
+
content,
|
|
321
|
+
rawText: new RawText(content),
|
|
322
|
+
regions: [{ resultStart: 1, sourceStart: 1, length: lineCount }],
|
|
323
|
+
time: startCommit.author.timestamp * 1000,
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
this.pushCandidate(queue, initialCandidate, seen);
|
|
327
|
+
|
|
328
|
+
// Track remaining lines to blame
|
|
329
|
+
let remaining = lineCount;
|
|
330
|
+
|
|
331
|
+
// Process candidates until all lines are blamed or queue is empty
|
|
332
|
+
while (queue.length > 0 && remaining > 0) {
|
|
333
|
+
const candidate = queue.shift();
|
|
334
|
+
if (!candidate) break;
|
|
335
|
+
|
|
336
|
+
if (candidate.regions.length === 0) {
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const parentCount = candidate.commit.parents.length;
|
|
341
|
+
|
|
342
|
+
if (parentCount === 0) {
|
|
343
|
+
// Root commit - blame all remaining regions to this commit
|
|
344
|
+
this.blameRegions(entries, candidate);
|
|
345
|
+
remaining -= this.countRegionLines(candidate.regions);
|
|
346
|
+
} else if (parentCount === 1) {
|
|
347
|
+
// Single parent - process normally
|
|
348
|
+
const blamed = await this.processOneParent(candidate, queue, seen, entries);
|
|
349
|
+
remaining -= blamed;
|
|
350
|
+
} else {
|
|
351
|
+
// Merge commit - process all parents (JGit's processMerge)
|
|
352
|
+
const blamed = await this.processMerge(candidate, queue, seen, entries);
|
|
353
|
+
remaining -= blamed;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// Sort entries by result line number
|
|
358
|
+
entries.sort((a, b) => a.resultStart - b.resultStart);
|
|
359
|
+
|
|
360
|
+
// Merge adjacent entries from the same commit and path
|
|
361
|
+
const mergedEntries = this.mergeAdjacentEntries(entries);
|
|
362
|
+
|
|
363
|
+
return this.createBlameResult(resultPath, lineCount, mergedEntries);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Process a commit with a single parent.
|
|
368
|
+
*
|
|
369
|
+
* @returns Number of lines blamed to this commit
|
|
370
|
+
*/
|
|
371
|
+
private async processOneParent(
|
|
372
|
+
candidate: BlameCandidate,
|
|
373
|
+
queue: BlameCandidate[],
|
|
374
|
+
seen: Set<ObjectId>,
|
|
375
|
+
entries: BlameEntry[],
|
|
376
|
+
): Promise<number> {
|
|
377
|
+
const parentId = candidate.commit.parents[0];
|
|
378
|
+
const parentCommit = await this.commits.load(parentId);
|
|
379
|
+
if (!parentCommit) {
|
|
380
|
+
// Parent commit not found - blame all regions to this commit
|
|
381
|
+
this.blameRegions(entries, candidate);
|
|
382
|
+
return this.countRegionLines(candidate.regions);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// Check if file exists in parent
|
|
386
|
+
let parentBlobId = await this.getFileBlob(parentCommit.tree, candidate.path);
|
|
387
|
+
let parentPath = candidate.path;
|
|
388
|
+
|
|
389
|
+
// If file doesn't exist in parent and rename following is enabled, search for rename
|
|
390
|
+
if (!parentBlobId && this.followRenames) {
|
|
391
|
+
const rename = await this.findRename(
|
|
392
|
+
parentCommit.tree,
|
|
393
|
+
candidate.commit.tree,
|
|
394
|
+
candidate.path,
|
|
395
|
+
candidate.content,
|
|
396
|
+
);
|
|
397
|
+
|
|
398
|
+
if (rename) {
|
|
399
|
+
parentBlobId = rename.oldBlobId;
|
|
400
|
+
parentPath = rename.oldPath;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
if (!parentBlobId) {
|
|
405
|
+
// File was introduced in this commit - blame all regions
|
|
406
|
+
this.blameRegions(entries, candidate);
|
|
407
|
+
return this.countRegionLines(candidate.regions);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// If blob is identical, pass blame to parent without diffing
|
|
411
|
+
if (parentBlobId === candidate.blobId) {
|
|
412
|
+
const parentContent = await this.collectBlob(parentBlobId);
|
|
413
|
+
const parentCandidate: BlameCandidate = {
|
|
414
|
+
commit: parentCommit,
|
|
415
|
+
commitId: parentId,
|
|
416
|
+
path: parentPath,
|
|
417
|
+
blobId: parentBlobId,
|
|
418
|
+
content: parentContent,
|
|
419
|
+
rawText: new RawText(parentContent),
|
|
420
|
+
regions: candidate.regions,
|
|
421
|
+
time: parentCommit.author.timestamp * 1000,
|
|
422
|
+
};
|
|
423
|
+
this.pushCandidate(queue, parentCandidate, seen);
|
|
424
|
+
return 0;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// Compute diff and split blame
|
|
428
|
+
const parentContent = await this.collectBlob(parentBlobId);
|
|
429
|
+
const parentRawText = new RawText(parentContent);
|
|
430
|
+
|
|
431
|
+
return this.splitBlame(
|
|
432
|
+
candidate,
|
|
433
|
+
parentCommit,
|
|
434
|
+
parentId,
|
|
435
|
+
parentPath,
|
|
436
|
+
parentBlobId,
|
|
437
|
+
parentContent,
|
|
438
|
+
parentRawText,
|
|
439
|
+
queue,
|
|
440
|
+
seen,
|
|
441
|
+
entries,
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Process a merge commit with multiple parents.
|
|
447
|
+
* Based on JGit's processMerge() algorithm.
|
|
448
|
+
*
|
|
449
|
+
* Key algorithm:
|
|
450
|
+
* 1. If any parent has identical blob, pass all blame to that parent
|
|
451
|
+
* 2. Otherwise, for each parent that has the file, compute diff
|
|
452
|
+
* 3. Lines that match a parent go to that parent's history
|
|
453
|
+
* 4. Lines that don't match any parent (conflict resolution) are blamed to merge commit
|
|
454
|
+
*
|
|
455
|
+
* @returns Number of lines blamed to the merge commit
|
|
456
|
+
*/
|
|
457
|
+
private async processMerge(
|
|
458
|
+
candidate: BlameCandidate,
|
|
459
|
+
queue: BlameCandidate[],
|
|
460
|
+
seen: Set<ObjectId>,
|
|
461
|
+
entries: BlameEntry[],
|
|
462
|
+
): Promise<number> {
|
|
463
|
+
const parentCount = candidate.commit.parents.length;
|
|
464
|
+
|
|
465
|
+
// Collect parent info
|
|
466
|
+
interface ParentInfo {
|
|
467
|
+
commit: Commit;
|
|
468
|
+
commitId: ObjectId;
|
|
469
|
+
blobId: ObjectId;
|
|
470
|
+
path: string;
|
|
471
|
+
content: Uint8Array;
|
|
472
|
+
rawText: RawText;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const parentInfos: (ParentInfo | null)[] = [];
|
|
476
|
+
|
|
477
|
+
// First pass: check all parents for the file
|
|
478
|
+
for (let pIdx = 0; pIdx < parentCount; pIdx++) {
|
|
479
|
+
const parentId = candidate.commit.parents[pIdx];
|
|
480
|
+
const parentCommit = await this.commits.load(parentId);
|
|
481
|
+
if (!parentCommit) {
|
|
482
|
+
parentInfos.push(null);
|
|
483
|
+
continue;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
let parentBlobId = await this.getFileBlob(parentCommit.tree, candidate.path);
|
|
487
|
+
let parentPath = candidate.path;
|
|
488
|
+
|
|
489
|
+
// Check for rename if file not found
|
|
490
|
+
if (!parentBlobId && this.followRenames) {
|
|
491
|
+
const rename = await this.findRename(
|
|
492
|
+
parentCommit.tree,
|
|
493
|
+
candidate.commit.tree,
|
|
494
|
+
candidate.path,
|
|
495
|
+
candidate.content,
|
|
496
|
+
);
|
|
497
|
+
if (rename) {
|
|
498
|
+
parentBlobId = rename.oldBlobId;
|
|
499
|
+
parentPath = rename.oldPath;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
if (parentBlobId) {
|
|
504
|
+
// If any parent has identical blob, pass all blame to that parent
|
|
505
|
+
if (parentBlobId === candidate.blobId) {
|
|
506
|
+
const parentContent = await this.collectBlob(parentBlobId);
|
|
507
|
+
const parentCandidate: BlameCandidate = {
|
|
508
|
+
commit: parentCommit,
|
|
509
|
+
commitId: parentId,
|
|
510
|
+
path: parentPath,
|
|
511
|
+
blobId: parentBlobId,
|
|
512
|
+
content: parentContent,
|
|
513
|
+
rawText: new RawText(parentContent),
|
|
514
|
+
regions: candidate.regions,
|
|
515
|
+
time: parentCommit.author.timestamp * 1000,
|
|
516
|
+
};
|
|
517
|
+
this.pushCandidate(queue, parentCandidate, seen);
|
|
518
|
+
return 0;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
const parentContent = await this.collectBlob(parentBlobId);
|
|
522
|
+
parentInfos.push({
|
|
523
|
+
commit: parentCommit,
|
|
524
|
+
commitId: parentId,
|
|
525
|
+
blobId: parentBlobId,
|
|
526
|
+
path: parentPath,
|
|
527
|
+
content: parentContent,
|
|
528
|
+
rawText: new RawText(parentContent),
|
|
529
|
+
});
|
|
530
|
+
} else {
|
|
531
|
+
parentInfos.push(null);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
// If no parents have the file, blame everything to this commit
|
|
536
|
+
const validParents = parentInfos.filter((p): p is ParentInfo => p !== null);
|
|
537
|
+
if (validParents.length === 0) {
|
|
538
|
+
this.blameRegions(entries, candidate);
|
|
539
|
+
return this.countRegionLines(candidate.regions);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// Process each parent and split blame
|
|
543
|
+
// We track which regions have been passed to parents
|
|
544
|
+
let currentRegions = candidate.regions;
|
|
545
|
+
|
|
546
|
+
for (const parentInfo of validParents) {
|
|
547
|
+
if (currentRegions.length === 0) break;
|
|
548
|
+
|
|
549
|
+
// Compute diff between parent and candidate
|
|
550
|
+
const comparator = RawTextComparator.DEFAULT;
|
|
551
|
+
const edits = this.diffAlgorithm(comparator, parentInfo.rawText, candidate.rawText);
|
|
552
|
+
|
|
553
|
+
// Split regions between parent and candidate
|
|
554
|
+
const { parentRegions, childRegions } = this.takeBlame(edits, currentRegions);
|
|
555
|
+
|
|
556
|
+
// Push parent candidate if it has regions
|
|
557
|
+
if (parentRegions.length > 0) {
|
|
558
|
+
const parentCandidate: BlameCandidate = {
|
|
559
|
+
commit: parentInfo.commit,
|
|
560
|
+
commitId: parentInfo.commitId,
|
|
561
|
+
path: parentInfo.path,
|
|
562
|
+
blobId: parentInfo.blobId,
|
|
563
|
+
content: parentInfo.content,
|
|
564
|
+
rawText: parentInfo.rawText,
|
|
565
|
+
regions: parentRegions,
|
|
566
|
+
time: parentInfo.commit.author.timestamp * 1000,
|
|
567
|
+
};
|
|
568
|
+
this.pushCandidate(queue, parentCandidate, seen);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
// Remaining regions stay with the merge commit
|
|
572
|
+
currentRegions = childRegions;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// Any remaining regions are blamed to the merge commit
|
|
576
|
+
// (these are conflict resolution lines)
|
|
577
|
+
if (currentRegions.length > 0) {
|
|
578
|
+
const mergeCandidate: BlameCandidate = {
|
|
579
|
+
...candidate,
|
|
580
|
+
regions: currentRegions,
|
|
581
|
+
};
|
|
582
|
+
this.blameRegions(entries, mergeCandidate);
|
|
583
|
+
return this.countRegionLines(currentRegions);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
return 0;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Split blame between a candidate and its parent using diff edits.
|
|
591
|
+
* Based on JGit's Candidate.takeBlame() method.
|
|
592
|
+
*
|
|
593
|
+
* For each line in the child that is unchanged (not covered by an edit),
|
|
594
|
+
* we need to find where that line came from in the parent. Due to insertions
|
|
595
|
+
* and deletions, unchanged lines in the child may not be contiguous in the parent.
|
|
596
|
+
*
|
|
597
|
+
* @returns Object with regions for parent and regions remaining with child
|
|
598
|
+
*/
|
|
599
|
+
private takeBlame(
|
|
600
|
+
edits: Edit[],
|
|
601
|
+
regions: BlameRegion[],
|
|
602
|
+
): { parentRegions: BlameRegion[]; childRegions: BlameRegion[] } {
|
|
603
|
+
const parentRegions: BlameRegion[] = [];
|
|
604
|
+
const childRegions: BlameRegion[] = [];
|
|
605
|
+
|
|
606
|
+
// Process each region against the edit list
|
|
607
|
+
// Edits are in terms of 0-based line numbers
|
|
608
|
+
// Regions use 1-based line numbers
|
|
609
|
+
|
|
610
|
+
for (const region of regions) {
|
|
611
|
+
// Convert to 0-based for comparison with edits
|
|
612
|
+
const regionStart = region.sourceStart - 1;
|
|
613
|
+
const regionEnd = regionStart + region.length;
|
|
614
|
+
|
|
615
|
+
// Track what parts of this region are covered by edits (changes)
|
|
616
|
+
// and what parts are unchanged (should go to parent)
|
|
617
|
+
const changedRanges: Array<{ start: number; end: number }> = [];
|
|
618
|
+
|
|
619
|
+
for (const edit of edits) {
|
|
620
|
+
// Edit's B side (child/new) tells us which lines in the child are different
|
|
621
|
+
const editStart = edit.beginB;
|
|
622
|
+
const editEnd = edit.endB;
|
|
623
|
+
|
|
624
|
+
// Check if edit overlaps with region
|
|
625
|
+
if (editEnd <= regionStart || editStart >= regionEnd) {
|
|
626
|
+
continue; // No overlap
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
// Compute overlap
|
|
630
|
+
const overlapStart = Math.max(editStart, regionStart);
|
|
631
|
+
const overlapEnd = Math.min(editEnd, regionEnd);
|
|
632
|
+
|
|
633
|
+
if (overlapEnd > overlapStart) {
|
|
634
|
+
changedRanges.push({ start: overlapStart, end: overlapEnd });
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
// Sort and merge overlapping changed ranges
|
|
639
|
+
changedRanges.sort((a, b) => a.start - b.start);
|
|
640
|
+
const mergedChanges: Array<{ start: number; end: number }> = [];
|
|
641
|
+
for (const range of changedRanges) {
|
|
642
|
+
if (
|
|
643
|
+
mergedChanges.length === 0 ||
|
|
644
|
+
mergedChanges[mergedChanges.length - 1].end < range.start
|
|
645
|
+
) {
|
|
646
|
+
mergedChanges.push({ ...range });
|
|
647
|
+
} else {
|
|
648
|
+
mergedChanges[mergedChanges.length - 1].end = Math.max(
|
|
649
|
+
mergedChanges[mergedChanges.length - 1].end,
|
|
650
|
+
range.end,
|
|
651
|
+
);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// Now split the region: unchanged parts go to parent, changed parts stay with child
|
|
656
|
+
// For unchanged parts, we need to create separate regions for each contiguous block
|
|
657
|
+
// in the parent, since mapping through edits may create gaps.
|
|
658
|
+
let pos = regionStart;
|
|
659
|
+
|
|
660
|
+
for (const change of mergedChanges) {
|
|
661
|
+
// Unchanged part before this change - process line by line to handle non-contiguous mapping
|
|
662
|
+
if (change.start > pos) {
|
|
663
|
+
this.addParentRegionsForUnchangedBlock(
|
|
664
|
+
pos,
|
|
665
|
+
change.start,
|
|
666
|
+
region.resultStart,
|
|
667
|
+
regionStart,
|
|
668
|
+
edits,
|
|
669
|
+
parentRegions,
|
|
670
|
+
);
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
// Changed part stays with child
|
|
674
|
+
if (change.end > change.start) {
|
|
675
|
+
childRegions.push({
|
|
676
|
+
resultStart: region.resultStart + (change.start - regionStart),
|
|
677
|
+
sourceStart: change.start + 1, // Convert back to 1-based
|
|
678
|
+
length: change.end - change.start,
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
pos = change.end;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
// Remaining unchanged part after last change
|
|
686
|
+
if (pos < regionEnd) {
|
|
687
|
+
this.addParentRegionsForUnchangedBlock(
|
|
688
|
+
pos,
|
|
689
|
+
regionEnd,
|
|
690
|
+
region.resultStart,
|
|
691
|
+
regionStart,
|
|
692
|
+
edits,
|
|
693
|
+
parentRegions,
|
|
694
|
+
);
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
return { parentRegions, childRegions };
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
/**
|
|
702
|
+
* Add parent regions for an unchanged block of lines.
|
|
703
|
+
* Since lines may map to non-contiguous positions in the parent
|
|
704
|
+
* (due to deletions), we need to create separate regions for each
|
|
705
|
+
* contiguous block in the parent.
|
|
706
|
+
*/
|
|
707
|
+
private addParentRegionsForUnchangedBlock(
|
|
708
|
+
startPos: number,
|
|
709
|
+
endPos: number,
|
|
710
|
+
resultStart: number,
|
|
711
|
+
regionStart: number,
|
|
712
|
+
edits: Edit[],
|
|
713
|
+
parentRegions: BlameRegion[],
|
|
714
|
+
): void {
|
|
715
|
+
// Map each line position and group into contiguous regions in the parent
|
|
716
|
+
let currentParentStart = -1;
|
|
717
|
+
let currentResultStart = -1;
|
|
718
|
+
let currentLength = 0;
|
|
719
|
+
let lastParentPos = -1;
|
|
720
|
+
|
|
721
|
+
for (let childPos = startPos; childPos < endPos; childPos++) {
|
|
722
|
+
const parentPos = this.mapPositionToParent(childPos, edits);
|
|
723
|
+
|
|
724
|
+
if (currentLength === 0) {
|
|
725
|
+
// Start a new region
|
|
726
|
+
currentParentStart = parentPos;
|
|
727
|
+
currentResultStart = resultStart + (childPos - regionStart);
|
|
728
|
+
currentLength = 1;
|
|
729
|
+
lastParentPos = parentPos;
|
|
730
|
+
} else if (parentPos === lastParentPos + 1) {
|
|
731
|
+
// Extend current region (contiguous in parent)
|
|
732
|
+
currentLength++;
|
|
733
|
+
lastParentPos = parentPos;
|
|
734
|
+
} else {
|
|
735
|
+
// Gap in parent - push current region and start new one
|
|
736
|
+
parentRegions.push({
|
|
737
|
+
resultStart: currentResultStart,
|
|
738
|
+
sourceStart: currentParentStart + 1, // Convert to 1-based
|
|
739
|
+
length: currentLength,
|
|
740
|
+
});
|
|
741
|
+
currentParentStart = parentPos;
|
|
742
|
+
currentResultStart = resultStart + (childPos - regionStart);
|
|
743
|
+
currentLength = 1;
|
|
744
|
+
lastParentPos = parentPos;
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
// Push final region
|
|
749
|
+
if (currentLength > 0) {
|
|
750
|
+
parentRegions.push({
|
|
751
|
+
resultStart: currentResultStart,
|
|
752
|
+
sourceStart: currentParentStart + 1, // Convert to 1-based
|
|
753
|
+
length: currentLength,
|
|
754
|
+
});
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
/**
|
|
759
|
+
* Map a position in the child file back to the corresponding position in the parent.
|
|
760
|
+
* Takes into account insertions and deletions.
|
|
761
|
+
*
|
|
762
|
+
* @param childPos 0-based position in child
|
|
763
|
+
* @param edits Edit list from diff
|
|
764
|
+
* @returns 0-based position in parent
|
|
765
|
+
*/
|
|
766
|
+
private mapPositionToParent(childPos: number, edits: Edit[]): number {
|
|
767
|
+
let offset = 0;
|
|
768
|
+
|
|
769
|
+
for (const edit of edits) {
|
|
770
|
+
if (edit.endB <= childPos) {
|
|
771
|
+
// This edit is before our position
|
|
772
|
+
// Adjust offset: lengthA - lengthB gives the shift
|
|
773
|
+
const lengthA = edit.endA - edit.beginA;
|
|
774
|
+
const lengthB = edit.endB - edit.beginB;
|
|
775
|
+
offset += lengthA - lengthB;
|
|
776
|
+
} else if (edit.beginB <= childPos) {
|
|
777
|
+
// Position is within an edit - this shouldn't happen for unchanged lines
|
|
778
|
+
// but handle gracefully by mapping to edit start in parent
|
|
779
|
+
return edit.beginA;
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
return childPos + offset;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* Split blame between a candidate and a single parent.
|
|
788
|
+
*
|
|
789
|
+
* @returns Number of lines blamed to the candidate
|
|
790
|
+
*/
|
|
791
|
+
private async splitBlame(
|
|
792
|
+
candidate: BlameCandidate,
|
|
793
|
+
parentCommit: Commit,
|
|
794
|
+
parentId: ObjectId,
|
|
795
|
+
parentPath: string,
|
|
796
|
+
parentBlobId: ObjectId,
|
|
797
|
+
parentContent: Uint8Array,
|
|
798
|
+
parentRawText: RawText,
|
|
799
|
+
queue: BlameCandidate[],
|
|
800
|
+
seen: Set<ObjectId>,
|
|
801
|
+
entries: BlameEntry[],
|
|
802
|
+
): Promise<number> {
|
|
803
|
+
// Compute diff
|
|
804
|
+
const comparator = RawTextComparator.DEFAULT;
|
|
805
|
+
const edits = this.diffAlgorithm(comparator, parentRawText, candidate.rawText);
|
|
806
|
+
|
|
807
|
+
if (edits.length === 0) {
|
|
808
|
+
// No changes (might happen with whitespace-ignoring comparator)
|
|
809
|
+
const parentCandidate: BlameCandidate = {
|
|
810
|
+
commit: parentCommit,
|
|
811
|
+
commitId: parentId,
|
|
812
|
+
path: parentPath,
|
|
813
|
+
blobId: parentBlobId,
|
|
814
|
+
content: parentContent,
|
|
815
|
+
rawText: parentRawText,
|
|
816
|
+
regions: candidate.regions,
|
|
817
|
+
time: parentCommit.author.timestamp * 1000,
|
|
818
|
+
};
|
|
819
|
+
this.pushCandidate(queue, parentCandidate, seen);
|
|
820
|
+
return 0;
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
// Split regions
|
|
824
|
+
const { parentRegions, childRegions } = this.takeBlame(edits, candidate.regions);
|
|
825
|
+
|
|
826
|
+
// Push parent candidate
|
|
827
|
+
if (parentRegions.length > 0) {
|
|
828
|
+
const parentCandidate: BlameCandidate = {
|
|
829
|
+
commit: parentCommit,
|
|
830
|
+
commitId: parentId,
|
|
831
|
+
path: parentPath,
|
|
832
|
+
blobId: parentBlobId,
|
|
833
|
+
content: parentContent,
|
|
834
|
+
rawText: parentRawText,
|
|
835
|
+
regions: parentRegions,
|
|
836
|
+
time: parentCommit.author.timestamp * 1000,
|
|
837
|
+
};
|
|
838
|
+
this.pushCandidate(queue, parentCandidate, seen);
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
// Blame child regions to this commit
|
|
842
|
+
if (childRegions.length > 0) {
|
|
843
|
+
const childCandidate: BlameCandidate = {
|
|
844
|
+
...candidate,
|
|
845
|
+
regions: childRegions,
|
|
846
|
+
};
|
|
847
|
+
this.blameRegions(entries, childCandidate);
|
|
848
|
+
return this.countRegionLines(childRegions);
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
return 0;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
/**
|
|
855
|
+
* Push a candidate onto the queue, maintaining descending time order.
|
|
856
|
+
* Handles merging of candidates for the same commit.
|
|
857
|
+
*/
|
|
858
|
+
private pushCandidate(
|
|
859
|
+
queue: BlameCandidate[],
|
|
860
|
+
candidate: BlameCandidate,
|
|
861
|
+
seen: Set<ObjectId>,
|
|
862
|
+
): void {
|
|
863
|
+
// Check if we already have a candidate for this commit
|
|
864
|
+
if (seen.has(candidate.commitId)) {
|
|
865
|
+
// Find and merge with existing candidate
|
|
866
|
+
const existing = queue.find(
|
|
867
|
+
(c) => c.commitId === candidate.commitId && c.path === candidate.path,
|
|
868
|
+
);
|
|
869
|
+
if (existing) {
|
|
870
|
+
// Merge regions
|
|
871
|
+
existing.regions = this.mergeBlameRegions(existing.regions, candidate.regions);
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
seen.add(candidate.commitId);
|
|
877
|
+
|
|
878
|
+
// Insert in descending time order
|
|
879
|
+
let insertIdx = 0;
|
|
880
|
+
while (insertIdx < queue.length && queue[insertIdx].time > candidate.time) {
|
|
881
|
+
insertIdx++;
|
|
882
|
+
}
|
|
883
|
+
queue.splice(insertIdx, 0, candidate);
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
/**
|
|
887
|
+
* Merge two region lists, maintaining sorted order by resultStart.
|
|
888
|
+
*/
|
|
889
|
+
private mergeBlameRegions(a: BlameRegion[], b: BlameRegion[]): BlameRegion[] {
|
|
890
|
+
const result: BlameRegion[] = [...a, ...b];
|
|
891
|
+
result.sort((x, y) => x.resultStart - y.resultStart);
|
|
892
|
+
|
|
893
|
+
// Merge adjacent regions
|
|
894
|
+
const merged: BlameRegion[] = [];
|
|
895
|
+
for (const region of result) {
|
|
896
|
+
if (merged.length === 0) {
|
|
897
|
+
merged.push({ ...region });
|
|
898
|
+
} else {
|
|
899
|
+
const last = merged[merged.length - 1];
|
|
900
|
+
if (
|
|
901
|
+
last.resultStart + last.length === region.resultStart &&
|
|
902
|
+
last.sourceStart + last.length === region.sourceStart
|
|
903
|
+
) {
|
|
904
|
+
last.length += region.length;
|
|
905
|
+
} else {
|
|
906
|
+
merged.push({ ...region });
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
return merged;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
/**
|
|
915
|
+
* Blame all regions in a candidate to its commit.
|
|
916
|
+
*/
|
|
917
|
+
private blameRegions(entries: BlameEntry[], candidate: BlameCandidate): void {
|
|
918
|
+
for (const region of candidate.regions) {
|
|
919
|
+
entries.push({
|
|
920
|
+
commit: candidate.commit,
|
|
921
|
+
commitId: candidate.commitId,
|
|
922
|
+
sourcePath: candidate.path,
|
|
923
|
+
sourceStart: region.sourceStart,
|
|
924
|
+
resultStart: region.resultStart,
|
|
925
|
+
lineCount: region.length,
|
|
926
|
+
});
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
/**
|
|
931
|
+
* Count total lines in a list of regions.
|
|
932
|
+
*/
|
|
933
|
+
private countRegionLines(regions: BlameRegion[]): number {
|
|
934
|
+
return regions.reduce((sum, r) => sum + r.length, 0);
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
/**
|
|
938
|
+
* Find a rename candidate for a file that doesn't exist in the parent commit.
|
|
939
|
+
*
|
|
940
|
+
* Searches for deleted files in the parent that have similar content to the
|
|
941
|
+
* file in the current commit.
|
|
942
|
+
*
|
|
943
|
+
* @param parentTreeId Tree ID of the parent commit
|
|
944
|
+
* @param currentTreeId Tree ID of the current commit
|
|
945
|
+
* @param newPath Path of the file in the current commit
|
|
946
|
+
* @param newContent Content of the file in the current commit
|
|
947
|
+
* @returns Rename information if found, undefined otherwise
|
|
948
|
+
*/
|
|
949
|
+
private async findRename(
|
|
950
|
+
parentTreeId: ObjectId,
|
|
951
|
+
currentTreeId: ObjectId,
|
|
952
|
+
_newPath: string,
|
|
953
|
+
newContent: Uint8Array,
|
|
954
|
+
): Promise<{ oldPath: string; oldBlobId: ObjectId; score: number } | undefined> {
|
|
955
|
+
// Skip binary files for rename detection
|
|
956
|
+
if (SimilarityIndex.isBinary(newContent)) {
|
|
957
|
+
return undefined;
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
// Collect all blob entries from both trees
|
|
961
|
+
const parentBlobs = new Map<string, TreeEntry>();
|
|
962
|
+
const currentBlobs = new Map<string, TreeEntry>();
|
|
963
|
+
|
|
964
|
+
await this.collectTreeEntries(parentTreeId, "", parentBlobs);
|
|
965
|
+
await this.collectTreeEntries(currentTreeId, "", currentBlobs);
|
|
966
|
+
|
|
967
|
+
// Find files that exist in parent but not in current (potential rename sources)
|
|
968
|
+
const deletedFiles: Array<{ path: string; entry: TreeEntry }> = [];
|
|
969
|
+
for (const [path, entry] of parentBlobs) {
|
|
970
|
+
if (!currentBlobs.has(path)) {
|
|
971
|
+
deletedFiles.push({ path, entry });
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
if (deletedFiles.length === 0) {
|
|
976
|
+
return undefined;
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
// Compute similarity index for the new file
|
|
980
|
+
const newIndex = SimilarityIndex.create(newContent);
|
|
981
|
+
|
|
982
|
+
// Find the best matching deleted file
|
|
983
|
+
let bestMatch: { path: string; blobId: ObjectId; score: number } | undefined;
|
|
984
|
+
|
|
985
|
+
for (const { path: oldPath, entry } of deletedFiles) {
|
|
986
|
+
// Load the old file content
|
|
987
|
+
const oldContent = await this.collectBlob(entry.id);
|
|
988
|
+
|
|
989
|
+
// Skip binary files
|
|
990
|
+
if (SimilarityIndex.isBinary(oldContent)) {
|
|
991
|
+
continue;
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
// Compute similarity
|
|
995
|
+
const oldIndex = SimilarityIndex.create(oldContent);
|
|
996
|
+
const score = newIndex.score(oldIndex);
|
|
997
|
+
|
|
998
|
+
// Check if this is a better match than what we have
|
|
999
|
+
if (score >= this.renameScore && (!bestMatch || score > bestMatch.score)) {
|
|
1000
|
+
bestMatch = {
|
|
1001
|
+
path: oldPath,
|
|
1002
|
+
blobId: entry.id,
|
|
1003
|
+
score,
|
|
1004
|
+
};
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
if (bestMatch) {
|
|
1009
|
+
return {
|
|
1010
|
+
oldPath: bestMatch.path,
|
|
1011
|
+
oldBlobId: bestMatch.blobId,
|
|
1012
|
+
score: bestMatch.score,
|
|
1013
|
+
};
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
return undefined;
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
/**
|
|
1020
|
+
* Collect all blob entries from a tree recursively.
|
|
1021
|
+
*/
|
|
1022
|
+
private async collectTreeEntries(
|
|
1023
|
+
treeId: ObjectId,
|
|
1024
|
+
prefix: string,
|
|
1025
|
+
entries: Map<string, TreeEntry>,
|
|
1026
|
+
): Promise<void> {
|
|
1027
|
+
const TREE_MODE = 0o40000;
|
|
1028
|
+
|
|
1029
|
+
for await (const entry of this.trees.loadTree(treeId)) {
|
|
1030
|
+
const path = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
1031
|
+
|
|
1032
|
+
if (entry.mode === TREE_MODE) {
|
|
1033
|
+
// Recurse into subtree
|
|
1034
|
+
await this.collectTreeEntries(entry.id, path, entries);
|
|
1035
|
+
} else {
|
|
1036
|
+
// Blob entry
|
|
1037
|
+
entries.set(path, entry);
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
/**
|
|
1043
|
+
* Collect blob content into a single buffer.
|
|
1044
|
+
*/
|
|
1045
|
+
private async collectBlob(blobId: ObjectId): Promise<Uint8Array> {
|
|
1046
|
+
const chunks: Uint8Array[] = [];
|
|
1047
|
+
let totalLength = 0;
|
|
1048
|
+
|
|
1049
|
+
const blobContent = await this.blobs.load(blobId);
|
|
1050
|
+
if (blobContent) {
|
|
1051
|
+
for await (const chunk of blobContent) {
|
|
1052
|
+
chunks.push(chunk);
|
|
1053
|
+
totalLength += chunk.length;
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
// Combine chunks into single buffer
|
|
1058
|
+
const result = new Uint8Array(totalLength);
|
|
1059
|
+
let offset = 0;
|
|
1060
|
+
for (const chunk of chunks) {
|
|
1061
|
+
result.set(chunk, offset);
|
|
1062
|
+
offset += chunk.length;
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
return result;
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
/**
|
|
1069
|
+
* Get blob ID for a file in a tree.
|
|
1070
|
+
*/
|
|
1071
|
+
private async getFileBlob(treeId: ObjectId, path: string): Promise<ObjectId | undefined> {
|
|
1072
|
+
const parts = path.split("/").filter((p) => p.length > 0);
|
|
1073
|
+
let currentTreeId = treeId;
|
|
1074
|
+
|
|
1075
|
+
for (let i = 0; i < parts.length; i++) {
|
|
1076
|
+
const name = parts[i];
|
|
1077
|
+
const isLast = i === parts.length - 1;
|
|
1078
|
+
|
|
1079
|
+
const entry = await this.trees.getEntry(currentTreeId, name);
|
|
1080
|
+
if (!entry) {
|
|
1081
|
+
return undefined;
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
if (isLast) {
|
|
1085
|
+
return entry.id;
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
// Navigate into subtree
|
|
1089
|
+
currentTreeId = entry.id;
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
return undefined;
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
/**
|
|
1096
|
+
* Count lines in content.
|
|
1097
|
+
*
|
|
1098
|
+
* Recognizes three types of line endings:
|
|
1099
|
+
* - LF (\n) - Unix style
|
|
1100
|
+
* - CRLF (\r\n) - Windows style (counts as one line ending)
|
|
1101
|
+
* - CR (\r) - Classic Mac style (standalone CR without following LF)
|
|
1102
|
+
*/
|
|
1103
|
+
private countLines(content: Uint8Array): number {
|
|
1104
|
+
if (content.length === 0) {
|
|
1105
|
+
return 0;
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
let count = 1;
|
|
1109
|
+
for (let i = 0; i < content.length; i++) {
|
|
1110
|
+
const byte = content[i];
|
|
1111
|
+
if (byte === 0x0a) {
|
|
1112
|
+
// '\n' - LF (Unix) or second byte of CRLF (Windows)
|
|
1113
|
+
count++;
|
|
1114
|
+
} else if (byte === 0x0d) {
|
|
1115
|
+
// '\r' - Check if it's CRLF or standalone CR
|
|
1116
|
+
if (i + 1 < content.length && content[i + 1] === 0x0a) {
|
|
1117
|
+
// CRLF - skip the CR, the LF will be handled in next iteration
|
|
1118
|
+
continue;
|
|
1119
|
+
}
|
|
1120
|
+
// Standalone CR (classic Mac style)
|
|
1121
|
+
count++;
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
// If file ends with newline (LF or CR), don't count the empty "line" after it
|
|
1126
|
+
const lastByte = content[content.length - 1];
|
|
1127
|
+
if (lastByte === 0x0a || lastByte === 0x0d) {
|
|
1128
|
+
count--;
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
return count;
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
/**
|
|
1135
|
+
* Merge adjacent entries from the same commit and source path.
|
|
1136
|
+
*/
|
|
1137
|
+
private mergeAdjacentEntries(entries: BlameEntry[]): BlameEntry[] {
|
|
1138
|
+
if (entries.length <= 1) {
|
|
1139
|
+
return entries;
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
const merged: BlameEntry[] = [];
|
|
1143
|
+
let current = entries[0];
|
|
1144
|
+
|
|
1145
|
+
for (let i = 1; i < entries.length; i++) {
|
|
1146
|
+
const next = entries[i];
|
|
1147
|
+
|
|
1148
|
+
// Check if can merge: same commit, same source path, and adjacent lines
|
|
1149
|
+
if (
|
|
1150
|
+
current.commitId === next.commitId &&
|
|
1151
|
+
current.sourcePath === next.sourcePath &&
|
|
1152
|
+
current.resultStart + current.lineCount === next.resultStart
|
|
1153
|
+
) {
|
|
1154
|
+
// Merge
|
|
1155
|
+
current = {
|
|
1156
|
+
...current,
|
|
1157
|
+
lineCount: current.lineCount + next.lineCount,
|
|
1158
|
+
};
|
|
1159
|
+
} else {
|
|
1160
|
+
merged.push(current);
|
|
1161
|
+
current = next;
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
merged.push(current);
|
|
1166
|
+
return merged;
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
/**
|
|
1170
|
+
* Create a BlameResult from entries.
|
|
1171
|
+
*/
|
|
1172
|
+
private createBlameResult(path: string, lineCount: number, entries: BlameEntry[]): BlameResult {
|
|
1173
|
+
// Build line-to-entry index for fast lookups
|
|
1174
|
+
const lineToEntry = new Map<number, BlameEntry>();
|
|
1175
|
+
for (const entry of entries) {
|
|
1176
|
+
for (let i = 0; i < entry.lineCount; i++) {
|
|
1177
|
+
lineToEntry.set(entry.resultStart + i, entry);
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
/**
|
|
1182
|
+
* Get the source line number for a given result line.
|
|
1183
|
+
* The source line is computed based on the entry's sourceStart and the offset
|
|
1184
|
+
* within the entry.
|
|
1185
|
+
*/
|
|
1186
|
+
function getSourceLineForResult(line: number): number | undefined {
|
|
1187
|
+
const entry = lineToEntry.get(line);
|
|
1188
|
+
if (!entry) return undefined;
|
|
1189
|
+
// Compute offset within the entry and add to sourceStart
|
|
1190
|
+
const offset = line - entry.resultStart;
|
|
1191
|
+
return entry.sourceStart + offset;
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
return {
|
|
1195
|
+
path,
|
|
1196
|
+
lineCount,
|
|
1197
|
+
entries,
|
|
1198
|
+
|
|
1199
|
+
getEntry(line: number): BlameEntry | undefined {
|
|
1200
|
+
return lineToEntry.get(line);
|
|
1201
|
+
},
|
|
1202
|
+
|
|
1203
|
+
getSourceCommit(line: number): Commit | undefined {
|
|
1204
|
+
const entry = lineToEntry.get(line);
|
|
1205
|
+
return entry?.commit;
|
|
1206
|
+
},
|
|
1207
|
+
|
|
1208
|
+
getSourceAuthor(line: number): PersonIdent | undefined {
|
|
1209
|
+
const entry = lineToEntry.get(line);
|
|
1210
|
+
return entry?.commit.author;
|
|
1211
|
+
},
|
|
1212
|
+
|
|
1213
|
+
getSourceLine(line: number): number | undefined {
|
|
1214
|
+
return getSourceLineForResult(line);
|
|
1215
|
+
},
|
|
1216
|
+
|
|
1217
|
+
getSourcePath(line: number): string | undefined {
|
|
1218
|
+
const entry = lineToEntry.get(line);
|
|
1219
|
+
return entry?.sourcePath;
|
|
1220
|
+
},
|
|
1221
|
+
|
|
1222
|
+
getLineTracking(): LineTracking[] {
|
|
1223
|
+
const tracking: LineTracking[] = [];
|
|
1224
|
+
for (let line = 1; line <= lineCount; line++) {
|
|
1225
|
+
const entry = lineToEntry.get(line);
|
|
1226
|
+
if (entry) {
|
|
1227
|
+
const offset = line - entry.resultStart;
|
|
1228
|
+
tracking.push({
|
|
1229
|
+
resultLine: line,
|
|
1230
|
+
commit: entry.commit,
|
|
1231
|
+
commitId: entry.commitId,
|
|
1232
|
+
sourcePath: entry.sourcePath,
|
|
1233
|
+
sourceLine: entry.sourceStart + offset,
|
|
1234
|
+
});
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
return tracking;
|
|
1238
|
+
},
|
|
1239
|
+
};
|
|
1240
|
+
}
|
|
1241
|
+
}
|