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

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