From d2d12e7c27340a6e0e9bef5cfc4b1cc81de3f7b0 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Wed, 13 Feb 2019 21:02:03 -0800 Subject: [PATCH] Closes #646 - adds open revision from branch command --- package.json | 9 +++++ src/commands.ts | 1 + src/commands/common.ts | 1 + src/commands/openFileRevision.ts | 2 +- src/commands/openFileRevisionFromBranch.ts | 58 ++++++++++++++++++++++++++++++ src/quickpicks/branchesAndTagsQuickPick.ts | 7 ++-- 6 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 src/commands/openFileRevisionFromBranch.ts diff --git a/package.json b/package.json index 6e7cdc2..6ef0692 100644 --- a/package.json +++ b/package.json @@ -2114,6 +2114,11 @@ "category": "GitLens" }, { + "command": "gitlens.openFileRevisionFromBranch", + "title": "Open Revision from Branch or Tag...", + "category": "GitLens" + }, + { "command": "gitlens.openRepoInRemote", "title": "Open Repository on Remote", "category": "GitLens", @@ -3037,6 +3042,10 @@ "when": "gitlens:activeFileStatus =~ /tracked/" }, { + "command": "gitlens.openFileRevisionFromBranch", + "when": "gitlens:activeFileStatus =~ /tracked/" + }, + { "command": "gitlens.openRepoInRemote", "when": "gitlens:hasRemotes" }, diff --git a/src/commands.ts b/src/commands.ts index 5e4a6a7..fb17da0 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -23,6 +23,7 @@ export * from './commands/openChangedFiles'; export * from './commands/openCommitInRemote'; export * from './commands/openFileInRemote'; export * from './commands/openFileRevision'; +export * from './commands/openFileRevisionFromBranch'; export * from './commands/openInRemote'; export * from './commands/openRepoInRemote'; export * from './commands/openWorkingFile'; diff --git a/src/commands/common.ts b/src/commands/common.ts index 7e969bf..f3d523e 100644 --- a/src/commands/common.ts +++ b/src/commands/common.ts @@ -51,6 +51,7 @@ export enum Commands { OpenCommitInRemote = 'gitlens.openCommitInRemote', OpenFileInRemote = 'gitlens.openFileInRemote', OpenFileRevision = 'gitlens.openFileRevision', + OpenFileRevisionFromBranch = 'gitlens.openFileRevisionFromBranch', OpenInRemote = 'gitlens.openInRemote', OpenRepoInRemote = 'gitlens.openRepoInRemote', OpenWorkingFile = 'gitlens.openWorkingFile', diff --git a/src/commands/openFileRevision.ts b/src/commands/openFileRevision.ts index 2e22345..605415e 100644 --- a/src/commands/openFileRevision.ts +++ b/src/commands/openFileRevision.ts @@ -51,7 +51,7 @@ export class OpenFileRevisionCommand extends ActiveEditorCommand { super(Commands.OpenFileRevision); } - async execute(editor: TextEditor, uri?: Uri, args: OpenFileRevisionCommandArgs = {}) { + async execute(editor: TextEditor | undefined, uri?: Uri, args: OpenFileRevisionCommandArgs = {}) { args = { ...args }; if (args.line === undefined) { args.line = editor == null ? 0 : editor.selection.active.line; diff --git a/src/commands/openFileRevisionFromBranch.ts b/src/commands/openFileRevisionFromBranch.ts new file mode 100644 index 0000000..b310a6e --- /dev/null +++ b/src/commands/openFileRevisionFromBranch.ts @@ -0,0 +1,58 @@ +'use strict'; +import { Range, TextDocumentShowOptions, TextEditor, Uri } from 'vscode'; +import { GlyphChars } from '../constants'; +import { GitBranch, GitTag, GitUri } from '../git/gitService'; +import { BranchesAndTagsQuickPick, BranchQuickPickItem, TagQuickPickItem } from '../quickpicks'; +import { Strings } from '../system'; +import { ActiveEditorCommand, command, Commands, getCommandUri, openEditor } from './common'; + +export interface OpenFileRevisionFromBranchCommandArgs { + branchOrTag?: GitBranch | GitTag; + + line?: number; + showOptions?: TextDocumentShowOptions; +} + +@command() +export class OpenFileRevisionFromBranchCommand extends ActiveEditorCommand { + constructor() { + super(Commands.OpenFileRevisionFromBranch); + } + + async execute(editor: TextEditor | undefined, uri?: Uri, args: OpenFileRevisionFromBranchCommandArgs = {}) { + uri = getCommandUri(uri, editor); + if (uri == null) return undefined; + + const gitUri = await GitUri.fromUri(uri); + if (!gitUri.repoPath) return undefined; + + if (args.branchOrTag === undefined) { + const placeHolder = `Open revision of ${gitUri.getFormattedPath()}${ + gitUri.sha ? ` ${Strings.pad(GlyphChars.Dot, 1, 1)} ${gitUri.shortSha}` : '' + } from Branch or Tag${GlyphChars.Ellipsis}`; + + const pick = await new BranchesAndTagsQuickPick(gitUri.repoPath).show(placeHolder, { + allowCommitId: false + }); + if (pick === undefined) return undefined; + + if (!(pick instanceof BranchQuickPickItem) && !(pick instanceof TagQuickPickItem)) { + return undefined; + } + + args.branchOrTag = pick.item; + } + + if (args.line !== undefined && args.line !== 0) { + if (args.showOptions === undefined) { + args.showOptions = {}; + } + args.showOptions.selection = new Range(args.line, 0, args.line, 0); + } + + return openEditor(GitUri.toRevisionUri(args.branchOrTag.ref, gitUri.fsPath, gitUri.repoPath), { + ...args.showOptions, + rethrow: true + }); + } +} diff --git a/src/quickpicks/branchesAndTagsQuickPick.ts b/src/quickpicks/branchesAndTagsQuickPick.ts index 263e232..a8a289f 100644 --- a/src/quickpicks/branchesAndTagsQuickPick.ts +++ b/src/quickpicks/branchesAndTagsQuickPick.ts @@ -115,7 +115,7 @@ export interface BranchesAndTagsQuickPickOptions { export class BranchesAndTagsQuickPick { constructor( - public readonly repoPath: string + public readonly repoPath: string | undefined ) {} async show( @@ -177,7 +177,10 @@ export class BranchesAndTagsQuickPick { quickpick.busy = true; quickpick.enabled = false; - if (await Container.git.validateReference(this.repoPath, ref)) { + if ( + this.repoPath === undefined || + (await Container.git.validateReference(this.repoPath, ref)) + ) { resolve(new RefQuickPickItem(ref)); } else {