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

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