From f547487e418abb4f0c3f3a41ffead25207d0e382 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Fri, 28 Sep 2018 00:10:15 -0400 Subject: [PATCH] Fixes issues with showing deleted file changes Fixes issues with added files in diff title --- src/commands/diffWith.ts | 15 +++++++++++++-- src/git/git.ts | 4 ++++ src/git/gitService.ts | 16 +++++++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/commands/diffWith.ts b/src/commands/diffWith.ts index f7f21a8..e402889 100644 --- a/src/commands/diffWith.ts +++ b/src/commands/diffWith.ts @@ -109,7 +109,18 @@ export class DiffWithCommand extends ActiveEditorCommand { lhsSha = args.lhs.sha; } if (args.rhs.sha !== GitService.deletedOrMissingSha) { - rhsSha = args.rhs.sha; + // Ensure that the file still exists in this commit + const status = await Container.git.getFileStatusForCommit( + args.repoPath, + args.rhs.uri.fsPath, + args.rhs.sha + ); + if (status !== undefined && status.status === 'D') { + args.rhs.sha = GitService.deletedOrMissingSha; + } + else { + rhsSha = args.rhs.sha; + } } const [lhs, rhs] = await Promise.all([ @@ -133,7 +144,7 @@ export class DiffWithCommand extends ActiveEditorCommand { rhsSuffix = `added in ${rhsSuffix}`; } - let lhsSuffix = GitService.shortenSha(lhsSha) || ''; + let lhsSuffix = args.lhs.sha !== GitService.deletedOrMissingSha ? GitService.shortenSha(lhsSha) || '' : ''; if (lhs === undefined && args.rhs.sha === '') { if (rhs !== undefined) { lhsSuffix = `not in ${lhsSuffix}`; diff --git a/src/git/git.ts b/src/git/git.ts index 47f6636..74204e3 100644 --- a/src/git/git.ts +++ b/src/git/git.ts @@ -712,6 +712,10 @@ export class Git { } } + static show_status(repoPath: string, fileName: string, ref: string) { + return git({ cwd: repoPath }, 'show', '--name-status', '--format=', ref, '--', fileName); + } + static stash_apply(repoPath: string, stashName: string, deleteAfter: boolean) { if (!stashName) return undefined; return git({ cwd: repoPath }, 'stash', deleteAfter ? 'pop' : 'apply', stashName); diff --git a/src/git/gitService.ts b/src/git/gitService.ts index 404cbf9..8fa1c89 100644 --- a/src/git/gitService.ts +++ b/src/git/gitService.ts @@ -1102,6 +1102,20 @@ export class GitService implements Disposable { } } + async getFileStatusForCommit(repoPath: string, fileName: string, ref: string): Promise { + if (ref === GitService.deletedOrMissingSha || GitService.isUncommitted(ref)) return undefined; + + Logger.log(`getFileStatusForCommit('${repoPath}', '${fileName}', '${ref}')`); + + const data = await Git.show_status(repoPath, fileName, ref); + if (!data) return undefined; + + const files = GitDiffParser.parseNameStatus(data, repoPath); + if (files === undefined || files.length === 0) return undefined; + + return files[0]; + } + async getRecentLogCommitForFile(repoPath: string | undefined, fileName: string): Promise { return this.getLogCommitForFile(repoPath, fileName, undefined); } @@ -1125,7 +1139,7 @@ export class GitService implements Disposable { options: { ref?: string; firstIfNotFound?: boolean; reverse?: boolean } = {} ): Promise { Logger.log( - `getFileLogCommit('${repoPath}', '${fileName}', '${options.ref}', ${options.firstIfNotFound}, ${ + `getLogCommitForFile('${repoPath}', '${fileName}', '${options.ref}', ${options.firstIfNotFound}, ${ options.reverse })` );