From b88eb545bff28fc781418b63dec4e85561a72421 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Sat, 23 Nov 2019 00:58:28 -0500 Subject: [PATCH] Changes diffWithWorking to use left diff Changes openRevision to use left diff --- package.json | 50 ++++++++++++++++++++++++++++++++++---- src/commands/common.ts | 2 ++ src/commands/diffWithWorking.ts | 32 ++++++++++++++++++++++--- src/commands/openRevisionFile.ts | 52 ++++++++++++++++++++++++++++++++-------- src/git/gitUri.ts | 2 +- 5 files changed, 120 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 4d57c5b..a319228 100644 --- a/package.json +++ b/package.json @@ -2218,6 +2218,15 @@ } }, { + "command": "gitlens.diffWithWorkingInDiffRight", + "title": "Open Changes with Working File", + "category": "GitLens", + "icon": { + "dark": "images/dark/icon-compare-ref-working.svg", + "light": "images/light/icon-compare-ref-working.svg" + } + }, + { "command": "gitlens.diffLineWithWorking", "title": "Open Line Changes with Working File", "category": "GitLens" @@ -2479,6 +2488,16 @@ "enablement": "gitlens:activeFileStatus =~ /revision/ && resourceScheme != git" }, { + "command": "gitlens.openRevisionFileInDiffRight", + "title": "Open Revision", + "category": "GitLens", + "icon": { + "dark": "images/dark/icon-open-revision.svg", + "light": "images/light/icon-open-revision.svg" + }, + "enablement": "gitlens:activeFileStatus =~ /revision/ && resourceScheme != git" + }, + { "command": "gitlens.openWorkingFile", "title": "Open File", "category": "GitLens", @@ -3519,7 +3538,11 @@ }, { "command": "gitlens.diffWithWorking", - "when": "gitlens:activeFileStatus =~ /revision/" + "when": "gitlens:activeFileStatus =~ /revision/ && !isInDiffEditor" + }, + { + "command": "gitlens.diffWithWorkingInDiffRight", + "when": "gitlens:activeFileStatus =~ /revision/ && isInDiffRightEditor" }, { "command": "gitlens.diffLineWithWorking", @@ -3683,7 +3706,11 @@ }, { "command": "gitlens.openRevisionFile", - "when": "gitlens:activeFileStatus =~ /revision/" + "when": "gitlens:activeFileStatus =~ /revision/ && isInDiffLeftEditor" + }, + { + "command": "gitlens.openRevisionFileInDiffRight", + "when": "gitlens:activeFileStatus =~ /revision/ && isInDiffRightEditor" }, { "command": "gitlens.openWorkingFile", @@ -4242,12 +4269,27 @@ "editor/title": [ { "command": "gitlens.diffWithWorking", - "when": "gitlens:activeFileStatus =~ /revision/ && resourceScheme =~ /^(?!(file|git)$).*$/", + "when": "gitlens:activeFileStatus =~ /revision/ && resourceScheme =~ /^(?!(file|git)$).*$/ && !isInDiffEditor", + "group": "navigation@-99" + }, + { + "command": "gitlens.diffWithWorking", + "when": "gitlens:activeFileStatus =~ /revision/ && resourceScheme =~ /^(?!(file|git)$).*$/ && isInDiffLeftEditor", + "group": "navigation@-99" + }, + { + "command": "gitlens.diffWithWorkingInDiffRight", + "when": "gitlens:activeFileStatus =~ /revision/ && resourceScheme =~ /^(?!(file|git)$).*$/ && isInDiffRightEditor", "group": "navigation@-99" }, { "command": "gitlens.openRevisionFile", - "when": "gitlens:activeFileStatus =~ /revision/ && resourceScheme =~ /^(?!(file|git)$).*$/ && isInDiffEditor", + "when": "gitlens:activeFileStatus =~ /revision/ && resourceScheme =~ /^(?!(file|git)$).*$/ && isInDiffLeftEditor", + "group": "navigation@-98" + }, + { + "command": "gitlens.openRevisionFileInDiffRight", + "when": "gitlens:activeFileStatus =~ /revision/ && resourceScheme =~ /^(?!(file|git)$).*$/ && isInDiffRightEditor", "group": "navigation@-98" }, { diff --git a/src/commands/common.ts b/src/commands/common.ts index 4b63f7c..6630b7a 100644 --- a/src/commands/common.ts +++ b/src/commands/common.ts @@ -49,6 +49,7 @@ export enum Commands { DiffLineWithPrevious = 'gitlens.diffLineWithPrevious', DiffWithRevision = 'gitlens.diffWithRevision', DiffWithWorking = 'gitlens.diffWithWorking', + DiffWithWorkingInDiffRight = 'gitlens.diffWithWorkingInDiffRight', DiffLineWithWorking = 'gitlens.diffLineWithWorking', ExploreRepoAtRevision = 'gitlens.exploreRepoAtRevision', ExternalDiff = 'gitlens.externalDiff', @@ -66,6 +67,7 @@ export enum Commands { OpenInRemote = 'gitlens.openInRemote', OpenRepoInRemote = 'gitlens.openRepoInRemote', OpenRevisionFile = 'gitlens.openRevisionFile', + OpenRevisionFileInDiffRight = 'gitlens.openRevisionFileInDiffRight', OpenWorkingFile = 'gitlens.openWorkingFile', PullRepositories = 'gitlens.pullRepositories', PushRepositories = 'gitlens.pushRepositories', diff --git a/src/commands/diffWithWorking.ts b/src/commands/diffWithWorking.ts index bc952ee..f25fd74 100644 --- a/src/commands/diffWithWorking.ts +++ b/src/commands/diffWithWorking.ts @@ -2,10 +2,13 @@ import { commands, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode'; import { Container } from '../container'; import { GitService, GitUri } from '../git/gitService'; -import { ActiveEditorCommand, command, Commands, getCommandUri } from './common'; +import { ActiveEditorCommand, command, CommandContext, Commands, getCommandUri } from './common'; import { DiffWithCommandArgs } from './diffWith'; +import { Messages } from '../messages'; +import { Logger } from '../logger'; export interface DiffWithWorkingCommandArgs { + inDiffRightEditor?: boolean; line?: number; showOptions?: TextDocumentShowOptions; } @@ -13,20 +16,43 @@ export interface DiffWithWorkingCommandArgs { @command() export class DiffWithWorkingCommand extends ActiveEditorCommand { constructor() { - super(Commands.DiffWithWorking); + super([Commands.DiffWithWorking, Commands.DiffWithWorkingInDiffRight]); + } + + protected preExecute(context: CommandContext, args?: DiffWithWorkingCommandArgs) { + if (context.command === Commands.DiffWithWorkingInDiffRight) { + args = { ...args }; + args.inDiffRightEditor = true; + } + + return this.execute(context.editor, context.uri, args); } async execute(editor?: TextEditor, uri?: Uri, args?: DiffWithWorkingCommandArgs): Promise { uri = getCommandUri(uri, editor); if (uri == null) return undefined; - const gitUri = await GitUri.fromUri(uri); + let gitUri = await GitUri.fromUri(uri); args = { ...args }; if (args.line === undefined) { args.line = editor == null ? 0 : editor.selection.active.line; } + if (args.inDiffRightEditor) { + try { + const diffUris = await Container.git.getPreviousDiffUris(gitUri.repoPath!, gitUri, gitUri.sha, 0); + gitUri = diffUris?.previous ?? gitUri; + } catch (ex) { + Logger.error( + ex, + 'DiffWithWorkingCommand', + `getPreviousDiffUris(${gitUri.repoPath}, ${gitUri.fsPath}, ${gitUri.sha})` + ); + return Messages.showGenericErrorMessage('Unable to open compare'); + } + } + // if (args.commit === undefined || args.commit.isUncommitted) { // If the sha is missing, just let the user know the file matches if (gitUri.sha === undefined) return window.showInformationMessage('File matches the working tree'); diff --git a/src/commands/openRevisionFile.ts b/src/commands/openRevisionFile.ts index 7e0045e..108dc55 100644 --- a/src/commands/openRevisionFile.ts +++ b/src/commands/openRevisionFile.ts @@ -5,10 +5,12 @@ import { Container } from '../container'; import { GitUri } from '../git/gitService'; import { Logger } from '../logger'; import { Messages } from '../messages'; -import { ActiveEditorCommand, command, Commands, findOrOpenEditor, getCommandUri } from './common'; +import { ActiveEditorCommand, command, CommandContext, Commands, findOrOpenEditor, getCommandUri } from './common'; export interface OpenRevisionFileCommandArgs { uri?: Uri; + + inDiffRightEditor?: boolean; line?: number; showOptions?: TextDocumentShowOptions; annotationType?: FileAnnotationType; @@ -17,10 +19,19 @@ export interface OpenRevisionFileCommandArgs { @command() export class OpenRevisionFileCommand extends ActiveEditorCommand { constructor() { - super(Commands.OpenRevisionFile); + super([Commands.OpenRevisionFile, Commands.OpenRevisionFileInDiffRight]); } - async execute(editor: TextEditor, uri?: Uri, args?: OpenRevisionFileCommandArgs) { + protected preExecute(context: CommandContext, args?: OpenRevisionFileCommandArgs) { + if (context.command === Commands.OpenRevisionFileInDiffRight) { + args = { ...args }; + args.inDiffRightEditor = true; + } + + return this.execute(context.editor, context.uri, args); + } + + async execute(editor?: TextEditor, uri?: Uri, args?: OpenRevisionFileCommandArgs) { args = { ...args }; if (args.line === undefined) { args.line = editor == null ? 0 : editor.selection.active.line; @@ -34,14 +45,35 @@ export class OpenRevisionFileCommand extends ActiveEditorCommand { uri = args.uri; } - args.uri = await GitUri.fromUri(uri); - if (GitUri.is(args.uri) && args.uri.sha) { - const commit = await Container.git.getCommit(args.uri.repoPath!, args.uri.sha); + const gitUri = await GitUri.fromUri(uri); + if (gitUri?.sha) { + if (args.inDiffRightEditor) { + try { + const diffUris = await Container.git.getPreviousDiffUris( + gitUri.repoPath!, + gitUri, + gitUri.sha, + 0 + ); + args.uri = GitUri.toRevisionUri(diffUris?.previous ?? gitUri); + } catch (ex) { + Logger.error( + ex, + 'OpenRevisionFileCommand', + `getPreviousDiffUris(${gitUri?.repoPath}, ${gitUri.fsPath}, ${gitUri?.sha})` + ); + return Messages.showGenericErrorMessage('Unable to open revision'); + } + } else { + const commit = await Container.git.getCommit(gitUri.repoPath!, gitUri.sha); - args.uri = - commit !== undefined && commit.status === 'D' - ? GitUri.toRevisionUri(commit.previousSha!, commit.previousUri.fsPath, commit.repoPath) - : GitUri.toRevisionUri(args.uri); + args.uri = + commit !== undefined && commit.status === 'D' + ? GitUri.toRevisionUri(commit.previousSha!, commit.previousUri.fsPath, commit.repoPath) + : GitUri.toRevisionUri(gitUri); + } + } else { + args.uri = GitUri.toRevisionUri(gitUri); } if (args.line !== undefined && args.line !== 0) { diff --git a/src/git/gitUri.ts b/src/git/gitUri.ts index cfebe7f..ce0053c 100644 --- a/src/git/gitUri.ts +++ b/src/git/gitUri.ts @@ -259,7 +259,7 @@ export class GitUri extends ((Uri as any) as UriEx) { @debug({ exit: uri => `returned ${Logger.toLoggable(uri)}` }) - static async fromUri(uri: Uri) { + static async fromUri(uri: Uri): Promise { if (GitUri.is(uri)) return uri; if (!Container.git.isTrackable(uri)) return new GitUri(uri);