blob: 721c812203be971f2e1bde94a317ee44e2e0b61e [file] [log] [blame]
Lars Hjemli06fe0c22006-12-13 00:13:27 +01001/* ui-tree.c: functions for tree output
2 *
Jeff Smith1649afd2017-10-01 23:39:09 -05003 * Copyright (C) 2006-2017 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 {
15 char *curr_rev;
16 char *match_path;
17 int state;
John Keeping8dced022018-06-18 14:23:45 +080018 bool use_render;
Lukas Fleischer210a5712013-03-03 17:22:30 +010019};
Lars Hjemli06fe0c22006-12-13 00:13:27 +010020
Lars Hjemli46b7abe2009-07-31 16:55:27 +020021static void print_text_buffer(const char *name, char *buf, unsigned long size)
Lars Hjemliae1d4d72009-01-31 17:45:48 +010022{
23 unsigned long lineno, idx;
Peter Wu4468ec12013-10-03 12:17:23 +020024 const char *numberfmt = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
Lars Hjemliae1d4d72009-01-31 17:45:48 +010025
26 html("<table summary='blob content' class='blob'>\n");
Lars Hjemli46b7abe2009-07-31 16:55:27 +020027
Lars Hjemlib0f946b2009-08-21 14:26:52 +020028 if (ctx.cfg.enable_tree_linenumbers) {
Florian Pritzd67cc7f2009-08-09 20:42:45 +000029 html("<tr><td class='linenumbers'><pre>");
30 idx = 0;
31 lineno = 0;
Lukas Fleischer53bc7472013-03-03 16:04:29 +010032
Florian Pritzd67cc7f2009-08-09 20:42:45 +000033 if (size) {
34 htmlf(numberfmt, ++lineno);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -050035 while (idx < size - 1) { // skip absolute last newline
Florian Pritzd67cc7f2009-08-09 20:42:45 +000036 if (buf[idx] == '\n')
37 htmlf(numberfmt, ++lineno);
38 idx++;
39 }
Eric Wong11297362009-03-14 18:41:47 -070040 }
Florian Pritzd67cc7f2009-08-09 20:42:45 +000041 html("</pre></td>\n");
Lars Hjemliae1d4d72009-01-31 17:45:48 +010042 }
Florian Pritzd67cc7f2009-08-09 20:42:45 +000043 else {
44 html("<tr>\n");
45 }
Florian Pritz03389d62009-08-09 13:43:18 +000046
47 if (ctx.repo->source_filter) {
John Keeping3d8a6502014-01-12 17:13:50 +000048 char *filter_arg = xstrdup(name);
Florian Pritz03389d62009-08-09 13:43:18 +000049 html("<td class='lines'><pre><code>");
John Keeping3d8a6502014-01-12 17:13:50 +000050 cgit_open_filter(ctx.repo->source_filter, filter_arg);
Mark Lodatod187b982010-09-04 14:18:16 -040051 html_raw(buf, size);
Florian Pritz03389d62009-08-09 13:43:18 +000052 cgit_close_filter(ctx.repo->source_filter);
John Keeping3d8a6502014-01-12 17:13:50 +000053 free(filter_arg);
Florian Pritz03389d62009-08-09 13:43:18 +000054 html("</code></pre></td></tr></table>\n");
55 return;
56 }
57
Lars Hjemliae1d4d72009-01-31 17:45:48 +010058 html("<td class='lines'><pre><code>");
59 html_txt(buf);
60 html("</code></pre></td></tr></table>\n");
61}
62
Lars Hjemli6063e7b2009-02-12 11:26:14 +010063#define ROWLEN 32
64
Lars Hjemliae1d4d72009-01-31 17:45:48 +010065static void print_binary_buffer(char *buf, unsigned long size)
66{
67 unsigned long ofs, idx;
Lars Hjemli6063e7b2009-02-12 11:26:14 +010068 static char ascii[ROWLEN + 1];
Lars Hjemliae1d4d72009-01-31 17:45:48 +010069
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 Hjemli6063e7b2009-02-12 11:26:14 +010072 for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
Mark Lodatoe4ddc8f2010-09-04 11:30:18 -040073 htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs);
Lars Hjemli6063e7b2009-02-12 11:26:14 +010074 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
Lars Hjemliae1d4d72009-01-31 17:45:48 +010075 htmlf("%*s%02x",
76 idx == 16 ? 4 : 1, "",
77 buf[idx] & 0xff);
78 html(" </td><td class='hex'>");
Lars Hjemli6063e7b2009-02-12 11:26:14 +010079 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 Hjemliae1d4d72009-01-31 17:45:48 +010083 html("</td></tr>\n");
84 }
85 html("</table>\n");
86}
87
John Keeping35a08292018-06-18 14:22:24 +080088static 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 Keeping8dced022018-06-18 14:23:45 +0800102static 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
116static 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
159static void print_object(const struct object_id *oid, char *path, const char *basename,
160 const char *rev, bool use_render)
Lars Hjemliffc69732007-06-16 20:20:42 +0200161{
162 enum object_type type;
John Keeping8dced022018-06-18 14:23:45 +0800163 struct cgit_filter *render;
164 char *buf, *mimetype;
Lars Hjemliae1d4d72009-01-31 17:45:48 +0100165 unsigned long size;
Lars Hjemliffc69732007-06-16 20:20:42 +0200166
Christian Hesse255b78f2018-06-04 18:49:28 +0200167 type = oid_object_info(the_repository, oid, &size);
Lars Hjemliffc69732007-06-16 20:20:42 +0200168 if (type == OBJ_BAD) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100169 cgit_print_error_page(404, "Not found",
Christian Hesse255b78f2018-06-04 18:49:28 +0200170 "Bad object name: %s", oid_to_hex(oid));
Lars Hjemliffc69732007-06-16 20:20:42 +0200171 return;
172 }
173
Christian Hesse255b78f2018-06-04 18:49:28 +0200174 buf = read_object_file(oid, &type, &size);
Lars Hjemliffc69732007-06-16 20:20:42 +0200175 if (!buf) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100176 cgit_print_error_page(500, "Internal server error",
Christian Hesse255b78f2018-06-04 18:49:28 +0200177 "Error reading object %s", oid_to_hex(oid));
Lars Hjemliffc69732007-06-16 20:20:42 +0200178 return;
179 }
180
John Keeping8dced022018-06-18 14:23:45 +0800181 render = get_render_for_filename(path);
182 mimetype = render ? NULL : get_mimetype_for_filename(path);
183
Jeff Smith9337c7e2017-10-01 23:39:06 -0500184 cgit_set_title_from_path(path);
Jason A. Donenfeld23f7dad2016-01-18 15:56:45 +0100185
John Keeping8dced022018-06-18 14:23:45 +0800186 /*
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 Keeping9c70c0b2015-08-14 12:47:20 +0100193 cgit_print_layout_start();
Christian Hesse255b78f2018-06-04 18:49:28 +0200194 htmlf("blob: %s (", oid_to_hex(oid));
Lars Hjemli65b7b872008-08-06 11:07:13 +0200195 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
Lukas Fleischerfb5a3732013-03-03 16:45:14 +0100196 rev, path);
John Keeping8dced022018-06-18 14:23:45 +0800197
Jeff Smith1649afd2017-10-01 23:39:09 -0500198 if (ctx.cfg.enable_blame) {
199 html(") (");
200 cgit_blame_link("blame", NULL, NULL, ctx.qry.head,
201 rev, path);
202 }
John Keeping8dced022018-06-18 14:23:45 +0800203 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 Herland48f7b982010-06-10 01:09:30 +0200212 html(")\n");
Michael Krelind6b01da2007-07-21 19:51:47 +0200213
John Keeping8dced022018-06-18 14:23:45 +0800214 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 Green77088592018-06-13 10:02:00 +0800222 free(buf);
John Keeping8dced022018-06-18 14:23:45 +0800223 free(mimetype);
Lars Hjemliffc69732007-06-16 20:20:42 +0200224}
225
John Keeping000cf292016-07-13 20:19:42 +0100226struct single_tree_ctx {
227 struct strbuf *path;
Jeff Smith86a6d352017-08-09 19:02:56 -0500228 struct object_id oid;
John Keeping000cf292016-07-13 20:19:42 +0100229 char *name;
230 size_t count;
231};
232
Christian Hesse255b78f2018-06-04 18:49:28 +0200233static int single_tree_cb(const struct object_id *oid, struct strbuf *base,
John Keeping000cf292016-07-13 20:19:42 +0100234 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 Hesse255b78f2018-06-04 18:49:28 +0200248 oidcpy(&ctx->oid, oid);
John Keeping000cf292016-07-13 20:19:42 +0100249 strbuf_addf(ctx->path, "/%s", pathname);
250 return 0;
251}
252
Christian Hesse255b78f2018-06-04 18:49:28 +0200253static void write_tree_link(const struct object_id *oid, char *name,
John Keeping000cf292016-07-13 20:19:42 +0100254 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 Hesse255b78f2018-06-04 18:49:28 +0200266 oidcpy(&tree_ctx.oid, oid);
John Keeping000cf292016-07-13 20:19:42 +0100267
268 while (tree_ctx.count == 1) {
269 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head, rev,
270 fullpath->buf);
271
Jeff Smith86a6d352017-08-09 19:02:56 -0500272 tree = lookup_tree(&tree_ctx.oid);
John Keeping000cf292016-07-13 20:19:42 +0100273 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 Hjemliffc69732007-06-16 20:20:42 +0200292
Christian Hesse255b78f2018-06-04 18:49:28 +0200293static int ls_item(const struct object_id *oid, struct strbuf *base,
Christian Hesse7358f632015-02-07 14:18:28 +0100294 const char *pathname, unsigned mode, int stage, void *cbdata)
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100295{
Lukas Fleischer210a5712013-03-03 17:22:30 +0100296 struct walk_tree_context *walk_tree_ctx = cbdata;
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100297 char *name;
John Keepingfb3655d2013-04-06 10:28:57 +0100298 struct strbuf fullpath = STRBUF_INIT;
299 struct strbuf class = STRBUF_INIT;
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200300 enum object_type type;
Lars Hjemlided93932007-05-11 12:12:48 +0200301 unsigned long size = 0;
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100302
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200303 name = xstrdup(pathname);
John Keepingfb3655d2013-04-06 10:28:57 +0100304 strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "",
305 ctx.qry.path ? "/" : "", name);
Lars Hjemli44947bf2007-06-17 01:23:08 +0200306
Lars Hjemli08a87572008-05-20 22:32:22 +0200307 if (!S_ISGITLINK(mode)) {
Christian Hesse255b78f2018-06-04 18:49:28 +0200308 type = oid_object_info(the_repository, oid, &size);
Lars Hjemli08a87572008-05-20 22:32:22 +0200309 if (type == OBJ_BAD) {
310 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
311 name,
Christian Hesse255b78f2018-06-04 18:49:28 +0200312 oid_to_hex(oid));
Christian Hesse896cd692015-10-09 13:15:44 +0200313 free(name);
Lars Hjemli08a87572008-05-20 22:32:22 +0200314 return 0;
315 }
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100316 }
Lars Hjemli44947bf2007-06-17 01:23:08 +0200317
Lars Hjemli426032f2007-06-17 13:17:00 +0200318 html("<tr><td class='ls-mode'>");
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +0100319 cgit_print_filemode(mode);
Lars Hjemli426032f2007-06-17 13:17:00 +0200320 html("</td><td>");
Jeffrey C. Olliee651cb02007-06-04 12:28:56 -0500321 if (S_ISGITLINK(mode)) {
Christian Hesse255b78f2018-06-04 18:49:28 +0200322 cgit_submodule_link("ls-mod", fullpath.buf, oid_to_hex(oid));
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200323 } else if (S_ISDIR(mode)) {
Christian Hesse255b78f2018-06-04 18:49:28 +0200324 write_tree_link(oid, name, walk_tree_ctx->curr_rev,
John Keeping000cf292016-07-13 20:19:42 +0100325 &fullpath);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100326 } else {
John Keepingfb3655d2013-04-06 10:28:57 +0100327 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 Hjemli06fe0c22006-12-13 00:13:27 +0100333 }
Lars Hjemli426032f2007-06-17 13:17:00 +0200334 htmlf("</td><td class='ls-size'>%li</td>", size);
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200335
Lars Hjemli48c487d2007-06-17 13:57:51 +0200336 html("<td>");
Lukas Fleischer210a5712013-03-03 17:22:30 +0100337 cgit_log_link("log", NULL, "button", ctx.qry.head,
John Keepingfb3655d2013-04-06 10:28:57 +0100338 walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL,
John Keeping30304d82015-08-12 15:55:28 +0100339 ctx.qry.showmsg, 0);
Lars Hjemli837d4642008-12-07 13:34:42 +0100340 if (ctx.repo->max_stats)
341 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
John Keepingfb3655d2013-04-06 10:28:57 +0100342 fullpath.buf);
Lars Hjemli6857bec2011-06-15 10:04:13 +0200343 if (!S_ISGITLINK(mode))
Lukas Fleischer210a5712013-03-03 17:22:30 +0100344 cgit_plain_link("plain", NULL, "button", ctx.qry.head,
John Keepingfb3655d2013-04-06 10:28:57 +0100345 walk_tree_ctx->curr_rev, fullpath.buf);
Jeff Smith1649afd2017-10-01 23:39:09 -0500346 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 Hjemli48c487d2007-06-17 13:57:51 +0200349 html("</td></tr>\n");
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100350 free(name);
John Keepingfb3655d2013-04-06 10:28:57 +0100351 strbuf_release(&fullpath);
352 strbuf_release(&class);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100353 return 0;
354}
355
John Keepinge3d3fff2015-03-08 16:32:16 +0000356static void ls_head(void)
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100357{
John Keeping9c70c0b2015-08-14 12:47:20 +0100358 cgit_print_layout_start();
Lars Hjemli29154832007-11-11 13:04:28 +0100359 html("<table summary='tree listing' class='list'>\n");
Lars Hjemli777faf72007-01-28 00:39:26 +0100360 html("<tr class='nohover'>");
Lars Hjemlia5304282006-12-17 23:55:53 +0100361 html("<th class='left'>Mode</th>");
362 html("<th class='left'>Name</th>");
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100363 html("<th class='right'>Size</th>");
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200364 html("<th/>");
Lars Hjemlia5304282006-12-17 23:55:53 +0100365 html("</tr>\n");
Lars Hjemliffc69732007-06-16 20:20:42 +0200366}
367
John Keepinge3d3fff2015-03-08 16:32:16 +0000368static void ls_tail(void)
Lars Hjemliffc69732007-06-16 20:20:42 +0200369{
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100370 html("</table>\n");
John Keeping9c70c0b2015-08-14 12:47:20 +0100371 cgit_print_layout_end();
Lars Hjemliffc69732007-06-16 20:20:42 +0200372}
373
Jeff Smith86a6d352017-08-09 19:02:56 -0500374static void ls_tree(const struct object_id *oid, char *path, struct walk_tree_context *walk_tree_ctx)
Lars Hjemliffc69732007-06-16 20:20:42 +0200375{
376 struct tree *tree;
John Keepingc1633c62013-03-02 12:32:11 +0000377 struct pathspec paths = {
378 .nr = 0
379 };
Lars Hjemliffc69732007-06-16 20:20:42 +0200380
Jeff Smith86a6d352017-08-09 19:02:56 -0500381 tree = parse_tree_indirect(oid);
Lars Hjemliffc69732007-06-16 20:20:42 +0200382 if (!tree) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100383 cgit_print_error_page(404, "Not found",
Christian Hesse255b78f2018-06-04 18:49:28 +0200384 "Not a tree object: %s", oid_to_hex(oid));
Lars Hjemliffc69732007-06-16 20:20:42 +0200385 return;
386 }
387
388 ls_head();
Lukas Fleischer210a5712013-03-03 17:22:30 +0100389 read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
Lars Hjemliffc69732007-06-16 20:20:42 +0200390 ls_tail();
391}
392
393
Christian Hesse255b78f2018-06-04 18:49:28 +0200394static int walk_tree(const struct object_id *oid, struct strbuf *base,
Christian Hesse7358f632015-02-07 14:18:28 +0100395 const char *pathname, unsigned mode, int stage, void *cbdata)
Lars Hjemliffc69732007-06-16 20:20:42 +0200396{
Lukas Fleischer210a5712013-03-03 17:22:30 +0100397 struct walk_tree_context *walk_tree_ctx = cbdata;
Lars Hjemliffc69732007-06-16 20:20:42 +0200398
Lukas Fleischer210a5712013-03-03 17:22:30 +0100399 if (walk_tree_ctx->state == 0) {
John Keepinge18a85b2016-08-13 11:54:46 +0100400 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 Hjemliffc69732007-06-16 20:20:42 +0200405 return READ_TREE_RECURSIVE;
406
407 if (S_ISDIR(mode)) {
Lukas Fleischer210a5712013-03-03 17:22:30 +0100408 walk_tree_ctx->state = 1;
Jeff Smith9337c7e2017-10-01 23:39:06 -0500409 cgit_set_title_from_path(buffer.buf);
John Keepinge18a85b2016-08-13 11:54:46 +0100410 strbuf_release(&buffer);
Lars Hjemliffc69732007-06-16 20:20:42 +0200411 ls_head();
412 return READ_TREE_RECURSIVE;
413 } else {
John Keeping9c70c0b2015-08-14 12:47:20 +0100414 walk_tree_ctx->state = 2;
John Keeping8dced022018-06-18 14:23:45 +0800415 print_object(oid, buffer.buf, pathname, walk_tree_ctx->curr_rev,
416 walk_tree_ctx->use_render);
John Keepinge18a85b2016-08-13 11:54:46 +0100417 strbuf_release(&buffer);
John Keeping8dced022018-06-18 14:23:45 +0800418
Lars Hjemliffc69732007-06-16 20:20:42 +0200419 return 0;
420 }
421 }
Christian Hesse255b78f2018-06-04 18:49:28 +0200422 ls_item(oid, base, pathname, mode, stage, walk_tree_ctx);
Lars Hjemliffc69732007-06-16 20:20:42 +0200423 return 0;
424}
425
Lars Hjemliffc69732007-06-16 20:20:42 +0200426/*
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 Keeping8dced022018-06-18 14:23:45 +0800431void cgit_print_tree(const char *rev, char *path, bool use_render)
Lars Hjemliffc69732007-06-16 20:20:42 +0200432{
Christian Hesse9dd3c5e2016-09-29 22:17:07 +0200433 struct object_id oid;
Lars Hjemliffc69732007-06-16 20:20:42 +0200434 struct commit *commit;
John Keepingc1633c62013-03-02 12:32:11 +0000435 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 Fleischer210a5712013-03-03 17:22:30 +0100443 struct walk_tree_context walk_tree_ctx = {
444 .match_path = path,
John Keeping8dced022018-06-18 14:23:45 +0800445 .state = 0,
446 .use_render = use_render,
Lukas Fleischer210a5712013-03-03 17:22:30 +0100447 };
Lars Hjemliffc69732007-06-16 20:20:42 +0200448
449 if (!rev)
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100450 rev = ctx.qry.head;
Lars Hjemliffc69732007-06-16 20:20:42 +0200451
Christian Hesse9dd3c5e2016-09-29 22:17:07 +0200452 if (get_oid(rev, &oid)) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100453 cgit_print_error_page(404, "Not found",
454 "Invalid revision name: %s", rev);
Lars Hjemliffc69732007-06-16 20:20:42 +0200455 return;
456 }
Jeff Smith86a6d352017-08-09 19:02:56 -0500457 commit = lookup_commit_reference(&oid);
Lars Hjemliffc69732007-06-16 20:20:42 +0200458 if (!commit || parse_commit(commit)) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100459 cgit_print_error_page(404, "Not found",
460 "Invalid commit reference: %s", rev);
Lars Hjemliffc69732007-06-16 20:20:42 +0200461 return;
462 }
463
Lukas Fleischer985d6ca2013-03-04 13:25:36 +0100464 walk_tree_ctx.curr_rev = xstrdup(rev);
465
Lars Hjemliffc69732007-06-16 20:20:42 +0200466 if (path == NULL) {
Christian Hesse255b78f2018-06-04 18:49:28 +0200467 ls_tree(&commit->maybe_tree->object.oid, NULL, &walk_tree_ctx);
Lukas Fleischer985d6ca2013-03-04 13:25:36 +0100468 goto cleanup;
Lars Hjemliffc69732007-06-16 20:20:42 +0200469 }
470
Christian Hesse255b78f2018-06-04 18:49:28 +0200471 read_tree_recursive(commit->maybe_tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
Lukas Fleischer210a5712013-03-03 17:22:30 +0100472 if (walk_tree_ctx.state == 1)
Lukas Fleischerbfe57662013-03-03 16:55:21 +0100473 ls_tail();
John Keeping9c70c0b2015-08-14 12:47:20 +0100474 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 Fleischer985d6ca2013-03-04 13:25:36 +0100478
479cleanup:
480 free(walk_tree_ctx.curr_rev);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100481}