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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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, 2020, 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 java.io.UnsupportedEncodingException;
  18. import java.nio.charset.Charset;
  19. import org.eclipse.jgit.api.errors.JGitInternalException;
  20. import org.eclipse.jgit.internal.JGitText;
  21. import org.eclipse.jgit.revwalk.RevObject;
  22. import org.eclipse.jgit.util.References;
  23. /**
  24. * Mutable builder to construct an annotated tag recording a project state.
  25. *
  26. * Applications should use this object when they need to manually construct a
  27. * tag and want precise control over its fields.
  28. *
  29. * To read a tag object, construct a {@link org.eclipse.jgit.revwalk.RevWalk}
  30. * and obtain a {@link org.eclipse.jgit.revwalk.RevTag} instance by calling
  31. * {@link org.eclipse.jgit.revwalk.RevWalk#parseTag(AnyObjectId)}.
  32. */
  33. public class TagBuilder extends ObjectBuilder {
  34. private static final byte[] hobject = Constants.encodeASCII("object"); //$NON-NLS-1$
  35. private static final byte[] htype = Constants.encodeASCII("type"); //$NON-NLS-1$
  36. private static final byte[] htag = Constants.encodeASCII("tag"); //$NON-NLS-1$
  37. private static final byte[] htagger = Constants.encodeASCII("tagger"); //$NON-NLS-1$
  38. private ObjectId object;
  39. private int type = Constants.OBJ_BAD;
  40. private String tag;
  41. /**
  42. * Get the type of object this tag refers to.
  43. *
  44. * @return the type of object this tag refers to.
  45. */
  46. public int getObjectType() {
  47. return type;
  48. }
  49. /**
  50. * Get the object this tag refers to.
  51. *
  52. * @return the object this tag refers to.
  53. */
  54. public ObjectId getObjectId() {
  55. return object;
  56. }
  57. /**
  58. * Set the object this tag refers to, and its type.
  59. *
  60. * @param obj
  61. * the object.
  62. * @param objType
  63. * the type of {@code obj}. Must be a valid type code.
  64. */
  65. public void setObjectId(AnyObjectId obj, int objType) {
  66. object = obj.copy();
  67. type = objType;
  68. }
  69. /**
  70. * Set the object this tag refers to, and infer its type.
  71. *
  72. * @param obj
  73. * the object the tag will refer to.
  74. */
  75. public void setObjectId(RevObject obj) {
  76. setObjectId(obj, obj.getType());
  77. }
  78. /**
  79. * Get short name of the tag (no {@code refs/tags/} prefix).
  80. *
  81. * @return short name of the tag (no {@code refs/tags/} prefix).
  82. */
  83. public String getTag() {
  84. return tag;
  85. }
  86. /**
  87. * Set the name of this tag.
  88. *
  89. * @param shortName
  90. * new short name of the tag. This short name should not start
  91. * with {@code refs/} as typically a tag is stored under the
  92. * reference derived from {@code "refs/tags/" + getTag()}.
  93. */
  94. public void setTag(String shortName) {
  95. this.tag = shortName;
  96. }
  97. /**
  98. * Get creator of this tag.
  99. *
  100. * @return creator of this tag. May be null.
  101. */
  102. public PersonIdent getTagger() {
  103. return getAuthor();
  104. }
  105. /**
  106. * Set the creator of this tag.
  107. *
  108. * @param taggerIdent
  109. * the creator. May be null.
  110. */
  111. public void setTagger(PersonIdent taggerIdent) {
  112. setAuthor(taggerIdent);
  113. }
  114. /**
  115. * Format this builder's state as an annotated tag object.
  116. *
  117. * @return this object in the canonical annotated tag format, suitable for
  118. * storage in a repository.
  119. */
  120. @Override
  121. public byte[] build() throws UnsupportedEncodingException {
  122. ByteArrayOutputStream os = new ByteArrayOutputStream();
  123. try (OutputStreamWriter w = new OutputStreamWriter(os,
  124. getEncoding())) {
  125. os.write(hobject);
  126. os.write(' ');
  127. getObjectId().copyTo(os);
  128. os.write('\n');
  129. os.write(htype);
  130. os.write(' ');
  131. os.write(Constants
  132. .encodeASCII(Constants.typeString(getObjectType())));
  133. os.write('\n');
  134. os.write(htag);
  135. os.write(' ');
  136. w.write(getTag());
  137. w.flush();
  138. os.write('\n');
  139. if (getTagger() != null) {
  140. os.write(htagger);
  141. os.write(' ');
  142. w.write(getTagger().toExternalString());
  143. w.flush();
  144. os.write('\n');
  145. }
  146. writeEncoding(getEncoding(), os);
  147. os.write('\n');
  148. String msg = getMessage();
  149. if (msg != null) {
  150. w.write(msg);
  151. w.flush();
  152. }
  153. GpgSignature signature = getGpgSignature();
  154. if (signature != null) {
  155. if (msg != null && !msg.isEmpty() && !msg.endsWith("\n")) { //$NON-NLS-1$
  156. // If signed, the message *must* end with a linefeed
  157. // character, otherwise signature verification will fail.
  158. // (The signature will have been computed over the payload
  159. // containing the message without LF, but will be verified
  160. // against a payload with the LF.) The signature must start
  161. // on a new line.
  162. throw new JGitInternalException(
  163. JGitText.get().signedTagMessageNoLf);
  164. }
  165. String externalForm = signature.toExternalString();
  166. w.write(externalForm);
  167. w.flush();
  168. if (!externalForm.endsWith("\n")) { //$NON-NLS-1$
  169. os.write('\n');
  170. }
  171. }
  172. } catch (IOException err) {
  173. // This should never occur, the only way to get it above is
  174. // for the ByteArrayOutputStream to throw, but it doesn't.
  175. //
  176. throw new RuntimeException(err);
  177. }
  178. return os.toByteArray();
  179. }
  180. /**
  181. * Format this builder's state as an annotated tag object.
  182. *
  183. * @return this object in the canonical annotated tag format, suitable for
  184. * storage in a repository, or {@code null} if the tag cannot be
  185. * encoded
  186. * @deprecated since 5.11; use {@link #build()} instead
  187. */
  188. @Deprecated
  189. public byte[] toByteArray() {
  190. try {
  191. return build();
  192. } catch (UnsupportedEncodingException e) {
  193. return null;
  194. }
  195. }
  196. /** {@inheritDoc} */
  197. @SuppressWarnings("nls")
  198. @Override
  199. public String toString() {
  200. StringBuilder r = new StringBuilder();
  201. r.append("Tag");
  202. r.append("={\n");
  203. r.append("object ");
  204. r.append(object != null ? object.name() : "NOT_SET");
  205. r.append("\n");
  206. r.append("type ");
  207. r.append(object != null ? Constants.typeString(type) : "NOT_SET");
  208. r.append("\n");
  209. r.append("tag ");
  210. r.append(tag != null ? tag : "NOT_SET");
  211. r.append("\n");
  212. if (getTagger() != null) {
  213. r.append("tagger ");
  214. r.append(getTagger());
  215. r.append("\n");
  216. }
  217. Charset encoding = getEncoding();
  218. if (!References.isSameObject(encoding, UTF_8)) {
  219. r.append("encoding ");
  220. r.append(encoding.name());
  221. r.append("\n");
  222. }
  223. r.append("\n");
  224. r.append(getMessage() != null ? getMessage() : "");
  225. GpgSignature signature = getGpgSignature();
  226. r.append(signature != null ? signature.toExternalString() : "");
  227. r.append("}");
  228. return r.toString();
  229. }
  230. }