選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

TagBuilder.java 6.3KB

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