Skip to content

Commit

Permalink
FIxes issues with open prior blame command
Browse files Browse the repository at this point in the history
  - Adds proper error handling & messaging
  - Handles uncommitted lines
Adds open prior blame to hover on uncommitted lines
  • Loading branch information
eamodio committed Jan 14, 2022
1 parent 1fc4d39 commit c18dc7b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
36 changes: 30 additions & 6 deletions src/commands/openFileAtRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,41 @@ export class OpenFileAtRevisionCommand extends ActiveEditorCommand {
if (context.command === Commands.OpenBlamePriorToChange) {
args = { ...args, annotationType: FileAnnotationType.Blame };
if (args.revisionUri == null && context.editor != null) {
const blameline = context.editor.selection.active.line;
if (blameline >= 0) {
const editorLine = context.editor.selection.active.line;
if (editorLine >= 0) {
try {
const gitUri = await GitUri.fromUri(context.editor.document.uri);
const blame = await Container.instance.git.getBlameForLine(gitUri, blameline);
if (blame != null && !blame.commit.isUncommitted && blame.commit.previousSha != null) {
args.revisionUri = GitUri.toRevisionUri(GitUri.fromCommit(blame.commit, true));
const blame = await Container.instance.git.getBlameForLine(gitUri, editorLine);
if (blame != null) {
if (blame.commit.isUncommitted) {
const diffUris = await blame.commit.getPreviousLineDiffUris(
gitUri,
editorLine,
gitUri.sha,
);
if (diffUris?.previous != null) {
args.revisionUri = GitUri.toRevisionUri(diffUris.previous);
} else {
void Messages.showCommitHasNoPreviousCommitWarningMessage(blame.commit);
return undefined;
}
} else if (blame?.commit.previousSha != null) {
args.revisionUri = GitUri.toRevisionUri(GitUri.fromCommit(blame.commit, true));
} else {
void Messages.showCommitHasNoPreviousCommitWarningMessage(blame.commit);
return undefined;
}
}
} catch {}
} catch (ex) {
Logger.error(ex, 'OpenBlamePriorToChangeCommand');
}
}
}

if (args.revisionUri == null) {
void Messages.showGenericErrorMessage('Unable to open blame');
return undefined;
}
}

return this.execute(context.editor, context.uri, args);
Expand Down
6 changes: 6 additions & 0 deletions src/git/formatters/commitFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@ export class CommitFormatter extends Formatter<GitCommit, CommitFormatOptions> {
repoPath: this._item.repoPath,
line: this._options.editor?.line,
})} "Open Changes with Previous Revision")`;

commands += ` &nbsp;&nbsp;[$(versions)](${OpenFileAtRevisionCommand.getMarkdownCommandArgs(
GitUri.toRevisionUri(diffUris.previous),
FileAnnotationType.Blame,
this._options.editor?.line,
)} "Open Blame Prior to this Change")`;
} else {
commands = `\`${this._padOrTruncate(
GitRevision.shorten(
Expand Down
5 changes: 2 additions & 3 deletions src/hovers/hovers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export namespace Hovers {
}
} else {
ref = commit.previousSha;
if (ref == null) return undefined;
}

const line = editorLine + 1;
Expand Down Expand Up @@ -73,9 +74,7 @@ export namespace Hovers {
let current;
if (commit.isUncommitted) {
const diffUris = await commit.getPreviousLineDiffUris(uri, editorLine, documentRef);
if (diffUris == null || diffUris.previous == null) {
return undefined;
}
if (diffUris?.previous == null) return undefined;

message = `[$(compare-changes)](${DiffWithCommand.getMarkdownCommandArgs({
lhs: {
Expand Down

0 comments on commit c18dc7b

Please sign in to comment.