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

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