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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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:";
  61. // package-private so the unit test can test this part only
  62. static String clean(String msg) {
  63. return msg.//
  64. replaceAll("(?i)(?m)^Signed-off-by:.*$\n?", "").//
  65. replaceAll("(?m)^#.*$\n?", "").//
  66. replaceAll("(?m)\n\n\n+", "\\\n").//
  67. replaceAll("\\n*$", "").//
  68. replaceAll("(?s)\ndiff --git.*", "").//
  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. * @throws IOException
  87. */
  88. public static ObjectId computeChangeId(final ObjectId treeId,
  89. final ObjectId firstParentId, final PersonIdent author,
  90. final PersonIdent committer, final String message)
  91. throws IOException {
  92. String cleanMessage = clean(message);
  93. if (cleanMessage.length() == 0)
  94. return null;
  95. StringBuilder b = new StringBuilder();
  96. b.append("tree ");
  97. b.append(ObjectId.toString(treeId));
  98. b.append("\n");
  99. if (firstParentId != null) {
  100. b.append("parent ");
  101. b.append(ObjectId.toString(firstParentId));
  102. b.append("\n");
  103. }
  104. b.append("author ");
  105. b.append(author.toExternalString());
  106. b.append("\n");
  107. b.append("committer ");
  108. b.append(committer.toExternalString());
  109. b.append("\n\n");
  110. b.append(cleanMessage);
  111. return new ObjectInserter.Formatter().idFor(Constants.OBJ_COMMIT, //
  112. b.toString().getBytes(Constants.CHARACTER_ENCODING));
  113. }
  114. private static final Pattern issuePattern = Pattern
  115. .compile("^(Bug|Issue)[a-zA-Z0-9-]*:.*$");
  116. private static final Pattern footerPattern = Pattern
  117. .compile("(^[a-zA-Z0-9-]+:(?!//).*$)");
  118. private static final Pattern includeInFooterPattern = Pattern
  119. .compile("^[ \\[].*$");
  120. /**
  121. * Find the right place to insert a Change-Id and return it.
  122. * <p>
  123. * The Change-Id is inserted before the first footer line but after a Bug
  124. * line.
  125. *
  126. * @param message
  127. * @param changeId
  128. * @return a commit message with an inserted Change-Id line
  129. */
  130. public static String insertId(String message, ObjectId changeId) {
  131. return insertId(message, changeId, false);
  132. }
  133. /**
  134. * Find the right place to insert a Change-Id and return it.
  135. * <p>
  136. * If no Change-Id is found the Change-Id is inserted before
  137. * the first footer line but after a Bug line.
  138. *
  139. * If Change-Id is found and replaceExisting is set to false,
  140. * the message is unchanged.
  141. *
  142. * If Change-Id is found and replaceExisting is set to true,
  143. * the Change-Id is replaced with {@code changeId}.
  144. *
  145. * @param message
  146. * @param changeId
  147. * @param replaceExisting
  148. * @return a commit message with an inserted Change-Id line
  149. */
  150. public static String insertId(String message, ObjectId changeId,
  151. boolean replaceExisting) {
  152. if (message.indexOf(CHANGE_ID) > 0) {
  153. if (replaceExisting) {
  154. int i = message.indexOf(CHANGE_ID) + 10;
  155. while (message.charAt(i) == ' ')
  156. i++;
  157. String oldId = message.length() == (i + 40) ?
  158. message.substring(i) : message.substring(i, i + 41);
  159. message = message.replace(oldId, "I" + changeId.getName());
  160. }
  161. return message;
  162. }
  163. String[] lines = message.split("\n");
  164. int footerFirstLine = lines.length;
  165. for (int i = lines.length - 1; i > 1; --i) {
  166. if (footerPattern.matcher(lines[i]).matches()) {
  167. footerFirstLine = i;
  168. continue;
  169. }
  170. if (footerFirstLine != lines.length && lines[i].length() == 0) {
  171. break;
  172. }
  173. if (footerFirstLine != lines.length
  174. && includeInFooterPattern.matcher(lines[i]).matches()) {
  175. footerFirstLine = i + 1;
  176. continue;
  177. }
  178. footerFirstLine = lines.length;
  179. break;
  180. }
  181. int insertAfter = footerFirstLine;
  182. for (int i = footerFirstLine; i < lines.length; ++i) {
  183. if (issuePattern.matcher(lines[i]).matches()) {
  184. insertAfter = i + 1;
  185. continue;
  186. }
  187. break;
  188. }
  189. StringBuilder ret = new StringBuilder();
  190. int i = 0;
  191. for (; i < insertAfter; ++i) {
  192. ret.append(lines[i]);
  193. ret.append("\n");
  194. }
  195. if (insertAfter == lines.length && insertAfter == footerFirstLine)
  196. ret.append("\n");
  197. ret.append(CHANGE_ID);
  198. ret.append(" I");
  199. ret.append(ObjectId.toString(changeId));
  200. ret.append("\n");
  201. for (; i < lines.length; ++i) {
  202. ret.append(lines[i]);
  203. ret.append("\n");
  204. }
  205. return ret.toString();
  206. }
  207. }