Id fromRevisionId = Id.fromString(fromRevision);
Id toRevisionId = toRevision == null ? getHeadRevisionId() : Id.fromString(toRevision);
List<StoredCommit> commits = new ArrayList<StoredCommit>();
try {
StoredCommit toCommit = rep.getCommit(toRevisionId);
if (toCommit.getBranchRootId() != null) {
throw new MicroKernelException("branch revisions are not supported: " + toRevisionId);
}
Commit fromCommit;
if (toRevisionId.equals(fromRevisionId)) {
fromCommit = toCommit;
} else {
fromCommit = rep.getCommit(fromRevisionId);
if (fromCommit.getCommitTS() > toCommit.getCommitTS()) {
// negative range, return empty journal
return "[]";
}
}
if (fromCommit.getBranchRootId() != null) {
throw new MicroKernelException("branch revisions are not supported: " + fromRevisionId);
}
// collect commits, starting with toRevisionId
// and traversing parent commit links until we've reached
// fromRevisionId
StoredCommit commit = toCommit;
while (commit != null) {
commits.add(commit);
if (commit.getId().equals(fromRevisionId)) {
break;
}
Id commitId = commit.getParentId();
if (commitId == null) {
// inconsistent revision history, ignore silently...
break;
}
commit = rep.getCommit(commitId);
if (commit.getCommitTS() < fromCommit.getCommitTS()) {
// inconsistent revision history, ignore silently...
break;
}
}
} catch (Exception e) {
throw new MicroKernelException(e);
}
JsopBuilder commitBuff = new JsopBuilder().array();
// iterate over commits in chronological order,
// starting with oldest commit
for (int i = commits.size() - 1; i >= 0; i--) {
StoredCommit commit = commits.get(i);
if (commit.getParentId() == null) {
continue;
}
String diff = commit.getChanges();
if (filtered) {
try {
diff = new DiffBuilder(
rep.getNodeState(commit.getParentId(), "/"),
rep.getNodeState(commit.getId(), "/"),
"/", -1, rep.getRevisionStore(), path).build();
if (diff.isEmpty()) {
continue;
}
} catch (Exception e) {
throw new MicroKernelException(e);
}
}
commitBuff.object().
key("id").value(commit.getId().toString()).
key("ts").value(commit.getCommitTS()).
key("msg").value(commit.getMsg()).
key("changes").value(diff).endObject();
}
return commitBuff.endArray().toString();
}