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.

127 lines
4.8 KiB

  1. 'use strict';
  2. import { commands, TextEditor, Uri, window } from 'vscode';
  3. import { GlyphChars } from '../constants';
  4. import { Container } from '../container';
  5. import { GitLog, GitUri } from '../git/gitService';
  6. import { Logger } from '../logger';
  7. import { Messages } from '../messages';
  8. import { BranchHistoryQuickPick, CommandQuickPickItem, ReferencesQuickPick } from '../quickpicks';
  9. import { ActiveEditorCachedCommand, command, Commands, getCommandUri, getRepoPathOrActiveOrPrompt } from './common';
  10. import { ShowQuickCommitDetailsCommandArgs } from './showQuickCommitDetails';
  11. export interface ShowQuickBranchHistoryCommandArgs {
  12. branch?: string;
  13. log?: GitLog;
  14. maxCount?: number;
  15. repoPath?: string;
  16. goBackCommand?: CommandQuickPickItem;
  17. nextPageCommand?: CommandQuickPickItem;
  18. }
  19. @command()
  20. export class ShowQuickBranchHistoryCommand extends ActiveEditorCachedCommand {
  21. constructor() {
  22. super(Commands.ShowQuickBranchHistory);
  23. }
  24. async execute(editor?: TextEditor, uri?: Uri, args: ShowQuickBranchHistoryCommandArgs = {}) {
  25. uri = getCommandUri(uri, editor);
  26. const gitUri = uri && (await GitUri.fromUri(uri));
  27. args = { ...args };
  28. let progressCancellation =
  29. args.branch === undefined ? undefined : BranchHistoryQuickPick.showProgress(args.branch);
  30. try {
  31. const repoPath =
  32. args.repoPath ||
  33. (await getRepoPathOrActiveOrPrompt(
  34. gitUri,
  35. editor,
  36. `Show branch history in which repository${GlyphChars.Ellipsis}`
  37. ));
  38. if (!repoPath) return undefined;
  39. if (args.branch === undefined) {
  40. let goBackCommand;
  41. if (!(await Container.git.getRepoPathOrActive(uri, editor))) {
  42. goBackCommand = new CommandQuickPickItem(
  43. {
  44. label: `go back ${GlyphChars.ArrowBack}`,
  45. description: 'to which repository'
  46. },
  47. Commands.ShowQuickBranchHistory,
  48. [uri, args]
  49. );
  50. }
  51. const pick = await new ReferencesQuickPick(repoPath).show(
  52. `Show history for branch${GlyphChars.Ellipsis}`,
  53. {
  54. checkmarks: false,
  55. goBack: goBackCommand,
  56. include: 'branches'
  57. }
  58. );
  59. if (pick === undefined) return undefined;
  60. if (pick instanceof CommandQuickPickItem) return pick.execute();
  61. args.branch = pick.ref;
  62. if (args.branch === undefined) return undefined;
  63. progressCancellation = BranchHistoryQuickPick.showProgress(args.branch);
  64. }
  65. if (args.log === undefined) {
  66. args.log = await Container.git.getLog(repoPath, {
  67. maxCount: args.maxCount,
  68. ref: (gitUri && gitUri.sha) || args.branch
  69. });
  70. if (args.log === undefined) return window.showWarningMessage('Unable to show branch history');
  71. }
  72. if (progressCancellation !== undefined && progressCancellation.token.isCancellationRequested) {
  73. return undefined;
  74. }
  75. const pick = await BranchHistoryQuickPick.show(
  76. args.log,
  77. gitUri,
  78. args.branch,
  79. progressCancellation!,
  80. args.goBackCommand,
  81. args.nextPageCommand
  82. );
  83. if (pick === undefined) return undefined;
  84. if (pick instanceof CommandQuickPickItem) return pick.execute();
  85. // Create a command to get back to here
  86. const currentCommand = new CommandQuickPickItem(
  87. {
  88. label: `go back ${GlyphChars.ArrowBack}`,
  89. description: `to history of ${GlyphChars.Space}$(git-branch) ${args.branch}`
  90. },
  91. Commands.ShowQuickBranchHistory,
  92. [uri, { ...args }]
  93. );
  94. const commandArgs: ShowQuickCommitDetailsCommandArgs = {
  95. sha: pick.item.sha,
  96. commit: pick.item,
  97. repoLog: args.log,
  98. goBackCommand: currentCommand
  99. };
  100. return commands.executeCommand(Commands.ShowQuickCommitDetails, pick.item.toGitUri(), commandArgs);
  101. }
  102. catch (ex) {
  103. Logger.error(ex, 'ShowQuickBranchHistoryCommand');
  104. return Messages.showGenericErrorMessage('Unable to show branch history');
  105. }
  106. finally {
  107. progressCancellation && progressCancellation.cancel();
  108. }
  109. }
  110. }