Skip to content

Commit d7ce5fe

Browse files
committed
Fixes #2515 - Adds openFileByRevision command
1 parent f976187 commit d7ce5fe

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
1010

1111
- Adds a `gitlens.fileAnnotations.dismissOnEscape` setting to specify whether pressing the `ESC` key dismisses the active file annotations — closes [#3016](https://github.com/gitkraken/vscode-gitlens/issues/3016)
1212
- Adds _Copy_ to search results in the _Search & Compare_ view to copy the search query to more easily share or paste queries into the _Commit Graph_
13+
- 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 #TODO](https://github.com/gitkraken/vscode-gitlens/pull/3036) by Victor Hallberg ([@mogelbrod](https://github.com/mogelbrod))
1314

1415
### Fixed
1516

package.json

+6
Original file line numberDiff line numberDiff line change
@@ -6054,6 +6054,12 @@
60546054
"icon": "$(gitlens-open-revision)",
60556055
"category": "GitLens"
60566056
},
6057+
{
6058+
"command": "gitlens.openFileByRevision",
6059+
"title": "Open File by Revision...",
6060+
"icon": "$(gitlens-open-revision)",
6061+
"category": "GitLens"
6062+
},
60576063
{
60586064
"command": "gitlens.openAutolinkUrl",
60596065
"title": "Open Autolink URL",

src/commands.ts

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import './commands/openFileFromRemote';
3636
import './commands/openFileOnRemote';
3737
import './commands/openFileAtRevision';
3838
import './commands/openFileAtRevisionFrom';
39+
import './commands/openFileByRevision';
3940
import './commands/openOnRemote';
4041
import './commands/openIssueOnRemote';
4142
import './commands/openPullRequestOnRemote';

src/commands/openFileByRevision.ts

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import type { TextDocumentShowOptions, TextEditor, Uri } from 'vscode';
2+
import type { FileAnnotationType } from '../config';
3+
import { Commands } from '../constants';
4+
import { Container } from '../container';
5+
import { openFileAtRevision } from '../git/actions/commit';
6+
import { GitUri } from '../git/gitUri';
7+
import { showNoRepositoryWarningMessage } from '../messages';
8+
import { showReferencePicker } from '../quickpicks/referencePicker';
9+
import { showRevisionPicker } from '../quickpicks/revisionPicker';
10+
import { command } from '../system/command';
11+
import { ActiveEditorCommand, getCommandUri } from './base';
12+
13+
export interface OpenFileByRevisionCommandArgs {
14+
revisionUri?: Uri;
15+
16+
line?: number;
17+
showOptions?: TextDocumentShowOptions;
18+
annotationType?: FileAnnotationType;
19+
}
20+
21+
@command()
22+
export class OpenFileByRevisionCommand extends ActiveEditorCommand {
23+
constructor(private readonly container: Container) {
24+
super([
25+
// TODO: Do we want to support these command variations?
26+
Commands.OpenFileByRevision /*, Commands.OpenFileByRevisionInDiffLeft, Commands.OpenFileByRevisionInDiffRight*/,
27+
]);
28+
}
29+
30+
async execute(editor?: TextEditor, uri?: Uri, args?: OpenFileByRevisionCommandArgs) {
31+
uri = getCommandUri(uri, editor);
32+
const gitUri = uri ? await GitUri.fromUri(uri) : undefined;
33+
// TODO: Should we ask user to select repository if there are multiple in the workspace?
34+
const repoPath = gitUri?.repoPath || this.container.git.getBestRepository()?.path
35+
36+
if (!repoPath) {
37+
void showNoRepositoryWarningMessage('Unable to determine repository path');
38+
return
39+
}
40+
41+
args = {...args}
42+
43+
let revisionUri = args.revisionUri
44+
if (revisionUri == null) {
45+
const pick = await showReferencePicker(
46+
repoPath,
47+
`Select Branch or Tag to browse for File`,
48+
'Choose a branch or tag',
49+
// TODO: This option appears to have been removed?
50+
// { allowEnteringRefs: true },
51+
);
52+
if (pick == null) return;
53+
revisionUri = GitUri.fromRepoPath(repoPath, pick.ref)
54+
}
55+
56+
const revisionGitUri = await GitUri.fromUri(revisionUri);
57+
const file = await showRevisionPicker(Container.instance, revisionGitUri, {
58+
title: 'Select File to open',
59+
});
60+
61+
if (!file) return;
62+
63+
await openFileAtRevision(file, {
64+
annotationType: args.annotationType,
65+
line: args.line,
66+
...args.showOptions,
67+
});
68+
}
69+
}

src/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ export const enum Commands {
189189
OpenFileOnRemoteFrom = 'gitlens.openFileOnRemoteFrom',
190190
OpenFileAtRevision = 'gitlens.openFileRevision',
191191
OpenFileAtRevisionFrom = 'gitlens.openFileRevisionFrom',
192+
OpenFileByRevision = 'gitlens.openFileByRevision',
192193
OpenFolderHistory = 'gitlens.openFolderHistory',
193194
OpenOnRemote = 'gitlens.openOnRemote',
194195
OpenIssueOnRemote = 'gitlens.openIssueOnRemote',

0 commit comments

Comments
 (0)