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.

DiffCommand.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright (C) 2011, Tomasz Zarna <Tomasz.Zarna@pl.ibm.com>
  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 static org.eclipse.jgit.lib.Constants.HEAD;
  45. import java.io.BufferedOutputStream;
  46. import java.io.IOException;
  47. import java.io.OutputStream;
  48. import java.util.List;
  49. import org.eclipse.jgit.api.errors.GitAPIException;
  50. import org.eclipse.jgit.api.errors.JGitInternalException;
  51. import org.eclipse.jgit.api.errors.NoHeadException;
  52. import org.eclipse.jgit.diff.DiffEntry;
  53. import org.eclipse.jgit.diff.DiffFormatter;
  54. import org.eclipse.jgit.dircache.DirCacheIterator;
  55. import org.eclipse.jgit.internal.JGitText;
  56. import org.eclipse.jgit.lib.NullProgressMonitor;
  57. import org.eclipse.jgit.lib.ObjectId;
  58. import org.eclipse.jgit.lib.ObjectReader;
  59. import org.eclipse.jgit.lib.ProgressMonitor;
  60. import org.eclipse.jgit.lib.Repository;
  61. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  62. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  63. import org.eclipse.jgit.treewalk.FileTreeIterator;
  64. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  65. import org.eclipse.jgit.util.io.NullOutputStream;
  66. /**
  67. * Show changes between commits, commit and working tree, etc.
  68. *
  69. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-diff.html"
  70. * >Git documentation about diff</a>
  71. */
  72. public class DiffCommand extends GitCommand<List<DiffEntry>> {
  73. private AbstractTreeIterator oldTree;
  74. private AbstractTreeIterator newTree;
  75. private boolean cached;
  76. private TreeFilter pathFilter = TreeFilter.ALL;
  77. private boolean showNameAndStatusOnly;
  78. private OutputStream out;
  79. private int contextLines = -1;
  80. private String sourcePrefix;
  81. private String destinationPrefix;
  82. private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
  83. /**
  84. * @param repo
  85. */
  86. protected DiffCommand(Repository repo) {
  87. super(repo);
  88. }
  89. /**
  90. * Executes the {@code Diff} command with all the options and parameters
  91. * collected by the setter methods (e.g. {@link #setCached(boolean)} of this
  92. * class. Each instance of this class should only be used for one invocation
  93. * of the command. Don't call this method twice on an instance.
  94. *
  95. * @return a DiffEntry for each path which is different
  96. */
  97. public List<DiffEntry> call() throws GitAPIException {
  98. final DiffFormatter diffFmt;
  99. if (out != null && !showNameAndStatusOnly)
  100. diffFmt = new DiffFormatter(new BufferedOutputStream(out));
  101. else
  102. diffFmt = new DiffFormatter(NullOutputStream.INSTANCE);
  103. diffFmt.setRepository(repo);
  104. diffFmt.setProgressMonitor(monitor);
  105. try {
  106. if (cached) {
  107. if (oldTree == null) {
  108. ObjectId head = repo.resolve(HEAD + "^{tree}"); //$NON-NLS-1$
  109. if (head == null)
  110. throw new NoHeadException(JGitText.get().cannotReadTree);
  111. CanonicalTreeParser p = new CanonicalTreeParser();
  112. ObjectReader reader = repo.newObjectReader();
  113. try {
  114. p.reset(reader, head);
  115. } finally {
  116. reader.release();
  117. }
  118. oldTree = p;
  119. }
  120. newTree = new DirCacheIterator(repo.readDirCache());
  121. } else {
  122. if (oldTree == null)
  123. oldTree = new DirCacheIterator(repo.readDirCache());
  124. if (newTree == null)
  125. newTree = new FileTreeIterator(repo);
  126. }
  127. diffFmt.setPathFilter(pathFilter);
  128. List<DiffEntry> result = diffFmt.scan(oldTree, newTree);
  129. if (showNameAndStatusOnly)
  130. return result;
  131. else {
  132. if (contextLines >= 0)
  133. diffFmt.setContext(contextLines);
  134. if (destinationPrefix != null)
  135. diffFmt.setNewPrefix(destinationPrefix);
  136. if (sourcePrefix != null)
  137. diffFmt.setOldPrefix(sourcePrefix);
  138. diffFmt.format(result);
  139. diffFmt.flush();
  140. return result;
  141. }
  142. } catch (IOException e) {
  143. throw new JGitInternalException(e.getMessage(), e);
  144. } finally {
  145. diffFmt.release();
  146. }
  147. }
  148. /**
  149. *
  150. * @param cached
  151. * whether to view the changes you staged for the next commit
  152. * @return this instance
  153. */
  154. public DiffCommand setCached(boolean cached) {
  155. this.cached = cached;
  156. return this;
  157. }
  158. /**
  159. * @param pathFilter
  160. * parameter, used to limit the diff to the named path
  161. * @return this instance
  162. */
  163. public DiffCommand setPathFilter(TreeFilter pathFilter) {
  164. this.pathFilter = pathFilter;
  165. return this;
  166. }
  167. /**
  168. * @param oldTree
  169. * the previous state
  170. * @return this instance
  171. */
  172. public DiffCommand setOldTree(AbstractTreeIterator oldTree) {
  173. this.oldTree = oldTree;
  174. return this;
  175. }
  176. /**
  177. * @param newTree
  178. * the updated state
  179. * @return this instance
  180. */
  181. public DiffCommand setNewTree(AbstractTreeIterator newTree) {
  182. this.newTree = newTree;
  183. return this;
  184. }
  185. /**
  186. * @param showNameAndStatusOnly
  187. * whether to return only names and status of changed files
  188. * @return this instance
  189. */
  190. public DiffCommand setShowNameAndStatusOnly(boolean showNameAndStatusOnly) {
  191. this.showNameAndStatusOnly = showNameAndStatusOnly;
  192. return this;
  193. }
  194. /**
  195. * @param out
  196. * the stream to write line data
  197. * @return this instance
  198. */
  199. public DiffCommand setOutputStream(OutputStream out) {
  200. this.out = out;
  201. return this;
  202. }
  203. /**
  204. * Set number of context lines instead of the usual three.
  205. *
  206. * @param contextLines
  207. * the number of context lines
  208. * @return this instance
  209. */
  210. public DiffCommand setContextLines(int contextLines) {
  211. this.contextLines = contextLines;
  212. return this;
  213. }
  214. /**
  215. * Set the given source prefix instead of "a/".
  216. *
  217. * @param sourcePrefix
  218. * the prefix
  219. * @return this instance
  220. */
  221. public DiffCommand setSourcePrefix(String sourcePrefix) {
  222. this.sourcePrefix = sourcePrefix;
  223. return this;
  224. }
  225. /**
  226. * Set the given destination prefix instead of "b/".
  227. *
  228. * @param destinationPrefix
  229. * the prefix
  230. * @return this instance
  231. */
  232. public DiffCommand setDestinationPrefix(String destinationPrefix) {
  233. this.destinationPrefix = destinationPrefix;
  234. return this;
  235. }
  236. /**
  237. * The progress monitor associated with the diff operation. By default, this
  238. * is set to <code>NullProgressMonitor</code>
  239. *
  240. * @see NullProgressMonitor
  241. *
  242. * @param monitor
  243. * a progress monitor
  244. * @return this instance
  245. */
  246. public DiffCommand setProgressMonitor(ProgressMonitor monitor) {
  247. this.monitor = monitor;
  248. return this;
  249. }
  250. }