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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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(), db.peel(tagRef).getPeeledObjectId());
  66. assertEquals("tag", walk.parseTag(tagRef.getObjectId()).getTagName());
  67. }
  68. }
  69. @Test
  70. public void testTagging() throws GitAPIException, JGitInternalException {
  71. try (Git git = new Git(db)) {
  72. git.commit().setMessage("initial commit").call();
  73. RevCommit commit = git.commit().setMessage("second commit").call();
  74. git.commit().setMessage("third commit").call();
  75. Ref tagRef = git.tag().setObjectId(commit).setName("tag").call();
  76. assertEquals(commit.getId(), db.peel(tagRef).getPeeledObjectId());
  77. }
  78. }
  79. @Test
  80. public void testUnannotatedTagging() throws GitAPIException,
  81. JGitInternalException {
  82. try (Git git = new Git(db)) {
  83. git.commit().setMessage("initial commit").call();
  84. RevCommit commit = git.commit().setMessage("second commit").call();
  85. git.commit().setMessage("third commit").call();
  86. Ref tagRef = git.tag().setObjectId(commit).setName("tag")
  87. .setAnnotated(false).call();
  88. assertEquals(commit.getId(), tagRef.getObjectId());
  89. }
  90. }
  91. @Test
  92. public void testEmptyTagName() throws GitAPIException {
  93. try (Git git = new Git(db)) {
  94. git.commit().setMessage("initial commit").call();
  95. try {
  96. // forget to tag name
  97. git.tag().setMessage("some message").call();
  98. fail("We should have failed without a tag name");
  99. } catch (InvalidTagNameException e) {
  100. // should hit here
  101. }
  102. }
  103. }
  104. @Test
  105. public void testInvalidTagName() throws GitAPIException {
  106. try (Git git = new Git(db)) {
  107. git.commit().setMessage("initial commit").call();
  108. try {
  109. git.tag().setName("bad~tag~name").setMessage("some message").call();
  110. fail("We should have failed due to a bad tag name");
  111. } catch (InvalidTagNameException e) {
  112. // should hit here
  113. }
  114. }
  115. }
  116. @Test
  117. public void testFailureOnSignedTags() throws GitAPIException {
  118. try (Git git = new Git(db)) {
  119. git.commit().setMessage("initial commit").call();
  120. try {
  121. git.tag().setSigned(true).setName("tag").call();
  122. fail("We should have failed with an UnsupportedOperationException due to signed tag");
  123. } catch (UnsupportedOperationException e) {
  124. // should hit here
  125. }
  126. }
  127. }
  128. private List<Ref> getTags() throws Exception {
  129. return db.getRefDatabase().getRefsByPrefix(R_TAGS);
  130. }
  131. @Test
  132. public void testDelete() throws Exception {
  133. try (Git git = new Git(db)) {
  134. git.commit().setMessage("initial commit").call();
  135. Ref tagRef = git.tag().setName("tag").call();
  136. assertEquals(1, getTags().size());
  137. List<String> deleted = git.tagDelete().setTags(tagRef.getName())
  138. .call();
  139. assertEquals(1, deleted.size());
  140. assertEquals(tagRef.getName(), deleted.get(0));
  141. assertEquals(0, getTags().size());
  142. Ref tagRef1 = git.tag().setName("tag1").call();
  143. Ref tagRef2 = git.tag().setName("tag2").call();
  144. assertEquals(2, getTags().size());
  145. deleted = git.tagDelete().setTags(tagRef1.getName(), tagRef2.getName())
  146. .call();
  147. assertEquals(2, deleted.size());
  148. assertEquals(0, getTags().size());
  149. }
  150. }
  151. @Test
  152. public void testDeleteFullName() throws Exception {
  153. try (Git git = new Git(db)) {
  154. git.commit().setMessage("initial commit").call();
  155. Ref tagRef = git.tag().setName("tag").call();
  156. assertEquals(1, getTags().size());
  157. List<String> deleted = git.tagDelete()
  158. .setTags(Repository.shortenRefName(tagRef.getName())).call();
  159. assertEquals(1, deleted.size());
  160. assertEquals(tagRef.getName(), deleted.get(0));
  161. assertEquals(0, getTags().size());
  162. }
  163. }
  164. @Test
  165. public void testDeleteEmptyTagNames() throws Exception {
  166. try (Git git = new Git(db)) {
  167. git.commit().setMessage("initial commit").call();
  168. List<String> deleted = git.tagDelete().setTags().call();
  169. assertEquals(0, deleted.size());
  170. }
  171. }
  172. @Test
  173. public void testDeleteNonExisting() throws Exception {
  174. try (Git git = new Git(db)) {
  175. git.commit().setMessage("initial commit").call();
  176. List<String> deleted = git.tagDelete().setTags("tag").call();
  177. assertEquals(0, deleted.size());
  178. }
  179. }
  180. @Test
  181. public void testDeleteBadName() throws Exception {
  182. try (Git git = new Git(db)) {
  183. git.commit().setMessage("initial commit").call();
  184. List<String> deleted = git.tagDelete().setTags("bad~tag~name")
  185. .call();
  186. assertEquals(0, deleted.size());
  187. }
  188. }
  189. @Test
  190. public void testShouldNotBlowUpIfThereAreNoTagsInRepository()
  191. throws Exception {
  192. try (Git git = new Git(db)) {
  193. git.add().addFilepattern("*").call();
  194. git.commit().setMessage("initial commit").call();
  195. List<Ref> list = git.tagList().call();
  196. assertEquals(0, list.size());
  197. }
  198. }
  199. @Test
  200. public void testShouldNotBlowUpIfThereAreNoCommitsInRepository()
  201. throws Exception {
  202. try (Git git = new Git(db)) {
  203. List<Ref> list = git.tagList().call();
  204. assertEquals(0, list.size());
  205. }
  206. }
  207. @Test
  208. public void testListAllTagsInRepositoryInOrder() throws Exception {
  209. try (Git git = new Git(db)) {
  210. git.add().addFilepattern("*").call();
  211. git.commit().setMessage("initial commit").call();
  212. git.tag().setName("v3").call();
  213. git.tag().setName("v2").call();
  214. git.tag().setName("v10").call();
  215. List<Ref> list = git.tagList().call();
  216. assertEquals(3, list.size());
  217. assertEquals("refs/tags/v10", list.get(0).getName());
  218. assertEquals("refs/tags/v2", list.get(1).getName());
  219. assertEquals("refs/tags/v3", list.get(2).getName());
  220. }
  221. }
  222. }