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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (C) 2010, 2013 Chris Aniszczyk <caniszczyk@gmail.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.api;
  44. import static org.eclipse.jgit.lib.Constants.R_TAGS;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.fail;
  47. import java.io.IOException;
  48. import java.util.List;
  49. import org.eclipse.jgit.api.errors.GitAPIException;
  50. import org.eclipse.jgit.api.errors.InvalidTagNameException;
  51. import org.eclipse.jgit.api.errors.JGitInternalException;
  52. import org.eclipse.jgit.junit.RepositoryTestCase;
  53. import org.eclipse.jgit.lib.Ref;
  54. import org.eclipse.jgit.lib.Repository;
  55. import org.eclipse.jgit.revwalk.RevCommit;
  56. import org.eclipse.jgit.revwalk.RevWalk;
  57. import org.junit.Test;
  58. public class TagCommandTest extends RepositoryTestCase {
  59. @Test
  60. public void testTaggingOnHead() throws GitAPIException, IOException {
  61. try (Git git = new Git(db);
  62. RevWalk walk = new RevWalk(db)) {
  63. RevCommit commit = git.commit().setMessage("initial commit").call();
  64. Ref tagRef = git.tag().setName("tag").call();
  65. assertEquals(commit.getId(),
  66. db.getRefDatabase().peel(tagRef).getPeeledObjectId());
  67. assertEquals("tag", walk.parseTag(tagRef.getObjectId()).getTagName());
  68. }
  69. }
  70. @Test
  71. public void testTagging()
  72. throws GitAPIException, JGitInternalException, IOException {
  73. try (Git git = new Git(db)) {
  74. git.commit().setMessage("initial commit").call();
  75. RevCommit commit = git.commit().setMessage("second commit").call();
  76. git.commit().setMessage("third commit").call();
  77. Ref tagRef = git.tag().setObjectId(commit).setName("tag").call();
  78. assertEquals(commit.getId(),
  79. db.getRefDatabase().peel(tagRef).getPeeledObjectId());
  80. }
  81. }
  82. @Test
  83. public void testUnannotatedTagging() throws GitAPIException,
  84. JGitInternalException {
  85. try (Git git = new Git(db)) {
  86. git.commit().setMessage("initial commit").call();
  87. RevCommit commit = git.commit().setMessage("second commit").call();
  88. git.commit().setMessage("third commit").call();
  89. Ref tagRef = git.tag().setObjectId(commit).setName("tag")
  90. .setAnnotated(false).call();
  91. assertEquals(commit.getId(), tagRef.getObjectId());
  92. }
  93. }
  94. @Test
  95. public void testEmptyTagName() throws GitAPIException {
  96. try (Git git = new Git(db)) {
  97. git.commit().setMessage("initial commit").call();
  98. try {
  99. // forget to tag name
  100. git.tag().setMessage("some message").call();
  101. fail("We should have failed without a tag name");
  102. } catch (InvalidTagNameException e) {
  103. // should hit here
  104. }
  105. }
  106. }
  107. @Test
  108. public void testInvalidTagName() throws GitAPIException {
  109. try (Git git = new Git(db)) {
  110. git.commit().setMessage("initial commit").call();
  111. try {
  112. git.tag().setName("bad~tag~name").setMessage("some message").call();
  113. fail("We should have failed due to a bad tag name");
  114. } catch (InvalidTagNameException e) {
  115. // should hit here
  116. }
  117. }
  118. }
  119. @Test
  120. public void testFailureOnSignedTags() throws GitAPIException {
  121. try (Git git = new Git(db)) {
  122. git.commit().setMessage("initial commit").call();
  123. try {
  124. git.tag().setSigned(true).setName("tag").call();
  125. fail("We should have failed with an UnsupportedOperationException due to signed tag");
  126. } catch (UnsupportedOperationException e) {
  127. // should hit here
  128. }
  129. }
  130. }
  131. private List<Ref> getTags() throws Exception {
  132. return db.getRefDatabase().getRefsByPrefix(R_TAGS);
  133. }
  134. @Test
  135. public void testDelete() throws Exception {
  136. try (Git git = new Git(db)) {
  137. git.commit().setMessage("initial commit").call();
  138. Ref tagRef = git.tag().setName("tag").call();
  139. assertEquals(1, getTags().size());
  140. List<String> deleted = git.tagDelete().setTags(tagRef.getName())
  141. .call();
  142. assertEquals(1, deleted.size());
  143. assertEquals(tagRef.getName(), deleted.get(0));
  144. assertEquals(0, getTags().size());
  145. Ref tagRef1 = git.tag().setName("tag1").call();
  146. Ref tagRef2 = git.tag().setName("tag2").call();
  147. assertEquals(2, getTags().size());
  148. deleted = git.tagDelete().setTags(tagRef1.getName(), tagRef2.getName())
  149. .call();
  150. assertEquals(2, deleted.size());
  151. assertEquals(0, getTags().size());
  152. }
  153. }
  154. @Test
  155. public void testDeleteFullName() throws Exception {
  156. try (Git git = new Git(db)) {
  157. git.commit().setMessage("initial commit").call();
  158. Ref tagRef = git.tag().setName("tag").call();
  159. assertEquals(1, getTags().size());
  160. List<String> deleted = git.tagDelete()
  161. .setTags(Repository.shortenRefName(tagRef.getName())).call();
  162. assertEquals(1, deleted.size());
  163. assertEquals(tagRef.getName(), deleted.get(0));
  164. assertEquals(0, getTags().size());
  165. }
  166. }
  167. @Test
  168. public void testDeleteEmptyTagNames() throws Exception {
  169. try (Git git = new Git(db)) {
  170. git.commit().setMessage("initial commit").call();
  171. List<String> deleted = git.tagDelete().setTags().call();
  172. assertEquals(0, deleted.size());
  173. }
  174. }
  175. @Test
  176. public void testDeleteNonExisting() throws Exception {
  177. try (Git git = new Git(db)) {
  178. git.commit().setMessage("initial commit").call();
  179. List<String> deleted = git.tagDelete().setTags("tag").call();
  180. assertEquals(0, deleted.size());
  181. }
  182. }
  183. @Test
  184. public void testDeleteBadName() throws Exception {
  185. try (Git git = new Git(db)) {
  186. git.commit().setMessage("initial commit").call();
  187. List<String> deleted = git.tagDelete().setTags("bad~tag~name")
  188. .call();
  189. assertEquals(0, deleted.size());
  190. }
  191. }
  192. @Test
  193. public void testShouldNotBlowUpIfThereAreNoTagsInRepository()
  194. throws Exception {
  195. try (Git git = new Git(db)) {
  196. git.add().addFilepattern("*").call();
  197. git.commit().setMessage("initial commit").call();
  198. List<Ref> list = git.tagList().call();
  199. assertEquals(0, list.size());
  200. }
  201. }
  202. @Test
  203. public void testShouldNotBlowUpIfThereAreNoCommitsInRepository()
  204. throws Exception {
  205. try (Git git = new Git(db)) {
  206. List<Ref> list = git.tagList().call();
  207. assertEquals(0, list.size());
  208. }
  209. }
  210. @Test
  211. public void testListAllTagsInRepositoryInOrder() throws Exception {
  212. try (Git git = new Git(db)) {
  213. git.add().addFilepattern("*").call();
  214. git.commit().setMessage("initial commit").call();
  215. git.tag().setName("v3").call();
  216. git.tag().setName("v2").call();
  217. git.tag().setName("v10").call();
  218. List<Ref> list = git.tagList().call();
  219. assertEquals(3, list.size());
  220. assertEquals("refs/tags/v10", list.get(0).getName());
  221. assertEquals("refs/tags/v2", list.get(1).getName());
  222. assertEquals("refs/tags/v3", list.get(2).getName());
  223. }
  224. }
  225. }