aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse
diff options
context:
space:
mode:
authorStefan Lay <stefan.lay@sap.com>2013-01-09 14:02:13 +0100
committerRobin Rosenberg <robin.rosenberg@dewire.com>2013-01-19 17:37:09 +0100
commit1ca7c581a326dab9418d3d3eb665e1f27f6ca8ed (patch)
tree5282571e6984f004532d1806e05b83dcb880cf8b /org.eclipse.jgit/src/org/eclipse
parent3f0176aea6e77cb39be35f091100f8874693593d (diff)
downloadjgit-1ca7c581a326dab9418d3d3eb665e1f27f6ca8ed.tar.gz
jgit-1ca7c581a326dab9418d3d3eb665e1f27f6ca8ed.zip
Only replace the ChangeId in the footer, not in the body
Additionally expose methods to find the first footer line and to find the position of the ChangeId footer in the commit message in order to enable reuse of these methods introduced for the fix. Change-Id: I87ecca009ca3bff1ca0de3c436ebd95736bf5a10 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com> Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/util/ChangeIdUtil.java95
1 files changed, 69 insertions, 26 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 a869b2beac..41bb4cc7eb 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/ChangeIdUtil.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/ChangeIdUtil.java
@@ -161,36 +161,26 @@ public class ChangeIdUtil {
*/
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()); //$NON-NLS-1$
+ int indexOfChangeId = indexOfChangeId(message, "\n"); //$NON-NLS-1$
+ if (indexOfChangeId > 0) {
+ if (!replaceExisting)
+ return message;
+ else {
+ StringBuilder ret = new StringBuilder(message.substring(0,
+ indexOfChangeId));
+ ret.append(CHANGE_ID);
+ ret.append(" I"); //$NON-NLS-1$
+ ret.append(ObjectId.toString(changeId));
+ int indexOfNextLineBreak = message.indexOf("\n", //$NON-NLS-1$
+ indexOfChangeId);
+ if (indexOfNextLineBreak > 0)
+ ret.append(message.substring(indexOfNextLineBreak));
+ return ret.toString();
}
- return message;
}
String[] lines = message.split("\n"); //$NON-NLS-1$
- int footerFirstLine = lines.length;
- for (int i = lines.length - 1; i > 1; --i) {
- if (footerPattern.matcher(lines[i]).matches()) {
- footerFirstLine = i;
- continue;
- }
- if (footerFirstLine != lines.length && lines[i].length() == 0) {
- break;
- }
- if (footerFirstLine != lines.length
- && includeInFooterPattern.matcher(lines[i]).matches()) {
- footerFirstLine = i + 1;
- continue;
- }
- footerFirstLine = lines.length;
- break;
- }
+ int footerFirstLine = indexOfFirstFooterLine(lines);
int insertAfter = footerFirstLine;
for (int i = footerFirstLine; i < lines.length; ++i) {
if (issuePattern.matcher(lines[i]).matches()) {
@@ -217,4 +207,57 @@ public class ChangeIdUtil {
}
return ret.toString();
}
+
+ /**
+ * Find the index in the String {@code} message} where the Change-Id entry
+ * begins
+ *
+ * @param message
+ * @param delimiter
+ * the line delimiter, like "\n" or "\r\n", needed to find the
+ * footer
+ * @return the index of the ChangeId footer in the message, or -1 if no
+ * ChangeId footer available
+ */
+ public static int indexOfChangeId(String message, String delimiter) {
+ String[] lines = message.split(delimiter);
+ int footerFirstLine = indexOfFirstFooterLine(lines);
+ if (footerFirstLine == lines.length)
+ return -1;
+
+ int indexOfFooter = 0;
+ for (int i = 0; i < footerFirstLine; ++i)
+ indexOfFooter += lines[i].length() + delimiter.length();
+ return message.indexOf(CHANGE_ID, indexOfFooter);
+ }
+
+ /**
+ * Find the index of the first line of the footer paragraph in an array of
+ * the lines, or lines.length if no footer is available
+ *
+ * @param lines
+ * the commit message split into lines and the line delimiters
+ * stripped off
+ * @return the index of the first line of the footer paragraph, or
+ * lines.length if no footer is available
+ */
+ public static int indexOfFirstFooterLine(String[] lines) {
+ int footerFirstLine = lines.length;
+ for (int i = lines.length - 1; i > 1; --i) {
+ if (footerPattern.matcher(lines[i]).matches()) {
+ footerFirstLine = i;
+ continue;
+ }
+ if (footerFirstLine != lines.length && lines[i].length() == 0)
+ break;
+ if (footerFirstLine != lines.length
+ && includeInFooterPattern.matcher(lines[i]).matches()) {
+ footerFirstLine = i + 1;
+ continue;
+ }
+ footerFirstLine = lines.length;
+ break;
+ }
+ return footerFirstLine;
+ }
}