public void stage(final ProgressListener progress, final Iterator<DiffEntry> unstaged,
final long numChanges) {
int i = 0;
progress.started();
final RevTree currentIndexHead = getTree();
Map<String, RevTreeBuilder> parentTress = Maps.newHashMap();
Map<String, ObjectId> parentMetadataIds = Maps.newHashMap();
Set<String> removedTrees = Sets.newHashSet();
StagingDatabase database = getDatabase();
while (unstaged.hasNext()) {
final DiffEntry diff = unstaged.next();
final String fullPath = diff.oldPath() == null ? diff.newPath() : diff.oldPath();
final String parentPath = NodeRef.parentPath(fullPath);
/*
* TODO: revisit, ideally the list of diff entries would come with one single entry for
* the whole removed tree instead of that one and every single children of it.
*/
if (removedTrees.contains(parentPath)) {
continue;
}
if (null == parentPath) {
// it is the root tree that's been changed, update head and ignore anything else
ObjectId newRoot = diff.newObjectId();
updateStageHead(newRoot);
progress.setProgress(100f);
progress.complete();
return;
}
RevTreeBuilder parentTree = getParentTree(currentIndexHead, parentPath, parentTress,
parentMetadataIds);
i++;
progress.setProgress((float) (i * 100) / numChanges);
NodeRef oldObject = diff.getOldObject();
NodeRef newObject = diff.getNewObject();
if (newObject == null) {
// Delete
parentTree.remove(oldObject.name());
if (TYPE.TREE.equals(oldObject.getType())) {
removedTrees.add(oldObject.path());
}
} else if (oldObject == null) {
// Add
Node node = newObject.getNode();
parentTree.put(node);
parentMetadataIds.put(newObject.path(), newObject.getMetadataId());
} else {
// Modify
Node node = newObject.getNode();
parentTree.put(node);
}
database.removeConflict(null, fullPath);
}
ObjectId newRootTree = currentIndexHead.getId();
for (Map.Entry<String, RevTreeBuilder> entry : parentTress.entrySet()) {
String changedTreePath = entry.getKey();
RevTreeBuilder changedTreeBuilder = entry.getValue();
RevTree changedTree = changedTreeBuilder.build();
ObjectId parentMetadataId = parentMetadataIds.get(changedTreePath);
if (NodeRef.ROOT.equals(changedTreePath)) {
// root
database.put(changedTree);
newRootTree = changedTree.getId();
} else {
// parentMetadataId = parentMetadataId == null ?
Supplier<RevTreeBuilder> rootTreeSupplier = getTreeSupplier();
newRootTree = context.command(WriteBack.class).setAncestor(rootTreeSupplier)
.setChildPath(changedTreePath).setMetadataId(parentMetadataId)