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.

RevParse.java 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (C) 2009, Daniel Cheng (aka SDiZ) <git@sdiz.net>
  3. * Copyright (C) 2009, Daniel Cheng (aka SDiZ) <j16sdiz+freenet@gmail.com>
  4. * Copyright (C) 2015 Thomas Meyer <thomas@m3y3r.de> and others
  5. *
  6. * This program and the accompanying materials are made available under the
  7. * terms of the Eclipse Distribution License v. 1.0 which is available at
  8. * https://www.eclipse.org/org/documents/edl-v10.php.
  9. *
  10. * SPDX-License-Identifier: BSD-3-Clause
  11. */
  12. package org.eclipse.jgit.pgm;
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import org.eclipse.jgit.lib.ObjectId;
  17. import org.eclipse.jgit.lib.Ref;
  18. import org.eclipse.jgit.pgm.internal.CLIText;
  19. import org.eclipse.jgit.pgm.opt.CmdLineParser;
  20. import org.kohsuke.args4j.Argument;
  21. import org.kohsuke.args4j.CmdLineException;
  22. import org.kohsuke.args4j.Option;
  23. @Command(usage = "usage_RevParse")
  24. class RevParse extends TextBuiltin {
  25. @Option(name = "--all", usage = "usage_RevParseAll")
  26. boolean all;
  27. @Option(name = "--verify", usage = "usage_RevParseVerify")
  28. boolean verify;
  29. @Argument(index = 0, metaVar = "metaVar_commitish")
  30. private List<ObjectId> commits = new ArrayList<>();
  31. /** {@inheritDoc} */
  32. @Override
  33. protected void run() {
  34. try {
  35. if (all) {
  36. for (Ref r : db.getRefDatabase().getRefs()) {
  37. ObjectId objectId = r.getObjectId();
  38. // getRefs skips dangling symrefs, so objectId should never
  39. // be null.
  40. if (objectId == null) {
  41. throw new NullPointerException();
  42. }
  43. outw.println(objectId.name());
  44. }
  45. } else {
  46. if (verify && commits.size() > 1) {
  47. final CmdLineParser clp = new CmdLineParser(this);
  48. throw new CmdLineException(clp,
  49. CLIText.format(CLIText.get().needSingleRevision));
  50. }
  51. for (ObjectId o : commits) {
  52. outw.println(o.name());
  53. }
  54. }
  55. } catch (IOException | CmdLineException e) {
  56. throw die(e.getMessage(), e);
  57. }
  58. }
  59. }