Skip to content

Commit 249c93a

Browse files
committed
Fixes #2515 - Adds openFileByRevision command
1 parent 4e7c81e commit 249c93a

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
1010

1111
- Adds support for OpenAI's GPT-4 Turbo and latest Anthropic models for GitLens' experimental AI features — closes [#3005](https://github.com/gitkraken/vscode-gitlens/issues/3005)
1212
- Adds support for opening renamed/deleted files using the _Open File at Revision..._ commands by showing a quickpick 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))
13+
- 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))
14+
- Open any file using new _Open File by Revision..._ command
15+
- Improve _Open File at Revision..._ commands by prompting user for file to open when the requested path doesn't exist in the specified revision
1316

1417
### Changed
1518

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5962,6 +5962,12 @@
59625962
"icon": "$(gitlens-open-revision)",
59635963
"category": "GitLens"
59645964
},
5965+
{
5966+
"command": "gitlens.openFileByRevision",
5967+
"title": "Open File by Revision...",
5968+
"icon": "$(gitlens-open-revision)",
5969+
"category": "GitLens"
5970+
},
59655971
{
59665972
"command": "gitlens.openAutolinkUrl",
59675973
"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 @@ import './commands/openFileFromRemote';
3434
import './commands/openFileOnRemote';
3535
import './commands/openFileAtRevision';
3636
import './commands/openFileAtRevisionFrom';
37+
import './commands/openFileByRevision';
3738
import './commands/openOnRemote';
3839
import './commands/openIssueOnRemote';
3940
import './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
@@ -184,6 +184,7 @@ export const enum Commands {
184184
OpenFileOnRemoteFrom = 'gitlens.openFileOnRemoteFrom',
185185
OpenFileAtRevision = 'gitlens.openFileRevision',
186186
OpenFileAtRevisionFrom = 'gitlens.openFileRevisionFrom',
187+
OpenFileByRevision = 'gitlens.openFileByRevision',
187188
OpenFolderHistory = 'gitlens.openFolderHistory',
188189
OpenOnRemote = 'gitlens.openOnRemote',
189190
OpenIssueOnRemote = 'gitlens.openIssueOnRemote',

0 commit comments

Comments
 (0)