From c4483b1b911cf272cb368e190d2b2de19fa17c79 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Tue, 6 Feb 2018 12:05:39 -0500 Subject: [PATCH] Fixes title issue with newly added files --- src/commands/diffWith.ts | 2 +- src/git/git.ts | 10 ++++++---- src/git/models/commit.ts | 2 +- src/gitService.ts | 9 ++++++--- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/commands/diffWith.ts b/src/commands/diffWith.ts index 90aa063..e536f85 100644 --- a/src/commands/diffWith.ts +++ b/src/commands/diffWith.ts @@ -136,7 +136,7 @@ export class DiffWithCommand extends ActiveEditorCommand { args.lhs.title = `${path.basename(args.lhs.uri.fsPath)}${suffix !== '' ? ` (${lhsPrefix}${suffix})` : ''}`; } if (args.rhs.title === undefined && args.rhs.sha !== GitService.deletedSha) { - const suffix = GitService.shortenSha(args.rhs.sha) || ''; + const suffix = GitService.shortenSha(args.rhs.sha, { uncommitted: 'working tree' }) || ''; args.rhs.title = `${path.basename(args.rhs.uri.fsPath)}${suffix !== '' ? ` (${rhsPrefix}${suffix})` : rhsPrefix}`; } diff --git a/src/git/git.ts b/src/git/git.ts index 7052e67..b7d8c0d 100644 --- a/src/git/git.ts +++ b/src/git/git.ts @@ -183,7 +183,7 @@ export class Git { ref = ''; } - const suffix = Strings.truncate(Strings.sanitizeForFileSystem(Git.isSha(ref) ? Git.shortenSha(ref) : ref), 50, ''); + const suffix = Strings.truncate(Strings.sanitizeForFileSystem(Git.isSha(ref) ? Git.shortenSha(ref)! : ref), 50, ''); const ext = path.extname(fileName); const tmp = await import('tmp'); @@ -224,9 +224,11 @@ export class Git { return sha === undefined ? false : Git.uncommittedRegex.test(sha); } - static shortenSha(sha: string) { - if (Git.isStagedUncommitted(sha)) return 'index'; - if (Git.isUncommitted(sha)) return ''; + static shortenSha(sha: string, strings: { deleted?: string, stagedUncommitted?: string, uncommitted?: string } = {}) { + strings = { uncommitted: '', stagedUncommitted: 'index', ...strings }; + + if (Git.isStagedUncommitted(sha)) return strings.stagedUncommitted; + if (Git.isUncommitted(sha)) return strings.uncommitted; const index = sha.indexOf('^'); if (index > 6) { diff --git a/src/git/models/commit.ts b/src/git/models/commit.ts index 0fd19f3..c96ba02 100644 --- a/src/git/models/commit.ts +++ b/src/git/models/commit.ts @@ -122,7 +122,7 @@ export abstract class GitCommit { protected _resolvedPreviousFileSha: string | undefined; get previousFileShortSha(): string { - return Git.shortenSha(this.previousFileSha); + return Git.shortenSha(this.previousFileSha)!; } get previousSha(): string | undefined { diff --git a/src/gitService.ts b/src/gitService.ts index be54ae1..a68ab03 100644 --- a/src/gitService.ts +++ b/src/gitService.ts @@ -1452,12 +1452,15 @@ export class GitService extends Disposable { return Git.isUncommitted(sha); } - static shortenSha(sha: string | undefined) { + static shortenSha(sha: string | undefined, strings: { deleted?: string, stagedUncommitted?: string, uncommitted?: string } = {}) { if (sha === undefined) return undefined; - if (sha === GitService.deletedSha) return '(deleted)'; + + strings = { deleted: '(deleted)', ...strings }; + + if (sha === GitService.deletedSha) return strings.deleted; return Git.isSha(sha) || Git.isStagedUncommitted(sha) - ? Git.shortenSha(sha) + ? Git.shortenSha(sha, strings) : sha; }