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