Fixes issues with diff with previous

Wouldn't always grab the correct commit
This commit is contained in:
Eric Amodio
2017-02-13 11:42:54 -05:00
parent 3ef538b713
commit d5d0c3a28d
6 changed files with 20 additions and 48 deletions

View File

@@ -21,7 +21,7 @@ export default class DiffLineWithPreviousCommand extends EditorCommand {
} }
line = line || editor.selection.active.line; line = line || editor.selection.active.line;
const gitUri = GitUri.fromUri(uri, this.git); let gitUri = GitUri.fromUri(uri, this.git);
if (!commit || GitProvider.isUncommitted(commit.sha)) { if (!commit || GitProvider.isUncommitted(commit.sha)) {
const blameline = line - gitUri.offset; const blameline = line - gitUri.offset;
@@ -32,10 +32,10 @@ export default class DiffLineWithPreviousCommand extends EditorCommand {
if (!blame) return window.showWarningMessage(`Unable to open diff. File is probably not under source control`); if (!blame) return window.showWarningMessage(`Unable to open diff. File is probably not under source control`);
commit = blame.commit; commit = blame.commit;
// If the current commit matches the blame, show the previous
if (gitUri.sha === commit.sha) { // If we don't have a sha or the current commit matches the blame, show the previous
commit = new GitCommit(commit.repoPath, commit.previousSha, commit.previousFileName, commit.author, commit.date, commit.message); if (!gitUri.sha || gitUri.sha === commit.sha) {
line = blame.line.line + 1 + gitUri.offset; return commands.executeCommand(Commands.DiffWithPrevious, new GitUri(uri, commit), undefined, line);
} }
// If the line is uncommitted, find the previous commit and treat it as a DiffWithWorking // If the line is uncommitted, find the previous commit and treat it as a DiffWithWorking
@@ -53,11 +53,11 @@ export default class DiffLineWithPreviousCommand extends EditorCommand {
} }
try { try {
const values = await Promise.all([ const [rhs, lhs] = await Promise.all([
this.git.getVersionedFile(gitUri.fsPath, gitUri.repoPath, gitUri.sha), this.git.getVersionedFile(gitUri.fsPath, gitUri.repoPath, gitUri.sha),
this.git.getVersionedFile(commit.uri.fsPath, commit.repoPath, commit.sha) this.git.getVersionedFile(commit.uri.fsPath, commit.repoPath, commit.sha)
]); ]);
await commands.executeCommand(BuiltInCommands.Diff, Uri.file(values[1]), Uri.file(values[0]), `${path.basename(commit.uri.fsPath)} (${commit.sha}) ↔ ${path.basename(gitUri.fsPath)} (${gitUri.sha})`); await commands.executeCommand(BuiltInCommands.Diff, Uri.file(lhs), Uri.file(rhs), `${path.basename(commit.uri.fsPath)} (${commit.sha}) ↔ ${path.basename(gitUri.fsPath)} (${gitUri.sha})`);
return await commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: line, at: 'center' }); return await commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: line, at: 'center' });
} }
catch (ex) { catch (ex) {

View File

@@ -34,7 +34,7 @@ export default class DiffWithPreviousCommand extends EditorCommand {
const gitUri = GitUri.fromUri(uri, this.git); const gitUri = GitUri.fromUri(uri, this.git);
try { try {
const log = await this.git.getLogForFile(gitUri.fsPath, gitUri.sha, gitUri.repoPath, rangeOrLine as Range); const log = await this.git.getLogForFile(gitUri.fsPath, undefined, gitUri.repoPath, rangeOrLine as Range);
if (!log) return window.showWarningMessage(`Unable to open diff. File is probably not under source control`); if (!log) return window.showWarningMessage(`Unable to open diff. File is probably not under source control`);
const sha = (commit && commit.sha) || gitUri.sha; const sha = (commit && commit.sha) || gitUri.sha;
@@ -51,38 +51,10 @@ export default class DiffWithPreviousCommand extends EditorCommand {
} }
try { try {
let rhs: string; const [rhs, lhs] = await Promise.all([
try { this.git.getVersionedFile(commit.uri.fsPath, commit.repoPath, commit.sha),
rhs = await this.git.getVersionedFile(commit.uri.fsPath, commit.repoPath, commit.sha); this.git.getVersionedFile(commit.previousUri.fsPath, commit.repoPath, commit.previousSha)
} ]);
catch (ex) {
if (ex.message.includes(`fatal: Path '${commit.originalFileName}' does not exist in '${commit.sha}'`)) {
try {
rhs = await this.git.getVersionedFile(commit.fileName, commit.repoPath, commit.sha);
}
catch (ex) {
Logger.error('[GitLens.DiffWithPreviousCommand]', 'getVersionedFile', ex);
return window.showErrorMessage(`Unable to open diff. See output channel for more details`);
}
}
}
let lhs: string;
try {
lhs = await this.git.getVersionedFile(commit.previousUri.fsPath, commit.repoPath, commit.previousSha);
}
catch (ex) {
if (ex.message.includes(`fatal: Path '${commit.previousFileName}' does not exist in '${commit.previousSha}'`)) {
try {
lhs = await this.git.getVersionedFile(commit.uri.fsPath, commit.repoPath, commit.previousSha);
}
catch (ex) {
Logger.error('[GitLens.DiffWithPreviousCommand]', 'getVersionedFile', ex);
return window.showErrorMessage(`Unable to open diff. See output channel for more details`);
}
}
}
await commands.executeCommand(BuiltInCommands.Diff, Uri.file(lhs), Uri.file(rhs), `${path.basename(commit.previousUri.fsPath)} (${commit.previousSha}) ↔ ${path.basename(commit.uri.fsPath)} (${commit.sha})`); await commands.executeCommand(BuiltInCommands.Diff, Uri.file(lhs), Uri.file(rhs), `${path.basename(commit.previousUri.fsPath)} (${commit.previousSha}) ↔ ${path.basename(commit.uri.fsPath)} (${commit.sha})`);
return await commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: line, at: 'center' }); return await commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: line, at: 'center' });
} }

View File

@@ -133,7 +133,7 @@ export class GitLogParserEnricher implements IGitEnricher<IGitLog> {
} }
else { else {
// Try to get the repoPath from the most recent commit // Try to get the repoPath from the most recent commit
repoPath = fileNameOrRepoPath.replace(`/${entry.fileName}`, ''); repoPath = fileNameOrRepoPath.replace(fileNameOrRepoPath.startsWith('/') ? `/${entry.fileName}` : entry.fileName, '');
relativeFileName = path.relative(repoPath, fileNameOrRepoPath).replace(/\\/g, '/'); relativeFileName = path.relative(repoPath, fileNameOrRepoPath).replace(/\\/g, '/');
} }
} }
@@ -160,10 +160,8 @@ export class GitLogParserEnricher implements IGitEnricher<IGitLog> {
if (recentCommit) { if (recentCommit) {
recentCommit.previousSha = commit.sha; recentCommit.previousSha = commit.sha;
if (!isRepoPath) {
recentCommit.previousFileName = commit.originalFileName || commit.fileName; recentCommit.previousFileName = commit.originalFileName || commit.fileName;
} }
}
recentCommit = commit; recentCommit = commit;
} }

View File

@@ -70,7 +70,7 @@ export default class Git {
const params = [`blame`, `--root`, format]; const params = [`blame`, `--root`, format];
if (sha) { if (sha) {
params.push(`${sha}^!`); params.push(sha);
} }
return gitCommand(root, ...params, `--`, file); return gitCommand(root, ...params, `--`, file);
@@ -81,7 +81,7 @@ export default class Git {
const params = [`blame`, `--root`, format, `-L ${startLine},${endLine}`]; const params = [`blame`, `--root`, format, `-L ${startLine},${endLine}`];
if (sha) { if (sha) {
params.push(`${sha}^!`); params.push(sha);
} }
return gitCommand(root, ...params, `--`, file); return gitCommand(root, ...params, `--`, file);

View File

@@ -42,6 +42,8 @@ export class GitUri extends Uri {
} }
static fromUri(uri: Uri, git?: GitProvider) { static fromUri(uri: Uri, git?: GitProvider) {
if (uri instanceof GitUri) return uri;
if (git) { if (git) {
const gitUri = git.getGitUriForFile(uri.fsPath); const gitUri = git.getGitUriForFile(uri.fsPath);
if (gitUri) return gitUri; if (gitUri) return gitUri;

View File

@@ -441,7 +441,7 @@ export default class GitProvider extends Disposable {
Logger.log(`getLogForFile('${fileName}', ${sha}, ${repoPath}, ${range && `[${range.start.line}, ${range.end.line}]`}, ${maxCount})`); Logger.log(`getLogForFile('${fileName}', ${sha}, ${repoPath}, ${range && `[${range.start.line}, ${range.end.line}]`}, ${maxCount})`);
fileName = Git.normalizePath(fileName); fileName = Git.normalizePath(fileName);
const useCaching = this.UseGitCaching && !range && !maxCount; const useCaching = this.UseGitCaching && !sha && !range && !maxCount;
let cacheKey: string; let cacheKey: string;
let entry: GitCacheEntry; let entry: GitCacheEntry;
@@ -464,7 +464,7 @@ export default class GitProvider extends Disposable {
return (range return (range
? Git.logRange(fileName, range.start.line + 1, range.end.line + 1, sha, repoPath, maxCount) ? Git.logRange(fileName, range.start.line + 1, range.end.line + 1, sha, repoPath, maxCount)
: Git.log(fileName, sha, repoPath, maxCount)) : Git.log(fileName, sha, repoPath, maxCount))
.then(data => new GitLogParserEnricher().enrich(data, fileName)) .then(data => new GitLogParserEnricher().enrich(data, repoPath || fileName, !!repoPath))
.catch(ex => { .catch(ex => {
// Trap and cache expected log errors // Trap and cache expected log errors
if (useCaching) { if (useCaching) {