aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Sohn <matthias.sohn@sap.com>2024-12-13 00:38:29 +0000
committerGerrit Code Review <support@gerrithub.io>2024-12-13 00:38:29 +0000
commitd7c9c503b24c90f644f21eb2a5f5175eaa58e749 (patch)
tree4efdd83c71a88078ef3c461431af1732c8c16ab9
parentcef96317d7ff16d42806780f58f5f2fa835e1688 (diff)
parente9094fffdb9d5d498cf75584267d1fd7e3690374 (diff)
downloadjgit-d7c9c503b24c90f644f21eb2a5f5175eaa58e749.tar.gz
jgit-d7c9c503b24c90f644f21eb2a5f5175eaa58e749.zip
Merge "RevertCommand: use only first line in revert commit message"
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java9
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitParseTest.java11
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java6
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java28
4 files changed, 45 insertions, 9 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java
index 4ebe994ef7..afd6708d21 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2011, Robin Rosenberg and others
+ * Copyright (C) 2011, 2024 Robin Rosenberg and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
@@ -59,7 +59,9 @@ public class RevertCommandTest extends RepositoryTestCase {
writeTrashFile("a",
"first line\nsecond line\nthird line\nfourth line\n");
git.add().addFilepattern("a").call();
- RevCommit fixingA = git.commit().setMessage("fixed a").call();
+ // Commit message with a non-empty second line on purpose
+ RevCommit fixingA = git.commit().setMessage("fixed a\nsecond line")
+ .call();
writeTrashFile("b", "first line\n");
git.add().addFilepattern("b").call();
@@ -78,7 +80,8 @@ public class RevertCommandTest extends RepositoryTestCase {
+ "This reverts commit " + fixingA.getId().getName() + ".\n";
assertEquals(expectedMessage, revertCommit.getFullMessage());
assertEquals("fixed b", history.next().getFullMessage());
- assertEquals("fixed a", history.next().getFullMessage());
+ assertEquals("fixed a\nsecond line",
+ history.next().getFullMessage());
assertEquals("enlarged a", history.next().getFullMessage());
assertEquals("create b", history.next().getFullMessage());
assertEquals("create a", history.next().getFullMessage());
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitParseTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitParseTest.java
index 6872289a8b..2955516af0 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitParseTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitParseTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2008-2009, Google Inc. and others
+ * Copyright (C) 2008, 2024 Google Inc. and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
@@ -408,6 +408,7 @@ public class RevCommitParseTest extends RepositoryTestCase {
final RevCommit c = create(msg);
assertEquals(msg, c.getFullMessage());
assertEquals(msg, c.getShortMessage());
+ assertEquals(msg, c.getFirstMessageLine());
}
@Test
@@ -415,6 +416,7 @@ public class RevCommitParseTest extends RepositoryTestCase {
final RevCommit c = create("\n");
assertEquals("\n", c.getFullMessage());
assertEquals("", c.getShortMessage());
+ assertEquals("", c.getFirstMessageLine());
}
@Test
@@ -423,6 +425,7 @@ public class RevCommitParseTest extends RepositoryTestCase {
final RevCommit c = create(shortMsg);
assertEquals(shortMsg, c.getFullMessage());
assertEquals(shortMsg, c.getShortMessage());
+ assertEquals(shortMsg, c.getFirstMessageLine());
}
@Test
@@ -432,6 +435,7 @@ public class RevCommitParseTest extends RepositoryTestCase {
final RevCommit c = create(fullMsg);
assertEquals(fullMsg, c.getFullMessage());
assertEquals(shortMsg, c.getShortMessage());
+ assertEquals(shortMsg, c.getFirstMessageLine());
}
@Test
@@ -441,6 +445,7 @@ public class RevCommitParseTest extends RepositoryTestCase {
final RevCommit c = create(fullMsg);
assertEquals(fullMsg, c.getFullMessage());
assertEquals(shortMsg, c.getShortMessage());
+ assertEquals("This is a", c.getFirstMessageLine());
}
@Test
@@ -450,6 +455,7 @@ public class RevCommitParseTest extends RepositoryTestCase {
final RevCommit c = create(fullMsg);
assertEquals(fullMsg, c.getFullMessage());
assertEquals(shortMsg, c.getShortMessage());
+ assertEquals("This is a", c.getFirstMessageLine());
}
@Test
@@ -461,6 +467,7 @@ public class RevCommitParseTest extends RepositoryTestCase {
final RevCommit c = create(fullMsg);
assertEquals(fullMsg, c.getFullMessage());
assertEquals(shortMsg, c.getShortMessage());
+ assertEquals(shortMsg, c.getFirstMessageLine());
}
@Test
@@ -480,6 +487,7 @@ public class RevCommitParseTest extends RepositoryTestCase {
assertEquals(author, p.getAuthorIdent());
assertEquals(committer, p.getCommitterIdent());
assertEquals("Test commit", p.getShortMessage());
+ assertEquals("Test commit", p.getFirstMessageLine());
assertEquals(src.getMessage(), p.getFullMessage());
}
@@ -494,6 +502,7 @@ public class RevCommitParseTest extends RepositoryTestCase {
final RevCommit c = create(fullMsg);
assertEquals(fullMsg, c.getFullMessage());
assertEquals(shortMsg, c.getShortMessage());
+ assertEquals("This fixes a", c.getFirstMessageLine());
}
private static ObjectId id(String str) {
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java
index 855c3b1cf3..6643c83662 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com> and others
+ * Copyright (C) 2010, 2024 Christian Halstrick <christian.halstrick@sap.com> and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
@@ -143,8 +143,8 @@ public class RevertCommand extends GitCommand<RevCommit> {
merger.setCommitNames(new String[] {
"BASE", ourName, revertName }); //$NON-NLS-1$
- String shortMessage = "Revert \"" + srcCommit.getShortMessage() //$NON-NLS-1$
- + "\""; //$NON-NLS-1$
+ String shortMessage = "Revert \"" //$NON-NLS-1$
+ + srcCommit.getFirstMessageLine() + '"';
String newMessage = shortMessage + "\n\n" //$NON-NLS-1$
+ "This reverts commit " + srcCommit.getId().getName() //$NON-NLS-1$
+ ".\n"; //$NON-NLS-1$
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java
index 743a8ccce0..55ddebf288 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java
@@ -1,6 +1,6 @@
/*
- * Copyright (C) 2008-2009, Google Inc.
- * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
+ * Copyright (C) 2008, 2009 Google Inc.
+ * Copyright (C) 2008, 2024 Shawn O. Pearce <spearce@spearce.org> and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
@@ -524,6 +524,30 @@ public class RevCommit extends RevObject {
}
/**
+ * Parse the commit message and return its first line, i.e., everything up
+ * to but not including the first newline, if any.
+ *
+ * @return the first line of the decoded commit message as a string; never
+ * {@code null}.
+ * @since 7.2
+ */
+ public final String getFirstMessageLine() {
+ int msgB = RawParseUtils.commitMessage(buffer, 0);
+ if (msgB < 0) {
+ return ""; //$NON-NLS-1$
+ }
+ int msgE = msgB;
+ byte[] raw = buffer;
+ while (msgE < raw.length && raw[msgE] != '\n') {
+ msgE++;
+ }
+ if (msgE > msgB && msgE > 0 && raw[msgE - 1] == '\r') {
+ msgE--;
+ }
+ return RawParseUtils.decode(guessEncoding(buffer), buffer, msgB, msgE);
+ }
+
+ /**
* Determine the encoding of the commit message buffer.
* <p>
* Locates the "encoding" header (if present) and returns its value. Due to