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");
40 static void print_binary_buffer(char *buf, unsigned long size)
42 unsigned long ofs, idx;
44 html("<table summary='blob content' class='bin-blob'>\n");
45 html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
46 for (ofs = 0; ofs < size; ofs += 32, buf += 32) {
47 htmlf("<tr><td class='right'>%04x</td><td class='hex'>", ofs);
48 for (idx = 0; idx < 32 && ofs + idx < size; idx++)
50 idx == 16 ? 4 : 1, "",
52 html(" </td><td class='hex'>");
53 for (idx = 0; idx < 32 && ofs + idx < size; idx++)
54 htmlf("%c", isgraph(buf[idx]) ? buf[idx] : '.');
60 static void print_object(const unsigned char *sha1, char *path)
62 enum object_type type;
66 type = sha1_object_info(sha1, &size);
67 if (type == OBJ_BAD) {
68 cgit_print_error(fmt("Bad object name: %s",
73 buf = read_sha1_file(sha1, &type, &size);
75 cgit_print_error(fmt("Error reading object %s",
81 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
83 htmlf(")<br/>blob: %s\n", sha1_to_hex(sha1));
85 if (buffer_is_binary(buf, size))
86 print_binary_buffer(buf, size);
88 print_text_buffer(buf, size);
92 static int ls_item(const unsigned char *sha1, const char *base, int baselen,
93 const char *pathname, unsigned int mode, int stage,
98 enum object_type type;
99 unsigned long size = 0;
101 name = xstrdup(pathname);
102 fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "",
103 ctx.qry.path ? "/" : "", name);
105 if (!S_ISGITLINK(mode)) {
106 type = sha1_object_info(sha1, &size);
107 if (type == OBJ_BAD) {
108 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
115 html("<tr><td class='ls-mode'>");
116 cgit_print_filemode(mode);
118 if (S_ISGITLINK(mode)) {
119 htmlf("<a class='ls-mod' href='");
120 html_attr(fmt(ctx.repo->module_link,
126 } else if (S_ISDIR(mode)) {
127 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
130 cgit_tree_link(name, NULL, "ls-blob", ctx.qry.head,
133 htmlf("</td><td class='ls-size'>%li</td>", size);
136 cgit_log_link("log", NULL, "button", ctx.qry.head, curr_rev,
137 fullpath, 0, NULL, NULL, ctx.qry.showmsg);
138 if (ctx.repo->max_stats)
139 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
141 html("</td></tr>\n");
146 static void ls_head()
148 html("<table summary='tree listing' class='list'>\n");
149 html("<tr class='nohover'>");
150 html("<th class='left'>Mode</th>");
151 html("<th class='left'>Name</th>");
152 html("<th class='right'>Size</th>");
158 static void ls_tail()
166 static void ls_tree(const unsigned char *sha1, char *path)
170 tree = parse_tree_indirect(sha1);
172 cgit_print_error(fmt("Not a tree object: %s",
178 read_tree_recursive(tree, "", 0, 1, NULL, ls_item, NULL);
183 static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
184 const char *pathname, unsigned mode, int stage,
188 static char buffer[PATH_MAX];
192 memcpy(buffer, base, baselen);
193 strcpy(buffer+baselen, pathname);
194 url = cgit_pageurl(ctx.qry.repo, "tree",
195 fmt("h=%s&path=%s", curr_rev, buffer));
197 cgit_tree_link(xstrdup(pathname), NULL, NULL, ctx.qry.head,
200 if (strcmp(match_path, buffer))
201 return READ_TREE_RECURSIVE;
206 return READ_TREE_RECURSIVE;
208 print_object(sha1, buffer);
212 ls_item(sha1, base, baselen, pathname, mode, stage, NULL);
218 * Show a tree or a blob
219 * rev: the commit pointing at the root tree object
220 * path: path to tree or blob
222 void cgit_print_tree(const char *rev, char *path)
224 unsigned char sha1[20];
225 struct commit *commit;
226 const char *paths[] = {path, NULL};
231 curr_rev = xstrdup(rev);
232 if (get_sha1(rev, sha1)) {
233 cgit_print_error(fmt("Invalid revision name: %s", rev));
236 commit = lookup_commit_reference(sha1);
237 if (!commit || parse_commit(commit)) {
238 cgit_print_error(fmt("Invalid commit reference: %s", rev));
242 html("path: <a href='");
243 html_attr(cgit_pageurl(ctx.qry.repo, "tree", fmt("h=%s", rev)));
247 ls_tree(commit->tree->object.sha1, NULL);
252 read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL);