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

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