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.

BlameResult.java 10KB

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