Skip to content

Commit 8cb86b8

Browse files
committed
Fixes gitkraken#2515 - Adds openFileByRevision command
1 parent 66ee212 commit 8cb86b8

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
88

99
### Added
1010

11-
- Adds support for opening renamed/deleted files using the _Open File at Revision..._ commands by showing a quickpicker prompt if the requested file doesn't exist in the selected revision — thanks to [PR #2825](https://github.com/gitkraken/vscode-gitlens/pull/2825) by Victor Hallberg ([@mogelbrod](https://github.com/mogelbrod))
11+
- Improve support for opening files from other branches — thanks to [PR #2825](https://github.com/gitkraken/vscode-gitlens/pull/2825) by Victor Hallberg ([@mogelbrod](https://github.com/mogelbrod))
12+
- Open any file using new _Open File by Revision..._ command
13+
- Improve _Open File at Revision..._ commands by prompting user for file to open when the requested path doesn't exist in the specified revision
1214

1315
### Changed
1416

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5416,6 +5416,12 @@
54165416
"icon": "$(gitlens-open-revision)",
54175417
"category": "GitLens"
54185418
},
5419+
{
5420+
"command": "gitlens.openFileByRevision",
5421+
"title": "Open File by Revision...",
5422+
"icon": "$(gitlens-open-revision)",
5423+
"category": "GitLens"
5424+
},
54195425
{
54205426
"command": "gitlens.openAutolinkUrl",
54215427
"title": "Open Autolink URL",

src/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export * from './commands/openFileFromRemote';
3434
export * from './commands/openFileOnRemote';
3535
export * from './commands/openFileAtRevision';
3636
export * from './commands/openFileAtRevisionFrom';
37+
export * from './commands/openFileByRevision';
3738
export * from './commands/openOnRemote';
3839
export * from './commands/openIssueOnRemote';
3940
export * from './commands/openPullRequestOnRemote';

src/commands/openFileByRevision.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import type { TextDocumentShowOptions, TextEditor, Uri } from 'vscode';
2+
import type { FileAnnotationType } from '../config';
3+
import { Commands } from '../constants';
4+
import type { Container } from '../container';
5+
import { openFileAtRevision, pickFileAtRevision } from '../git/actions/commit';
6+
import { GitUri } from '../git/gitUri';
7+
import { showNoRepositoryWarningMessage } from '../messages';
8+
import { showReferencePicker } from '../quickpicks/referencePicker';
9+
import { command } from '../system/command';
10+
import { ActiveEditorCommand, getCommandUri } from './base';
11+
12+
export interface OpenFileByRevisionCommandArgs {
13+
revisionUri?: Uri;
14+
15+
line?: number;
16+
showOptions?: TextDocumentShowOptions;
17+
annotationType?: FileAnnotationType;
18+
}
19+
20+
@command()
21+
export class OpenFileByRevisionCommand extends ActiveEditorCommand {
22+
constructor(private readonly container: Container) {
23+
super([
24+
Commands.OpenFileByRevision /*, Commands.OpenFileByRevisionInDiffLeft, Commands.OpenFileByRevisionInDiffRight*/,
25+
]);
26+
}
27+
28+
async execute(editor?: TextEditor, uri?: Uri, args?: OpenFileByRevisionCommandArgs) {
29+
uri = getCommandUri(uri, editor);
30+
if (uri == null) return;
31+
32+
const gitUri = await GitUri.fromUri(uri);
33+
if (!gitUri.repoPath) {
34+
void showNoRepositoryWarningMessage('Unable to determine repository path');
35+
return;
36+
}
37+
38+
args = { ...args };
39+
40+
if (args.revisionUri == null) {
41+
let resolveKeyboardPickPromise: (reference: Uri) => void;
42+
const keyboardPickPromise = new Promise<Uri>(resolve => { resolveKeyboardPickPromise = resolve; });
43+
const referencePickPromise = showReferencePicker(
44+
gitUri.repoPath,
45+
`Select Branch or Tag to browse for File`,
46+
'Choose a branch or tag',
47+
{
48+
allowEnteringRefs: true,
49+
keys: ['right', 'alt+right', 'ctrl+right'],
50+
onDidPressKey: (key, quickpick) => {
51+
const [item] = quickpick.activeItems;
52+
if (item != null) {
53+
const refUri = this.container.git.getRevisionUri(item.ref, gitUri.fsPath, gitUri.repoPath!);
54+
resolveKeyboardPickPromise(refUri);
55+
}
56+
},
57+
},
58+
).then(commit => {
59+
return commit ? new GitUri(gitUri, commit) : undefined;
60+
});
61+
const revision = await Promise.race([keyboardPickPromise, referencePickPromise]);
62+
if (revision == null) return;
63+
args.revisionUri = revision;
64+
}
65+
66+
const revUri = await GitUri.fromUri(args.revisionUri);
67+
const file = await pickFileAtRevision(revUri, {
68+
title: 'Select File to open',
69+
initialPath: gitUri.relativePath,
70+
});
71+
72+
if (!file) return;
73+
74+
await openFileAtRevision(file, {
75+
annotationType: args.annotationType,
76+
line: args.line,
77+
...args.showOptions,
78+
});
79+
}
80+
}

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ export const enum Commands {
178178
OpenFileOnRemoteFrom = 'gitlens.openFileOnRemoteFrom',
179179
OpenFileAtRevision = 'gitlens.openFileRevision',
180180
OpenFileAtRevisionFrom = 'gitlens.openFileRevisionFrom',
181+
OpenFileByRevision = 'gitlens.openFileByRevision',
181182
OpenFolderHistory = 'gitlens.openFolderHistory',
182183
OpenOnRemote = 'gitlens.openOnRemote',
183184
OpenIssueOnRemote = 'gitlens.openIssueOnRemote',

0 commit comments

Comments
 (0)