1 /* ui-tree.c: functions for tree output
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
13 #include "ui-shared.h"
15 struct walk_tree_context {
21 static void print_text_buffer(const char *name, char *buf, unsigned long size)
23 unsigned long lineno, idx;
24 const char *numberfmt =
25 "<a class='no' id='n%1$d' name='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 html("<td class='lines'><pre><code>");
50 ctx.repo->source_filter->argv[1] = xstrdup(name);
51 cgit_open_filter(ctx.repo->source_filter);
53 cgit_close_filter(ctx.repo->source_filter);
54 free(ctx.repo->source_filter->argv[1]);
55 ctx.repo->source_filter->argv[1] = NULL;
56 html("</code></pre></td></tr></table>\n");
60 html("<td class='lines'><pre><code>");
62 html("</code></pre></td></tr></table>\n");
67 static void print_binary_buffer(char *buf, unsigned long size)
69 unsigned long ofs, idx;
70 static char ascii[ROWLEN + 1];
72 html("<table summary='blob content' class='bin-blob'>\n");
73 html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
74 for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
75 htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs);
76 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
78 idx == 16 ? 4 : 1, "",
80 html(" </td><td class='hex'>");
81 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
82 ascii[idx] = isgraph(buf[idx]) ? buf[idx] : '.';
90 static void print_object(const unsigned char *sha1, char *path, const char *basename, const char *rev)
92 enum object_type type;
96 type = sha1_object_info(sha1, &size);
97 if (type == OBJ_BAD) {
98 cgit_print_error("Bad object name: %s", sha1_to_hex(sha1));
102 buf = read_sha1_file(sha1, &type, &size);
104 cgit_print_error("Error reading object %s", sha1_to_hex(sha1));
108 htmlf("blob: %s (", sha1_to_hex(sha1));
109 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
113 if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
114 htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
115 size / 1024, ctx.cfg.max_blob_size);
119 if (buffer_is_binary(buf, size))
120 print_binary_buffer(buf, size);
122 print_text_buffer(basename, buf, size);
126 static int ls_item(const unsigned char *sha1, const char *base, int baselen,
127 const char *pathname, unsigned int mode, int stage,
130 struct walk_tree_context *walk_tree_ctx = cbdata;
134 enum object_type type;
135 unsigned long size = 0;
137 name = xstrdup(pathname);
138 fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "",
139 ctx.qry.path ? "/" : "", name);
141 if (!S_ISGITLINK(mode)) {
142 type = sha1_object_info(sha1, &size);
143 if (type == OBJ_BAD) {
144 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
151 html("<tr><td class='ls-mode'>");
152 cgit_print_filemode(mode);
154 if (S_ISGITLINK(mode)) {
155 cgit_submodule_link("ls-mod", fullpath, sha1_to_hex(sha1));
156 } else if (S_ISDIR(mode)) {
157 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
158 walk_tree_ctx->curr_rev, fullpath);
160 class = strrchr(name, '.');
162 class = fmt("ls-blob %s", class + 1);
165 cgit_tree_link(name, NULL, class, ctx.qry.head,
166 walk_tree_ctx->curr_rev, fullpath);
168 htmlf("</td><td class='ls-size'>%li</td>", size);
171 cgit_log_link("log", NULL, "button", ctx.qry.head,
172 walk_tree_ctx->curr_rev, fullpath, 0, NULL, NULL,
174 if (ctx.repo->max_stats)
175 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
177 if (!S_ISGITLINK(mode))
178 cgit_plain_link("plain", NULL, "button", ctx.qry.head,
179 walk_tree_ctx->curr_rev, fullpath);
180 html("</td></tr>\n");
185 static void ls_head()
187 html("<table summary='tree listing' class='list'>\n");
188 html("<tr class='nohover'>");
189 html("<th class='left'>Mode</th>");
190 html("<th class='left'>Name</th>");
191 html("<th class='right'>Size</th>");
196 static void ls_tail()
201 static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_context *walk_tree_ctx)
204 struct pathspec paths = {
208 tree = parse_tree_indirect(sha1);
210 cgit_print_error("Not a tree object: %s", sha1_to_hex(sha1));
215 read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
220 static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
221 const char *pathname, unsigned mode, int stage,
224 struct walk_tree_context *walk_tree_ctx = cbdata;
225 static char buffer[PATH_MAX];
227 if (walk_tree_ctx->state == 0) {
228 memcpy(buffer, base, baselen);
229 strcpy(buffer + baselen, pathname);
230 if (strcmp(walk_tree_ctx->match_path, buffer))
231 return READ_TREE_RECURSIVE;
234 walk_tree_ctx->state = 1;
236 return READ_TREE_RECURSIVE;
238 print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev);
242 ls_item(sha1, base, baselen, pathname, mode, stage, walk_tree_ctx);
248 * Show a tree or a blob
249 * rev: the commit pointing at the root tree object
250 * path: path to tree or blob
252 void cgit_print_tree(const char *rev, char *path)
254 unsigned char sha1[20];
255 struct commit *commit;
256 struct pathspec_item path_items = {
258 .len = path ? strlen(path) : 0
260 struct pathspec paths = {
264 struct walk_tree_context walk_tree_ctx = {
272 if (get_sha1(rev, sha1)) {
273 cgit_print_error("Invalid revision name: %s", rev);
276 commit = lookup_commit_reference(sha1);
277 if (!commit || parse_commit(commit)) {
278 cgit_print_error("Invalid commit reference: %s", rev);
282 walk_tree_ctx.curr_rev = xstrdup(rev);
285 ls_tree(commit->tree->object.sha1, NULL, &walk_tree_ctx);
289 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
290 if (walk_tree_ctx.state == 1)
294 free(walk_tree_ctx.curr_rev);