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

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