blob: 6ffd4ddca398d34309e0c17a3a157960b72c2e57 [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) {
Florian Pritzd67cc7f2009-08-09 20:42:45 +000031 html("<tr><td class='linenumbers'><pre>");
32 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,
105 char *buf, unsigned long size)
106{
107 char *filter_arg = xstrdup(name);
108
109 html("<div class='blob'>");
110 cgit_open_filter(render, filter_arg);
111 html_raw(buf, size);
112 cgit_close_filter(render);
113 html("</div>");
114
115 free(filter_arg);
116}
117
118static void include_file(const char *path, const char *mimetype)
119{
120 const char *delim = "?";
121
122 html("<div class='blob'>");
123
124 if (!strncmp(mimetype, "image/", 6)) {
125 html("<img alt='");
126 html_attr(path);
127 html("' src='");
128 } else {
129 html("<iframe sandbox='allow-scripts' src='");
130 }
131
132 if (ctx.cfg.virtual_root) {
133 html_url_path(ctx.cfg.virtual_root);
134 html_url_path(ctx.repo->url);
135 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
136 html("/");
137 html("plain/");
138 html_url_path(path);
139 } else {
140 html_url_path(ctx.cfg.script_name);
141 html("?url=");
142 html_url_arg(ctx.repo->url);
143 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
144 html("/");
145 html("plain/");
146 if (path)
147 html_url_arg(path);
148 delim = "&";
149 }
150 if (ctx.qry.head && ctx.repo->defbranch &&
151 strcmp(ctx.qry.head, ctx.repo->defbranch)) {
152 html(delim);
153 html("h=");
154 html_url_arg(ctx.qry.head);
155 delim = "&";
156 }
157
158 html("'></div>");
159}
160
161static void print_object(const struct object_id *oid, char *path, const char *basename,
162 const char *rev, bool use_render)
Lars Hjemliffc69732007-06-16 20:20:42 +0200163{
164 enum object_type type;
John Keeping8dced022018-06-18 14:23:45 +0800165 struct cgit_filter *render;
166 char *buf, *mimetype;
Lars Hjemliae1d4d72009-01-31 17:45:48 +0100167 unsigned long size;
Lars Hjemliffc69732007-06-16 20:20:42 +0200168
Christian Hesse255b78f2018-06-04 18:49:28 +0200169 type = oid_object_info(the_repository, oid, &size);
Lars Hjemliffc69732007-06-16 20:20:42 +0200170 if (type == OBJ_BAD) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100171 cgit_print_error_page(404, "Not found",
Christian Hesse255b78f2018-06-04 18:49:28 +0200172 "Bad object name: %s", oid_to_hex(oid));
Lars Hjemliffc69732007-06-16 20:20:42 +0200173 return;
174 }
175
Christian Hesse255b78f2018-06-04 18:49:28 +0200176 buf = read_object_file(oid, &type, &size);
Lars Hjemliffc69732007-06-16 20:20:42 +0200177 if (!buf) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100178 cgit_print_error_page(500, "Internal server error",
Christian Hesse255b78f2018-06-04 18:49:28 +0200179 "Error reading object %s", oid_to_hex(oid));
Lars Hjemliffc69732007-06-16 20:20:42 +0200180 return;
181 }
182
John Keeping8dced022018-06-18 14:23:45 +0800183 render = get_render_for_filename(path);
184 mimetype = render ? NULL : get_mimetype_for_filename(path);
185
Jeff Smith9337c7e2017-10-01 23:39:06 -0500186 cgit_set_title_from_path(path);
Jason A. Donenfeld23f7dad2016-01-18 15:56:45 +0100187
John Keeping8dced022018-06-18 14:23:45 +0800188 /*
189 * If we don't have a render filter or a mimetype, we won't include the
190 * file in the page.
191 */
192 if (!render && !mimetype)
193 use_render = false;
194
John Keeping9c70c0b2015-08-14 12:47:20 +0100195 cgit_print_layout_start();
Christian Hesse255b78f2018-06-04 18:49:28 +0200196 htmlf("blob: %s (", oid_to_hex(oid));
Lars Hjemli65b7b872008-08-06 11:07:13 +0200197 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
Lukas Fleischerfb5a3732013-03-03 16:45:14 +0100198 rev, path);
John Keeping8dced022018-06-18 14:23:45 +0800199
Jeff Smith1649afd2017-10-01 23:39:09 -0500200 if (ctx.cfg.enable_blame) {
201 html(") (");
202 cgit_blame_link("blame", NULL, NULL, ctx.qry.head,
203 rev, path);
204 }
John Keeping8dced022018-06-18 14:23:45 +0800205 if (use_render) {
206 html(", ");
207 cgit_source_link("source", NULL, NULL, ctx.qry.head,
208 rev, path);
209 } else if (render || mimetype) {
210 html(", ");
211 cgit_tree_link("render", NULL, NULL, ctx.qry.head,
212 rev, path);
213 }
Johan Herland48f7b982010-06-10 01:09:30 +0200214 html(")\n");
Michael Krelind6b01da2007-07-21 19:51:47 +0200215
John Keeping8dced022018-06-18 14:23:45 +0800216 if (use_render) {
217 if (render)
218 render_buffer(render, basename, buf, size);
219 else
220 include_file(path, mimetype);
221 } else
222 print_buffer(basename, buf, size);
223
Andy Green77088592018-06-13 10:02:00 +0800224 free(buf);
John Keeping8dced022018-06-18 14:23:45 +0800225 free(mimetype);
Lars Hjemliffc69732007-06-16 20:20:42 +0200226}
227
John Keeping000cf292016-07-13 20:19:42 +0100228struct single_tree_ctx {
229 struct strbuf *path;
Jeff Smith86a6d352017-08-09 19:02:56 -0500230 struct object_id oid;
John Keeping000cf292016-07-13 20:19:42 +0100231 char *name;
232 size_t count;
233};
234
Christian Hesse255b78f2018-06-04 18:49:28 +0200235static int single_tree_cb(const struct object_id *oid, struct strbuf *base,
John Keeping000cf292016-07-13 20:19:42 +0100236 const char *pathname, unsigned mode, int stage,
237 void *cbdata)
238{
239 struct single_tree_ctx *ctx = cbdata;
240
241 if (++ctx->count > 1)
242 return -1;
243
244 if (!S_ISDIR(mode)) {
245 ctx->count = 2;
246 return -1;
247 }
248
249 ctx->name = xstrdup(pathname);
Christian Hesse255b78f2018-06-04 18:49:28 +0200250 oidcpy(&ctx->oid, oid);
John Keeping000cf292016-07-13 20:19:42 +0100251 strbuf_addf(ctx->path, "/%s", pathname);
252 return 0;
253}
254
Christian Hesse255b78f2018-06-04 18:49:28 +0200255static void write_tree_link(const struct object_id *oid, char *name,
John Keeping000cf292016-07-13 20:19:42 +0100256 char *rev, struct strbuf *fullpath)
257{
258 size_t initial_length = fullpath->len;
259 struct tree *tree;
260 struct single_tree_ctx tree_ctx = {
261 .path = fullpath,
262 .count = 1,
263 };
264 struct pathspec paths = {
265 .nr = 0
266 };
267
Christian Hesse255b78f2018-06-04 18:49:28 +0200268 oidcpy(&tree_ctx.oid, oid);
John Keeping000cf292016-07-13 20:19:42 +0100269
270 while (tree_ctx.count == 1) {
271 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head, rev,
272 fullpath->buf);
273
Jeff Smith86a6d352017-08-09 19:02:56 -0500274 tree = lookup_tree(&tree_ctx.oid);
John Keeping000cf292016-07-13 20:19:42 +0100275 if (!tree)
276 return;
277
278 free(tree_ctx.name);
279 tree_ctx.name = NULL;
280 tree_ctx.count = 0;
281
282 read_tree_recursive(tree, "", 0, 1, &paths, single_tree_cb,
283 &tree_ctx);
284
285 if (tree_ctx.count != 1)
286 break;
287
288 html(" / ");
289 name = tree_ctx.name;
290 }
291
292 strbuf_setlen(fullpath, initial_length);
293}
Lars Hjemliffc69732007-06-16 20:20:42 +0200294
Christian Hesse255b78f2018-06-04 18:49:28 +0200295static int ls_item(const struct object_id *oid, struct strbuf *base,
Christian Hesse7358f632015-02-07 14:18:28 +0100296 const char *pathname, unsigned mode, int stage, void *cbdata)
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100297{
Lukas Fleischer210a5712013-03-03 17:22:30 +0100298 struct walk_tree_context *walk_tree_ctx = cbdata;
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100299 char *name;
John Keepingfb3655d2013-04-06 10:28:57 +0100300 struct strbuf fullpath = STRBUF_INIT;
301 struct strbuf class = STRBUF_INIT;
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200302 enum object_type type;
Lars Hjemlided93932007-05-11 12:12:48 +0200303 unsigned long size = 0;
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100304
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200305 name = xstrdup(pathname);
John Keepingfb3655d2013-04-06 10:28:57 +0100306 strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "",
307 ctx.qry.path ? "/" : "", name);
Lars Hjemli44947bf2007-06-17 01:23:08 +0200308
Lars Hjemli08a87572008-05-20 22:32:22 +0200309 if (!S_ISGITLINK(mode)) {
Christian Hesse255b78f2018-06-04 18:49:28 +0200310 type = oid_object_info(the_repository, oid, &size);
Lars Hjemli08a87572008-05-20 22:32:22 +0200311 if (type == OBJ_BAD) {
312 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
313 name,
Christian Hesse255b78f2018-06-04 18:49:28 +0200314 oid_to_hex(oid));
Christian Hesse896cd692015-10-09 13:15:44 +0200315 free(name);
Lars Hjemli08a87572008-05-20 22:32:22 +0200316 return 0;
317 }
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100318 }
Lars Hjemli44947bf2007-06-17 01:23:08 +0200319
Lars Hjemli426032f2007-06-17 13:17:00 +0200320 html("<tr><td class='ls-mode'>");
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +0100321 cgit_print_filemode(mode);
Lars Hjemli426032f2007-06-17 13:17:00 +0200322 html("</td><td>");
Jeffrey C. Olliee651cb02007-06-04 12:28:56 -0500323 if (S_ISGITLINK(mode)) {
Christian Hesse255b78f2018-06-04 18:49:28 +0200324 cgit_submodule_link("ls-mod", fullpath.buf, oid_to_hex(oid));
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200325 } else if (S_ISDIR(mode)) {
Christian Hesse255b78f2018-06-04 18:49:28 +0200326 write_tree_link(oid, name, walk_tree_ctx->curr_rev,
John Keeping000cf292016-07-13 20:19:42 +0100327 &fullpath);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100328 } else {
John Keepingfb3655d2013-04-06 10:28:57 +0100329 char *ext = strrchr(name, '.');
Andy Green83070692018-06-18 14:25:11 +0800330
John Keepingfb3655d2013-04-06 10:28:57 +0100331 strbuf_addstr(&class, "ls-blob");
332 if (ext)
333 strbuf_addf(&class, " %s", ext + 1);
Andy Green83070692018-06-18 14:25:11 +0800334
John Keepingfb3655d2013-04-06 10:28:57 +0100335 cgit_tree_link(name, NULL, class.buf, ctx.qry.head,
336 walk_tree_ctx->curr_rev, fullpath.buf);
Andy Green83070692018-06-18 14:25:11 +0800337
338 if (!walk_tree_ctx->inline_filename &&
339 string_list_has_string(&ctx.repo->inline_readme, name)) {
340 walk_tree_ctx->inline_filename = xstrdup(pathname);
341 oidcpy(&walk_tree_ctx->inline_oid, oid);
342 }
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100343 }
Lars Hjemli426032f2007-06-17 13:17:00 +0200344 htmlf("</td><td class='ls-size'>%li</td>", size);
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200345
Lars Hjemli48c487d2007-06-17 13:57:51 +0200346 html("<td>");
Lukas Fleischer210a5712013-03-03 17:22:30 +0100347 cgit_log_link("log", NULL, "button", ctx.qry.head,
John Keepingfb3655d2013-04-06 10:28:57 +0100348 walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL,
John Keeping30304d82015-08-12 15:55:28 +0100349 ctx.qry.showmsg, 0);
Lars Hjemli837d4642008-12-07 13:34:42 +0100350 if (ctx.repo->max_stats)
351 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
John Keepingfb3655d2013-04-06 10:28:57 +0100352 fullpath.buf);
Lars Hjemli6857bec2011-06-15 10:04:13 +0200353 if (!S_ISGITLINK(mode))
Lukas Fleischer210a5712013-03-03 17:22:30 +0100354 cgit_plain_link("plain", NULL, "button", ctx.qry.head,
John Keepingfb3655d2013-04-06 10:28:57 +0100355 walk_tree_ctx->curr_rev, fullpath.buf);
Jeff Smith1649afd2017-10-01 23:39:09 -0500356 if (!S_ISDIR(mode) && ctx.cfg.enable_blame)
357 cgit_blame_link("blame", NULL, "button", ctx.qry.head,
358 walk_tree_ctx->curr_rev, fullpath.buf);
Lars Hjemli48c487d2007-06-17 13:57:51 +0200359 html("</td></tr>\n");
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100360 free(name);
John Keepingfb3655d2013-04-06 10:28:57 +0100361 strbuf_release(&fullpath);
362 strbuf_release(&class);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100363 return 0;
364}
365
John Keepinge3d3fff2015-03-08 16:32:16 +0000366static void ls_head(void)
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100367{
John Keeping9c70c0b2015-08-14 12:47:20 +0100368 cgit_print_layout_start();
Lars Hjemli29154832007-11-11 13:04:28 +0100369 html("<table summary='tree listing' class='list'>\n");
Lars Hjemli777faf72007-01-28 00:39:26 +0100370 html("<tr class='nohover'>");
Lars Hjemlia5304282006-12-17 23:55:53 +0100371 html("<th class='left'>Mode</th>");
372 html("<th class='left'>Name</th>");
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100373 html("<th class='right'>Size</th>");
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200374 html("<th/>");
Lars Hjemlia5304282006-12-17 23:55:53 +0100375 html("</tr>\n");
Lars Hjemliffc69732007-06-16 20:20:42 +0200376}
377
Andy Green85cd2122018-06-18 14:24:20 +0800378static void ls_tail(struct walk_tree_context *walk_tree_ctx)
Lars Hjemliffc69732007-06-16 20:20:42 +0200379{
Andy Green83070692018-06-18 14:25:11 +0800380 struct cgit_filter *render;
381 enum object_type type;
382 char *buf, *mimetype;
383 unsigned long size;
384
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100385 html("</table>\n");
Andy Green83070692018-06-18 14:25:11 +0800386
387 if (!walk_tree_ctx->inline_filename)
388 goto done;
389
390 type = oid_object_info(the_repository, &walk_tree_ctx->inline_oid, &size);
391 if (type == OBJ_BAD)
392 goto done;
393
394 buf = read_object_file(&walk_tree_ctx->inline_oid, &type, &size);
395 if (!buf)
396 goto done;
397
398 /* create a vertical gap between tree nav / inline */
399 html("<table class=\"tabs\"><tr><td></td></tr></table>");
400
401 render = get_render_for_filename(walk_tree_ctx->inline_filename);
402 mimetype = render ? NULL : get_mimetype_for_filename(
403 walk_tree_ctx->inline_filename);
404
405 htmlf("<h2>%s</h2>", walk_tree_ctx->inline_filename);
406 html("<div class=blob>&nbsp;</div>\n");
407
408 if (render)
409 render_buffer(render, walk_tree_ctx->inline_filename,
410 buf, size);
411 else if (mimetype)
412 include_file(walk_tree_ctx->inline_filename, mimetype);
413 else
414 print_buffer(walk_tree_ctx->inline_filename, buf, size);
415
416 free(mimetype);
417 free(buf);
418
419done:
John Keeping9c70c0b2015-08-14 12:47:20 +0100420 cgit_print_layout_end();
Lars Hjemliffc69732007-06-16 20:20:42 +0200421}
422
Jeff Smith86a6d352017-08-09 19:02:56 -0500423static void ls_tree(const struct object_id *oid, char *path, struct walk_tree_context *walk_tree_ctx)
Lars Hjemliffc69732007-06-16 20:20:42 +0200424{
425 struct tree *tree;
John Keepingc1633c62013-03-02 12:32:11 +0000426 struct pathspec paths = {
427 .nr = 0
428 };
Lars Hjemliffc69732007-06-16 20:20:42 +0200429
Jeff Smith86a6d352017-08-09 19:02:56 -0500430 tree = parse_tree_indirect(oid);
Lars Hjemliffc69732007-06-16 20:20:42 +0200431 if (!tree) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100432 cgit_print_error_page(404, "Not found",
Christian Hesse255b78f2018-06-04 18:49:28 +0200433 "Not a tree object: %s", oid_to_hex(oid));
Lars Hjemliffc69732007-06-16 20:20:42 +0200434 return;
435 }
436
437 ls_head();
Lukas Fleischer210a5712013-03-03 17:22:30 +0100438 read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
Andy Green85cd2122018-06-18 14:24:20 +0800439 ls_tail(walk_tree_ctx);
Lars Hjemliffc69732007-06-16 20:20:42 +0200440}
441
442
Christian Hesse255b78f2018-06-04 18:49:28 +0200443static int walk_tree(const struct object_id *oid, struct strbuf *base,
Christian Hesse7358f632015-02-07 14:18:28 +0100444 const char *pathname, unsigned mode, int stage, void *cbdata)
Lars Hjemliffc69732007-06-16 20:20:42 +0200445{
Lukas Fleischer210a5712013-03-03 17:22:30 +0100446 struct walk_tree_context *walk_tree_ctx = cbdata;
Lars Hjemliffc69732007-06-16 20:20:42 +0200447
Lukas Fleischer210a5712013-03-03 17:22:30 +0100448 if (walk_tree_ctx->state == 0) {
John Keepinge18a85b2016-08-13 11:54:46 +0100449 struct strbuf buffer = STRBUF_INIT;
450
451 strbuf_addbuf(&buffer, base);
452 strbuf_addstr(&buffer, pathname);
453 if (strcmp(walk_tree_ctx->match_path, buffer.buf))
Lars Hjemliffc69732007-06-16 20:20:42 +0200454 return READ_TREE_RECURSIVE;
455
456 if (S_ISDIR(mode)) {
Lukas Fleischer210a5712013-03-03 17:22:30 +0100457 walk_tree_ctx->state = 1;
Jeff Smith9337c7e2017-10-01 23:39:06 -0500458 cgit_set_title_from_path(buffer.buf);
John Keepinge18a85b2016-08-13 11:54:46 +0100459 strbuf_release(&buffer);
Lars Hjemliffc69732007-06-16 20:20:42 +0200460 ls_head();
461 return READ_TREE_RECURSIVE;
462 } else {
John Keeping9c70c0b2015-08-14 12:47:20 +0100463 walk_tree_ctx->state = 2;
John Keeping8dced022018-06-18 14:23:45 +0800464 print_object(oid, buffer.buf, pathname, walk_tree_ctx->curr_rev,
465 walk_tree_ctx->use_render);
John Keepinge18a85b2016-08-13 11:54:46 +0100466 strbuf_release(&buffer);
John Keeping8dced022018-06-18 14:23:45 +0800467
Lars Hjemliffc69732007-06-16 20:20:42 +0200468 return 0;
469 }
470 }
Christian Hesse255b78f2018-06-04 18:49:28 +0200471 ls_item(oid, base, pathname, mode, stage, walk_tree_ctx);
Lars Hjemliffc69732007-06-16 20:20:42 +0200472 return 0;
473}
474
Lars Hjemliffc69732007-06-16 20:20:42 +0200475/*
476 * Show a tree or a blob
477 * rev: the commit pointing at the root tree object
478 * path: path to tree or blob
479 */
John Keeping8dced022018-06-18 14:23:45 +0800480void cgit_print_tree(const char *rev, char *path, bool use_render)
Lars Hjemliffc69732007-06-16 20:20:42 +0200481{
Christian Hesse9dd3c5e2016-09-29 22:17:07 +0200482 struct object_id oid;
Lars Hjemliffc69732007-06-16 20:20:42 +0200483 struct commit *commit;
John Keepingc1633c62013-03-02 12:32:11 +0000484 struct pathspec_item path_items = {
485 .match = path,
486 .len = path ? strlen(path) : 0
487 };
488 struct pathspec paths = {
489 .nr = path ? 1 : 0,
490 .items = &path_items
491 };
Lukas Fleischer210a5712013-03-03 17:22:30 +0100492 struct walk_tree_context walk_tree_ctx = {
493 .match_path = path,
John Keeping8dced022018-06-18 14:23:45 +0800494 .state = 0,
495 .use_render = use_render,
Lukas Fleischer210a5712013-03-03 17:22:30 +0100496 };
Lars Hjemliffc69732007-06-16 20:20:42 +0200497
498 if (!rev)
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100499 rev = ctx.qry.head;
Lars Hjemliffc69732007-06-16 20:20:42 +0200500
Christian Hesse9dd3c5e2016-09-29 22:17:07 +0200501 if (get_oid(rev, &oid)) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100502 cgit_print_error_page(404, "Not found",
503 "Invalid revision name: %s", rev);
Lars Hjemliffc69732007-06-16 20:20:42 +0200504 return;
505 }
Jeff Smith86a6d352017-08-09 19:02:56 -0500506 commit = lookup_commit_reference(&oid);
Lars Hjemliffc69732007-06-16 20:20:42 +0200507 if (!commit || parse_commit(commit)) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100508 cgit_print_error_page(404, "Not found",
509 "Invalid commit reference: %s", rev);
Lars Hjemliffc69732007-06-16 20:20:42 +0200510 return;
511 }
512
Lukas Fleischer985d6ca2013-03-04 13:25:36 +0100513 walk_tree_ctx.curr_rev = xstrdup(rev);
514
Lars Hjemliffc69732007-06-16 20:20:42 +0200515 if (path == NULL) {
Christian Hesse255b78f2018-06-04 18:49:28 +0200516 ls_tree(&commit->maybe_tree->object.oid, NULL, &walk_tree_ctx);
Lukas Fleischer985d6ca2013-03-04 13:25:36 +0100517 goto cleanup;
Lars Hjemliffc69732007-06-16 20:20:42 +0200518 }
519
Christian Hesse255b78f2018-06-04 18:49:28 +0200520 read_tree_recursive(commit->maybe_tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
Lukas Fleischer210a5712013-03-03 17:22:30 +0100521 if (walk_tree_ctx.state == 1)
Andy Green85cd2122018-06-18 14:24:20 +0800522 ls_tail(&walk_tree_ctx);
John Keeping9c70c0b2015-08-14 12:47:20 +0100523 else if (walk_tree_ctx.state == 2)
524 cgit_print_layout_end();
525 else
526 cgit_print_error_page(404, "Not found", "Path not found");
Lukas Fleischer985d6ca2013-03-04 13:25:36 +0100527
528cleanup:
529 free(walk_tree_ctx.curr_rev);
Andy Green83070692018-06-18 14:25:11 +0800530 if (walk_tree_ctx.inline_filename)
531 free(walk_tree_ctx.inline_filename);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100532}