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 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 PersonIdent} for the presumed author and time
  80. * @param committer
  81. * the {@link PersonIdent} for the presumed committer and time
  82. * @param message
  83. * The commit message
  84. * @return the change id SHA1 string (without the 'I') or null if the
  85. * message is not complete enough
  86. */
  87. public static ObjectId computeChangeId(final ObjectId treeId,
  88. final ObjectId firstParentId, final PersonIdent author,
  89. final PersonIdent committer, final String message) {
  90. String cleanMessage = clean(message);
  91. if (cleanMessage.length() == 0)
  92. return null;
  93. StringBuilder b = new StringBuilder();
  94. b.append("tree "); //$NON-NLS-1$
  95. b.append(ObjectId.toString(treeId));
  96. b.append("\n"); //$NON-NLS-1$
  97. if (firstParentId != null) {
  98. b.append("parent "); //$NON-NLS-1$
  99. b.append(ObjectId.toString(firstParentId));
  100. b.append("\n"); //$NON-NLS-1$
  101. }
  102. b.append("author "); //$NON-NLS-1$
  103. b.append(author.toExternalString());
  104. b.append("\n"); //$NON-NLS-1$
  105. b.append("committer "); //$NON-NLS-1$
  106. b.append(committer.toExternalString());
  107. b.append("\n\n"); //$NON-NLS-1$
  108. b.append(cleanMessage);
  109. try (ObjectInserter f = new ObjectInserter.Formatter()) {
  110. return f.idFor(Constants.OBJ_COMMIT, Constants.encode(b.toString()));
  111. }
  112. }
  113. private static final Pattern issuePattern = Pattern
  114. .compile("^(Bug|Issue)[a-zA-Z0-9-]*:.*$"); //$NON-NLS-1$
  115. private static final Pattern footerPattern = Pattern
  116. .compile("(^[a-zA-Z0-9-]+:(?!//).*$)"); //$NON-NLS-1$
  117. private static final Pattern changeIdPattern = Pattern
  118. .compile("(^" + CHANGE_ID + " *I[a-f0-9]{40}$)"); //$NON-NLS-1$ //$NON-NLS-2$
  119. private static final Pattern includeInFooterPattern = Pattern
  120. .compile("^[ \\[].*$"); //$NON-NLS-1$
  121. private static final Pattern trailingWhitespace = Pattern.compile("\\s+$"); //$NON-NLS-1$
  122. /**
  123. * Find the right place to insert a Change-Id and return it.
  124. * <p>
  125. * The Change-Id is inserted before the first footer line but after a Bug
  126. * line.
  127. *
  128. * @param message
  129. * @param changeId
  130. * @return a commit message with an inserted Change-Id line
  131. */
  132. public static String insertId(String message, ObjectId changeId) {
  133. return insertId(message, changeId, false);
  134. }
  135. /**
  136. * Find the right place to insert a Change-Id and return it.
  137. * <p>
  138. * If no Change-Id is found the Change-Id is inserted before
  139. * the first footer line but after a Bug line.
  140. *
  141. * If Change-Id is found and replaceExisting is set to false,
  142. * the message is unchanged.
  143. *
  144. * If Change-Id is found and replaceExisting is set to true,
  145. * the Change-Id is replaced with {@code changeId}.
  146. *
  147. * @param message
  148. * @param changeId
  149. * @param replaceExisting
  150. * @return a commit message with an inserted Change-Id line
  151. */
  152. public static String insertId(String message, ObjectId changeId,
  153. boolean replaceExisting) {
  154. int indexOfChangeId = indexOfChangeId(message, "\n"); //$NON-NLS-1$
  155. if (indexOfChangeId > 0) {
  156. if (!replaceExisting)
  157. return message;
  158. else {
  159. StringBuilder ret = new StringBuilder(message.substring(0,
  160. indexOfChangeId));
  161. ret.append(CHANGE_ID);
  162. ret.append(" I"); //$NON-NLS-1$
  163. ret.append(ObjectId.toString(changeId));
  164. int indexOfNextLineBreak = message.indexOf("\n", //$NON-NLS-1$
  165. indexOfChangeId);
  166. if (indexOfNextLineBreak > 0)
  167. ret.append(message.substring(indexOfNextLineBreak));
  168. return ret.toString();
  169. }
  170. }
  171. String[] lines = message.split("\n"); //$NON-NLS-1$
  172. int footerFirstLine = indexOfFirstFooterLine(lines);
  173. int insertAfter = footerFirstLine;
  174. for (int i = footerFirstLine; i < lines.length; ++i) {
  175. if (issuePattern.matcher(lines[i]).matches()) {
  176. insertAfter = i + 1;
  177. continue;
  178. }
  179. break;
  180. }
  181. StringBuilder ret = new StringBuilder();
  182. int i = 0;
  183. for (; i < insertAfter; ++i) {
  184. ret.append(lines[i]);
  185. ret.append("\n"); //$NON-NLS-1$
  186. }
  187. if (insertAfter == lines.length && insertAfter == footerFirstLine)
  188. ret.append("\n"); //$NON-NLS-1$
  189. ret.append(CHANGE_ID);
  190. ret.append(" I"); //$NON-NLS-1$
  191. ret.append(ObjectId.toString(changeId));
  192. ret.append("\n"); //$NON-NLS-1$
  193. for (; i < lines.length; ++i) {
  194. ret.append(lines[i]);
  195. ret.append("\n"); //$NON-NLS-1$
  196. }
  197. return ret.toString();
  198. }
  199. /**
  200. * Return the index in the String {@code message} where the Change-Id entry
  201. * in the footer begins. If there are more than one entries matching the
  202. * pattern, return the index of the last one in the last section. Because of
  203. * Bug: 400818 we release the constraint here that a footer must contain
  204. * only lines matching {@code footerPattern}.
  205. *
  206. * @param message
  207. * @param delimiter
  208. * the line delimiter, like "\n" or "\r\n", needed to find the
  209. * footer
  210. * @return the index of the ChangeId footer in the message, or -1 if no
  211. * ChangeId footer available
  212. */
  213. public static int indexOfChangeId(String message, String delimiter) {
  214. String[] lines = message.split(delimiter);
  215. if (lines.length == 0)
  216. return -1;
  217. int indexOfChangeIdLine = 0;
  218. boolean inFooter = false;
  219. for (int i = lines.length - 1; i >= 0; --i) {
  220. if (!inFooter && isEmptyLine(lines[i]))
  221. continue;
  222. inFooter = true;
  223. if (changeIdPattern.matcher(trimRight(lines[i])).matches()) {
  224. indexOfChangeIdLine = i;
  225. break;
  226. } else if (isEmptyLine(lines[i]) || i == 0)
  227. return -1;
  228. }
  229. int indexOfChangeIdLineinString = 0;
  230. for (int i = 0; i < indexOfChangeIdLine; ++i)
  231. indexOfChangeIdLineinString += lines[i].length()
  232. + delimiter.length();
  233. return indexOfChangeIdLineinString
  234. + lines[indexOfChangeIdLine].indexOf(CHANGE_ID);
  235. }
  236. private static boolean isEmptyLine(String line) {
  237. return line.trim().length() == 0;
  238. }
  239. private static String trimRight(String s) {
  240. return trailingWhitespace.matcher(s).replaceAll(""); //$NON-NLS-1$
  241. }
  242. /**
  243. * Find the index of the first line of the footer paragraph in an array of
  244. * the lines, or lines.length if no footer is available
  245. *
  246. * @param lines
  247. * the commit message split into lines and the line delimiters
  248. * stripped off
  249. * @return the index of the first line of the footer paragraph, or
  250. * lines.length if no footer is available
  251. */
  252. public static int indexOfFirstFooterLine(String[] lines) {
  253. int footerFirstLine = lines.length;
  254. for (int i = lines.length - 1; i > 1; --i) {
  255. if (footerPattern.matcher(lines[i]).matches()) {
  256. footerFirstLine = i;
  257. continue;
  258. }
  259. if (footerFirstLine != lines.length && lines[i].length() == 0)
  260. break;
  261. if (footerFirstLine != lines.length
  262. && includeInFooterPattern.matcher(lines[i]).matches()) {
  263. footerFirstLine = i + 1;
  264. continue;
  265. }
  266. footerFirstLine = lines.length;
  267. break;
  268. }
  269. return footerFirstLine;
  270. }
  271. }