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

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