blob: f96b8454752c177e973f6e70a3cbfa28273a52e2 [file] [log] [blame]
Lars Hjemli06fe0c22006-12-13 00:13:27 +01001/* ui-tree.c: functions for tree output
2 *
Andy Green83070692018-06-18 14:25:11 +08003 * Copyright (C) 2006-2018 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemli06fe0c22006-12-13 00:13:27 +01004 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +010010#include "ui-tree.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010011#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010012#include "ui-shared.h"
Lars Hjemli06fe0c22006-12-13 00:13:27 +010013
Lukas Fleischer210a5712013-03-03 17:22:30 +010014struct walk_tree_context {
Andy Green83070692018-06-18 14:25:11 +080015 struct object_id inline_oid;
Lukas Fleischer210a5712013-03-03 17:22:30 +010016 char *curr_rev;
17 char *match_path;
Andy Green83070692018-06-18 14:25:11 +080018 char *inline_filename;
Lukas Fleischer210a5712013-03-03 17:22:30 +010019 int state;
John Keeping8dced022018-06-18 14:23:45 +080020 bool use_render;
Lukas Fleischer210a5712013-03-03 17:22:30 +010021};
Lars Hjemli06fe0c22006-12-13 00:13:27 +010022
Lars Hjemli46b7abe2009-07-31 16:55:27 +020023static void print_text_buffer(const char *name, char *buf, unsigned long size)
Lars Hjemliae1d4d72009-01-31 17:45:48 +010024{
25 unsigned long lineno, idx;
Peter Wu4468ec12013-10-03 12:17:23 +020026 const char *numberfmt = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
Lars Hjemliae1d4d72009-01-31 17:45:48 +010027
28 html("<table summary='blob content' class='blob'>\n");
Lars Hjemli46b7abe2009-07-31 16:55:27 +020029
Lars Hjemlib0f946b2009-08-21 14:26:52 +020030 if (ctx.cfg.enable_tree_linenumbers) {
Andy Green91984092018-06-24 09:08:40 +080031 html("<tr><td id='linenumbers' class='linenumbers'><pre>");
Florian Pritzd67cc7f2009-08-09 20:42:45 +000032 idx = 0;
33 lineno = 0;
Lukas Fleischer53bc7472013-03-03 16:04:29 +010034
Florian Pritzd67cc7f2009-08-09 20:42:45 +000035 if (size) {
36 htmlf(numberfmt, ++lineno);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -050037 while (idx < size - 1) { // skip absolute last newline
Florian Pritzd67cc7f2009-08-09 20:42:45 +000038 if (buf[idx] == '\n')
39 htmlf(numberfmt, ++lineno);
40 idx++;
41 }
Eric Wong11297362009-03-14 18:41:47 -070042 }
Florian Pritzd67cc7f2009-08-09 20:42:45 +000043 html("</pre></td>\n");
Lars Hjemliae1d4d72009-01-31 17:45:48 +010044 }
Florian Pritzd67cc7f2009-08-09 20:42:45 +000045 else {
46 html("<tr>\n");
47 }
Florian Pritz03389d62009-08-09 13:43:18 +000048
49 if (ctx.repo->source_filter) {
John Keeping3d8a6502014-01-12 17:13:50 +000050 char *filter_arg = xstrdup(name);
Florian Pritz03389d62009-08-09 13:43:18 +000051 html("<td class='lines'><pre><code>");
John Keeping3d8a6502014-01-12 17:13:50 +000052 cgit_open_filter(ctx.repo->source_filter, filter_arg);
Mark Lodatod187b982010-09-04 14:18:16 -040053 html_raw(buf, size);
Florian Pritz03389d62009-08-09 13:43:18 +000054 cgit_close_filter(ctx.repo->source_filter);
John Keeping3d8a6502014-01-12 17:13:50 +000055 free(filter_arg);
Florian Pritz03389d62009-08-09 13:43:18 +000056 html("</code></pre></td></tr></table>\n");
57 return;
58 }
59
Lars Hjemliae1d4d72009-01-31 17:45:48 +010060 html("<td class='lines'><pre><code>");
61 html_txt(buf);
62 html("</code></pre></td></tr></table>\n");
63}
64
Lars Hjemli6063e7b2009-02-12 11:26:14 +010065#define ROWLEN 32
66
Lars Hjemliae1d4d72009-01-31 17:45:48 +010067static void print_binary_buffer(char *buf, unsigned long size)
68{
69 unsigned long ofs, idx;
Lars Hjemli6063e7b2009-02-12 11:26:14 +010070 static char ascii[ROWLEN + 1];
Lars Hjemliae1d4d72009-01-31 17:45:48 +010071
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 Hjemli6063e7b2009-02-12 11:26:14 +010074 for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
Mark Lodatoe4ddc8f2010-09-04 11:30:18 -040075 htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs);
Lars Hjemli6063e7b2009-02-12 11:26:14 +010076 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
Lars Hjemliae1d4d72009-01-31 17:45:48 +010077 htmlf("%*s%02x",
78 idx == 16 ? 4 : 1, "",
79 buf[idx] & 0xff);
80 html(" </td><td class='hex'>");
Lars Hjemli6063e7b2009-02-12 11:26:14 +010081 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 Hjemliae1d4d72009-01-31 17:45:48 +010085 html("</td></tr>\n");
86 }
87 html("</table>\n");
88}
89
John Keeping35a08292018-06-18 14:22:24 +080090static 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 Keeping8dced022018-06-18 14:23:45 +0800104static void render_buffer(struct cgit_filter *render, const char *name,
Andy Green1ef4d392018-06-18 14:25:59 +0800105 char *buf, unsigned long size)
John Keeping8dced022018-06-18 14:23:45 +0800106{
107 char *filter_arg = xstrdup(name);
Andy Green1ef4d392018-06-18 14:25:59 +0800108 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 Keeping8dced022018-06-18 14:23:45 +0800111
112 html("<div class='blob'>");
Andy Green1ef4d392018-06-18 14:25:59 +0800113 cgit_open_filter(render, filter_arg, sb_pre.buf, sb_post.buf);
John Keeping8dced022018-06-18 14:23:45 +0800114 html_raw(buf, size);
115 cgit_close_filter(render);
116 html("</div>");
117
Andy Green1ef4d392018-06-18 14:25:59 +0800118 strbuf_release(&sb_pre);
119 strbuf_release(&sb_post);
John Keeping8dced022018-06-18 14:23:45 +0800120 free(filter_arg);
121}
122
123static 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
166static void print_object(const struct object_id *oid, char *path, const char *basename,
167 const char *rev, bool use_render)
Lars Hjemliffc69732007-06-16 20:20:42 +0200168{
169 enum object_type type;
John Keeping8dced022018-06-18 14:23:45 +0800170 struct cgit_filter *render;
171 char *buf, *mimetype;
Lars Hjemliae1d4d72009-01-31 17:45:48 +0100172 unsigned long size;
Lars Hjemliffc69732007-06-16 20:20:42 +0200173
Christian Hesse255b78f2018-06-04 18:49:28 +0200174 type = oid_object_info(the_repository, oid, &size);
Lars Hjemliffc69732007-06-16 20:20:42 +0200175 if (type == OBJ_BAD) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100176 cgit_print_error_page(404, "Not found",
Christian Hesse255b78f2018-06-04 18:49:28 +0200177 "Bad object name: %s", oid_to_hex(oid));
Lars Hjemliffc69732007-06-16 20:20:42 +0200178 return;
179 }
180
Christian Hesse255b78f2018-06-04 18:49:28 +0200181 buf = read_object_file(oid, &type, &size);
Lars Hjemliffc69732007-06-16 20:20:42 +0200182 if (!buf) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100183 cgit_print_error_page(500, "Internal server error",
Christian Hesse255b78f2018-06-04 18:49:28 +0200184 "Error reading object %s", oid_to_hex(oid));
Lars Hjemliffc69732007-06-16 20:20:42 +0200185 return;
186 }
187
John Keeping8dced022018-06-18 14:23:45 +0800188 render = get_render_for_filename(path);
189 mimetype = render ? NULL : get_mimetype_for_filename(path);
190
Jeff Smith9337c7e2017-10-01 23:39:06 -0500191 cgit_set_title_from_path(path);
Jason A. Donenfeld23f7dad2016-01-18 15:56:45 +0100192
John Keeping8dced022018-06-18 14:23:45 +0800193 /*
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 Keeping9c70c0b2015-08-14 12:47:20 +0100200 cgit_print_layout_start();
Christian Hesse255b78f2018-06-04 18:49:28 +0200201 htmlf("blob: %s (", oid_to_hex(oid));
Lars Hjemli65b7b872008-08-06 11:07:13 +0200202 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
Lukas Fleischerfb5a3732013-03-03 16:45:14 +0100203 rev, path);
John Keeping8dced022018-06-18 14:23:45 +0800204
Jeff Smith1649afd2017-10-01 23:39:09 -0500205 if (ctx.cfg.enable_blame) {
206 html(") (");
207 cgit_blame_link("blame", NULL, NULL, ctx.qry.head,
208 rev, path);
209 }
John Keeping8dced022018-06-18 14:23:45 +0800210 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 Herland48f7b982010-06-10 01:09:30 +0200219 html(")\n");
Michael Krelind6b01da2007-07-21 19:51:47 +0200220
John Keeping8dced022018-06-18 14:23:45 +0800221 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 Green77088592018-06-13 10:02:00 +0800229 free(buf);
John Keeping8dced022018-06-18 14:23:45 +0800230 free(mimetype);
Lars Hjemliffc69732007-06-16 20:20:42 +0200231}
232
John Keeping000cf292016-07-13 20:19:42 +0100233struct single_tree_ctx {
234 struct strbuf *path;
Jeff Smith86a6d352017-08-09 19:02:56 -0500235 struct object_id oid;
John Keeping000cf292016-07-13 20:19:42 +0100236 char *name;
237 size_t count;
238};
239
Christian Hesse255b78f2018-06-04 18:49:28 +0200240static int single_tree_cb(const struct object_id *oid, struct strbuf *base,
John Keeping000cf292016-07-13 20:19:42 +0100241 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 Hesse255b78f2018-06-04 18:49:28 +0200255 oidcpy(&ctx->oid, oid);
John Keeping000cf292016-07-13 20:19:42 +0100256 strbuf_addf(ctx->path, "/%s", pathname);
257 return 0;
258}
259
Christian Hesse255b78f2018-06-04 18:49:28 +0200260static void write_tree_link(const struct object_id *oid, char *name,
John Keeping000cf292016-07-13 20:19:42 +0100261 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 Hesse255b78f2018-06-04 18:49:28 +0200273 oidcpy(&tree_ctx.oid, oid);
John Keeping000cf292016-07-13 20:19:42 +0100274
275 while (tree_ctx.count == 1) {
276 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head, rev,
277 fullpath->buf);
278
Jeff Smith86a6d352017-08-09 19:02:56 -0500279 tree = lookup_tree(&tree_ctx.oid);
John Keeping000cf292016-07-13 20:19:42 +0100280 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 Hjemliffc69732007-06-16 20:20:42 +0200299
Christian Hesse255b78f2018-06-04 18:49:28 +0200300static int ls_item(const struct object_id *oid, struct strbuf *base,
Christian Hesse7358f632015-02-07 14:18:28 +0100301 const char *pathname, unsigned mode, int stage, void *cbdata)
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100302{
Lukas Fleischer210a5712013-03-03 17:22:30 +0100303 struct walk_tree_context *walk_tree_ctx = cbdata;
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100304 char *name;
John Keepingfb3655d2013-04-06 10:28:57 +0100305 struct strbuf fullpath = STRBUF_INIT;
306 struct strbuf class = STRBUF_INIT;
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200307 enum object_type type;
Lars Hjemlided93932007-05-11 12:12:48 +0200308 unsigned long size = 0;
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100309
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200310 name = xstrdup(pathname);
John Keepingfb3655d2013-04-06 10:28:57 +0100311 strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "",
312 ctx.qry.path ? "/" : "", name);
Lars Hjemli44947bf2007-06-17 01:23:08 +0200313
Lars Hjemli08a87572008-05-20 22:32:22 +0200314 if (!S_ISGITLINK(mode)) {
Christian Hesse255b78f2018-06-04 18:49:28 +0200315 type = oid_object_info(the_repository, oid, &size);
Lars Hjemli08a87572008-05-20 22:32:22 +0200316 if (type == OBJ_BAD) {
317 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
318 name,
Christian Hesse255b78f2018-06-04 18:49:28 +0200319 oid_to_hex(oid));
Christian Hesse896cd692015-10-09 13:15:44 +0200320 free(name);
Lars Hjemli08a87572008-05-20 22:32:22 +0200321 return 0;
322 }
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100323 }
Lars Hjemli44947bf2007-06-17 01:23:08 +0200324
Lars Hjemli426032f2007-06-17 13:17:00 +0200325 html("<tr><td class='ls-mode'>");
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +0100326 cgit_print_filemode(mode);
Lars Hjemli426032f2007-06-17 13:17:00 +0200327 html("</td><td>");
Jeffrey C. Olliee651cb02007-06-04 12:28:56 -0500328 if (S_ISGITLINK(mode)) {
Christian Hesse255b78f2018-06-04 18:49:28 +0200329 cgit_submodule_link("ls-mod", fullpath.buf, oid_to_hex(oid));
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200330 } else if (S_ISDIR(mode)) {
Christian Hesse255b78f2018-06-04 18:49:28 +0200331 write_tree_link(oid, name, walk_tree_ctx->curr_rev,
John Keeping000cf292016-07-13 20:19:42 +0100332 &fullpath);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100333 } else {
John Keepingfb3655d2013-04-06 10:28:57 +0100334 char *ext = strrchr(name, '.');
Andy Green83070692018-06-18 14:25:11 +0800335
John Keepingfb3655d2013-04-06 10:28:57 +0100336 strbuf_addstr(&class, "ls-blob");
337 if (ext)
338 strbuf_addf(&class, " %s", ext + 1);
Andy Green83070692018-06-18 14:25:11 +0800339
John Keepingfb3655d2013-04-06 10:28:57 +0100340 cgit_tree_link(name, NULL, class.buf, ctx.qry.head,
341 walk_tree_ctx->curr_rev, fullpath.buf);
Andy Green83070692018-06-18 14:25:11 +0800342
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 Hjemli06fe0c22006-12-13 00:13:27 +0100348 }
Lars Hjemli426032f2007-06-17 13:17:00 +0200349 htmlf("</td><td class='ls-size'>%li</td>", size);
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200350
Lars Hjemli48c487d2007-06-17 13:57:51 +0200351 html("<td>");
Lukas Fleischer210a5712013-03-03 17:22:30 +0100352 cgit_log_link("log", NULL, "button", ctx.qry.head,
John Keepingfb3655d2013-04-06 10:28:57 +0100353 walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL,
John Keeping30304d82015-08-12 15:55:28 +0100354 ctx.qry.showmsg, 0);
Lars Hjemli837d4642008-12-07 13:34:42 +0100355 if (ctx.repo->max_stats)
356 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
John Keepingfb3655d2013-04-06 10:28:57 +0100357 fullpath.buf);
Lars Hjemli6857bec2011-06-15 10:04:13 +0200358 if (!S_ISGITLINK(mode))
Lukas Fleischer210a5712013-03-03 17:22:30 +0100359 cgit_plain_link("plain", NULL, "button", ctx.qry.head,
John Keepingfb3655d2013-04-06 10:28:57 +0100360 walk_tree_ctx->curr_rev, fullpath.buf);
Jeff Smith1649afd2017-10-01 23:39:09 -0500361 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 Hjemli48c487d2007-06-17 13:57:51 +0200364 html("</td></tr>\n");
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100365 free(name);
John Keepingfb3655d2013-04-06 10:28:57 +0100366 strbuf_release(&fullpath);
367 strbuf_release(&class);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100368 return 0;
369}
370
John Keepinge3d3fff2015-03-08 16:32:16 +0000371static void ls_head(void)
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100372{
John Keeping9c70c0b2015-08-14 12:47:20 +0100373 cgit_print_layout_start();
Lars Hjemli29154832007-11-11 13:04:28 +0100374 html("<table summary='tree listing' class='list'>\n");
Lars Hjemli777faf72007-01-28 00:39:26 +0100375 html("<tr class='nohover'>");
Lars Hjemlia5304282006-12-17 23:55:53 +0100376 html("<th class='left'>Mode</th>");
377 html("<th class='left'>Name</th>");
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100378 html("<th class='right'>Size</th>");
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200379 html("<th/>");
Lars Hjemlia5304282006-12-17 23:55:53 +0100380 html("</tr>\n");
Lars Hjemliffc69732007-06-16 20:20:42 +0200381}
382
Andy Green85cd2122018-06-18 14:24:20 +0800383static void ls_tail(struct walk_tree_context *walk_tree_ctx)
Lars Hjemliffc69732007-06-16 20:20:42 +0200384{
Andy Green83070692018-06-18 14:25:11 +0800385 struct cgit_filter *render;
386 enum object_type type;
387 char *buf, *mimetype;
388 unsigned long size;
389
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100390 html("</table>\n");
Andy Green83070692018-06-18 14:25:11 +0800391
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>&nbsp;</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
424done:
John Keeping9c70c0b2015-08-14 12:47:20 +0100425 cgit_print_layout_end();
Lars Hjemliffc69732007-06-16 20:20:42 +0200426}
427
Jeff Smith86a6d352017-08-09 19:02:56 -0500428static void ls_tree(const struct object_id *oid, char *path, struct walk_tree_context *walk_tree_ctx)
Lars Hjemliffc69732007-06-16 20:20:42 +0200429{
430 struct tree *tree;
John Keepingc1633c62013-03-02 12:32:11 +0000431 struct pathspec paths = {
432 .nr = 0
433 };
Lars Hjemliffc69732007-06-16 20:20:42 +0200434
Jeff Smith86a6d352017-08-09 19:02:56 -0500435 tree = parse_tree_indirect(oid);
Lars Hjemliffc69732007-06-16 20:20:42 +0200436 if (!tree) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100437 cgit_print_error_page(404, "Not found",
Christian Hesse255b78f2018-06-04 18:49:28 +0200438 "Not a tree object: %s", oid_to_hex(oid));
Lars Hjemliffc69732007-06-16 20:20:42 +0200439 return;
440 }
441
442 ls_head();
Lukas Fleischer210a5712013-03-03 17:22:30 +0100443 read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
Andy Green85cd2122018-06-18 14:24:20 +0800444 ls_tail(walk_tree_ctx);
Lars Hjemliffc69732007-06-16 20:20:42 +0200445}
446
447
Christian Hesse255b78f2018-06-04 18:49:28 +0200448static int walk_tree(const struct object_id *oid, struct strbuf *base,
Christian Hesse7358f632015-02-07 14:18:28 +0100449 const char *pathname, unsigned mode, int stage, void *cbdata)
Lars Hjemliffc69732007-06-16 20:20:42 +0200450{
Lukas Fleischer210a5712013-03-03 17:22:30 +0100451 struct walk_tree_context *walk_tree_ctx = cbdata;
Lars Hjemliffc69732007-06-16 20:20:42 +0200452
Lukas Fleischer210a5712013-03-03 17:22:30 +0100453 if (walk_tree_ctx->state == 0) {
John Keepinge18a85b2016-08-13 11:54:46 +0100454 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 Hjemliffc69732007-06-16 20:20:42 +0200459 return READ_TREE_RECURSIVE;
460
461 if (S_ISDIR(mode)) {
Lukas Fleischer210a5712013-03-03 17:22:30 +0100462 walk_tree_ctx->state = 1;
Jeff Smith9337c7e2017-10-01 23:39:06 -0500463 cgit_set_title_from_path(buffer.buf);
John Keepinge18a85b2016-08-13 11:54:46 +0100464 strbuf_release(&buffer);
Lars Hjemliffc69732007-06-16 20:20:42 +0200465 ls_head();
466 return READ_TREE_RECURSIVE;
467 } else {
John Keeping9c70c0b2015-08-14 12:47:20 +0100468 walk_tree_ctx->state = 2;
John Keeping8dced022018-06-18 14:23:45 +0800469 print_object(oid, buffer.buf, pathname, walk_tree_ctx->curr_rev,
470 walk_tree_ctx->use_render);
John Keepinge18a85b2016-08-13 11:54:46 +0100471 strbuf_release(&buffer);
John Keeping8dced022018-06-18 14:23:45 +0800472
Lars Hjemliffc69732007-06-16 20:20:42 +0200473 return 0;
474 }
475 }
Christian Hesse255b78f2018-06-04 18:49:28 +0200476 ls_item(oid, base, pathname, mode, stage, walk_tree_ctx);
Lars Hjemliffc69732007-06-16 20:20:42 +0200477 return 0;
478}
479
Lars Hjemliffc69732007-06-16 20:20:42 +0200480/*
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 Keeping8dced022018-06-18 14:23:45 +0800485void cgit_print_tree(const char *rev, char *path, bool use_render)
Lars Hjemliffc69732007-06-16 20:20:42 +0200486{
Christian Hesse9dd3c5e2016-09-29 22:17:07 +0200487 struct object_id oid;
Lars Hjemliffc69732007-06-16 20:20:42 +0200488 struct commit *commit;
John Keepingc1633c62013-03-02 12:32:11 +0000489 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 Fleischer210a5712013-03-03 17:22:30 +0100497 struct walk_tree_context walk_tree_ctx = {
498 .match_path = path,
John Keeping8dced022018-06-18 14:23:45 +0800499 .state = 0,
500 .use_render = use_render,
Lukas Fleischer210a5712013-03-03 17:22:30 +0100501 };
Lars Hjemliffc69732007-06-16 20:20:42 +0200502
503 if (!rev)
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100504 rev = ctx.qry.head;
Lars Hjemliffc69732007-06-16 20:20:42 +0200505
Christian Hesse9dd3c5e2016-09-29 22:17:07 +0200506 if (get_oid(rev, &oid)) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100507 cgit_print_error_page(404, "Not found",
508 "Invalid revision name: %s", rev);
Lars Hjemliffc69732007-06-16 20:20:42 +0200509 return;
510 }
Jeff Smith86a6d352017-08-09 19:02:56 -0500511 commit = lookup_commit_reference(&oid);
Lars Hjemliffc69732007-06-16 20:20:42 +0200512 if (!commit || parse_commit(commit)) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100513 cgit_print_error_page(404, "Not found",
514 "Invalid commit reference: %s", rev);
Lars Hjemliffc69732007-06-16 20:20:42 +0200515 return;
516 }
517
Lukas Fleischer985d6ca2013-03-04 13:25:36 +0100518 walk_tree_ctx.curr_rev = xstrdup(rev);
519
Lars Hjemliffc69732007-06-16 20:20:42 +0200520 if (path == NULL) {
Christian Hesse255b78f2018-06-04 18:49:28 +0200521 ls_tree(&commit->maybe_tree->object.oid, NULL, &walk_tree_ctx);
Lukas Fleischer985d6ca2013-03-04 13:25:36 +0100522 goto cleanup;
Lars Hjemliffc69732007-06-16 20:20:42 +0200523 }
524
Christian Hesse255b78f2018-06-04 18:49:28 +0200525 read_tree_recursive(commit->maybe_tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
Lukas Fleischer210a5712013-03-03 17:22:30 +0100526 if (walk_tree_ctx.state == 1)
Andy Green85cd2122018-06-18 14:24:20 +0800527 ls_tail(&walk_tree_ctx);
John Keeping9c70c0b2015-08-14 12:47:20 +0100528 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 Fleischer985d6ca2013-03-04 13:25:36 +0100532
533cleanup:
534 free(walk_tree_ctx.curr_rev);
Andy Green83070692018-06-18 14:25:11 +0800535 if (walk_tree_ctx.inline_filename)
536 free(walk_tree_ctx.inline_filename);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100537}