mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-02-11 18:48:38 -05:00
66 lines
2.4 KiB
TypeScript
66 lines
2.4 KiB
TypeScript
'use strict';
|
|
import { commands, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
|
|
import { ActiveEditorCommand, Commands, getCommandUri } from './common';
|
|
import { DiffWithCommandArgs } from './diffWith';
|
|
import { GitCommit, GitService, GitUri } from '../gitService';
|
|
import { Logger } from '../logger';
|
|
import { Messages } from '../messages';
|
|
|
|
export interface DiffLineWithPreviousCommandArgs {
|
|
commit?: GitCommit;
|
|
|
|
line?: number;
|
|
showOptions?: TextDocumentShowOptions;
|
|
}
|
|
|
|
export class DiffLineWithPreviousCommand extends ActiveEditorCommand {
|
|
|
|
constructor(private git: GitService) {
|
|
super(Commands.DiffLineWithPrevious);
|
|
}
|
|
|
|
async execute(editor?: TextEditor, uri?: Uri, args: DiffLineWithPreviousCommandArgs = {}): Promise<any> {
|
|
uri = getCommandUri(uri, editor);
|
|
if (uri === undefined) return undefined;
|
|
|
|
const gitUri = await GitUri.fromUri(uri, this.git);
|
|
|
|
args = { ...args };
|
|
if (args.line === undefined) {
|
|
args.line = editor === undefined ? gitUri.offset : editor.selection.active.line;
|
|
}
|
|
|
|
if (args.commit === undefined || GitService.isUncommitted(args.commit.sha)) {
|
|
if (editor !== undefined && editor.document !== undefined && editor.document.isDirty) return undefined;
|
|
|
|
const blameline = args.line - gitUri.offset;
|
|
if (blameline < 0) return undefined;
|
|
|
|
try {
|
|
const blame = await this.git.getBlameForLine(gitUri, blameline);
|
|
if (blame === undefined) return Messages.showFileNotUnderSourceControlWarningMessage('Unable to open compare');
|
|
|
|
args.commit = blame.commit;
|
|
}
|
|
catch (ex) {
|
|
Logger.error(ex, 'DiffLineWithPreviousCommand', `getBlameForLine(${blameline})`);
|
|
return window.showErrorMessage(`Unable to open compare. See output channel for more details`);
|
|
}
|
|
}
|
|
|
|
const diffArgs: DiffWithCommandArgs = {
|
|
repoPath: args.commit.repoPath,
|
|
lhs: {
|
|
sha: args.commit.previousSha !== undefined ? args.commit.previousSha : GitService.fakeSha,
|
|
uri: args.commit.previousUri
|
|
},
|
|
rhs: {
|
|
sha: args.commit.sha,
|
|
uri: args.commit.uri
|
|
},
|
|
line: args.line,
|
|
showOptions: args.showOptions
|
|
};
|
|
return commands.executeCommand(Commands.DiffWith, diffArgs);
|
|
}
|
|
} |