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.9KB

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