|
| 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 | +} |
0 commit comments