Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Tag.java 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
  3. * Copyright (C) 2009, Google Inc.
  4. * Copyright (C) 2008, Charles O'Farrell <charleso@charleso.org>
  5. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg.lists@dewire.com>
  6. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  7. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  8. *
  9. * This program and the accompanying materials are made available under the
  10. * terms of the Eclipse Distribution License v. 1.0 which is available at
  11. * https://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * SPDX-License-Identifier: BSD-3-Clause
  14. */
  15. package org.eclipse.jgit.pgm;
  16. import java.io.IOException;
  17. import java.text.MessageFormat;
  18. import java.util.List;
  19. import org.eclipse.jgit.api.Git;
  20. import org.eclipse.jgit.api.ListTagCommand;
  21. import org.eclipse.jgit.api.TagCommand;
  22. import org.eclipse.jgit.api.errors.GitAPIException;
  23. import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
  24. import org.eclipse.jgit.lib.ObjectId;
  25. import org.eclipse.jgit.lib.Ref;
  26. import org.eclipse.jgit.lib.Repository;
  27. import org.eclipse.jgit.pgm.internal.CLIText;
  28. import org.eclipse.jgit.revwalk.RevWalk;
  29. import org.kohsuke.args4j.Argument;
  30. import org.kohsuke.args4j.Option;
  31. @Command(common = true, usage = "usage_CreateATag")
  32. class Tag extends TextBuiltin {
  33. @Option(name = "-f", usage = "usage_forceReplacingAnExistingTag")
  34. private boolean force;
  35. @Option(name = "-d", usage = "usage_tagDelete")
  36. private boolean delete;
  37. @Option(name = "-m", metaVar = "metaVar_message", usage = "usage_tagMessage")
  38. private String message = ""; //$NON-NLS-1$
  39. @Argument(index = 0, metaVar = "metaVar_name")
  40. private String tagName;
  41. @Argument(index = 1, metaVar = "metaVar_object")
  42. private ObjectId object;
  43. /** {@inheritDoc} */
  44. @Override
  45. protected void run() {
  46. try (Git git = new Git(db)) {
  47. if (tagName != null) {
  48. if (delete) {
  49. List<String> deletedTags = git.tagDelete().setTags(tagName)
  50. .call();
  51. if (deletedTags.isEmpty()) {
  52. throw die(MessageFormat
  53. .format(CLIText.get().tagNotFound, tagName));
  54. }
  55. } else {
  56. TagCommand command = git.tag().setForceUpdate(force)
  57. .setMessage(message).setName(tagName);
  58. if (object != null) {
  59. try (RevWalk walk = new RevWalk(db)) {
  60. command.setObjectId(walk.parseAny(object));
  61. }
  62. }
  63. try {
  64. command.call();
  65. } catch (RefAlreadyExistsException e) {
  66. throw die(MessageFormat.format(
  67. CLIText.get().tagAlreadyExists, tagName), e);
  68. }
  69. }
  70. } else {
  71. ListTagCommand command = git.tagList();
  72. List<Ref> list = command.call();
  73. for (Ref ref : list) {
  74. outw.println(Repository.shortenRefName(ref.getName()));
  75. }
  76. }
  77. } catch (GitAPIException | IOException e) {
  78. throw die(e.getMessage(), e);
  79. }
  80. }
  81. }