1 /* ui-tree.c: functions for tree output
3 * Copyright (C) 2006-2017 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
9 #define USE_THE_REPOSITORY_VARIABLE
14 #include "ui-shared.h"
16 struct walk_tree_context {
22 static void print_text_buffer(const char *name, char *buf, unsigned long size)
24 unsigned long lineno, idx;
25 const char *numberfmt = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
27 html("<table summary='blob content' class='blob'>\n");
29 if (ctx.cfg.enable_tree_linenumbers) {
30 html("<tr><td class='linenumbers'><pre>");
35 htmlf(numberfmt, ++lineno);
36 while (idx < size - 1) { // skip absolute last newline
38 htmlf(numberfmt, ++lineno);
42 html("</pre></td>\n");
48 if (ctx.repo->source_filter) {
49 char *filter_arg = xstrdup(name);
50 html("<td class='lines'><pre><code>");
51 cgit_open_filter(ctx.repo->source_filter, filter_arg);
53 cgit_close_filter(ctx.repo->source_filter);
55 html("</code></pre></td></tr></table>\n");
59 html("<td class='lines'><pre><code>");
61 html("</code></pre></td></tr></table>\n");
66 static void print_binary_buffer(char *buf, unsigned long size)
68 unsigned long ofs, idx;
69 static char ascii[ROWLEN + 1];
71 html("<table summary='blob content' class='bin-blob'>\n");
72 html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
73 for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
74 htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs);
75 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
77 idx == 16 ? 4 : 1, "",
79 html(" </td><td class='hex'>");
80 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
81 ascii[idx] = isgraph(buf[idx]) ? buf[idx] : '.';
89 static void print_object(const struct object_id *oid, const char *path, const char *basename, const char *rev)
91 enum object_type type;
96 type = oid_object_info(the_repository, oid, &size);
97 if (type == OBJ_BAD) {
98 cgit_print_error_page(404, "Not found",
99 "Bad object name: %s", oid_to_hex(oid));
103 buf = repo_read_object_file(the_repository, oid, &type, &size);
105 cgit_print_error_page(500, "Internal server error",
106 "Error reading object %s", oid_to_hex(oid));
109 is_binary = buffer_is_binary(buf, size);
111 cgit_set_title_from_path(path);
113 cgit_print_layout_start();
114 htmlf("blob: %s (", oid_to_hex(oid));
115 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
117 if (ctx.repo->enable_blame && !is_binary) {
119 cgit_blame_link("blame", NULL, NULL, ctx.qry.head,
124 if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
125 htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
126 size / 1024, ctx.cfg.max_blob_size);
131 print_binary_buffer(buf, size);
133 print_text_buffer(basename, buf, size);
138 struct single_tree_ctx {
140 struct object_id oid;
145 static int single_tree_cb(const struct object_id *oid, struct strbuf *base,
146 const char *pathname, unsigned mode, void *cbdata)
148 struct single_tree_ctx *ctx = cbdata;
150 if (++ctx->count > 1)
153 if (!S_ISDIR(mode)) {
158 ctx->name = xstrdup(pathname);
159 oidcpy(&ctx->oid, oid);
160 strbuf_addf(ctx->path, "/%s", pathname);
164 static void write_tree_link(const struct object_id *oid, char *name,
165 char *rev, struct strbuf *fullpath)
167 size_t initial_length = fullpath->len;
169 struct single_tree_ctx tree_ctx = {
173 struct pathspec paths = {
177 oidcpy(&tree_ctx.oid, oid);
179 while (tree_ctx.count == 1) {
180 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head, rev,
183 tree = lookup_tree(the_repository, &tree_ctx.oid);
188 tree_ctx.name = NULL;
191 read_tree(the_repository, tree, &paths, single_tree_cb, &tree_ctx);
193 if (tree_ctx.count != 1)
197 name = tree_ctx.name;
200 strbuf_setlen(fullpath, initial_length);
203 static int ls_item(const struct object_id *oid, struct strbuf *base,
204 const char *pathname, unsigned mode, void *cbdata)
206 struct walk_tree_context *walk_tree_ctx = cbdata;
208 struct strbuf fullpath = STRBUF_INIT;
209 struct strbuf linkpath = STRBUF_INIT;
210 struct strbuf class = STRBUF_INIT;
211 enum object_type type;
212 unsigned long size = 0;
215 name = xstrdup(pathname);
216 strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "",
217 ctx.qry.path ? "/" : "", name);
219 if (!S_ISGITLINK(mode)) {
220 type = oid_object_info(the_repository, oid, &size);
221 if (type == OBJ_BAD) {
222 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
229 html("<tr><td class='ls-mode'>");
230 cgit_print_filemode(mode);
232 if (S_ISGITLINK(mode)) {
233 cgit_submodule_link("ls-mod", fullpath.buf, oid_to_hex(oid));
234 } else if (S_ISDIR(mode)) {
235 write_tree_link(oid, name, walk_tree_ctx->curr_rev,
238 char *ext = strrchr(name, '.');
239 strbuf_addstr(&class, "ls-blob");
241 strbuf_addf(&class, " %s", ext + 1);
242 cgit_tree_link(name, NULL, class.buf, ctx.qry.head,
243 walk_tree_ctx->curr_rev, fullpath.buf);
247 buf = repo_read_object_file(the_repository, oid, &type, &size);
249 htmlf("Error reading object: %s", oid_to_hex(oid));
252 strbuf_addbuf(&linkpath, &fullpath);
253 strbuf_addf(&linkpath, "/../%s", buf);
254 strbuf_normalize_path(&linkpath);
255 cgit_tree_link(buf, NULL, class.buf, ctx.qry.head,
256 walk_tree_ctx->curr_rev, linkpath.buf);
258 strbuf_release(&linkpath);
260 htmlf("</td><td class='ls-size'>%li</td>", size);
263 cgit_log_link("log", NULL, "button", ctx.qry.head,
264 walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL,
266 if (ctx.repo->max_stats)
267 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
269 if (!S_ISGITLINK(mode))
270 cgit_plain_link("plain", NULL, "button", ctx.qry.head,
271 walk_tree_ctx->curr_rev, fullpath.buf);
272 if (!S_ISDIR(mode) && ctx.repo->enable_blame)
273 cgit_blame_link("blame", NULL, "button", ctx.qry.head,
274 walk_tree_ctx->curr_rev, fullpath.buf);
275 html("</td></tr>\n");
279 strbuf_release(&fullpath);
280 strbuf_release(&class);
284 static void ls_head(void)
286 cgit_print_layout_start();
287 html("<table summary='tree listing' class='list'>\n");
288 html("<tr class='nohover'>");
289 html("<th class='left'>Mode</th>");
290 html("<th class='left'>Name</th>");
291 html("<th class='right'>Size</th>");
296 static void ls_tail(void)
299 cgit_print_layout_end();
302 static void ls_tree(const struct object_id *oid, const char *path, struct walk_tree_context *walk_tree_ctx)
305 struct pathspec paths = {
309 tree = parse_tree_indirect(oid);
311 cgit_print_error_page(404, "Not found",
312 "Not a tree object: %s", oid_to_hex(oid));
317 read_tree(the_repository, tree, &paths, ls_item, walk_tree_ctx);
322 static int walk_tree(const struct object_id *oid, struct strbuf *base,
323 const char *pathname, unsigned mode, void *cbdata)
325 struct walk_tree_context *walk_tree_ctx = cbdata;
327 if (walk_tree_ctx->state == 0) {
328 struct strbuf buffer = STRBUF_INIT;
330 strbuf_addbuf(&buffer, base);
331 strbuf_addstr(&buffer, pathname);
332 if (strcmp(walk_tree_ctx->match_path, buffer.buf))
333 return READ_TREE_RECURSIVE;
336 walk_tree_ctx->state = 1;
337 cgit_set_title_from_path(buffer.buf);
338 strbuf_release(&buffer);
340 return READ_TREE_RECURSIVE;
342 walk_tree_ctx->state = 2;
343 print_object(oid, buffer.buf, pathname, walk_tree_ctx->curr_rev);
344 strbuf_release(&buffer);
348 ls_item(oid, base, pathname, mode, walk_tree_ctx);
353 * Show a tree or a blob
354 * rev: the commit pointing at the root tree object
355 * path: path to tree or blob
357 void cgit_print_tree(const char *rev, char *path)
359 struct object_id oid;
360 struct commit *commit;
361 struct pathspec_item path_items = {
363 .len = path ? strlen(path) : 0
365 struct pathspec paths = {
369 struct walk_tree_context walk_tree_ctx = {
377 if (repo_get_oid(the_repository, rev, &oid)) {
378 cgit_print_error_page(404, "Not found",
379 "Invalid revision name: %s", rev);
382 commit = lookup_commit_reference(the_repository, &oid);
383 if (!commit || repo_parse_commit(the_repository, commit)) {
384 cgit_print_error_page(404, "Not found",
385 "Invalid commit reference: %s", rev);
389 walk_tree_ctx.curr_rev = xstrdup(rev);
392 ls_tree(get_commit_tree_oid(commit), NULL, &walk_tree_ctx);
396 read_tree(the_repository, repo_get_commit_tree(the_repository, commit),
397 &paths, walk_tree, &walk_tree_ctx);
398 if (walk_tree_ctx.state == 1)
400 else if (walk_tree_ctx.state == 2)
401 cgit_print_layout_end();
403 cgit_print_error_page(404, "Not found", "Path not found");
406 free(walk_tree_ctx.curr_rev);