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.

Diff.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com>
  3. * Copyright (C) 2009, Johannes E. Schindelin
  4. * Copyright (C) 2009, Johannes Schindelin <johannes.schindelin@gmx.de>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.pgm;
  46. import static java.lang.Integer.valueOf;
  47. import static org.eclipse.jgit.lib.Constants.HEAD;
  48. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_STRING_LENGTH;
  49. import java.io.BufferedOutputStream;
  50. import java.io.IOException;
  51. import java.text.MessageFormat;
  52. import java.util.List;
  53. import java.util.concurrent.TimeUnit;
  54. import org.eclipse.jgit.diff.DiffAlgorithm;
  55. import org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm;
  56. import org.eclipse.jgit.diff.DiffEntry;
  57. import org.eclipse.jgit.diff.DiffFormatter;
  58. import org.eclipse.jgit.diff.RawTextComparator;
  59. import org.eclipse.jgit.diff.RenameDetector;
  60. import org.eclipse.jgit.dircache.DirCacheIterator;
  61. import org.eclipse.jgit.errors.RevisionSyntaxException;
  62. import org.eclipse.jgit.lib.ObjectId;
  63. import org.eclipse.jgit.lib.ObjectReader;
  64. import org.eclipse.jgit.lib.Repository;
  65. import org.eclipse.jgit.lib.TextProgressMonitor;
  66. import org.eclipse.jgit.pgm.internal.CLIText;
  67. import org.eclipse.jgit.pgm.opt.PathTreeFilterHandler;
  68. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  69. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  70. import org.eclipse.jgit.treewalk.FileTreeIterator;
  71. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  72. import org.eclipse.jgit.util.io.ThrowingPrintWriter;
  73. import org.kohsuke.args4j.Argument;
  74. import org.kohsuke.args4j.Option;
  75. @Command(common = true, usage = "usage_ShowDiffs")
  76. class Diff extends TextBuiltin {
  77. private DiffFormatter diffFmt;
  78. @Argument(index = 0, metaVar = "metaVar_treeish")
  79. private AbstractTreeIterator oldTree;
  80. @Argument(index = 1, metaVar = "metaVar_treeish")
  81. private AbstractTreeIterator newTree;
  82. @Option(name = "--cached", aliases = { "--staged" }, usage = "usage_cached")
  83. private boolean cached;
  84. @Option(name = "--", metaVar = "metaVar_paths", handler = PathTreeFilterHandler.class)
  85. private TreeFilter pathFilter = TreeFilter.ALL;
  86. // BEGIN -- Options shared with Log
  87. @Option(name = "-p", usage = "usage_showPatch")
  88. boolean showPatch;
  89. @Option(name = "-M", usage = "usage_detectRenames")
  90. private Boolean detectRenames;
  91. @Option(name = "--no-renames", usage = "usage_noRenames")
  92. void noRenames(@SuppressWarnings("unused") boolean on) {
  93. detectRenames = Boolean.FALSE;
  94. }
  95. @Option(name = "--algorithm", metaVar = "metaVar_diffAlg", usage = "usage_diffAlgorithm")
  96. void setAlgorithm(SupportedAlgorithm s) {
  97. diffFmt.setDiffAlgorithm(DiffAlgorithm.getAlgorithm(s));
  98. }
  99. @Option(name = "-l", usage = "usage_renameLimit")
  100. private Integer renameLimit;
  101. @Option(name = "--name-status", usage = "usage_nameStatus")
  102. private boolean showNameAndStatusOnly;
  103. @Option(name = "--ignore-space-at-eol")
  104. void ignoreSpaceAtEol(@SuppressWarnings("unused") boolean on) {
  105. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_TRAILING);
  106. }
  107. @Option(name = "--ignore-leading-space")
  108. void ignoreLeadingSpace(@SuppressWarnings("unused") boolean on) {
  109. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_LEADING);
  110. }
  111. @Option(name = "-b", aliases = { "--ignore-space-change" })
  112. void ignoreSpaceChange(@SuppressWarnings("unused") boolean on) {
  113. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_CHANGE);
  114. }
  115. @Option(name = "-w", aliases = { "--ignore-all-space" })
  116. void ignoreAllSpace(@SuppressWarnings("unused") boolean on) {
  117. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_ALL);
  118. }
  119. @Option(name = "-U", aliases = { "--unified" }, metaVar = "metaVar_linesOfContext")
  120. void unified(int lines) {
  121. diffFmt.setContext(lines);
  122. }
  123. @Option(name = "--abbrev", metaVar = "metaVar_n")
  124. void abbrev(int lines) {
  125. diffFmt.setAbbreviationLength(lines);
  126. }
  127. @Option(name = "--full-index")
  128. void abbrev(@SuppressWarnings("unused") boolean on) {
  129. diffFmt.setAbbreviationLength(OBJECT_ID_STRING_LENGTH);
  130. }
  131. @Option(name = "--src-prefix", metaVar = "metaVar_prefix", usage = "usage_srcPrefix")
  132. void sourcePrefix(String path) {
  133. diffFmt.setOldPrefix(path);
  134. }
  135. @Option(name = "--dst-prefix", metaVar = "metaVar_prefix", usage = "usage_dstPrefix")
  136. void dstPrefix(String path) {
  137. diffFmt.setNewPrefix(path);
  138. }
  139. @Option(name = "--no-prefix", usage = "usage_noPrefix")
  140. void noPrefix(@SuppressWarnings("unused") boolean on) {
  141. diffFmt.setOldPrefix(""); //$NON-NLS-1$
  142. diffFmt.setNewPrefix(""); //$NON-NLS-1$
  143. }
  144. // END -- Options shared with Log
  145. /** {@inheritDoc} */
  146. @Override
  147. protected void init(Repository repository, String gitDir) {
  148. super.init(repository, gitDir);
  149. diffFmt = new DiffFormatter(new BufferedOutputStream(outs));
  150. }
  151. /** {@inheritDoc} */
  152. @Override
  153. protected void run() {
  154. diffFmt.setRepository(db);
  155. try {
  156. if (cached) {
  157. if (oldTree == null) {
  158. ObjectId head = db.resolve(HEAD + "^{tree}"); //$NON-NLS-1$
  159. if (head == null) {
  160. die(MessageFormat.format(CLIText.get().notATree, HEAD));
  161. }
  162. CanonicalTreeParser p = new CanonicalTreeParser();
  163. try (ObjectReader reader = db.newObjectReader()) {
  164. p.reset(reader, head);
  165. }
  166. oldTree = p;
  167. }
  168. newTree = new DirCacheIterator(db.readDirCache());
  169. } else if (oldTree == null) {
  170. oldTree = new DirCacheIterator(db.readDirCache());
  171. newTree = new FileTreeIterator(db);
  172. } else if (newTree == null) {
  173. newTree = new FileTreeIterator(db);
  174. }
  175. TextProgressMonitor pm = new TextProgressMonitor(errw);
  176. pm.setDelayStart(2, TimeUnit.SECONDS);
  177. diffFmt.setProgressMonitor(pm);
  178. diffFmt.setPathFilter(pathFilter);
  179. if (detectRenames != null) {
  180. diffFmt.setDetectRenames(detectRenames.booleanValue());
  181. }
  182. if (renameLimit != null && diffFmt.isDetectRenames()) {
  183. RenameDetector rd = diffFmt.getRenameDetector();
  184. rd.setRenameLimit(renameLimit.intValue());
  185. }
  186. if (showNameAndStatusOnly) {
  187. nameStatus(outw, diffFmt.scan(oldTree, newTree));
  188. outw.flush();
  189. } else {
  190. diffFmt.format(oldTree, newTree);
  191. diffFmt.flush();
  192. }
  193. } catch (RevisionSyntaxException | IOException e) {
  194. throw die(e.getMessage(), e);
  195. } finally {
  196. diffFmt.close();
  197. }
  198. }
  199. static void nameStatus(ThrowingPrintWriter out, List<DiffEntry> files)
  200. throws IOException {
  201. for (DiffEntry ent : files) {
  202. switch (ent.getChangeType()) {
  203. case ADD:
  204. out.println("A\t" + ent.getNewPath()); //$NON-NLS-1$
  205. break;
  206. case DELETE:
  207. out.println("D\t" + ent.getOldPath()); //$NON-NLS-1$
  208. break;
  209. case MODIFY:
  210. out.println("M\t" + ent.getNewPath()); //$NON-NLS-1$
  211. break;
  212. case COPY:
  213. out.format("C%1$03d\t%2$s\t%3$s", valueOf(ent.getScore()), // //$NON-NLS-1$
  214. ent.getOldPath(), ent.getNewPath());
  215. out.println();
  216. break;
  217. case RENAME:
  218. out.format("R%1$03d\t%2$s\t%3$s", valueOf(ent.getScore()), // //$NON-NLS-1$
  219. ent.getOldPath(), ent.getNewPath());
  220. out.println();
  221. break;
  222. }
  223. }
  224. }
  225. }