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.

Reset.java 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (C) 2011, Chris Aniszczyk <caniszczyk@gmail.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.pgm;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import org.eclipse.jgit.api.Git;
  14. import org.eclipse.jgit.api.ResetCommand;
  15. import org.eclipse.jgit.api.ResetCommand.ResetType;
  16. import org.eclipse.jgit.api.errors.GitAPIException;
  17. import org.eclipse.jgit.pgm.internal.CLIText;
  18. import org.kohsuke.args4j.Argument;
  19. import org.kohsuke.args4j.Option;
  20. import org.kohsuke.args4j.spi.RestOfArgumentsHandler;
  21. @Command(common = true, usage = "usage_reset")
  22. class Reset extends TextBuiltin {
  23. @Option(name = "--soft", usage = "usage_resetSoft")
  24. private boolean soft = false;
  25. @Option(name = "--mixed", usage = "usage_resetMixed")
  26. private boolean mixed = false;
  27. @Option(name = "--hard", usage = "usage_resetHard")
  28. private boolean hard = false;
  29. @Argument(required = false, index = 0, metaVar = "metaVar_commitish", usage = "usage_resetReference")
  30. private String commit;
  31. @Argument(required = false, index = 1, metaVar = "metaVar_paths")
  32. @Option(name = "--", metaVar = "metaVar_paths", handler = RestOfArgumentsHandler.class)
  33. private List<String> paths = new ArrayList<>();
  34. /** {@inheritDoc} */
  35. @Override
  36. protected void run() {
  37. try (Git git = new Git(db)) {
  38. ResetCommand command = git.reset();
  39. command.setRef(commit);
  40. if (!paths.isEmpty()) {
  41. for (String path : paths) {
  42. command.addPath(path);
  43. }
  44. } else {
  45. ResetType mode = null;
  46. if (soft) {
  47. mode = selectMode(mode, ResetType.SOFT);
  48. }
  49. if (mixed) {
  50. mode = selectMode(mode, ResetType.MIXED);
  51. }
  52. if (hard) {
  53. mode = selectMode(mode, ResetType.HARD);
  54. }
  55. if (mode == null) {
  56. throw die(CLIText.get().resetNoMode);
  57. }
  58. command.setMode(mode);
  59. }
  60. command.call();
  61. } catch (GitAPIException e) {
  62. throw die(e.getMessage(), e);
  63. }
  64. }
  65. private static ResetType selectMode(ResetType mode, ResetType want) {
  66. if (mode != null)
  67. throw die("reset modes are mutually exclusive, select one"); //$NON-NLS-1$
  68. return want;
  69. }
  70. }