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.

BlameCommand.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (C) 2011, GitHub 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.api;
  44. import java.io.File;
  45. import java.io.FileInputStream;
  46. import java.io.FileNotFoundException;
  47. import java.io.IOException;
  48. import java.io.InputStream;
  49. import java.util.ArrayList;
  50. import java.util.Collection;
  51. import java.util.Collections;
  52. import org.eclipse.jgit.api.errors.GitAPIException;
  53. import org.eclipse.jgit.api.errors.JGitInternalException;
  54. import org.eclipse.jgit.blame.BlameGenerator;
  55. import org.eclipse.jgit.blame.BlameResult;
  56. import org.eclipse.jgit.diff.DiffAlgorithm;
  57. import org.eclipse.jgit.diff.RawText;
  58. import org.eclipse.jgit.diff.RawTextComparator;
  59. import org.eclipse.jgit.dircache.DirCache;
  60. import org.eclipse.jgit.lib.AnyObjectId;
  61. import org.eclipse.jgit.lib.Constants;
  62. import org.eclipse.jgit.lib.CoreConfig.AutoCRLF;
  63. import org.eclipse.jgit.lib.ObjectId;
  64. import org.eclipse.jgit.lib.Repository;
  65. import org.eclipse.jgit.treewalk.WorkingTreeOptions;
  66. import org.eclipse.jgit.util.IO;
  67. import org.eclipse.jgit.util.io.AutoLFInputStream;
  68. /**
  69. * Blame command for building a {@link BlameResult} for a file path.
  70. */
  71. public class BlameCommand extends GitCommand<BlameResult> {
  72. private String path;
  73. private DiffAlgorithm diffAlgorithm;
  74. private RawTextComparator textComparator;
  75. private ObjectId startCommit;
  76. private Collection<ObjectId> reverseEndCommits;
  77. private Boolean followFileRenames;
  78. /**
  79. * @param repo
  80. */
  81. public BlameCommand(Repository repo) {
  82. super(repo);
  83. }
  84. /**
  85. * Set file path.
  86. *
  87. * @param filePath
  88. * file path (with <code>/</code> as separator)
  89. * @return this command
  90. */
  91. public BlameCommand setFilePath(String filePath) {
  92. this.path = filePath;
  93. return this;
  94. }
  95. /**
  96. * Set diff algorithm
  97. *
  98. * @param diffAlgorithm
  99. * @return this command
  100. */
  101. public BlameCommand setDiffAlgorithm(DiffAlgorithm diffAlgorithm) {
  102. this.diffAlgorithm = diffAlgorithm;
  103. return this;
  104. }
  105. /**
  106. * Set raw text comparator
  107. *
  108. * @param textComparator
  109. * @return this command
  110. */
  111. public BlameCommand setTextComparator(RawTextComparator textComparator) {
  112. this.textComparator = textComparator;
  113. return this;
  114. }
  115. /**
  116. * Set start commit id
  117. *
  118. * @param commit
  119. * @return this command
  120. */
  121. public BlameCommand setStartCommit(AnyObjectId commit) {
  122. this.startCommit = commit.toObjectId();
  123. return this;
  124. }
  125. /**
  126. * Enable (or disable) following file renames.
  127. * <p>
  128. * If true renames are followed using the standard FollowFilter behavior
  129. * used by RevWalk (which matches {@code git log --follow} in the C
  130. * implementation). This is not the same as copy/move detection as
  131. * implemented by the C implementation's of {@code git blame -M -C}.
  132. *
  133. * @param follow
  134. * enable following.
  135. * @return {@code this}
  136. */
  137. public BlameCommand setFollowFileRenames(boolean follow) {
  138. followFileRenames = Boolean.valueOf(follow);
  139. return this;
  140. }
  141. /**
  142. * Configure the command to compute reverse blame (history of deletes).
  143. *
  144. * @param start
  145. * oldest commit to traverse from. The result file will be loaded
  146. * from this commit's tree.
  147. * @param end
  148. * most recent commit to stop traversal at. Usually an active
  149. * branch tip, tag, or HEAD.
  150. * @return {@code this}
  151. * @throws IOException
  152. * the repository cannot be read.
  153. */
  154. public BlameCommand reverse(AnyObjectId start, AnyObjectId end)
  155. throws IOException {
  156. return reverse(start, Collections.singleton(end.toObjectId()));
  157. }
  158. /**
  159. * Configure the generator to compute reverse blame (history of deletes).
  160. *
  161. * @param start
  162. * oldest commit to traverse from. The result file will be loaded
  163. * from this commit's tree.
  164. * @param end
  165. * most recent commits to stop traversal at. Usually an active
  166. * branch tip, tag, or HEAD.
  167. * @return {@code this}
  168. * @throws IOException
  169. * the repository cannot be read.
  170. */
  171. public BlameCommand reverse(AnyObjectId start, Collection<ObjectId> end)
  172. throws IOException {
  173. startCommit = start.toObjectId();
  174. reverseEndCommits = new ArrayList<ObjectId>(end);
  175. return this;
  176. }
  177. /**
  178. * Generate a list of lines with information about when the lines were
  179. * introduced into the file path.
  180. *
  181. * @return list of lines
  182. */
  183. public BlameResult call() throws GitAPIException {
  184. checkCallable();
  185. try (BlameGenerator gen = new BlameGenerator(repo, path)) {
  186. if (diffAlgorithm != null)
  187. gen.setDiffAlgorithm(diffAlgorithm);
  188. if (textComparator != null)
  189. gen.setTextComparator(textComparator);
  190. if (followFileRenames != null)
  191. gen.setFollowFileRenames(followFileRenames.booleanValue());
  192. if (reverseEndCommits != null)
  193. gen.reverse(startCommit, reverseEndCommits);
  194. else if (startCommit != null)
  195. gen.push(null, startCommit);
  196. else {
  197. gen.push(null, repo.resolve(Constants.HEAD));
  198. if (!repo.isBare()) {
  199. DirCache dc = repo.readDirCache();
  200. int entry = dc.findEntry(path);
  201. if (0 <= entry)
  202. gen.push(null, dc.getEntry(entry).getObjectId());
  203. File inTree = new File(repo.getWorkTree(), path);
  204. if (repo.getFS().isFile(inTree)) {
  205. RawText rawText = getRawText(inTree);
  206. gen.push(null, rawText);
  207. }
  208. }
  209. }
  210. return gen.computeBlameResult();
  211. } catch (IOException e) {
  212. throw new JGitInternalException(e.getMessage(), e);
  213. }
  214. }
  215. private RawText getRawText(File inTree) throws IOException,
  216. FileNotFoundException {
  217. RawText rawText;
  218. WorkingTreeOptions workingTreeOptions = getRepository().getConfig()
  219. .get(WorkingTreeOptions.KEY);
  220. AutoCRLF autoCRLF = workingTreeOptions.getAutoCRLF();
  221. switch (autoCRLF) {
  222. case FALSE:
  223. case INPUT:
  224. // Git used the repo format on checkout, but other tools
  225. // may change the format to CRLF. We ignore that here.
  226. rawText = new RawText(inTree);
  227. break;
  228. case TRUE:
  229. AutoLFInputStream in = new AutoLFInputStream(
  230. new FileInputStream(inTree), true);
  231. // Canonicalization should lead to same or shorter length
  232. // (CRLF to LF), so the file size on disk is an upper size bound
  233. rawText = new RawText(toByteArray(in, (int) inTree.length()));
  234. break;
  235. default:
  236. throw new IllegalArgumentException(
  237. "Unknown autocrlf option " + autoCRLF); //$NON-NLS-1$
  238. }
  239. return rawText;
  240. }
  241. private static byte[] toByteArray(InputStream source, int upperSizeLimit)
  242. throws IOException {
  243. byte[] buffer = new byte[upperSizeLimit];
  244. try {
  245. int read = IO.readFully(source, buffer, 0);
  246. if (read == upperSizeLimit)
  247. return buffer;
  248. else {
  249. byte[] copy = new byte[read];
  250. System.arraycopy(buffer, 0, copy, 0, read);
  251. return copy;
  252. }
  253. } finally {
  254. source.close();
  255. }
  256. }
  257. }