View Javadoc
1   /*
2    * Copyright (C) 2011, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.blame;
45  
46  import java.io.IOException;
47  
48  import org.eclipse.jgit.diff.RawText;
49  import org.eclipse.jgit.lib.PersonIdent;
50  import org.eclipse.jgit.revwalk.RevCommit;
51  
52  /**
53   * Collects line annotations for inspection by applications.
54   * <p>
55   * A result is usually updated incrementally as the BlameGenerator digs back
56   * further through history. Applications that want to lay annotations down text
57   * to the original source file in a viewer may find the BlameResult structure an
58   * easy way to acquire the information, at the expense of keeping tables in
59   * memory tracking every line of the result file.
60   * <p>
61   * This class is not thread-safe.
62   * <p>
63   * During blame processing there are two files involved:
64   * <ul>
65   * <li>result - The file whose lines are being examined. This is the revision
66   * the user is trying to view blame/annotation information alongside of.</li>
67   * <li>source - The file that was blamed with supplying one or more lines of
68   * data into result. The source may be a different file path (due to copy or
69   * rename). Source line numbers may differ from result line numbers due to lines
70   * being added/removed in intermediate revisions.</li>
71   * </ul>
72   */
73  public class BlameResult {
74  	/**
75  	 * Construct a new BlameResult for a generator.
76  	 *
77  	 * @param gen
78  	 *            the generator the result will consume records from.
79  	 * @return the new result object. null if the generator cannot find the path
80  	 *         it starts from.
81  	 * @throws IOException
82  	 *             the repository cannot be read.
83  	 */
84  	public static BlameResult create(BlameGenerator gen) throws IOException {
85  		String path = gen.getResultPath();
86  		RawText contents = gen.getResultContents();
87  		if (contents == null) {
88  			gen.close();
89  			return null;
90  		}
91  		return new BlameResult(gen, path, contents);
92  	}
93  
94  	private final String resultPath;
95  
96  	private final RevCommit[] sourceCommits;
97  
98  	private final PersonIdent[] sourceAuthors;
99  
100 	private final PersonIdent[] sourceCommitters;
101 
102 	private final String[] sourcePaths;
103 
104 	/** Warning: these are actually 1-based. */
105 	private final int[] sourceLines;
106 
107 	private RawText resultContents;
108 
109 	private BlameGenerator generator;
110 
111 	private int lastLength;
112 
113 	BlameResult(BlameGenerator bg, String path, RawText text) {
114 		generator = bg;
115 		resultPath = path;
116 		resultContents = text;
117 
118 		int cnt = text.size();
119 		sourceCommits = new RevCommit[cnt];
120 		sourceAuthors = new PersonIdent[cnt];
121 		sourceCommitters = new PersonIdent[cnt];
122 		sourceLines = new int[cnt];
123 		sourcePaths = new String[cnt];
124 	}
125 
126 	/** @return path of the file this result annotates. */
127 	public String getResultPath() {
128 		return resultPath;
129 	}
130 
131 	/** @return contents of the result file, available for display. */
132 	public RawText getResultContents() {
133 		return resultContents;
134 	}
135 
136 	/** Throw away the {@link #getResultContents()}. */
137 	public void discardResultContents() {
138 		resultContents = null;
139 	}
140 
141 	/**
142 	 * Check if the given result line has been annotated yet.
143 	 *
144 	 * @param idx
145 	 *            line to read data of, 0 based.
146 	 * @return true if the data has been annotated, false otherwise.
147 	 */
148 	public boolean hasSourceData(int idx) {
149 		return sourceLines[idx] != 0;
150 	}
151 
152 	/**
153 	 * Check if the given result line has been annotated yet.
154 	 *
155 	 * @param start
156 	 *            first index to examine.
157 	 * @param end
158 	 *            last index to examine.
159 	 * @return true if the data has been annotated, false otherwise.
160 	 */
161 	public boolean hasSourceData(int start, int end) {
162 		for (; start < end; start++)
163 			if (sourceLines[start] == 0)
164 				return false;
165 		return true;
166 	}
167 
168 	/**
169 	 * Get the commit that provided the specified line of the result.
170 	 * <p>
171 	 * The source commit may be null if the line was blamed to an uncommitted
172 	 * revision, such as the working tree copy, or during a reverse blame if the
173 	 * line survives to the end revision (e.g. the branch tip).
174 	 *
175 	 * @param idx
176 	 *            line to read data of, 0 based.
177 	 * @return commit that provided line {@code idx}. May be null.
178 	 */
179 	public RevCommit getSourceCommit(int idx) {
180 		return sourceCommits[idx];
181 	}
182 
183 	/**
184 	 * Get the author that provided the specified line of the result.
185 	 *
186 	 * @param idx
187 	 *            line to read data of, 0 based.
188 	 * @return author that provided line {@code idx}. May be null.
189 	 */
190 	public PersonIdent getSourceAuthor(int idx) {
191 		return sourceAuthors[idx];
192 	}
193 
194 	/**
195 	 * Get the committer that provided the specified line of the result.
196 	 *
197 	 * @param idx
198 	 *            line to read data of, 0 based.
199 	 * @return committer that provided line {@code idx}. May be null.
200 	 */
201 	public PersonIdent getSourceCommitter(int idx) {
202 		return sourceCommitters[idx];
203 	}
204 
205 	/**
206 	 * Get the file path that provided the specified line of the result.
207 	 *
208 	 * @param idx
209 	 *            line to read data of, 0 based.
210 	 * @return source file path that provided line {@code idx}.
211 	 */
212 	public String getSourcePath(int idx) {
213 		return sourcePaths[idx];
214 	}
215 
216 	/**
217 	 * Get the corresponding line number in the source file.
218 	 *
219 	 * @param idx
220 	 *            line to read data of, 0 based.
221 	 * @return matching line number in the source file.
222 	 */
223 	public int getSourceLine(int idx) {
224 		return sourceLines[idx] - 1;
225 	}
226 
227 	/**
228 	 * Compute all pending information.
229 	 *
230 	 * @throws IOException
231 	 *             the repository cannot be read.
232 	 */
233 	public void computeAll() throws IOException {
234 		BlameGenerator gen = generator;
235 		if (gen == null)
236 			return;
237 
238 		try {
239 			while (gen.next())
240 				loadFrom(gen);
241 		} finally {
242 			gen.close();
243 			generator = null;
244 		}
245 	}
246 
247 	/**
248 	 * Compute the next available segment and return the first index.
249 	 * <p>
250 	 * Computes one segment and returns to the caller the first index that is
251 	 * available. After return the caller can also inspect {@link #lastLength()}
252 	 * to determine how many lines of the result were computed.
253 	 *
254 	 * @return index that is now available. -1 if no more are available.
255 	 * @throws IOException
256 	 *             the repository cannot be read.
257 	 */
258 	public int computeNext() throws IOException {
259 		BlameGenerator gen = generator;
260 		if (gen == null)
261 			return -1;
262 
263 		if (gen.next()) {
264 			loadFrom(gen);
265 			lastLength = gen.getRegionLength();
266 			return gen.getResultStart();
267 		} else {
268 			gen.close();
269 			generator = null;
270 			return -1;
271 		}
272 	}
273 
274 	/** @return length of the last segment found by {@link #computeNext()}. */
275 	public int lastLength() {
276 		return lastLength;
277 	}
278 
279 	/**
280 	 * Compute until the entire range has been populated.
281 	 *
282 	 * @param start
283 	 *            first index to examine (inclusive).
284 	 * @param end
285 	 *            end index (exclusive).
286 	 * @throws IOException
287 	 *             the repository cannot be read.
288 	 */
289 	public void computeRange(int start, int end) throws IOException {
290 		BlameGenerator gen = generator;
291 		if (gen == null)
292 			return;
293 		if (start == 0 && end == resultContents.size()) {
294 			computeAll();
295 			return;
296 		}
297 
298 		while (start < end) {
299 			if (hasSourceData(start, end))
300 				return;
301 
302 			if (!gen.next()) {
303 				gen.close();
304 				generator = null;
305 				return;
306 			}
307 
308 			loadFrom(gen);
309 
310 			// If the result contains either end of our current range bounds,
311 			// update the bounds to avoid scanning that section during the
312 			// next loop iteration.
313 
314 			int resLine = gen.getResultStart();
315 			int resEnd = gen.getResultEnd();
316 
317 			if (resLine <= start && start < resEnd)
318 				start = resEnd;
319 
320 			if (resLine <= end && end < resEnd)
321 				end = resLine;
322 		}
323 	}
324 
325 	@Override
326 	public String toString() {
327 		StringBuilder r = new StringBuilder();
328 		r.append("BlameResult: "); //$NON-NLS-1$
329 		r.append(getResultPath());
330 		return r.toString();
331 	}
332 
333 	private void loadFrom(BlameGenerator gen) {
334 		RevCommit srcCommit = gen.getSourceCommit();
335 		PersonIdent srcAuthor = gen.getSourceAuthor();
336 		PersonIdent srcCommitter = gen.getSourceCommitter();
337 		String srcPath = gen.getSourcePath();
338 		int srcLine = gen.getSourceStart();
339 		int resLine = gen.getResultStart();
340 		int resEnd = gen.getResultEnd();
341 
342 		for (; resLine < resEnd; resLine++) {
343 			// Reverse blame can generate multiple results for the same line.
344 			// Favor the first one selected, as this is the oldest and most
345 			// likely to be nearest to the inquiry made by the user.
346 			if (sourceLines[resLine] != 0)
347 				continue;
348 
349 			sourceCommits[resLine] = srcCommit;
350 			sourceAuthors[resLine] = srcAuthor;
351 			sourceCommitters[resLine] = srcCommitter;
352 			sourcePaths[resLine] = srcPath;
353 
354 			// Since sourceLines is 1-based to permit hasSourceData to use 0 to
355 			// mean the line has not been annotated yet, pre-increment instead
356 			// of the traditional post-increment when making the assignment.
357 			sourceLines[resLine] = ++srcLine;
358 		}
359 	}
360 }