From a91e61aa79790455c6f073c5e05c3a7db9f9857d Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Tue, 13 Jun 2023 23:04:45 -0400 Subject: [PATCH] Fixes comparison commands on File/Line History --- src/commands/base.ts | 6 +++-- src/views/viewCommands.ts | 68 +++++++++++++++++++++++++++++++---------------- 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/src/commands/base.ts b/src/commands/base.ts index 707e268..6aee7f5 100644 --- a/src/commands/base.ts +++ b/src/commands/base.ts @@ -23,7 +23,7 @@ import type { GitTag } from '../git/models/tag'; import { isTag } from '../git/models/tag'; import { registerCommand } from '../system/command'; import { sequentialize } from '../system/function'; -import { ViewNode, ViewRefNode } from '../views/nodes/viewNode'; +import { ViewNode, ViewRefFileNode, ViewRefNode } from '../views/nodes/viewNode'; export function getCommandUri(uri?: Uri, editor?: TextEditor): Uri | undefined { // Always use the editor.uri (if we have one), so we are correct for a split diff @@ -162,7 +162,9 @@ export function isCommandContextViewNodeHasFileRefs(context: CommandContext): co export function isCommandContextViewNodeHasRef( context: CommandContext, ): context is CommandViewNodeContext & { node: ViewNode & { ref: GitReference } } { - return context.type === 'viewItem' && context.node instanceof ViewRefNode; + return ( + context.type === 'viewItem' && context.node instanceof ViewRefNode && context.node instanceof ViewRefFileNode + ); } export function isCommandContextViewNodeHasRemote( diff --git a/src/views/viewCommands.ts b/src/views/viewCommands.ts index 6982ff2..60a725c 100644 --- a/src/views/viewCommands.ts +++ b/src/views/viewCommands.ts @@ -376,9 +376,9 @@ export class ViewCommands { } @debug() - private async createBranch(node?: ViewRefNode | BranchesNode | BranchTrackingStatusNode) { + private async createBranch(node?: ViewRefNode | ViewRefFileNode | BranchesNode | BranchTrackingStatusNode) { let from = - node instanceof ViewRefNode + node instanceof ViewRefNode || node instanceof ViewRefFileNode ? node?.ref : node instanceof BranchTrackingStatusNode ? node.branch @@ -426,9 +426,9 @@ export class ViewCommands { } @debug() - private async createTag(node?: ViewRefNode | TagsNode | BranchTrackingStatusNode) { + private async createTag(node?: ViewRefNode | ViewRefFileNode | TagsNode | BranchTrackingStatusNode) { let from = - node instanceof ViewRefNode + node instanceof ViewRefNode || node instanceof ViewRefFileNode ? node?.ref : node instanceof BranchTrackingStatusNode ? node.branch @@ -887,8 +887,12 @@ export class ViewCommands { } @debug() - private compareHeadWith(node: ViewRefNode) { - if (!(node instanceof ViewRefNode)) return Promise.resolve(); + private compareHeadWith(node: ViewRefNode | ViewRefFileNode) { + if (!(node instanceof ViewRefNode) && !(node instanceof ViewRefFileNode)) return Promise.resolve(); + + if (node instanceof ViewRefFileNode) { + return this.compareFileWith(node.repoPath, node.uri, node.ref.ref, undefined, 'HEAD'); + } return this.container.searchAndCompareView.compare(node.repoPath, 'HEAD', node.ref); } @@ -902,8 +906,12 @@ export class ViewCommands { } @debug() - private compareWorkingWith(node: ViewRefNode) { - if (!(node instanceof ViewRefNode)) return Promise.resolve(); + private compareWorkingWith(node: ViewRefNode | ViewRefFileNode) { + if (!(node instanceof ViewRefNode) && !(node instanceof ViewRefFileNode)) return Promise.resolve(); + + if (node instanceof ViewRefFileNode) { + return this.compareFileWith(node.repoPath, node.uri, node.ref.ref, undefined, ''); + } return this.container.searchAndCompareView.compare(node.repoPath, '', node.ref); } @@ -929,19 +937,43 @@ export class ViewCommands { } @debug() - private compareWithSelected(node: ViewRefNode) { - if (!(node instanceof ViewRefNode)) return; + private compareWithSelected(node: ViewRefNode | ViewRefFileNode) { + if (!(node instanceof ViewRefNode) && !(node instanceof ViewRefFileNode)) return; this.container.searchAndCompareView.compareWithSelected(node.repoPath, node.ref); } @debug() - private selectForCompare(node: ViewRefNode) { - if (!(node instanceof ViewRefNode)) return; + private selectForCompare(node: ViewRefNode | ViewRefFileNode) { + if (!(node instanceof ViewRefNode) && !(node instanceof ViewRefFileNode)) return; this.container.searchAndCompareView.selectForCompare(node.repoPath, node.ref); } + private async compareFileWith( + repoPath: string, + lhsUri: Uri, + lhsRef: string, + rhsUri: Uri | undefined, + rhsRef: string, + ) { + if (rhsUri == null) { + rhsUri = await this.container.git.getWorkingUri(repoPath, lhsUri); + } + + return executeCommand(Commands.DiffWith, { + repoPath: repoPath, + lhs: { + sha: lhsRef, + uri: lhsUri, + }, + rhs: { + sha: rhsRef, + uri: rhsUri ?? lhsUri, + }, + }); + } + @debug() private compareFileWithSelected(node: ViewRefFileNode) { if (this._selectedFile == null || !(node instanceof ViewRefFileNode) || node.ref == null) { @@ -958,17 +990,7 @@ export class ViewCommands { this._selectedFile = undefined; void setContext('gitlens:views:canCompare:file', false); - return executeCommand(Commands.DiffWith, { - repoPath: selected.repoPath, - lhs: { - sha: selected.ref, - uri: selected.uri!, - }, - rhs: { - sha: node.ref.ref, - uri: node.uri, - }, - }); + return this.compareFileWith(selected.repoPath!, selected.uri!, selected.ref, node.uri, node.ref.ref); } private _selectedFile: CompareSelectedInfo | undefined;