toRevision = MongoUtil.toMongoRepresentation(toRevisionId);
}
List<MongoCommit> commits = getCommits(fromRevision, toRevision);
MongoCommit toCommit = extractCommit(commits, toRevision);
MongoCommit fromCommit;
if (toRevision == fromRevision) {
fromCommit = toCommit;
} else {
fromCommit = extractCommit(commits, fromRevision);
}
if (fromCommit != null && fromCommit.getBranchId() != null) {
if (!fromCommit.getBranchId().equals(toCommit.getBranchId())) {
throw new MicroKernelException("Inconsistent range specified: fromRevision denotes a private branch while toRevision denotes a head or another private branch");
}
}
if (fromCommit != null && fromCommit.getTimestamp() > toCommit.getTimestamp()) {
// negative range, return empty journal
return "[]";
}
JsopBuilder commitBuff = new JsopBuilder().array();
// Iterate over commits in chronological order, starting with oldest commit
for (int i = commits.size() - 1; i >= 0; i--) {
MongoCommit commit = commits.get(i);
String diff = commit.getDiff();
if (MongoUtil.isFiltered(path)) {
try {
diff = new DiffBuilder(
MongoUtil.wrap(getNode("/", commit.getBaseRevisionId())),
MongoUtil.wrap(getNode("/", commit.getRevisionId())),
"/", -1, new SimpleMongoNodeStore(), path).build();
if (diff.isEmpty()) {
continue;
}
} catch (Exception e) {
throw new MicroKernelException(e);
}
}
commitBuff.object()
.key("id").value(MongoUtil.fromMongoRepresentation(commit.getRevisionId()))
.key("ts").value(commit.getTimestamp())
.key("msg").value(commit.getMessage());
if (commit.getBranchId() != null) {
commitBuff.key("branchRootId").value(commit.getBranchId().toString());
}
commitBuff.key("changes").value(diff).endObject();
}
return commitBuff.endArray().toString();
}