use of org.locationtech.geogig.api.plumbing.WriteTree in project GeoGig by boundlessgeo.
the class AbstractMappedRemoteRepo method fetchSparseCommit.
/**
* This function takes all of the changes introduced by the specified commit and filters them
* based on the repository filter. It then uses the filtered results to construct a new commit
* that is the descendant of commits that the original's parents are mapped to.
*
* @param commitId the commit id of the original, non-sparse commit
* @param allowEmpty allow the function to create an empty sparse commit
*/
private void fetchSparseCommit(ObjectId commitId, boolean allowEmpty) {
Optional<RevObject> object = getObject(commitId);
if (object.isPresent() && object.get().getType().equals(TYPE.COMMIT)) {
RevCommit commit = (RevCommit) object.get();
FilteredDiffIterator changes = getFilteredChanges(commit);
GraphDatabase graphDatabase = localRepository.graphDatabase();
ObjectDatabase objectDatabase = localRepository.objectDatabase();
graphDatabase.put(commit.getId(), commit.getParentIds());
RevTree rootTree = RevTree.EMPTY;
if (commit.getParentIds().size() > 0) {
// Map this commit to the last "sparse" commit in my ancestry
ObjectId mappedCommit = graphDatabase.getMapping(commit.getParentIds().get(0));
graphDatabase.map(commit.getId(), mappedCommit);
Optional<ObjectId> treeId = localRepository.command(ResolveTreeish.class).setTreeish(mappedCommit).call();
if (treeId.isPresent()) {
rootTree = localRepository.getTree(treeId.get());
}
} else {
graphDatabase.map(commit.getId(), ObjectId.NULL);
}
Iterator<DiffEntry> it = Iterators.filter(changes, new Predicate<DiffEntry>() {
@Override
public boolean apply(DiffEntry e) {
return true;
}
});
if (it.hasNext()) {
// Create new commit
WriteTree writeTree = localRepository.command(WriteTree.class).setOldRoot(Suppliers.ofInstance(rootTree)).setDiffSupplier(Suppliers.ofInstance((Iterator<DiffEntry>) it));
if (changes.isAutoIngesting()) {
// the iterator already ingests objects into the ObjectDatabase
writeTree.dontMoveObjects();
}
ObjectId newTreeId = writeTree.call();
CommitBuilder builder = new CommitBuilder(commit);
List<ObjectId> newParents = new LinkedList<ObjectId>();
for (ObjectId parentCommitId : commit.getParentIds()) {
newParents.add(graphDatabase.getMapping(parentCommitId));
}
builder.setParentIds(newParents);
builder.setTreeId(newTreeId);
RevCommit mapped = builder.build();
objectDatabase.put(mapped);
if (changes.wasFiltered()) {
graphDatabase.setProperty(mapped.getId(), GraphDatabase.SPARSE_FLAG, "true");
}
graphDatabase.map(mapped.getId(), commit.getId());
// Replace the old mapping with the new commit Id.
graphDatabase.map(commit.getId(), mapped.getId());
} else if (allowEmpty) {
CommitBuilder builder = new CommitBuilder(commit);
List<ObjectId> newParents = new LinkedList<ObjectId>();
for (ObjectId parentCommitId : commit.getParentIds()) {
newParents.add(graphDatabase.getMapping(parentCommitId));
}
builder.setParentIds(newParents);
builder.setTreeId(rootTree.getId());
builder.setMessage(PLACEHOLDER_COMMIT_MESSAGE);
RevCommit mapped = builder.build();
objectDatabase.put(mapped);
graphDatabase.setProperty(mapped.getId(), GraphDatabase.SPARSE_FLAG, "true");
graphDatabase.map(mapped.getId(), commit.getId());
// Replace the old mapping with the new commit Id.
graphDatabase.map(commit.getId(), mapped.getId());
} else {
// Mark the mapped commit as sparse, since it wont have these changes
graphDatabase.setProperty(graphDatabase.getMapping(commit.getId()), GraphDatabase.SPARSE_FLAG, "true");
}
}
}
Aggregations