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