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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 static java.nio.charset.StandardCharsets.UTF_8;
  47. import java.io.ByteArrayOutputStream;
  48. import java.io.IOException;
  49. import java.io.OutputStreamWriter;
  50. import org.eclipse.jgit.revwalk.RevObject;
  51. /**
  52. * Mutable builder to construct an annotated tag recording a project state.
  53. *
  54. * Applications should use this object when they need to manually construct a
  55. * tag and want precise control over its fields.
  56. *
  57. * To read a tag object, construct a {@link org.eclipse.jgit.revwalk.RevWalk}
  58. * and obtain a {@link org.eclipse.jgit.revwalk.RevTag} instance by calling
  59. * {@link org.eclipse.jgit.revwalk.RevWalk#parseTag(AnyObjectId)}.
  60. */
  61. public class TagBuilder {
  62. private ObjectId object;
  63. private int type = Constants.OBJ_BAD;
  64. private String tag;
  65. private PersonIdent tagger;
  66. private String message;
  67. /**
  68. * Get the type of object this tag refers to.
  69. *
  70. * @return the type of object this tag refers to.
  71. */
  72. public int getObjectType() {
  73. return type;
  74. }
  75. /**
  76. * Get the object this tag refers to.
  77. *
  78. * @return the object this tag refers to.
  79. */
  80. public ObjectId getObjectId() {
  81. return object;
  82. }
  83. /**
  84. * Set the object this tag refers to, and its type.
  85. *
  86. * @param obj
  87. * the object.
  88. * @param objType
  89. * the type of {@code obj}. Must be a valid type code.
  90. */
  91. public void setObjectId(AnyObjectId obj, int objType) {
  92. object = obj.copy();
  93. type = objType;
  94. }
  95. /**
  96. * Set the object this tag refers to, and infer its type.
  97. *
  98. * @param obj
  99. * the object the tag will refer to.
  100. */
  101. public void setObjectId(RevObject obj) {
  102. setObjectId(obj, obj.getType());
  103. }
  104. /**
  105. * Get short name of the tag (no {@code refs/tags/} prefix).
  106. *
  107. * @return short name of the tag (no {@code refs/tags/} prefix).
  108. */
  109. public String getTag() {
  110. return tag;
  111. }
  112. /**
  113. * Set the name of this tag.
  114. *
  115. * @param shortName
  116. * new short name of the tag. This short name should not start
  117. * with {@code refs/} as typically a tag is stored under the
  118. * reference derived from {@code "refs/tags/" + getTag()}.
  119. */
  120. public void setTag(String shortName) {
  121. this.tag = shortName;
  122. }
  123. /**
  124. * Get creator of this tag.
  125. *
  126. * @return creator of this tag. May be null.
  127. */
  128. public PersonIdent getTagger() {
  129. return tagger;
  130. }
  131. /**
  132. * Set the creator of this tag.
  133. *
  134. * @param taggerIdent
  135. * the creator. May be null.
  136. */
  137. public void setTagger(PersonIdent taggerIdent) {
  138. tagger = taggerIdent;
  139. }
  140. /**
  141. * Get the complete commit message.
  142. *
  143. * @return the complete commit message.
  144. */
  145. public String getMessage() {
  146. return message;
  147. }
  148. /**
  149. * Set the tag's message.
  150. *
  151. * @param newMessage
  152. * the tag's message.
  153. */
  154. public void setMessage(String newMessage) {
  155. message = newMessage;
  156. }
  157. /**
  158. * Format this builder's state as an annotated tag object.
  159. *
  160. * @return this object in the canonical annotated tag format, suitable for
  161. * storage in a repository.
  162. */
  163. public byte[] build() {
  164. ByteArrayOutputStream os = new ByteArrayOutputStream();
  165. try (OutputStreamWriter w = new OutputStreamWriter(os,
  166. UTF_8)) {
  167. w.write("object "); //$NON-NLS-1$
  168. getObjectId().copyTo(w);
  169. w.write('\n');
  170. w.write("type "); //$NON-NLS-1$
  171. w.write(Constants.typeString(getObjectType()));
  172. w.write("\n"); //$NON-NLS-1$
  173. w.write("tag "); //$NON-NLS-1$
  174. w.write(getTag());
  175. w.write("\n"); //$NON-NLS-1$
  176. if (getTagger() != null) {
  177. w.write("tagger "); //$NON-NLS-1$
  178. w.write(getTagger().toExternalString());
  179. w.write('\n');
  180. }
  181. w.write('\n');
  182. if (getMessage() != null)
  183. w.write(getMessage());
  184. } catch (IOException err) {
  185. // This should never occur, the only way to get it above is
  186. // for the ByteArrayOutputStream to throw, but it doesn't.
  187. //
  188. throw new RuntimeException(err);
  189. }
  190. return os.toByteArray();
  191. }
  192. /**
  193. * Format this builder's state as an annotated tag object.
  194. *
  195. * @return this object in the canonical annotated tag format, suitable for
  196. * storage in a repository.
  197. */
  198. public byte[] toByteArray() {
  199. return build();
  200. }
  201. /** {@inheritDoc} */
  202. @SuppressWarnings("nls")
  203. @Override
  204. public String toString() {
  205. StringBuilder r = new StringBuilder();
  206. r.append("Tag");
  207. r.append("={\n");
  208. r.append("object ");
  209. r.append(object != null ? object.name() : "NOT_SET");
  210. r.append("\n");
  211. r.append("type ");
  212. r.append(object != null ? Constants.typeString(type) : "NOT_SET");
  213. r.append("\n");
  214. r.append("tag ");
  215. r.append(tag != null ? tag : "NOT_SET");
  216. r.append("\n");
  217. if (tagger != null) {
  218. r.append("tagger ");
  219. r.append(tagger);
  220. r.append("\n");
  221. }
  222. r.append("\n");
  223. r.append(message != null ? message : "");
  224. r.append("}");
  225. return r.toString();
  226. }
  227. }