X-Git-Url: https://git.cameronkatri.com/cgit.git/blobdiff_plain/210a5711ad8135de025b1a058079eead3d680a67..61ff10065b579fa38182fcf10cc7e63839acd53c:/ui-tree.c?ds=inline diff --git a/ui-tree.c b/ui-tree.c index 561f9e7..aa5dee9 100644 --- a/ui-tree.c +++ b/ui-tree.c @@ -8,6 +8,7 @@ #include #include "cgit.h" +#include "ui-tree.h" #include "html.h" #include "ui-shared.h" @@ -94,15 +95,13 @@ static void print_object(const unsigned char *sha1, char *path, const char *base type = sha1_object_info(sha1, &size); if (type == OBJ_BAD) { - cgit_print_error(fmt("Bad object name: %s", - sha1_to_hex(sha1))); + cgit_print_error("Bad object name: %s", sha1_to_hex(sha1)); return; } buf = read_sha1_file(sha1, &type, &size); if (!buf) { - cgit_print_error(fmt("Error reading object %s", - sha1_to_hex(sha1))); + cgit_print_error("Error reading object %s", sha1_to_hex(sha1)); return; } @@ -130,14 +129,14 @@ static int ls_item(const unsigned char *sha1, const char *base, int baselen, { struct walk_tree_context *walk_tree_ctx = cbdata; char *name; - char *fullpath; - char *class; + struct strbuf fullpath = STRBUF_INIT; + struct strbuf class = STRBUF_INIT; enum object_type type; unsigned long size = 0; name = xstrdup(pathname); - fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "", - ctx.qry.path ? "/" : "", name); + strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "", + ctx.qry.path ? "/" : "", name); if (!S_ISGITLINK(mode)) { type = sha1_object_info(sha1, &size); @@ -153,33 +152,34 @@ static int ls_item(const unsigned char *sha1, const char *base, int baselen, cgit_print_filemode(mode); html(""); if (S_ISGITLINK(mode)) { - cgit_submodule_link("ls-mod", fullpath, sha1_to_hex(sha1)); + cgit_submodule_link("ls-mod", fullpath.buf, sha1_to_hex(sha1)); } else if (S_ISDIR(mode)) { cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head, - walk_tree_ctx->curr_rev, fullpath); + walk_tree_ctx->curr_rev, fullpath.buf); } else { - class = strrchr(name, '.'); - if (class != NULL) { - class = fmt("ls-blob %s", class + 1); - } else - class = "ls-blob"; - cgit_tree_link(name, NULL, class, ctx.qry.head, - walk_tree_ctx->curr_rev, fullpath); + char *ext = strrchr(name, '.'); + strbuf_addstr(&class, "ls-blob"); + if (ext) + strbuf_addf(&class, " %s", ext + 1); + cgit_tree_link(name, NULL, class.buf, ctx.qry.head, + walk_tree_ctx->curr_rev, fullpath.buf); } htmlf("%li", size); html(""); cgit_log_link("log", NULL, "button", ctx.qry.head, - walk_tree_ctx->curr_rev, fullpath, 0, NULL, NULL, + walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL, ctx.qry.showmsg); if (ctx.repo->max_stats) cgit_stats_link("stats", NULL, "button", ctx.qry.head, - fullpath); + fullpath.buf); if (!S_ISGITLINK(mode)) cgit_plain_link("plain", NULL, "button", ctx.qry.head, - walk_tree_ctx->curr_rev, fullpath); + walk_tree_ctx->curr_rev, fullpath.buf); html("\n"); free(name); + strbuf_release(&fullpath); + strbuf_release(&class); return 0; } @@ -208,8 +208,7 @@ static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_cont tree = parse_tree_indirect(sha1); if (!tree) { - cgit_print_error(fmt("Not a tree object: %s", - sha1_to_hex(sha1))); + cgit_print_error("Not a tree object: %s", sha1_to_hex(sha1)); return; } @@ -271,23 +270,27 @@ void cgit_print_tree(const char *rev, char *path) if (!rev) rev = ctx.qry.head; - walk_tree_ctx.curr_rev = xstrdup(rev); if (get_sha1(rev, sha1)) { - cgit_print_error(fmt("Invalid revision name: %s", rev)); + cgit_print_error("Invalid revision name: %s", rev); return; } commit = lookup_commit_reference(sha1); if (!commit || parse_commit(commit)) { - cgit_print_error(fmt("Invalid commit reference: %s", rev)); + cgit_print_error("Invalid commit reference: %s", rev); return; } + walk_tree_ctx.curr_rev = xstrdup(rev); + if (path == NULL) { ls_tree(commit->tree->object.sha1, NULL, &walk_tree_ctx); - return; + goto cleanup; } read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx); if (walk_tree_ctx.state == 1) ls_tail(); + +cleanup: + free(walk_tree_ctx.curr_rev); }