Skip to content

Commit d2d12e7

Browse files
committed
Closes #646 - adds open revision from branch command
1 parent a47e07b commit d2d12e7

File tree

6 files changed

+75
-3
lines changed

6 files changed

+75
-3
lines changed

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,6 +2113,11 @@
21132113
"title": "Open Revision...",
21142114
"category": "GitLens"
21152115
},
2116+
{
2117+
"command": "gitlens.openFileRevisionFromBranch",
2118+
"title": "Open Revision from Branch or Tag...",
2119+
"category": "GitLens"
2120+
},
21162121
{
21172122
"command": "gitlens.openRepoInRemote",
21182123
"title": "Open Repository on Remote",
@@ -3036,6 +3041,10 @@
30363041
"command": "gitlens.openFileRevision",
30373042
"when": "gitlens:activeFileStatus =~ /tracked/"
30383043
},
3044+
{
3045+
"command": "gitlens.openFileRevisionFromBranch",
3046+
"when": "gitlens:activeFileStatus =~ /tracked/"
3047+
},
30393048
{
30403049
"command": "gitlens.openRepoInRemote",
30413050
"when": "gitlens:hasRemotes"

src/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export * from './commands/openChangedFiles';
2323
export * from './commands/openCommitInRemote';
2424
export * from './commands/openFileInRemote';
2525
export * from './commands/openFileRevision';
26+
export * from './commands/openFileRevisionFromBranch';
2627
export * from './commands/openInRemote';
2728
export * from './commands/openRepoInRemote';
2829
export * from './commands/openWorkingFile';

src/commands/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export enum Commands {
5151
OpenCommitInRemote = 'gitlens.openCommitInRemote',
5252
OpenFileInRemote = 'gitlens.openFileInRemote',
5353
OpenFileRevision = 'gitlens.openFileRevision',
54+
OpenFileRevisionFromBranch = 'gitlens.openFileRevisionFromBranch',
5455
OpenInRemote = 'gitlens.openInRemote',
5556
OpenRepoInRemote = 'gitlens.openRepoInRemote',
5657
OpenWorkingFile = 'gitlens.openWorkingFile',

src/commands/openFileRevision.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class OpenFileRevisionCommand extends ActiveEditorCommand {
5151
super(Commands.OpenFileRevision);
5252
}
5353

54-
async execute(editor: TextEditor, uri?: Uri, args: OpenFileRevisionCommandArgs = {}) {
54+
async execute(editor: TextEditor | undefined, uri?: Uri, args: OpenFileRevisionCommandArgs = {}) {
5555
args = { ...args };
5656
if (args.line === undefined) {
5757
args.line = editor == null ? 0 : editor.selection.active.line;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
import { Range, TextDocumentShowOptions, TextEditor, Uri } from 'vscode';
3+
import { GlyphChars } from '../constants';
4+
import { GitBranch, GitTag, GitUri } from '../git/gitService';
5+
import { BranchesAndTagsQuickPick, BranchQuickPickItem, TagQuickPickItem } from '../quickpicks';
6+
import { Strings } from '../system';
7+
import { ActiveEditorCommand, command, Commands, getCommandUri, openEditor } from './common';
8+
9+
export interface OpenFileRevisionFromBranchCommandArgs {
10+
branchOrTag?: GitBranch | GitTag;
11+
12+
line?: number;
13+
showOptions?: TextDocumentShowOptions;
14+
}
15+
16+
@command()
17+
export class OpenFileRevisionFromBranchCommand extends ActiveEditorCommand {
18+
constructor() {
19+
super(Commands.OpenFileRevisionFromBranch);
20+
}
21+
22+
async execute(editor: TextEditor | undefined, uri?: Uri, args: OpenFileRevisionFromBranchCommandArgs = {}) {
23+
uri = getCommandUri(uri, editor);
24+
if (uri == null) return undefined;
25+
26+
const gitUri = await GitUri.fromUri(uri);
27+
if (!gitUri.repoPath) return undefined;
28+
29+
if (args.branchOrTag === undefined) {
30+
const placeHolder = `Open revision of ${gitUri.getFormattedPath()}${
31+
gitUri.sha ? ` ${Strings.pad(GlyphChars.Dot, 1, 1)} ${gitUri.shortSha}` : ''
32+
} from Branch or Tag${GlyphChars.Ellipsis}`;
33+
34+
const pick = await new BranchesAndTagsQuickPick(gitUri.repoPath).show(placeHolder, {
35+
allowCommitId: false
36+
});
37+
if (pick === undefined) return undefined;
38+
39+
if (!(pick instanceof BranchQuickPickItem) && !(pick instanceof TagQuickPickItem)) {
40+
return undefined;
41+
}
42+
43+
args.branchOrTag = pick.item;
44+
}
45+
46+
if (args.line !== undefined && args.line !== 0) {
47+
if (args.showOptions === undefined) {
48+
args.showOptions = {};
49+
}
50+
args.showOptions.selection = new Range(args.line, 0, args.line, 0);
51+
}
52+
53+
return openEditor(GitUri.toRevisionUri(args.branchOrTag.ref, gitUri.fsPath, gitUri.repoPath), {
54+
...args.showOptions,
55+
rethrow: true
56+
});
57+
}
58+
}

src/quickpicks/branchesAndTagsQuickPick.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export interface BranchesAndTagsQuickPickOptions {
115115

116116
export class BranchesAndTagsQuickPick {
117117
constructor(
118-
public readonly repoPath: string
118+
public readonly repoPath: string | undefined
119119
) {}
120120

121121
async show(
@@ -177,7 +177,10 @@ export class BranchesAndTagsQuickPick {
177177
quickpick.busy = true;
178178
quickpick.enabled = false;
179179

180-
if (await Container.git.validateReference(this.repoPath, ref)) {
180+
if (
181+
this.repoPath === undefined ||
182+
(await Container.git.validateReference(this.repoPath, ref))
183+
) {
181184
resolve(new RefQuickPickItem(ref));
182185
}
183186
else {

0 commit comments

Comments
 (0)