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

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