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.

TagCommand.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 java.io.IOException;
  45. import java.text.MessageFormat;
  46. import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
  47. import org.eclipse.jgit.api.errors.GitAPIException;
  48. import org.eclipse.jgit.api.errors.InvalidTagNameException;
  49. import org.eclipse.jgit.api.errors.JGitInternalException;
  50. import org.eclipse.jgit.api.errors.NoHeadException;
  51. import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
  52. import org.eclipse.jgit.internal.JGitText;
  53. import org.eclipse.jgit.lib.Constants;
  54. import org.eclipse.jgit.lib.ObjectId;
  55. import org.eclipse.jgit.lib.ObjectInserter;
  56. import org.eclipse.jgit.lib.PersonIdent;
  57. import org.eclipse.jgit.lib.Ref;
  58. import org.eclipse.jgit.lib.RefUpdate;
  59. import org.eclipse.jgit.lib.RefUpdate.Result;
  60. import org.eclipse.jgit.lib.Repository;
  61. import org.eclipse.jgit.lib.RepositoryState;
  62. import org.eclipse.jgit.lib.TagBuilder;
  63. import org.eclipse.jgit.revwalk.RevObject;
  64. import org.eclipse.jgit.revwalk.RevWalk;
  65. /**
  66. * Create/update an annotated tag object or a simple unannotated tag
  67. * <p>
  68. * Examples (<code>git</code> is a {@link Git} instance):
  69. * <p>
  70. * Create a new tag for the current commit:
  71. *
  72. * <pre>
  73. * git.tag().setName(&quot;v1.0&quot;).setMessage(&quot;First stable release&quot;).call();
  74. * </pre>
  75. * <p>
  76. *
  77. * <p>
  78. * Create a new unannotated tag for the current commit:
  79. *
  80. * <pre>
  81. * git.tag().setName(&quot;v1.0&quot;).setAnnotated(false).call();
  82. * </pre>
  83. * <p>
  84. *
  85. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-tag.html"
  86. * >Git documentation about Tag</a>
  87. */
  88. public class TagCommand extends GitCommand<Ref> {
  89. private RevObject id;
  90. private String name;
  91. private String message;
  92. private PersonIdent tagger;
  93. private boolean signed;
  94. private boolean forceUpdate;
  95. private boolean annotated = true;
  96. /**
  97. * @param repo
  98. */
  99. protected TagCommand(Repository repo) {
  100. super(repo);
  101. }
  102. /**
  103. * Executes the {@code tag} command with all the options and parameters
  104. * collected by the setter methods of this class. Each instance of this
  105. * class should only be used for one invocation of the command (means: one
  106. * call to {@link #call()})
  107. *
  108. * @return a {@link Ref} a ref pointing to a tag
  109. * @throws NoHeadException
  110. * when called on a git repo without a HEAD reference
  111. * @since 2.0
  112. */
  113. public Ref call() throws GitAPIException, ConcurrentRefUpdateException,
  114. InvalidTagNameException, NoHeadException {
  115. checkCallable();
  116. RepositoryState state = repo.getRepositoryState();
  117. processOptions(state);
  118. try (RevWalk revWalk = new RevWalk(repo)) {
  119. // if no id is set, we should attempt to use HEAD
  120. if (id == null) {
  121. ObjectId objectId = repo.resolve(Constants.HEAD + "^{commit}"); //$NON-NLS-1$
  122. if (objectId == null)
  123. throw new NoHeadException(
  124. JGitText.get().tagOnRepoWithoutHEADCurrentlyNotSupported);
  125. id = revWalk.parseCommit(objectId);
  126. }
  127. if (!annotated) {
  128. if (message != null || tagger != null)
  129. throw new JGitInternalException(
  130. JGitText.get().messageAndTaggerNotAllowedInUnannotatedTags);
  131. return updateTagRef(id, revWalk, name,
  132. "SimpleTag[" + name + " : " + id //$NON-NLS-1$ //$NON-NLS-2$
  133. + "]"); //$NON-NLS-1$
  134. }
  135. // create the tag object
  136. TagBuilder newTag = new TagBuilder();
  137. newTag.setTag(name);
  138. newTag.setMessage(message);
  139. newTag.setTagger(tagger);
  140. newTag.setObjectId(id);
  141. // write the tag object
  142. try (ObjectInserter inserter = repo.newObjectInserter()) {
  143. ObjectId tagId = inserter.insert(newTag);
  144. inserter.flush();
  145. String tag = newTag.getTag();
  146. return updateTagRef(tagId, revWalk, tag, newTag.toString());
  147. }
  148. } catch (IOException e) {
  149. throw new JGitInternalException(
  150. JGitText.get().exceptionCaughtDuringExecutionOfTagCommand,
  151. e);
  152. }
  153. }
  154. private Ref updateTagRef(ObjectId tagId, RevWalk revWalk,
  155. String tagName, String newTagToString) throws IOException,
  156. ConcurrentRefUpdateException, RefAlreadyExistsException {
  157. String refName = Constants.R_TAGS + tagName;
  158. RefUpdate tagRef = repo.updateRef(refName);
  159. tagRef.setNewObjectId(tagId);
  160. tagRef.setForceUpdate(forceUpdate);
  161. tagRef.setRefLogMessage("tagged " + name, false); //$NON-NLS-1$
  162. Result updateResult = tagRef.update(revWalk);
  163. switch (updateResult) {
  164. case NEW:
  165. case FORCED:
  166. return repo.exactRef(refName);
  167. case LOCK_FAILURE:
  168. throw new ConcurrentRefUpdateException(
  169. JGitText.get().couldNotLockHEAD, tagRef.getRef(),
  170. updateResult);
  171. case REJECTED:
  172. throw new RefAlreadyExistsException(MessageFormat.format(
  173. JGitText.get().tagAlreadyExists, newTagToString));
  174. default:
  175. throw new JGitInternalException(MessageFormat.format(
  176. JGitText.get().updatingRefFailed, refName, newTagToString,
  177. updateResult));
  178. }
  179. }
  180. /**
  181. * Sets default values for not explicitly specified options. Then validates
  182. * that all required data has been provided.
  183. *
  184. * @param state
  185. * the state of the repository we are working on
  186. *
  187. * @throws InvalidTagNameException
  188. * if the tag name is null or invalid
  189. * @throws UnsupportedOperationException
  190. * if the tag is signed (not supported yet)
  191. */
  192. private void processOptions(RepositoryState state)
  193. throws InvalidTagNameException {
  194. if (tagger == null && annotated)
  195. tagger = new PersonIdent(repo);
  196. if (name == null || !Repository.isValidRefName(Constants.R_TAGS + name))
  197. throw new InvalidTagNameException(
  198. MessageFormat.format(JGitText.get().tagNameInvalid,
  199. name == null ? "<null>" : name)); //$NON-NLS-1$
  200. if (signed)
  201. throw new UnsupportedOperationException(
  202. JGitText.get().signingNotSupportedOnTag);
  203. }
  204. /**
  205. * @param name
  206. * the tag name used for the {@code tag}
  207. * @return {@code this}
  208. */
  209. public TagCommand setName(String name) {
  210. checkCallable();
  211. this.name = name;
  212. return this;
  213. }
  214. /**
  215. * @return the tag name used for the <code>tag</code>
  216. */
  217. public String getName() {
  218. return name;
  219. }
  220. /**
  221. * @return the tag message used for the <code>tag</code>
  222. */
  223. public String getMessage() {
  224. return message;
  225. }
  226. /**
  227. * @param message
  228. * the tag message used for the {@code tag}
  229. * @return {@code this}
  230. */
  231. public TagCommand setMessage(String message) {
  232. checkCallable();
  233. this.message = message;
  234. return this;
  235. }
  236. /**
  237. * @return whether the tag is signed
  238. */
  239. public boolean isSigned() {
  240. return signed;
  241. }
  242. /**
  243. * If set to true the Tag command creates a signed tag object. This
  244. * corresponds to the parameter -s on the command line.
  245. *
  246. * @param signed
  247. * @return {@code this}
  248. */
  249. public TagCommand setSigned(boolean signed) {
  250. this.signed = signed;
  251. return this;
  252. }
  253. /**
  254. * Sets the tagger of the tag. If the tagger is null, a PersonIdent will be
  255. * created from the info in the repository.
  256. *
  257. * @param tagger
  258. * @return {@code this}
  259. */
  260. public TagCommand setTagger(PersonIdent tagger) {
  261. this.tagger = tagger;
  262. return this;
  263. }
  264. /**
  265. * @return the tagger of the tag
  266. */
  267. public PersonIdent getTagger() {
  268. return tagger;
  269. }
  270. /**
  271. * @return the object id of the tag
  272. */
  273. public RevObject getObjectId() {
  274. return id;
  275. }
  276. /**
  277. * Sets the object id of the tag. If the object id is null, the commit
  278. * pointed to from HEAD will be used.
  279. *
  280. * @param id
  281. * @return {@code this}
  282. */
  283. public TagCommand setObjectId(RevObject id) {
  284. this.id = id;
  285. return this;
  286. }
  287. /**
  288. * @return is this a force update
  289. */
  290. public boolean isForceUpdate() {
  291. return forceUpdate;
  292. }
  293. /**
  294. * If set to true the Tag command may replace an existing tag object. This
  295. * corresponds to the parameter -f on the command line.
  296. *
  297. * @param forceUpdate
  298. * @return {@code this}
  299. */
  300. public TagCommand setForceUpdate(boolean forceUpdate) {
  301. this.forceUpdate = forceUpdate;
  302. return this;
  303. }
  304. /**
  305. * @param annotated
  306. * @return {@code this}
  307. * @since 3.0
  308. */
  309. public TagCommand setAnnotated(boolean annotated) {
  310. this.annotated = annotated;
  311. return this;
  312. }
  313. /**
  314. * @return true if this command will create an annotated tag (default is
  315. * true)
  316. * @since 3.0
  317. */
  318. public boolean isAnnotated() {
  319. return annotated;
  320. }
  321. }