|
| 1 | +'use strict'; |
| 2 | +import { Range, TextDocumentShowOptions, TextEditor, Uri } from 'vscode'; |
| 3 | +import { GlyphChars } from '../constants'; |
| 4 | +import { GitBranch, GitTag, GitUri } from '../git/gitService'; |
| 5 | +import { BranchesAndTagsQuickPick, BranchQuickPickItem, TagQuickPickItem } from '../quickpicks'; |
| 6 | +import { Strings } from '../system'; |
| 7 | +import { ActiveEditorCommand, command, Commands, getCommandUri, openEditor } from './common'; |
| 8 | + |
| 9 | +export interface OpenFileRevisionFromBranchCommandArgs { |
| 10 | + branchOrTag?: GitBranch | GitTag; |
| 11 | + |
| 12 | + line?: number; |
| 13 | + showOptions?: TextDocumentShowOptions; |
| 14 | +} |
| 15 | + |
| 16 | +@command() |
| 17 | +export class OpenFileRevisionFromBranchCommand extends ActiveEditorCommand { |
| 18 | + constructor() { |
| 19 | + super(Commands.OpenFileRevisionFromBranch); |
| 20 | + } |
| 21 | + |
| 22 | + async execute(editor: TextEditor | undefined, uri?: Uri, args: OpenFileRevisionFromBranchCommandArgs = {}) { |
| 23 | + uri = getCommandUri(uri, editor); |
| 24 | + if (uri == null) return undefined; |
| 25 | + |
| 26 | + const gitUri = await GitUri.fromUri(uri); |
| 27 | + if (!gitUri.repoPath) return undefined; |
| 28 | + |
| 29 | + if (args.branchOrTag === undefined) { |
| 30 | + const placeHolder = `Open revision of ${gitUri.getFormattedPath()}${ |
| 31 | + gitUri.sha ? ` ${Strings.pad(GlyphChars.Dot, 1, 1)} ${gitUri.shortSha}` : '' |
| 32 | + } from Branch or Tag${GlyphChars.Ellipsis}`; |
| 33 | + |
| 34 | + const pick = await new BranchesAndTagsQuickPick(gitUri.repoPath).show(placeHolder, { |
| 35 | + allowCommitId: false |
| 36 | + }); |
| 37 | + if (pick === undefined) return undefined; |
| 38 | + |
| 39 | + if (!(pick instanceof BranchQuickPickItem) && !(pick instanceof TagQuickPickItem)) { |
| 40 | + return undefined; |
| 41 | + } |
| 42 | + |
| 43 | + args.branchOrTag = pick.item; |
| 44 | + } |
| 45 | + |
| 46 | + if (args.line !== undefined && args.line !== 0) { |
| 47 | + if (args.showOptions === undefined) { |
| 48 | + args.showOptions = {}; |
| 49 | + } |
| 50 | + args.showOptions.selection = new Range(args.line, 0, args.line, 0); |
| 51 | + } |
| 52 | + |
| 53 | + return openEditor(GitUri.toRevisionUri(args.branchOrTag.ref, gitUri.fsPath, gitUri.repoPath), { |
| 54 | + ...args.showOptions, |
| 55 | + rethrow: true |
| 56 | + }); |
| 57 | + } |
| 58 | +} |
0 commit comments