1.7 KB53 lines
Blame
1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8import type {CommitInfo} from 'isl/src/types';
9
10import {diffLines, splitLines} from 'shared/diff';
11
12export function getRealignedBlameInfo(
13 baseBlame: Array<[line: string, info: CommitInfo | undefined]>,
14 newCode: string,
15): Array<[line: string, info: CommitInfo | undefined]> {
16 // TODO: we could refuse to realign for gigantic files, since this is done synchronously it could affect perf.
17
18 const baseLines = baseBlame.map(l => l[0]);
19 const newLines = splitLines(newCode);
20
21 const lineDiffs = diffLines(baseLines, newLines);
22
23 const newRevisionInfo = [...baseBlame];
24 let accumulatedOffset = 0;
25
26 // apply each change to the list of blame
27 for (const [a1, a2, b1, b2] of lineDiffs) {
28 const newEntries = new Array<[string, CommitInfo | undefined]>(b2 - b1).fill(['', undefined]);
29
30 newRevisionInfo.splice(a1 + accumulatedOffset, a2 - a1, ...newEntries);
31
32 // We removed (a2-a1) entries, then added (b2-b1) entries,
33 // which means the *next* a1 index that previously pointed in baseBlame
34 // needs to be offset according to this change since we're modifying newRevisionInfo in-place.
35 accumulatedOffset += b2 - b1 - (a2 - a1);
36 }
37
38 return newRevisionInfo;
39}
40
41/**
42 * Shorten a commit's author string to show inline:
43 * John Smith john@company.com -> John Smith
44 * john@company.com -> john@company.com
45 */
46export function shortenAuthorName(author: string): string {
47 const matchLeadingName = /(.*) [<>()a-zA-Z0-9\-_.+]+@.*/.exec(author);
48 if (matchLeadingName?.[1]) {
49 return matchLeadingName?.[1];
50 }
51 return author;
52}
53