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