Create local branch for target during merge

This commit is contained in:
René Pfeuffer
2018-12-10 11:43:27 +01:00
parent 8b518d320d
commit 0e333002db
2 changed files with 45 additions and 7 deletions

View File

@@ -97,16 +97,30 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
private void checkOutTargetBranch() throws IOException {
try {
ObjectId targetRevision = resolveRevision(target);
clone.checkout().setName(targetRevision.getName()).call();
clone.checkout().setName(target).call();
} catch (RefNotFoundException e) {
logger.debug("could not checkout target branch {} for merge", target, e);
throw notFound(entity("revision", target).in(context.getRepository()));
logger.trace("could not checkout target branch {} for merge directly; trying to create local branch", target, e);
checkOutTargetAsNewLocalBranch();
} catch (GitAPIException e) {
throw new InternalRepositoryException(context.getRepository(), "could not checkout target branch for merge: " + target, e);
}
}
private void checkOutTargetAsNewLocalBranch() throws IOException {
try {
ObjectId targetRevision = resolveRevision(target);
if (targetRevision == null) {
throw notFound(entity("revision", target).in(context.getRepository()));
}
clone.checkout().setStartPoint(targetRevision.getName()).setName(target).setCreateBranch(true).call();
} catch (RefNotFoundException e) {
logger.debug("could not checkout target branch {} for merge as local branch", target, e);
throw notFound(entity("revision", target).in(context.getRepository()));
} catch (GitAPIException e) {
throw new InternalRepositoryException(context.getRepository(), "could not checkout target branch for merge as local branch: " + target, e);
}
}
private MergeResult doMergeInClone() throws IOException {
MergeResult result;
try {
@@ -164,7 +178,7 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
try {
clone.push().call();
} catch (GitAPIException e) {
throw new InternalRepositoryException(context.getRepository(), "could not push merged branch " + toMerge + " to origin", e);
throw new InternalRepositoryException(context.getRepository(), "could not push merged branch " + target + " to origin", e);
}
logger.debug("pushed merged branch {}", target);
}

View File

@@ -126,8 +126,6 @@ public class GitMergeCommandTest extends AbstractGitCommandTestBase {
Repository repository = createContext().open();
ObjectId firstMergeCommit = new Git(repository).log().add(repository.resolve("master")).setMaxCount(1).call().iterator().next().getId();
// Transport.register(new ScmTransportProtocol(of(hookEventFacade), of(gitRepositoryHandler)));
MergeCommandResult secondMergeCommandResult = command.merge(request);
assertThat(secondMergeCommandResult.isSuccess()).isTrue();
@@ -196,6 +194,32 @@ public class GitMergeCommandTest extends AbstractGitCommandTestBase {
assertThat(mergeAuthor.getEmailAddress()).isEqualTo("dirk@holistic.det");
}
@Test
public void shouldMergeIntoNotDefaultBranch() throws IOException, GitAPIException {
GitMergeCommand command = createCommand();
MergeCommandRequest request = new MergeCommandRequest();
request.setAuthor(new Person("Dirk Gently", "dirk@holistic.det"));
request.setTargetBranch("mergeable");
request.setBranchToMerge("master");
MergeCommandResult mergeCommandResult = command.merge(request);
Repository repository = createContext().open();
assertThat(mergeCommandResult.isSuccess()).isTrue();
Iterable<RevCommit> commits = new Git(repository).log().add(repository.resolve("mergeable")).setMaxCount(1).call();
RevCommit mergeCommit = commits.iterator().next();
PersonIdent mergeAuthor = mergeCommit.getAuthorIdent();
String message = mergeCommit.getFullMessage();
assertThat(mergeAuthor.getName()).isEqualTo("Dirk Gently");
assertThat(mergeAuthor.getEmailAddress()).isEqualTo("dirk@holistic.det");
assertThat(message).contains("master", "mergeable");
// We expect the merge result of file b.txt here by looking up the sha hash of its content.
// If the file is missing (aka not merged correctly) this will throw a MissingObjectException:
byte[] contentOfFileB = repository.open(repository.resolve("9513e9c76e73f3e562fd8e4c909d0607113c77c6")).getBytes();
assertThat(new String(contentOfFileB)).isEqualTo("b\ncontent from branch\n");
}
private GitMergeCommand createCommand() {
return new GitMergeCommand(createContext(), repository, new SimpleGitWorkdirFactory());
}