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

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