+ if (path[0])
+ return fmtalloc("%.*s%s/", baselen, base, path);
+ else
+ return fmtalloc("%.*s/", baselen, base);
+}
+
+static void print_dir(const struct object_id *oid, const char *base,
+ int baselen, const char *path)
+{
+ char *fullpath, *slash;
+ size_t len;
+
+ fullpath = buildpath(base, baselen, path);
+ slash = (fullpath[0] == '/' ? "" : "/");
+ ctx.page.etag = oid_to_hex(oid);
+ cgit_print_http_headers();
+ htmlf("<html><head><title>%s", slash);
+ html_txt(fullpath);
+ htmlf("</title></head>\n<body>\n<h2>%s", slash);
+ html_txt(fullpath);
+ html("</h2>\n<ul>\n");
+ len = strlen(fullpath);
+ if (len > 1) {
+ fullpath[len - 1] = 0;
+ slash = strrchr(fullpath, '/');
+ if (slash)
+ *(slash + 1) = 0;
+ else {
+ free(fullpath);
+ fullpath = NULL;
+ }
+ html("<li>");
+ cgit_plain_link("../", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
+ fullpath);
+ html("</li>\n");
+ }
+ free(fullpath);
+}
+
+static void print_dir_entry(const struct object_id *oid, const char *base,
+ int baselen, const char *path, unsigned mode)
+{
+ char *fullpath;
+
+ fullpath = buildpath(base, baselen, path);
+ if (!S_ISDIR(mode) && !S_ISGITLINK(mode))
+ fullpath[strlen(fullpath) - 1] = 0;
+ html(" <li>");
+ if (S_ISGITLINK(mode)) {
+ cgit_submodule_link(NULL, fullpath, oid_to_hex(oid));
+ } else
+ cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
+ fullpath);
+ html("</li>\n");
+ free(fullpath);
+}
+
+static void print_dir_tail(void)
+{
+ html(" </ul>\n</body></html>\n");
+}
+
+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 (base->len == walk_tree_ctx->match_baselen) {
+ if (S_ISREG(mode) || S_ISLNK(mode)) {
+ if (print_object(oid, pathname))
+ walk_tree_ctx->match = 1;
+ } else if (S_ISDIR(mode)) {
+ print_dir(oid, base->buf, base->len, pathname);
+ walk_tree_ctx->match = 2;
+ return READ_TREE_RECURSIVE;
+ }
+ } else if (base->len < INT_MAX && (int)base->len > walk_tree_ctx->match_baselen) {
+ print_dir_entry(oid, base->buf, base->len, pathname, mode);
+ walk_tree_ctx->match = 2;
+ } else if (S_ISDIR(mode)) {