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.

126 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 { BranchesQuickPick, BranchHistoryQuickPick, CommandQuickPickItem } from '../quickpicks';
  9. import { Strings } from '../system';
  10. import { ActiveEditorCachedCommand, Commands, getCommandUri, getRepoPathOrActiveOrPrompt } from './common';
  11. import { ShowQuickCommitDetailsCommandArgs } from './showQuickCommitDetails';
  12. export interface ShowQuickBranchHistoryCommandArgs {
  13. branch?: string;
  14. log?: GitLog;
  15. maxCount?: number;
  16. repoPath?: string;
  17. goBackCommand?: CommandQuickPickItem;
  18. nextPageCommand?: CommandQuickPickItem;
  19. }
  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. const branches = await Container.git.getBranches(repoPath);
  41. let goBackCommand;
  42. if (!(await Container.git.getRepoPathOrActive(uri, editor))) {
  43. goBackCommand = new CommandQuickPickItem(
  44. {
  45. label: `go back ${GlyphChars.ArrowBack}`,
  46. description: `${Strings.pad(GlyphChars.Dash, 2, 3)} to which repository`
  47. },
  48. Commands.ShowQuickBranchHistory,
  49. [uri, args]
  50. );
  51. }
  52. const pick = await BranchesQuickPick.show(branches, `Show history for branch${GlyphChars.Ellipsis}`, {
  53. goBackCommand: goBackCommand
  54. });
  55. if (pick === undefined) return undefined;
  56. if (pick instanceof CommandQuickPickItem) return pick.execute();
  57. args.branch = pick.branch.name;
  58. if (args.branch === undefined) return undefined;
  59. progressCancellation = BranchHistoryQuickPick.showProgress(args.branch);
  60. }
  61. if (args.log === undefined) {
  62. args.log = await Container.git.getLog(repoPath, {
  63. maxCount: args.maxCount,
  64. ref: (gitUri && gitUri.sha) || args.branch
  65. });
  66. if (args.log === undefined) return window.showWarningMessage(`Unable to show branch history`);
  67. }
  68. if (progressCancellation !== undefined && progressCancellation.token.isCancellationRequested) {
  69. return undefined;
  70. }
  71. const pick = await BranchHistoryQuickPick.show(
  72. args.log,
  73. gitUri,
  74. args.branch,
  75. progressCancellation!,
  76. args.goBackCommand,
  77. args.nextPageCommand
  78. );
  79. if (pick === undefined) return undefined;
  80. if (pick instanceof CommandQuickPickItem) return pick.execute();
  81. // Create a command to get back to here
  82. const currentCommand = new CommandQuickPickItem(
  83. {
  84. label: `go back ${GlyphChars.ArrowBack}`,
  85. description: `${Strings.pad(GlyphChars.Dash, 2, 3)} to ${GlyphChars.Space}$(git-branch) ${
  86. args.branch
  87. } history`
  88. },
  89. Commands.ShowQuickBranchHistory,
  90. [uri, { ...args }]
  91. );
  92. return commands.executeCommand(Commands.ShowQuickCommitDetails, pick.commit.toGitUri(), {
  93. sha: pick.commit.sha,
  94. commit: pick.commit,
  95. repoLog: args.log,
  96. goBackCommand: currentCommand
  97. } as ShowQuickCommitDetailsCommandArgs);
  98. }
  99. catch (ex) {
  100. Logger.error(ex, 'ShowQuickBranchHistoryCommand');
  101. return Messages.showGenericErrorMessage('Unable to show branch history');
  102. }
  103. finally {
  104. progressCancellation && progressCancellation.cancel();
  105. }
  106. }
  107. }