]> git.cameronkatri.com Git - cgit.git/blobdiff - ui-tree.c
cmd: redirect empty about/ to homepage or summary
[cgit.git] / ui-tree.c
index 1b310d5304b2ebd8c44594e018bc3eb534a6e8f0..120066c425346f4b48d8061ed5f908da66138fc3 100644 (file)
--- a/ui-tree.c
+++ b/ui-tree.c
@@ -84,6 +84,37 @@ static void print_binary_buffer(char *buf, unsigned long size)
        html("</table>\n");
 }
 
+static void set_title_from_path(const char *path)
+{
+       size_t path_len, path_index, path_last_end;
+       char *new_title;
+
+       if (!path)
+               return;
+
+       path_len = strlen(path);
+       new_title = xmalloc(path_len + 3 + strlen(ctx.page.title) + 1);
+       new_title[0] = '\0';
+
+       for (path_index = path_len, path_last_end = path_len; path_index-- > 0;) {
+               if (path[path_index] == '/') {
+                       if (path_index == path_len - 1) {
+                               path_last_end = path_index - 1;
+                               continue;
+                       }
+                       strncat(new_title, &path[path_index + 1], path_last_end - path_index - 1);
+                       strcat(new_title, "\\");
+                       path_last_end = path_index;
+               }
+       }
+       if (path_last_end)
+               strncat(new_title, path, path_last_end);
+
+       strcat(new_title, " - ");
+       strcat(new_title, ctx.page.title);
+       ctx.page.title = new_title;
+}
+
 static void print_object(const unsigned char *sha1, char *path, const char *basename, const char *rev)
 {
        enum object_type type;
@@ -104,6 +135,8 @@ static void print_object(const unsigned char *sha1, char *path, const char *base
                return;
        }
 
+       set_title_from_path(path);
+
        cgit_print_layout_start();
        htmlf("blob: %s (", sha1_to_hex(sha1));
        cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
@@ -143,6 +176,7 @@ static int ls_item(const unsigned char *sha1, struct strbuf *base,
                        htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
                              name,
                              sha1_to_hex(sha1));
+                       free(name);
                        return 0;
                }
        }
@@ -234,6 +268,7 @@ static int walk_tree(const unsigned char *sha1, struct strbuf *base,
 
                if (S_ISDIR(mode)) {
                        walk_tree_ctx->state = 1;
+                       set_title_from_path(buffer);
                        ls_head();
                        return READ_TREE_RECURSIVE;
                } else {
@@ -286,7 +321,7 @@ void cgit_print_tree(const char *rev, char *path)
        walk_tree_ctx.curr_rev = xstrdup(rev);
 
        if (path == NULL) {
-               ls_tree(commit->tree->object.sha1, NULL, &walk_tree_ctx);
+               ls_tree(commit->tree->object.oid.hash, NULL, &walk_tree_ctx);
                goto cleanup;
        }