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.

DeleteTagCommand.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (C) 2011, Tomasz Zarna <Tomasz.Zarna@pl.ibm.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.api;
  11. import java.io.IOException;
  12. import java.text.MessageFormat;
  13. import java.util.ArrayList;
  14. import java.util.Arrays;
  15. import java.util.HashSet;
  16. import java.util.List;
  17. import java.util.Set;
  18. import org.eclipse.jgit.api.errors.GitAPIException;
  19. import org.eclipse.jgit.api.errors.JGitInternalException;
  20. import org.eclipse.jgit.internal.JGitText;
  21. import org.eclipse.jgit.lib.Ref;
  22. import org.eclipse.jgit.lib.RefUpdate;
  23. import org.eclipse.jgit.lib.RefUpdate.Result;
  24. import org.eclipse.jgit.lib.Repository;
  25. /**
  26. * Used to delete one or several tags.
  27. *
  28. * The result of {@link #call()} is a list with the (full) names of the deleted
  29. * tags.
  30. *
  31. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-tag.html"
  32. * >Git documentation about Tag</a>
  33. */
  34. public class DeleteTagCommand extends GitCommand<List<String>> {
  35. private final Set<String> tags = new HashSet<>();
  36. /**
  37. * Constructor for DeleteTagCommand
  38. *
  39. * @param repo
  40. * the {@link org.eclipse.jgit.lib.Repository}
  41. */
  42. protected DeleteTagCommand(Repository repo) {
  43. super(repo);
  44. }
  45. /** {@inheritDoc} */
  46. @Override
  47. public List<String> call() throws GitAPIException {
  48. checkCallable();
  49. List<String> result = new ArrayList<>();
  50. if (tags.isEmpty())
  51. return result;
  52. try {
  53. setCallable(false);
  54. for (String tagName : tags) {
  55. if (tagName == null)
  56. continue;
  57. Ref currentRef = repo.findRef(tagName);
  58. if (currentRef == null)
  59. continue;
  60. String fullName = currentRef.getName();
  61. RefUpdate update = repo.updateRef(fullName);
  62. update.setForceUpdate(true);
  63. Result deleteResult = update.delete();
  64. boolean ok = true;
  65. switch (deleteResult) {
  66. case IO_FAILURE:
  67. case LOCK_FAILURE:
  68. case REJECTED:
  69. ok = false;
  70. break;
  71. default:
  72. break;
  73. }
  74. if (ok) {
  75. result.add(fullName);
  76. } else
  77. throw new JGitInternalException(MessageFormat.format(
  78. JGitText.get().deleteTagUnexpectedResult,
  79. deleteResult.name()));
  80. }
  81. return result;
  82. } catch (IOException ioe) {
  83. throw new JGitInternalException(ioe.getMessage(), ioe);
  84. }
  85. }
  86. /**
  87. * Set names of the tags to delete
  88. *
  89. * @param tags
  90. * the names of the tags to delete; if not set, this will do
  91. * nothing; invalid tag names will simply be ignored
  92. * @return this instance
  93. */
  94. public DeleteTagCommand setTags(String... tags) {
  95. checkCallable();
  96. this.tags.clear();
  97. this.tags.addAll(Arrays.asList(tags));
  98. return this;
  99. }
  100. }