use of org.eclipse.jgit.merge.Merger in project gerrit by GerritCodeReview.
the class CheckMergeability method apply.
@Override
public MergeableInfo apply(BranchResource resource) throws IOException, BadRequestException, ResourceNotFoundException {
if (!(submitType.equals(SubmitType.MERGE_ALWAYS) || submitType.equals(SubmitType.MERGE_IF_NECESSARY))) {
throw new BadRequestException("Submit type: " + submitType + " is not supported");
}
MergeableInfo result = new MergeableInfo();
result.submitType = submitType;
result.strategy = strategy;
try (Repository git = gitManager.openRepository(resource.getNameKey());
RevWalk rw = new RevWalk(git);
ObjectInserter inserter = new InMemoryInserter(git)) {
Merger m = MergeUtil.newMerger(inserter, git.getConfig(), strategy);
Ref destRef = git.getRefDatabase().exactRef(resource.getRef());
if (destRef == null) {
throw new ResourceNotFoundException(resource.getRef());
}
RevCommit targetCommit = rw.parseCommit(destRef.getObjectId());
RevCommit sourceCommit = MergeUtil.resolveCommit(git, rw, source);
if (!resource.getControl().canReadCommit(db.get(), git, sourceCommit)) {
throw new BadRequestException("do not have read permission for: " + source);
}
if (rw.isMergedInto(sourceCommit, targetCommit)) {
result.mergeable = true;
result.commitMerged = true;
result.contentMerged = true;
return result;
}
if (m.merge(false, targetCommit, sourceCommit)) {
result.mergeable = true;
result.commitMerged = false;
result.contentMerged = m.getResultTreeId().equals(targetCommit.getTree());
} else {
result.mergeable = false;
if (m instanceof ResolveMerger) {
result.conflicts = ((ResolveMerger) m).getUnmergedPaths();
}
}
} catch (IllegalArgumentException e) {
throw new BadRequestException(e.getMessage());
}
return result;
}
use of org.eclipse.jgit.merge.Merger in project gerrit by GerritCodeReview.
the class MergeUtil method createMergeCommit.
public static RevCommit createMergeCommit(ObjectInserter inserter, Config repoConfig, RevCommit mergeTip, RevCommit originalCommit, String mergeStrategy, PersonIdent committerIndent, String commitMsg, RevWalk rw) throws IOException, MergeIdenticalTreeException, MergeConflictException {
if (!MergeStrategy.THEIRS.getName().equals(mergeStrategy) && rw.isMergedInto(originalCommit, mergeTip)) {
throw new ChangeAlreadyMergedException("'" + originalCommit.getName() + "' has already been merged");
}
Merger m = newMerger(inserter, repoConfig, mergeStrategy);
if (m.merge(false, mergeTip, originalCommit)) {
ObjectId tree = m.getResultTreeId();
CommitBuilder mergeCommit = new CommitBuilder();
mergeCommit.setTreeId(tree);
mergeCommit.setParentIds(mergeTip, originalCommit);
mergeCommit.setAuthor(committerIndent);
mergeCommit.setCommitter(committerIndent);
mergeCommit.setMessage(commitMsg);
return rw.parseCommit(inserter.insert(mergeCommit));
}
List<String> conflicts = ImmutableList.of();
if (m instanceof ResolveMerger) {
conflicts = ((ResolveMerger) m).getUnmergedPaths();
}
throw new MergeConflictException(createConflictMessage(conflicts));
}
use of org.eclipse.jgit.merge.Merger in project gerrit by GerritCodeReview.
the class MergeUtil method newThreeWayMerger.
public static ThreeWayMerger newThreeWayMerger(ObjectInserter inserter, Config repoConfig, String strategyName) {
Merger m = newMerger(inserter, repoConfig, strategyName);
checkArgument(m instanceof ThreeWayMerger, "merge strategy %s does not support three-way merging", strategyName);
return (ThreeWayMerger) m;
}
Aggregations