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

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