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 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.IOException;
  46. import java.util.ArrayList;
  47. import java.util.Collection;
  48. import java.util.Collections;
  49. import org.eclipse.jgit.api.errors.JGitInternalException;
  50. import org.eclipse.jgit.blame.BlameGenerator;
  51. import org.eclipse.jgit.blame.BlameResult;
  52. import org.eclipse.jgit.diff.DiffAlgorithm;
  53. import org.eclipse.jgit.diff.RawText;
  54. import org.eclipse.jgit.diff.RawTextComparator;
  55. import org.eclipse.jgit.dircache.DirCache;
  56. import org.eclipse.jgit.lib.AnyObjectId;
  57. import org.eclipse.jgit.lib.Constants;
  58. import org.eclipse.jgit.lib.ObjectId;
  59. import org.eclipse.jgit.lib.Repository;
  60. /**
  61. * Blame command for building a {@link BlameResult} for a file path.
  62. */
  63. public class BlameCommand extends GitCommand<BlameResult> {
  64. private String path;
  65. private DiffAlgorithm diffAlgorithm;
  66. private RawTextComparator textComparator;
  67. private ObjectId startCommit;
  68. private Collection<ObjectId> reverseEndCommits;
  69. private Boolean followFileRenames;
  70. /**
  71. * @param repo
  72. */
  73. public BlameCommand(Repository repo) {
  74. super(repo);
  75. }
  76. /**
  77. * Set file path
  78. *
  79. * @param filePath
  80. * @return this command
  81. */
  82. public BlameCommand setFilePath(String filePath) {
  83. this.path = filePath;
  84. return this;
  85. }
  86. /**
  87. * Set diff algorithm
  88. *
  89. * @param diffAlgorithm
  90. * @return this command
  91. */
  92. public BlameCommand setDiffAlgorithm(DiffAlgorithm diffAlgorithm) {
  93. this.diffAlgorithm = diffAlgorithm;
  94. return this;
  95. }
  96. /**
  97. * Set raw text comparator
  98. *
  99. * @param textComparator
  100. * @return this command
  101. */
  102. public BlameCommand setTextComparator(RawTextComparator textComparator) {
  103. this.textComparator = textComparator;
  104. return this;
  105. }
  106. /**
  107. * Set start commit id
  108. *
  109. * @param commit
  110. * @return this command
  111. */
  112. public BlameCommand setStartCommit(AnyObjectId commit) {
  113. this.startCommit = commit.toObjectId();
  114. return this;
  115. }
  116. /**
  117. * Enable (or disable) following file renames.
  118. * <p>
  119. * If true renames are followed using the standard FollowFilter behavior
  120. * used by RevWalk (which matches {@code git log --follow} in the C
  121. * implementation). This is not the same as copy/move detection as
  122. * implemented by the C implementation's of {@code git blame -M -C}.
  123. *
  124. * @param follow
  125. * enable following.
  126. * @return {@code this}
  127. */
  128. public BlameCommand setFollowFileRenames(boolean follow) {
  129. followFileRenames = Boolean.valueOf(follow);
  130. return this;
  131. }
  132. /**
  133. * Configure the command to compute reverse blame (history of deletes).
  134. *
  135. * @param start
  136. * oldest commit to traverse from. The result file will be loaded
  137. * from this commit's tree.
  138. * @param end
  139. * most recent commit to stop traversal at. Usually an active
  140. * branch tip, tag, or HEAD.
  141. * @return {@code this}
  142. * @throws IOException
  143. * the repository cannot be read.
  144. */
  145. public BlameCommand reverse(AnyObjectId start, AnyObjectId end)
  146. throws IOException {
  147. return reverse(start, Collections.singleton(end.toObjectId()));
  148. }
  149. /**
  150. * Configure the generator to compute reverse blame (history of deletes).
  151. *
  152. * @param start
  153. * oldest commit to traverse from. The result file will be loaded
  154. * from this commit's tree.
  155. * @param end
  156. * most recent commits to stop traversal at. Usually an active
  157. * branch tip, tag, or HEAD.
  158. * @return {@code this}
  159. * @throws IOException
  160. * the repository cannot be read.
  161. */
  162. public BlameCommand reverse(AnyObjectId start, Collection<ObjectId> end)
  163. throws IOException {
  164. startCommit = start.toObjectId();
  165. reverseEndCommits = new ArrayList<ObjectId>(end);
  166. return this;
  167. }
  168. /**
  169. * Generate a list of lines with information about when the lines were
  170. * introduced into the file path.
  171. *
  172. * @return list of lines
  173. */
  174. public BlameResult call() throws JGitInternalException {
  175. checkCallable();
  176. BlameGenerator gen = new BlameGenerator(repo, path);
  177. try {
  178. if (diffAlgorithm != null)
  179. gen.setDiffAlgorithm(diffAlgorithm);
  180. if (textComparator != null)
  181. gen.setTextComparator(textComparator);
  182. if (followFileRenames != null)
  183. gen.setFollowFileRenames(followFileRenames.booleanValue());
  184. if (reverseEndCommits != null)
  185. gen.reverse(startCommit, reverseEndCommits);
  186. else if (startCommit != null)
  187. gen.push(null, startCommit);
  188. else {
  189. gen.push(null, repo.resolve(Constants.HEAD));
  190. if (!repo.isBare()) {
  191. DirCache dc = repo.readDirCache();
  192. int entry = dc.findEntry(path);
  193. if (0 <= entry)
  194. gen.push(null, dc.getEntry(entry).getObjectId());
  195. File inTree = new File(repo.getWorkTree(), path);
  196. if (inTree.isFile())
  197. gen.push(null, new RawText(inTree));
  198. }
  199. }
  200. return gen.computeBlameResult();
  201. } catch (IOException e) {
  202. throw new JGitInternalException(e.getMessage(), e);
  203. } finally {
  204. gen.release();
  205. }
  206. }
  207. }