implement fast forward if possible

This commit is contained in:
Eduard Heimbuch
2019-11-06 15:26:20 +01:00
parent b4f1e8874a
commit cf90654e24
3 changed files with 46 additions and 3 deletions

View File

@@ -13,6 +13,7 @@ import static java.util.Collections.unmodifiableCollection;
*/
public class MergeCommandResult {
private final Collection<String> filesWithConflict;
private boolean aborted = false;
private MergeCommandResult(Collection<String> filesWithConflict) {
this.filesWithConflict = filesWithConflict;
@@ -41,4 +42,12 @@ public class MergeCommandResult {
public Collection<String> getFilesWithConflict() {
return unmodifiableCollection(filesWithConflict);
}
public boolean isAborted() {
return aborted;
}
public void setAborted(boolean aborted) {
this.aborted = aborted;
}
}

View File

@@ -3,7 +3,6 @@ package sonia.scm.repository.spi;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.MergeCommand.FastForwardMode;
import org.eclipse.jgit.api.MergeResult;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.ObjectId;
@@ -20,6 +19,7 @@ import sonia.scm.repository.api.MergeStrategy;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.Set;
public class GitMergeCommand extends AbstractGitCommand implements MergeCommand {
@@ -93,10 +93,17 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
@Override
MergeCommandResult run() throws IOException {
MergeResult result = doMergeInClone();
if (result.getMergeStatus().isSuccessful()) {
if (mergeStrategy != MergeStrategy.FAST_FORWARD_IF_POSSIBLE) {
doCommit();
}
push();
return MergeCommandResult.success();
} else if (mergeStrategy == MergeStrategy.FAST_FORWARD_IF_POSSIBLE) {
MergeCommandResult failure = MergeCommandResult.failure(Collections.emptyList());
failure.setAborted(true);
return failure;
} else {
return analyseFailure(result);
}
@@ -113,8 +120,10 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
if (mergeStrategy == MergeStrategy.SQUASH) {
mergeCommand.setSquash(true);
} else if (mergeStrategy == MergeStrategy.FAST_FORWARD_IF_POSSIBLE) {
mergeCommand.setFastForward(org.eclipse.jgit.api.MergeCommand.FastForwardMode.FF_ONLY);
} else {
mergeCommand.setFastForward(FastForwardMode.NO_FF);
mergeCommand.setFastForward(org.eclipse.jgit.api.MergeCommand.FastForwardMode.NO_FF);
}
result = mergeCommand.call();

View File

@@ -262,6 +262,31 @@ public class GitMergeCommandTest extends AbstractGitCommandTestBase {
assertThat(changes.size()).isEqualTo(3);
}
@Test
public void shouldMergeWithFastForward() throws IOException, GitAPIException {
Repository repository = createContext().open();
ObjectId featureBranchHead = new Git(repository).log().add(repository.resolve("squash")).setMaxCount(1).call().iterator().next().getId();
GitMergeCommand command = createCommand();
MergeCommandRequest request = new MergeCommandRequest();
request.setBranchToMerge("squash");
request.setTargetBranch("master");
request.setMergeStrategy(MergeStrategy.FAST_FORWARD_IF_POSSIBLE);
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
MergeCommandResult mergeCommandResult = command.merge(request);
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();
assertThat(mergeAuthor.getName()).isEqualTo("Philip J Fry");
assertThat(mergeCommit.getId()).isEqualTo(featureBranchHead);
}
@Test(expected = NotFoundException.class)
public void shouldHandleNotExistingSourceBranchInMerge() {
GitMergeCommand command = createCommand();