switch (toMergeDiff.changeType()) {
case ADDED:
if (toMergeDiff.getNewObject().equals(mergeIntoDiff.getNewObject())) {
// already added in current branch, no need to do anything
} else {
TYPE type = command(ResolveObjectType.class).setObjectId(
toMergeDiff.getNewObject().objectId()).call();
if (TYPE.TREE.equals(type)) {
boolean conflict = !toMergeDiff.getNewObject().getMetadataId()
.equals(mergeIntoDiff.getNewObject().getMetadataId());
if (conflict) {
// In this case, we store the metadata id, not the element id
ancestorVersionId = ancestorVersion.isPresent() ? ancestorVersion
.get().getMetadataId() : ObjectId.NULL;
ours = mergeIntoDiff.getNewObject().getMetadataId();
theirs = toMergeDiff.getNewObject().getMetadataId();
report.addConflict(new Conflict(path, ancestorVersionId, ours,
theirs));
}
// if the metadata ids match, it means both branches have added the same
// tree, maybe with different content, but there is no need to do
// anything. The correct tree is already there and the merge can be run
// safely, so we do not add it neither as a conflicted change nor as an
// unconflicted one
} else {
report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
}
}
break;
case REMOVED:
// removed by both histories => no conflict and no need to do anything
break;
case MODIFIED:
TYPE type = command(ResolveObjectType.class).setObjectId(
toMergeDiff.getNewObject().objectId()).call();
if (TYPE.TREE.equals(type)) {
boolean conflict = !toMergeDiff.getNewObject().getMetadataId()
.equals(mergeIntoDiff.getNewObject().getMetadataId());
if (conflict) {
// In this case, we store the metadata id, not the element id
ancestorVersionId = ancestorVersion.isPresent() ? ancestorVersion.get()
.getMetadataId() : ObjectId.NULL;
ours = mergeIntoDiff.getNewObject().getMetadataId();
theirs = toMergeDiff.getNewObject().getMetadataId();
report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
}
} else {
FeatureDiff toMergeFeatureDiff = command(DiffFeature.class)
.setOldVersion(Suppliers.ofInstance(toMergeDiff.getOldObject()))
.setNewVersion(Suppliers.ofInstance(toMergeDiff.getNewObject()))
.call();
FeatureDiff mergeIntoFeatureDiff = command(DiffFeature.class)
.setOldVersion(Suppliers.ofInstance(mergeIntoDiff.getOldObject()))
.setNewVersion(Suppliers.ofInstance(mergeIntoDiff.getNewObject()))
.call();
if (toMergeFeatureDiff.conflicts(mergeIntoFeatureDiff)) {
report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
} else {
// if the feature types are different we report a conflict and do not
// try to perform automerge
if (!toMergeDiff.getNewObject().getMetadataId()
.equals(mergeIntoDiff.getNewObject().getMetadataId())) {
report.addConflict(new Conflict(path, ancestorVersionId, ours,
theirs));
} else if (!toMergeFeatureDiff.equals(mergeIntoFeatureDiff)) {
Feature mergedFeature = command(MergeFeaturesOp.class)
.setFirstFeature(mergeIntoDiff.getNewObject())
.setSecondFeature(toMergeDiff.getNewObject())
.setAncestorFeature(mergeIntoDiff.getOldObject()).call();
RevFeature revFeature = RevFeatureBuilder.build(mergedFeature);
if (revFeature.getId().equals(toMergeDiff.newObjectId())) {
// the resulting merged feature equals the feature to merge from
// the branch, which means that it exists in the repo and there
// is no need to add it
report.addUnconflicted(toMergeDiff);
} else {
RevFeatureType featureType = command(RevObjectParse.class)
.setObjectId(
mergeIntoDiff.getNewObject().getMetadataId())
.call(RevFeatureType.class).get();
FeatureInfo merged = new FeatureInfo(mergedFeature,
featureType, path);
report.addMerged(merged);
}
}
}
}
break;
}
} else {
// If the element is a tree, not a feature, it might be a conflict even if the other
// branch has not modified it.
// If we are removing the tree, we have to make sure that there are no features
// modified in the other branch under it.
if (ChangeType.REMOVED.equals(toMergeDiff.changeType())) {
TYPE type = command(ResolveObjectType.class).setObjectId(
toMergeDiff.oldObjectId()).call();
if (TYPE.TREE.equals(type)) {
String parentPath = toMergeDiff.oldPath();
Set<Entry<String, DiffEntry>> entries = mergeIntoDiffs.entrySet();
boolean conflict = false;