91 regels
2.8 KiB

  1. 'use strict';
  2. import { commands, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
  3. import { Container } from '../container';
  4. import { GitCommit, GitService, GitUri } from '../git/gitService';
  5. import { Logger } from '../logger';
  6. import { Messages } from '../messages';
  7. import { ActiveEditorCommand, command, Commands, getCommandUri } from './common';
  8. import { DiffWithCommandArgs } from './diffWith';
  9. export interface DiffLineWithWorkingCommandArgs {
  10. commit?: GitCommit;
  11. line?: number;
  12. showOptions?: TextDocumentShowOptions;
  13. }
  14. @command()
  15. export class DiffLineWithWorkingCommand extends ActiveEditorCommand {
  16. constructor() {
  17. super(Commands.DiffLineWithWorking);
  18. }
  19. async execute(editor?: TextEditor, uri?: Uri, args?: DiffLineWithWorkingCommandArgs): Promise<any> {
  20. uri = getCommandUri(uri, editor);
  21. if (uri == null) return undefined;
  22. const gitUri = await GitUri.fromUri(uri);
  23. args = { ...args };
  24. if (args.line === undefined) {
  25. args.line = editor == null ? 0 : editor.selection.active.line;
  26. }
  27. if (args.commit === undefined || args.commit.isUncommitted) {
  28. const blameline = args.line;
  29. if (blameline < 0) return undefined;
  30. try {
  31. const blame =
  32. editor && editor.document && editor.document.isDirty
  33. ? await Container.git.getBlameForLineContents(gitUri, blameline, editor.document.getText())
  34. : await Container.git.getBlameForLine(gitUri, blameline);
  35. if (blame === undefined) {
  36. return Messages.showFileNotUnderSourceControlWarningMessage('Unable to open compare');
  37. }
  38. args.commit = blame.commit;
  39. // If the line is uncommitted, change the previous commit
  40. if (args.commit.isUncommitted) {
  41. const status = await Container.git.getStatusForFile(gitUri.repoPath!, gitUri.fsPath);
  42. args.commit = args.commit.with({
  43. sha:
  44. status !== undefined && status.indexStatus !== undefined
  45. ? GitService.uncommittedStagedSha
  46. : args.commit.previousSha!,
  47. fileName: args.commit.previousFileName!,
  48. originalFileName: null,
  49. previousSha: null,
  50. previousFileName: null
  51. });
  52. // editor lines are 0-based
  53. args.line = blame.line.line - 1;
  54. }
  55. } catch (ex) {
  56. Logger.error(ex, 'DiffLineWithWorkingCommand', `getBlameForLine(${blameline})`);
  57. return Messages.showGenericErrorMessage('Unable to open compare');
  58. }
  59. }
  60. const workingUri = await args.commit.getWorkingUri();
  61. if (workingUri === undefined) {
  62. return window.showWarningMessage('Unable to open compare. File has been deleted from the working tree');
  63. }
  64. const diffArgs: DiffWithCommandArgs = {
  65. repoPath: args.commit.repoPath,
  66. lhs: {
  67. sha: args.commit.sha,
  68. uri: args.commit.uri
  69. },
  70. rhs: {
  71. sha: '',
  72. uri: workingUri
  73. },
  74. line: args.line,
  75. showOptions: args.showOptions
  76. };
  77. return commands.executeCommand(Commands.DiffWith, diffArgs);
  78. }
  79. }