Skip to content

Commit 6122550

Browse files
Zyckeamodio
authored andcommitted
Fix incorrect author display You(#457) (#460)
Fixes #457: incorrect displayed username `You`
1 parent b5f94fc commit 6122550

File tree

4 files changed

+68
-25
lines changed

4 files changed

+68
-25
lines changed

src/git/git.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,16 @@ export class Git {
395395
}
396396
}
397397

398+
static async config_getRegex(pattern: string, repoPath?: string) {
399+
try {
400+
const data = await gitCommandCore({ cwd: repoPath || '' }, 'config', '--get-regex', pattern);
401+
return data.trim();
402+
}
403+
catch {
404+
return undefined;
405+
}
406+
}
407+
398408
static diff(repoPath: string, fileName: string, sha1?: string, sha2?: string, options: { encoding?: string } = {}) {
399409
const params = ['-c', 'color.diff=false', 'diff', '--diff-filter=M', '-M', '--no-ext-diff', '--minimal'];
400410
if (sha1) {

src/git/parsers/blameParser.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class GitBlameParser {
2828
data: string,
2929
repoPath: string | undefined,
3030
fileName: string,
31-
currentUser: string | undefined
31+
currentUser: { name?: string, email?: string } | undefined
3232
): GitBlame | undefined {
3333
if (!data) return undefined;
3434

@@ -69,9 +69,6 @@ export class GitBlameParser {
6969
.slice(1)
7070
.join(' ')
7171
.trim();
72-
if (currentUser !== undefined && currentUser === entry.author) {
73-
entry.author = 'You';
74-
}
7572
}
7673
break;
7774

@@ -125,7 +122,7 @@ export class GitBlameParser {
125122
}
126123
first = false;
127124

128-
GitBlameParser.parseEntry(entry, repoPath, relativeFileName, commits, authors, lines);
125+
GitBlameParser.parseEntry(entry, repoPath, relativeFileName, commits, authors, lines, currentUser);
129126

130127
entry = undefined;
131128
break;
@@ -160,11 +157,20 @@ export class GitBlameParser {
160157
fileName: string | undefined,
161158
commits: Map<string, GitBlameCommit>,
162159
authors: Map<string, GitAuthor>,
163-
lines: GitCommitLine[]
160+
lines: GitCommitLine[],
161+
currentUser: {name?: string, email?: string} | undefined
164162
) {
165163
let commit = commits.get(entry.sha);
166164
if (commit === undefined) {
167165
if (entry.author !== undefined) {
166+
if (
167+
currentUser !== undefined &&
168+
(currentUser.name !== undefined || currentUser.email !== undefined) &&
169+
(currentUser.name === undefined || currentUser.name === entry.author) &&
170+
(currentUser.email === undefined || currentUser.email === entry.authorEmail)
171+
) {
172+
entry.author = 'You';
173+
}
168174
let author = authors.get(entry.author);
169175
if (author === undefined) {
170176
author = {

src/git/parsers/logParser.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class GitLogParser {
3232
repoPath: string | undefined,
3333
fileName: string | undefined,
3434
sha: string | undefined,
35-
currentUser: string | undefined,
35+
currentUser: { name?: string; email?: string } | undefined,
3636
maxCount: number | undefined,
3737
reverse: boolean,
3838
range: Range | undefined
@@ -87,9 +87,6 @@ export class GitLogParser {
8787
}
8888
else {
8989
entry.author = line.substring(4);
90-
if (currentUser !== undefined && currentUser === entry.author) {
91-
entry.author = 'You';
92-
}
9390
}
9491
break;
9592

@@ -211,7 +208,8 @@ export class GitLogParser {
211208
relativeFileName,
212209
commits,
213210
authors,
214-
recentCommit
211+
recentCommit,
212+
currentUser
215213
);
216214

217215
break;
@@ -238,10 +236,19 @@ export class GitLogParser {
238236
relativeFileName: string,
239237
commits: Map<string, GitLogCommit>,
240238
authors: Map<string, GitAuthor>,
241-
recentCommit: GitLogCommit | undefined
239+
recentCommit: GitLogCommit | undefined,
240+
currentUser: {name?: string, email?: string} | undefined
242241
): GitLogCommit | undefined {
243242
if (commit === undefined) {
244243
if (entry.author !== undefined) {
244+
if (
245+
currentUser !== undefined &&
246+
(currentUser.name !== undefined || currentUser.email !== undefined) &&
247+
(currentUser.name === undefined || currentUser.name === entry.author) &&
248+
(currentUser.email === undefined || currentUser.email === entry.email)
249+
) {
250+
entry.author = 'You';
251+
}
245252
let author = authors.get(entry.author);
246253
if (author === undefined) {
247254
author = {

src/gitService.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ export class GitService extends Disposable {
612612
args: Container.config.advanced.blame.customArguments,
613613
ignoreWhitespace: Container.config.blame.ignoreWhitespace
614614
});
615-
const blame = GitBlameParser.parse(data, root, file, await this.getCurrentUsername(root));
615+
const blame = GitBlameParser.parse(data, root, file, await this.getCurrentUser(root));
616616
return blame;
617617
}
618618
catch (ex) {
@@ -692,7 +692,7 @@ export class GitService extends Disposable {
692692
correlationKey: `:${key}`,
693693
ignoreWhitespace: Container.config.blame.ignoreWhitespace
694694
});
695-
const blame = GitBlameParser.parse(data, root, file, await this.getCurrentUsername(root));
695+
const blame = GitBlameParser.parse(data, root, file, await this.getCurrentUser(root));
696696
return blame;
697697
}
698698
catch (ex) {
@@ -755,7 +755,7 @@ export class GitService extends Disposable {
755755
data,
756756
uri.repoPath,
757757
fileName,
758-
await this.getCurrentUsername(uri.repoPath!)
758+
await this.getCurrentUser(uri.repoPath!)
759759
);
760760
if (blame === undefined) return undefined;
761761

@@ -808,7 +808,7 @@ export class GitService extends Disposable {
808808
startLine: lineToBlame,
809809
endLine: lineToBlame
810810
});
811-
const currentUser = await this.getCurrentUsername(uri.repoPath!);
811+
const currentUser = await this.getCurrentUser(uri.repoPath!);
812812
const blame = GitBlameParser.parse(data, uri.repoPath, fileName, currentUser);
813813
if (blame === undefined) return undefined;
814814

@@ -925,14 +925,34 @@ export class GitService extends Disposable {
925925
}
926926

927927
// TODO: Clear cache when git config changes
928-
private _userNameMapCache: Map<string, string | undefined> = new Map();
928+
private _userMapCache = new Map<string, { name?: string; email?: string } | null>();
929929

930-
async getCurrentUsername(repoPath: string) {
931-
let user = this._userNameMapCache.get(repoPath);
932-
if (user === undefined) {
933-
user = await Git.config_get('user.name', repoPath);
934-
this._userNameMapCache.set(repoPath, user);
930+
async getCurrentUser(repoPath: string) {
931+
let user = this._userMapCache.get(repoPath);
932+
if (user != null) return user;
933+
if (user === null) return undefined;
934+
935+
const data = await Git.config_getRegex('user.(name|email)', repoPath);
936+
if (!data) {
937+
this._userMapCache.set(repoPath, null);
938+
return undefined;
935939
}
940+
941+
user = { name: undefined, email: undefined };
942+
943+
let match: RegExpExecArray | null = null;
944+
const userConfigRegex = /^user\.(name|email) (.*)$/gm;
945+
do {
946+
match = userConfigRegex.exec(data);
947+
if (match == null) {
948+
break;
949+
}
950+
951+
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
952+
user[match[1] as 'name' | 'email'] = (' ' + match[2]).substr(1);
953+
} while (match !== null);
954+
955+
this._userMapCache.set(repoPath, user);
936956
return user;
937957
}
938958

@@ -1120,7 +1140,7 @@ export class GitService extends Disposable {
11201140
repoPath,
11211141
undefined,
11221142
options.ref,
1123-
await this.getCurrentUsername(repoPath),
1143+
await this.getCurrentUser(repoPath),
11241144
maxCount,
11251145
options.reverse!,
11261146
undefined
@@ -1182,7 +1202,7 @@ export class GitService extends Disposable {
11821202
repoPath,
11831203
undefined,
11841204
undefined,
1185-
await this.getCurrentUsername(repoPath),
1205+
await this.getCurrentUser(repoPath),
11861206
maxCount,
11871207
false,
11881208
undefined
@@ -1337,7 +1357,7 @@ export class GitService extends Disposable {
13371357
root,
13381358
file,
13391359
opts.ref,
1340-
await this.getCurrentUsername(root),
1360+
await this.getCurrentUser(root),
13411361
maxCount,
13421362
opts.reverse!,
13431363
range

0 commit comments

Comments
 (0)