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.

FooterLineTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright (C) 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 org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertNotNull;
  14. import static org.junit.Assert.assertNull;
  15. import static org.junit.Assert.assertTrue;
  16. import java.io.IOException;
  17. import java.util.List;
  18. import org.eclipse.jgit.junit.RepositoryTestCase;
  19. import org.eclipse.jgit.lib.Constants;
  20. import org.eclipse.jgit.lib.ObjectId;
  21. import org.junit.Test;
  22. public class FooterLineTest extends RepositoryTestCase {
  23. @Test
  24. public void testNoFooters_EmptyBody() throws IOException {
  25. final RevCommit commit = parse("");
  26. final List<FooterLine> footers = commit.getFooterLines();
  27. assertNotNull(footers);
  28. assertEquals(0, footers.size());
  29. }
  30. @Test
  31. public void testNoFooters_NewlineOnlyBody1() throws IOException {
  32. final RevCommit commit = parse("\n");
  33. final List<FooterLine> footers = commit.getFooterLines();
  34. assertNotNull(footers);
  35. assertEquals(0, footers.size());
  36. }
  37. @Test
  38. public void testNoFooters_NewlineOnlyBody5() throws IOException {
  39. final RevCommit commit = parse("\n\n\n\n\n");
  40. final List<FooterLine> footers = commit.getFooterLines();
  41. assertNotNull(footers);
  42. assertEquals(0, footers.size());
  43. }
  44. @Test
  45. public void testNoFooters_OneLineBodyNoLF() throws IOException {
  46. final RevCommit commit = parse("this is a commit");
  47. final List<FooterLine> footers = commit.getFooterLines();
  48. assertNotNull(footers);
  49. assertEquals(0, footers.size());
  50. }
  51. @Test
  52. public void testNoFooters_OneLineBodyWithLF() throws IOException {
  53. final RevCommit commit = parse("this is a commit\n");
  54. final List<FooterLine> footers = commit.getFooterLines();
  55. assertNotNull(footers);
  56. assertEquals(0, footers.size());
  57. }
  58. @Test
  59. public void testNoFooters_ShortBodyNoLF() throws IOException {
  60. final RevCommit commit = parse("subject\n\nbody of commit");
  61. final List<FooterLine> footers = commit.getFooterLines();
  62. assertNotNull(footers);
  63. assertEquals(0, footers.size());
  64. }
  65. @Test
  66. public void testNoFooters_ShortBodyWithLF() throws IOException {
  67. final RevCommit commit = parse("subject\n\nbody of commit\n");
  68. final List<FooterLine> footers = commit.getFooterLines();
  69. assertNotNull(footers);
  70. assertEquals(0, footers.size());
  71. }
  72. @Test
  73. public void testSignedOffBy_OneUserNoLF() throws IOException {
  74. final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
  75. + "Signed-off-by: A. U. Thor <a@example.com>");
  76. final List<FooterLine> footers = commit.getFooterLines();
  77. FooterLine f;
  78. assertNotNull(footers);
  79. assertEquals(1, footers.size());
  80. f = footers.get(0);
  81. assertEquals("Signed-off-by", f.getKey());
  82. assertEquals("A. U. Thor <a@example.com>", f.getValue());
  83. assertEquals("a@example.com", f.getEmailAddress());
  84. }
  85. @Test
  86. public void testSignedOffBy_OneUserWithLF() throws IOException {
  87. final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
  88. + "Signed-off-by: A. U. Thor <a@example.com>\n");
  89. final List<FooterLine> footers = commit.getFooterLines();
  90. FooterLine f;
  91. assertNotNull(footers);
  92. assertEquals(1, footers.size());
  93. f = footers.get(0);
  94. assertEquals("Signed-off-by", f.getKey());
  95. assertEquals("A. U. Thor <a@example.com>", f.getValue());
  96. assertEquals("a@example.com", f.getEmailAddress());
  97. }
  98. @Test
  99. public void testSignedOffBy_IgnoreWhitespace() throws IOException {
  100. // We only ignore leading whitespace on the value, trailing
  101. // is assumed part of the value.
  102. //
  103. final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
  104. + "Signed-off-by: A. U. Thor <a@example.com> \n");
  105. final List<FooterLine> footers = commit.getFooterLines();
  106. FooterLine f;
  107. assertNotNull(footers);
  108. assertEquals(1, footers.size());
  109. f = footers.get(0);
  110. assertEquals("Signed-off-by", f.getKey());
  111. assertEquals("A. U. Thor <a@example.com> ", f.getValue());
  112. assertEquals("a@example.com", f.getEmailAddress());
  113. }
  114. @Test
  115. public void testEmptyValueNoLF() throws IOException {
  116. final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
  117. + "Signed-off-by:");
  118. final List<FooterLine> footers = commit.getFooterLines();
  119. FooterLine f;
  120. assertNotNull(footers);
  121. assertEquals(1, footers.size());
  122. f = footers.get(0);
  123. assertEquals("Signed-off-by", f.getKey());
  124. assertEquals("", f.getValue());
  125. assertNull(f.getEmailAddress());
  126. }
  127. @Test
  128. public void testEmptyValueWithLF() throws IOException {
  129. final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
  130. + "Signed-off-by:\n");
  131. final List<FooterLine> footers = commit.getFooterLines();
  132. FooterLine f;
  133. assertNotNull(footers);
  134. assertEquals(1, footers.size());
  135. f = footers.get(0);
  136. assertEquals("Signed-off-by", f.getKey());
  137. assertEquals("", f.getValue());
  138. assertNull(f.getEmailAddress());
  139. }
  140. @Test
  141. public void testShortKey() throws IOException {
  142. final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
  143. + "K:V\n");
  144. final List<FooterLine> footers = commit.getFooterLines();
  145. FooterLine f;
  146. assertNotNull(footers);
  147. assertEquals(1, footers.size());
  148. f = footers.get(0);
  149. assertEquals("K", f.getKey());
  150. assertEquals("V", f.getValue());
  151. assertNull(f.getEmailAddress());
  152. }
  153. @Test
  154. public void testNonDelimtedEmail() throws IOException {
  155. final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
  156. + "Acked-by: re@example.com\n");
  157. final List<FooterLine> footers = commit.getFooterLines();
  158. FooterLine f;
  159. assertNotNull(footers);
  160. assertEquals(1, footers.size());
  161. f = footers.get(0);
  162. assertEquals("Acked-by", f.getKey());
  163. assertEquals("re@example.com", f.getValue());
  164. assertEquals("re@example.com", f.getEmailAddress());
  165. }
  166. @Test
  167. public void testNotEmail() throws IOException {
  168. final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
  169. + "Acked-by: Main Tain Er\n");
  170. final List<FooterLine> footers = commit.getFooterLines();
  171. FooterLine f;
  172. assertNotNull(footers);
  173. assertEquals(1, footers.size());
  174. f = footers.get(0);
  175. assertEquals("Acked-by", f.getKey());
  176. assertEquals("Main Tain Er", f.getValue());
  177. assertNull(f.getEmailAddress());
  178. }
  179. @Test
  180. public void testSignedOffBy_ManyUsers() throws IOException {
  181. final RevCommit commit = parse("subject\n\nbody of commit\n"
  182. + "Not-A-Footer-Line: this line must not be read as a footer\n"
  183. + "\n" // paragraph break, now footers appear in final block
  184. + "Signed-off-by: A. U. Thor <a@example.com>\n"
  185. + "CC: <some.mailing.list@example.com>\n"
  186. + "Acked-by: Some Reviewer <sr@example.com>\n"
  187. + "Signed-off-by: Main Tain Er <mte@example.com>\n");
  188. final List<FooterLine> footers = commit.getFooterLines();
  189. FooterLine f;
  190. assertNotNull(footers);
  191. assertEquals(4, footers.size());
  192. f = footers.get(0);
  193. assertEquals("Signed-off-by", f.getKey());
  194. assertEquals("A. U. Thor <a@example.com>", f.getValue());
  195. assertEquals("a@example.com", f.getEmailAddress());
  196. f = footers.get(1);
  197. assertEquals("CC", f.getKey());
  198. assertEquals("<some.mailing.list@example.com>", f.getValue());
  199. assertEquals("some.mailing.list@example.com", f.getEmailAddress());
  200. f = footers.get(2);
  201. assertEquals("Acked-by", f.getKey());
  202. assertEquals("Some Reviewer <sr@example.com>", f.getValue());
  203. assertEquals("sr@example.com", f.getEmailAddress());
  204. f = footers.get(3);
  205. assertEquals("Signed-off-by", f.getKey());
  206. assertEquals("Main Tain Er <mte@example.com>", f.getValue());
  207. assertEquals("mte@example.com", f.getEmailAddress());
  208. }
  209. @Test
  210. public void testSignedOffBy_SkipNonFooter() throws IOException {
  211. final RevCommit commit = parse("subject\n\nbody of commit\n"
  212. + "Not-A-Footer-Line: this line must not be read as a footer\n"
  213. + "\n" // paragraph break, now footers appear in final block
  214. + "Signed-off-by: A. U. Thor <a@example.com>\n"
  215. + "CC: <some.mailing.list@example.com>\n"
  216. + "not really a footer line but we'll skip it anyway\n"
  217. + "Acked-by: Some Reviewer <sr@example.com>\n"
  218. + "Signed-off-by: Main Tain Er <mte@example.com>\n");
  219. final List<FooterLine> footers = commit.getFooterLines();
  220. FooterLine f;
  221. assertNotNull(footers);
  222. assertEquals(4, footers.size());
  223. f = footers.get(0);
  224. assertEquals("Signed-off-by", f.getKey());
  225. assertEquals("A. U. Thor <a@example.com>", f.getValue());
  226. f = footers.get(1);
  227. assertEquals("CC", f.getKey());
  228. assertEquals("<some.mailing.list@example.com>", f.getValue());
  229. f = footers.get(2);
  230. assertEquals("Acked-by", f.getKey());
  231. assertEquals("Some Reviewer <sr@example.com>", f.getValue());
  232. f = footers.get(3);
  233. assertEquals("Signed-off-by", f.getKey());
  234. assertEquals("Main Tain Er <mte@example.com>", f.getValue());
  235. }
  236. @Test
  237. public void testFilterFootersIgnoreCase() throws IOException {
  238. final RevCommit commit = parse("subject\n\nbody of commit\n"
  239. + "Not-A-Footer-Line: this line must not be read as a footer\n"
  240. + "\n" // paragraph break, now footers appear in final block
  241. + "Signed-Off-By: A. U. Thor <a@example.com>\n"
  242. + "CC: <some.mailing.list@example.com>\n"
  243. + "Acked-by: Some Reviewer <sr@example.com>\n"
  244. + "signed-off-by: Main Tain Er <mte@example.com>\n");
  245. final List<String> footers = commit.getFooterLines("signed-off-by");
  246. assertNotNull(footers);
  247. assertEquals(2, footers.size());
  248. assertEquals("A. U. Thor <a@example.com>", footers.get(0));
  249. assertEquals("Main Tain Er <mte@example.com>", footers.get(1));
  250. }
  251. @Test
  252. public void testMatchesBugId() throws IOException {
  253. final RevCommit commit = parse("this is a commit subject for test\n"
  254. + "\n" // paragraph break, now footers appear in final block
  255. + "Simple-Bug-Id: 42\n");
  256. final List<FooterLine> footers = commit.getFooterLines();
  257. assertNotNull(footers);
  258. assertEquals(1, footers.size());
  259. final FooterLine line = footers.get(0);
  260. assertNotNull(line);
  261. assertEquals("Simple-Bug-Id", line.getKey());
  262. assertEquals("42", line.getValue());
  263. final FooterKey bugid = new FooterKey("Simple-Bug-Id");
  264. assertTrue("matches Simple-Bug-Id", line.matches(bugid));
  265. assertFalse("not Signed-off-by", line.matches(FooterKey.SIGNED_OFF_BY));
  266. assertFalse("not CC", line.matches(FooterKey.CC));
  267. }
  268. private RevCommit parse(String msg) throws IOException {
  269. final StringBuilder buf = new StringBuilder();
  270. buf.append("tree " + ObjectId.zeroId().name() + "\n");
  271. buf.append("author A. U. Thor <a@example.com> 1 +0000\n");
  272. buf.append("committer A. U. Thor <a@example.com> 1 +0000\n");
  273. buf.append("\n");
  274. buf.append(msg);
  275. try (RevWalk walk = new RevWalk(db)) {
  276. RevCommit c = new RevCommit(ObjectId.zeroId());
  277. c.parseCanonical(walk, Constants.encode(buf.toString()));
  278. return c;
  279. }
  280. }
  281. }