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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 org.eclipse.jgit.lib.Constants.HEAD;
  47. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_STRING_LENGTH;
  48. import java.io.BufferedOutputStream;
  49. import java.io.PrintWriter;
  50. import java.text.MessageFormat;
  51. import java.util.List;
  52. import java.util.concurrent.TimeUnit;
  53. import org.eclipse.jgit.diff.DiffAlgorithm;
  54. import org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm;
  55. import org.eclipse.jgit.diff.DiffEntry;
  56. import org.eclipse.jgit.diff.DiffFormatter;
  57. import org.eclipse.jgit.diff.RawTextComparator;
  58. import org.eclipse.jgit.diff.RenameDetector;
  59. import org.eclipse.jgit.dircache.DirCacheIterator;
  60. import org.eclipse.jgit.lib.ObjectId;
  61. import org.eclipse.jgit.lib.ObjectReader;
  62. import org.eclipse.jgit.lib.TextProgressMonitor;
  63. import org.eclipse.jgit.pgm.opt.PathTreeFilterHandler;
  64. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  65. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  66. import org.eclipse.jgit.treewalk.FileTreeIterator;
  67. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  68. import org.kohsuke.args4j.Argument;
  69. import org.kohsuke.args4j.Option;
  70. @Command(common = true, usage = "usage_ShowDiffs")
  71. class Diff extends TextBuiltin {
  72. private final DiffFormatter diffFmt = new DiffFormatter( //
  73. new BufferedOutputStream(System.out));
  74. @Argument(index = 0, metaVar = "metaVar_treeish")
  75. private AbstractTreeIterator oldTree;
  76. @Argument(index = 1, metaVar = "metaVar_treeish")
  77. private AbstractTreeIterator newTree;
  78. @Option(name = "--cached", usage = "usage_cached")
  79. private boolean cached;
  80. @Option(name = "--", metaVar = "metaVar_paths", multiValued = true, handler = PathTreeFilterHandler.class)
  81. private TreeFilter pathFilter = TreeFilter.ALL;
  82. // BEGIN -- Options shared with Log
  83. @Option(name = "-p", usage = "usage_showPatch")
  84. boolean showPatch;
  85. @Option(name = "-M", usage = "usage_detectRenames")
  86. private Boolean detectRenames;
  87. @Option(name = "--no-renames", usage = "usage_noRenames")
  88. void noRenames(@SuppressWarnings("unused") boolean on) {
  89. detectRenames = Boolean.FALSE;
  90. }
  91. @Option(name = "--algorithm", metaVar = "metaVar_diffAlg", usage = "usage_diffAlgorithm")
  92. void setAlgorithm(SupportedAlgorithm s) {
  93. diffFmt.setDiffAlgorithm(DiffAlgorithm.getAlgorithm(s));
  94. }
  95. @Option(name = "-l", usage = "usage_renameLimit")
  96. private Integer renameLimit;
  97. @Option(name = "--name-status", usage = "usage_nameStatus")
  98. private boolean showNameAndStatusOnly;
  99. @Option(name = "--ignore-space-at-eol")
  100. void ignoreSpaceAtEol(@SuppressWarnings("unused") boolean on) {
  101. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_TRAILING);
  102. }
  103. @Option(name = "--ignore-leading-space")
  104. void ignoreLeadingSpace(@SuppressWarnings("unused") boolean on) {
  105. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_LEADING);
  106. }
  107. @Option(name = "-b", aliases = { "--ignore-space-change" })
  108. void ignoreSpaceChange(@SuppressWarnings("unused") boolean on) {
  109. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_CHANGE);
  110. }
  111. @Option(name = "-w", aliases = { "--ignore-all-space" })
  112. void ignoreAllSpace(@SuppressWarnings("unused") boolean on) {
  113. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_ALL);
  114. }
  115. @Option(name = "-U", aliases = { "--unified" }, metaVar = "metaVar_linesOfContext")
  116. void unified(int lines) {
  117. diffFmt.setContext(lines);
  118. }
  119. @Option(name = "--abbrev", metaVar = "metaVar_n")
  120. void abbrev(int lines) {
  121. diffFmt.setAbbreviationLength(lines);
  122. }
  123. @Option(name = "--full-index")
  124. void abbrev(@SuppressWarnings("unused") boolean on) {
  125. diffFmt.setAbbreviationLength(OBJECT_ID_STRING_LENGTH);
  126. }
  127. @Option(name = "--src-prefix", usage = "usage_srcPrefix")
  128. void sourcePrefix(String path) {
  129. diffFmt.setOldPrefix(path);
  130. }
  131. @Option(name = "--dst-prefix", usage = "usage_dstPrefix")
  132. void dstPrefix(String path) {
  133. diffFmt.setNewPrefix(path);
  134. }
  135. @Option(name = "--no-prefix", usage = "usage_noPrefix")
  136. void noPrefix(@SuppressWarnings("unused") boolean on) {
  137. diffFmt.setOldPrefix("");
  138. diffFmt.setNewPrefix("");
  139. }
  140. // END -- Options shared with Log
  141. @Override
  142. protected void run() throws Exception {
  143. diffFmt.setRepository(db);
  144. try {
  145. if (cached) {
  146. if (oldTree == null) {
  147. ObjectId head = db.resolve(HEAD + "^{tree}");
  148. if (head == null)
  149. die(MessageFormat.format(CLIText.get().notATree, HEAD));
  150. CanonicalTreeParser p = new CanonicalTreeParser();
  151. ObjectReader reader = db.newObjectReader();
  152. try {
  153. p.reset(reader, head);
  154. } finally {
  155. reader.release();
  156. }
  157. oldTree = p;
  158. }
  159. newTree = new DirCacheIterator(db.readDirCache());
  160. } else if (oldTree == null) {
  161. oldTree = new DirCacheIterator(db.readDirCache());
  162. newTree = new FileTreeIterator(db);
  163. } else if (newTree == null)
  164. newTree = new FileTreeIterator(db);
  165. TextProgressMonitor pm = new TextProgressMonitor();
  166. pm.setDelayStart(2, TimeUnit.SECONDS);
  167. diffFmt.setProgressMonitor(pm);
  168. diffFmt.setPathFilter(pathFilter);
  169. if (detectRenames != null)
  170. diffFmt.setDetectRenames(detectRenames.booleanValue());
  171. if (renameLimit != null && diffFmt.isDetectRenames()) {
  172. RenameDetector rd = diffFmt.getRenameDetector();
  173. rd.setRenameLimit(renameLimit.intValue());
  174. }
  175. if (showNameAndStatusOnly) {
  176. nameStatus(out, diffFmt.scan(oldTree, newTree));
  177. out.flush();
  178. } else {
  179. diffFmt.format(oldTree, newTree);
  180. diffFmt.flush();
  181. }
  182. } finally {
  183. diffFmt.release();
  184. }
  185. }
  186. static void nameStatus(PrintWriter out, List<DiffEntry> files) {
  187. for (DiffEntry ent : files) {
  188. switch (ent.getChangeType()) {
  189. case ADD:
  190. out.println("A\t" + ent.getNewPath());
  191. break;
  192. case DELETE:
  193. out.println("D\t" + ent.getOldPath());
  194. break;
  195. case MODIFY:
  196. out.println("M\t" + ent.getNewPath());
  197. break;
  198. case COPY:
  199. out.format("C%1$03d\t%2$s\t%3$s", ent.getScore(), //
  200. ent.getOldPath(), ent.getNewPath());
  201. out.println();
  202. break;
  203. case RENAME:
  204. out.format("R%1$03d\t%2$s\t%3$s", ent.getScore(), //
  205. ent.getOldPath(), ent.getNewPath());
  206. out.println();
  207. break;
  208. }
  209. }
  210. }
  211. }