diff options
author | Stefan Lay <stefan.lay@sap.com> | 2010-08-05 14:11:38 +0200 |
---|---|---|
committer | Chris Aniszczyk <caniszczyk@gmail.com> | 2010-08-05 12:23:38 -0500 |
commit | 4b464ed458199476ec6547ff7fcdd9dd6f6608e6 (patch) | |
tree | d61f4c4169743a930fc65a68775bcad1ce804471 /org.eclipse.jgit | |
parent | 60c5939b236723cb43344115eed03caf8de2b22a (diff) | |
download | jgit-4b464ed458199476ec6547ff7fcdd9dd6f6608e6.tar.gz jgit-4b464ed458199476ec6547ff7fcdd9dd6f6608e6.zip |
Allow to replace existing Change-Id
It is useful to be able to replace an existing Change-Id
in the message, for example if the user decides not to
amend the previous commit.
Bug: 321188
Change-Id: I594e7f9efd0c57d794d2bd26d55ec45f4e6a47fd
Signed-off-by: Stefan Lay <stefan.lay@sap.com>
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/util/ChangeIdUtil.java | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/ChangeIdUtil.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/ChangeIdUtil.java index 2ffcbed9f1..a8e505d6e5 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/ChangeIdUtil.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/ChangeIdUtil.java @@ -61,6 +61,8 @@ import org.eclipse.jgit.lib.PersonIdent; */ public class ChangeIdUtil { + static final String CHANGE_ID = "Change-Id:"; + // package-private so the unit test can test this part only static String clean(String msg) { return msg.// @@ -136,8 +138,39 @@ public class ChangeIdUtil { * @return a commit message with an inserted Change-Id line */ public static String insertId(String message, ObjectId changeId) { - if (message.indexOf("\nChange-Id:") > 0) + return insertId(message, changeId, false); + } + + /** + * Find the right place to insert a Change-Id and return it. + * <p> + * If no Change-Id is found the Change-Id is inserted before + * the first footer line but after a Bug line. + * + * If Change-Id is found and replaceExisting is set to false, + * the message is unchanged. + * + * If Change-Id is found and replaceExisting is set to true, + * the Change-Id is replaced with {@code changeId}. + * + * @param message + * @param changeId + * @param replaceExisting + * @return a commit message with an inserted Change-Id line + */ + public static String insertId(String message, ObjectId changeId, + boolean replaceExisting) { + if (message.indexOf(CHANGE_ID) > 0) { + if (replaceExisting) { + int i = message.indexOf(CHANGE_ID) + 10; + while (message.charAt(i) == ' ') + i++; + String oldId = message.length() == (i + 40) ? + message.substring(i) : message.substring(i, i + 41); + message = message.replace(oldId, "I" + changeId.getName()); + } return message; + } String[] lines = message.split("\n"); int footerFirstLine = lines.length; @@ -173,7 +206,8 @@ public class ChangeIdUtil { } if (insertAfter == lines.length && insertAfter == footerFirstLine) ret.append("\n"); - ret.append("Change-Id: I"); + ret.append(CHANGE_ID); + ret.append(" I"); ret.append(ObjectId.toString(changeId)); ret.append("\n"); for (; i < lines.length; ++i) { |