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.

TagCommandTest.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (C) 2010, 2013 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.api;
  11. import static org.eclipse.jgit.lib.Constants.R_TAGS;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.fail;
  14. import java.io.IOException;
  15. import java.util.List;
  16. import org.eclipse.jgit.api.errors.GitAPIException;
  17. import org.eclipse.jgit.api.errors.InvalidTagNameException;
  18. import org.eclipse.jgit.api.errors.JGitInternalException;
  19. import org.eclipse.jgit.junit.RepositoryTestCase;
  20. import org.eclipse.jgit.lib.Ref;
  21. import org.eclipse.jgit.lib.Repository;
  22. import org.eclipse.jgit.revwalk.RevCommit;
  23. import org.eclipse.jgit.revwalk.RevWalk;
  24. import org.junit.Test;
  25. public class TagCommandTest extends RepositoryTestCase {
  26. @Test
  27. public void testTaggingOnHead() throws GitAPIException, IOException {
  28. try (Git git = new Git(db);
  29. RevWalk walk = new RevWalk(db)) {
  30. RevCommit commit = git.commit().setMessage("initial commit").call();
  31. Ref tagRef = git.tag().setName("tag").call();
  32. assertEquals(commit.getId(),
  33. db.getRefDatabase().peel(tagRef).getPeeledObjectId());
  34. assertEquals("tag", walk.parseTag(tagRef.getObjectId()).getTagName());
  35. }
  36. }
  37. @Test
  38. public void testTagging()
  39. throws GitAPIException, JGitInternalException, IOException {
  40. try (Git git = new Git(db)) {
  41. git.commit().setMessage("initial commit").call();
  42. RevCommit commit = git.commit().setMessage("second commit").call();
  43. git.commit().setMessage("third commit").call();
  44. Ref tagRef = git.tag().setObjectId(commit).setName("tag").call();
  45. assertEquals(commit.getId(),
  46. db.getRefDatabase().peel(tagRef).getPeeledObjectId());
  47. }
  48. }
  49. @Test
  50. public void testUnannotatedTagging() throws GitAPIException,
  51. JGitInternalException {
  52. try (Git git = new Git(db)) {
  53. git.commit().setMessage("initial commit").call();
  54. RevCommit commit = git.commit().setMessage("second commit").call();
  55. git.commit().setMessage("third commit").call();
  56. Ref tagRef = git.tag().setObjectId(commit).setName("tag")
  57. .setAnnotated(false).call();
  58. assertEquals(commit.getId(), tagRef.getObjectId());
  59. }
  60. }
  61. @Test
  62. public void testEmptyTagName() throws GitAPIException {
  63. try (Git git = new Git(db)) {
  64. git.commit().setMessage("initial commit").call();
  65. try {
  66. // forget to tag name
  67. git.tag().setMessage("some message").call();
  68. fail("We should have failed without a tag name");
  69. } catch (InvalidTagNameException e) {
  70. // should hit here
  71. }
  72. }
  73. }
  74. @Test
  75. public void testInvalidTagName() throws GitAPIException {
  76. try (Git git = new Git(db)) {
  77. git.commit().setMessage("initial commit").call();
  78. try {
  79. git.tag().setName("bad~tag~name").setMessage("some message").call();
  80. fail("We should have failed due to a bad tag name");
  81. } catch (InvalidTagNameException e) {
  82. // should hit here
  83. }
  84. }
  85. }
  86. @Test
  87. public void testFailureOnSignedTags() throws GitAPIException {
  88. try (Git git = new Git(db)) {
  89. git.commit().setMessage("initial commit").call();
  90. try {
  91. git.tag().setSigned(true).setName("tag").call();
  92. fail("We should have failed with an UnsupportedOperationException due to signed tag");
  93. } catch (UnsupportedOperationException e) {
  94. // should hit here
  95. }
  96. }
  97. }
  98. private List<Ref> getTags() throws Exception {
  99. return db.getRefDatabase().getRefsByPrefix(R_TAGS);
  100. }
  101. @Test
  102. public void testDelete() throws Exception {
  103. try (Git git = new Git(db)) {
  104. git.commit().setMessage("initial commit").call();
  105. Ref tagRef = git.tag().setName("tag").call();
  106. assertEquals(1, getTags().size());
  107. List<String> deleted = git.tagDelete().setTags(tagRef.getName())
  108. .call();
  109. assertEquals(1, deleted.size());
  110. assertEquals(tagRef.getName(), deleted.get(0));
  111. assertEquals(0, getTags().size());
  112. Ref tagRef1 = git.tag().setName("tag1").call();
  113. Ref tagRef2 = git.tag().setName("tag2").call();
  114. assertEquals(2, getTags().size());
  115. deleted = git.tagDelete().setTags(tagRef1.getName(), tagRef2.getName())
  116. .call();
  117. assertEquals(2, deleted.size());
  118. assertEquals(0, getTags().size());
  119. }
  120. }
  121. @Test
  122. public void testDeleteFullName() throws Exception {
  123. try (Git git = new Git(db)) {
  124. git.commit().setMessage("initial commit").call();
  125. Ref tagRef = git.tag().setName("tag").call();
  126. assertEquals(1, getTags().size());
  127. List<String> deleted = git.tagDelete()
  128. .setTags(Repository.shortenRefName(tagRef.getName())).call();
  129. assertEquals(1, deleted.size());
  130. assertEquals(tagRef.getName(), deleted.get(0));
  131. assertEquals(0, getTags().size());
  132. }
  133. }
  134. @Test
  135. public void testDeleteEmptyTagNames() throws Exception {
  136. try (Git git = new Git(db)) {
  137. git.commit().setMessage("initial commit").call();
  138. List<String> deleted = git.tagDelete().setTags().call();
  139. assertEquals(0, deleted.size());
  140. }
  141. }
  142. @Test
  143. public void testDeleteNonExisting() throws Exception {
  144. try (Git git = new Git(db)) {
  145. git.commit().setMessage("initial commit").call();
  146. List<String> deleted = git.tagDelete().setTags("tag").call();
  147. assertEquals(0, deleted.size());
  148. }
  149. }
  150. @Test
  151. public void testDeleteBadName() throws Exception {
  152. try (Git git = new Git(db)) {
  153. git.commit().setMessage("initial commit").call();
  154. List<String> deleted = git.tagDelete().setTags("bad~tag~name")
  155. .call();
  156. assertEquals(0, deleted.size());
  157. }
  158. }
  159. @Test
  160. public void testShouldNotBlowUpIfThereAreNoTagsInRepository()
  161. throws Exception {
  162. try (Git git = new Git(db)) {
  163. git.add().addFilepattern("*").call();
  164. git.commit().setMessage("initial commit").call();
  165. List<Ref> list = git.tagList().call();
  166. assertEquals(0, list.size());
  167. }
  168. }
  169. @Test
  170. public void testShouldNotBlowUpIfThereAreNoCommitsInRepository()
  171. throws Exception {
  172. try (Git git = new Git(db)) {
  173. List<Ref> list = git.tagList().call();
  174. assertEquals(0, list.size());
  175. }
  176. }
  177. @Test
  178. public void testListAllTagsInRepositoryInOrder() throws Exception {
  179. try (Git git = new Git(db)) {
  180. git.add().addFilepattern("*").call();
  181. git.commit().setMessage("initial commit").call();
  182. git.tag().setName("v3").call();
  183. git.tag().setName("v2").call();
  184. git.tag().setName("v10").call();
  185. List<Ref> list = git.tagList().call();
  186. assertEquals(3, list.size());
  187. assertEquals("refs/tags/v10", list.get(0).getName());
  188. assertEquals("refs/tags/v2", list.get(1).getName());
  189. assertEquals("refs/tags/v3", list.get(2).getName());
  190. }
  191. }
  192. }