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.

99 lines
2.8 KiB

  1. 'use strict';
  2. import { TextDocumentShowOptions, TextEditor, Uri } from 'vscode';
  3. import { ActiveEditorCommand, command, Commands, executeCommand, getCommandUri } from './common';
  4. import { GlyphChars, quickPickTitleMaxChars } from '../constants';
  5. import { Container } from '../container';
  6. import { DiffWithCommandArgs } from './diffWith';
  7. import { GitRevision } from '../git/git';
  8. import { GitUri } from '../git/gitUri';
  9. import { Logger } from '../logger';
  10. import { Messages } from '../messages';
  11. import { CommandQuickPickItem, CommitPicker } from '../quickpicks';
  12. import { Strings } from '../system';
  13. export interface DiffWithRevisionCommandArgs {
  14. line?: number;
  15. showOptions?: TextDocumentShowOptions;
  16. }
  17. @command()
  18. export class DiffWithRevisionCommand extends ActiveEditorCommand {
  19. constructor() {
  20. super(Commands.DiffWithRevision);
  21. }
  22. async execute(editor?: TextEditor, uri?: Uri, args?: DiffWithRevisionCommandArgs): Promise<any> {
  23. uri = getCommandUri(uri, editor);
  24. if (uri == null) return;
  25. const gitUri = await GitUri.fromUri(uri);
  26. args = { ...args };
  27. if (args.line == null) {
  28. args.line = editor?.selection.active.line ?? 0;
  29. }
  30. try {
  31. const log = Container.git
  32. .getLogForFile(gitUri.repoPath, gitUri.fsPath)
  33. .then(
  34. log =>
  35. log ??
  36. (gitUri.sha
  37. ? Container.git.getLogForFile(gitUri.repoPath, gitUri.fsPath, { ref: gitUri.sha })
  38. : undefined),
  39. );
  40. const title = `Open Changes with Revision${Strings.pad(GlyphChars.Dot, 2, 2)}`;
  41. const pick = await CommitPicker.show(
  42. log,
  43. `${title}${gitUri.getFormattedFilename({
  44. suffix: gitUri.sha ? `:${GitRevision.shorten(gitUri.sha)}` : undefined,
  45. truncateTo: quickPickTitleMaxChars - title.length,
  46. })}`,
  47. 'Choose a commit to compare with',
  48. {
  49. picked: gitUri.sha,
  50. keys: ['right', 'alt+right', 'ctrl+right'],
  51. onDidPressKey: async (key, item) => {
  52. void (await executeCommand<DiffWithCommandArgs>(Commands.DiffWith, {
  53. repoPath: gitUri.repoPath,
  54. lhs: {
  55. sha: item.item.ref,
  56. uri: gitUri,
  57. },
  58. rhs: {
  59. sha: '',
  60. uri: gitUri,
  61. },
  62. line: args!.line,
  63. showOptions: args!.showOptions,
  64. }));
  65. },
  66. showOtherReferences: CommandQuickPickItem.fromCommand(
  67. 'Choose a branch or tag...',
  68. Commands.DiffWithRevisionFrom,
  69. ),
  70. },
  71. );
  72. if (pick == null) return;
  73. void (await executeCommand<DiffWithCommandArgs>(Commands.DiffWith, {
  74. repoPath: gitUri.repoPath,
  75. lhs: {
  76. sha: pick.ref,
  77. uri: gitUri,
  78. },
  79. rhs: {
  80. sha: '',
  81. uri: gitUri,
  82. },
  83. line: args.line,
  84. showOptions: args.showOptions,
  85. }));
  86. } catch (ex) {
  87. Logger.error(ex, 'DiffWithRevisionCommand');
  88. void Messages.showGenericErrorMessage('Unable to open compare');
  89. }
  90. }
  91. }