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.

RevTag.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  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.revwalk;
  46. import java.io.IOException;
  47. import java.nio.charset.Charset;
  48. import org.eclipse.jgit.errors.CorruptObjectException;
  49. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  50. import org.eclipse.jgit.errors.MissingObjectException;
  51. import org.eclipse.jgit.lib.AnyObjectId;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.eclipse.jgit.lib.ObjectInserter;
  54. import org.eclipse.jgit.lib.ObjectReader;
  55. import org.eclipse.jgit.lib.PersonIdent;
  56. import org.eclipse.jgit.util.MutableInteger;
  57. import org.eclipse.jgit.util.RawParseUtils;
  58. /** An annotated tag. */
  59. public class RevTag extends RevObject {
  60. /**
  61. * Parse an annotated tag from its canonical format.
  62. *
  63. * This method constructs a temporary revision pool, parses the tag as
  64. * supplied, and returns it to the caller. Since the tag was built inside of
  65. * a private revision pool its object pointer will be initialized, but will
  66. * not have its headers loaded.
  67. *
  68. * Applications are discouraged from using this API. Callers usually need
  69. * more than one object. Use {@link RevWalk#parseTag(AnyObjectId)} to obtain
  70. * a RevTag from an existing repository.
  71. *
  72. * @param raw
  73. * the canonical formatted tag to be parsed.
  74. * @return the parsed tag, in an isolated revision pool that is not
  75. * available to the caller.
  76. * @throws CorruptObjectException
  77. * the tag contains a malformed header that cannot be handled.
  78. */
  79. public static RevTag parse(byte[] raw) throws CorruptObjectException {
  80. return parse(new RevWalk((ObjectReader) null), raw);
  81. }
  82. /**
  83. * Parse an annotated tag from its canonical format.
  84. *
  85. * This method inserts the tag directly into the caller supplied revision
  86. * pool, making it appear as though the tag exists in the repository, even
  87. * if it doesn't. The repository under the pool is not affected.
  88. *
  89. * @param rw
  90. * the revision pool to allocate the tag within. The tag's object
  91. * pointer will be obtained from this pool.
  92. * @param raw
  93. * the canonical formatted tag to be parsed.
  94. * @return the parsed tag, in an isolated revision pool that is not
  95. * available to the caller.
  96. * @throws CorruptObjectException
  97. * the tag contains a malformed header that cannot be handled.
  98. */
  99. public static RevTag parse(RevWalk rw, byte[] raw)
  100. throws CorruptObjectException {
  101. ObjectInserter.Formatter fmt = new ObjectInserter.Formatter();
  102. boolean retain = rw.isRetainBody();
  103. rw.setRetainBody(true);
  104. RevTag r = rw.lookupTag(fmt.idFor(Constants.OBJ_TAG, raw));
  105. r.parseCanonical(rw, raw);
  106. rw.setRetainBody(retain);
  107. return r;
  108. }
  109. private RevObject object;
  110. private byte[] buffer;
  111. private String tagName;
  112. /**
  113. * Create a new tag reference.
  114. *
  115. * @param id
  116. * object name for the tag.
  117. */
  118. protected RevTag(final AnyObjectId id) {
  119. super(id);
  120. }
  121. @Override
  122. void parseHeaders(final RevWalk walk) throws MissingObjectException,
  123. IncorrectObjectTypeException, IOException {
  124. parseCanonical(walk, walk.getCachedBytes(this));
  125. }
  126. @Override
  127. void parseBody(final RevWalk walk) throws MissingObjectException,
  128. IncorrectObjectTypeException, IOException {
  129. if (buffer == null) {
  130. buffer = walk.getCachedBytes(this);
  131. if ((flags & PARSED) == 0)
  132. parseCanonical(walk, buffer);
  133. }
  134. }
  135. void parseCanonical(final RevWalk walk, final byte[] rawTag)
  136. throws CorruptObjectException {
  137. final MutableInteger pos = new MutableInteger();
  138. final int oType;
  139. pos.value = 53; // "object $sha1\ntype "
  140. oType = Constants.decodeTypeString(this, rawTag, (byte) '\n', pos);
  141. walk.idBuffer.fromString(rawTag, 7);
  142. object = walk.lookupAny(walk.idBuffer, oType);
  143. int p = pos.value += 4; // "tag "
  144. final int nameEnd = RawParseUtils.nextLF(rawTag, p) - 1;
  145. tagName = RawParseUtils.decode(Constants.CHARSET, rawTag, p, nameEnd);
  146. if (walk.isRetainBody())
  147. buffer = rawTag;
  148. flags |= PARSED;
  149. }
  150. @Override
  151. public final int getType() {
  152. return Constants.OBJ_TAG;
  153. }
  154. /**
  155. * Parse the tagger identity from the raw buffer.
  156. * <p>
  157. * This method parses and returns the content of the tagger line, after
  158. * taking the tag's character set into account and decoding the tagger
  159. * name and email address. This method is fairly expensive and produces a
  160. * new PersonIdent instance on each invocation. Callers should invoke this
  161. * method only if they are certain they will be outputting the result, and
  162. * should cache the return value for as long as necessary to use all
  163. * information from it.
  164. *
  165. * @return identity of the tagger (name, email) and the time the tag
  166. * was made by the tagger; null if no tagger line was found.
  167. */
  168. public final PersonIdent getTaggerIdent() {
  169. final byte[] raw = buffer;
  170. final int nameB = RawParseUtils.tagger(raw, 0);
  171. if (nameB < 0)
  172. return null;
  173. return RawParseUtils.parsePersonIdent(raw, nameB);
  174. }
  175. /**
  176. * Parse the complete tag message and decode it to a string.
  177. * <p>
  178. * This method parses and returns the message portion of the tag buffer,
  179. * after taking the tag's character set into account and decoding the buffer
  180. * using that character set. This method is a fairly expensive operation and
  181. * produces a new string on each invocation.
  182. *
  183. * @return decoded tag message as a string. Never null.
  184. */
  185. public final String getFullMessage() {
  186. final byte[] raw = buffer;
  187. final int msgB = RawParseUtils.tagMessage(raw, 0);
  188. if (msgB < 0)
  189. return "";
  190. final Charset enc = RawParseUtils.parseEncoding(raw);
  191. return RawParseUtils.decode(enc, raw, msgB, raw.length);
  192. }
  193. /**
  194. * Parse the tag message and return the first "line" of it.
  195. * <p>
  196. * The first line is everything up to the first pair of LFs. This is the
  197. * "oneline" format, suitable for output in a single line display.
  198. * <p>
  199. * This method parses and returns the message portion of the tag buffer,
  200. * after taking the tag's character set into account and decoding the buffer
  201. * using that character set. This method is a fairly expensive operation and
  202. * produces a new string on each invocation.
  203. *
  204. * @return decoded tag message as a string. Never null. The returned string
  205. * does not contain any LFs, even if the first paragraph spanned
  206. * multiple lines. Embedded LFs are converted to spaces.
  207. */
  208. public final String getShortMessage() {
  209. final byte[] raw = buffer;
  210. final int msgB = RawParseUtils.tagMessage(raw, 0);
  211. if (msgB < 0)
  212. return "";
  213. final Charset enc = RawParseUtils.parseEncoding(raw);
  214. final int msgE = RawParseUtils.endOfParagraph(raw, msgB);
  215. String str = RawParseUtils.decode(enc, raw, msgB, msgE);
  216. if (RevCommit.hasLF(raw, msgB, msgE))
  217. str = str.replace('\n', ' ');
  218. return str;
  219. }
  220. /**
  221. * Get a reference to the object this tag was placed on.
  222. *
  223. * @return object this tag refers to.
  224. */
  225. public final RevObject getObject() {
  226. return object;
  227. }
  228. /**
  229. * Get the name of this tag, from the tag header.
  230. *
  231. * @return name of the tag, according to the tag header.
  232. */
  233. public final String getTagName() {
  234. return tagName;
  235. }
  236. final void disposeBody() {
  237. buffer = null;
  238. }
  239. }