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