ObjectId parentCommitId = ObjectId.NULL;
if (commit.getParentIds().size() > 0) {
parentCommitId = commit.getParentIds().get(0);
}
ObjectId parentTreeId = ObjectId.NULL;
Repository repository = repository();
if (repository.commitExists(parentCommitId)) {
parentTreeId = repository.getCommit(parentCommitId).getTreeId();
}
// get changes
Iterator<DiffEntry> diffs = command(DiffTree.class).setOldTree(parentTreeId)
.setNewTree(commit.getTreeId()).setReportTrees(true).call();
while (diffs.hasNext()) {
DiffEntry diff = diffs.next();
String path = diff.oldPath() == null ? diff.newPath() : diff.oldPath();
Optional<RevObject> obj = command(RevObjectParse.class).setRefSpec(
Ref.HEAD + ":" + path).call();
switch (diff.changeType()) {
case ADDED:
if (obj.isPresent()) {
TYPE type = command(ResolveObjectType.class).setObjectId(
diff.getNewObject().objectId()).call();
if (TYPE.TREE.equals(type)) {
NodeRef headVersion = command(FindTreeChild.class).setChildPath(path)
.setParent(repository.getOrCreateHeadTree()).call().get();
if (!headVersion.getMetadataId()
.equals(diff.getNewObject().getMetadataId())) {
report.addConflict(new Conflict(path, ObjectId.NULL, diff
.getNewObject().getMetadataId(), headVersion.getMetadataId()));
}
} else {
if (!obj.get().getId().equals(diff.newObjectId())) {
report.addConflict(new Conflict(path, ObjectId.NULL,
diff.newObjectId(), obj.get().getId()));
}
}
} else {
report.addUnconflicted(diff);
}
break;
case REMOVED:
if (obj.isPresent()) {
if (obj.get().getId().equals(diff.oldObjectId())) {
report.addUnconflicted(diff);
} else {
report.addConflict(new Conflict(path, diff.oldObjectId(), ObjectId.NULL,
obj.get().getId()));
}
}
break;
case MODIFIED:
TYPE type = command(ResolveObjectType.class).setObjectId(
diff.getNewObject().objectId()).call();
if (TYPE.TREE.equals(type)) {
// TODO:see how to do this. For now, we will pass any change as a conflicted
// one
if (!diff.isChange()) {
report.addUnconflicted(diff);
}
} else {
String refSpec = Ref.HEAD + ":" + path;
obj = command(RevObjectParse.class).setRefSpec(refSpec).call();
if (!obj.isPresent()) {
// git reports this as a conflict but does not mark as conflicted, just adds
// the missing file.
// We add it and consider it unconflicted
report.addUnconflicted(diff);
break;
}
RevFeature feature = (RevFeature) obj.get();
DepthSearch depthSearch = new DepthSearch(repository.objectDatabase());
Optional<NodeRef> noderef = depthSearch
.find(this.workingTree().getTree(), path);
RevFeatureType featureType = command(RevObjectParse.class)
.setObjectId(noderef.get().getMetadataId()).call(RevFeatureType.class)
.get();