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)
12 #include "ui-shared.h"
19 static void print_text_buffer(const char *name, char *buf, unsigned long size)
21 unsigned long lineno, idx;
22 const char *numberfmt =
23 "<a class='no' id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a>\n";
25 html("<table summary='blob content' class='blob'>\n");
27 if (ctx.cfg.enable_tree_linenumbers) {
28 html("<tr><td class='linenumbers'><pre>");
33 htmlf(numberfmt, ++lineno);
34 while (idx < size - 1) { // skip absolute last newline
36 htmlf(numberfmt, ++lineno);
40 html("</pre></td>\n");
46 if (ctx.repo->source_filter) {
47 html("<td class='lines'><pre><code>");
48 ctx.repo->source_filter->argv[1] = xstrdup(name);
49 cgit_open_filter(ctx.repo->source_filter);
51 cgit_close_filter(ctx.repo->source_filter);
52 free(ctx.repo->source_filter->argv[1]);
53 ctx.repo->source_filter->argv[1] = NULL;
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(fmt("Bad object name: %s",
101 buf = read_sha1_file(sha1, &type, &size);
103 cgit_print_error(fmt("Error reading object %s",
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,
133 enum object_type type;
134 unsigned long size = 0;
136 name = xstrdup(pathname);
137 fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "",
138 ctx.qry.path ? "/" : "", name);
140 if (!S_ISGITLINK(mode)) {
141 type = sha1_object_info(sha1, &size);
142 if (type == OBJ_BAD) {
143 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
150 html("<tr><td class='ls-mode'>");
151 cgit_print_filemode(mode);
153 if (S_ISGITLINK(mode)) {
154 cgit_submodule_link("ls-mod", fullpath, sha1_to_hex(sha1));
155 } else if (S_ISDIR(mode)) {
156 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
159 class = strrchr(name, '.');
161 class = fmt("ls-blob %s", class + 1);
164 cgit_tree_link(name, NULL, class, ctx.qry.head,
167 htmlf("</td><td class='ls-size'>%li</td>", size);
170 cgit_log_link("log", NULL, "button", ctx.qry.head, curr_rev,
171 fullpath, 0, NULL, NULL, ctx.qry.showmsg);
172 if (ctx.repo->max_stats)
173 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
175 if (!S_ISGITLINK(mode))
176 cgit_plain_link("plain", NULL, "button", ctx.qry.head, curr_rev,
178 html("</td></tr>\n");
183 static void ls_head()
185 html("<table summary='tree listing' class='list'>\n");
186 html("<tr class='nohover'>");
187 html("<th class='left'>Mode</th>");
188 html("<th class='left'>Name</th>");
189 html("<th class='right'>Size</th>");
195 static void ls_tail()
203 static void ls_tree(const unsigned char *sha1, char *path)
206 struct pathspec paths = {
210 tree = parse_tree_indirect(sha1);
212 cgit_print_error(fmt("Not a tree object: %s",
218 read_tree_recursive(tree, "", 0, 1, &paths, ls_item, NULL);
223 static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
224 const char *pathname, unsigned mode, int stage,
227 static char buffer[PATH_MAX];
230 memcpy(buffer, base, baselen);
231 strcpy(buffer + baselen, pathname);
232 if (strcmp(match_path, buffer))
233 return READ_TREE_RECURSIVE;
238 return READ_TREE_RECURSIVE;
240 print_object(sha1, buffer, pathname, curr_rev);
244 ls_item(sha1, base, baselen, pathname, mode, stage, NULL);
250 * Show a tree or a blob
251 * rev: the commit pointing at the root tree object
252 * path: path to tree or blob
254 void cgit_print_tree(const char *rev, char *path)
256 unsigned char sha1[20];
257 struct commit *commit;
258 struct pathspec_item path_items = {
260 .len = path ? strlen(path) : 0
262 struct pathspec paths = {
270 curr_rev = xstrdup(rev);
271 if (get_sha1(rev, sha1)) {
272 cgit_print_error(fmt("Invalid revision name: %s", rev));
275 commit = lookup_commit_reference(sha1);
276 if (!commit || parse_commit(commit)) {
277 cgit_print_error(fmt("Invalid commit reference: %s", rev));
282 ls_tree(commit->tree->object.sha1, NULL);
288 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, NULL);