mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-03 12:05:52 +01:00
implement squash to mergeCommand
This commit is contained in:
@@ -16,6 +16,7 @@ import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.repository.Person;
|
||||
import sonia.scm.repository.api.MergeCommandResult;
|
||||
import sonia.scm.repository.api.MergeDryRunCommandResult;
|
||||
import sonia.scm.repository.api.ScmMergeStrategy;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.MessageFormat;
|
||||
@@ -61,6 +62,7 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
|
||||
private final String toMerge;
|
||||
private final Person author;
|
||||
private final String messageTemplate;
|
||||
private final ScmMergeStrategy scmMergeStrategy;
|
||||
|
||||
private MergeWorker(Git clone, MergeCommandRequest request) {
|
||||
super(clone);
|
||||
@@ -68,6 +70,7 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
|
||||
this.toMerge = request.getBranchToMerge();
|
||||
this.author = request.getAuthor();
|
||||
this.messageTemplate = request.getMessageTemplate();
|
||||
this.scmMergeStrategy = request.getScmMergeStrategy();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -86,11 +89,18 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
|
||||
MergeResult result;
|
||||
try {
|
||||
ObjectId sourceRevision = resolveRevision(toMerge);
|
||||
result = getClone().merge()
|
||||
.setFastForward(FastForwardMode.NO_FF)
|
||||
org.eclipse.jgit.api.MergeCommand mergeCommand = getClone().merge();
|
||||
mergeCommand
|
||||
.setCommit(false) // we want to set the author manually
|
||||
.include(toMerge, sourceRevision)
|
||||
.call();
|
||||
.include(toMerge, sourceRevision);
|
||||
|
||||
if (scmMergeStrategy == ScmMergeStrategy.SQUASH) {
|
||||
mergeCommand.setSquash(true);
|
||||
} else {
|
||||
mergeCommand.setFastForward(FastForwardMode.NO_FF);
|
||||
}
|
||||
|
||||
result = mergeCommand.call();
|
||||
} catch (GitAPIException e) {
|
||||
throw new InternalRepositoryException(context.getRepository(), "could not merge branch " + toMerge + " into " + target, e);
|
||||
}
|
||||
|
||||
@@ -15,10 +15,12 @@ import org.junit.Test;
|
||||
import sonia.scm.NotFoundException;
|
||||
import sonia.scm.repository.Person;
|
||||
import sonia.scm.repository.api.MergeCommandResult;
|
||||
import sonia.scm.repository.api.ScmMergeStrategy;
|
||||
import sonia.scm.repository.util.WorkdirProvider;
|
||||
import sonia.scm.user.User;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -211,6 +213,55 @@ public class GitMergeCommandTest extends AbstractGitCommandTestBase {
|
||||
assertThat(new String(contentOfFileB)).isEqualTo("b\ncontent from branch\n");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSquashCommitsIfSquashIsEnabled() throws IOException, GitAPIException {
|
||||
GitMergeCommand command = createCommand();
|
||||
MergeCommandRequest request = new MergeCommandRequest();
|
||||
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
|
||||
request.setBranchToMerge("squash");
|
||||
request.setTargetBranch("master");
|
||||
request.setMessageTemplate("this is a squash");
|
||||
request.setScmMergeStrategy(ScmMergeStrategy.SQUASH);
|
||||
|
||||
MergeCommandResult mergeCommandResult = command.merge(request);
|
||||
|
||||
Repository repository = createContext().open();
|
||||
assertThat(mergeCommandResult.isSuccess()).isTrue();
|
||||
|
||||
Iterable<RevCommit> commits = new Git(repository).log().add(repository.resolve("master")).setMaxCount(1).call();
|
||||
RevCommit mergeCommit = commits.iterator().next();
|
||||
PersonIdent mergeAuthor = mergeCommit.getAuthorIdent();
|
||||
String message = mergeCommit.getFullMessage();
|
||||
assertThat(mergeAuthor.getName()).isEqualTo("Dirk Gently");
|
||||
assertThat(message).isEqualTo("this is a squash");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSquashThreeCommitsIntoOne() throws IOException, GitAPIException {
|
||||
GitMergeCommand command = createCommand();
|
||||
MergeCommandRequest request = new MergeCommandRequest();
|
||||
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
|
||||
request.setBranchToMerge("squash");
|
||||
request.setTargetBranch("master");
|
||||
request.setMessageTemplate("squash three commits");
|
||||
request.setScmMergeStrategy(ScmMergeStrategy.SQUASH);
|
||||
Repository gitRepository = createContext().open();
|
||||
MergeCommandResult mergeCommandResult = command.merge(request);
|
||||
|
||||
assertThat(mergeCommandResult.isSuccess()).isTrue();
|
||||
|
||||
Iterable<RevCommit> commits = new Git(gitRepository).log().add(gitRepository.resolve("master")).setMaxCount(1).call();
|
||||
RevCommit mergeCommit = commits.iterator().next();
|
||||
PersonIdent mergeAuthor = mergeCommit.getAuthorIdent();
|
||||
String message = mergeCommit.getFullMessage();
|
||||
assertThat(mergeAuthor.getName()).isEqualTo("Dirk Gently");
|
||||
assertThat(message).isEqualTo("squash three commits");
|
||||
|
||||
GitModificationsCommand modificationsCommand = new GitModificationsCommand(createContext(), repository);
|
||||
List<String> changes = modificationsCommand.getModifications("master").getAdded();
|
||||
assertThat(changes.size()).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test(expected = NotFoundException.class)
|
||||
public void shouldHandleNotExistingSourceBranchInMerge() {
|
||||
GitMergeCommand command = createCommand();
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user