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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. RevWalk revWalk = new RevWalk(repo);
  119. try {
  120. // if no id is set, we should attempt to use HEAD
  121. if (id == null) {
  122. ObjectId objectId = repo.resolve(Constants.HEAD + "^{commit}"); //$NON-NLS-1$
  123. if (objectId == null)
  124. throw new NoHeadException(
  125. JGitText.get().tagOnRepoWithoutHEADCurrentlyNotSupported);
  126. id = revWalk.parseCommit(objectId);
  127. }
  128. if (!annotated) {
  129. if (message != null || tagger != null)
  130. throw new JGitInternalException(
  131. JGitText.get().messageAndTaggerNotAllowedInUnannotatedTags);
  132. return updateTagRef(id, revWalk, name,
  133. "SimpleTag[" + name + " : " + id //$NON-NLS-1$ //$NON-NLS-2$
  134. + "]"); //$NON-NLS-1$
  135. }
  136. // create the tag object
  137. TagBuilder newTag = new TagBuilder();
  138. newTag.setTag(name);
  139. newTag.setMessage(message);
  140. newTag.setTagger(tagger);
  141. newTag.setObjectId(id);
  142. // write the tag object
  143. ObjectInserter inserter = repo.newObjectInserter();
  144. try {
  145. ObjectId tagId = inserter.insert(newTag);
  146. inserter.flush();
  147. String tag = newTag.getTag();
  148. return updateTagRef(tagId, revWalk, tag, newTag.toString());
  149. } finally {
  150. inserter.release();
  151. }
  152. } catch (IOException e) {
  153. throw new JGitInternalException(
  154. JGitText.get().exceptionCaughtDuringExecutionOfTagCommand,
  155. e);
  156. } finally {
  157. revWalk.release();
  158. }
  159. }
  160. private Ref updateTagRef(ObjectId tagId, RevWalk revWalk,
  161. String tagName, String newTagToString) throws IOException,
  162. ConcurrentRefUpdateException, RefAlreadyExistsException {
  163. String refName = Constants.R_TAGS + tagName;
  164. RefUpdate tagRef = repo.updateRef(refName);
  165. tagRef.setNewObjectId(tagId);
  166. tagRef.setForceUpdate(forceUpdate);
  167. tagRef.setRefLogMessage("tagged " + name, false); //$NON-NLS-1$
  168. Result updateResult = tagRef.update(revWalk);
  169. switch (updateResult) {
  170. case NEW:
  171. case FORCED:
  172. return repo.getRef(refName);
  173. case LOCK_FAILURE:
  174. throw new ConcurrentRefUpdateException(
  175. JGitText.get().couldNotLockHEAD, tagRef.getRef(),
  176. updateResult);
  177. case REJECTED:
  178. throw new RefAlreadyExistsException(MessageFormat.format(
  179. JGitText.get().tagAlreadyExists, newTagToString));
  180. default:
  181. throw new JGitInternalException(MessageFormat.format(
  182. JGitText.get().updatingRefFailed, refName, newTagToString,
  183. updateResult));
  184. }
  185. }
  186. /**
  187. * Sets default values for not explicitly specified options. Then validates
  188. * that all required data has been provided.
  189. *
  190. * @param state
  191. * the state of the repository we are working on
  192. *
  193. * @throws InvalidTagNameException
  194. * if the tag name is null or invalid
  195. * @throws UnsupportedOperationException
  196. * if the tag is signed (not supported yet)
  197. */
  198. private void processOptions(RepositoryState state)
  199. throws InvalidTagNameException {
  200. if (tagger == null && annotated)
  201. tagger = new PersonIdent(repo);
  202. if (name == null || !Repository.isValidRefName(Constants.R_TAGS + name))
  203. throw new InvalidTagNameException(MessageFormat.format(JGitText
  204. .get().tagNameInvalid, name == null ? "<null>" : name));
  205. if (signed)
  206. throw new UnsupportedOperationException(
  207. JGitText.get().signingNotSupportedOnTag);
  208. }
  209. /**
  210. * @param name
  211. * the tag name used for the {@code tag}
  212. * @return {@code this}
  213. */
  214. public TagCommand setName(String name) {
  215. checkCallable();
  216. this.name = name;
  217. return this;
  218. }
  219. /**
  220. * @return the tag name used for the <code>tag</code>
  221. */
  222. public String getName() {
  223. return name;
  224. }
  225. /**
  226. * @return the tag message used for the <code>tag</code>
  227. */
  228. public String getMessage() {
  229. return message;
  230. }
  231. /**
  232. * @param message
  233. * the tag message used for the {@code tag}
  234. * @return {@code this}
  235. */
  236. public TagCommand setMessage(String message) {
  237. checkCallable();
  238. this.message = message;
  239. return this;
  240. }
  241. /**
  242. * @return whether the tag is signed
  243. */
  244. public boolean isSigned() {
  245. return signed;
  246. }
  247. /**
  248. * If set to true the Tag command creates a signed tag object. This
  249. * corresponds to the parameter -s on the command line.
  250. *
  251. * @param signed
  252. * @return {@code this}
  253. */
  254. public TagCommand setSigned(boolean signed) {
  255. this.signed = signed;
  256. return this;
  257. }
  258. /**
  259. * Sets the tagger of the tag. If the tagger is null, a PersonIdent will be
  260. * created from the info in the repository.
  261. *
  262. * @param tagger
  263. * @return {@code this}
  264. */
  265. public TagCommand setTagger(PersonIdent tagger) {
  266. this.tagger = tagger;
  267. return this;
  268. }
  269. /**
  270. * @return the tagger of the tag
  271. */
  272. public PersonIdent getTagger() {
  273. return tagger;
  274. }
  275. /**
  276. * @return the object id of the tag
  277. */
  278. public RevObject getObjectId() {
  279. return id;
  280. }
  281. /**
  282. * Sets the object id of the tag. If the object id is null, the commit
  283. * pointed to from HEAD will be used.
  284. *
  285. * @param id
  286. * @return {@code this}
  287. */
  288. public TagCommand setObjectId(RevObject id) {
  289. this.id = id;
  290. return this;
  291. }
  292. /**
  293. * @return is this a force update
  294. */
  295. public boolean isForceUpdate() {
  296. return forceUpdate;
  297. }
  298. /**
  299. * If set to true the Tag command may replace an existing tag object. This
  300. * corresponds to the parameter -f on the command line.
  301. *
  302. * @param forceUpdate
  303. * @return {@code this}
  304. */
  305. public TagCommand setForceUpdate(boolean forceUpdate) {
  306. this.forceUpdate = forceUpdate;
  307. return this;
  308. }
  309. /**
  310. * @param annotated
  311. * @return {@code this}
  312. * @since 3.0
  313. */
  314. public TagCommand setAnnotated(boolean annotated) {
  315. this.annotated = annotated;
  316. return this;
  317. }
  318. /**
  319. * @return true if this command will create an annotated tag (default is
  320. * true)
  321. * @since 3.0
  322. */
  323. public boolean isAnnotated() {
  324. return annotated;
  325. }
  326. }