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"
18 static void print_text_buffer(char *buf, unsigned long size)
20 unsigned long lineno, idx;
21 const char *numberfmt =
22 "<a class='no' id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a>\n";
24 html("<table summary='blob content' class='blob'>\n");
25 html("<tr><td class='linenumbers'><pre>");
28 htmlf(numberfmt, ++lineno);
29 while(idx < size - 1) { // skip absolute last newline
31 htmlf(numberfmt, ++lineno);
34 html("</pre></td>\n");
35 html("<td class='lines'><pre><code>");
37 html("</code></pre></td></tr></table>\n");
42 static void print_binary_buffer(char *buf, unsigned long size)
44 unsigned long ofs, idx;
45 static char ascii[ROWLEN + 1];
47 html("<table summary='blob content' class='bin-blob'>\n");
48 html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
49 for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
50 htmlf("<tr><td class='right'>%04x</td><td class='hex'>", ofs);
51 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
53 idx == 16 ? 4 : 1, "",
55 html(" </td><td class='hex'>");
56 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
57 ascii[idx] = isgraph(buf[idx]) ? buf[idx] : '.';
65 static void print_object(const unsigned char *sha1, char *path)
67 enum object_type type;
71 type = sha1_object_info(sha1, &size);
72 if (type == OBJ_BAD) {
73 cgit_print_error(fmt("Bad object name: %s",
78 buf = read_sha1_file(sha1, &type, &size);
80 cgit_print_error(fmt("Error reading object %s",
86 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
88 htmlf(")<br/>blob: %s\n", sha1_to_hex(sha1));
90 if (buffer_is_binary(buf, size))
91 print_binary_buffer(buf, size);
93 print_text_buffer(buf, size);
97 static int ls_item(const unsigned char *sha1, const char *base, int baselen,
98 const char *pathname, unsigned int mode, int stage,
103 enum object_type type;
104 unsigned long size = 0;
106 name = xstrdup(pathname);
107 fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "",
108 ctx.qry.path ? "/" : "", name);
110 if (!S_ISGITLINK(mode)) {
111 type = sha1_object_info(sha1, &size);
112 if (type == OBJ_BAD) {
113 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
120 html("<tr><td class='ls-mode'>");
121 cgit_print_filemode(mode);
123 if (S_ISGITLINK(mode)) {
124 htmlf("<a class='ls-mod' href='");
125 html_attr(fmt(ctx.repo->module_link,
131 } else if (S_ISDIR(mode)) {
132 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
135 cgit_tree_link(name, NULL, "ls-blob", ctx.qry.head,
138 htmlf("</td><td class='ls-size'>%li</td>", size);
141 cgit_log_link("log", NULL, "button", ctx.qry.head, curr_rev,
142 fullpath, 0, NULL, NULL, ctx.qry.showmsg);
143 if (ctx.repo->max_stats)
144 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
146 html("</td></tr>\n");
151 static void ls_head()
153 html("<table summary='tree listing' class='list'>\n");
154 html("<tr class='nohover'>");
155 html("<th class='left'>Mode</th>");
156 html("<th class='left'>Name</th>");
157 html("<th class='right'>Size</th>");
163 static void ls_tail()
171 static void ls_tree(const unsigned char *sha1, char *path)
175 tree = parse_tree_indirect(sha1);
177 cgit_print_error(fmt("Not a tree object: %s",
183 read_tree_recursive(tree, "", 0, 1, NULL, ls_item, NULL);
188 static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
189 const char *pathname, unsigned mode, int stage,
193 static char buffer[PATH_MAX];
197 memcpy(buffer, base, baselen);
198 strcpy(buffer+baselen, pathname);
199 url = cgit_pageurl(ctx.qry.repo, "tree",
200 fmt("h=%s&path=%s", curr_rev, buffer));
202 cgit_tree_link(xstrdup(pathname), NULL, NULL, ctx.qry.head,
205 if (strcmp(match_path, buffer))
206 return READ_TREE_RECURSIVE;
211 return READ_TREE_RECURSIVE;
213 print_object(sha1, buffer);
217 ls_item(sha1, base, baselen, pathname, mode, stage, NULL);
223 * Show a tree or a blob
224 * rev: the commit pointing at the root tree object
225 * path: path to tree or blob
227 void cgit_print_tree(const char *rev, char *path)
229 unsigned char sha1[20];
230 struct commit *commit;
231 const char *paths[] = {path, NULL};
236 curr_rev = xstrdup(rev);
237 if (get_sha1(rev, sha1)) {
238 cgit_print_error(fmt("Invalid revision name: %s", rev));
241 commit = lookup_commit_reference(sha1);
242 if (!commit || parse_commit(commit)) {
243 cgit_print_error(fmt("Invalid commit reference: %s", rev));
247 html("path: <a href='");
248 html_attr(cgit_pageurl(ctx.qry.repo, "tree", fmt("h=%s", rev)));
252 ls_tree(commit->tree->object.sha1, NULL);
257 read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL);