diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java index 9d87f0c29..8a07118f8 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java @@ -68,6 +68,7 @@ import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.RefUpdate; import org.eclipse.jgit.lib.RefUpdate.Result; +import org.eclipse.jgit.lib.ReflogEntry; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.StoredConfig; import org.eclipse.jgit.revwalk.RevCommit; @@ -436,6 +437,36 @@ public class CommitCommandTest extends RepositoryTestCase { } } + @Test + public void testReflogs() throws Exception { + try (Git git = new Git(db)) { + writeTrashFile("f", "1"); + git.add().addFilepattern("f").call(); + git.commit().setMessage("c1").call(); + writeTrashFile("f", "2"); + git.commit().setMessage("c2").setAll(true).setReflogComment(null) + .call(); + writeTrashFile("f", "3"); + git.commit().setMessage("c3").setAll(true) + .setReflogComment("testRl").call(); + + db.getReflogReader(Constants.HEAD).getReverseEntries(); + + assertEquals("testRl;commit (initial): c1;", reflogComments( + db.getReflogReader(Constants.HEAD).getReverseEntries())); + assertEquals("testRl;commit (initial): c1;", reflogComments( + db.getReflogReader(db.getBranch()).getReverseEntries())); + } + } + + private static String reflogComments(List entries) { + StringBuffer b = new StringBuffer(); + for (ReflogEntry e : entries) { + b.append(e.getComment()).append(";"); + } + return b.toString(); + } + @Test(expected = WrongRepositoryStateException.class) public void commitAmendOnInitialShouldFail() throws Exception { try (Git git = new Git(db)) { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java index 0abb8ba4e..e54169ade 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java @@ -124,6 +124,8 @@ public class CommitCommand extends GitCommand { private String reflogComment; + private boolean useDefaultReflogMessage = true; + /** * Setting this option bypasses the pre-commit and commit-msg hooks. */ @@ -258,7 +260,7 @@ public class CommitCommand extends GitCommand { RevCommit revCommit = rw.parseCommit(commitId); RefUpdate ru = repo.updateRef(Constants.HEAD); ru.setNewObjectId(commitId); - if (reflogComment != null) { + if (!useDefaultReflogMessage) { ru.setRefLogMessage(reflogComment, false); } else { String prefix = amend ? "commit (amend): " //$NON-NLS-1$ @@ -790,10 +792,13 @@ public class CommitCommand extends GitCommand { * Override the message written to the reflog * * @param reflogComment + * the comment to be written into the reflog or null + * to specify that no reflog should be written * @return {@code this} */ public CommitCommand setReflogComment(String reflogComment) { this.reflogComment = reflogComment; + useDefaultReflogMessage = false; return this; }