1 /* ui-tree.c: functions for tree output
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
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 = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
26 html("<table summary='blob content' class='blob'>\n");
28 if (ctx.cfg.enable_tree_linenumbers) {
29 html("<tr><td class='linenumbers'><pre>");
34 htmlf(numberfmt, ++lineno);
35 while (idx < size - 1) { // skip absolute last newline
37 htmlf(numberfmt, ++lineno);
41 html("</pre></td>\n");
47 if (ctx.repo->source_filter) {
48 char *filter_arg = xstrdup(name);
49 html("<td class='lines'><pre><code>");
50 cgit_open_filter(ctx.repo->source_filter, filter_arg);
52 cgit_close_filter(ctx.repo->source_filter);
54 html("</code></pre></td></tr></table>\n");
58 html("<td class='lines'><pre><code>");
60 html("</code></pre></td></tr></table>\n");
65 static void print_binary_buffer(char *buf, unsigned long size)
67 unsigned long ofs, idx;
68 static char ascii[ROWLEN + 1];
70 html("<table summary='blob content' class='bin-blob'>\n");
71 html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
72 for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
73 htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs);
74 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
76 idx == 16 ? 4 : 1, "",
78 html(" </td><td class='hex'>");
79 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
80 ascii[idx] = isgraph(buf[idx]) ? buf[idx] : '.';
88 static void print_object(const unsigned char *sha1, char *path, const char *basename, const char *rev)
90 enum object_type type;
94 type = sha1_object_info(sha1, &size);
95 if (type == OBJ_BAD) {
96 cgit_print_error("Bad object name: %s", sha1_to_hex(sha1));
100 buf = read_sha1_file(sha1, &type, &size);
102 cgit_print_error("Error reading object %s", sha1_to_hex(sha1));
106 htmlf("blob: %s (", sha1_to_hex(sha1));
107 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
111 if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
112 htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
113 size / 1024, ctx.cfg.max_blob_size);
117 if (buffer_is_binary(buf, size))
118 print_binary_buffer(buf, size);
120 print_text_buffer(basename, buf, size);
124 static int ls_item(const unsigned char *sha1, const char *base, int baselen,
125 const char *pathname, unsigned int mode, int stage,
128 struct walk_tree_context *walk_tree_ctx = cbdata;
130 struct strbuf fullpath = STRBUF_INIT;
131 struct strbuf class = STRBUF_INIT;
132 enum object_type type;
133 unsigned long size = 0;
135 name = xstrdup(pathname);
136 strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "",
137 ctx.qry.path ? "/" : "", name);
139 if (!S_ISGITLINK(mode)) {
140 type = sha1_object_info(sha1, &size);
141 if (type == OBJ_BAD) {
142 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
149 html("<tr><td class='ls-mode'>");
150 cgit_print_filemode(mode);
152 if (S_ISGITLINK(mode)) {
153 cgit_submodule_link("ls-mod", fullpath.buf, sha1_to_hex(sha1));
154 } else if (S_ISDIR(mode)) {
155 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
156 walk_tree_ctx->curr_rev, fullpath.buf);
158 char *ext = strrchr(name, '.');
159 strbuf_addstr(&class, "ls-blob");
161 strbuf_addf(&class, " %s", ext + 1);
162 cgit_tree_link(name, NULL, class.buf, ctx.qry.head,
163 walk_tree_ctx->curr_rev, fullpath.buf);
165 htmlf("</td><td class='ls-size'>%li</td>", size);
168 cgit_log_link("log", NULL, "button", ctx.qry.head,
169 walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL,
171 if (ctx.repo->max_stats)
172 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
174 if (!S_ISGITLINK(mode))
175 cgit_plain_link("plain", NULL, "button", ctx.qry.head,
176 walk_tree_ctx->curr_rev, fullpath.buf);
177 html("</td></tr>\n");
179 strbuf_release(&fullpath);
180 strbuf_release(&class);
184 static void ls_head()
186 html("<table summary='tree listing' class='list'>\n");
187 html("<tr class='nohover'>");
188 html("<th class='left'>Mode</th>");
189 html("<th class='left'>Name</th>");
190 html("<th class='right'>Size</th>");
195 static void ls_tail()
200 static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_context *walk_tree_ctx)
203 struct pathspec paths = {
207 tree = parse_tree_indirect(sha1);
209 cgit_print_error("Not a tree object: %s", sha1_to_hex(sha1));
214 read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
219 static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
220 const char *pathname, unsigned mode, int stage,
223 struct walk_tree_context *walk_tree_ctx = cbdata;
224 static char buffer[PATH_MAX];
226 if (walk_tree_ctx->state == 0) {
227 memcpy(buffer, base, baselen);
228 strcpy(buffer + baselen, pathname);
229 if (strcmp(walk_tree_ctx->match_path, buffer))
230 return READ_TREE_RECURSIVE;
233 walk_tree_ctx->state = 1;
235 return READ_TREE_RECURSIVE;
237 print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev);
241 ls_item(sha1, base, baselen, pathname, mode, stage, walk_tree_ctx);
247 * Show a tree or a blob
248 * rev: the commit pointing at the root tree object
249 * path: path to tree or blob
251 void cgit_print_tree(const char *rev, char *path)
253 unsigned char sha1[20];
254 struct commit *commit;
255 struct pathspec_item path_items = {
257 .len = path ? strlen(path) : 0
259 struct pathspec paths = {
263 struct walk_tree_context walk_tree_ctx = {
271 if (get_sha1(rev, sha1)) {
272 cgit_print_error("Invalid revision name: %s", rev);
275 commit = lookup_commit_reference(sha1);
276 if (!commit || parse_commit(commit)) {
277 cgit_print_error("Invalid commit reference: %s", rev);
281 walk_tree_ctx.curr_rev = xstrdup(rev);
284 ls_tree(commit->tree->object.sha1, NULL, &walk_tree_ctx);
288 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
289 if (walk_tree_ctx.state == 1)
293 free(walk_tree_ctx.curr_rev);