Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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