Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.PersonIdent;
  54. import org.eclipse.jgit.lib.Tag;
  55. import org.eclipse.jgit.util.MutableInteger;
  56. import org.eclipse.jgit.util.RawParseUtils;
  57. /** An annotated tag. */
  58. public class RevTag extends RevObject {
  59. private RevObject object;
  60. private byte[] buffer;
  61. private String tagName;
  62. /**
  63. * Create a new tag reference.
  64. *
  65. * @param id
  66. * object name for the tag.
  67. */
  68. protected RevTag(final AnyObjectId id) {
  69. super(id);
  70. }
  71. @Override
  72. void parseHeaders(final RevWalk walk) throws MissingObjectException,
  73. IncorrectObjectTypeException, IOException {
  74. parseCanonical(walk, loadCanonical(walk));
  75. }
  76. @Override
  77. void parseBody(final RevWalk walk) throws MissingObjectException,
  78. IncorrectObjectTypeException, IOException {
  79. if (buffer == null) {
  80. buffer = loadCanonical(walk);
  81. if ((flags & PARSED) == 0)
  82. parseCanonical(walk, buffer);
  83. }
  84. }
  85. void parseCanonical(final RevWalk walk, final byte[] rawTag)
  86. throws CorruptObjectException {
  87. final MutableInteger pos = new MutableInteger();
  88. final int oType;
  89. pos.value = 53; // "object $sha1\ntype "
  90. oType = Constants.decodeTypeString(this, rawTag, (byte) '\n', pos);
  91. walk.idBuffer.fromString(rawTag, 7);
  92. object = walk.lookupAny(walk.idBuffer, oType);
  93. int p = pos.value += 4; // "tag "
  94. final int nameEnd = RawParseUtils.nextLF(rawTag, p) - 1;
  95. tagName = RawParseUtils.decode(Constants.CHARSET, rawTag, p, nameEnd);
  96. if (walk.isRetainBody())
  97. buffer = rawTag;
  98. flags |= PARSED;
  99. }
  100. @Override
  101. public final int getType() {
  102. return Constants.OBJ_TAG;
  103. }
  104. /**
  105. * Parse the tagger identity from the raw buffer.
  106. * <p>
  107. * This method parses and returns the content of the tagger line, after
  108. * taking the tag's character set into account and decoding the tagger
  109. * name and email address. This method is fairly expensive and produces a
  110. * new PersonIdent instance on each invocation. Callers should invoke this
  111. * method only if they are certain they will be outputting the result, and
  112. * should cache the return value for as long as necessary to use all
  113. * information from it.
  114. *
  115. * @return identity of the tagger (name, email) and the time the tag
  116. * was made by the tagger; null if no tagger line was found.
  117. */
  118. public final PersonIdent getTaggerIdent() {
  119. final byte[] raw = buffer;
  120. final int nameB = RawParseUtils.tagger(raw, 0);
  121. if (nameB < 0)
  122. return null;
  123. return RawParseUtils.parsePersonIdent(raw, nameB);
  124. }
  125. /**
  126. * Parse the complete tag message and decode it to a string.
  127. * <p>
  128. * This method parses and returns the message portion of the tag buffer,
  129. * after taking the tag's character set into account and decoding the buffer
  130. * using that character set. This method is a fairly expensive operation and
  131. * produces a new string on each invocation.
  132. *
  133. * @return decoded tag message as a string. Never null.
  134. */
  135. public final String getFullMessage() {
  136. final byte[] raw = buffer;
  137. final int msgB = RawParseUtils.tagMessage(raw, 0);
  138. if (msgB < 0)
  139. return "";
  140. final Charset enc = RawParseUtils.parseEncoding(raw);
  141. return RawParseUtils.decode(enc, raw, msgB, raw.length);
  142. }
  143. /**
  144. * Parse the tag message and return the first "line" of it.
  145. * <p>
  146. * The first line is everything up to the first pair of LFs. This is the
  147. * "oneline" format, suitable for output in a single line display.
  148. * <p>
  149. * This method parses and returns the message portion of the tag buffer,
  150. * after taking the tag's character set into account and decoding the buffer
  151. * using that character set. This method is a fairly expensive operation and
  152. * produces a new string on each invocation.
  153. *
  154. * @return decoded tag message as a string. Never null. The returned string
  155. * does not contain any LFs, even if the first paragraph spanned
  156. * multiple lines. Embedded LFs are converted to spaces.
  157. */
  158. public final String getShortMessage() {
  159. final byte[] raw = buffer;
  160. final int msgB = RawParseUtils.tagMessage(raw, 0);
  161. if (msgB < 0)
  162. return "";
  163. final Charset enc = RawParseUtils.parseEncoding(raw);
  164. final int msgE = RawParseUtils.endOfParagraph(raw, msgB);
  165. String str = RawParseUtils.decode(enc, raw, msgB, msgE);
  166. if (RevCommit.hasLF(raw, msgB, msgE))
  167. str = str.replace('\n', ' ');
  168. return str;
  169. }
  170. /**
  171. * Parse this tag buffer for display.
  172. *
  173. * @param walk
  174. * revision walker owning this reference.
  175. * @return parsed tag.
  176. */
  177. public Tag asTag(final RevWalk walk) {
  178. return new Tag(walk.db, this, tagName, buffer);
  179. }
  180. /**
  181. * Get a reference to the object this tag was placed on.
  182. *
  183. * @return object this tag refers to.
  184. */
  185. public final RevObject getObject() {
  186. return object;
  187. }
  188. /**
  189. * Get the name of this tag, from the tag header.
  190. *
  191. * @return name of the tag, according to the tag header.
  192. */
  193. public final String getTagName() {
  194. return tagName;
  195. }
  196. final void disposeBody() {
  197. buffer = null;
  198. }
  199. }