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.

RevCommitParseTest.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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.revwalk;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNotNull;
  46. import static org.junit.Assert.assertNull;
  47. import static org.junit.Assert.assertSame;
  48. import java.io.ByteArrayOutputStream;
  49. import java.io.UnsupportedEncodingException;
  50. import java.util.TimeZone;
  51. import org.eclipse.jgit.junit.RepositoryTestCase;
  52. import org.eclipse.jgit.lib.CommitBuilder;
  53. import org.eclipse.jgit.lib.Constants;
  54. import org.eclipse.jgit.lib.ObjectId;
  55. import org.eclipse.jgit.lib.ObjectInserter;
  56. import org.eclipse.jgit.lib.PersonIdent;
  57. import org.junit.Test;
  58. public class RevCommitParseTest extends RepositoryTestCase {
  59. @Test
  60. public void testParse_NoParents() throws Exception {
  61. final ObjectId treeId = id("9788669ad918b6fcce64af8882fc9a81cb6aba67");
  62. final String authorName = "A U. Thor";
  63. final String authorEmail = "a_u_thor@example.com";
  64. final int authorTime = 1218123387;
  65. final String authorTimeZone = "+0700";
  66. final String committerName = "C O. Miter";
  67. final String committerEmail = "comiter@example.com";
  68. final int committerTime = 1218123390;
  69. final String committerTimeZone = "-0500";
  70. final StringBuilder body = new StringBuilder();
  71. body.append("tree ");
  72. body.append(treeId.name());
  73. body.append("\n");
  74. body.append("author ");
  75. body.append(authorName);
  76. body.append(" <");
  77. body.append(authorEmail);
  78. body.append("> ");
  79. body.append(authorTime);
  80. body.append(" ");
  81. body.append(authorTimeZone);
  82. body.append(" \n");
  83. body.append("committer ");
  84. body.append(committerName);
  85. body.append(" <");
  86. body.append(committerEmail);
  87. body.append("> ");
  88. body.append(committerTime);
  89. body.append(" ");
  90. body.append(committerTimeZone);
  91. body.append("\n");
  92. body.append("\n");
  93. final RevWalk rw = new RevWalk(db);
  94. final RevCommit c;
  95. c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
  96. assertNull(c.getTree());
  97. assertNull(c.parents);
  98. c.parseCanonical(rw, body.toString().getBytes("UTF-8"));
  99. assertNotNull(c.getTree());
  100. assertEquals(treeId, c.getTree().getId());
  101. assertSame(rw.lookupTree(treeId), c.getTree());
  102. assertNotNull(c.parents);
  103. assertEquals(0, c.parents.length);
  104. assertEquals("", c.getFullMessage());
  105. final PersonIdent cAuthor = c.getAuthorIdent();
  106. assertNotNull(cAuthor);
  107. assertEquals(authorName, cAuthor.getName());
  108. assertEquals(authorEmail, cAuthor.getEmailAddress());
  109. assertEquals((long)authorTime * 1000, cAuthor.getWhen().getTime());
  110. assertEquals(TimeZone.getTimeZone("GMT" + authorTimeZone), cAuthor.getTimeZone());
  111. final PersonIdent cCommitter = c.getCommitterIdent();
  112. assertNotNull(cCommitter);
  113. assertEquals(committerName, cCommitter.getName());
  114. assertEquals(committerEmail, cCommitter.getEmailAddress());
  115. assertEquals((long)committerTime * 1000, cCommitter.getWhen().getTime());
  116. assertEquals(TimeZone.getTimeZone("GMT" + committerTimeZone), cCommitter.getTimeZone());
  117. }
  118. private RevCommit create(final String msg) throws Exception {
  119. final StringBuilder b = new StringBuilder();
  120. b.append("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n");
  121. b.append("author A U. Thor <a_u_thor@example.com> 1218123387 +0700\n");
  122. b.append("committer C O. Miter <c@example.com> 1218123390 -0500\n");
  123. b.append("\n");
  124. b.append(msg);
  125. final RevCommit c;
  126. c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
  127. c.parseCanonical(new RevWalk(db), b.toString().getBytes("UTF-8"));
  128. return c;
  129. }
  130. @Test
  131. public void testParse_WeirdHeaderOnlyCommit() throws Exception {
  132. final StringBuilder b = new StringBuilder();
  133. b.append("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n");
  134. b.append("author A U. Thor <a_u_thor@example.com> 1218123387 +0700\n");
  135. b.append("committer C O. Miter <c@example.com> 1218123390 -0500\n");
  136. final RevCommit c;
  137. c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
  138. c.parseCanonical(new RevWalk(db), b.toString().getBytes("UTF-8"));
  139. assertEquals("", c.getFullMessage());
  140. assertEquals("", c.getShortMessage());
  141. }
  142. @Test
  143. public void testParse_incompleteAuthorAndCommitter() throws Exception {
  144. final StringBuilder b = new StringBuilder();
  145. b.append("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n");
  146. b.append("author <a_u_thor@example.com> 1218123387 +0700\n");
  147. b.append("committer <> 1218123390 -0500\n");
  148. final RevCommit c;
  149. c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
  150. c.parseCanonical(new RevWalk(db), b.toString().getBytes("UTF-8"));
  151. assertEquals(new PersonIdent("", "a_u_thor@example.com", 1218123387000l, 7), c.getAuthorIdent());
  152. assertEquals(new PersonIdent("", "", 1218123390000l, -5), c.getCommitterIdent());
  153. }
  154. @Test
  155. public void testParse_implicit_UTF8_encoded() throws Exception {
  156. final ByteArrayOutputStream b = new ByteArrayOutputStream();
  157. b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n".getBytes("UTF-8"));
  158. b.write("author F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n".getBytes("UTF-8"));
  159. b.write("committer C O. Miter <c@example.com> 1218123390 -0500\n".getBytes("UTF-8"));
  160. b.write("\n".getBytes("UTF-8"));
  161. b.write("Sm\u00f6rg\u00e5sbord\n".getBytes("UTF-8"));
  162. b.write("\n".getBytes("UTF-8"));
  163. b.write("\u304d\u308c\u3044\n".getBytes("UTF-8"));
  164. final RevCommit c;
  165. c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); // bogus id
  166. c.parseCanonical(new RevWalk(db), b.toByteArray());
  167. assertSame(Constants.CHARSET, c.getEncoding());
  168. assertEquals("F\u00f6r fattare", c.getAuthorIdent().getName());
  169. assertEquals("Sm\u00f6rg\u00e5sbord", c.getShortMessage());
  170. assertEquals("Sm\u00f6rg\u00e5sbord\n\n\u304d\u308c\u3044\n", c.getFullMessage());
  171. }
  172. @Test
  173. public void testParse_implicit_mixed_encoded() throws Exception {
  174. final ByteArrayOutputStream b = new ByteArrayOutputStream();
  175. b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n".getBytes("UTF-8"));
  176. b.write("author F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n".getBytes("ISO-8859-1"));
  177. b.write("committer C O. Miter <c@example.com> 1218123390 -0500\n".getBytes("UTF-8"));
  178. b.write("\n".getBytes("UTF-8"));
  179. b.write("Sm\u00f6rg\u00e5sbord\n".getBytes("UTF-8"));
  180. b.write("\n".getBytes("UTF-8"));
  181. b.write("\u304d\u308c\u3044\n".getBytes("UTF-8"));
  182. final RevCommit c;
  183. c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); // bogus id
  184. c.parseCanonical(new RevWalk(db), b.toByteArray());
  185. assertSame(Constants.CHARSET, c.getEncoding());
  186. assertEquals("F\u00f6r fattare", c.getAuthorIdent().getName());
  187. assertEquals("Sm\u00f6rg\u00e5sbord", c.getShortMessage());
  188. assertEquals("Sm\u00f6rg\u00e5sbord\n\n\u304d\u308c\u3044\n", c.getFullMessage());
  189. }
  190. /**
  191. * Test parsing of a commit whose encoding is given and works.
  192. *
  193. * @throws Exception
  194. */
  195. @Test
  196. public void testParse_explicit_encoded() throws Exception {
  197. final ByteArrayOutputStream b = new ByteArrayOutputStream();
  198. b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n".getBytes("EUC-JP"));
  199. b.write("author F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n".getBytes("EUC-JP"));
  200. b.write("committer C O. Miter <c@example.com> 1218123390 -0500\n".getBytes("EUC-JP"));
  201. b.write("encoding euc_JP\n".getBytes("EUC-JP"));
  202. b.write("\n".getBytes("EUC-JP"));
  203. b.write("\u304d\u308c\u3044\n".getBytes("EUC-JP"));
  204. b.write("\n".getBytes("EUC-JP"));
  205. b.write("Hi\n".getBytes("EUC-JP"));
  206. final RevCommit c;
  207. c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); // bogus id
  208. c.parseCanonical(new RevWalk(db), b.toByteArray());
  209. assertEquals("EUC-JP", c.getEncoding().name());
  210. assertEquals("F\u00f6r fattare", c.getAuthorIdent().getName());
  211. assertEquals("\u304d\u308c\u3044", c.getShortMessage());
  212. assertEquals("\u304d\u308c\u3044\n\nHi\n", c.getFullMessage());
  213. }
  214. /**
  215. * This is a twisted case, but show what we expect here. We can revise the
  216. * expectations provided this case is updated.
  217. *
  218. * What happens here is that an encoding us given, but data is not encoded
  219. * that way (and we can detect it), so we try other encodings.
  220. *
  221. * @throws Exception
  222. */
  223. @Test
  224. public void testParse_explicit_bad_encoded() throws Exception {
  225. final ByteArrayOutputStream b = new ByteArrayOutputStream();
  226. b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n".getBytes("UTF-8"));
  227. b.write("author F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n".getBytes("ISO-8859-1"));
  228. b.write("committer C O. Miter <c@example.com> 1218123390 -0500\n".getBytes("UTF-8"));
  229. b.write("encoding EUC-JP\n".getBytes("UTF-8"));
  230. b.write("\n".getBytes("UTF-8"));
  231. b.write("\u304d\u308c\u3044\n".getBytes("UTF-8"));
  232. b.write("\n".getBytes("UTF-8"));
  233. b.write("Hi\n".getBytes("UTF-8"));
  234. final RevCommit c;
  235. c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); // bogus id
  236. c.parseCanonical(new RevWalk(db), b.toByteArray());
  237. assertEquals("EUC-JP", c.getEncoding().name());
  238. assertEquals("F\u00f6r fattare", c.getAuthorIdent().getName());
  239. assertEquals("\u304d\u308c\u3044", c.getShortMessage());
  240. assertEquals("\u304d\u308c\u3044\n\nHi\n", c.getFullMessage());
  241. }
  242. /**
  243. * This is a twisted case too, but show what we expect here. We can revise the
  244. * expectations provided this case is updated.
  245. *
  246. * What happens here is that an encoding us given, but data is not encoded
  247. * that way (and we can detect it), so we try other encodings. Here data could
  248. * actually be decoded in the stated encoding, but we override using UTF-8.
  249. *
  250. * @throws Exception
  251. */
  252. @Test
  253. public void testParse_explicit_bad_encoded2() throws Exception {
  254. final ByteArrayOutputStream b = new ByteArrayOutputStream();
  255. b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n".getBytes("UTF-8"));
  256. b.write("author F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n".getBytes("UTF-8"));
  257. b.write("committer C O. Miter <c@example.com> 1218123390 -0500\n".getBytes("UTF-8"));
  258. b.write("encoding ISO-8859-1\n".getBytes("UTF-8"));
  259. b.write("\n".getBytes("UTF-8"));
  260. b.write("\u304d\u308c\u3044\n".getBytes("UTF-8"));
  261. b.write("\n".getBytes("UTF-8"));
  262. b.write("Hi\n".getBytes("UTF-8"));
  263. final RevCommit c;
  264. c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); // bogus id
  265. c.parseCanonical(new RevWalk(db), b.toByteArray());
  266. assertEquals("ISO-8859-1", c.getEncoding().name());
  267. assertEquals("F\u00f6r fattare", c.getAuthorIdent().getName());
  268. assertEquals("\u304d\u308c\u3044", c.getShortMessage());
  269. assertEquals("\u304d\u308c\u3044\n\nHi\n", c.getFullMessage());
  270. }
  271. @Test
  272. public void testParse_NoMessage() throws Exception {
  273. final String msg = "";
  274. final RevCommit c = create(msg);
  275. assertEquals(msg, c.getFullMessage());
  276. assertEquals(msg, c.getShortMessage());
  277. }
  278. @Test
  279. public void testParse_OnlyLFMessage() throws Exception {
  280. final RevCommit c = create("\n");
  281. assertEquals("\n", c.getFullMessage());
  282. assertEquals("", c.getShortMessage());
  283. }
  284. @Test
  285. public void testParse_ShortLineOnlyNoLF() throws Exception {
  286. final String shortMsg = "This is a short message.";
  287. final RevCommit c = create(shortMsg);
  288. assertEquals(shortMsg, c.getFullMessage());
  289. assertEquals(shortMsg, c.getShortMessage());
  290. }
  291. @Test
  292. public void testParse_ShortLineOnlyEndLF() throws Exception {
  293. final String shortMsg = "This is a short message.";
  294. final String fullMsg = shortMsg + "\n";
  295. final RevCommit c = create(fullMsg);
  296. assertEquals(fullMsg, c.getFullMessage());
  297. assertEquals(shortMsg, c.getShortMessage());
  298. }
  299. @Test
  300. public void testParse_ShortLineOnlyEmbeddedLF() throws Exception {
  301. final String fullMsg = "This is a\nshort message.";
  302. final String shortMsg = fullMsg.replace('\n', ' ');
  303. final RevCommit c = create(fullMsg);
  304. assertEquals(fullMsg, c.getFullMessage());
  305. assertEquals(shortMsg, c.getShortMessage());
  306. }
  307. @Test
  308. public void testParse_ShortLineOnlyEmbeddedAndEndingLF() throws Exception {
  309. final String fullMsg = "This is a\nshort message.\n";
  310. final String shortMsg = "This is a short message.";
  311. final RevCommit c = create(fullMsg);
  312. assertEquals(fullMsg, c.getFullMessage());
  313. assertEquals(shortMsg, c.getShortMessage());
  314. }
  315. @Test
  316. public void testParse_GitStyleMessage() throws Exception {
  317. final String shortMsg = "This fixes a bug.";
  318. final String body = "We do it with magic and pixie dust and stuff.\n"
  319. + "\n" + "Signed-off-by: A U. Thor <author@example.com>\n";
  320. final String fullMsg = shortMsg + "\n" + "\n" + body;
  321. final RevCommit c = create(fullMsg);
  322. assertEquals(fullMsg, c.getFullMessage());
  323. assertEquals(shortMsg, c.getShortMessage());
  324. }
  325. @Test
  326. public void testParse_PublicParseMethod()
  327. throws UnsupportedEncodingException {
  328. ObjectInserter.Formatter fmt = new ObjectInserter.Formatter();
  329. CommitBuilder src = new CommitBuilder();
  330. src.setTreeId(fmt.idFor(Constants.OBJ_TREE, new byte[] {}));
  331. src.setAuthor(author);
  332. src.setCommitter(committer);
  333. src.setMessage("Test commit\n\nThis is a test.\n");
  334. RevCommit p = RevCommit.parse(src.build());
  335. assertEquals(src.getTreeId(), p.getTree());
  336. assertEquals(0, p.getParentCount());
  337. assertEquals(author, p.getAuthorIdent());
  338. assertEquals(committer, p.getCommitterIdent());
  339. assertEquals("Test commit", p.getShortMessage());
  340. assertEquals(src.getMessage(), p.getFullMessage());
  341. }
  342. @Test
  343. public void testParse_GitStyleMessageWithCRLF() throws Exception {
  344. final String shortMsgIn = "This fixes a\r\nbug.\r\n\r\n";
  345. final String shortMsg = "This fixes a bug.";
  346. final String body = "We do it with magic and pixie dust\r\nand stuff.\r\n"
  347. + "\r\n\r\n"
  348. + "Signed-off-by: A U. Thor <author@example.com>\r\n";
  349. final String fullMsg = shortMsgIn + "\r\n" + "\r\n" + body;
  350. final RevCommit c = create(fullMsg);
  351. assertEquals(fullMsg, c.getFullMessage());
  352. assertEquals(shortMsg, c.getShortMessage());
  353. }
  354. private static ObjectId id(final String str) {
  355. return ObjectId.fromString(str);
  356. }
  357. }