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 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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> and others
  5. *
  6. * This program and the accompanying materials are made available under the
  7. * terms of the Eclipse Distribution License v. 1.0 which is available at
  8. * https://www.eclipse.org/org/documents/edl-v10.php.
  9. *
  10. * SPDX-License-Identifier: BSD-3-Clause
  11. */
  12. package org.eclipse.jgit.pgm;
  13. import static java.lang.Integer.valueOf;
  14. import static org.eclipse.jgit.lib.Constants.HEAD;
  15. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_STRING_LENGTH;
  16. import java.io.BufferedOutputStream;
  17. import java.io.IOException;
  18. import java.text.MessageFormat;
  19. import java.util.List;
  20. import java.util.concurrent.TimeUnit;
  21. import org.eclipse.jgit.diff.DiffAlgorithm;
  22. import org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm;
  23. import org.eclipse.jgit.diff.DiffEntry;
  24. import org.eclipse.jgit.diff.DiffFormatter;
  25. import org.eclipse.jgit.diff.RawTextComparator;
  26. import org.eclipse.jgit.diff.RenameDetector;
  27. import org.eclipse.jgit.dircache.DirCacheIterator;
  28. import org.eclipse.jgit.errors.RevisionSyntaxException;
  29. import org.eclipse.jgit.lib.ObjectId;
  30. import org.eclipse.jgit.lib.ObjectReader;
  31. import org.eclipse.jgit.lib.Repository;
  32. import org.eclipse.jgit.lib.TextProgressMonitor;
  33. import org.eclipse.jgit.pgm.internal.CLIText;
  34. import org.eclipse.jgit.pgm.opt.PathTreeFilterHandler;
  35. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  36. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  37. import org.eclipse.jgit.treewalk.FileTreeIterator;
  38. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  39. import org.eclipse.jgit.util.io.ThrowingPrintWriter;
  40. import org.kohsuke.args4j.Argument;
  41. import org.kohsuke.args4j.Option;
  42. @Command(common = true, usage = "usage_ShowDiffs")
  43. class Diff extends TextBuiltin {
  44. private DiffFormatter diffFmt;
  45. @Argument(index = 0, metaVar = "metaVar_treeish")
  46. private AbstractTreeIterator oldTree;
  47. @Argument(index = 1, metaVar = "metaVar_treeish")
  48. private AbstractTreeIterator newTree;
  49. @Option(name = "--cached", aliases = { "--staged" }, usage = "usage_cached")
  50. private boolean cached;
  51. @Option(name = "--", metaVar = "metaVar_paths", handler = PathTreeFilterHandler.class)
  52. private TreeFilter pathFilter = TreeFilter.ALL;
  53. // BEGIN -- Options shared with Log
  54. @Option(name = "-p", usage = "usage_showPatch")
  55. boolean showPatch;
  56. @Option(name = "-M", usage = "usage_detectRenames")
  57. private Boolean detectRenames;
  58. @Option(name = "--no-renames", usage = "usage_noRenames")
  59. void noRenames(@SuppressWarnings("unused") boolean on) {
  60. detectRenames = Boolean.FALSE;
  61. }
  62. @Option(name = "--algorithm", metaVar = "metaVar_diffAlg", usage = "usage_diffAlgorithm")
  63. void setAlgorithm(SupportedAlgorithm s) {
  64. diffFmt.setDiffAlgorithm(DiffAlgorithm.getAlgorithm(s));
  65. }
  66. @Option(name = "-l", usage = "usage_renameLimit")
  67. private Integer renameLimit;
  68. @Option(name = "--name-status", usage = "usage_nameStatus")
  69. private boolean showNameAndStatusOnly;
  70. @Option(name = "--ignore-space-at-eol")
  71. void ignoreSpaceAtEol(@SuppressWarnings("unused") boolean on) {
  72. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_TRAILING);
  73. }
  74. @Option(name = "--ignore-leading-space")
  75. void ignoreLeadingSpace(@SuppressWarnings("unused") boolean on) {
  76. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_LEADING);
  77. }
  78. @Option(name = "-b", aliases = { "--ignore-space-change" })
  79. void ignoreSpaceChange(@SuppressWarnings("unused") boolean on) {
  80. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_CHANGE);
  81. }
  82. @Option(name = "-w", aliases = { "--ignore-all-space" })
  83. void ignoreAllSpace(@SuppressWarnings("unused") boolean on) {
  84. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_ALL);
  85. }
  86. @Option(name = "-U", aliases = { "--unified" }, metaVar = "metaVar_linesOfContext")
  87. void unified(int lines) {
  88. diffFmt.setContext(lines);
  89. }
  90. @Option(name = "--abbrev", metaVar = "metaVar_n")
  91. void abbrev(int lines) {
  92. diffFmt.setAbbreviationLength(lines);
  93. }
  94. @Option(name = "--full-index")
  95. void abbrev(@SuppressWarnings("unused") boolean on) {
  96. diffFmt.setAbbreviationLength(OBJECT_ID_STRING_LENGTH);
  97. }
  98. @Option(name = "--src-prefix", metaVar = "metaVar_prefix", usage = "usage_srcPrefix")
  99. void sourcePrefix(String path) {
  100. diffFmt.setOldPrefix(path);
  101. }
  102. @Option(name = "--dst-prefix", metaVar = "metaVar_prefix", usage = "usage_dstPrefix")
  103. void dstPrefix(String path) {
  104. diffFmt.setNewPrefix(path);
  105. }
  106. @Option(name = "--no-prefix", usage = "usage_noPrefix")
  107. void noPrefix(@SuppressWarnings("unused") boolean on) {
  108. diffFmt.setOldPrefix(""); //$NON-NLS-1$
  109. diffFmt.setNewPrefix(""); //$NON-NLS-1$
  110. }
  111. // END -- Options shared with Log
  112. /** {@inheritDoc} */
  113. @Override
  114. protected void init(Repository repository, String gitDir) {
  115. super.init(repository, gitDir);
  116. diffFmt = new DiffFormatter(new BufferedOutputStream(outs));
  117. }
  118. /** {@inheritDoc} */
  119. @Override
  120. protected void run() {
  121. diffFmt.setRepository(db);
  122. try {
  123. if (cached) {
  124. if (oldTree == null) {
  125. ObjectId head = db.resolve(HEAD + "^{tree}"); //$NON-NLS-1$
  126. if (head == null) {
  127. die(MessageFormat.format(CLIText.get().notATree, HEAD));
  128. }
  129. CanonicalTreeParser p = new CanonicalTreeParser();
  130. try (ObjectReader reader = db.newObjectReader()) {
  131. p.reset(reader, head);
  132. }
  133. oldTree = p;
  134. }
  135. newTree = new DirCacheIterator(db.readDirCache());
  136. } else if (oldTree == null) {
  137. oldTree = new DirCacheIterator(db.readDirCache());
  138. newTree = new FileTreeIterator(db);
  139. } else if (newTree == null) {
  140. newTree = new FileTreeIterator(db);
  141. }
  142. TextProgressMonitor pm = new TextProgressMonitor(errw);
  143. pm.setDelayStart(2, TimeUnit.SECONDS);
  144. diffFmt.setProgressMonitor(pm);
  145. diffFmt.setPathFilter(pathFilter);
  146. if (detectRenames != null) {
  147. diffFmt.setDetectRenames(detectRenames.booleanValue());
  148. }
  149. if (renameLimit != null && diffFmt.isDetectRenames()) {
  150. RenameDetector rd = diffFmt.getRenameDetector();
  151. rd.setRenameLimit(renameLimit.intValue());
  152. }
  153. if (showNameAndStatusOnly) {
  154. nameStatus(outw, diffFmt.scan(oldTree, newTree));
  155. outw.flush();
  156. } else {
  157. diffFmt.format(oldTree, newTree);
  158. diffFmt.flush();
  159. }
  160. } catch (RevisionSyntaxException | IOException e) {
  161. throw die(e.getMessage(), e);
  162. } finally {
  163. diffFmt.close();
  164. }
  165. }
  166. static void nameStatus(ThrowingPrintWriter out, List<DiffEntry> files)
  167. throws IOException {
  168. for (DiffEntry ent : files) {
  169. switch (ent.getChangeType()) {
  170. case ADD:
  171. out.println("A\t" + ent.getNewPath()); //$NON-NLS-1$
  172. break;
  173. case DELETE:
  174. out.println("D\t" + ent.getOldPath()); //$NON-NLS-1$
  175. break;
  176. case MODIFY:
  177. out.println("M\t" + ent.getNewPath()); //$NON-NLS-1$
  178. break;
  179. case COPY:
  180. out.format("C%1$03d\t%2$s\t%3$s", valueOf(ent.getScore()), // //$NON-NLS-1$
  181. ent.getOldPath(), ent.getNewPath());
  182. out.println();
  183. break;
  184. case RENAME:
  185. out.format("R%1$03d\t%2$s\t%3$s", valueOf(ent.getScore()), // //$NON-NLS-1$
  186. ent.getOldPath(), ent.getNewPath());
  187. out.println();
  188. break;
  189. }
  190. }
  191. }
  192. }