+struct walk_tree_context {
+ const char *match_path;
+ struct object_id *matched_oid;
+ unsigned int found_path:1;
+ unsigned int file_only:1;
+};
+
+static int walk_tree(const struct object_id *oid, struct strbuf *base,
+ const char *pathname, unsigned mode, int stage, void *cbdata)
+{
+ struct walk_tree_context *walk_tree_ctx = cbdata;
+
+ if (walk_tree_ctx->file_only && !S_ISREG(mode))
+ return READ_TREE_RECURSIVE;
+ if (strncmp(base->buf, walk_tree_ctx->match_path, base->len)
+ || strcmp(walk_tree_ctx->match_path + base->len, pathname))
+ return READ_TREE_RECURSIVE;
+ oidcpy(walk_tree_ctx->matched_oid, oid);
+ walk_tree_ctx->found_path = 1;
+ return 0;
+}
+
+int cgit_ref_path_exists(const char *path, const char *ref, int file_only)
+{
+ struct object_id oid;
+ unsigned long size;
+ struct pathspec_item path_items = {
+ .match = xstrdup(path),
+ .len = strlen(path)
+ };
+ struct pathspec paths = {
+ .nr = 1,
+ .items = &path_items
+ };
+ struct walk_tree_context walk_tree_ctx = {
+ .match_path = path,
+ .matched_oid = &oid,
+ .found_path = 0,
+ .file_only = file_only
+ };
+
+ if (get_oid(ref, &oid))
+ goto done;
+ if (oid_object_info(the_repository, &oid, &size) != OBJ_COMMIT)
+ goto done;
+ read_tree_recursive(the_repository, lookup_commit_reference(the_repository, &oid)->maybe_tree,
+ "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
+
+done:
+ free(path_items.match);
+ return walk_tree_ctx.found_path;
+}
+
+int cgit_print_file(char *path, const char *head, int file_only)