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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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> and others
  5. *
  6. * This program and the accompanying materials are made available under the
  7. * terms of the Eclipse Distribution License v. 1.0 which is available at
  8. * https://www.eclipse.org/org/documents/edl-v10.php.
  9. *
  10. * SPDX-License-Identifier: BSD-3-Clause
  11. */
  12. package org.eclipse.jgit.lib;
  13. import static java.nio.charset.StandardCharsets.UTF_8;
  14. import java.io.ByteArrayOutputStream;
  15. import java.io.IOException;
  16. import java.io.OutputStreamWriter;
  17. import org.eclipse.jgit.revwalk.RevObject;
  18. /**
  19. * Mutable builder to construct an annotated tag recording a project state.
  20. *
  21. * Applications should use this object when they need to manually construct a
  22. * tag and want precise control over its fields.
  23. *
  24. * To read a tag object, construct a {@link org.eclipse.jgit.revwalk.RevWalk}
  25. * and obtain a {@link org.eclipse.jgit.revwalk.RevTag} instance by calling
  26. * {@link org.eclipse.jgit.revwalk.RevWalk#parseTag(AnyObjectId)}.
  27. */
  28. public class TagBuilder {
  29. private ObjectId object;
  30. private int type = Constants.OBJ_BAD;
  31. private String tag;
  32. private PersonIdent tagger;
  33. private String message;
  34. /**
  35. * Get the type of object this tag refers to.
  36. *
  37. * @return the type of object this tag refers to.
  38. */
  39. public int getObjectType() {
  40. return type;
  41. }
  42. /**
  43. * Get the object this tag refers to.
  44. *
  45. * @return the object this tag refers to.
  46. */
  47. public ObjectId getObjectId() {
  48. return object;
  49. }
  50. /**
  51. * Set the object this tag refers to, and its type.
  52. *
  53. * @param obj
  54. * the object.
  55. * @param objType
  56. * the type of {@code obj}. Must be a valid type code.
  57. */
  58. public void setObjectId(AnyObjectId obj, int objType) {
  59. object = obj.copy();
  60. type = objType;
  61. }
  62. /**
  63. * Set the object this tag refers to, and infer its type.
  64. *
  65. * @param obj
  66. * the object the tag will refer to.
  67. */
  68. public void setObjectId(RevObject obj) {
  69. setObjectId(obj, obj.getType());
  70. }
  71. /**
  72. * Get short name of the tag (no {@code refs/tags/} prefix).
  73. *
  74. * @return short name of the tag (no {@code refs/tags/} prefix).
  75. */
  76. public String getTag() {
  77. return tag;
  78. }
  79. /**
  80. * Set the name of this tag.
  81. *
  82. * @param shortName
  83. * new short name of the tag. This short name should not start
  84. * with {@code refs/} as typically a tag is stored under the
  85. * reference derived from {@code "refs/tags/" + getTag()}.
  86. */
  87. public void setTag(String shortName) {
  88. this.tag = shortName;
  89. }
  90. /**
  91. * Get creator of this tag.
  92. *
  93. * @return creator of this tag. May be null.
  94. */
  95. public PersonIdent getTagger() {
  96. return tagger;
  97. }
  98. /**
  99. * Set the creator of this tag.
  100. *
  101. * @param taggerIdent
  102. * the creator. May be null.
  103. */
  104. public void setTagger(PersonIdent taggerIdent) {
  105. tagger = taggerIdent;
  106. }
  107. /**
  108. * Get the complete commit message.
  109. *
  110. * @return the complete commit message.
  111. */
  112. public String getMessage() {
  113. return message;
  114. }
  115. /**
  116. * Set the tag's message.
  117. *
  118. * @param newMessage
  119. * the tag's message.
  120. */
  121. public void setMessage(String newMessage) {
  122. message = newMessage;
  123. }
  124. /**
  125. * Format this builder's state as an annotated tag object.
  126. *
  127. * @return this object in the canonical annotated tag format, suitable for
  128. * storage in a repository.
  129. */
  130. public byte[] build() {
  131. ByteArrayOutputStream os = new ByteArrayOutputStream();
  132. try (OutputStreamWriter w = new OutputStreamWriter(os,
  133. UTF_8)) {
  134. w.write("object "); //$NON-NLS-1$
  135. getObjectId().copyTo(w);
  136. w.write('\n');
  137. w.write("type "); //$NON-NLS-1$
  138. w.write(Constants.typeString(getObjectType()));
  139. w.write("\n"); //$NON-NLS-1$
  140. w.write("tag "); //$NON-NLS-1$
  141. w.write(getTag());
  142. w.write("\n"); //$NON-NLS-1$
  143. if (getTagger() != null) {
  144. w.write("tagger "); //$NON-NLS-1$
  145. w.write(getTagger().toExternalString());
  146. w.write('\n');
  147. }
  148. w.write('\n');
  149. if (getMessage() != null)
  150. w.write(getMessage());
  151. } catch (IOException err) {
  152. // This should never occur, the only way to get it above is
  153. // for the ByteArrayOutputStream to throw, but it doesn't.
  154. //
  155. throw new RuntimeException(err);
  156. }
  157. return os.toByteArray();
  158. }
  159. /**
  160. * Format this builder's state as an annotated tag object.
  161. *
  162. * @return this object in the canonical annotated tag format, suitable for
  163. * storage in a repository.
  164. */
  165. public byte[] toByteArray() {
  166. return build();
  167. }
  168. /** {@inheritDoc} */
  169. @SuppressWarnings("nls")
  170. @Override
  171. public String toString() {
  172. StringBuilder r = new StringBuilder();
  173. r.append("Tag");
  174. r.append("={\n");
  175. r.append("object ");
  176. r.append(object != null ? object.name() : "NOT_SET");
  177. r.append("\n");
  178. r.append("type ");
  179. r.append(object != null ? Constants.typeString(type) : "NOT_SET");
  180. r.append("\n");
  181. r.append("tag ");
  182. r.append(tag != null ? tag : "NOT_SET");
  183. r.append("\n");
  184. if (tagger != null) {
  185. r.append("tagger ");
  186. r.append(tagger);
  187. r.append("\n");
  188. }
  189. r.append("\n");
  190. r.append(message != null ? message : "");
  191. r.append("}");
  192. return r.toString();
  193. }
  194. }