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.

EGitPatchHistoryTest.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (C) 2008, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.patch;
  44. import java.io.BufferedReader;
  45. import java.io.IOException;
  46. import java.io.InputStreamReader;
  47. import java.io.UnsupportedEncodingException;
  48. import java.util.HashMap;
  49. import java.util.HashSet;
  50. import junit.framework.TestCase;
  51. import org.eclipse.jgit.lib.Constants;
  52. import org.eclipse.jgit.util.MutableInteger;
  53. import org.eclipse.jgit.util.RawParseUtils;
  54. import org.eclipse.jgit.util.TemporaryBuffer;
  55. public class EGitPatchHistoryTest extends TestCase {
  56. public void testParseHistory() throws Exception {
  57. final NumStatReader numstat = new NumStatReader();
  58. numstat.read();
  59. final HashMap<String, HashMap<String, StatInfo>> stats = numstat.stats;
  60. assertEquals(1211, stats.size());
  61. new PatchReader(stats).read();
  62. }
  63. static class StatInfo {
  64. int added, deleted;
  65. }
  66. static class PatchReader extends CommitReader {
  67. final HashSet<String> offBy1;
  68. final HashMap<String, HashMap<String, StatInfo>> stats;
  69. int errors;
  70. PatchReader(final HashMap<String, HashMap<String, StatInfo>> s)
  71. throws IOException {
  72. super(new String[] { "-p" });
  73. stats = s;
  74. offBy1 = new HashSet<String>();
  75. offBy1.add("9bda5ece6806cd797416eaa47c7b927cc6e9c3b2");
  76. }
  77. @Override
  78. void onCommit(String cid, byte[] buf) {
  79. final HashMap<String, StatInfo> files = stats.remove(cid);
  80. assertNotNull("No files for " + cid, files);
  81. final Patch p = new Patch();
  82. p.parse(buf, 0, buf.length - 1);
  83. assertEquals("File count " + cid, files.size(), p.getFiles().size());
  84. if (!p.getErrors().isEmpty()) {
  85. for (final FormatError e : p.getErrors()) {
  86. System.out.println("error " + e.getMessage());
  87. System.out.println(" at " + e.getLineText());
  88. }
  89. dump(buf);
  90. fail("Unexpected error in " + cid);
  91. }
  92. for (final FileHeader fh : p.getFiles()) {
  93. final String fileName;
  94. if (fh.getChangeType() != FileHeader.ChangeType.DELETE)
  95. fileName = fh.getNewName();
  96. else
  97. fileName = fh.getOldName();
  98. final StatInfo s = files.remove(fileName);
  99. final String nid = fileName + " in " + cid;
  100. assertNotNull("No " + nid, s);
  101. int added = 0, deleted = 0;
  102. for (final HunkHeader h : fh.getHunks()) {
  103. added += h.getOldImage().getLinesAdded();
  104. deleted += h.getOldImage().getLinesDeleted();
  105. }
  106. if (s.added == added) {
  107. //
  108. } else if (s.added == added + 1 && offBy1.contains(cid)) {
  109. //
  110. } else {
  111. dump(buf);
  112. assertEquals("Added diff in " + nid, s.added, added);
  113. }
  114. if (s.deleted == deleted) {
  115. //
  116. } else if (s.deleted == deleted + 1 && offBy1.contains(cid)) {
  117. //
  118. } else {
  119. dump(buf);
  120. assertEquals("Deleted diff in " + nid, s.deleted, deleted);
  121. }
  122. }
  123. assertTrue("Missed files in " + cid, files.isEmpty());
  124. }
  125. private static void dump(final byte[] buf) {
  126. String str;
  127. try {
  128. str = new String(buf, 0, buf.length - 1, "ISO-8859-1");
  129. } catch (UnsupportedEncodingException e) {
  130. throw new RuntimeException(e);
  131. }
  132. System.out.println("<<" + str + ">>");
  133. }
  134. }
  135. static class NumStatReader extends CommitReader {
  136. final HashMap<String, HashMap<String, StatInfo>> stats = new HashMap<String, HashMap<String, StatInfo>>();
  137. NumStatReader() throws IOException {
  138. super(new String[] { "--numstat" });
  139. }
  140. @Override
  141. void onCommit(String commitId, byte[] buf) {
  142. final HashMap<String, StatInfo> files = new HashMap<String, StatInfo>();
  143. final MutableInteger ptr = new MutableInteger();
  144. while (ptr.value < buf.length) {
  145. if (buf[ptr.value] == '\n')
  146. break;
  147. final StatInfo i = new StatInfo();
  148. i.added = RawParseUtils.parseBase10(buf, ptr.value, ptr);
  149. i.deleted = RawParseUtils.parseBase10(buf, ptr.value + 1, ptr);
  150. final int eol = RawParseUtils.nextLF(buf, ptr.value);
  151. final String name = RawParseUtils.decode(Constants.CHARSET,
  152. buf, ptr.value + 1, eol - 1);
  153. files.put(name, i);
  154. ptr.value = eol;
  155. }
  156. stats.put(commitId, files);
  157. }
  158. }
  159. static abstract class CommitReader {
  160. private Process proc;
  161. CommitReader(final String[] args) throws IOException {
  162. final String[] realArgs = new String[3 + args.length + 1];
  163. realArgs[0] = "git";
  164. realArgs[1] = "log";
  165. realArgs[2] = "--pretty=format:commit %H";
  166. System.arraycopy(args, 0, realArgs, 3, args.length);
  167. realArgs[3 + args.length] = "a4b98ed15ea5f165a7aa0f2fd2ea6fcce6710925";
  168. proc = Runtime.getRuntime().exec(realArgs);
  169. proc.getOutputStream().close();
  170. proc.getErrorStream().close();
  171. }
  172. void read() throws IOException, InterruptedException {
  173. final BufferedReader in = new BufferedReader(new InputStreamReader(
  174. proc.getInputStream(), "ISO-8859-1"));
  175. String commitId = null;
  176. TemporaryBuffer buf = null;
  177. for (;;) {
  178. String line = in.readLine();
  179. if (line == null)
  180. break;
  181. if (line.startsWith("commit ")) {
  182. if (buf != null) {
  183. buf.close();
  184. onCommit(commitId, buf.toByteArray());
  185. buf.destroy();
  186. }
  187. commitId = line.substring("commit ".length());
  188. buf = new TemporaryBuffer();
  189. } else if (buf != null) {
  190. buf.write(line.getBytes("ISO-8859-1"));
  191. buf.write('\n');
  192. }
  193. }
  194. in.close();
  195. assertEquals(0, proc.waitFor());
  196. proc = null;
  197. }
  198. abstract void onCommit(String commitId, byte[] buf);
  199. }
  200. }