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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 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.release();
  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. /** @return path of the file this result annotates. */
  111. public String getResultPath() {
  112. return resultPath;
  113. }
  114. /** @return contents of the result file, available for display. */
  115. public RawText getResultContents() {
  116. return resultContents;
  117. }
  118. /** Throw away the {@link #getResultContents()}. */
  119. public void discardResultContents() {
  120. resultContents = null;
  121. }
  122. /**
  123. * Check if the given result line has been annotated yet.
  124. *
  125. * @param idx
  126. * line to read data of, 0 based.
  127. * @return true if the data has been annotated, false otherwise.
  128. */
  129. public boolean hasSourceData(int idx) {
  130. return sourceLines[idx] != 0;
  131. }
  132. /**
  133. * Check if the given result line has been annotated yet.
  134. *
  135. * @param start
  136. * first index to examine.
  137. * @param end
  138. * last index to examine.
  139. * @return true if the data has been annotated, false otherwise.
  140. */
  141. public boolean hasSourceData(int start, int end) {
  142. for (; start < end; start++)
  143. if (sourceLines[start] == 0)
  144. return false;
  145. return true;
  146. }
  147. /**
  148. * Get the commit that provided the specified line of the result.
  149. * <p>
  150. * The source commit may be null if the line was blamed to an uncommitted
  151. * revision, such as the working tree copy, or during a reverse blame if the
  152. * line survives to the end revision (e.g. the branch tip).
  153. *
  154. * @param idx
  155. * line to read data of, 0 based.
  156. * @return commit that provided line {@code idx}. May be null.
  157. */
  158. public RevCommit getSourceCommit(int idx) {
  159. return sourceCommits[idx];
  160. }
  161. /**
  162. * Get the author that provided the specified line of the result.
  163. *
  164. * @param idx
  165. * line to read data of, 0 based.
  166. * @return author that provided line {@code idx}. May be null.
  167. */
  168. public PersonIdent getSourceAuthor(int idx) {
  169. return sourceAuthors[idx];
  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. * Get the file path that provided the specified line of the result.
  183. *
  184. * @param idx
  185. * line to read data of, 0 based.
  186. * @return source file path that provided line {@code idx}.
  187. */
  188. public String getSourcePath(int idx) {
  189. return sourcePaths[idx];
  190. }
  191. /**
  192. * Get the corresponding line number in the source file.
  193. *
  194. * @param idx
  195. * line to read data of, 0 based.
  196. * @return matching line number in the source file.
  197. */
  198. public int getSourceLine(int idx) {
  199. return sourceLines[idx] - 1;
  200. }
  201. /**
  202. * Compute all pending information.
  203. *
  204. * @throws IOException
  205. * the repository cannot be read.
  206. */
  207. public void computeAll() throws IOException {
  208. BlameGenerator gen = generator;
  209. if (gen == null)
  210. return;
  211. try {
  212. while (gen.next())
  213. loadFrom(gen);
  214. } finally {
  215. gen.release();
  216. generator = null;
  217. }
  218. }
  219. /**
  220. * Compute the next available segment and return the first index.
  221. * <p>
  222. * Computes one segment and returns to the caller the first index that is
  223. * available. After return the caller can also inspect {@link #lastLength()}
  224. * to determine how many lines of the result were computed.
  225. *
  226. * @return index that is now available. -1 if no more are available.
  227. * @throws IOException
  228. * the repository cannot be read.
  229. */
  230. public int computeNext() throws IOException {
  231. BlameGenerator gen = generator;
  232. if (gen == null)
  233. return -1;
  234. if (gen.next()) {
  235. loadFrom(gen);
  236. lastLength = gen.getRegionLength();
  237. return gen.getResultStart();
  238. } else {
  239. gen.release();
  240. generator = null;
  241. return -1;
  242. }
  243. }
  244. /** @return length of the last segment found by {@link #computeNext()}. */
  245. public int lastLength() {
  246. return lastLength;
  247. }
  248. /**
  249. * Compute until the entire range has been populated.
  250. *
  251. * @param start
  252. * first index to examine.
  253. * @param end
  254. * last index to examine.
  255. * @throws IOException
  256. * the repository cannot be read.
  257. */
  258. public void computeRange(int start, int end) throws IOException {
  259. BlameGenerator gen = generator;
  260. if (gen == null)
  261. return;
  262. while (start < end) {
  263. if (hasSourceData(start, end))
  264. return;
  265. if (!gen.next()) {
  266. gen.release();
  267. generator = null;
  268. return;
  269. }
  270. loadFrom(gen);
  271. // If the result contains either end of our current range bounds,
  272. // update the bounds to avoid scanning that section during the
  273. // next loop iteration.
  274. int resLine = gen.getResultStart();
  275. int resEnd = gen.getResultEnd();
  276. if (resLine <= start && start < resEnd)
  277. start = resEnd;
  278. if (resLine <= end && end < resEnd)
  279. end = resLine;
  280. }
  281. }
  282. @Override
  283. public String toString() {
  284. StringBuilder r = new StringBuilder();
  285. r.append("BlameResult: "); //$NON-NLS-1$
  286. r.append(getResultPath());
  287. return r.toString();
  288. }
  289. private void loadFrom(BlameGenerator gen) {
  290. RevCommit srcCommit = gen.getSourceCommit();
  291. PersonIdent srcAuthor = gen.getSourceAuthor();
  292. PersonIdent srcCommitter = gen.getSourceCommitter();
  293. String srcPath = gen.getSourcePath();
  294. int srcLine = gen.getSourceStart();
  295. int resLine = gen.getResultStart();
  296. int resEnd = gen.getResultEnd();
  297. for (; resLine < resEnd; resLine++) {
  298. // Reverse blame can generate multiple results for the same line.
  299. // Favor the first one selected, as this is the oldest and most
  300. // likely to be nearest to the inquiry made by the user.
  301. if (sourceLines[resLine] != 0)
  302. continue;
  303. sourceCommits[resLine] = srcCommit;
  304. sourceAuthors[resLine] = srcAuthor;
  305. sourceCommitters[resLine] = srcCommitter;
  306. sourcePaths[resLine] = srcPath;
  307. // Since sourceLines is 1-based to permit hasSourceData to use 0 to
  308. // mean the line has not been annotated yet, pre-increment instead
  309. // of the traditional post-increment when making the assignment.
  310. sourceLines[resLine] = ++srcLine;
  311. }
  312. }
  313. }