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

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