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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (C) 2008-2009, 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 static java.nio.charset.StandardCharsets.ISO_8859_1;
  45. import static java.nio.charset.StandardCharsets.UTF_8;
  46. import static org.junit.Assert.assertEquals;
  47. import static org.junit.Assert.assertNotNull;
  48. import static org.junit.Assert.assertTrue;
  49. import static org.junit.Assert.fail;
  50. import java.io.BufferedReader;
  51. import java.io.IOException;
  52. import java.io.InputStreamReader;
  53. import java.io.UnsupportedEncodingException;
  54. import java.util.HashMap;
  55. import java.util.HashSet;
  56. import org.eclipse.jgit.util.MutableInteger;
  57. import org.eclipse.jgit.util.RawParseUtils;
  58. import org.eclipse.jgit.util.TemporaryBuffer;
  59. import org.junit.Test;
  60. public class EGitPatchHistoryTest {
  61. @Test
  62. public void testParseHistory() throws Exception {
  63. final NumStatReader numstat = new NumStatReader();
  64. numstat.read();
  65. final HashMap<String, HashMap<String, StatInfo>> stats = numstat.stats;
  66. assertEquals(1211, stats.size());
  67. new PatchReader(stats).read();
  68. }
  69. static class StatInfo {
  70. int added, deleted;
  71. }
  72. static class PatchReader extends CommitReader {
  73. final HashSet<String> offBy1;
  74. final HashMap<String, HashMap<String, StatInfo>> stats;
  75. int errors;
  76. PatchReader(HashMap<String, HashMap<String, StatInfo>> s)
  77. throws IOException {
  78. super(new String[] { "-p" });
  79. stats = s;
  80. offBy1 = new HashSet<>();
  81. offBy1.add("9bda5ece6806cd797416eaa47c7b927cc6e9c3b2");
  82. }
  83. @Override
  84. void onCommit(String cid, byte[] buf) {
  85. final HashMap<String, StatInfo> files = stats.remove(cid);
  86. assertNotNull("No files for " + cid, files);
  87. final Patch p = new Patch();
  88. p.parse(buf, 0, buf.length - 1);
  89. assertEquals("File count " + cid, files.size(), p.getFiles().size());
  90. if (!p.getErrors().isEmpty()) {
  91. for (FormatError e : p.getErrors()) {
  92. System.out.println("error " + e.getMessage());
  93. System.out.println(" at " + e.getLineText());
  94. }
  95. dump(buf);
  96. fail("Unexpected error in " + cid);
  97. }
  98. for (FileHeader fh : p.getFiles()) {
  99. final String fileName;
  100. if (fh.getChangeType() != FileHeader.ChangeType.DELETE)
  101. fileName = fh.getNewPath();
  102. else
  103. fileName = fh.getOldPath();
  104. final StatInfo s = files.remove(fileName);
  105. final String nid = fileName + " in " + cid;
  106. assertNotNull("No " + nid, s);
  107. int added = 0, deleted = 0;
  108. for (HunkHeader h : fh.getHunks()) {
  109. added += h.getOldImage().getLinesAdded();
  110. deleted += h.getOldImage().getLinesDeleted();
  111. }
  112. if (s.added == added) {
  113. //
  114. } else if (s.added == added + 1 && offBy1.contains(cid)) {
  115. //
  116. } else {
  117. dump(buf);
  118. assertEquals("Added diff in " + nid, s.added, added);
  119. }
  120. if (s.deleted == deleted) {
  121. //
  122. } else if (s.deleted == deleted + 1 && offBy1.contains(cid)) {
  123. //
  124. } else {
  125. dump(buf);
  126. assertEquals("Deleted diff in " + nid, s.deleted, deleted);
  127. }
  128. }
  129. assertTrue("Missed files in " + cid, files.isEmpty());
  130. }
  131. private static void dump(byte[] buf) {
  132. String str;
  133. try {
  134. str = new String(buf, 0, buf.length - 1, "ISO-8859-1");
  135. } catch (UnsupportedEncodingException e) {
  136. throw new RuntimeException(e);
  137. }
  138. System.out.println("<<" + str + ">>");
  139. }
  140. }
  141. static class NumStatReader extends CommitReader {
  142. final HashMap<String, HashMap<String, StatInfo>> stats = new HashMap<>();
  143. NumStatReader() throws IOException {
  144. super(new String[] { "--numstat" });
  145. }
  146. @Override
  147. void onCommit(String commitId, byte[] buf) {
  148. final HashMap<String, StatInfo> files = new HashMap<>();
  149. final MutableInteger ptr = new MutableInteger();
  150. while (ptr.value < buf.length) {
  151. if (buf[ptr.value] == '\n')
  152. break;
  153. final StatInfo i = new StatInfo();
  154. i.added = RawParseUtils.parseBase10(buf, ptr.value, ptr);
  155. i.deleted = RawParseUtils.parseBase10(buf, ptr.value + 1, ptr);
  156. final int eol = RawParseUtils.nextLF(buf, ptr.value);
  157. final String name = RawParseUtils.decode(UTF_8,
  158. buf, ptr.value + 1, eol - 1);
  159. files.put(name, i);
  160. ptr.value = eol;
  161. }
  162. stats.put(commitId, files);
  163. }
  164. }
  165. static abstract class CommitReader {
  166. private Process proc;
  167. CommitReader(String[] args) throws IOException {
  168. final String[] realArgs = new String[3 + args.length + 1];
  169. realArgs[0] = "git";
  170. realArgs[1] = "log";
  171. realArgs[2] = "--pretty=format:commit %H";
  172. System.arraycopy(args, 0, realArgs, 3, args.length);
  173. realArgs[3 + args.length] = "a4b98ed15ea5f165a7aa0f2fd2ea6fcce6710925";
  174. proc = Runtime.getRuntime().exec(realArgs);
  175. proc.getOutputStream().close();
  176. proc.getErrorStream().close();
  177. }
  178. void read() throws IOException, InterruptedException {
  179. try (BufferedReader in = new BufferedReader(
  180. new InputStreamReader(proc.getInputStream(), ISO_8859_1))) {
  181. String commitId = null;
  182. TemporaryBuffer buf = null;
  183. for (;;) {
  184. String line = in.readLine();
  185. if (line == null)
  186. break;
  187. if (line.startsWith("commit ")) {
  188. if (buf != null) {
  189. buf.close();
  190. onCommit(commitId, buf.toByteArray());
  191. buf.destroy();
  192. }
  193. commitId = line.substring("commit ".length());
  194. buf = new TemporaryBuffer.LocalFile(null);
  195. } else if (buf != null) {
  196. buf.write(line.getBytes(ISO_8859_1));
  197. buf.write('\n');
  198. }
  199. }
  200. }
  201. assertEquals(0, proc.waitFor());
  202. proc = null;
  203. }
  204. abstract void onCommit(String commitId, byte[] buf);
  205. }
  206. }