From eb03a428c7b1bae04b7b241219d7692d6b52b996 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Wed, 28 Sep 2022 01:18:38 -0400 Subject: [PATCH] Adds Show in Commit Graph to view items e.g. branches, commits, stashes, tags --- package.json | 17 ++++++- src/commands/gitCommands.actions.ts | 10 ++-- src/constants.ts | 2 +- src/git/formatters/commitFormatter.ts | 10 ++-- src/git/models/reference.ts | 17 +++++++ src/plus/webviews/graph/graphWebview.ts | 53 ++++++++++++++-------- .../commitDetails/commitDetailsWebviewView.ts | 8 ++-- 7 files changed, 82 insertions(+), 35 deletions(-) diff --git a/package.json b/package.json index de24f19..cade14d 100644 --- a/package.json +++ b/package.json @@ -4039,6 +4039,12 @@ "icon": "$(gitlens-graph)" }, { + "command": "gitlens.showInCommitGraph", + "title": "Show in Commit Graph", + "category": "GitLens+", + "icon": "$(gitlens-graph)" + }, + { "command": "gitlens.refreshGraphPage", "title": "Refresh", "category": "GitLens", @@ -6701,6 +6707,10 @@ "when": "gitlens:enabled" }, { + "command": "gitlens.showInCommitGraph", + "when": "false" + }, + { "command": "gitlens.refreshGraphPage", "when": "false" }, @@ -9866,9 +9876,14 @@ "group": "3_gitlens_explore@0" }, { + "command": "gitlens.showInCommitGraph", + "when": "viewItem =~ /gitlens:(branch|commit|stash|tag|file\\b(?=.*?\\b\\+committed\\b))\\b/", + "group": "3_gitlens_explore@1" + }, + { "command": "gitlens.revealCommitInView", "when": "view =~ /gitlens\\.views\\.(?!commits|branches\\b)/ && viewItem =~ /gitlens:commit\\b/", - "group": "3_gitlens_explore@1" + "group": "3_gitlens_explore@2" }, { "command": "gitlens.openCommitOnRemote", diff --git a/src/commands/gitCommands.actions.ts b/src/commands/gitCommands.actions.ts index e8a9f6b..0fe7b6b 100644 --- a/src/commands/gitCommands.actions.ts +++ b/src/commands/gitCommands.actions.ts @@ -21,16 +21,15 @@ import type { GitContributor } from '../git/models/contributor'; import type { GitFile } from '../git/models/file'; import type { GitBranchReference, - GitReference, GitRevisionReference, GitStashReference, GitTagReference, } from '../git/models/reference'; -import { GitRevision } from '../git/models/reference'; +import { GitReference, GitRevision } from '../git/models/reference'; import type { GitRemote } from '../git/models/remote'; import type { Repository } from '../git/models/repository'; import type { GitWorktree } from '../git/models/worktree'; -import type { ShowCommitInGraphCommandArgs } from '../plus/webviews/graph/graphWebview'; +import type { ShowInCommitGraphCommandArgs } from '../plus/webviews/graph/graphWebview'; import { RepositoryPicker } from '../quickpicks/repositoryPicker'; import { ensure } from '../system/array'; import { executeCommand, executeCoreCommand, executeEditorCommand } from '../system/command'; @@ -778,9 +777,8 @@ export namespace GitActions { commit: GitRevisionReference | GitCommit, options?: { preserveFocus?: boolean }, ): Promise { - void (await executeCommand(Commands.ShowCommitInGraph, { - sha: commit.ref, - repoPath: commit.repoPath, + void (await executeCommand(Commands.ShowInCommitGraph, { + ref: GitReference.fromRevision(commit), preserveFocus: options?.preserveFocus, })); } diff --git a/src/constants.ts b/src/constants.ts index acd5794..a07d235 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -163,13 +163,13 @@ export const enum Commands { SearchCommitsInView = 'gitlens.views.searchAndCompare.searchCommits', SetViewsLayout = 'gitlens.setViewsLayout', ShowBranchesView = 'gitlens.showBranchesView', - ShowCommitInGraph = 'gitlens.showCommitInGraph', ShowCommitInView = 'gitlens.showCommitInView', ShowCommitsInView = 'gitlens.showCommitsInView', ShowCommitsView = 'gitlens.showCommitsView', ShowContributorsView = 'gitlens.showContributorsView', ShowHomeView = 'gitlens.showHomeView', ShowFileHistoryView = 'gitlens.showFileHistoryView', + ShowInCommitGraph = 'gitlens.showInCommitGraph', ShowLastQuickPick = 'gitlens.showLastQuickPick', ShowLineHistoryView = 'gitlens.showLineHistoryView', ShowQuickBranchHistory = 'gitlens.showQuickBranchHistory', diff --git a/src/git/formatters/commitFormatter.ts b/src/git/formatters/commitFormatter.ts index 88b3b00..a0192ec 100644 --- a/src/git/formatters/commitFormatter.ts +++ b/src/git/formatters/commitFormatter.ts @@ -19,7 +19,7 @@ import { configuration, DateStyle, FileAnnotationType } from '../../configuratio import { Commands, GlyphChars } from '../../constants'; import { Container } from '../../container'; import { emojify } from '../../emojis'; -import type { ShowCommitInGraphCommandArgs } from '../../plus/webviews/graph/graphWebview'; +import type { ShowInCommitGraphCommandArgs } from '../../plus/webviews/graph/graphWebview'; import { join, map } from '../../system/iterable'; import { PromiseCancelledError } from '../../system/promise'; import type { TokenOptions } from '../../system/string'; @@ -30,7 +30,7 @@ import type { GitCommit } from '../models/commit'; import { isCommit } from '../models/commit'; import type { IssueOrPullRequest } from '../models/issue'; import { PullRequest } from '../models/pullRequest'; -import { GitRevision } from '../models/reference'; +import { GitReference, GitRevision } from '../models/reference'; import { GitRemote } from '../models/remote'; import type { RemoteProvider } from '../remotes/remoteProvider'; import type { FormatOptions } from './formatter'; @@ -354,9 +354,9 @@ export class CommitFormatter extends Formatter { )} "Open Blame Prior to this Change")`; } - commands += `  [$(gitlens-graph)](${Command.getMarkdownCommandArgsCore( - Commands.ShowCommitInGraph, - { sha: this._item.sha, repoPath: this._item.repoPath }, + commands += `  [$(gitlens-graph)](${Command.getMarkdownCommandArgsCore( + Commands.ShowInCommitGraph, + { ref: GitReference.fromRevision(this._item) }, )} "Show in Commit Graph")`; if (this._options.remotes != null && this._options.remotes.length !== 0) { diff --git a/src/git/models/reference.ts b/src/git/models/reference.ts index 7d3d50a..211b6e1 100644 --- a/src/git/models/reference.ts +++ b/src/git/models/reference.ts @@ -209,6 +209,23 @@ export namespace GitReference { }); } + export function fromRevision(revision: GitRevisionReference) { + if (revision.refType === 'stash') { + return create(revision.ref, revision.repoPath, { + refType: revision.refType, + name: revision.name, + number: revision.number, + message: revision.message, + }); + } + + return create(revision.ref, revision.repoPath, { + refType: revision.refType, + name: revision.name, + message: revision.message, + }); + } + export function fromTag(tag: GitTagReference) { return create(tag.ref, tag.repoPath, { refType: tag.refType, diff --git a/src/plus/webviews/graph/graphWebview.ts b/src/plus/webviews/graph/graphWebview.ts index 4e834d2..b47f16a 100644 --- a/src/plus/webviews/graph/graphWebview.ts +++ b/src/plus/webviews/graph/graphWebview.ts @@ -47,6 +47,11 @@ import { updateRecordValue } from '../../../system/object'; import { isDarkTheme, isLightTheme } from '../../../system/utils'; import type { WebviewItemContext } from '../../../system/webview'; import { isWebviewItemContext, serializeWebviewItemContext } from '../../../system/webview'; +import type { BranchNode } from '../../../views/nodes/branchNode'; +import type { CommitFileNode } from '../../../views/nodes/commitFileNode'; +import type { CommitNode } from '../../../views/nodes/commitNode'; +import type { StashNode } from '../../../views/nodes/stashNode'; +import type { TagNode } from '../../../views/nodes/tagNode'; import { RepositoryFolderNode } from '../../../views/nodes/viewNode'; import { onIpc } from '../../../webviews/protocol'; import type { IpcMessage, IpcMessageParams, IpcNotificationType } from '../../../webviews/protocol'; @@ -88,9 +93,8 @@ import { UpdateSelectionCommandType, } from './protocol'; -export interface ShowCommitInGraphCommandArgs { - repoPath: string; - sha: string; +export interface ShowInCommitGraphCommandArgs { + ref: GitReference; preserveFocus?: boolean; } @@ -157,22 +161,35 @@ export class GraphWebview extends WebviewBase { this.disposables.push( configuration.onDidChange(this.onConfigurationChanged, this), { dispose: () => this._statusBarItem?.dispose() }, - registerCommand(Commands.ShowCommitInGraph, (args: ShowCommitInGraphCommandArgs) => { - this.repository = this.container.git.getRepository(args.repoPath); - this.setSelectedRows(args.sha); - - if (this._panel == null) { - void this.show({ preserveFocus: args.preserveFocus }); - } else { - if (this._graph?.ids.has(args.sha)) { - void this.notifyDidChangeSelection(); - return; + registerCommand( + Commands.ShowInCommitGraph, + async ( + args: ShowInCommitGraphCommandArgs | BranchNode | CommitNode | CommitFileNode | StashNode | TagNode, + ) => { + this.repository = this.container.git.getRepository(args.ref.repoPath); + let sha = args.ref.ref; + if (!GitRevision.isSha(sha)) { + sha = await this.container.git.resolveReference(args.ref.repoPath, sha, undefined, { + force: true, + }); } - - this.setSelectedRows(args.sha); - void this.onGetMoreCommits({ sha: args.sha }); - } - }), + this.setSelectedRows(sha); + + const preserveFocus = 'preserveFocus' in args ? args.preserveFocus ?? false : false; + if (this._panel == null) { + void this.show({ preserveFocus: preserveFocus }); + } else { + this._panel.reveal(this._panel.viewColumn ?? ViewColumn.Active, preserveFocus ?? false); + if (this._graph?.ids.has(sha)) { + void this.notifyDidChangeSelection(); + return; + } + + this.setSelectedRows(sha); + void this.onGetMoreCommits({ sha: sha }); + } + }, + ), ); this.onConfigurationChanged(); diff --git a/src/webviews/commitDetails/commitDetailsWebviewView.ts b/src/webviews/commitDetails/commitDetailsWebviewView.ts index a68b123..eccf405 100644 --- a/src/webviews/commitDetails/commitDetailsWebviewView.ts +++ b/src/webviews/commitDetails/commitDetailsWebviewView.ts @@ -18,8 +18,9 @@ import { serializeIssueOrPullRequest } from '../../git/models/issue'; import type { PullRequest } from '../../git/models/pullRequest'; import { serializePullRequest } from '../../git/models/pullRequest'; import type { GitRevisionReference } from '../../git/models/reference'; +import { GitReference } from '../../git/models/reference'; import { Logger } from '../../logger'; -import type { ShowCommitInGraphCommandArgs } from '../../plus/webviews/graph/graphWebview'; +import type { ShowInCommitGraphCommandArgs } from '../../plus/webviews/graph/graphWebview'; import { executeCommand } from '../../system/command'; import type { DateTimeFormat } from '../../system/date'; import { debug, getLogScope } from '../../system/decorators/log'; @@ -237,9 +238,8 @@ export class CommitDetailsWebviewView extends WebviewViewBase(Commands.ShowCommitInGraph, { - repoPath: this._context.commit.repoPath, - sha: this._context.commit.sha, + void executeCommand(Commands.ShowInCommitGraph, { + ref: GitReference.fromRevision(this._context.commit), }); break; case 'more':