}
RevWalk walk = new RevWalk(repo);
// add try catch to catch failures
Git git = new Git(repo);
RevCommit commit = walk.parseCommit(head.getObjectId());
RevTree tree = commit.getTree();
TreeWalk treeWalk = new TreeWalk(repo);
treeWalk.addTree(tree);
treeWalk.setRecursive(false);
if (!filePath.equals("")) {
PathFilter pathFilter = PathFilter.create(filePath);
treeWalk.setFilter(pathFilter);
}
if (!treeWalk.next()) {
CanonicalTreeParser canonicalTreeParser = treeWalk
.getTree(0, CanonicalTreeParser.class);
ArrayList<HashMap<String, Object>> contents = new ArrayList<HashMap<String, Object>>();
if (canonicalTreeParser != null) {
while (!canonicalTreeParser.eof()) {
String path = canonicalTreeParser
.getEntryPathString();
FileMode mode = canonicalTreeParser
.getEntryFileMode();
listEntry(path, mode.equals(FileMode.TREE) ? "dir"
: "file", "0", path, projectName, head.getName(), git, contents);
canonicalTreeParser.next();
}
}
String response = JSONUtil.write(contents);
resp.setContentType("application/json");
resp.setHeader("Cache-Control", "no-cache");
resp.setHeader("ETag", "\"" + tree.getId().getName() + "\"");
log.debug(response);
out.write(response);
} else {
// if (treeWalk.isSubtree()) {
// treeWalk.enterSubtree();
// }
ArrayList<HashMap<String, Object>> contents = new ArrayList<HashMap<String, Object>>();
do {
if (treeWalk.isSubtree()) {
String test = new String(treeWalk.getRawPath());
if (test.length() /*treeWalk.getPathLength()*/ > filePath
.length()) {
listEntry(treeWalk.getNameString(), "dir", "0", treeWalk.getPathString(), projectName, head.getName(), git, contents);
}
if (test.length() /*treeWalk.getPathLength()*/ <= filePath
.length()) {
treeWalk.enterSubtree();
}
} else {
ObjectId objId = treeWalk.getObjectId(0);
ObjectLoader loader = repo.open(objId);
long size = loader.getSize();
listEntry(treeWalk.getNameString(), "file", Long.toString(size), treeWalk.getPathString(), projectName, head.getName(), git, contents);
}
} while (treeWalk.next());
String response = JSONUtil.write(contents);
resp.setContentType("application/json");
resp.setHeader("Cache-Control", "no-cache");
resp.setHeader("ETag", "\"" + tree.getId().getName() + "\"");
log.debug(response);
out.write(response);
}
walk.release();
treeWalk.release();