Browse Source

CommitCommand: Use original author on amend if author is not set

This way, callers don't have to parse author ident of HEAD themselves.

Bug: 362391
Change-Id: I383a817e6ed4707d637c52c007bc7b57728e6c85
stable-2.2
Robin Stocker 12 years ago
parent
commit
79f33419ec
  1. 44
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java
  2. 10
      org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java

44
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java

@ -48,7 +48,9 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.File; import java.io.File;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.TimeZone;
import org.eclipse.jgit.api.errors.WrongRepositoryStateException; import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
import org.eclipse.jgit.diff.DiffEntry; import org.eclipse.jgit.diff.DiffEntry;
@ -57,6 +59,7 @@ import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.RefUpdate; import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.RefUpdate.Result; import org.eclipse.jgit.lib.RefUpdate.Result;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
@ -427,4 +430,45 @@ public class CommitCommandTest extends RepositoryTestCase {
Git git = new Git(db); Git git = new Git(db);
git.commit().setAmend(true).setMessage("initial commit").call(); git.commit().setAmend(true).setMessage("initial commit").call();
} }
@Test
public void commitAmendWithoutAuthorShouldSetOriginalAuthorAndAuthorTime()
throws Exception {
Git git = new Git(db);
writeTrashFile("file1", "file1");
git.add().addFilepattern("file1").call();
final String authorName = "First Author";
final String authorEmail = "author@example.org";
final Date authorDate = new Date(1349621117000L);
PersonIdent firstAuthor = new PersonIdent(authorName, authorEmail,
authorDate, TimeZone.getTimeZone("UTC"));
git.commit().setMessage("initial commit").setAuthor(firstAuthor).call();
RevCommit amended = git.commit().setAmend(true)
.setMessage("amend commit").call();
PersonIdent amendedAuthor = amended.getAuthorIdent();
assertEquals(authorName, amendedAuthor.getName());
assertEquals(authorEmail, amendedAuthor.getEmailAddress());
assertEquals(authorDate.getTime(), amendedAuthor.getWhen().getTime());
}
@Test
public void commitAmendWithAuthorShouldUseIt() throws Exception {
Git git = new Git(db);
writeTrashFile("file1", "file1");
git.add().addFilepattern("file1").call();
git.commit().setMessage("initial commit").call();
RevCommit amended = git.commit().setAmend(true)
.setAuthor("New Author", "newauthor@example.org")
.setMessage("amend commit").call();
PersonIdent amendedAuthor = amended.getAuthorIdent();
assertEquals("New Author", amendedAuthor.getName());
assertEquals("newauthor@example.org", amendedAuthor.getEmailAddress());
}
} }

10
org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java

@ -187,6 +187,8 @@ public class CommitCommand extends GitCommand<RevCommit> {
RevCommit[] p = previousCommit.getParents(); RevCommit[] p = previousCommit.getParents();
for (int i = 0; i < p.length; i++) for (int i = 0; i < p.length; i++)
parents.add(0, p[i].getId()); parents.add(0, p[i].getId());
if (author == null)
author = previousCommit.getAuthorIdent();
} else { } else {
parents.add(0, headId); parents.add(0, headId);
} }
@ -471,7 +473,7 @@ public class CommitCommand extends GitCommand<RevCommit> {
private void processOptions(RepositoryState state) throws NoMessageException { private void processOptions(RepositoryState state) throws NoMessageException {
if (committer == null) if (committer == null)
committer = new PersonIdent(repo); committer = new PersonIdent(repo);
if (author == null) if (author == null && !amend)
author = committer; author = committer;
// when doing a merge commit parse MERGE_HEAD and MERGE_MSG files // when doing a merge commit parse MERGE_HEAD and MERGE_MSG files
@ -574,7 +576,8 @@ public class CommitCommand extends GitCommand<RevCommit> {
/** /**
* Sets the author for this {@code commit}. If no author is explicitly * Sets the author for this {@code commit}. If no author is explicitly
* specified because this method is never called or called with {@code null} * specified because this method is never called or called with {@code null}
* value then the author will be set to the committer. * value then the author will be set to the committer or to the original
* author when amending.
* *
* @param author * @param author
* the author used for the {@code commit} * the author used for the {@code commit}
@ -589,7 +592,8 @@ public class CommitCommand extends GitCommand<RevCommit> {
/** /**
* Sets the author for this {@code commit}. If no author is explicitly * Sets the author for this {@code commit}. If no author is explicitly
* specified because this method is never called or called with {@code null} * specified because this method is never called or called with {@code null}
* value then the author will be set to the committer. * value then the author will be set to the committer or to the original
* author when amending.
* *
* @param name * @param name
* the name of the author used for the {@code commit} * the name of the author used for the {@code commit}

Loading…
Cancel
Save