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.

ChangeIdUtil.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (C) 2010, Robin Rosenberg
  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.util;
  44. import java.util.regex.Pattern;
  45. import org.eclipse.jgit.lib.Constants;
  46. import org.eclipse.jgit.lib.ObjectId;
  47. import org.eclipse.jgit.lib.ObjectInserter;
  48. import org.eclipse.jgit.lib.PersonIdent;
  49. /**
  50. * Utilities for creating and working with Change-Id's, like the one used by
  51. * Gerrit Code Review.
  52. * <p>
  53. * A Change-Id is a SHA-1 computed from the content of a commit, in a similar
  54. * fashion to how the commit id is computed. Unlike the commit id a Change-Id is
  55. * retained in the commit and subsequent revised commits in the footer of the
  56. * commit text.
  57. */
  58. public class ChangeIdUtil {
  59. static final String CHANGE_ID = "Change-Id:"; //$NON-NLS-1$
  60. // package-private so the unit test can test this part only
  61. @SuppressWarnings("nls")
  62. static String clean(String msg) {
  63. return msg.//
  64. replaceAll("(?i)(?m)^Signed-off-by:.*$\n?", "").// //$NON-NLS-1$
  65. replaceAll("(?m)^#.*$\n?", "").// //$NON-NLS-1$
  66. replaceAll("(?m)\n\n\n+", "\\\n").// //$NON-NLS-1$
  67. replaceAll("\\n*$", "").// //$NON-NLS-1$
  68. replaceAll("(?s)\ndiff --git.*", "").// //$NON-NLS-1$
  69. trim();
  70. }
  71. /**
  72. * Compute a Change-Id.
  73. *
  74. * @param treeId
  75. * The id of the tree that would be committed
  76. * @param firstParentId
  77. * parent id of previous commit or null
  78. * @param author
  79. * the {@link org.eclipse.jgit.lib.PersonIdent} for the presumed
  80. * author and time
  81. * @param committer
  82. * the {@link org.eclipse.jgit.lib.PersonIdent} for the presumed
  83. * committer and time
  84. * @param message
  85. * The commit message
  86. * @return the change id SHA1 string (without the 'I') or null if the
  87. * message is not complete enough
  88. */
  89. public static ObjectId computeChangeId(final ObjectId treeId,
  90. final ObjectId firstParentId, final PersonIdent author,
  91. final PersonIdent committer, final String message) {
  92. String cleanMessage = clean(message);
  93. if (cleanMessage.length() == 0)
  94. return null;
  95. StringBuilder b = new StringBuilder();
  96. b.append("tree "); //$NON-NLS-1$
  97. b.append(ObjectId.toString(treeId));
  98. b.append("\n"); //$NON-NLS-1$
  99. if (firstParentId != null) {
  100. b.append("parent "); //$NON-NLS-1$
  101. b.append(ObjectId.toString(firstParentId));
  102. b.append("\n"); //$NON-NLS-1$
  103. }
  104. b.append("author "); //$NON-NLS-1$
  105. b.append(author.toExternalString());
  106. b.append("\n"); //$NON-NLS-1$
  107. b.append("committer "); //$NON-NLS-1$
  108. b.append(committer.toExternalString());
  109. b.append("\n\n"); //$NON-NLS-1$
  110. b.append(cleanMessage);
  111. try (ObjectInserter f = new ObjectInserter.Formatter()) {
  112. return f.idFor(Constants.OBJ_COMMIT, Constants.encode(b.toString()));
  113. }
  114. }
  115. private static final Pattern issuePattern = Pattern
  116. .compile("^(Bug|Issue)[a-zA-Z0-9-]*:.*$"); //$NON-NLS-1$
  117. private static final Pattern footerPattern = Pattern
  118. .compile("(^[a-zA-Z0-9-]+:(?!//).*$)"); //$NON-NLS-1$
  119. private static final Pattern changeIdPattern = Pattern
  120. .compile("(^" + CHANGE_ID + " *I[a-f0-9]{40}$)"); //$NON-NLS-1$ //$NON-NLS-2$
  121. private static final Pattern includeInFooterPattern = Pattern
  122. .compile("^[ \\[].*$"); //$NON-NLS-1$
  123. private static final Pattern trailingWhitespace = Pattern.compile("\\s+$"); //$NON-NLS-1$
  124. /**
  125. * Find the right place to insert a Change-Id and return it.
  126. * <p>
  127. * The Change-Id is inserted before the first footer line but after a Bug
  128. * line.
  129. *
  130. * @param message
  131. * a message.
  132. * @param changeId
  133. * a Change-Id.
  134. * @return a commit message with an inserted Change-Id line
  135. */
  136. public static String insertId(String message, ObjectId changeId) {
  137. return insertId(message, changeId, false);
  138. }
  139. /**
  140. * Find the right place to insert a Change-Id and return it.
  141. * <p>
  142. * If no Change-Id is found the Change-Id is inserted before the first
  143. * footer line but after a Bug line.
  144. *
  145. * If Change-Id is found and replaceExisting is set to false, the message is
  146. * unchanged.
  147. *
  148. * If Change-Id is found and replaceExisting is set to true, the Change-Id
  149. * is replaced with {@code changeId}.
  150. *
  151. * @param message
  152. * a message.
  153. * @param changeId
  154. * a Change-Id.
  155. * @param replaceExisting
  156. * a boolean.
  157. * @return a commit message with an inserted Change-Id line
  158. */
  159. public static String insertId(String message, ObjectId changeId,
  160. boolean replaceExisting) {
  161. int indexOfChangeId = indexOfChangeId(message, "\n"); //$NON-NLS-1$
  162. if (indexOfChangeId > 0) {
  163. if (!replaceExisting)
  164. return message;
  165. else {
  166. StringBuilder ret = new StringBuilder(message.substring(0,
  167. indexOfChangeId));
  168. ret.append(CHANGE_ID);
  169. ret.append(" I"); //$NON-NLS-1$
  170. ret.append(ObjectId.toString(changeId));
  171. int indexOfNextLineBreak = message.indexOf("\n", //$NON-NLS-1$
  172. indexOfChangeId);
  173. if (indexOfNextLineBreak > 0)
  174. ret.append(message.substring(indexOfNextLineBreak));
  175. return ret.toString();
  176. }
  177. }
  178. String[] lines = message.split("\n"); //$NON-NLS-1$
  179. int footerFirstLine = indexOfFirstFooterLine(lines);
  180. int insertAfter = footerFirstLine;
  181. for (int i = footerFirstLine; i < lines.length; ++i) {
  182. if (issuePattern.matcher(lines[i]).matches()) {
  183. insertAfter = i + 1;
  184. continue;
  185. }
  186. break;
  187. }
  188. StringBuilder ret = new StringBuilder();
  189. int i = 0;
  190. for (; i < insertAfter; ++i) {
  191. ret.append(lines[i]);
  192. ret.append("\n"); //$NON-NLS-1$
  193. }
  194. if (insertAfter == lines.length && insertAfter == footerFirstLine)
  195. ret.append("\n"); //$NON-NLS-1$
  196. ret.append(CHANGE_ID);
  197. ret.append(" I"); //$NON-NLS-1$
  198. ret.append(ObjectId.toString(changeId));
  199. ret.append("\n"); //$NON-NLS-1$
  200. for (; i < lines.length; ++i) {
  201. ret.append(lines[i]);
  202. ret.append("\n"); //$NON-NLS-1$
  203. }
  204. return ret.toString();
  205. }
  206. /**
  207. * Return the index in the String {@code message} where the Change-Id entry
  208. * in the footer begins. If there are more than one entries matching the
  209. * pattern, return the index of the last one in the last section. Because of
  210. * Bug: 400818 we release the constraint here that a footer must contain
  211. * only lines matching {@code footerPattern}.
  212. *
  213. * @param message
  214. * a message.
  215. * @param delimiter
  216. * the line delimiter, like "\n" or "\r\n", needed to find the
  217. * footer
  218. * @return the index of the ChangeId footer in the message, or -1 if no
  219. * ChangeId footer available
  220. */
  221. public static int indexOfChangeId(String message, String delimiter) {
  222. String[] lines = message.split(delimiter);
  223. if (lines.length == 0)
  224. return -1;
  225. int indexOfChangeIdLine = 0;
  226. boolean inFooter = false;
  227. for (int i = lines.length - 1; i >= 0; --i) {
  228. if (!inFooter && isEmptyLine(lines[i]))
  229. continue;
  230. inFooter = true;
  231. if (changeIdPattern.matcher(trimRight(lines[i])).matches()) {
  232. indexOfChangeIdLine = i;
  233. break;
  234. } else if (isEmptyLine(lines[i]) || i == 0)
  235. return -1;
  236. }
  237. int indexOfChangeIdLineinString = 0;
  238. for (int i = 0; i < indexOfChangeIdLine; ++i)
  239. indexOfChangeIdLineinString += lines[i].length()
  240. + delimiter.length();
  241. return indexOfChangeIdLineinString
  242. + lines[indexOfChangeIdLine].indexOf(CHANGE_ID);
  243. }
  244. private static boolean isEmptyLine(String line) {
  245. return line.trim().length() == 0;
  246. }
  247. private static String trimRight(String s) {
  248. return trailingWhitespace.matcher(s).replaceAll(""); //$NON-NLS-1$
  249. }
  250. /**
  251. * Find the index of the first line of the footer paragraph in an array of
  252. * the lines, or lines.length if no footer is available
  253. *
  254. * @param lines
  255. * the commit message split into lines and the line delimiters
  256. * stripped off
  257. * @return the index of the first line of the footer paragraph, or
  258. * lines.length if no footer is available
  259. */
  260. public static int indexOfFirstFooterLine(String[] lines) {
  261. int footerFirstLine = lines.length;
  262. for (int i = lines.length - 1; i > 1; --i) {
  263. if (footerPattern.matcher(lines[i]).matches()) {
  264. footerFirstLine = i;
  265. continue;
  266. }
  267. if (footerFirstLine != lines.length && lines[i].length() == 0)
  268. break;
  269. if (footerFirstLine != lines.length
  270. && includeInFooterPattern.matcher(lines[i]).matches()) {
  271. footerFirstLine = i + 1;
  272. continue;
  273. }
  274. footerFirstLine = lines.length;
  275. break;
  276. }
  277. return footerFirstLine;
  278. }
  279. }