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; |
| 18 | }; |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 19 | |
Lars Hjemli | 46b7abe | 2009-07-31 16:55:27 +0200 | [diff] [blame] | 20 | 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] | 21 | { |
| 22 | unsigned long lineno, idx; |
Peter Wu | 4468ec1 | 2013-10-03 12:17:23 +0200 | [diff] [blame] | 23 | 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] | 24 | |
| 25 | html("<table summary='blob content' class='blob'>\n"); |
Lars Hjemli | 46b7abe | 2009-07-31 16:55:27 +0200 | [diff] [blame] | 26 | |
Lars Hjemli | b0f946b | 2009-08-21 14:26:52 +0200 | [diff] [blame] | 27 | if (ctx.cfg.enable_tree_linenumbers) { |
Florian Pritz | d67cc7f | 2009-08-09 20:42:45 +0000 | [diff] [blame] | 28 | html("<tr><td class='linenumbers'><pre>"); |
| 29 | idx = 0; |
| 30 | lineno = 0; |
Lukas Fleischer | 53bc747 | 2013-03-03 16:04:29 +0100 | [diff] [blame] | 31 | |
Florian Pritz | d67cc7f | 2009-08-09 20:42:45 +0000 | [diff] [blame] | 32 | if (size) { |
| 33 | htmlf(numberfmt, ++lineno); |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 34 | while (idx < size - 1) { // skip absolute last newline |
Florian Pritz | d67cc7f | 2009-08-09 20:42:45 +0000 | [diff] [blame] | 35 | if (buf[idx] == '\n') |
| 36 | htmlf(numberfmt, ++lineno); |
| 37 | idx++; |
| 38 | } |
Eric Wong | 1129736 | 2009-03-14 18:41:47 -0700 | [diff] [blame] | 39 | } |
Florian Pritz | d67cc7f | 2009-08-09 20:42:45 +0000 | [diff] [blame] | 40 | html("</pre></td>\n"); |
Lars Hjemli | ae1d4d7 | 2009-01-31 17:45:48 +0100 | [diff] [blame] | 41 | } |
Florian Pritz | d67cc7f | 2009-08-09 20:42:45 +0000 | [diff] [blame] | 42 | else { |
| 43 | html("<tr>\n"); |
| 44 | } |
Florian Pritz | 03389d6 | 2009-08-09 13:43:18 +0000 | [diff] [blame] | 45 | |
| 46 | if (ctx.repo->source_filter) { |
John Keeping | 3d8a650 | 2014-01-12 17:13:50 +0000 | [diff] [blame] | 47 | char *filter_arg = xstrdup(name); |
Florian Pritz | 03389d6 | 2009-08-09 13:43:18 +0000 | [diff] [blame] | 48 | html("<td class='lines'><pre><code>"); |
John Keeping | 3d8a650 | 2014-01-12 17:13:50 +0000 | [diff] [blame] | 49 | cgit_open_filter(ctx.repo->source_filter, filter_arg); |
Mark Lodato | d187b98 | 2010-09-04 14:18:16 -0400 | [diff] [blame] | 50 | html_raw(buf, size); |
Florian Pritz | 03389d6 | 2009-08-09 13:43:18 +0000 | [diff] [blame] | 51 | cgit_close_filter(ctx.repo->source_filter); |
John Keeping | 3d8a650 | 2014-01-12 17:13:50 +0000 | [diff] [blame] | 52 | free(filter_arg); |
Florian Pritz | 03389d6 | 2009-08-09 13:43:18 +0000 | [diff] [blame] | 53 | html("</code></pre></td></tr></table>\n"); |
| 54 | return; |
| 55 | } |
| 56 | |
Lars Hjemli | ae1d4d7 | 2009-01-31 17:45:48 +0100 | [diff] [blame] | 57 | html("<td class='lines'><pre><code>"); |
| 58 | html_txt(buf); |
| 59 | html("</code></pre></td></tr></table>\n"); |
| 60 | } |
| 61 | |
Lars Hjemli | 6063e7b | 2009-02-12 11:26:14 +0100 | [diff] [blame] | 62 | #define ROWLEN 32 |
| 63 | |
Lars Hjemli | ae1d4d7 | 2009-01-31 17:45:48 +0100 | [diff] [blame] | 64 | static void print_binary_buffer(char *buf, unsigned long size) |
| 65 | { |
| 66 | unsigned long ofs, idx; |
Lars Hjemli | 6063e7b | 2009-02-12 11:26:14 +0100 | [diff] [blame] | 67 | static char ascii[ROWLEN + 1]; |
Lars Hjemli | ae1d4d7 | 2009-01-31 17:45:48 +0100 | [diff] [blame] | 68 | |
| 69 | html("<table summary='blob content' class='bin-blob'>\n"); |
| 70 | 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] | 71 | for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) { |
Mark Lodato | e4ddc8f | 2010-09-04 11:30:18 -0400 | [diff] [blame] | 72 | htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs); |
Lars Hjemli | 6063e7b | 2009-02-12 11:26:14 +0100 | [diff] [blame] | 73 | for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++) |
Lars Hjemli | ae1d4d7 | 2009-01-31 17:45:48 +0100 | [diff] [blame] | 74 | htmlf("%*s%02x", |
| 75 | idx == 16 ? 4 : 1, "", |
| 76 | buf[idx] & 0xff); |
| 77 | html(" </td><td class='hex'>"); |
Lars Hjemli | 6063e7b | 2009-02-12 11:26:14 +0100 | [diff] [blame] | 78 | for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++) |
| 79 | ascii[idx] = isgraph(buf[idx]) ? buf[idx] : '.'; |
| 80 | ascii[idx] = '\0'; |
| 81 | html_txt(ascii); |
Lars Hjemli | ae1d4d7 | 2009-01-31 17:45:48 +0100 | [diff] [blame] | 82 | html("</td></tr>\n"); |
| 83 | } |
| 84 | html("</table>\n"); |
| 85 | } |
| 86 | |
Lukas Fleischer | fb5a373 | 2013-03-03 16:45:14 +0100 | [diff] [blame] | 87 | static void print_object(const unsigned char *sha1, char *path, const char *basename, const char *rev) |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 88 | { |
| 89 | enum object_type type; |
Lars Hjemli | 0835ffe | 2007-09-20 00:21:47 +0200 | [diff] [blame] | 90 | char *buf; |
Lars Hjemli | ae1d4d7 | 2009-01-31 17:45:48 +0100 | [diff] [blame] | 91 | unsigned long size; |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 92 | |
| 93 | type = sha1_object_info(sha1, &size); |
| 94 | if (type == OBJ_BAD) { |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 95 | cgit_print_error_page(404, "Not found", |
| 96 | "Bad object name: %s", sha1_to_hex(sha1)); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
| 100 | buf = read_sha1_file(sha1, &type, &size); |
| 101 | if (!buf) { |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 102 | cgit_print_error_page(500, "Internal server error", |
| 103 | "Error reading object %s", sha1_to_hex(sha1)); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 104 | return; |
| 105 | } |
| 106 | |
Jeff Smith | 9337c7e | 2017-10-01 23:39:06 -0500 | [diff] [blame] | 107 | cgit_set_title_from_path(path); |
Jason A. Donenfeld | 23f7dad | 2016-01-18 15:56:45 +0100 | [diff] [blame] | 108 | |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 109 | cgit_print_layout_start(); |
Johan Herland | 48f7b98 | 2010-06-10 01:09:30 +0200 | [diff] [blame] | 110 | htmlf("blob: %s (", sha1_to_hex(sha1)); |
Lars Hjemli | 65b7b87 | 2008-08-06 11:07:13 +0200 | [diff] [blame] | 111 | cgit_plain_link("plain", NULL, NULL, ctx.qry.head, |
Lukas Fleischer | fb5a373 | 2013-03-03 16:45:14 +0100 | [diff] [blame] | 112 | rev, path); |
Jeff Smith | 1649afd | 2017-10-01 23:39:09 -0500 | [diff] [blame] | 113 | if (ctx.cfg.enable_blame) { |
| 114 | html(") ("); |
| 115 | cgit_blame_link("blame", NULL, NULL, ctx.qry.head, |
| 116 | rev, path); |
| 117 | } |
Johan Herland | 48f7b98 | 2010-06-10 01:09:30 +0200 | [diff] [blame] | 118 | html(")\n"); |
Michael Krelin | d6b01da | 2007-07-21 19:51:47 +0200 | [diff] [blame] | 119 | |
Georg Lukas | ef07ccc | 2009-11-28 03:44:33 +0100 | [diff] [blame] | 120 | if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) { |
Mark Lodato | e4ddc8f | 2010-09-04 11:30:18 -0400 | [diff] [blame] | 121 | htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>", |
Georg Lukas | ef07ccc | 2009-11-28 03:44:33 +0100 | [diff] [blame] | 122 | size / 1024, ctx.cfg.max_blob_size); |
| 123 | return; |
| 124 | } |
| 125 | |
Lars Hjemli | ae1d4d7 | 2009-01-31 17:45:48 +0100 | [diff] [blame] | 126 | if (buffer_is_binary(buf, size)) |
| 127 | print_binary_buffer(buf, size); |
| 128 | else |
Lars Hjemli | 46b7abe | 2009-07-31 16:55:27 +0200 | [diff] [blame] | 129 | print_text_buffer(basename, buf, size); |
Andy Green | 7708859 | 2018-06-13 10:02:00 +0800 | [diff] [blame^] | 130 | |
| 131 | free(buf); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 132 | } |
| 133 | |
John Keeping | 000cf29 | 2016-07-13 20:19:42 +0100 | [diff] [blame] | 134 | struct single_tree_ctx { |
| 135 | struct strbuf *path; |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 136 | struct object_id oid; |
John Keeping | 000cf29 | 2016-07-13 20:19:42 +0100 | [diff] [blame] | 137 | char *name; |
| 138 | size_t count; |
| 139 | }; |
| 140 | |
| 141 | static int single_tree_cb(const unsigned char *sha1, struct strbuf *base, |
| 142 | const char *pathname, unsigned mode, int stage, |
| 143 | void *cbdata) |
| 144 | { |
| 145 | struct single_tree_ctx *ctx = cbdata; |
| 146 | |
| 147 | if (++ctx->count > 1) |
| 148 | return -1; |
| 149 | |
| 150 | if (!S_ISDIR(mode)) { |
| 151 | ctx->count = 2; |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | ctx->name = xstrdup(pathname); |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 156 | hashcpy(ctx->oid.hash, sha1); |
John Keeping | 000cf29 | 2016-07-13 20:19:42 +0100 | [diff] [blame] | 157 | strbuf_addf(ctx->path, "/%s", pathname); |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static void write_tree_link(const unsigned char *sha1, char *name, |
| 162 | char *rev, struct strbuf *fullpath) |
| 163 | { |
| 164 | size_t initial_length = fullpath->len; |
| 165 | struct tree *tree; |
| 166 | struct single_tree_ctx tree_ctx = { |
| 167 | .path = fullpath, |
| 168 | .count = 1, |
| 169 | }; |
| 170 | struct pathspec paths = { |
| 171 | .nr = 0 |
| 172 | }; |
| 173 | |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 174 | hashcpy(tree_ctx.oid.hash, sha1); |
John Keeping | 000cf29 | 2016-07-13 20:19:42 +0100 | [diff] [blame] | 175 | |
| 176 | while (tree_ctx.count == 1) { |
| 177 | cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head, rev, |
| 178 | fullpath->buf); |
| 179 | |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 180 | tree = lookup_tree(&tree_ctx.oid); |
John Keeping | 000cf29 | 2016-07-13 20:19:42 +0100 | [diff] [blame] | 181 | if (!tree) |
| 182 | return; |
| 183 | |
| 184 | free(tree_ctx.name); |
| 185 | tree_ctx.name = NULL; |
| 186 | tree_ctx.count = 0; |
| 187 | |
| 188 | read_tree_recursive(tree, "", 0, 1, &paths, single_tree_cb, |
| 189 | &tree_ctx); |
| 190 | |
| 191 | if (tree_ctx.count != 1) |
| 192 | break; |
| 193 | |
| 194 | html(" / "); |
| 195 | name = tree_ctx.name; |
| 196 | } |
| 197 | |
| 198 | strbuf_setlen(fullpath, initial_length); |
| 199 | } |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 200 | |
Christian Hesse | 7358f63 | 2015-02-07 14:18:28 +0100 | [diff] [blame] | 201 | static int ls_item(const unsigned char *sha1, struct strbuf *base, |
| 202 | const char *pathname, unsigned mode, int stage, void *cbdata) |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 203 | { |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 204 | struct walk_tree_context *walk_tree_ctx = cbdata; |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 205 | char *name; |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 206 | struct strbuf fullpath = STRBUF_INIT; |
| 207 | struct strbuf class = STRBUF_INIT; |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame] | 208 | enum object_type type; |
Lars Hjemli | ded9393 | 2007-05-11 12:12:48 +0200 | [diff] [blame] | 209 | unsigned long size = 0; |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 210 | |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame] | 211 | name = xstrdup(pathname); |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 212 | strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "", |
| 213 | ctx.qry.path ? "/" : "", name); |
Lars Hjemli | 44947bf | 2007-06-17 01:23:08 +0200 | [diff] [blame] | 214 | |
Lars Hjemli | 08a8757 | 2008-05-20 22:32:22 +0200 | [diff] [blame] | 215 | if (!S_ISGITLINK(mode)) { |
| 216 | type = sha1_object_info(sha1, &size); |
| 217 | if (type == OBJ_BAD) { |
| 218 | htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>", |
| 219 | name, |
| 220 | sha1_to_hex(sha1)); |
Christian Hesse | 896cd69 | 2015-10-09 13:15:44 +0200 | [diff] [blame] | 221 | free(name); |
Lars Hjemli | 08a8757 | 2008-05-20 22:32:22 +0200 | [diff] [blame] | 222 | return 0; |
| 223 | } |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 224 | } |
Lars Hjemli | 44947bf | 2007-06-17 01:23:08 +0200 | [diff] [blame] | 225 | |
Lars Hjemli | 426032f | 2007-06-17 13:17:00 +0200 | [diff] [blame] | 226 | html("<tr><td class='ls-mode'>"); |
Lars Hjemli | b1f9b9c | 2008-02-23 22:45:33 +0100 | [diff] [blame] | 227 | cgit_print_filemode(mode); |
Lars Hjemli | 426032f | 2007-06-17 13:17:00 +0200 | [diff] [blame] | 228 | html("</td><td>"); |
Jeffrey C. Ollie | e651cb0 | 2007-06-04 12:28:56 -0500 | [diff] [blame] | 229 | if (S_ISGITLINK(mode)) { |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 230 | cgit_submodule_link("ls-mod", fullpath.buf, sha1_to_hex(sha1)); |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame] | 231 | } else if (S_ISDIR(mode)) { |
John Keeping | 000cf29 | 2016-07-13 20:19:42 +0100 | [diff] [blame] | 232 | write_tree_link(sha1, name, walk_tree_ctx->curr_rev, |
| 233 | &fullpath); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 234 | } else { |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 235 | char *ext = strrchr(name, '.'); |
| 236 | strbuf_addstr(&class, "ls-blob"); |
| 237 | if (ext) |
| 238 | strbuf_addf(&class, " %s", ext + 1); |
| 239 | cgit_tree_link(name, NULL, class.buf, ctx.qry.head, |
| 240 | walk_tree_ctx->curr_rev, fullpath.buf); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 241 | } |
Lars Hjemli | 426032f | 2007-06-17 13:17:00 +0200 | [diff] [blame] | 242 | htmlf("</td><td class='ls-size'>%li</td>", size); |
Lars Hjemli | 9fb53af | 2007-05-14 11:10:59 +0200 | [diff] [blame] | 243 | |
Lars Hjemli | 48c487d | 2007-06-17 13:57:51 +0200 | [diff] [blame] | 244 | html("<td>"); |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 245 | cgit_log_link("log", NULL, "button", ctx.qry.head, |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 246 | walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL, |
John Keeping | 30304d8 | 2015-08-12 15:55:28 +0100 | [diff] [blame] | 247 | ctx.qry.showmsg, 0); |
Lars Hjemli | 837d464 | 2008-12-07 13:34:42 +0100 | [diff] [blame] | 248 | if (ctx.repo->max_stats) |
| 249 | cgit_stats_link("stats", NULL, "button", ctx.qry.head, |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 250 | fullpath.buf); |
Lars Hjemli | 6857bec | 2011-06-15 10:04:13 +0200 | [diff] [blame] | 251 | if (!S_ISGITLINK(mode)) |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 252 | cgit_plain_link("plain", NULL, "button", ctx.qry.head, |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 253 | walk_tree_ctx->curr_rev, fullpath.buf); |
Jeff Smith | 1649afd | 2017-10-01 23:39:09 -0500 | [diff] [blame] | 254 | if (!S_ISDIR(mode) && ctx.cfg.enable_blame) |
| 255 | cgit_blame_link("blame", NULL, "button", ctx.qry.head, |
| 256 | walk_tree_ctx->curr_rev, fullpath.buf); |
Lars Hjemli | 48c487d | 2007-06-17 13:57:51 +0200 | [diff] [blame] | 257 | html("</td></tr>\n"); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 258 | free(name); |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 259 | strbuf_release(&fullpath); |
| 260 | strbuf_release(&class); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 261 | return 0; |
| 262 | } |
| 263 | |
John Keeping | e3d3fff | 2015-03-08 16:32:16 +0000 | [diff] [blame] | 264 | static void ls_head(void) |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 265 | { |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 266 | cgit_print_layout_start(); |
Lars Hjemli | 2915483 | 2007-11-11 13:04:28 +0100 | [diff] [blame] | 267 | html("<table summary='tree listing' class='list'>\n"); |
Lars Hjemli | 777faf7 | 2007-01-28 00:39:26 +0100 | [diff] [blame] | 268 | html("<tr class='nohover'>"); |
Lars Hjemli | a530428 | 2006-12-17 23:55:53 +0100 | [diff] [blame] | 269 | html("<th class='left'>Mode</th>"); |
| 270 | html("<th class='left'>Name</th>"); |
Lars Hjemli | 9a8f886 | 2006-12-16 00:19:56 +0100 | [diff] [blame] | 271 | html("<th class='right'>Size</th>"); |
Lars Hjemli | 9fb53af | 2007-05-14 11:10:59 +0200 | [diff] [blame] | 272 | html("<th/>"); |
Lars Hjemli | a530428 | 2006-12-17 23:55:53 +0100 | [diff] [blame] | 273 | html("</tr>\n"); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 274 | } |
| 275 | |
John Keeping | e3d3fff | 2015-03-08 16:32:16 +0000 | [diff] [blame] | 276 | static void ls_tail(void) |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 277 | { |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 278 | html("</table>\n"); |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 279 | cgit_print_layout_end(); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 280 | } |
| 281 | |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 282 | 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] | 283 | { |
| 284 | struct tree *tree; |
John Keeping | c1633c6 | 2013-03-02 12:32:11 +0000 | [diff] [blame] | 285 | struct pathspec paths = { |
| 286 | .nr = 0 |
| 287 | }; |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 288 | |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 289 | tree = parse_tree_indirect(oid); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 290 | if (!tree) { |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 291 | cgit_print_error_page(404, "Not found", |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 292 | "Not a tree object: %s", sha1_to_hex(oid->hash)); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 293 | return; |
| 294 | } |
| 295 | |
| 296 | ls_head(); |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 297 | read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 298 | ls_tail(); |
| 299 | } |
| 300 | |
| 301 | |
Christian Hesse | 7358f63 | 2015-02-07 14:18:28 +0100 | [diff] [blame] | 302 | static int walk_tree(const unsigned char *sha1, struct strbuf *base, |
| 303 | const char *pathname, unsigned mode, int stage, void *cbdata) |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 304 | { |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 305 | struct walk_tree_context *walk_tree_ctx = cbdata; |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 306 | |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 307 | if (walk_tree_ctx->state == 0) { |
John Keeping | e18a85b | 2016-08-13 11:54:46 +0100 | [diff] [blame] | 308 | struct strbuf buffer = STRBUF_INIT; |
| 309 | |
| 310 | strbuf_addbuf(&buffer, base); |
| 311 | strbuf_addstr(&buffer, pathname); |
| 312 | if (strcmp(walk_tree_ctx->match_path, buffer.buf)) |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 313 | return READ_TREE_RECURSIVE; |
| 314 | |
| 315 | if (S_ISDIR(mode)) { |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 316 | walk_tree_ctx->state = 1; |
Jeff Smith | 9337c7e | 2017-10-01 23:39:06 -0500 | [diff] [blame] | 317 | cgit_set_title_from_path(buffer.buf); |
John Keeping | e18a85b | 2016-08-13 11:54:46 +0100 | [diff] [blame] | 318 | strbuf_release(&buffer); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 319 | ls_head(); |
| 320 | return READ_TREE_RECURSIVE; |
| 321 | } else { |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 322 | walk_tree_ctx->state = 2; |
John Keeping | e18a85b | 2016-08-13 11:54:46 +0100 | [diff] [blame] | 323 | print_object(sha1, buffer.buf, pathname, walk_tree_ctx->curr_rev); |
| 324 | strbuf_release(&buffer); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | } |
Christian Hesse | 7358f63 | 2015-02-07 14:18:28 +0100 | [diff] [blame] | 328 | ls_item(sha1, base, pathname, mode, stage, walk_tree_ctx); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 332 | /* |
| 333 | * Show a tree or a blob |
| 334 | * rev: the commit pointing at the root tree object |
| 335 | * path: path to tree or blob |
| 336 | */ |
| 337 | void cgit_print_tree(const char *rev, char *path) |
| 338 | { |
Christian Hesse | 9dd3c5e | 2016-09-29 22:17:07 +0200 | [diff] [blame] | 339 | struct object_id oid; |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 340 | struct commit *commit; |
John Keeping | c1633c6 | 2013-03-02 12:32:11 +0000 | [diff] [blame] | 341 | struct pathspec_item path_items = { |
| 342 | .match = path, |
| 343 | .len = path ? strlen(path) : 0 |
| 344 | }; |
| 345 | struct pathspec paths = { |
| 346 | .nr = path ? 1 : 0, |
| 347 | .items = &path_items |
| 348 | }; |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 349 | struct walk_tree_context walk_tree_ctx = { |
| 350 | .match_path = path, |
| 351 | .state = 0 |
| 352 | }; |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 353 | |
| 354 | if (!rev) |
Lars Hjemli | d14d77f | 2008-02-16 11:53:40 +0100 | [diff] [blame] | 355 | rev = ctx.qry.head; |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 356 | |
Christian Hesse | 9dd3c5e | 2016-09-29 22:17:07 +0200 | [diff] [blame] | 357 | if (get_oid(rev, &oid)) { |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 358 | cgit_print_error_page(404, "Not found", |
| 359 | "Invalid revision name: %s", rev); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 360 | return; |
| 361 | } |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 362 | commit = lookup_commit_reference(&oid); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 363 | if (!commit || parse_commit(commit)) { |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 364 | cgit_print_error_page(404, "Not found", |
| 365 | "Invalid commit reference: %s", rev); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 366 | return; |
| 367 | } |
| 368 | |
Lukas Fleischer | 985d6ca | 2013-03-04 13:25:36 +0100 | [diff] [blame] | 369 | walk_tree_ctx.curr_rev = xstrdup(rev); |
| 370 | |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 371 | if (path == NULL) { |
Jeff Smith | 86a6d35 | 2017-08-09 19:02:56 -0500 | [diff] [blame] | 372 | ls_tree(&commit->tree->object.oid, NULL, &walk_tree_ctx); |
Lukas Fleischer | 985d6ca | 2013-03-04 13:25:36 +0100 | [diff] [blame] | 373 | goto cleanup; |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame] | 374 | } |
| 375 | |
Lukas Fleischer | 210a571 | 2013-03-03 17:22:30 +0100 | [diff] [blame] | 376 | read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx); |
| 377 | if (walk_tree_ctx.state == 1) |
Lukas Fleischer | bfe5766 | 2013-03-03 16:55:21 +0100 | [diff] [blame] | 378 | ls_tail(); |
John Keeping | 9c70c0b | 2015-08-14 12:47:20 +0100 | [diff] [blame] | 379 | else if (walk_tree_ctx.state == 2) |
| 380 | cgit_print_layout_end(); |
| 381 | else |
| 382 | cgit_print_error_page(404, "Not found", "Path not found"); |
Lukas Fleischer | 985d6ca | 2013-03-04 13:25:36 +0100 | [diff] [blame] | 383 | |
| 384 | cleanup: |
| 385 | free(walk_tree_ctx.curr_rev); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 386 | } |