Set author for merge

This commit is contained in:
René Pfeuffer
2018-11-07 14:02:02 +01:00
parent 2d04e6c2f0
commit 8771f68081
4 changed files with 49 additions and 6 deletions

View File

@@ -71,8 +71,7 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
MergeResult result;
try {
result = clone.merge()
.setCommit(true)
.setMessage(String.format(MERGE_COMMIT_MESSAGE_TEMPLATE, toMerge, target))
.setCommit(false) // we want to set the author manually
.include(toMerge, resolveRevision(toMerge))
.call();
} catch (GitAPIException e) {
@@ -80,6 +79,14 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
}
if (result.getMergeStatus().isSuccessful()) {
logger.debug("merged branch {} into {}", toMerge, target);
try {
clone.commit()
.setAuthor(request.getAuthor().getName(), request.getAuthor().getMail())
.setMessage(String.format(MERGE_COMMIT_MESSAGE_TEMPLATE, toMerge, target))
.call();
} catch (GitAPIException e) {
throw new InternalRepositoryException("could not commit merge between branch " + toMerge + " and " + target, e);
}
try {
clone.push().call();
} catch (GitAPIException e) {

View File

@@ -1,9 +1,17 @@
package sonia.scm.repository.spi;
import org.assertj.core.api.Assertions;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Test;
import sonia.scm.repository.Person;
import sonia.scm.repository.api.MergeCommandResult;
import java.io.IOException;
import static org.assertj.core.api.Assertions.assertThat;
@@ -34,15 +42,22 @@ public class GitMergeCommandTest extends AbstractGitCommandTestBase {
}
@Test
public void shouldMergeMergeableBranches() {
public void shouldMergeMergeableBranches() throws IOException, GitAPIException {
GitMergeCommand command = createCommand();
MergeCommandRequest request = new MergeCommandRequest();
request.setTargetBranch("master");
request.setBranchToMerge("mergeable");
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
MergeCommandResult mergeCommandResult = command.merge(request);
assertThat(mergeCommandResult.isSuccess()).isTrue();
Repository repository = createContext().open();
Iterable<RevCommit> mergeCommit = new Git(repository).log().add(repository.resolve("master")).setMaxCount(1).call();
PersonIdent mergeAuthor = mergeCommit.iterator().next().getAuthorIdent();
assertThat(mergeAuthor.getName()).isEqualTo("Dirk Gently");
assertThat(mergeAuthor.getEmailAddress()).isEqualTo("dirk@holistic.det");
}
@Test