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.

84 lines
2.2 KiB

  1. import type { TextEditor, Uri } from 'vscode';
  2. import { Commands } from '../constants';
  3. import type { Container } from '../container';
  4. import { Logger } from '../logger';
  5. import { showGenericErrorMessage } from '../messages';
  6. import { RepositoryPicker } from '../quickpicks/repositoryPicker';
  7. import { command } from '../system/command';
  8. import type { CommandContext } from './base';
  9. import { ActiveEditorCommand, getCommandUri } from './base';
  10. export interface CompareWithCommandArgs {
  11. ref1?: string;
  12. ref2?: string;
  13. }
  14. @command()
  15. export class CompareWithCommand extends ActiveEditorCommand {
  16. constructor(private readonly container: Container) {
  17. super([
  18. Commands.CompareWith,
  19. Commands.CompareHeadWith,
  20. Commands.CompareWorkingWith,
  21. Commands.Deprecated_DiffHeadWith,
  22. Commands.Deprecated_DiffWorkingWith,
  23. ]);
  24. }
  25. protected override preExecute(context: CommandContext, args?: CompareWithCommandArgs) {
  26. switch (context.command) {
  27. case Commands.CompareWith:
  28. args = { ...args };
  29. break;
  30. case Commands.CompareHeadWith:
  31. case Commands.Deprecated_DiffHeadWith:
  32. args = { ...args };
  33. args.ref1 = 'HEAD';
  34. break;
  35. case Commands.CompareWorkingWith:
  36. case Commands.Deprecated_DiffWorkingWith:
  37. args = { ...args };
  38. args.ref1 = '';
  39. break;
  40. }
  41. return this.execute(context.editor, context.uri, args);
  42. }
  43. async execute(editor?: TextEditor, uri?: Uri, args?: CompareWithCommandArgs) {
  44. uri = getCommandUri(uri, editor);
  45. args = { ...args };
  46. try {
  47. let title;
  48. switch (args.ref1) {
  49. case null:
  50. title = 'Compare';
  51. break;
  52. case '':
  53. title = 'Compare Working Tree with';
  54. break;
  55. case 'HEAD':
  56. title = 'Compare HEAD with';
  57. break;
  58. default:
  59. title = `Compare ${args.ref1} with`;
  60. break;
  61. }
  62. const repoPath = (await RepositoryPicker.getBestRepositoryOrShow(uri, editor, title))?.path;
  63. if (!repoPath) return;
  64. if (args.ref1 != null && args.ref2 != null) {
  65. await this.container.searchAndCompareView.compare(repoPath, args.ref1, args.ref2);
  66. } else {
  67. this.container.searchAndCompareView.selectForCompare(repoPath, args.ref1, { prompt: true });
  68. }
  69. } catch (ex) {
  70. Logger.error(ex, 'CompareWithCommmand');
  71. void showGenericErrorMessage('Unable to open comparison');
  72. }
  73. }
  74. }