Skip to content

Commit

Permalink
Fixes #2515 - Adds openFileByRevision command
Browse files Browse the repository at this point in the history
  • Loading branch information
mogelbrod committed Dec 16, 2023
1 parent f976187 commit d7ce5fe
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

- 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)
- 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_
- 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))

### Fixed

Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6054,6 +6054,12 @@
"icon": "$(gitlens-open-revision)",
"category": "GitLens"
},
{
"command": "gitlens.openFileByRevision",
"title": "Open File by Revision...",
"icon": "$(gitlens-open-revision)",
"category": "GitLens"
},
{
"command": "gitlens.openAutolinkUrl",
"title": "Open Autolink URL",
Expand Down
1 change: 1 addition & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import './commands/openFileFromRemote';
import './commands/openFileOnRemote';
import './commands/openFileAtRevision';
import './commands/openFileAtRevisionFrom';
import './commands/openFileByRevision';
import './commands/openOnRemote';
import './commands/openIssueOnRemote';
import './commands/openPullRequestOnRemote';
Expand Down
69 changes: 69 additions & 0 deletions src/commands/openFileByRevision.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import type { TextDocumentShowOptions, TextEditor, Uri } from 'vscode';
import type { FileAnnotationType } from '../config';
import { Commands } from '../constants';
import { Container } from '../container';
import { openFileAtRevision } from '../git/actions/commit';
import { GitUri } from '../git/gitUri';
import { showNoRepositoryWarningMessage } from '../messages';
import { showReferencePicker } from '../quickpicks/referencePicker';
import { showRevisionPicker } from '../quickpicks/revisionPicker';
import { command } from '../system/command';
import { ActiveEditorCommand, getCommandUri } from './base';

export interface OpenFileByRevisionCommandArgs {
revisionUri?: Uri;

line?: number;
showOptions?: TextDocumentShowOptions;
annotationType?: FileAnnotationType;
}

@command()
export class OpenFileByRevisionCommand extends ActiveEditorCommand {
constructor(private readonly container: Container) {
super([
// TODO: Do we want to support these command variations?
Commands.OpenFileByRevision /*, Commands.OpenFileByRevisionInDiffLeft, Commands.OpenFileByRevisionInDiffRight*/,
]);
}

async execute(editor?: TextEditor, uri?: Uri, args?: OpenFileByRevisionCommandArgs) {
uri = getCommandUri(uri, editor);
const gitUri = uri ? await GitUri.fromUri(uri) : undefined;
// TODO: Should we ask user to select repository if there are multiple in the workspace?
const repoPath = gitUri?.repoPath || this.container.git.getBestRepository()?.path

if (!repoPath) {
void showNoRepositoryWarningMessage('Unable to determine repository path');
return
}

args = {...args}

let revisionUri = args.revisionUri
if (revisionUri == null) {
const pick = await showReferencePicker(
repoPath,
`Select Branch or Tag to browse for File`,
'Choose a branch or tag',
// TODO: This option appears to have been removed?
// { allowEnteringRefs: true },
);
if (pick == null) return;
revisionUri = GitUri.fromRepoPath(repoPath, pick.ref)
}

const revisionGitUri = await GitUri.fromUri(revisionUri);
const file = await showRevisionPicker(Container.instance, revisionGitUri, {
title: 'Select File to open',
});

if (!file) return;

await openFileAtRevision(file, {
annotationType: args.annotationType,
line: args.line,
...args.showOptions,
});
}
}
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export const enum Commands {
OpenFileOnRemoteFrom = 'gitlens.openFileOnRemoteFrom',
OpenFileAtRevision = 'gitlens.openFileRevision',
OpenFileAtRevisionFrom = 'gitlens.openFileRevisionFrom',
OpenFileByRevision = 'gitlens.openFileByRevision',
OpenFolderHistory = 'gitlens.openFolderHistory',
OpenOnRemote = 'gitlens.openOnRemote',
OpenIssueOnRemote = 'gitlens.openIssueOnRemote',
Expand Down

0 comments on commit d7ce5fe

Please sign in to comment.