mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 23:45:44 +01:00
implement fast forward if possible
This commit is contained in:
@@ -13,6 +13,7 @@ import static java.util.Collections.unmodifiableCollection;
|
|||||||
*/
|
*/
|
||||||
public class MergeCommandResult {
|
public class MergeCommandResult {
|
||||||
private final Collection<String> filesWithConflict;
|
private final Collection<String> filesWithConflict;
|
||||||
|
private boolean aborted = false;
|
||||||
|
|
||||||
private MergeCommandResult(Collection<String> filesWithConflict) {
|
private MergeCommandResult(Collection<String> filesWithConflict) {
|
||||||
this.filesWithConflict = filesWithConflict;
|
this.filesWithConflict = filesWithConflict;
|
||||||
@@ -41,4 +42,12 @@ public class MergeCommandResult {
|
|||||||
public Collection<String> getFilesWithConflict() {
|
public Collection<String> getFilesWithConflict() {
|
||||||
return unmodifiableCollection(filesWithConflict);
|
return unmodifiableCollection(filesWithConflict);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isAborted() {
|
||||||
|
return aborted;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAborted(boolean aborted) {
|
||||||
|
this.aborted = aborted;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package sonia.scm.repository.spi;
|
|||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import org.eclipse.jgit.api.Git;
|
import org.eclipse.jgit.api.Git;
|
||||||
import org.eclipse.jgit.api.MergeCommand.FastForwardMode;
|
|
||||||
import org.eclipse.jgit.api.MergeResult;
|
import org.eclipse.jgit.api.MergeResult;
|
||||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||||
import org.eclipse.jgit.lib.ObjectId;
|
import org.eclipse.jgit.lib.ObjectId;
|
||||||
@@ -20,6 +19,7 @@ import sonia.scm.repository.api.MergeStrategy;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class GitMergeCommand extends AbstractGitCommand implements MergeCommand {
|
public class GitMergeCommand extends AbstractGitCommand implements MergeCommand {
|
||||||
@@ -93,10 +93,17 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
|
|||||||
@Override
|
@Override
|
||||||
MergeCommandResult run() throws IOException {
|
MergeCommandResult run() throws IOException {
|
||||||
MergeResult result = doMergeInClone();
|
MergeResult result = doMergeInClone();
|
||||||
|
|
||||||
if (result.getMergeStatus().isSuccessful()) {
|
if (result.getMergeStatus().isSuccessful()) {
|
||||||
|
if (mergeStrategy != MergeStrategy.FAST_FORWARD_IF_POSSIBLE) {
|
||||||
doCommit();
|
doCommit();
|
||||||
|
}
|
||||||
push();
|
push();
|
||||||
return MergeCommandResult.success();
|
return MergeCommandResult.success();
|
||||||
|
} else if (mergeStrategy == MergeStrategy.FAST_FORWARD_IF_POSSIBLE) {
|
||||||
|
MergeCommandResult failure = MergeCommandResult.failure(Collections.emptyList());
|
||||||
|
failure.setAborted(true);
|
||||||
|
return failure;
|
||||||
} else {
|
} else {
|
||||||
return analyseFailure(result);
|
return analyseFailure(result);
|
||||||
}
|
}
|
||||||
@@ -113,8 +120,10 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
|
|||||||
|
|
||||||
if (mergeStrategy == MergeStrategy.SQUASH) {
|
if (mergeStrategy == MergeStrategy.SQUASH) {
|
||||||
mergeCommand.setSquash(true);
|
mergeCommand.setSquash(true);
|
||||||
|
} else if (mergeStrategy == MergeStrategy.FAST_FORWARD_IF_POSSIBLE) {
|
||||||
|
mergeCommand.setFastForward(org.eclipse.jgit.api.MergeCommand.FastForwardMode.FF_ONLY);
|
||||||
} else {
|
} else {
|
||||||
mergeCommand.setFastForward(FastForwardMode.NO_FF);
|
mergeCommand.setFastForward(org.eclipse.jgit.api.MergeCommand.FastForwardMode.NO_FF);
|
||||||
}
|
}
|
||||||
|
|
||||||
result = mergeCommand.call();
|
result = mergeCommand.call();
|
||||||
|
|||||||
@@ -262,6 +262,31 @@ public class GitMergeCommandTest extends AbstractGitCommandTestBase {
|
|||||||
assertThat(changes.size()).isEqualTo(3);
|
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)
|
@Test(expected = NotFoundException.class)
|
||||||
public void shouldHandleNotExistingSourceBranchInMerge() {
|
public void shouldHandleNotExistingSourceBranchInMerge() {
|
||||||
GitMergeCommand command = createCommand();
|
GitMergeCommand command = createCommand();
|
||||||
|
|||||||
Reference in New Issue
Block a user