Skip to content

Commit

Permalink
Closes #646 - adds open revision from branch command
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Feb 14, 2019
1 parent a47e07b commit d2d12e7
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 3 deletions.
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2113,6 +2113,11 @@
"title": "Open Revision...",
"category": "GitLens"
},
{
"command": "gitlens.openFileRevisionFromBranch",
"title": "Open Revision from Branch or Tag...",
"category": "GitLens"
},
{
"command": "gitlens.openRepoInRemote",
"title": "Open Repository on Remote",
Expand Down Expand Up @@ -3036,6 +3041,10 @@
"command": "gitlens.openFileRevision",
"when": "gitlens:activeFileStatus =~ /tracked/"
},
{
"command": "gitlens.openFileRevisionFromBranch",
"when": "gitlens:activeFileStatus =~ /tracked/"
},
{
"command": "gitlens.openRepoInRemote",
"when": "gitlens:hasRemotes"
Expand Down
1 change: 1 addition & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
1 change: 1 addition & 0 deletions src/commands/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/commands/openFileRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
58 changes: 58 additions & 0 deletions src/commands/openFileRevisionFromBranch.ts
Original file line number Diff line number Diff line change
@@ -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
});
}
}
7 changes: 5 additions & 2 deletions src/quickpicks/branchesAndTagsQuickPick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export interface BranchesAndTagsQuickPickOptions {

export class BranchesAndTagsQuickPick {
constructor(
public readonly repoPath: string
public readonly repoPath: string | undefined
) {}

async show(
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit d2d12e7

Please sign in to comment.