+ cgit_print_layout_end();
+}
+
+static void ls_tree(const struct object_id *oid, const char *path, struct walk_tree_context *walk_tree_ctx)
+{
+ struct tree *tree;
+ struct pathspec paths = {
+ .nr = 0
+ };
+
+ tree = parse_tree_indirect(oid);
+ if (!tree) {
+ cgit_print_error_page(404, "Not found",
+ "Not a tree object: %s", oid_to_hex(oid));
+ return;
+ }
+
+ ls_head();
+ read_tree(the_repository, tree, &paths, ls_item, walk_tree_ctx);
+ ls_tail();
+}
+
+
+static int walk_tree(const struct object_id *oid, struct strbuf *base,
+ const char *pathname, unsigned mode, void *cbdata)
+{
+ struct walk_tree_context *walk_tree_ctx = cbdata;
+
+ if (walk_tree_ctx->state == 0) {
+ struct strbuf buffer = STRBUF_INIT;
+
+ strbuf_addbuf(&buffer, base);
+ strbuf_addstr(&buffer, pathname);
+ if (strcmp(walk_tree_ctx->match_path, buffer.buf))
+ return READ_TREE_RECURSIVE;
+
+ if (S_ISDIR(mode)) {
+ walk_tree_ctx->state = 1;
+ cgit_set_title_from_path(buffer.buf);
+ strbuf_release(&buffer);
+ ls_head();
+ return READ_TREE_RECURSIVE;
+ } else {
+ walk_tree_ctx->state = 2;
+ print_object(oid, buffer.buf, pathname, walk_tree_ctx->curr_rev);
+ strbuf_release(&buffer);
+ return 0;
+ }
+ }
+ ls_item(oid, base, pathname, mode, walk_tree_ctx);
+ return 0;
+}
+
+/*
+ * Show a tree or a blob
+ * rev: the commit pointing at the root tree object
+ * path: path to tree or blob
+ */
+void cgit_print_tree(const char *rev, char *path)
+{
+ struct object_id oid;
+ struct commit *commit;
+ struct pathspec_item path_items = {
+ .match = path,
+ .len = path ? strlen(path) : 0
+ };
+ struct pathspec paths = {
+ .nr = path ? 1 : 0,
+ .items = &path_items
+ };
+ struct walk_tree_context walk_tree_ctx = {
+ .match_path = path,
+ .state = 0
+ };
+
+ if (!rev)
+ rev = ctx.qry.head;
+
+ if (get_oid(rev, &oid)) {
+ cgit_print_error_page(404, "Not found",
+ "Invalid revision name: %s", rev);
+ return;
+ }
+ commit = lookup_commit_reference(the_repository, &oid);
+ if (!commit || parse_commit(commit)) {
+ cgit_print_error_page(404, "Not found",
+ "Invalid commit reference: %s", rev);
+ return;
+ }
+
+ walk_tree_ctx.curr_rev = xstrdup(rev);
+
+ if (path == NULL) {
+ ls_tree(get_commit_tree_oid(commit), NULL, &walk_tree_ctx);
+ goto cleanup;
+ }
+
+ read_tree(the_repository, repo_get_commit_tree(the_repository, commit),
+ &paths, walk_tree, &walk_tree_ctx);
+ if (walk_tree_ctx.state == 1)
+ ls_tail();
+ else if (walk_tree_ctx.state == 2)
+ cgit_print_layout_end();
+ else
+ cgit_print_error_page(404, "Not found", "Path not found");
+
+cleanup:
+ free(walk_tree_ctx.curr_rev);