1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.api;
11
12 import java.io.IOException;
13 import java.text.MessageFormat;
14
15 import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
16 import org.eclipse.jgit.api.errors.GitAPIException;
17 import org.eclipse.jgit.api.errors.InvalidTagNameException;
18 import org.eclipse.jgit.api.errors.JGitInternalException;
19 import org.eclipse.jgit.api.errors.NoHeadException;
20 import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
21 import org.eclipse.jgit.internal.JGitText;
22 import org.eclipse.jgit.lib.Constants;
23 import org.eclipse.jgit.lib.ObjectId;
24 import org.eclipse.jgit.lib.ObjectInserter;
25 import org.eclipse.jgit.lib.PersonIdent;
26 import org.eclipse.jgit.lib.Ref;
27 import org.eclipse.jgit.lib.RefUpdate;
28 import org.eclipse.jgit.lib.RefUpdate.Result;
29 import org.eclipse.jgit.lib.Repository;
30 import org.eclipse.jgit.lib.RepositoryState;
31 import org.eclipse.jgit.lib.TagBuilder;
32 import org.eclipse.jgit.revwalk.RevObject;
33 import org.eclipse.jgit.revwalk.RevWalk;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public class TagCommand extends GitCommand<Ref> {
59 private RevObject id;
60
61 private String name;
62
63 private String message;
64
65 private PersonIdent tagger;
66
67 private boolean signed;
68
69 private boolean forceUpdate;
70
71 private boolean annotated = true;
72
73
74
75
76
77
78 protected TagCommand(Repository repo) {
79 super(repo);
80 }
81
82
83
84
85
86
87
88
89
90
91
92 @Override
93 public Ref call() throws GitAPIException, ConcurrentRefUpdateException,
94 InvalidTagNameException, NoHeadException {
95 checkCallable();
96
97 RepositoryState state = repo.getRepositoryState();
98 processOptions(state);
99
100 try (RevWalklk.html#RevWalk">RevWalk revWalk = new RevWalk(repo)) {
101
102 if (id == null) {
103 ObjectId objectId = repo.resolve(Constants.HEAD + "^{commit}");
104 if (objectId == null)
105 throw new NoHeadException(
106 JGitText.get().tagOnRepoWithoutHEADCurrentlyNotSupported);
107
108 id = revWalk.parseCommit(objectId);
109 }
110
111 if (!annotated) {
112 if (message != null || tagger != null)
113 throw new JGitInternalException(
114 JGitText.get().messageAndTaggerNotAllowedInUnannotatedTags);
115 return updateTagRef(id, revWalk, name,
116 "SimpleTag[" + name + " : " + id
117 + "]");
118 }
119
120
121 TagBuilder newTag = new TagBuilder();
122 newTag.setTag(name);
123 newTag.setMessage(message);
124 newTag.setTagger(tagger);
125 newTag.setObjectId(id);
126
127
128 try (ObjectInserter inserter = repo.newObjectInserter()) {
129 ObjectId tagId = inserter.insert(newTag);
130 inserter.flush();
131
132 String tag = newTag.getTag();
133 return updateTagRef(tagId, revWalk, tag, newTag.toString());
134
135 }
136
137 } catch (IOException e) {
138 throw new JGitInternalException(
139 JGitText.get().exceptionCaughtDuringExecutionOfTagCommand,
140 e);
141 }
142 }
143
144 private Ref updateTagRef(ObjectId tagId, RevWalk revWalk,
145 String tagName, String newTagToString) throws IOException,
146 ConcurrentRefUpdateException, RefAlreadyExistsException {
147 String refName = Constants.R_TAGS + tagName;
148 RefUpdate tagRef = repo.updateRef(refName);
149 tagRef.setNewObjectId(tagId);
150 tagRef.setForceUpdate(forceUpdate);
151 tagRef.setRefLogMessage("tagged " + name, false);
152 Result updateResult = tagRef.update(revWalk);
153 switch (updateResult) {
154 case NEW:
155 case FORCED:
156 return repo.exactRef(refName);
157 case LOCK_FAILURE:
158 throw new ConcurrentRefUpdateException(
159 JGitText.get().couldNotLockHEAD, tagRef.getRef(),
160 updateResult);
161 case REJECTED:
162 throw new RefAlreadyExistsException(MessageFormat.format(
163 JGitText.get().tagAlreadyExists, newTagToString));
164 default:
165 throw new JGitInternalException(MessageFormat.format(
166 JGitText.get().updatingRefFailed, refName, newTagToString,
167 updateResult));
168 }
169 }
170
171
172
173
174
175
176
177
178
179
180
181
182
183 private void processOptions(RepositoryState state)
184 throws InvalidTagNameException {
185 if (tagger == null && annotated)
186 tagger = new PersonIdent(repo);
187 if (name == null || !Repository.isValidRefName(Constants.R_TAGS + name))
188 throw new InvalidTagNameException(
189 MessageFormat.format(JGitText.get().tagNameInvalid,
190 name == null ? "<null>" : name));
191 if (signed)
192 throw new UnsupportedOperationException(
193 JGitText.get().signingNotSupportedOnTag);
194 }
195
196
197
198
199
200
201
202
203 public TagCommand setName(String name) {
204 checkCallable();
205 this.name = name;
206 return this;
207 }
208
209
210
211
212
213
214 public String getName() {
215 return name;
216 }
217
218
219
220
221
222
223 public String getMessage() {
224 return message;
225 }
226
227
228
229
230
231
232
233
234 public TagCommand setMessage(String message) {
235 checkCallable();
236 this.message = message;
237 return this;
238 }
239
240
241
242
243
244
245 public boolean isSigned() {
246 return signed;
247 }
248
249
250
251
252
253
254
255
256
257 public TagCommand setSigned(boolean signed) {
258 this.signed = signed;
259 return this;
260 }
261
262
263
264
265
266
267
268
269
270 public TagCommand setTagger(PersonIdent tagger) {
271 this.tagger = tagger;
272 return this;
273 }
274
275
276
277
278
279
280 public PersonIdent getTagger() {
281 return tagger;
282 }
283
284
285
286
287
288
289 public RevObject getObjectId() {
290 return id;
291 }
292
293
294
295
296
297
298
299
300
301 public TagCommand setObjectId(RevObject id) {
302 this.id = id;
303 return this;
304 }
305
306
307
308
309
310
311 public boolean isForceUpdate() {
312 return forceUpdate;
313 }
314
315
316
317
318
319
320
321
322
323 public TagCommand setForceUpdate(boolean forceUpdate) {
324 this.forceUpdate = forceUpdate;
325 return this;
326 }
327
328
329
330
331
332
333
334
335
336 public TagCommand setAnnotated(boolean annotated) {
337 this.annotated = annotated;
338 return this;
339 }
340
341
342
343
344
345
346
347
348 public boolean isAnnotated() {
349 return annotated;
350 }
351 }