Skip to content

Commit

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

## [Unreleased]

### Added
- 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/TODO) by Victor Hallberg ([@mogelbrod](https://github.com/mogelbrod))

### Fixed

- Fixes [#2482](https://github.com/gitkraken/vscode-gitlens/issues/2482) - Unresponsive "commits" view and "branches" view update due to git log
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5985,6 +5985,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 @@ -34,6 +34,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
80 changes: 80 additions & 0 deletions src/commands/openFileByRevision.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import type { TextDocumentShowOptions, TextEditor, Uri } from 'vscode';
import type { FileAnnotationType } from '../config';
import { Commands } from '../constants';
import type { Container } from '../container';
import { openFileAtRevision, pickFileAtRevision } from '../git/actions/commit';
import { GitUri } from '../git/gitUri';
import { showNoRepositoryWarningMessage } from '../messages';
import { showReferencePicker } from '../quickpicks/referencePicker';
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([
Commands.OpenFileByRevision /*, Commands.OpenFileByRevisionInDiffLeft, Commands.OpenFileByRevisionInDiffRight*/,
]);
}

async execute(editor?: TextEditor, uri?: Uri, args?: OpenFileByRevisionCommandArgs) {
uri = getCommandUri(uri, editor);
if (uri == null) return;

const gitUri = await GitUri.fromUri(uri);
if (!gitUri.repoPath) {
void showNoRepositoryWarningMessage('Unable to determine repository path');
return;
}

args = { ...args };

if (args.revisionUri == null) {
let resolveKeyboardPickPromise: (reference: Uri) => void;
const keyboardPickPromise = new Promise<Uri>(resolve => { resolveKeyboardPickPromise = resolve; });
const referencePickPromise = showReferencePicker(
gitUri.repoPath,
`Select Branch or Tag to browse for File`,
'Choose a branch or tag',
{
allowEnteringRefs: true,
keys: ['right', 'alt+right', 'ctrl+right'],
onDidPressKey: (key, quickpick) => {
const [item] = quickpick.activeItems;
if (item != null) {
const refUri = this.container.git.getRevisionUri(item.ref, gitUri.fsPath, gitUri.repoPath!);
resolveKeyboardPickPromise(refUri);
}
},
},
).then(commit => {
return commit ? new GitUri(gitUri, commit) : undefined;
});
const revision = await Promise.race([keyboardPickPromise, referencePickPromise]);
if (revision == null) return;
args.revisionUri = revision;
}

const revUri = await GitUri.fromUri(args.revisionUri);
const file = await pickFileAtRevision(revUri, {
title: 'Select File to open',
initialPath: gitUri.relativePath,
});

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 @@ -186,6 +186,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 b26d79b

Please sign in to comment.