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

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