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

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