You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.5 KiB

  1. 'use strict';
  2. import { commands, TextEditor, Uri, window } from 'vscode';
  3. import { GlyphChars } from '../constants';
  4. import { Container } from '../container';
  5. import { GitUri, RemoteResourceType } from '../git/gitService';
  6. import { Logger } from '../logger';
  7. import { CommandQuickPickItem, ReferencesQuickPick, ReferencesQuickPickIncludes } from '../quickpicks';
  8. import {
  9. ActiveEditorCommand,
  10. command,
  11. CommandContext,
  12. Commands,
  13. getCommandUri,
  14. getRepoPathOrActiveOrPrompt,
  15. isCommandViewContextWithBranch
  16. } from './common';
  17. import { OpenInRemoteCommandArgs } from './openInRemote';
  18. export interface OpenBranchInRemoteCommandArgs {
  19. branch?: string;
  20. remote?: string;
  21. }
  22. @command()
  23. export class OpenBranchInRemoteCommand extends ActiveEditorCommand {
  24. constructor() {
  25. super(Commands.OpenBranchInRemote);
  26. }
  27. protected preExecute(context: CommandContext, args?: OpenBranchInRemoteCommandArgs) {
  28. if (isCommandViewContextWithBranch(context)) {
  29. args = { ...args };
  30. args.branch = context.node.branch.name;
  31. args.remote = context.node.branch.getRemoteName();
  32. }
  33. return this.execute(context.editor, context.uri, args);
  34. }
  35. async execute(editor?: TextEditor, uri?: Uri, args?: OpenBranchInRemoteCommandArgs) {
  36. uri = getCommandUri(uri, editor);
  37. const gitUri = uri && (await GitUri.fromUri(uri));
  38. const repoPath = await getRepoPathOrActiveOrPrompt(
  39. gitUri,
  40. editor,
  41. `Open branch on remote for which repository${GlyphChars.Ellipsis}`
  42. );
  43. if (!repoPath) return undefined;
  44. args = { ...args };
  45. try {
  46. if (args.branch === undefined) {
  47. const pick = await new ReferencesQuickPick(repoPath).show(
  48. `Open which branch on remote${GlyphChars.Ellipsis}`,
  49. {
  50. autoPick: true,
  51. checkmarks: false,
  52. filterBranches: b => b.tracking !== undefined,
  53. include: ReferencesQuickPickIncludes.Branches
  54. }
  55. );
  56. if (pick === undefined || pick instanceof CommandQuickPickItem) return undefined;
  57. args.branch = pick.ref;
  58. }
  59. const remotes = await Container.git.getRemotes(repoPath);
  60. const commandArgs: OpenInRemoteCommandArgs = {
  61. resource: {
  62. type: RemoteResourceType.Branch,
  63. branch: args.branch || 'HEAD'
  64. },
  65. remote: args.remote,
  66. remotes: remotes
  67. };
  68. return commands.executeCommand(Commands.OpenInRemote, uri, commandArgs);
  69. } catch (ex) {
  70. Logger.error(ex, 'OpenBranchInRemoteCommandArgs');
  71. return window.showErrorMessage(
  72. 'Unable to open branch on remote provider. See output channel for more details'
  73. );
  74. }
  75. }
  76. }