Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 1 | /* ui-tree.c: functions for tree output |
| 2 | * |
| 3 | * Copyright (C) 2006 Lars Hjemli |
| 4 | * |
| 5 | * Licensed under GNU General Public License v2 |
| 6 | * (see COPYING for full license text) |
| 7 | */ |
| 8 | |
| 9 | #include "cgit.h" |
| 10 | |
Lars Hjemli | 9fb53af | 2007-05-14 11:10:59 +0200 | [diff] [blame] | 11 | char *curr_rev; |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 12 | char *match_path; |
| 13 | int header = 0; |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 14 | |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 15 | static void print_object(const unsigned char *sha1, char *path) |
| 16 | { |
| 17 | enum object_type type; |
Lars Hjemli | 0835ffe | 2007-09-20 00:21:47 +0200 | [diff] [blame] | 18 | char *buf; |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 19 | unsigned long size, lineno, start, idx; |
Lars Hjemli | 2915483 | 2007-11-11 13:04:28 +0100 | [diff] [blame^] | 20 | const char *linefmt = "<tr><td class='no'><a id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a></td><td class='txt'>"; |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 21 | |
| 22 | type = sha1_object_info(sha1, &size); |
| 23 | if (type == OBJ_BAD) { |
| 24 | cgit_print_error(fmt("Bad object name: %s", |
| 25 | sha1_to_hex(sha1))); |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | buf = read_sha1_file(sha1, &type, &size); |
| 30 | if (!buf) { |
| 31 | cgit_print_error(fmt("Error reading object %s", |
| 32 | sha1_to_hex(sha1))); |
| 33 | return; |
| 34 | } |
| 35 | |
Michael Krelin | d6b01da | 2007-07-21 19:51:47 +0200 | [diff] [blame] | 36 | html(" blob: <a href='"); |
| 37 | html_attr(cgit_pageurl(cgit_query_repo, "blob", fmt("id=%s", sha1_to_hex(sha1)))); |
| 38 | htmlf("'>%s</a>",sha1_to_hex(sha1)); |
| 39 | |
Lars Hjemli | 2915483 | 2007-11-11 13:04:28 +0100 | [diff] [blame^] | 40 | html("<table summary='blob content' class='blob'>\n"); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 41 | idx = 0; |
| 42 | start = 0; |
| 43 | lineno = 0; |
| 44 | while(idx < size) { |
| 45 | if (buf[idx] == '\n') { |
| 46 | buf[idx] = '\0'; |
Lars Hjemli | ff7a394 | 2007-09-20 00:00:06 +0200 | [diff] [blame] | 47 | htmlf(linefmt, ++lineno); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 48 | html_txt(buf + start); |
| 49 | html("</td></tr>\n"); |
| 50 | start = idx + 1; |
| 51 | } |
| 52 | idx++; |
| 53 | } |
Lars Hjemli | ff7a394 | 2007-09-20 00:00:06 +0200 | [diff] [blame] | 54 | htmlf(linefmt, ++lineno); |
| 55 | html_txt(buf + start); |
| 56 | html("</td></tr>\n"); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 57 | html("</table>\n"); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | static int ls_item(const unsigned char *sha1, const char *base, int baselen, |
| 62 | const char *pathname, unsigned int mode, int stage) |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 63 | { |
| 64 | char *name; |
Lars Hjemli | 44947bf | 2007-06-17 01:23:08 +0200 | [diff] [blame] | 65 | char *fullpath; |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame] | 66 | enum object_type type; |
Lars Hjemli | ded9393 | 2007-05-11 12:12:48 +0200 | [diff] [blame] | 67 | unsigned long size = 0; |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 68 | |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame] | 69 | name = xstrdup(pathname); |
Lars Hjemli | 44947bf | 2007-06-17 01:23:08 +0200 | [diff] [blame] | 70 | fullpath = fmt("%s%s%s", cgit_query_path ? cgit_query_path : "", |
| 71 | cgit_query_path ? "/" : "", name); |
| 72 | |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame] | 73 | type = sha1_object_info(sha1, &size); |
Jeffrey C. Ollie | e651cb0 | 2007-06-04 12:28:56 -0500 | [diff] [blame] | 74 | if (type == OBJ_BAD && !S_ISGITLINK(mode)) { |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame] | 75 | htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>", |
| 76 | name, |
| 77 | sha1_to_hex(sha1)); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 78 | return 0; |
| 79 | } |
Lars Hjemli | 44947bf | 2007-06-17 01:23:08 +0200 | [diff] [blame] | 80 | |
Lars Hjemli | 426032f | 2007-06-17 13:17:00 +0200 | [diff] [blame] | 81 | html("<tr><td class='ls-mode'>"); |
Lars Hjemli | a530428 | 2006-12-17 23:55:53 +0100 | [diff] [blame] | 82 | html_filemode(mode); |
Lars Hjemli | 426032f | 2007-06-17 13:17:00 +0200 | [diff] [blame] | 83 | html("</td><td>"); |
Jeffrey C. Ollie | e651cb0 | 2007-06-04 12:28:56 -0500 | [diff] [blame] | 84 | if (S_ISGITLINK(mode)) { |
Lars Hjemli | 426032f | 2007-06-17 13:17:00 +0200 | [diff] [blame] | 85 | htmlf("<a class='ls-mod' href='"); |
Lars Hjemli | ded9393 | 2007-05-11 12:12:48 +0200 | [diff] [blame] | 86 | html_attr(fmt(cgit_repo->module_link, |
| 87 | name, |
| 88 | sha1_to_hex(sha1))); |
Lars Hjemli | 44947bf | 2007-06-17 01:23:08 +0200 | [diff] [blame] | 89 | html("'>"); |
| 90 | html_txt(name); |
| 91 | html("</a>"); |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame] | 92 | } else if (S_ISDIR(mode)) { |
Lars Hjemli | 426032f | 2007-06-17 13:17:00 +0200 | [diff] [blame] | 93 | cgit_tree_link(name, NULL, "ls-dir", cgit_query_head, |
Lars Hjemli | 44947bf | 2007-06-17 01:23:08 +0200 | [diff] [blame] | 94 | curr_rev, fullpath); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 95 | } else { |
Lars Hjemli | 426032f | 2007-06-17 13:17:00 +0200 | [diff] [blame] | 96 | cgit_tree_link(name, NULL, "ls-blob", cgit_query_head, |
Lars Hjemli | 44947bf | 2007-06-17 01:23:08 +0200 | [diff] [blame] | 97 | curr_rev, fullpath); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 98 | } |
Lars Hjemli | 426032f | 2007-06-17 13:17:00 +0200 | [diff] [blame] | 99 | htmlf("</td><td class='ls-size'>%li</td>", size); |
Lars Hjemli | 9fb53af | 2007-05-14 11:10:59 +0200 | [diff] [blame] | 100 | |
Lars Hjemli | 48c487d | 2007-06-17 13:57:51 +0200 | [diff] [blame] | 101 | html("<td>"); |
Lars Hjemli | a215bf4 | 2007-06-18 22:12:09 +0200 | [diff] [blame] | 102 | cgit_log_link("log", NULL, "button", cgit_query_head, curr_rev, |
Lars Hjemli | 5114031 | 2007-11-03 10:42:37 +0100 | [diff] [blame] | 103 | fullpath, 0, NULL, NULL); |
Lars Hjemli | 48c487d | 2007-06-17 13:57:51 +0200 | [diff] [blame] | 104 | html("</td></tr>\n"); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 105 | free(name); |
| 106 | return 0; |
| 107 | } |
| 108 | |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 109 | static void ls_head() |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 110 | { |
Lars Hjemli | 2915483 | 2007-11-11 13:04:28 +0100 | [diff] [blame^] | 111 | html("<table summary='tree listing' class='list'>\n"); |
Lars Hjemli | 777faf7 | 2007-01-28 00:39:26 +0100 | [diff] [blame] | 112 | html("<tr class='nohover'>"); |
Lars Hjemli | a530428 | 2006-12-17 23:55:53 +0100 | [diff] [blame] | 113 | html("<th class='left'>Mode</th>"); |
| 114 | html("<th class='left'>Name</th>"); |
Lars Hjemli | 9a8f886 | 2006-12-16 00:19:56 +0100 | [diff] [blame] | 115 | html("<th class='right'>Size</th>"); |
Lars Hjemli | 9fb53af | 2007-05-14 11:10:59 +0200 | [diff] [blame] | 116 | html("<th/>"); |
Lars Hjemli | a530428 | 2006-12-17 23:55:53 +0100 | [diff] [blame] | 117 | html("</tr>\n"); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 118 | header = 1; |
| 119 | } |
| 120 | |
| 121 | static void ls_tail() |
| 122 | { |
| 123 | if (!header) |
| 124 | return; |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 125 | html("</table>\n"); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 126 | header = 0; |
| 127 | } |
| 128 | |
| 129 | static void ls_tree(const unsigned char *sha1, char *path) |
| 130 | { |
| 131 | struct tree *tree; |
| 132 | |
| 133 | tree = parse_tree_indirect(sha1); |
| 134 | if (!tree) { |
| 135 | cgit_print_error(fmt("Not a tree object: %s", |
| 136 | sha1_to_hex(sha1))); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | ls_head(); |
| 141 | read_tree_recursive(tree, "", 0, 1, NULL, ls_item); |
| 142 | ls_tail(); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | static int walk_tree(const unsigned char *sha1, const char *base, int baselen, |
| 147 | const char *pathname, unsigned mode, int stage) |
| 148 | { |
| 149 | static int state; |
| 150 | static char buffer[PATH_MAX]; |
| 151 | char *url; |
| 152 | |
| 153 | if (state == 0) { |
| 154 | memcpy(buffer, base, baselen); |
| 155 | strcpy(buffer+baselen, pathname); |
| 156 | url = cgit_pageurl(cgit_query_repo, "tree", |
| 157 | fmt("h=%s&path=%s", curr_rev, buffer)); |
Lars Hjemli | 44947bf | 2007-06-17 01:23:08 +0200 | [diff] [blame] | 158 | html("/"); |
| 159 | cgit_tree_link(xstrdup(pathname), NULL, NULL, cgit_query_head, |
| 160 | curr_rev, buffer); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 161 | |
| 162 | if (strcmp(match_path, buffer)) |
| 163 | return READ_TREE_RECURSIVE; |
| 164 | |
| 165 | if (S_ISDIR(mode)) { |
| 166 | state = 1; |
| 167 | ls_head(); |
| 168 | return READ_TREE_RECURSIVE; |
| 169 | } else { |
| 170 | print_object(sha1, buffer); |
| 171 | return 0; |
| 172 | } |
| 173 | } |
| 174 | ls_item(sha1, base, baselen, pathname, mode, stage); |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | /* |
| 180 | * Show a tree or a blob |
| 181 | * rev: the commit pointing at the root tree object |
| 182 | * path: path to tree or blob |
| 183 | */ |
| 184 | void cgit_print_tree(const char *rev, char *path) |
| 185 | { |
| 186 | unsigned char sha1[20]; |
| 187 | struct commit *commit; |
| 188 | const char *paths[] = {path, NULL}; |
| 189 | |
| 190 | if (!rev) |
| 191 | rev = cgit_query_head; |
| 192 | |
| 193 | curr_rev = xstrdup(rev); |
| 194 | if (get_sha1(rev, sha1)) { |
| 195 | cgit_print_error(fmt("Invalid revision name: %s", rev)); |
| 196 | return; |
| 197 | } |
| 198 | commit = lookup_commit_reference(sha1); |
| 199 | if (!commit || parse_commit(commit)) { |
| 200 | cgit_print_error(fmt("Invalid commit reference: %s", rev)); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | html("path: <a href='"); |
| 205 | html_attr(cgit_pageurl(cgit_query_repo, "tree", fmt("h=%s", rev))); |
| 206 | html("'>root</a>"); |
| 207 | |
| 208 | if (path == NULL) { |
| 209 | ls_tree(commit->tree->object.sha1, NULL); |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | match_path = path; |
| 214 | read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree); |
| 215 | ls_tail(); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 216 | } |