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

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