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.

ChangeIdUtilTest.java 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /*
  2. * Copyright (C) 2010, Robin Rosenberg
  3. * Copyright (C) 2009, Google, Inc.
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.util;
  45. import static org.junit.Assert.assertEquals;
  46. import java.util.concurrent.TimeUnit;
  47. import org.eclipse.jgit.junit.MockSystemReader;
  48. import org.eclipse.jgit.lib.ObjectId;
  49. import org.eclipse.jgit.lib.PersonIdent;
  50. import org.junit.Test;
  51. /**
  52. * Portions of this test is from CommitMsgHookTest in the Android project Gerrit
  53. */
  54. public class ChangeIdUtilTest {
  55. private final String SOB1 = "Signed-off-by: J Author <ja@example.com>\n";
  56. private final String SOB2 = "Signed-off-by: J Committer <jc@example.com>\n";
  57. final PersonIdent p = RawParseUtils.parsePersonIdent(
  58. "A U Thor <author@example.com> 1142878501 -0500");
  59. final PersonIdent q = RawParseUtils.parsePersonIdent(
  60. "W Riter <writer@example.com> 1142878502 -0500");
  61. ObjectId treeId = ObjectId
  62. .fromString("f51de923607cd51cf872b928a6b523ba823f7f35");
  63. ObjectId treeId1 = ObjectId
  64. .fromString("4b825dc642cb6eb9a060e54bf8d69288fbee4904");
  65. final ObjectId treeId2 = ObjectId
  66. .fromString("617601c79811cbbae338512798318b4e5b70c9ac");
  67. ObjectId parentId = ObjectId
  68. .fromString("91fea719aaf9447feb9580477eb3dd08b62b5eca");
  69. ObjectId parentId1 = null;
  70. final ObjectId parentId2 = ObjectId
  71. .fromString("485c91e0600b165c301c278bfbae3e492413980c");
  72. MockSystemReader mockSystemReader = new MockSystemReader();
  73. final long when = mockSystemReader.getCurrentTime();
  74. final int tz = new MockSystemReader().getTimezone(when);
  75. PersonIdent author = new PersonIdent("J. Author", "ja@example.com");
  76. {
  77. author = new PersonIdent(author, when, tz);
  78. }
  79. PersonIdent committer = new PersonIdent("J. Committer", "jc@example.com");
  80. {
  81. committer = new PersonIdent(committer, when, tz);
  82. }
  83. @Test
  84. public void testClean() {
  85. assertEquals("hej", ChangeIdUtil.clean("hej\n\n"));
  86. assertEquals("hej\n\nsan", ChangeIdUtil.clean("hej\n\nsan\n\n"));
  87. assertEquals("hej\nsan", ChangeIdUtil.clean("hej\n#men\nsan\n\n#men"));
  88. assertEquals("hej\nsan", ChangeIdUtil.clean("hej\nsan\n\n#men"));
  89. assertEquals("hej\nsan", ChangeIdUtil.clean("#no\nhej\nsan\n\n#men"));
  90. assertEquals("hej\nsan", ChangeIdUtil
  91. .clean("#no\nhej\nsan\nSigned-off-by: me \n#men"));
  92. }
  93. @Test
  94. public void testId() {
  95. String msg = "A\nMessage\n";
  96. ObjectId id = ChangeIdUtil.computeChangeId(treeId, parentId, p, q, msg);
  97. assertEquals("73f3751208ac92cbb76f9a26ac4a0d9d472e381b", ObjectId
  98. .toString(id));
  99. }
  100. @Test
  101. public void testHasChangeid() throws Exception {
  102. assertEquals(
  103. "has changeid\nmore text\n\nBug: 33\nSigned-off-by: me@you.too\n"
  104. + "Change-Id: I0123456789012345678901234567890123456789\n",
  105. call("has changeid\nmore text\n\nBug: 33\nSigned-off-by: me@you.too\n"
  106. + "Change-Id: I0123456789012345678901234567890123456789\n"));
  107. }
  108. @Test
  109. public void testHasChangeidWithReplacement() throws Exception {
  110. assertEquals(
  111. "has changeid\nmore text\n\nSigned-off-by: me@you.too\n"
  112. + "Change-Id: I2178563fada5edb2c99a8d8c0d619471b050ec24\nBug: 33\n",
  113. call("has changeid\nmore text\n\nSigned-off-by: me@you.too\n"
  114. + "Change-Id: I0123456789012345678901234567890123456789\nBug: 33\n",
  115. true));
  116. }
  117. @Test
  118. public void testHasChangeidWithReplacementInLastLine() throws Exception {
  119. assertEquals(
  120. "has changeid\nmore text\n\nBug: 33\nSigned-off-by: me@you.too\n"
  121. + "Change-Id: I1d6578f4c96e3db4dd707705fe3d17bf658c4758\n",
  122. call("has changeid\nmore text\n\nBug: 33\nSigned-off-by: me@you.too\n"
  123. + "Change-Id: I0123456789012345678901234567890123456789\n",
  124. true));
  125. }
  126. @Test
  127. public void testHasChangeidWithReplacementInLastLineNoLineBreak()
  128. throws Exception {
  129. assertEquals(
  130. "has changeid\nmore text\n\nBug: 33\nSigned-off-by: me@you.too\n"
  131. + "Change-Id: I1d6578f4c96e3db4dd707705fe3d17bf658c4758",
  132. call("has changeid\nmore text\n\nBug: 33\nSigned-off-by: me@you.too\n"
  133. + "Change-Id: I0123456789012345678901234567890123456789",
  134. true));
  135. }
  136. @Test
  137. public void testHasChangeidWithSpacesBeforeId() throws Exception {
  138. assertEquals(
  139. "has changeid\nmore text\n\nBug: 33\nSigned-off-by: me@you.too\n"
  140. + "Change-Id: Ie7575eaf450fdd0002df2e642426faf251de3ad9\n",
  141. call("has changeid\nmore text\n\nBug: 33\nSigned-off-by: me@you.too\n"
  142. + "Change-Id: I0123456789012345678901234567890123456789\n",
  143. true));
  144. }
  145. @Test
  146. public void testHasChangeidWithReplacementWithChangeIdInCommitMessage()
  147. throws Exception {
  148. assertEquals(
  149. "has changeid\nmore text\n"
  150. + "Change-Id: I0123456789012345678901234567890123456789\n\n"
  151. + "Bug: 33\nSigned-off-by: me@you.too\n"
  152. + "Change-Id: Ie48d10d59ef67995ca89688ac0171b88f10dd520\n",
  153. call("has changeid\nmore text\n"
  154. + "Change-Id: I0123456789012345678901234567890123456789\n\n"
  155. + "Bug: 33\nSigned-off-by: me@you.too\n"
  156. + "Change-Id: I0123456789012345678901234567890123456789\n",
  157. true));
  158. }
  159. @Test
  160. public void testOneliner() throws Exception {
  161. assertEquals(
  162. "oneliner\n\nChange-Id: I3a98091ce4470de88d52ae317fcd297e2339f063\n",
  163. call("oneliner\n"));
  164. }
  165. @Test
  166. public void testOnelinerFollowedByBlank() throws Exception {
  167. assertEquals(
  168. "oneliner followed by blank\n\nChange-Id: I3a12c21ef342a18498f95c62efbc186cd782b743\n",
  169. call("oneliner followed by blank\n"));
  170. }
  171. @Test
  172. public void testATwoLines() throws Exception {
  173. assertEquals(
  174. "a two lines\nwith text withour break after subject line\n\nChange-Id: I549a0fed3d69b7876c54b4f5a35637135fd43fac\n",
  175. call("a two lines\nwith text withour break after subject line\n"));
  176. }
  177. @Test
  178. public void testRegularCommit() throws Exception {
  179. assertEquals(
  180. "regular commit\n\nwith header and body\n\nChange-Id: I62d8749d3c3a888c11e3fadc3924220a19389766\n",
  181. call("regular commit\n\nwith header and body\n"));
  182. }
  183. @Test
  184. public void testRegularCommitWithSob_ButNoBody() throws Exception {
  185. assertEquals(
  186. "regular commit with sob, but no body\n\nChange-Id: I0f0b4307e9944ecbd5a9f6b9489e25cfaede43c4\nSigned-off-by: me@you.too\n",
  187. call("regular commit with sob, but no body\n\nSigned-off-by: me@you.too\n"));
  188. }
  189. @Test
  190. public void testACommitWithBug_SubButNoBody() throws Exception {
  191. assertEquals(
  192. "a commit with bug, sub but no body\n\nBug: 33\nChange-Id: I337e264868613dab6d1e11a34f394db369487412\nSigned-off-by: me@you.too\n",
  193. call("a commit with bug, sub but no body\n\nBug: 33\nSigned-off-by: me@you.too\n"));
  194. }
  195. @Test
  196. public void testACommitWithSubject_NoBodySobAndBug() throws Exception {
  197. assertEquals(
  198. "a commit with subject, no body sob and bug\n\nChange-Id: Ib3616d4bf77707a3215a6cb0602c004ee119a445\nSigned-off-by: me@you.too\nBug: 33\n",
  199. call("a commit with subject, no body sob and bug\n\nSigned-off-by: me@you.too\nBug: 33\n"));
  200. }
  201. @Test
  202. public void testACommitWithSubjectBug_NonFooterLineAndSob()
  203. throws Exception {
  204. assertEquals(
  205. "a commit with subject bug, non-footer line and sob\n\nBug: 33\nmore text\nSigned-off-by: me@you.too\n\nChange-Id: Ia8500eab2304e6e5eac6ae488ff44d5d850d118a\n",
  206. call("a commit with subject bug, non-footer line and sob\n\nBug: 33\nmore text\nSigned-off-by: me@you.too\n"));
  207. }
  208. @Test
  209. public void testACommitWithSubject_NonFooterAndBugAndSob() throws Exception {
  210. assertEquals(
  211. "a commit with subject, non-footer and bug and sob\n\nmore text (two empty lines after bug)\nBug: 33\n\n\nChange-Id: Idac75ccbad2ab6727b8612e344df5190d87891dd\nSigned-off-by: me@you.too\n",
  212. call("a commit with subject, non-footer and bug and sob\n\nmore text (two empty lines after bug)\nBug: 33\n\n\nSigned-off-by: me@you.too\n"));
  213. }
  214. @Test
  215. public void testACommitWithSubjectBodyBugBrackersAndSob() throws Exception {
  216. assertEquals(
  217. "a commit with subject body, bug. brackers and sob\n\nText\n\nBug: 33\nChange-Id: I90ecb589bef766302532c3e00915e10114b00f62\n[bracket]\nSigned-off-by: me@you.too\n",
  218. call("a commit with subject body, bug. brackers and sob\n\nText\n\nBug: 33\n[bracket]\nSigned-off-by: me@you.too\n\n"));
  219. }
  220. @Test
  221. public void testACommitWithSubjectBodyBugLineWithASpaceAndSob()
  222. throws Exception {
  223. assertEquals(
  224. "a commit with subject body, bug. line with a space and sob\n\nText\n\nBug: 33\nChange-Id: I864e2218bdee033c8ce9a7f923af9e0d5dc16863\n \nSigned-off-by: me@you.too\n",
  225. call("a commit with subject body, bug. line with a space and sob\n\nText\n\nBug: 33\n \nSigned-off-by: me@you.too\n\n"));
  226. }
  227. @Test
  228. public void testACommitWithSubjectBodyBugEmptyLineAndSob() throws Exception {
  229. assertEquals(
  230. "a commit with subject body, bug. empty line and sob\n\nText\n\nBug: 33\nChange-Id: I33f119f533313883e6ada3df600c4f0d4db23a76\n \nSigned-off-by: me@you.too\n",
  231. call("a commit with subject body, bug. empty line and sob\n\nText\n\nBug: 33\n \nSigned-off-by: me@you.too\n\n"));
  232. }
  233. @Test
  234. public void testEmptyMessages() throws Exception {
  235. // Empty input must not produce a change id.
  236. hookDoesNotModify("");
  237. hookDoesNotModify(" ");
  238. hookDoesNotModify("\n");
  239. hookDoesNotModify("\n\n");
  240. hookDoesNotModify(" \n ");
  241. hookDoesNotModify("#");
  242. hookDoesNotModify("#\n");
  243. hookDoesNotModify("# on branch master\n# Untracked files:\n");
  244. hookDoesNotModify("\n# on branch master\n# Untracked files:\n");
  245. hookDoesNotModify("\n\n# on branch master\n# Untracked files:\n");
  246. hookDoesNotModify("\n# on branch master\ndiff --git a/src b/src\n"
  247. + "new file mode 100644\nindex 0000000..c78b7f0\n");
  248. }
  249. @Test
  250. public void testChangeIdAlreadySet() throws Exception {
  251. // If a Change-Id is already present in the footer, the hook must
  252. // not modify the message but instead must leave the identity alone.
  253. //
  254. hookDoesNotModify("a\n" + //
  255. "\n" + //
  256. "Change-Id: Iaeac9b4149291060228ef0154db2985a31111335\n");
  257. hookDoesNotModify("fix: this thing\n" + //
  258. "\n" + //
  259. "Change-Id: I388bdaf52ed05b55e62a22d0a20d2c1ae0d33e7e\n");
  260. hookDoesNotModify("fix-a-widget: this thing\n" + //
  261. "\n" + //
  262. "Change-Id: Id3bc5359d768a6400450283e12bdfb6cd135ea4b\n");
  263. hookDoesNotModify("FIX: this thing\n" + //
  264. "\n" + //
  265. "Change-Id: I1b55098b5a2cce0b3f3da783dda50d5f79f873fa\n");
  266. hookDoesNotModify("Fix-A-Widget: this thing\n" + //
  267. "\n" + //
  268. "Change-Id: I4f4e2e1e8568ddc1509baecb8c1270a1fb4b6da7\n");
  269. }
  270. @Test
  271. public void testChangeIdAlreadySetWithReplacement() throws Exception {
  272. // If a Change-Id is already present in the footer, the hook
  273. // replaces the Change-Id with the new value..
  274. //
  275. assertEquals("a\n" + //
  276. "\n" + //
  277. "Change-Id: Ifa324efa85bfb3c8696a46a0f67fa70c35be5f5f\n",
  278. call("a\n" + //
  279. "\n" + //
  280. "Change-Id: Iaeac9b4149291060228ef0154db2985a31111335\n",
  281. true));
  282. assertEquals("fix: this thing\n" + //
  283. "\n" + //
  284. "Change-Id: Ib63e4990a06412a3f24bd93bb160e98ac1bd412b\n",
  285. call("fix: this thing\n" + //
  286. "\n" + //
  287. "Change-Id: I388bdaf52ed05b55e62a22d0a20d2c1ae0d33e7e\n",
  288. true));
  289. assertEquals("fix-a-widget: this thing\n" + //
  290. "\n" + //
  291. "Change-Id: If0444e4d0cabcf41b3d3b46b7e9a7a64a82117af\n",
  292. call("fix-a-widget: this thing\n" + //
  293. "\n" + //
  294. "Change-Id: Id3bc5359d768a6400450283e12bdfb6cd135ea4b\n",
  295. true));
  296. assertEquals("FIX: this thing\n" + //
  297. "\n" + //
  298. "Change-Id: Iba5a3b2d5e5df46448f6daf362b6bfa775c6491d\n",
  299. call("FIX: this thing\n" + //
  300. "\n" + //
  301. "Change-Id: I1b55098b5a2cce0b3f3da783dda50d5f79f873fa\n",
  302. true));
  303. assertEquals("Fix-A-Widget: this thing\n" + //
  304. "\n" + //
  305. "Change-Id: I2573d47c62c42429fbe424d70cfba931f8f87848\n",
  306. call("Fix-A-Widget: this thing\n" + //
  307. "\n" + //
  308. "Change-Id: I4f4e2e1e8568ddc1509baecb8c1270a1fb4b6da7\n",
  309. true));
  310. }
  311. @Test
  312. public void testTimeAltersId() throws Exception {
  313. assertEquals("a\n" + //
  314. "\n" + //
  315. "Change-Id: I7fc3876fee63c766a2063df97fbe04a2dddd8d7c\n",//
  316. call("a\n"));
  317. tick();
  318. assertEquals("a\n" + //
  319. "\n" + //
  320. "Change-Id: I3251906b99dda598a58a6346d8126237ee1ea800\n",//
  321. call("a\n"));
  322. tick();
  323. assertEquals("a\n" + //
  324. "\n" + //
  325. "Change-Id: I69adf9208d828f41a3d7e41afbca63aff37c0c5c\n",//
  326. call("a\n"));
  327. }
  328. /** Increment the {@link #author} and {@link #committer} times. */
  329. protected void tick() {
  330. final long delta = TimeUnit.MILLISECONDS.convert(5 * 60,
  331. TimeUnit.SECONDS);
  332. final long now = author.getWhen().getTime() + delta;
  333. author = new PersonIdent(author, now, tz);
  334. committer = new PersonIdent(committer, now, tz);
  335. }
  336. @Test
  337. public void testFirstParentAltersId() throws Exception {
  338. assertEquals("a\n" + //
  339. "\n" + //
  340. "Change-Id: I7fc3876fee63c766a2063df97fbe04a2dddd8d7c\n",//
  341. call("a\n"));
  342. parentId1 = parentId2;
  343. assertEquals("a\n" + //
  344. "\n" + //
  345. "Change-Id: I51e86482bde7f92028541aaf724d3a3f996e7ea2\n",//
  346. call("a\n"));
  347. }
  348. @Test
  349. public void testDirCacheAltersId() throws Exception {
  350. assertEquals("a\n" + //
  351. "\n" + //
  352. "Change-Id: I7fc3876fee63c766a2063df97fbe04a2dddd8d7c\n",//
  353. call("a\n"));
  354. treeId1 = treeId2;
  355. assertEquals("a\n" + //
  356. "\n" + //
  357. "Change-Id: If56597ea9759f23b070677ea6f064c60c38da631\n",//
  358. call("a\n"));
  359. }
  360. @Test
  361. public void testSingleLineMessages() throws Exception {
  362. assertEquals("a\n" + //
  363. "\n" + //
  364. "Change-Id: I7fc3876fee63c766a2063df97fbe04a2dddd8d7c\n",//
  365. call("a\n"));
  366. assertEquals("fix: this thing\n" + //
  367. "\n" + //
  368. "Change-Id: I0f13d0e6c739ca3ae399a05a93792e80feb97f37\n",//
  369. call("fix: this thing\n"));
  370. assertEquals("fix-a-widget: this thing\n" + //
  371. "\n" + //
  372. "Change-Id: I1a1a0c751e4273d532e4046a501a612b9b8a775e\n",//
  373. call("fix-a-widget: this thing\n"));
  374. assertEquals("FIX: this thing\n" + //
  375. "\n" + //
  376. "Change-Id: If816d944c57d3893b60cf10c65931fead1290d97\n",//
  377. call("FIX: this thing\n"));
  378. assertEquals("Fix-A-Widget: this thing\n" + //
  379. "\n" + //
  380. "Change-Id: I3e18d00cbda2ba1f73aeb63ed8c7d57d7fd16c76\n",//
  381. call("Fix-A-Widget: this thing\n"));
  382. }
  383. @Test
  384. public void testMultiLineMessagesWithoutFooter() throws Exception {
  385. assertEquals("a\n" + //
  386. "\n" + //
  387. "b\n" + //
  388. "\n" + //
  389. "Change-Id: Id0b4f42d3d6fc1569595c9b97cb665e738486f5d\n",//
  390. call("a\n" + "\n" + "b\n"));
  391. assertEquals("a\n" + //
  392. "\n" + //
  393. "b\nc\nd\ne\n" + //
  394. "\n" + //
  395. "Change-Id: I7d237b20058a0f46cc3f5fabc4a0476877289d75\n",//
  396. call("a\n" + "\n" + "b\nc\nd\ne\n"));
  397. assertEquals("a\n" + //
  398. "\n" + //
  399. "b\nc\nd\ne\n" + //
  400. "\n" + //
  401. "f\ng\nh\n" + //
  402. "\n" + //
  403. "Change-Id: I382e662f47bf164d6878b7fe61637873ab7fa4e8\n",//
  404. call("a\n" + "\n" + "b\nc\nd\ne\n" + "\n" + "f\ng\nh\n"));
  405. }
  406. @Test
  407. public void testSingleLineMessagesWithSignedOffBy() throws Exception {
  408. assertEquals("a\n" + //
  409. "\n" + //
  410. "Change-Id: I7fc3876fee63c766a2063df97fbe04a2dddd8d7c\n" + //
  411. SOB1,//
  412. call("a\n" + "\n" + SOB1));
  413. assertEquals("a\n" + //
  414. "\n" + //
  415. "Change-Id: I7fc3876fee63c766a2063df97fbe04a2dddd8d7c\n" + //
  416. SOB1 + //
  417. SOB2,//
  418. call("a\n" + "\n" + SOB1 + SOB2));
  419. }
  420. @Test
  421. public void testMultiLineMessagesWithSignedOffBy() throws Exception {
  422. assertEquals("a\n" + //
  423. "\n" + //
  424. "b\nc\nd\ne\n" + //
  425. "\n" + //
  426. "f\ng\nh\n" + //
  427. "\n" + //
  428. "Change-Id: I382e662f47bf164d6878b7fe61637873ab7fa4e8\n" + //
  429. SOB1,//
  430. call("a\n" + "\n" + "b\nc\nd\ne\n" + "\n" + "f\ng\nh\n" + "\n"
  431. + SOB1));
  432. assertEquals("a\n" + //
  433. "\n" + //
  434. "b\nc\nd\ne\n" + //
  435. "\n" + //
  436. "f\ng\nh\n" + //
  437. "\n" + //
  438. "Change-Id: I382e662f47bf164d6878b7fe61637873ab7fa4e8\n" + //
  439. SOB1 + //
  440. SOB2,//
  441. call("a\n" + //
  442. "\n" + //
  443. "b\nc\nd\ne\n" + //
  444. "\n" + //
  445. "f\ng\nh\n" + //
  446. "\n" + //
  447. SOB1 + //
  448. SOB2));
  449. assertEquals("a\n" + //
  450. "\n" + //
  451. "b: not a footer\nc\nd\ne\n" + //
  452. "\n" + //
  453. "f\ng\nh\n" + //
  454. "\n" + //
  455. "Change-Id: I8869aabd44b3017cd55d2d7e0d546a03e3931ee2\n" + //
  456. SOB1 + //
  457. SOB2,//
  458. call("a\n" + //
  459. "\n" + //
  460. "b: not a footer\nc\nd\ne\n" + //
  461. "\n" + //
  462. "f\ng\nh\n" + //
  463. "\n" + //
  464. SOB1 + //
  465. SOB2));
  466. }
  467. @Test
  468. public void testNoteInMiddle() throws Exception {
  469. assertEquals("a\n" + //
  470. "\n" + //
  471. "NOTE: This\n" + //
  472. "does not fix it.\n" + //
  473. "\n" + //
  474. "Change-Id: I988a127969a6ee5e58db546aab74fc46e66847f8\n", //
  475. call("a\n" + //
  476. "\n" + //
  477. "NOTE: This\n" + //
  478. "does not fix it.\n"));
  479. }
  480. @Test
  481. public void testKernelStyleFooter() throws Exception {
  482. assertEquals("a\n" + //
  483. "\n" + //
  484. "Change-Id: I1bd787f9e7590a2ac82b02c404c955ffb21877c4\n" + //
  485. SOB1 + //
  486. "[ja: Fixed\n" + //
  487. " the indentation]\n" + //
  488. SOB2, //
  489. call("a\n" + //
  490. "\n" + //
  491. SOB1 + //
  492. "[ja: Fixed\n" + //
  493. " the indentation]\n" + //
  494. SOB2));
  495. }
  496. @Test
  497. public void testChangeIdAfterBugOrIssue() throws Exception {
  498. assertEquals("a\n" + //
  499. "\n" + //
  500. "Bug: 42\n" + //
  501. "Change-Id: I8c0321227c4324e670b9ae8cf40eccc87af21b1b\n" + //
  502. SOB1,//
  503. call("a\n" + //
  504. "\n" + //
  505. "Bug: 42\n" + //
  506. SOB1));
  507. assertEquals("a\n" + //
  508. "\n" + //
  509. "Issue: 42\n" + //
  510. "Change-Id: Ie66e07d89ae5b114c0975b49cf326e90331dd822\n" + //
  511. SOB1,//
  512. call("a\n" + //
  513. "\n" + //
  514. "Issue: 42\n" + //
  515. SOB1));
  516. }
  517. @Test
  518. public void testWithEndingURL() throws Exception {
  519. assertEquals("a\n" + //
  520. "\n" + //
  521. "http://example.com/ fixes this\n" + //
  522. "\n" + //
  523. "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n", //
  524. call("a\n" + //
  525. "\n" + //
  526. "http://example.com/ fixes this\n"));
  527. assertEquals("a\n" + //
  528. "\n" + //
  529. "https://example.com/ fixes this\n" + //
  530. "\n" + //
  531. "Change-Id: I62b9039e2fc0dce274af55e8f99312a8a80a805d\n", //
  532. call("a\n" + //
  533. "\n" + //
  534. "https://example.com/ fixes this\n"));
  535. assertEquals("a\n" + //
  536. "\n" + //
  537. "ftp://example.com/ fixes this\n" + //
  538. "\n" + //
  539. "Change-Id: I71b05dc1f6b9a5540a53a693e64d58b65a8910e8\n", //
  540. call("a\n" + //
  541. "\n" + //
  542. "ftp://example.com/ fixes this\n"));
  543. assertEquals("a\n" + //
  544. "\n" + //
  545. "git://example.com/ fixes this\n" + //
  546. "\n" + //
  547. "Change-Id: Id34e942baa68d790633737d815ddf11bac9183e5\n", //
  548. call("a\n" + //
  549. "\n" + //
  550. "git://example.com/ fixes this\n"));
  551. }
  552. @Test
  553. public void testIndexOfChangeId() {
  554. assertEquals(-1, ChangeIdUtil.indexOfChangeId("", "\n"));
  555. assertEquals(-1, ChangeIdUtil.indexOfChangeId("\n", "\n"));
  556. assertEquals(-1, ChangeIdUtil.indexOfChangeId("\r\n", "\r\n"));
  557. assertEquals(3, ChangeIdUtil.indexOfChangeId("x\n" + "\n"
  558. + "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n",
  559. "\n"));
  560. assertEquals(3, ChangeIdUtil.indexOfChangeId("x\n" + "\n"
  561. + "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n\n\n",
  562. "\n"));
  563. assertEquals(3, ChangeIdUtil.indexOfChangeId("x\n" + "\n"
  564. + "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n \n \n",
  565. "\n"));
  566. assertEquals(3, ChangeIdUtil.indexOfChangeId("x\n" + "\n"
  567. + "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n",
  568. "\n"));
  569. // leading whitespace is rejected by Gerrit
  570. assertEquals(-1, ChangeIdUtil.indexOfChangeId("x\n" + "\n"
  571. + " Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n",
  572. "\n"));
  573. assertEquals(-1, ChangeIdUtil.indexOfChangeId("x\n" + "\n"
  574. + "\t Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n",
  575. "\n"));
  576. assertEquals(-1, ChangeIdUtil.indexOfChangeId("x\n" + "\n"
  577. + "Change-Id: \n", "\n"));
  578. assertEquals(3, ChangeIdUtil.indexOfChangeId("x\n" + "\n"
  579. + "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701 \n",
  580. "\n"));
  581. assertEquals(12, ChangeIdUtil.indexOfChangeId("x\n" + "\n"
  582. + "Bug 4711\n"
  583. + "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n",
  584. "\n"));
  585. assertEquals(56, ChangeIdUtil.indexOfChangeId("x\n"
  586. + "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n"
  587. + "\n"
  588. + "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n",
  589. "\n"));
  590. assertEquals(-1, ChangeIdUtil.indexOfChangeId("x\n"
  591. + "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n"
  592. + "\n" + "x\n", "\n"));
  593. assertEquals(-1, ChangeIdUtil.indexOfChangeId("x\n\n"
  594. + "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n"
  595. + "\n" + "x\n", "\n"));
  596. assertEquals(5, ChangeIdUtil.indexOfChangeId("x\r\n" + "\r\n"
  597. + "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\r\n",
  598. "\r\n"));
  599. assertEquals(3, ChangeIdUtil.indexOfChangeId("x\r" + "\r"
  600. + "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\r",
  601. "\r"));
  602. assertEquals(3, ChangeIdUtil.indexOfChangeId("x\r" + "\r"
  603. + "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\r",
  604. "\r"));
  605. assertEquals(8, ChangeIdUtil.indexOfChangeId("x\ny\n\nz\n" + "\n"
  606. + "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n",
  607. "\n"));
  608. }
  609. private void hookDoesNotModify(final String in) throws Exception {
  610. assertEquals(in, call(in));
  611. }
  612. private String call(final String body) throws Exception {
  613. return call(body, false);
  614. }
  615. private String call(final String body, boolean replaceExisting) throws Exception {
  616. ObjectId computeChangeId = ChangeIdUtil.computeChangeId(treeId1,
  617. parentId1, author, committer, body);
  618. if (computeChangeId == null)
  619. return body;
  620. return ChangeIdUtil.insertId(body, computeChangeId, replaceExisting);
  621. }
  622. }