Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Diff.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 java.io.BufferedOutputStream;
  47. import java.io.PrintWriter;
  48. import java.util.List;
  49. import org.eclipse.jgit.diff.DiffEntry;
  50. import org.eclipse.jgit.diff.DiffFormatter;
  51. import org.eclipse.jgit.diff.RawTextIgnoreAllWhitespace;
  52. import org.eclipse.jgit.diff.RawTextIgnoreLeadingWhitespace;
  53. import org.eclipse.jgit.diff.RawTextIgnoreTrailingWhitespace;
  54. import org.eclipse.jgit.diff.RawTextIgnoreWhitespaceChange;
  55. import org.eclipse.jgit.diff.RenameDetector;
  56. import org.eclipse.jgit.lib.Constants;
  57. import org.eclipse.jgit.lib.TextProgressMonitor;
  58. import org.eclipse.jgit.pgm.opt.PathTreeFilterHandler;
  59. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  60. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  61. import org.kohsuke.args4j.Argument;
  62. import org.kohsuke.args4j.Option;
  63. @Command(common = true, usage = "usage_ShowDiffs")
  64. class Diff extends TextBuiltin {
  65. private final DiffFormatter diffFmt = new DiffFormatter( //
  66. new BufferedOutputStream(System.out));
  67. @Argument(index = 0, metaVar = "metaVar_treeish", required = true)
  68. private AbstractTreeIterator oldTree;
  69. @Argument(index = 1, metaVar = "metaVar_treeish", required = true)
  70. private AbstractTreeIterator newTree;
  71. @Option(name = "--", metaVar = "metaVar_paths", multiValued = true, handler = PathTreeFilterHandler.class)
  72. private TreeFilter pathFilter = TreeFilter.ALL;
  73. // BEGIN -- Options shared with Log
  74. @Option(name = "-p", usage = "usage_showPatch")
  75. boolean showPatch;
  76. @Option(name = "-M", usage = "usage_detectRenames")
  77. private Boolean detectRenames;
  78. @Option(name = "--no-renames", usage = "usage_noRenames")
  79. void noRenames(@SuppressWarnings("unused") boolean on) {
  80. detectRenames = Boolean.FALSE;
  81. }
  82. @Option(name = "-l", usage = "usage_renameLimit")
  83. private Integer renameLimit;
  84. @Option(name = "--name-status", usage = "usage_nameStatus")
  85. private boolean showNameAndStatusOnly;
  86. @Option(name = "--ignore-space-at-eol")
  87. void ignoreSpaceAtEol(@SuppressWarnings("unused") boolean on) {
  88. diffFmt.setRawTextFactory(RawTextIgnoreTrailingWhitespace.FACTORY);
  89. }
  90. @Option(name = "--ignore-leading-space")
  91. void ignoreLeadingSpace(@SuppressWarnings("unused") boolean on) {
  92. diffFmt.setRawTextFactory(RawTextIgnoreLeadingWhitespace.FACTORY);
  93. }
  94. @Option(name = "-b", aliases = { "--ignore-space-change" })
  95. void ignoreSpaceChange(@SuppressWarnings("unused") boolean on) {
  96. diffFmt.setRawTextFactory(RawTextIgnoreWhitespaceChange.FACTORY);
  97. }
  98. @Option(name = "-w", aliases = { "--ignore-all-space" })
  99. void ignoreAllSpace(@SuppressWarnings("unused") boolean on) {
  100. diffFmt.setRawTextFactory(RawTextIgnoreAllWhitespace.FACTORY);
  101. }
  102. @Option(name = "-U", aliases = { "--unified" }, metaVar = "metaVar_linesOfContext")
  103. void unified(int lines) {
  104. diffFmt.setContext(lines);
  105. }
  106. @Option(name = "--abbrev", metaVar = "metaVar_n")
  107. void abbrev(int lines) {
  108. diffFmt.setAbbreviationLength(lines);
  109. }
  110. @Option(name = "--full-index")
  111. void abbrev(@SuppressWarnings("unused") boolean on) {
  112. diffFmt.setAbbreviationLength(Constants.OBJECT_ID_STRING_LENGTH);
  113. }
  114. @Option(name = "--src-prefix", usage = "usage_srcPrefix")
  115. void sourcePrefix(String path) {
  116. diffFmt.setOldPrefix(path);
  117. }
  118. @Option(name = "--dst-prefix", usage = "usage_dstPrefix")
  119. void dstPrefix(String path) {
  120. diffFmt.setNewPrefix(path);
  121. }
  122. @Option(name = "--no-prefix", usage = "usage_noPrefix")
  123. void noPrefix(@SuppressWarnings("unused") boolean on) {
  124. diffFmt.setOldPrefix("");
  125. diffFmt.setNewPrefix("");
  126. }
  127. // END -- Options shared with Log
  128. @Override
  129. protected void run() throws Exception {
  130. diffFmt.setRepository(db);
  131. try {
  132. diffFmt.setProgressMonitor(new TextProgressMonitor());
  133. diffFmt.setPathFilter(pathFilter);
  134. if (detectRenames != null)
  135. diffFmt.setDetectRenames(detectRenames.booleanValue());
  136. if (renameLimit != null && diffFmt.isDetectRenames()) {
  137. RenameDetector rd = diffFmt.getRenameDetector();
  138. rd.setRenameLimit(renameLimit.intValue());
  139. }
  140. if (showNameAndStatusOnly) {
  141. nameStatus(out, diffFmt.scan(oldTree, newTree));
  142. out.flush();
  143. } else {
  144. diffFmt.format(oldTree, newTree);
  145. diffFmt.flush();
  146. }
  147. } finally {
  148. diffFmt.release();
  149. }
  150. }
  151. static void nameStatus(PrintWriter out, List<DiffEntry> files) {
  152. for (DiffEntry ent : files) {
  153. switch (ent.getChangeType()) {
  154. case ADD:
  155. out.println("A\t" + ent.getNewPath());
  156. break;
  157. case DELETE:
  158. out.println("D\t" + ent.getOldPath());
  159. break;
  160. case MODIFY:
  161. out.println("M\t" + ent.getNewPath());
  162. break;
  163. case COPY:
  164. out.format("C%1$03d\t%2$s\t%3$s", ent.getScore(), //
  165. ent.getOldPath(), ent.getNewPath());
  166. out.println();
  167. break;
  168. case RENAME:
  169. out.format("R%1$03d\t%2$s\t%3$s", ent.getScore(), //
  170. ent.getOldPath(), ent.getNewPath());
  171. out.println();
  172. break;
  173. }
  174. }
  175. }
  176. }