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.

RevTagParseTest.java 17KB

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