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.

77 lines
3.7 KiB

  1. 'use strict';
  2. import { Strings } from '../system';
  3. import { MessageItem, window } from 'vscode';
  4. import { Command, CommandContext, Commands, isCommandViewContextWithCommit } from './common';
  5. import { GlyphChars } from '../constants';
  6. import { GitService, GitStashCommit } from '../gitService';
  7. import { Logger } from '../logger';
  8. import { CommandQuickPickItem, CommitQuickPickItem, StashListQuickPick } from '../quickPicks';
  9. export interface StashApplyCommandArgs {
  10. confirm?: boolean;
  11. deleteAfter?: boolean;
  12. stashItem?: { stashName: string, message: string };
  13. goBackCommand?: CommandQuickPickItem;
  14. }
  15. export class StashApplyCommand extends Command {
  16. constructor(private git: GitService) {
  17. super(Commands.StashApply);
  18. }
  19. protected async preExecute(context: CommandContext, args: StashApplyCommandArgs = { confirm: true, deleteAfter: false }) {
  20. if (isCommandViewContextWithCommit<GitStashCommit>(context)) {
  21. args = { ...args };
  22. args.stashItem = { stashName: context.node.commit.stashName, message: context.node.commit.message };
  23. return this.execute(args);
  24. }
  25. return this.execute(args);
  26. }
  27. async execute(args: StashApplyCommandArgs = { confirm: true, deleteAfter: false }) {
  28. if (!this.git.repoPath) return undefined;
  29. args = { ...args };
  30. if (args.stashItem === undefined || args.stashItem.stashName === undefined) {
  31. const stash = await this.git.getStashList(this.git.repoPath);
  32. if (stash === undefined) return window.showInformationMessage(`There are no stashed changes`);
  33. const currentCommand = new CommandQuickPickItem({
  34. label: `go back ${GlyphChars.ArrowBack}`,
  35. description: `${Strings.pad(GlyphChars.Dash, 2, 3)} to apply stashed changes`
  36. }, Commands.StashApply, [args]);
  37. const pick = await StashListQuickPick.show(this.git, stash, 'apply', args.goBackCommand, currentCommand);
  38. if (pick === undefined || !(pick instanceof CommitQuickPickItem)) return args.goBackCommand === undefined ? undefined : args.goBackCommand.execute();
  39. args.goBackCommand = currentCommand;
  40. args.stashItem = pick.commit as GitStashCommit;
  41. }
  42. try {
  43. if (args.confirm) {
  44. const message = args.stashItem.message.length > 80 ? `${args.stashItem.message.substring(0, 80)}${GlyphChars.Ellipsis}` : args.stashItem.message;
  45. const result = await window.showWarningMessage(`Apply stashed changes '${message}' to your working tree?`, { title: 'Yes, delete after applying' } as MessageItem, { title: 'Yes' } as MessageItem, { title: 'No', isCloseAffordance: true } as MessageItem);
  46. if (result === undefined || result.title === 'No') return args.goBackCommand === undefined ? undefined : args.goBackCommand.execute();
  47. args.deleteAfter = result.title !== 'Yes';
  48. }
  49. return await this.git.stashApply(this.git.repoPath, args.stashItem.stashName, args.deleteAfter);
  50. }
  51. catch (ex) {
  52. Logger.error(ex, 'StashApplyCommand');
  53. if (ex.message.includes('Your local changes to the following files would be overwritten by merge')) {
  54. return window.showWarningMessage(`Unable to apply stash. Your working tree changes would be overwritten.`);
  55. }
  56. else if (ex.message.includes('Auto-merging') && ex.message.includes('CONFLICT')) {
  57. return window.showInformationMessage(`Stash applied with conflicts`);
  58. }
  59. else {
  60. return window.showErrorMessage(`Unable to apply stash. See output channel for more details`);
  61. }
  62. }
  63. }
  64. }