Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 1 | /* ui-tree.c: functions for tree output |
| 2 | * |
Jeff Smith | 1649afd | 2017-10-01 23:39:09 -0500 | [diff] [blame] | 3 | * Copyright (C) 2006-2017 cgit Development Team <cgit@lists.zx2c4.com> |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 4 | * |
| 5 | * Licensed under GNU General Public License v2 |
| 6 | * (see COPYING for full license text) |
| 7 | */ |
| 8 | |
| 9 | #include "cgit.h" |
John Keeping | 8f20879 | 2013-04-06 11:37:59 +0100 | [diff] [blame] | 10 | #include "ui-tree.h" |
Lars Hjemli | b1f9b9c | 2008-02-23 22:45:33 +0100 | [diff] [blame] | 11 | #include "html.h" |
Lars Hjemli | a4d1ca1 | 2008-03-24 16:50:57 +0100 | [diff] [blame] | 12 | #include "ui-shared.h" |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 13 | |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 14 | struct walk_tree_context { |
| 15 | char *curr_rev; |
| 16 | char *match_path; |
| 17 | int state; |
John Keeping | 8dced02 | 2018-06-18 14:23:45 +0800 | [diff] [blame^] | 18 | bool use_render; |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 19 | }; |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 20 | |
Lars Hjemli | 46b7abe | 2009-07-31 16:55:27 +0200 | [diff] [blame] | 21 | static void print_text_buffer(const char *name, char *buf, unsigned long size) |
Lars Hjemli | ae1d4d7 | 2009-01-31 17:45:48 +0100 | [diff] [blame] | 22 | { |
| 23 | unsigned long lineno, idx; |
Peter Wu | 4468ec1 | 2013-10-03 12:17:23 +0200 | [diff] [blame] | 24 | const char *numberfmt = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n"; |
Lars Hjemli | ae1d4d7 | 2009-01-31 17:45:48 +0100 | [diff] [blame] | 25 | |
| 26 | html("<table summary='blob content' class='blob'>\n"); |
Lars Hjemli | 46b7abe | 2009-07-31 16:55:27 +0200 | [diff] [blame] | 27 | |
Lars Hjemli | b0f946b | 2009-08-21 14:26:52 +0200 | [diff] [blame] | 28 | if (ctx.cfg.enable_tree_linenumbers) { |
Florian Pritz | d67cc7f | 2009-08-09 20:42:45 +0000 | [diff] [blame] | 29 | html("<tr><td class='linenumbers'><pre>"); |
| 30 | idx = 0; |
| 31 | lineno = 0; |
Lukas Fleischer | 53bc747 | 2013-03-03 16:04:29 +0100 | [diff] [blame] | 32 | |
Florian Pritz | d67cc7f | 2009-08-09 20:42:45 +0000 | [diff] [blame] | 33 | if (size) { |
| 34 | htmlf(numberfmt, ++lineno); |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 35 | while (idx < size - 1) { // skip absolute last newline |
Florian Pritz | d67cc7f | 2009-08-09 20:42:45 +0000 | [diff] [blame] | 36 | if (buf[idx] == '\n') |
| 37 | htmlf(numberfmt, ++lineno); |
| 38 | idx++; |
| 39 | } |
Eric Wong | 1129736 | 2009-03-14 18:41:47 -0700 | [diff] [blame] | 40 | } |
Florian Pritz | d67cc7f | 2009-08-09 20:42:45 +0000 | [diff] [blame] | 41 | html("</pre></td>\n"); |
Lars Hjemli | ae1d4d7 | 2009-01-31 17:45:48 +0100 | [diff] [blame] | 42 | } |
Florian Pritz | d67cc7f | 2009-08-09 20:42:45 +0000 | [diff] [blame] | 43 | else { |
| 44 | html("<tr>\n"); |
| 45 | } |
Florian Pritz | 03389d6 | 2009-08-09 13:43:18 +0000 | [diff] [blame] | 46 | |
| 47 | if (ctx.repo->source_filter) { |
John Keeping | 3d8a650 | 2014-01-12 17:13:50 +0000 | [diff] [blame] | 48 | char *filter_arg = xstrdup(name); |
Florian Pritz | 03389d6 | 2009-08-09 13:43:18 +0000 | [diff] [blame] | 49 | html("<td class='lines'><pre><code>"); |
John Keeping | 3d8a650 | 2014-01-12 17:13:50 +0000 | [diff] [blame] | 50 | cgit_open_filter(ctx.repo->source_filter, filter_arg); |
Mark Lodato | d187b98 | 2010-09-04 14:18:16 -0400 | [diff] [blame] | 51 | html_raw(buf, size); |
Florian Pritz | 03389d6 | 2009-08-09 13:43:18 +0000 | [diff] [blame] | 52 | cgit_close_filter(ctx.repo->source_filter); |
John Keeping | 3d8a650 | 2014-01-12 17:13:50 +0000 | [diff] [blame] | 53 | free(filter_arg); |
Florian Pritz | 03389d6 | 2009-08-09 13:43:18 +0000 | [diff] [blame] | 54 | html("</code></pre></td></tr></table>\n"); |
| 55 | return; |
| 56 | } |
| 57 | |
Lars Hjemli | ae1d4d7 | 2009-01-31 17:45:48 +0100 | [diff] [blame] | 58 | html("<td class='lines'><pre><code>"); |
| 59 | html_txt(buf); |
| 60 | html("</code></pre></td></tr></table>\n"); |
| 61 | } |
| 62 | |
Lars Hjemli | 6063e7b | 2009-02-12 11:26:14 +0100 | [diff] [blame] | 63 | #define ROWLEN 32 |
| 64 | |
Lars Hjemli | ae1d4d7 | 2009-01-31 17:45:48 +0100 | [diff] [blame] | 65 | static void print_binary_buffer(char *buf, unsigned long size) |
| 66 | { |
| 67 | unsigned long ofs, idx; |
Lars Hjemli | 6063e7b | 2009-02-12 11:26:14 +0100 | [diff] [blame] | 68 | static char ascii[ROWLEN + 1]; |
Lars Hjemli | ae1d4d7 | 2009-01-31 17:45:48 +0100 | [diff] [blame] | 69 | |
| 70 | html("<table summary='blob content' class='bin-blob'>\n"); |
| 71 | html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>"); |
Lars Hjemli | 6063e7b | 2009-02-12 11:26:14 +0100 | [diff] [blame] | 72 | for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) { |
Mark Lodato | e4ddc8f | 2010-09-04 11:30:18 -0400 | [diff] [blame] | 73 | htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs); |
Lars Hjemli | 6063e7b | 2009-02-12 11:26:14 +0100 | [diff] [blame] | 74 | for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++) |
Lars Hjemli | ae1d4d7 | 2009-01-31 17:45:48 +0100 | [diff] [blame] | 75 | htmlf("%*s%02x", |
| 76 | idx == 16 ? 4 : 1, "", |
| 77 | buf[idx] & 0xff); |
| 78 | html(" </td><td class='hex'>"); |
Lars Hjemli | 6063e7b | 2009-02-12 11:26:14 +0100 | [diff] [blame] | 79 | for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++) |
| 80 | ascii[idx] = isgraph(buf[idx]) ? buf[idx] : '.'; |
| 81 | ascii[idx] = '\0'; |
| 82 | html_txt(ascii); |
Lars Hjemli | ae1d4d7 | 2009-01-31 17:45:48 +0100 | [diff] [blame] | 83 | html("</td></tr>\n"); |
| 84 | } |
| 85 | html("</table>\n"); |
| 86 | } |
| 87 | |
John Keeping | 35a0829 | 2018-06-18 14:22:24 +0800 | [diff] [blame] | 88 | static void print_buffer(const char *basename, char *buf, unsigned long size) |
| 89 | { |
| 90 | if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) { |
| 91 | htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>", |
| 92 | size / 1024, ctx.cfg.max_blob_size); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | if (buffer_is_binary(buf, size)) |
| 97 | print_binary_buffer(buf, size); |
| 98 | else |
| 99 | print_text_buffer(basename, buf, size); |
| 100 | } |
| 101 | |
John Keeping | 8dced02 | 2018-06-18 14:23:45 +0800 | [diff] [blame^] | 102 | static void render_buffer(struct cgit_filter *render, const char *name, |
| 103 | char *buf, unsigned long size) |
| 104 | { |
| 105 | char *filter_arg = xstrdup(name); |
| 106 | |
| 107 | html("<div class='blob'>"); |
| 108 | cgit_open_filter(render, filter_arg); |
| 109 | html_raw(buf, size); |
| 110 | cgit_close_filter(render); |
| 111 | html("</div>"); |
| 112 | |
| 113 | free(filter_arg); |
| 114 | } |
| 115 | |
| 116 | static void include_file(const char *path, const char *mimetype) |
| 117 | { |
| 118 | const char *delim = "?"; |
| 119 | |
| 120 | html("<div class='blob'>"); |
| 121 | |
| 122 | if (!strncmp(mimetype, "image/", 6)) { |
| 123 | html("<img alt='"); |
| 124 | html_attr(path); |
| 125 | html("' src='"); |
| 126 | } else { |
| 127 | html("<iframe sandbox='allow-scripts' src='"); |
| 128 | } |
| 129 | |
| 130 | if (ctx.cfg.virtual_root) { |
| 131 | html_url_path(ctx.cfg.virtual_root); |
| 132 | html_url_path(ctx.repo->url); |
| 133 | if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') |
| 134 | html("/"); |
| 135 | html("plain/"); |
| 136 | html_url_path(path); |
| 137 | } else { |
| 138 | html_url_path(ctx.cfg.script_name); |
| 139 | html("?url="); |
| 140 | html_url_arg(ctx.repo->url); |
| 141 | if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') |
| 142 | html("/"); |
| 143 | html("plain/"); |
| 144 | if (path) |
| 145 | html_url_arg(path); |
| 146 | delim = "&"; |
| 147 | } |
| 148 | if (ctx.qry.head && ctx.repo->defbranch && |
| 149 | strcmp(ctx.qry.head, ctx.repo->defbranch)) { |
| 150 | html(delim); |
| 151 | html("h="); |
| 152 | html_url_arg(ctx.qry.head); |
| 153 | delim = "&"; |
| 154 | } |
| 155 | |
| 156 | html("'></div>"); |
| 157 | } |
| 158 | |
| 159 | static void print_object(const struct object_id *oid, char *path, const char *basename, |
| 160 | const char *rev, bool use_render) |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 161 | { |
| 162 | enum object_type type; |
John Keeping | 8dced02 | 2018-06-18 14:23:45 +0800 | [diff] [blame^] | 163 | struct cgit_filter *render; |
| 164 | char *buf, *mimetype; |
Lars Hjemli | ae1d4d7 | 2009-01-31 17:45:48 +0100 | [diff] [blame] | 165 | unsigned long size; |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 166 | |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 167 | type = oid_object_info(the_repository, oid, &size); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 168 | if (type == OBJ_BAD) { |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 169 | cgit_print_error_page(404, "Not found", |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 170 | "Bad object name: %s", oid_to_hex(oid)); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 174 | buf = read_object_file(oid, &type, &size); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 175 | if (!buf) { |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 176 | cgit_print_error_page(500, "Internal server error", |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 177 | "Error reading object %s", oid_to_hex(oid)); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | |
John Keeping | 8dced02 | 2018-06-18 14:23:45 +0800 | [diff] [blame^] | 181 | render = get_render_for_filename(path); |
| 182 | mimetype = render ? NULL : get_mimetype_for_filename(path); |
| 183 | |
Jeff Smith | 9337c7e | 2017-10-01 23:39:06 -0500 | [diff] [blame] | 184 | cgit_set_title_from_path(path); |
Jason A. Donenfeld | 23f7dad | 2016-01-18 15:56:45 +0100 | [diff] [blame] | 185 | |
John Keeping | 8dced02 | 2018-06-18 14:23:45 +0800 | [diff] [blame^] | 186 | /* |
| 187 | * If we don't have a render filter or a mimetype, we won't include the |
| 188 | * file in the page. |
| 189 | */ |
| 190 | if (!render && !mimetype) |
| 191 | use_render = false; |
| 192 | |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 193 | cgit_print_layout_start(); |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 194 | htmlf("blob: %s (", oid_to_hex(oid)); |
Lars Hjemli | 65b7b87 | 2008-08-06 11:07:13 +0200 | [diff] [blame] | 195 | cgit_plain_link("plain", NULL, NULL, ctx.qry.head, |
Lukas Fleischer | fb5a373 | 2013-03-03 16:45:14 +0100 | [diff] [blame] | 196 | rev, path); |
John Keeping | 8dced02 | 2018-06-18 14:23:45 +0800 | [diff] [blame^] | 197 | |
Jeff Smith | 1649afd | 2017-10-01 23:39:09 -0500 | [diff] [blame] | 198 | if (ctx.cfg.enable_blame) { |
| 199 | html(") ("); |
| 200 | cgit_blame_link("blame", NULL, NULL, ctx.qry.head, |
| 201 | rev, path); |
| 202 | } |
John Keeping | 8dced02 | 2018-06-18 14:23:45 +0800 | [diff] [blame^] | 203 | if (use_render) { |
| 204 | html(", "); |
| 205 | cgit_source_link("source", NULL, NULL, ctx.qry.head, |
| 206 | rev, path); |
| 207 | } else if (render || mimetype) { |
| 208 | html(", "); |
| 209 | cgit_tree_link("render", NULL, NULL, ctx.qry.head, |
| 210 | rev, path); |
| 211 | } |
Johan Herland | 48f7b98 | 2010-06-10 01:09:30 +0200 | [diff] [blame] | 212 | html(")\n"); |
Michael Krelin | d6b01da | 2007-07-21 19:51:47 +0200 | [diff] [blame] | 213 | |
John Keeping | 8dced02 | 2018-06-18 14:23:45 +0800 | [diff] [blame^] | 214 | if (use_render) { |
| 215 | if (render) |
| 216 | render_buffer(render, basename, buf, size); |
| 217 | else |
| 218 | include_file(path, mimetype); |
| 219 | } else |
| 220 | print_buffer(basename, buf, size); |
| 221 | |
Andy Green | 7708859 | 2018-06-13 10:02:00 +0800 | [diff] [blame] | 222 | free(buf); |
John Keeping | 8dced02 | 2018-06-18 14:23:45 +0800 | [diff] [blame^] | 223 | free(mimetype); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 224 | } |
| 225 | |
John Keeping | 000cf29 | 2016-07-13 20:19:42 +0100 | [diff] [blame] | 226 | struct single_tree_ctx { |
| 227 | struct strbuf *path; |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 228 | struct object_id oid; |
John Keeping | 000cf29 | 2016-07-13 20:19:42 +0100 | [diff] [blame] | 229 | char *name; |
| 230 | size_t count; |
| 231 | }; |
| 232 | |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 233 | static int single_tree_cb(const struct object_id *oid, struct strbuf *base, |
John Keeping | 000cf29 | 2016-07-13 20:19:42 +0100 | [diff] [blame] | 234 | const char *pathname, unsigned mode, int stage, |
| 235 | void *cbdata) |
| 236 | { |
| 237 | struct single_tree_ctx *ctx = cbdata; |
| 238 | |
| 239 | if (++ctx->count > 1) |
| 240 | return -1; |
| 241 | |
| 242 | if (!S_ISDIR(mode)) { |
| 243 | ctx->count = 2; |
| 244 | return -1; |
| 245 | } |
| 246 | |
| 247 | ctx->name = xstrdup(pathname); |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 248 | oidcpy(&ctx->oid, oid); |
John Keeping | 000cf29 | 2016-07-13 20:19:42 +0100 | [diff] [blame] | 249 | strbuf_addf(ctx->path, "/%s", pathname); |
| 250 | return 0; |
| 251 | } |
| 252 | |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 253 | static void write_tree_link(const struct object_id *oid, char *name, |
John Keeping | 000cf29 | 2016-07-13 20:19:42 +0100 | [diff] [blame] | 254 | char *rev, struct strbuf *fullpath) |
| 255 | { |
| 256 | size_t initial_length = fullpath->len; |
| 257 | struct tree *tree; |
| 258 | struct single_tree_ctx tree_ctx = { |
| 259 | .path = fullpath, |
| 260 | .count = 1, |
| 261 | }; |
| 262 | struct pathspec paths = { |
| 263 | .nr = 0 |
| 264 | }; |
| 265 | |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 266 | oidcpy(&tree_ctx.oid, oid); |
John Keeping | 000cf29 | 2016-07-13 20:19:42 +0100 | [diff] [blame] | 267 | |
| 268 | while (tree_ctx.count == 1) { |
| 269 | cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head, rev, |
| 270 | fullpath->buf); |
| 271 | |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 272 | tree = lookup_tree(&tree_ctx.oid); |
John Keeping | 000cf29 | 2016-07-13 20:19:42 +0100 | [diff] [blame] | 273 | if (!tree) |
| 274 | return; |
| 275 | |
| 276 | free(tree_ctx.name); |
| 277 | tree_ctx.name = NULL; |
| 278 | tree_ctx.count = 0; |
| 279 | |
| 280 | read_tree_recursive(tree, "", 0, 1, &paths, single_tree_cb, |
| 281 | &tree_ctx); |
| 282 | |
| 283 | if (tree_ctx.count != 1) |
| 284 | break; |
| 285 | |
| 286 | html(" / "); |
| 287 | name = tree_ctx.name; |
| 288 | } |
| 289 | |
| 290 | strbuf_setlen(fullpath, initial_length); |
| 291 | } |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 292 | |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 293 | static int ls_item(const struct object_id *oid, struct strbuf *base, |
Christian Hesse | 7358f63 | 2015-02-07 14:18:28 +0100 | [diff] [blame] | 294 | const char *pathname, unsigned mode, int stage, void *cbdata) |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 295 | { |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 296 | struct walk_tree_context *walk_tree_ctx = cbdata; |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 297 | char *name; |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 298 | struct strbuf fullpath = STRBUF_INIT; |
| 299 | struct strbuf class = STRBUF_INIT; |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame] | 300 | enum object_type type; |
Lars Hjemli | ded9393 | 2007-05-11 12:12:48 +0200 | [diff] [blame] | 301 | unsigned long size = 0; |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 302 | |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame] | 303 | name = xstrdup(pathname); |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 304 | strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "", |
| 305 | ctx.qry.path ? "/" : "", name); |
Lars Hjemli | 44947bf | 2007-06-17 01:23:08 +0200 | [diff] [blame] | 306 | |
Lars Hjemli | 08a8757 | 2008-05-20 22:32:22 +0200 | [diff] [blame] | 307 | if (!S_ISGITLINK(mode)) { |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 308 | type = oid_object_info(the_repository, oid, &size); |
Lars Hjemli | 08a8757 | 2008-05-20 22:32:22 +0200 | [diff] [blame] | 309 | if (type == OBJ_BAD) { |
| 310 | htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>", |
| 311 | name, |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 312 | oid_to_hex(oid)); |
Christian Hesse | 896cd69 | 2015-10-09 13:15:44 +0200 | [diff] [blame] | 313 | free(name); |
Lars Hjemli | 08a8757 | 2008-05-20 22:32:22 +0200 | [diff] [blame] | 314 | return 0; |
| 315 | } |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 316 | } |
Lars Hjemli | 44947bf | 2007-06-17 01:23:08 +0200 | [diff] [blame] | 317 | |
Lars Hjemli | 426032f | 2007-06-17 13:17:00 +0200 | [diff] [blame] | 318 | html("<tr><td class='ls-mode'>"); |
Lars Hjemli | b1f9b9c | 2008-02-23 22:45:33 +0100 | [diff] [blame] | 319 | cgit_print_filemode(mode); |
Lars Hjemli | 426032f | 2007-06-17 13:17:00 +0200 | [diff] [blame] | 320 | html("</td><td>"); |
Jeffrey C. Ollie | e651cb0 | 2007-06-04 12:28:56 -0500 | [diff] [blame] | 321 | if (S_ISGITLINK(mode)) { |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 322 | cgit_submodule_link("ls-mod", fullpath.buf, oid_to_hex(oid)); |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame] | 323 | } else if (S_ISDIR(mode)) { |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 324 | write_tree_link(oid, name, walk_tree_ctx->curr_rev, |
John Keeping | 000cf29 | 2016-07-13 20:19:42 +0100 | [diff] [blame] | 325 | &fullpath); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 326 | } else { |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 327 | char *ext = strrchr(name, '.'); |
| 328 | strbuf_addstr(&class, "ls-blob"); |
| 329 | if (ext) |
| 330 | strbuf_addf(&class, " %s", ext + 1); |
| 331 | cgit_tree_link(name, NULL, class.buf, ctx.qry.head, |
| 332 | walk_tree_ctx->curr_rev, fullpath.buf); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 333 | } |
Lars Hjemli | 426032f | 2007-06-17 13:17:00 +0200 | [diff] [blame] | 334 | htmlf("</td><td class='ls-size'>%li</td>", size); |
Lars Hjemli | 9fb53af | 2007-05-14 11:10:59 +0200 | [diff] [blame] | 335 | |
Lars Hjemli | 48c487d | 2007-06-17 13:57:51 +0200 | [diff] [blame] | 336 | html("<td>"); |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 337 | cgit_log_link("log", NULL, "button", ctx.qry.head, |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 338 | walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL, |
John Keeping | 30304d8 | 2015-08-12 15:55:28 +0100 | [diff] [blame] | 339 | ctx.qry.showmsg, 0); |
Lars Hjemli | 837d464 | 2008-12-07 13:34:42 +0100 | [diff] [blame] | 340 | if (ctx.repo->max_stats) |
| 341 | cgit_stats_link("stats", NULL, "button", ctx.qry.head, |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 342 | fullpath.buf); |
Lars Hjemli | 6857bec | 2011-06-15 10:04:13 +0200 | [diff] [blame] | 343 | if (!S_ISGITLINK(mode)) |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 344 | cgit_plain_link("plain", NULL, "button", ctx.qry.head, |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 345 | walk_tree_ctx->curr_rev, fullpath.buf); |
Jeff Smith | 1649afd | 2017-10-01 23:39:09 -0500 | [diff] [blame] | 346 | if (!S_ISDIR(mode) && ctx.cfg.enable_blame) |
| 347 | cgit_blame_link("blame", NULL, "button", ctx.qry.head, |
| 348 | walk_tree_ctx->curr_rev, fullpath.buf); |
Lars Hjemli | 48c487d | 2007-06-17 13:57:51 +0200 | [diff] [blame] | 349 | html("</td></tr>\n"); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 350 | free(name); |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 351 | strbuf_release(&fullpath); |
| 352 | strbuf_release(&class); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 353 | return 0; |
| 354 | } |
| 355 | |
John Keeping | e3d3fff | 2015-03-08 16:32:16 +0000 | [diff] [blame] | 356 | static void ls_head(void) |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 357 | { |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 358 | cgit_print_layout_start(); |
Lars Hjemli | 2915483 | 2007-11-11 13:04:28 +0100 | [diff] [blame] | 359 | html("<table summary='tree listing' class='list'>\n"); |
Lars Hjemli | 777faf7 | 2007-01-28 00:39:26 +0100 | [diff] [blame] | 360 | html("<tr class='nohover'>"); |
Lars Hjemli | a530428 | 2006-12-17 23:55:53 +0100 | [diff] [blame] | 361 | html("<th class='left'>Mode</th>"); |
| 362 | html("<th class='left'>Name</th>"); |
Lars Hjemli | 9a8f886 | 2006-12-16 00:19:56 +0100 | [diff] [blame] | 363 | html("<th class='right'>Size</th>"); |
Lars Hjemli | 9fb53af | 2007-05-14 11:10:59 +0200 | [diff] [blame] | 364 | html("<th/>"); |
Lars Hjemli | a530428 | 2006-12-17 23:55:53 +0100 | [diff] [blame] | 365 | html("</tr>\n"); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 366 | } |
| 367 | |
John Keeping | e3d3fff | 2015-03-08 16:32:16 +0000 | [diff] [blame] | 368 | static void ls_tail(void) |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 369 | { |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 370 | html("</table>\n"); |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 371 | cgit_print_layout_end(); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 372 | } |
| 373 | |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 374 | static void ls_tree(const struct object_id *oid, char *path, struct walk_tree_context *walk_tree_ctx) |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 375 | { |
| 376 | struct tree *tree; |
John Keeping | c1633c6 | 2013-03-02 12:32:11 +0000 | [diff] [blame] | 377 | struct pathspec paths = { |
| 378 | .nr = 0 |
| 379 | }; |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 380 | |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 381 | tree = parse_tree_indirect(oid); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 382 | if (!tree) { |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 383 | cgit_print_error_page(404, "Not found", |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 384 | "Not a tree object: %s", oid_to_hex(oid)); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 385 | return; |
| 386 | } |
| 387 | |
| 388 | ls_head(); |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 389 | read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 390 | ls_tail(); |
| 391 | } |
| 392 | |
| 393 | |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 394 | static int walk_tree(const struct object_id *oid, struct strbuf *base, |
Christian Hesse | 7358f63 | 2015-02-07 14:18:28 +0100 | [diff] [blame] | 395 | const char *pathname, unsigned mode, int stage, void *cbdata) |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 396 | { |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 397 | struct walk_tree_context *walk_tree_ctx = cbdata; |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 398 | |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 399 | if (walk_tree_ctx->state == 0) { |
John Keeping | e18a85b | 2016-08-13 11:54:46 +0100 | [diff] [blame] | 400 | struct strbuf buffer = STRBUF_INIT; |
| 401 | |
| 402 | strbuf_addbuf(&buffer, base); |
| 403 | strbuf_addstr(&buffer, pathname); |
| 404 | if (strcmp(walk_tree_ctx->match_path, buffer.buf)) |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 405 | return READ_TREE_RECURSIVE; |
| 406 | |
| 407 | if (S_ISDIR(mode)) { |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 408 | walk_tree_ctx->state = 1; |
Jeff Smith | 9337c7e | 2017-10-01 23:39:06 -0500 | [diff] [blame] | 409 | cgit_set_title_from_path(buffer.buf); |
John Keeping | e18a85b | 2016-08-13 11:54:46 +0100 | [diff] [blame] | 410 | strbuf_release(&buffer); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 411 | ls_head(); |
| 412 | return READ_TREE_RECURSIVE; |
| 413 | } else { |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 414 | walk_tree_ctx->state = 2; |
John Keeping | 8dced02 | 2018-06-18 14:23:45 +0800 | [diff] [blame^] | 415 | print_object(oid, buffer.buf, pathname, walk_tree_ctx->curr_rev, |
| 416 | walk_tree_ctx->use_render); |
John Keeping | e18a85b | 2016-08-13 11:54:46 +0100 | [diff] [blame] | 417 | strbuf_release(&buffer); |
John Keeping | 8dced02 | 2018-06-18 14:23:45 +0800 | [diff] [blame^] | 418 | |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | } |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 422 | ls_item(oid, base, pathname, mode, stage, walk_tree_ctx); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 423 | return 0; |
| 424 | } |
| 425 | |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 426 | /* |
| 427 | * Show a tree or a blob |
| 428 | * rev: the commit pointing at the root tree object |
| 429 | * path: path to tree or blob |
| 430 | */ |
John Keeping | 8dced02 | 2018-06-18 14:23:45 +0800 | [diff] [blame^] | 431 | void cgit_print_tree(const char *rev, char *path, bool use_render) |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 432 | { |
Christian Hesse | 9dd3c5e | 2016-09-29 22:17:07 +0200 | [diff] [blame] | 433 | struct object_id oid; |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 434 | struct commit *commit; |
John Keeping | c1633c6 | 2013-03-02 12:32:11 +0000 | [diff] [blame] | 435 | struct pathspec_item path_items = { |
| 436 | .match = path, |
| 437 | .len = path ? strlen(path) : 0 |
| 438 | }; |
| 439 | struct pathspec paths = { |
| 440 | .nr = path ? 1 : 0, |
| 441 | .items = &path_items |
| 442 | }; |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 443 | struct walk_tree_context walk_tree_ctx = { |
| 444 | .match_path = path, |
John Keeping | 8dced02 | 2018-06-18 14:23:45 +0800 | [diff] [blame^] | 445 | .state = 0, |
| 446 | .use_render = use_render, |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 447 | }; |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 448 | |
| 449 | if (!rev) |
Lars Hjemli | d14d77f | 2008-02-16 11:53:40 +0100 | [diff] [blame] | 450 | rev = ctx.qry.head; |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 451 | |
Christian Hesse | 9dd3c5e | 2016-09-29 22:17:07 +0200 | [diff] [blame] | 452 | if (get_oid(rev, &oid)) { |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 453 | cgit_print_error_page(404, "Not found", |
| 454 | "Invalid revision name: %s", rev); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 455 | return; |
| 456 | } |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 457 | commit = lookup_commit_reference(&oid); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 458 | if (!commit || parse_commit(commit)) { |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 459 | cgit_print_error_page(404, "Not found", |
| 460 | "Invalid commit reference: %s", rev); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 461 | return; |
| 462 | } |
| 463 | |
Lukas Fleischer | 985d6ca | 2013-03-04 13:25:36 +0100 | [diff] [blame] | 464 | walk_tree_ctx.curr_rev = xstrdup(rev); |
| 465 | |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 466 | if (path == NULL) { |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 467 | ls_tree(&commit->maybe_tree->object.oid, NULL, &walk_tree_ctx); |
Lukas Fleischer | 985d6ca | 2013-03-04 13:25:36 +0100 | [diff] [blame] | 468 | goto cleanup; |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Christian Hesse | 255b78f | 2018-06-04 18:49:28 +0200 | [diff] [blame] | 471 | read_tree_recursive(commit->maybe_tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx); |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 472 | if (walk_tree_ctx.state == 1) |
Lukas Fleischer | bfe5766 | 2013-03-03 16:55:21 +0100 | [diff] [blame] | 473 | ls_tail(); |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 474 | else if (walk_tree_ctx.state == 2) |
| 475 | cgit_print_layout_end(); |
| 476 | else |
| 477 | cgit_print_error_page(404, "Not found", "Path not found"); |
Lukas Fleischer | 985d6ca | 2013-03-04 13:25:36 +0100 | [diff] [blame] | 478 | |
| 479 | cleanup: |
| 480 | free(walk_tree_ctx.curr_rev); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 481 | } |