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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.IOException;
  47. import java.io.PrintStream;
  48. import java.util.ArrayList;
  49. import java.util.List;
  50. import org.kohsuke.args4j.Argument;
  51. import org.kohsuke.args4j.Option;
  52. import org.eclipse.jgit.diff.DiffFormatter;
  53. import org.eclipse.jgit.diff.MyersDiff;
  54. import org.eclipse.jgit.diff.RawText;
  55. import org.eclipse.jgit.diff.RawTextIgnoreAllWhitespace;
  56. import org.eclipse.jgit.diff.RawTextIgnoreLeadingWhitespace;
  57. import org.eclipse.jgit.diff.RawTextIgnoreTrailingWhitespace;
  58. import org.eclipse.jgit.diff.RawTextIgnoreWhitespaceChange;
  59. import org.eclipse.jgit.lib.FileMode;
  60. import org.eclipse.jgit.lib.ObjectId;
  61. import org.eclipse.jgit.pgm.opt.PathTreeFilterHandler;
  62. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  63. import org.eclipse.jgit.treewalk.TreeWalk;
  64. import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
  65. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  66. @Command(common = true, usage = "usage_ShowDiffs")
  67. class Diff extends TextBuiltin {
  68. @Argument(index = 0, metaVar = "metaVar_treeish", required = true)
  69. void tree_0(final AbstractTreeIterator c) {
  70. trees.add(c);
  71. }
  72. @Argument(index = 1, metaVar = "metaVar_treeish", required = true)
  73. private final List<AbstractTreeIterator> trees = new ArrayList<AbstractTreeIterator>();
  74. @Option(name = "--", metaVar = "metaVar_port", multiValued = true, handler = PathTreeFilterHandler.class)
  75. private TreeFilter pathFilter = TreeFilter.ALL;
  76. @Option(name = "--ignore-space-at-eol")
  77. private boolean ignoreWsTrailing;
  78. @Option(name = "--ignore-leading-space")
  79. private boolean ignoreWsLeading;
  80. @Option(name = "-b", aliases = { "--ignore-space-change" })
  81. private boolean ignoreWsChange;
  82. @Option(name = "-w", aliases = { "--ignore-all-space" })
  83. private boolean ignoreWsAll;
  84. private DiffFormatter fmt = new DiffFormatter();
  85. @Override
  86. protected void run() throws Exception {
  87. final TreeWalk walk = new TreeWalk(db);
  88. walk.reset();
  89. walk.setRecursive(true);
  90. for (final AbstractTreeIterator i : trees)
  91. walk.addTree(i);
  92. walk.setFilter(AndTreeFilter.create(TreeFilter.ANY_DIFF, pathFilter));
  93. while (walk.next())
  94. outputDiff(System.out, walk.getPathString(),
  95. walk.getObjectId(0), walk.getFileMode(0),
  96. walk.getObjectId(1), walk.getFileMode(1));
  97. }
  98. protected void outputDiff(PrintStream out, String path,
  99. ObjectId id1, FileMode mode1, ObjectId id2, FileMode mode2) throws IOException {
  100. String name1 = "a/" + path;
  101. String name2 = "b/" + path;
  102. out.println("diff --git " + name1 + " " + name2);
  103. boolean isNew=false;
  104. boolean isDelete=false;
  105. if (id1.equals(ObjectId.zeroId())) {
  106. out.println("new file mode " + mode2);
  107. isNew=true;
  108. } else if (id2.equals(ObjectId.zeroId())) {
  109. out.println("deleted file mode " + mode1);
  110. isDelete=true;
  111. } else if (!mode1.equals(mode2)) {
  112. out.println("old mode " + mode1);
  113. out.println("new mode " + mode2);
  114. }
  115. out.println("index " + id1.abbreviate(db, 7).name()
  116. + ".." + id2.abbreviate(db, 7).name()
  117. + (mode1.equals(mode2) ? " " + mode1 : ""));
  118. out.println("--- " + (isNew ? "/dev/null" : name1));
  119. out.println("+++ " + (isDelete ? "/dev/null" : name2));
  120. byte[] aRaw = getRawBytes(id1);
  121. byte[] bRaw = getRawBytes(id2);
  122. if (RawText.isBinary(aRaw) || RawText.isBinary(bRaw)) {
  123. out.println("Binary files differ");
  124. return;
  125. }
  126. RawText a = getRawText(aRaw);
  127. RawText b = getRawText(bRaw);
  128. MyersDiff diff = new MyersDiff(a, b);
  129. fmt.formatEdits(out, a, b, diff.getEdits());
  130. }
  131. private byte[] getRawBytes(ObjectId id) throws IOException {
  132. if (id.equals(ObjectId.zeroId()))
  133. return new byte[] {};
  134. return db.openBlob(id).getCachedBytes();
  135. }
  136. private RawText getRawText(byte[] raw) {
  137. if (ignoreWsAll)
  138. return new RawTextIgnoreAllWhitespace(raw);
  139. else if (ignoreWsTrailing)
  140. return new RawTextIgnoreTrailingWhitespace(raw);
  141. else if (ignoreWsChange)
  142. return new RawTextIgnoreWhitespaceChange(raw);
  143. else if (ignoreWsLeading)
  144. return new RawTextIgnoreLeadingWhitespace(raw);
  145. else
  146. return new RawText(raw);
  147. }
  148. }