Skip to content

Commit

Permalink
Fixes diffs with renamed files
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Apr 28, 2019
1 parent e901bff commit c69777e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/annotations/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,13 @@ export class Annotations {
const commitLine = commit.lines.find(l => l.line === line) || commit.lines[0];

const commitEditorLine = commitLine.originalLine - 1;
const hunkLine = await Container.git.getDiffForLine(uri, commitEditorLine, ref);
const hunkLine = await Container.git.getDiffForLine(
uri,
commitEditorLine,
ref,
undefined,
commit.originalFileName
);
const message = this.getHoverDiffMessage(commit, uri, hunkLine, commitEditorLine);

return {
Expand Down
11 changes: 8 additions & 3 deletions src/git/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -897,10 +897,10 @@ export class Git {
repoPath: string,
fileName: string,
ref: string,
originalFileName?: string,
{ similarityThreshold }: { similarityThreshold?: number } = {}
) {
return git<string>(
{ cwd: repoPath },
const params = [
'show',
`-M${similarityThreshold == null ? '' : `${similarityThreshold}%`}`,
'--format=',
Expand All @@ -909,7 +909,12 @@ export class Git {
ref,
'--',
fileName
);
];
if (originalFileName != null && originalFileName.length !== 0) {
params.push(originalFileName);
}

return git<string>({ cwd: repoPath }, ...params);
}

static show_status(repoPath: string, fileName: string, ref: string) {
Expand Down
18 changes: 13 additions & 5 deletions src/git/gitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,12 @@ export class GitService implements Disposable {
}

@log()
async getDiffForFile(uri: GitUri, ref1: string | undefined, ref2?: string): Promise<GitDiff | undefined> {
async getDiffForFile(
uri: GitUri,
ref1: string | undefined,
ref2?: string,
originalFileName?: string
): Promise<GitDiff | undefined> {
const cc = Logger.getCorrelationContext();

let key = 'diff';
Expand Down Expand Up @@ -1218,6 +1223,7 @@ export class GitService implements Disposable {
uri.fsPath,
ref1,
ref2,
originalFileName,
{ encoding: GitService.getEncoding(uri) },
doc,
key,
Expand All @@ -1241,6 +1247,7 @@ export class GitService implements Disposable {
fileName: string,
ref1: string | undefined,
ref2: string | undefined,
originalFileName: string | undefined,
options: { encoding?: string },
document: TrackedDocument<GitDocumentState>,
key: string,
Expand All @@ -1251,7 +1258,7 @@ export class GitService implements Disposable {
try {
let data;
if (ref1 !== undefined && ref2 === undefined && !GitService.isStagedUncommitted(ref1)) {
data = await Git.show_diff(root, file, ref1, {
data = await Git.show_diff(root, file, ref1, originalFileName, {
similarityThreshold: Container.config.advanced.similarityThreshold
});
}
Expand Down Expand Up @@ -1290,13 +1297,14 @@ export class GitService implements Disposable {
uri: GitUri,
editorLine: number, // editor lines are 0-based
ref1: string | undefined,
ref2?: string
ref2?: string,
originalFileName?: string
): Promise<GitDiffHunkLine | undefined> {
try {
let diff = await this.getDiffForFile(uri, ref1, ref2);
let diff = await this.getDiffForFile(uri, ref1, ref2, originalFileName);
// If we didn't find a diff & ref1 is undefined (meaning uncommitted), check for a staged diff
if (diff === undefined && ref1 === undefined) {
diff = await this.getDiffForFile(uri, Git.stagedUncommittedSha, ref2);
diff = await this.getDiffForFile(uri, Git.stagedUncommittedSha, ref2, originalFileName);
}

if (diff === undefined) return undefined;
Expand Down
4 changes: 3 additions & 1 deletion src/git/parsers/blameParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ export class GitBlameParser {
new Date((entry.authorDate as any) * 1000),
entry.summary!,
relativeFileName,
relativeFileName !== entry.fileName ? entry.fileName : undefined,
entry.previousFileName !== undefined && entry.previousFileName !== entry.fileName
? entry.previousFileName
: undefined,
entry.previousSha,
entry.previousSha && entry.previousFileName,
[]
Expand Down

0 comments on commit c69777e

Please sign in to comment.