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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.lib.FileMode;
  56. import org.eclipse.jgit.lib.ObjectId;
  57. import org.eclipse.jgit.pgm.opt.PathTreeFilterHandler;
  58. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  59. import org.eclipse.jgit.treewalk.TreeWalk;
  60. import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
  61. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  62. @Command(common = true, usage = "usage_ShowDiffs")
  63. class Diff extends TextBuiltin {
  64. @Argument(index = 0, metaVar = "metaVar_treeish", required = true)
  65. void tree_0(final AbstractTreeIterator c) {
  66. trees.add(c);
  67. }
  68. @Argument(index = 1, metaVar = "metaVar_treeish", required = true)
  69. private final List<AbstractTreeIterator> trees = new ArrayList<AbstractTreeIterator>();
  70. @Option(name = "--", metaVar = "metaVar_port", multiValued = true, handler = PathTreeFilterHandler.class)
  71. private TreeFilter pathFilter = TreeFilter.ALL;
  72. private DiffFormatter fmt = new DiffFormatter();
  73. @Override
  74. protected void run() throws Exception {
  75. final TreeWalk walk = new TreeWalk(db);
  76. walk.reset();
  77. walk.setRecursive(true);
  78. for (final AbstractTreeIterator i : trees)
  79. walk.addTree(i);
  80. walk.setFilter(AndTreeFilter.create(TreeFilter.ANY_DIFF, pathFilter));
  81. while (walk.next())
  82. outputDiff(System.out, walk.getPathString(),
  83. walk.getObjectId(0), walk.getFileMode(0),
  84. walk.getObjectId(1), walk.getFileMode(1));
  85. }
  86. protected void outputDiff(PrintStream out, String path,
  87. ObjectId id1, FileMode mode1, ObjectId id2, FileMode mode2) throws IOException {
  88. String name1 = "a/" + path;
  89. String name2 = "b/" + path;
  90. out.println("diff --git " + name1 + " " + name2);
  91. boolean isNew=false;
  92. boolean isDelete=false;
  93. if (id1.equals(ObjectId.zeroId())) {
  94. out.println("new file mode " + mode2);
  95. isNew=true;
  96. } else if (id2.equals(ObjectId.zeroId())) {
  97. out.println("deleted file mode " + mode1);
  98. isDelete=true;
  99. } else if (!mode1.equals(mode2)) {
  100. out.println("old mode " + mode1);
  101. out.println("new mode " + mode2);
  102. }
  103. out.println("index " + id1.abbreviate(db, 7).name()
  104. + ".." + id2.abbreviate(db, 7).name()
  105. + (mode1.equals(mode2) ? " " + mode1 : ""));
  106. out.println("--- " + (isNew ? "/dev/null" : name1));
  107. out.println("+++ " + (isDelete ? "/dev/null" : name2));
  108. RawText a = getRawText(id1);
  109. RawText b = getRawText(id2);
  110. MyersDiff diff = new MyersDiff(a, b);
  111. fmt.formatEdits(out, a, b, diff.getEdits());
  112. }
  113. private RawText getRawText(ObjectId id) throws IOException {
  114. if (id.equals(ObjectId.zeroId()))
  115. return new RawText(new byte[] { });
  116. return new RawText(db.openBlob(id).getCachedBytes());
  117. }
  118. }