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

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