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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * <p>
  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. * <p>
  90. * The body of the tag (message, tagger, signature) is always retained in
  91. * the returned {@code RevTag}, even if the supplied {@code RevWalk} has
  92. * been configured with {@code setRetainBody(false)}.
  93. *
  94. * @param rw
  95. * the revision pool to allocate the tag within. The tag's object
  96. * pointer will be obtained from this pool.
  97. * @param raw
  98. * the canonical formatted tag to be parsed. This buffer will be
  99. * retained by the returned {@code RevTag} and must not be
  100. * modified by the caller.
  101. * @return the parsed tag, in an isolated revision pool that is not
  102. * available to the caller.
  103. * @throws CorruptObjectException
  104. * the tag contains a malformed header that cannot be handled.
  105. */
  106. public static RevTag parse(RevWalk rw, byte[] raw)
  107. throws CorruptObjectException {
  108. try (ObjectInserter.Formatter fmt = new ObjectInserter.Formatter()) {
  109. RevTag r = rw.lookupTag(fmt.idFor(Constants.OBJ_TAG, raw));
  110. r.parseCanonical(rw, raw);
  111. r.buffer = raw;
  112. return r;
  113. }
  114. }
  115. private RevObject object;
  116. private byte[] buffer;
  117. private String tagName;
  118. /**
  119. * Create a new tag reference.
  120. *
  121. * @param id
  122. * object name for the tag.
  123. */
  124. protected RevTag(final AnyObjectId id) {
  125. super(id);
  126. }
  127. @Override
  128. void parseHeaders(final RevWalk walk) throws MissingObjectException,
  129. IncorrectObjectTypeException, IOException {
  130. parseCanonical(walk, walk.getCachedBytes(this));
  131. }
  132. @Override
  133. void parseBody(final RevWalk walk) throws MissingObjectException,
  134. IncorrectObjectTypeException, IOException {
  135. if (buffer == null) {
  136. buffer = walk.getCachedBytes(this);
  137. if ((flags & PARSED) == 0)
  138. parseCanonical(walk, buffer);
  139. }
  140. }
  141. void parseCanonical(final RevWalk walk, final byte[] rawTag)
  142. throws CorruptObjectException {
  143. final MutableInteger pos = new MutableInteger();
  144. final int oType;
  145. pos.value = 53; // "object $sha1\ntype "
  146. oType = Constants.decodeTypeString(this, rawTag, (byte) '\n', pos);
  147. walk.idBuffer.fromString(rawTag, 7);
  148. object = walk.lookupAny(walk.idBuffer, oType);
  149. int p = pos.value += 4; // "tag "
  150. final int nameEnd = RawParseUtils.nextLF(rawTag, p) - 1;
  151. tagName = RawParseUtils.decode(Constants.CHARSET, rawTag, p, nameEnd);
  152. if (walk.isRetainBody())
  153. buffer = rawTag;
  154. flags |= PARSED;
  155. }
  156. @Override
  157. public final int getType() {
  158. return Constants.OBJ_TAG;
  159. }
  160. /**
  161. * Parse the tagger identity from the raw buffer.
  162. * <p>
  163. * This method parses and returns the content of the tagger line, after
  164. * taking the tag's character set into account and decoding the tagger
  165. * name and email address. This method is fairly expensive and produces a
  166. * new PersonIdent instance on each invocation. Callers should invoke this
  167. * method only if they are certain they will be outputting the result, and
  168. * should cache the return value for as long as necessary to use all
  169. * information from it.
  170. *
  171. * @return identity of the tagger (name, email) and the time the tag
  172. * was made by the tagger; null if no tagger line was found.
  173. */
  174. public final PersonIdent getTaggerIdent() {
  175. final byte[] raw = buffer;
  176. final int nameB = RawParseUtils.tagger(raw, 0);
  177. if (nameB < 0)
  178. return null;
  179. return RawParseUtils.parsePersonIdent(raw, nameB);
  180. }
  181. /**
  182. * Parse the complete tag message and decode it to a string.
  183. * <p>
  184. * This method parses and returns the message portion of the tag buffer,
  185. * after taking the tag's character set into account and decoding the buffer
  186. * using that character set. This method is a fairly expensive operation and
  187. * produces a new string on each invocation.
  188. *
  189. * @return decoded tag message as a string. Never null.
  190. */
  191. public final String getFullMessage() {
  192. final byte[] raw = buffer;
  193. final int msgB = RawParseUtils.tagMessage(raw, 0);
  194. if (msgB < 0)
  195. return ""; //$NON-NLS-1$
  196. final Charset enc = RawParseUtils.parseEncoding(raw);
  197. return RawParseUtils.decode(enc, raw, msgB, raw.length);
  198. }
  199. /**
  200. * Parse the tag message and return the first "line" of it.
  201. * <p>
  202. * The first line is everything up to the first pair of LFs. This is the
  203. * "oneline" format, suitable for output in a single line display.
  204. * <p>
  205. * This method parses and returns the message portion of the tag buffer,
  206. * after taking the tag's character set into account and decoding the buffer
  207. * using that character set. This method is a fairly expensive operation and
  208. * produces a new string on each invocation.
  209. *
  210. * @return decoded tag message as a string. Never null. The returned string
  211. * does not contain any LFs, even if the first paragraph spanned
  212. * multiple lines. Embedded LFs are converted to spaces.
  213. */
  214. public final String getShortMessage() {
  215. final byte[] raw = buffer;
  216. final int msgB = RawParseUtils.tagMessage(raw, 0);
  217. if (msgB < 0)
  218. return ""; //$NON-NLS-1$
  219. final Charset enc = RawParseUtils.parseEncoding(raw);
  220. final int msgE = RawParseUtils.endOfParagraph(raw, msgB);
  221. String str = RawParseUtils.decode(enc, raw, msgB, msgE);
  222. if (RevCommit.hasLF(raw, msgB, msgE))
  223. str = StringUtils.replaceLineBreaksWithSpace(str);
  224. return str;
  225. }
  226. /**
  227. * Get a reference to the object this tag was placed on.
  228. * <p>
  229. * Note that the returned object has only been looked up (see
  230. * {@link RevWalk#lookupAny(AnyObjectId, int)}. To access the contents it
  231. * needs to be parsed, see {@link RevWalk#parseHeaders(RevObject)} and
  232. * {@link RevWalk#parseBody(RevObject)}.
  233. * <p>
  234. * As an alternative, use {@link RevWalk#peel(RevObject)} and pass this
  235. * {@link RevTag} to peel it until the first non-tag object.
  236. *
  237. * @return object this tag refers to (only looked up, not parsed)
  238. */
  239. public final RevObject getObject() {
  240. return object;
  241. }
  242. /**
  243. * Get the name of this tag, from the tag header.
  244. *
  245. * @return name of the tag, according to the tag header.
  246. */
  247. public final String getTagName() {
  248. return tagName;
  249. }
  250. /**
  251. * Discard the message buffer to reduce memory usage.
  252. * <p>
  253. * After discarding the memory usage of the {@code RevTag} is reduced to
  254. * only the {@link #getObject()} pointer and {@link #getTagName()}.
  255. * Accessing other properties such as {@link #getTaggerIdent()} or either
  256. * message function requires reloading the buffer by invoking
  257. * {@link RevWalk#parseBody(RevObject)}.
  258. *
  259. * @since 4.0
  260. */
  261. public final void disposeBody() {
  262. buffer = null;
  263. }
  264. }