mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-01-15 01:25:42 -05:00
Adds error messages for failed operations
Adds showHistory command support to CodeLens Fixes and improve the showHistory explorer Refactoring
This commit is contained in:
@@ -1,74 +1,60 @@
|
||||
'use strict';
|
||||
import { Iterables } from '../system';
|
||||
import { commands, TextEditor, TextEditorEdit, Uri, window } from 'vscode';
|
||||
import { commands, Range, TextEditor, TextEditorEdit, Uri, window } from 'vscode';
|
||||
import { EditorCommand } from './commands';
|
||||
import { BuiltInCommands, Commands } from '../constants';
|
||||
import BlameAnnotationController from '../blameAnnotationController';
|
||||
import GitProvider from '../gitProvider';
|
||||
import GitProvider, { GitCommit } from '../gitProvider';
|
||||
import { Logger } from '../logger';
|
||||
import * as moment from 'moment';
|
||||
import * as path from 'path';
|
||||
|
||||
export default class DiffWithPreviousCommand extends EditorCommand {
|
||||
constructor(private git: GitProvider, private annotationController: BlameAnnotationController) {
|
||||
constructor(private git: GitProvider) {
|
||||
super(Commands.DiffWithPrevious);
|
||||
}
|
||||
|
||||
async execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, repoPath?: string, sha?: string, shaUri?: Uri, compareWithSha?: string, compareWithUri?: Uri, line?: number) {
|
||||
line = line || editor.selection.active.line;
|
||||
if (sha && !GitProvider.isUncommitted(sha)) {
|
||||
if (!compareWithSha) {
|
||||
return window.showInformationMessage(`Commit ${sha} has no previous commit`);
|
||||
async execute(editor: TextEditor, edit: TextEditorEdit): Promise<any>;
|
||||
async execute(editor: TextEditor, edit: TextEditorEdit, uri: Uri, commit: GitCommit, range?: Range): Promise<any>;
|
||||
async execute(editor: TextEditor, edit: TextEditorEdit, uri: Uri, commit: GitCommit, line?: number): Promise<any>;
|
||||
async execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, commit?: GitCommit, rangeOrLine?: Range | number): Promise<any> {
|
||||
let line = editor.selection.active.line;
|
||||
if (typeof rangeOrLine === 'number') {
|
||||
line = rangeOrLine || line;
|
||||
}
|
||||
|
||||
if (!commit || rangeOrLine instanceof Range) {
|
||||
if (!(uri instanceof Uri)) {
|
||||
if (!editor.document) return undefined;
|
||||
uri = editor.document.uri;
|
||||
}
|
||||
|
||||
return Promise.all([this.git.getVersionedFile(shaUri.fsPath, repoPath, sha), this.git.getVersionedFile(compareWithUri.fsPath, repoPath, compareWithSha)])
|
||||
.then(values => commands.executeCommand(BuiltInCommands.Diff, Uri.file(values[1]), Uri.file(values[0]), `${path.basename(compareWithUri.fsPath)} (${compareWithSha}) ↔ ${path.basename(shaUri.fsPath)} (${sha})`))
|
||||
.then(() => commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: line, at: 'center' }))
|
||||
.catch(ex => Logger.error('[GitLens.DiffWithPreviousCommand]', 'getVersionedFile', ex));
|
||||
}
|
||||
|
||||
if (!(uri instanceof Uri)) {
|
||||
if (!editor.document) return undefined;
|
||||
uri = editor.document.uri;
|
||||
}
|
||||
|
||||
if (this.annotationController.annotated) {
|
||||
try {
|
||||
const blame = await this.git.getBlameForLine(uri.fsPath, line);
|
||||
if (!blame) return undefined;
|
||||
const log = await this.git.getLogForFile(uri.fsPath, <Range>rangeOrLine);
|
||||
if (!log) return window.showWarningMessage(`Unable to open diff. File is probably not under source control`);
|
||||
|
||||
// If the line is uncommitted, find the previous commit
|
||||
const commit = blame.commit;
|
||||
if (commit.isUncommitted) {
|
||||
try {
|
||||
const prevBlame = await this.git.getBlameForLine(commit.previousUri.fsPath, blame.line.originalLine + 1, commit.previousSha, commit.repoPath);
|
||||
if (!prevBlame) return undefined;
|
||||
|
||||
const prevCommit = prevBlame.commit;
|
||||
return commands.executeCommand(Commands.DiffWithPrevious, commit.previousUri, commit.repoPath, commit.previousSha, commit.previousUri, prevCommit.sha, prevCommit.uri, blame.line.originalLine);
|
||||
}
|
||||
catch (ex) {
|
||||
Logger.error('[GitLens.DiffWithPreviousCommand]', `getBlameForLine(${blame.line.originalLine}, ${commit.previousSha})`, ex);
|
||||
}
|
||||
}
|
||||
return commands.executeCommand(Commands.DiffWithPrevious, commit.uri, commit.repoPath, commit.sha, commit.uri, commit.previousSha, commit.previousUri, line);
|
||||
}
|
||||
catch (ex) {
|
||||
Logger.error('[GitLens.DiffWithPreviousCommand]', `getBlameForLine(${line})`, ex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
try {
|
||||
const log = await this.git.getLogForFile(uri.fsPath);
|
||||
if (!log) return undefined;
|
||||
|
||||
const commits = log.commits.values();
|
||||
const commit = Iterables.next(commits);
|
||||
const prevCommit = Iterables.next(commits);
|
||||
return commands.executeCommand(Commands.DiffWithPrevious, commit.uri, commit.repoPath, commit.sha, commit.uri, prevCommit.sha, prevCommit.uri, line);
|
||||
commit = commit ? Iterables.find(log.commits.values(), _ => _.sha === commit.sha) : Iterables.first(log.commits.values());
|
||||
}
|
||||
catch (ex) {
|
||||
Logger.error('[GitLens.DiffWithPreviousCommand]', `getLogForFile(${uri.fsPath})`, ex);
|
||||
return window.showErrorMessage(`Unable to open diff. See output channel for more details`);
|
||||
}
|
||||
}
|
||||
|
||||
if (!commit.previousSha) {
|
||||
return window.showInformationMessage(`Commit ${commit.sha} (${commit.author}, ${moment(commit.date).fromNow()}) has no previous commit`);
|
||||
}
|
||||
|
||||
try {
|
||||
const values = await Promise.all([
|
||||
this.git.getVersionedFile(commit.uri.fsPath, commit.repoPath, commit.sha),
|
||||
this.git.getVersionedFile(commit.previousUri.fsPath, commit.repoPath, commit.previousSha)
|
||||
]);
|
||||
await commands.executeCommand(BuiltInCommands.Diff, Uri.file(values[1]), Uri.file(values[0]), `${path.basename(commit.previousUri.fsPath)} (${commit.previousSha}) ↔ ${path.basename(commit.uri.fsPath)} (${commit.sha})`);
|
||||
return await commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: line, at: 'center' });
|
||||
}
|
||||
catch (ex) {
|
||||
Logger.error('[GitLens.DiffWithPreviousCommand]', 'getVersionedFile', ex);
|
||||
return window.showErrorMessage(`Unable to open diff. See output channel for more details`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user