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 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (C) 2010, 2020 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.assertFalse;
  14. import static org.junit.Assert.assertThrows;
  15. import static org.junit.Assert.assertTrue;
  16. import static org.junit.Assert.fail;
  17. import java.io.IOException;
  18. import java.util.List;
  19. import org.eclipse.jgit.api.errors.GitAPIException;
  20. import org.eclipse.jgit.api.errors.InvalidTagNameException;
  21. import org.eclipse.jgit.api.errors.JGitInternalException;
  22. import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
  23. import org.eclipse.jgit.junit.RepositoryTestCase;
  24. import org.eclipse.jgit.lib.Ref;
  25. import org.eclipse.jgit.lib.RefUpdate;
  26. import org.eclipse.jgit.lib.Repository;
  27. import org.eclipse.jgit.revwalk.RevCommit;
  28. import org.eclipse.jgit.revwalk.RevWalk;
  29. import org.junit.Test;
  30. public class TagCommandTest extends RepositoryTestCase {
  31. @Test
  32. public void testTagKind() {
  33. try (Git git = new Git(db)) {
  34. assertTrue(git.tag().isAnnotated());
  35. assertTrue(git.tag().setSigned(true).isAnnotated());
  36. assertTrue(git.tag().setSigned(false).isAnnotated());
  37. assertTrue(git.tag().setSigningKey(null).isAnnotated());
  38. assertTrue(git.tag().setSigningKey("something").isAnnotated());
  39. assertTrue(git.tag().setSigned(false).setSigningKey(null)
  40. .isAnnotated());
  41. assertTrue(git.tag().setSigned(false).setSigningKey("something")
  42. .isAnnotated());
  43. assertTrue(git.tag().setSigned(true).setSigningKey(null)
  44. .isAnnotated());
  45. assertTrue(git.tag().setSigned(true).setSigningKey("something")
  46. .isAnnotated());
  47. assertTrue(git.tag().setAnnotated(true).isAnnotated());
  48. assertTrue(
  49. git.tag().setAnnotated(true).setSigned(true).isAnnotated());
  50. assertTrue(git.tag().setAnnotated(true).setSigned(false)
  51. .isAnnotated());
  52. assertTrue(git.tag().setAnnotated(true).setSigningKey(null)
  53. .isAnnotated());
  54. assertTrue(git.tag().setAnnotated(true).setSigningKey("something")
  55. .isAnnotated());
  56. assertTrue(git.tag().setAnnotated(true).setSigned(false)
  57. .setSigningKey(null).isAnnotated());
  58. assertTrue(git.tag().setAnnotated(true).setSigned(false)
  59. .setSigningKey("something").isAnnotated());
  60. assertTrue(git.tag().setAnnotated(true).setSigned(true)
  61. .setSigningKey(null).isAnnotated());
  62. assertTrue(git.tag().setAnnotated(true).setSigned(true)
  63. .setSigningKey("something").isAnnotated());
  64. assertFalse(git.tag().setAnnotated(false).isAnnotated());
  65. assertTrue(git.tag().setAnnotated(false).setSigned(true)
  66. .isAnnotated());
  67. assertFalse(git.tag().setAnnotated(false).setSigned(false)
  68. .isAnnotated());
  69. assertFalse(git.tag().setAnnotated(false).setSigningKey(null)
  70. .isAnnotated());
  71. assertTrue(git.tag().setAnnotated(false).setSigningKey("something")
  72. .isAnnotated());
  73. assertFalse(git.tag().setAnnotated(false).setSigned(false)
  74. .setSigningKey(null).isAnnotated());
  75. assertTrue(git.tag().setAnnotated(false).setSigned(false)
  76. .setSigningKey("something").isAnnotated());
  77. assertTrue(git.tag().setAnnotated(false).setSigned(true)
  78. .setSigningKey(null).isAnnotated());
  79. assertTrue(git.tag().setAnnotated(false).setSigned(true)
  80. .setSigningKey("something").isAnnotated());
  81. }
  82. }
  83. @Test
  84. public void testTaggingOnHead() throws GitAPIException, IOException {
  85. try (Git git = new Git(db);
  86. RevWalk walk = new RevWalk(db)) {
  87. RevCommit commit = git.commit().setMessage("initial commit").call();
  88. Ref tagRef = git.tag().setName("tag").call();
  89. assertEquals(commit.getId(),
  90. db.getRefDatabase().peel(tagRef).getPeeledObjectId());
  91. assertEquals("tag", walk.parseTag(tagRef.getObjectId()).getTagName());
  92. }
  93. }
  94. @Test
  95. public void testTagging()
  96. throws GitAPIException, JGitInternalException, IOException {
  97. try (Git git = new Git(db)) {
  98. git.commit().setMessage("initial commit").call();
  99. RevCommit commit = git.commit().setMessage("second commit").call();
  100. git.commit().setMessage("third commit").call();
  101. Ref tagRef = git.tag().setObjectId(commit).setName("tag").call();
  102. assertEquals(commit.getId(),
  103. db.getRefDatabase().peel(tagRef).getPeeledObjectId());
  104. }
  105. }
  106. @Test
  107. public void testUnannotatedTagging() throws GitAPIException,
  108. JGitInternalException {
  109. try (Git git = new Git(db)) {
  110. git.commit().setMessage("initial commit").call();
  111. RevCommit commit = git.commit().setMessage("second commit").call();
  112. git.commit().setMessage("third commit").call();
  113. Ref tagRef = git.tag().setObjectId(commit).setName("tag")
  114. .setAnnotated(false).call();
  115. assertEquals(commit.getId(), tagRef.getObjectId());
  116. }
  117. }
  118. @Test
  119. public void testForceNoChangeLightweight() throws GitAPIException {
  120. try (Git git = new Git(db)) {
  121. git.commit().setMessage("initial commit").call();
  122. RevCommit commit = git.commit().setMessage("second commit").call();
  123. git.commit().setMessage("third commit").call();
  124. Ref tagRef = git.tag().setObjectId(commit).setName("tag")
  125. .setAnnotated(false).call();
  126. assertEquals(commit.getId(), tagRef.getObjectId());
  127. // Without force, we want to get a RefAlreadyExistsException
  128. RefAlreadyExistsException e = assertThrows(
  129. RefAlreadyExistsException.class,
  130. () -> git.tag().setObjectId(commit).setName("tag")
  131. .setAnnotated(false).call());
  132. assertEquals(RefUpdate.Result.NO_CHANGE, e.getUpdateResult());
  133. // With force the call should work
  134. assertEquals(commit.getId(),
  135. git.tag().setObjectId(commit).setName("tag")
  136. .setAnnotated(false).setForceUpdate(true).call()
  137. .getObjectId());
  138. }
  139. }
  140. @Test
  141. public void testEmptyTagName() throws GitAPIException {
  142. try (Git git = new Git(db)) {
  143. git.commit().setMessage("initial commit").call();
  144. try {
  145. // forget to tag name
  146. git.tag().setMessage("some message").call();
  147. fail("We should have failed without a tag name");
  148. } catch (InvalidTagNameException e) {
  149. // should hit here
  150. }
  151. }
  152. }
  153. @Test
  154. public void testInvalidTagName() throws GitAPIException {
  155. try (Git git = new Git(db)) {
  156. git.commit().setMessage("initial commit").call();
  157. try {
  158. git.tag().setName("bad~tag~name").setMessage("some message").call();
  159. fail("We should have failed due to a bad tag name");
  160. } catch (InvalidTagNameException e) {
  161. // should hit here
  162. }
  163. }
  164. }
  165. private List<Ref> getTags() throws Exception {
  166. return db.getRefDatabase().getRefsByPrefix(R_TAGS);
  167. }
  168. @Test
  169. public void testDelete() throws Exception {
  170. try (Git git = new Git(db)) {
  171. git.commit().setMessage("initial commit").call();
  172. Ref tagRef = git.tag().setName("tag").call();
  173. assertEquals(1, getTags().size());
  174. List<String> deleted = git.tagDelete().setTags(tagRef.getName())
  175. .call();
  176. assertEquals(1, deleted.size());
  177. assertEquals(tagRef.getName(), deleted.get(0));
  178. assertEquals(0, getTags().size());
  179. Ref tagRef1 = git.tag().setName("tag1").call();
  180. Ref tagRef2 = git.tag().setName("tag2").call();
  181. assertEquals(2, getTags().size());
  182. deleted = git.tagDelete().setTags(tagRef1.getName(), tagRef2.getName())
  183. .call();
  184. assertEquals(2, deleted.size());
  185. assertEquals(0, getTags().size());
  186. }
  187. }
  188. @Test
  189. public void testDeleteFullName() throws Exception {
  190. try (Git git = new Git(db)) {
  191. git.commit().setMessage("initial commit").call();
  192. Ref tagRef = git.tag().setName("tag").call();
  193. assertEquals(1, getTags().size());
  194. List<String> deleted = git.tagDelete()
  195. .setTags(Repository.shortenRefName(tagRef.getName())).call();
  196. assertEquals(1, deleted.size());
  197. assertEquals(tagRef.getName(), deleted.get(0));
  198. assertEquals(0, getTags().size());
  199. }
  200. }
  201. @Test
  202. public void testDeleteEmptyTagNames() throws Exception {
  203. try (Git git = new Git(db)) {
  204. git.commit().setMessage("initial commit").call();
  205. List<String> deleted = git.tagDelete().setTags().call();
  206. assertEquals(0, deleted.size());
  207. }
  208. }
  209. @Test
  210. public void testDeleteNonExisting() throws Exception {
  211. try (Git git = new Git(db)) {
  212. git.commit().setMessage("initial commit").call();
  213. List<String> deleted = git.tagDelete().setTags("tag").call();
  214. assertEquals(0, deleted.size());
  215. }
  216. }
  217. @Test
  218. public void testDeleteBadName() throws Exception {
  219. try (Git git = new Git(db)) {
  220. git.commit().setMessage("initial commit").call();
  221. List<String> deleted = git.tagDelete().setTags("bad~tag~name")
  222. .call();
  223. assertEquals(0, deleted.size());
  224. }
  225. }
  226. @Test
  227. public void testShouldNotBlowUpIfThereAreNoTagsInRepository()
  228. throws Exception {
  229. try (Git git = new Git(db)) {
  230. git.add().addFilepattern("*").call();
  231. git.commit().setMessage("initial commit").call();
  232. List<Ref> list = git.tagList().call();
  233. assertEquals(0, list.size());
  234. }
  235. }
  236. @Test
  237. public void testShouldNotBlowUpIfThereAreNoCommitsInRepository()
  238. throws Exception {
  239. try (Git git = new Git(db)) {
  240. List<Ref> list = git.tagList().call();
  241. assertEquals(0, list.size());
  242. }
  243. }
  244. @Test
  245. public void testListAllTagsInRepositoryInOrder() throws Exception {
  246. try (Git git = new Git(db)) {
  247. git.add().addFilepattern("*").call();
  248. git.commit().setMessage("initial commit").call();
  249. git.tag().setName("v3").call();
  250. git.tag().setName("v2").call();
  251. git.tag().setName("v10").call();
  252. List<Ref> list = git.tagList().call();
  253. assertEquals(3, list.size());
  254. assertEquals("refs/tags/v10", list.get(0).getName());
  255. assertEquals("refs/tags/v2", list.get(1).getName());
  256. assertEquals("refs/tags/v3", list.get(2).getName());
  257. }
  258. }
  259. }