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);
}