Skip to content

Commit cd7eeda

Browse files
committed
Closes #341 - changes author to You when appropriate
1 parent 71aace5 commit cd7eeda

File tree

4 files changed

+73
-15
lines changed

4 files changed

+73
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
88
### Added
99
- Adds an indicator to the *GitLens* explorer's branch history to mark the the tips of all branches
1010

11+
### Changed
12+
- Changes the author name to "You" when appropriate — closes [#341](https://github.com/eamodio/vscode-gitlens/issues/341)
13+
1114
### Fixed
1215
- Fixes [#345](https://github.com/eamodio/vscode-gitlens/issues/345) - Custom date formats don't work in the GitLens view
1316
- Fixes [#336](https://github.com/eamodio/vscode-gitlens/issues/336) - Default Settings Get Added Automatically

src/git/parsers/blameParser.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface BlameEntry {
2525

2626
export class GitBlameParser {
2727

28-
static parse(data: string, repoPath: string | undefined, fileName: string): GitBlame | undefined {
28+
static parse(data: string, repoPath: string | undefined, fileName: string, currentUser: string | undefined): GitBlame | undefined {
2929
if (!data) return undefined;
3030

3131
const authors: Map<string, GitAuthor> = new Map();
@@ -57,9 +57,15 @@ export class GitBlameParser {
5757

5858
switch (lineParts[0]) {
5959
case 'author':
60-
entry.author = Git.isUncommitted(entry.sha)
61-
? 'You'
62-
: lineParts.slice(1).join(' ').trim();
60+
if (Git.isUncommitted(entry.sha)) {
61+
entry.author = 'You';
62+
}
63+
else {
64+
entry.author = lineParts.slice(1).join(' ').trim();
65+
if (currentUser !== undefined && currentUser === entry.author) {
66+
entry.author = 'You';
67+
}
68+
}
6369
break;
6470

6571
case 'author-mail':

src/git/parsers/logParser.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const emptyEntry: LogEntry = {};
2828

2929
export class GitLogParser {
3030

31-
static parse(data: string, type: GitCommitType, repoPath: string | undefined, fileName: string | undefined, sha: string | undefined, maxCount: number | undefined, reverse: boolean, range: Range | undefined): GitLog | undefined {
31+
static parse(data: string, type: GitCommitType, repoPath: string | undefined, fileName: string | undefined, sha: string | undefined, currentUser: string | undefined, maxCount: number | undefined, reverse: boolean, range: Range | undefined): GitLog | undefined {
3232
if (!data) return undefined;
3333

3434
let relativeFileName: string;
@@ -74,9 +74,15 @@ export class GitLogParser {
7474
break;
7575

7676
case 97: // 'a': // author
77-
entry.author = Git.isUncommitted(entry.ref)
78-
? 'You'
79-
: line.substring(4);
77+
if (Git.isUncommitted(entry.ref)) {
78+
entry.author = 'You';
79+
}
80+
else {
81+
entry.author = line.substring(4);
82+
if (currentUser !== undefined && currentUser === entry.author) {
83+
entry.author = 'You';
84+
}
85+
}
8086
break;
8187

8288
case 101: // 'e': // author-mail

src/gitService.ts

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ export class GitService extends Disposable {
457457

458458
try {
459459
const data = await Git.blame(root, file, uri.sha, { ignoreWhitespace: Container.config.blame.ignoreWhitespace });
460-
const blame = GitBlameParser.parse(data, root, file);
460+
const blame = GitBlameParser.parse(data, root, file, await this.getCurrentUsername(root));
461461
return blame;
462462
}
463463
catch (ex) {
@@ -526,7 +526,7 @@ export class GitService extends Disposable {
526526

527527
try {
528528
const data = await Git.blame_contents(root, file, contents, { correlationKey: `:${key}`, ignoreWhitespace: Container.config.blame.ignoreWhitespace });
529-
const blame = GitBlameParser.parse(data, root, file);
529+
const blame = GitBlameParser.parse(data, root, file, await this.getCurrentUsername(root));
530530
return blame;
531531
}
532532
catch (ex) {
@@ -576,7 +576,7 @@ export class GitService extends Disposable {
576576

577577
try {
578578
const data = await Git.blame(uri.repoPath, fileName, uri.sha, { ignoreWhitespace: Container.config.blame.ignoreWhitespace, startLine: lineToBlame, endLine: lineToBlame });
579-
const blame = GitBlameParser.parse(data, uri.repoPath, fileName);
579+
const blame = GitBlameParser.parse(data, uri.repoPath, fileName, await this.getCurrentUsername(uri.repoPath!));
580580
if (blame === undefined) return undefined;
581581

582582
return {
@@ -618,7 +618,8 @@ export class GitService extends Disposable {
618618

619619
try {
620620
const data = await Git.blame_contents(uri.repoPath, fileName, contents, { ignoreWhitespace: Container.config.blame.ignoreWhitespace, startLine: lineToBlame, endLine: lineToBlame });
621-
const blame = GitBlameParser.parse(data, uri.repoPath, fileName);
621+
const currentUser = await this.getCurrentUsername(uri.repoPath!);
622+
const blame = GitBlameParser.parse(data, uri.repoPath, fileName, currentUser);
622623
if (blame === undefined) return undefined;
623624

624625
return {
@@ -723,6 +724,18 @@ export class GitService extends Disposable {
723724
return await Git.config_get(key, repoPath);
724725
}
725726

727+
// TODO: Clear cache when git config changes
728+
private _userNameMapCache: Map<string, string | undefined> = new Map();
729+
730+
async getCurrentUsername(repoPath: string) {
731+
let user = this._userNameMapCache.get(repoPath);
732+
if (user === undefined) {
733+
user = await Git.config_get('user.name', repoPath);
734+
this._userNameMapCache.set(repoPath, user);
735+
}
736+
return user;
737+
}
738+
726739
async getDiffForFile(uri: GitUri, sha1?: string, sha2?: string): Promise<GitDiff | undefined> {
727740
if (sha1 !== undefined && sha2 === undefined && uri.sha !== undefined) {
728741
sha2 = uri.sha;
@@ -868,7 +881,17 @@ export class GitService extends Disposable {
868881

869882
try {
870883
const data = await Git.log(repoPath, { maxCount: maxCount, ref: options.ref, reverse: options.reverse });
871-
const log = GitLogParser.parse(data, GitCommitType.Branch, repoPath, undefined, options.ref, maxCount, options.reverse!, undefined);
884+
const log = GitLogParser.parse(
885+
data,
886+
GitCommitType.Branch,
887+
repoPath,
888+
undefined,
889+
options.ref,
890+
await this.getCurrentUsername(repoPath),
891+
maxCount,
892+
options.reverse!,
893+
undefined
894+
);
872895

873896
if (log !== undefined) {
874897
const opts = { ...options };
@@ -914,7 +937,17 @@ export class GitService extends Disposable {
914937

915938
try {
916939
const data = await Git.log_search(repoPath, searchArgs, { maxCount: maxCount });
917-
const log = GitLogParser.parse(data, GitCommitType.Branch, repoPath, undefined, undefined, maxCount, false, undefined);
940+
const log = GitLogParser.parse(
941+
data,
942+
GitCommitType.Branch,
943+
repoPath,
944+
undefined,
945+
undefined,
946+
await this.getCurrentUsername(repoPath),
947+
maxCount,
948+
false,
949+
undefined
950+
);
918951

919952
if (log !== undefined) {
920953
const opts = { ...options };
@@ -1015,7 +1048,17 @@ export class GitService extends Disposable {
10151048
: options.maxCount;
10161049

10171050
const data = await Git.log_file(root, file, { ...opts, maxCount: maxCount, startLine: range && range.start.line + 1, endLine: range && range.end.line + 1 });
1018-
const log = GitLogParser.parse(data, GitCommitType.File, root, file, opts.ref, maxCount, opts.reverse!, range);
1051+
const log = GitLogParser.parse(
1052+
data,
1053+
GitCommitType.File,
1054+
root,
1055+
file,
1056+
opts.ref,
1057+
await this.getCurrentUsername(root),
1058+
maxCount,
1059+
opts.reverse!,
1060+
range
1061+
);
10191062

10201063
if (log !== undefined) {
10211064
const opts = { ...options };

0 commit comments

Comments
 (0)