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.

TagBuilder.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (C) 2006-2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.lib;
  46. import java.io.ByteArrayOutputStream;
  47. import java.io.IOException;
  48. import java.io.OutputStreamWriter;
  49. import org.eclipse.jgit.revwalk.RevObject;
  50. /**
  51. * Mutable builder to construct an annotated tag recording a project state.
  52. *
  53. * Applications should use this object when they need to manually construct a
  54. * tag and want precise control over its fields.
  55. *
  56. * To read a tag object, construct a {@link org.eclipse.jgit.revwalk.RevWalk}
  57. * and obtain a {@link org.eclipse.jgit.revwalk.RevTag} instance by calling
  58. * {@link org.eclipse.jgit.revwalk.RevWalk#parseTag(AnyObjectId)}.
  59. */
  60. public class TagBuilder {
  61. private ObjectId tagId;
  62. private ObjectId object;
  63. private int type = Constants.OBJ_BAD;
  64. private String tag;
  65. private PersonIdent tagger;
  66. private String message;
  67. /** @return this tag's object id. */
  68. public ObjectId getTagId() {
  69. return tagId;
  70. }
  71. /**
  72. * Set the id of this tag object.
  73. *
  74. * @param id
  75. * the id that we calculated for this object.
  76. */
  77. public void setTagId(ObjectId id) {
  78. tagId = id;
  79. }
  80. /** @return the type of object this tag refers to. */
  81. public int getObjectType() {
  82. return type;
  83. }
  84. /** @return the object this tag refers to. */
  85. public ObjectId getObjectId() {
  86. return object;
  87. }
  88. /**
  89. * Set the object this tag refers to, and its type.
  90. *
  91. * @param obj
  92. * the object.
  93. * @param objType
  94. * the type of {@code obj}. Must be a valid type code.
  95. */
  96. public void setObjectId(AnyObjectId obj, int objType) {
  97. object = obj.copy();
  98. type = objType;
  99. tagId = null;
  100. }
  101. /**
  102. * Set the object this tag refers to, and infer its type.
  103. *
  104. * @param obj
  105. * the object the tag will refer to.
  106. */
  107. public void setObjectId(RevObject obj) {
  108. setObjectId(obj, obj.getType());
  109. }
  110. /** @return short name of the tag (no {@code refs/tags/} prefix). */
  111. public String getTag() {
  112. return tag;
  113. }
  114. /**
  115. * Set the name of this tag.
  116. *
  117. * @param shortName
  118. * new short name of the tag. This short name should not start
  119. * with {@code refs/} as typically a tag is stored under the
  120. * reference derived from {@code "refs/tags/" + getTag()}.
  121. */
  122. public void setTag(String shortName) {
  123. this.tag = shortName;
  124. tagId = null;
  125. }
  126. /** @return creator of this tag. May be null. */
  127. public PersonIdent getTagger() {
  128. return tagger;
  129. }
  130. /**
  131. * Set the creator of this tag.
  132. *
  133. * @param taggerIdent
  134. * the creator. May be null.
  135. */
  136. public void setTagger(PersonIdent taggerIdent) {
  137. tagger = taggerIdent;
  138. tagId = null;
  139. }
  140. /** @return the complete commit message. */
  141. public String getMessage() {
  142. return message;
  143. }
  144. /**
  145. * Set the tag's message.
  146. *
  147. * @param newMessage
  148. * the tag's message.
  149. */
  150. public void setMessage(final String newMessage) {
  151. message = newMessage;
  152. tagId = null;
  153. }
  154. /**
  155. * Format this builder's state as an annotated tag object.
  156. *
  157. * As a side effect, {@link #getTagId()} will be populated with the proper
  158. * ObjectId for the formatted content.
  159. *
  160. * @return this object in the canonical annotated tag format, suitable for
  161. * storage in a repository.
  162. */
  163. public byte[] format() {
  164. return format(new ObjectInserter.Formatter());
  165. }
  166. /**
  167. * Format this builder's state as an annotated tag object.
  168. *
  169. * As a side effect, {@link #getTagId()} will be populated with the proper
  170. * ObjectId for the formatted content.
  171. *
  172. * @param oi
  173. * the inserter whose formatting support will be reused. The
  174. * inserter itself is not affected, and the annotated tag is not
  175. * actually inserted into the repository.
  176. * @return this object in the canonical annotated tag format, suitable for
  177. * storage in a repository.
  178. */
  179. public byte[] format(ObjectInserter oi) {
  180. ByteArrayOutputStream os = new ByteArrayOutputStream();
  181. OutputStreamWriter w = new OutputStreamWriter(os, Constants.CHARSET);
  182. try {
  183. w.write("object ");
  184. getObjectId().copyTo(w);
  185. w.write('\n');
  186. w.write("type ");
  187. w.write(Constants.typeString(getObjectType()));
  188. w.write("\n");
  189. w.write("tag ");
  190. w.write(getTag());
  191. w.write("\n");
  192. if (getTagger() != null) {
  193. w.write("tagger ");
  194. w.write(getTagger().toExternalString());
  195. w.write('\n');
  196. }
  197. w.write('\n');
  198. if (getMessage() != null)
  199. w.write(getMessage());
  200. w.close();
  201. } catch (IOException err) {
  202. // This should never occur, the only way to get it above is
  203. // for the ByteArrayOutputStream to throw, but it doesn't.
  204. //
  205. throw new RuntimeException(err);
  206. }
  207. byte[] content = os.toByteArray();
  208. setTagId(oi.idFor(Constants.OBJ_TAG, content));
  209. return content;
  210. }
  211. @Override
  212. public String toString() {
  213. StringBuilder r = new StringBuilder();
  214. r.append("Tag");
  215. if (tagId != null)
  216. r.append("[" + tagId.name() + "]");
  217. r.append("={\n");
  218. r.append("object ");
  219. r.append(object != null ? object.name() : "NOT_SET");
  220. r.append("\n");
  221. r.append("type ");
  222. r.append(object != null ? Constants.typeString(type) : "NOT_SET");
  223. r.append("\n");
  224. r.append("tag ");
  225. r.append(tag != null ? tag : "NOT_SET");
  226. r.append("\n");
  227. if (tagger != null) {
  228. r.append("tagger ");
  229. r.append(tagger);
  230. r.append("\n");
  231. }
  232. r.append("\n");
  233. r.append(message != null ? message : "");
  234. r.append("}");
  235. return r.toString();
  236. }
  237. }