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

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