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

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