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>");
30 htmlf(numberfmt, ++lineno);
31 while(idx < size - 1) { // skip absolute last newline
33 htmlf(numberfmt, ++lineno);
37 html("</pre></td>\n");
38 html("<td class='lines'><pre><code>");
40 html("</code></pre></td></tr></table>\n");
45 static void print_binary_buffer(char *buf, unsigned long size)
47 unsigned long ofs, idx;
48 static char ascii[ROWLEN + 1];
50 html("<table summary='blob content' class='bin-blob'>\n");
51 html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
52 for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
53 htmlf("<tr><td class='right'>%04x</td><td class='hex'>", ofs);
54 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
56 idx == 16 ? 4 : 1, "",
58 html(" </td><td class='hex'>");
59 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
60 ascii[idx] = isgraph(buf[idx]) ? buf[idx] : '.';
68 static void print_object(const unsigned char *sha1, char *path)
70 enum object_type type;
74 type = sha1_object_info(sha1, &size);
75 if (type == OBJ_BAD) {
76 cgit_print_error(fmt("Bad object name: %s",
81 buf = read_sha1_file(sha1, &type, &size);
83 cgit_print_error(fmt("Error reading object %s",
89 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
91 htmlf(")<br/>blob: %s\n", sha1_to_hex(sha1));
93 if (buffer_is_binary(buf, size))
94 print_binary_buffer(buf, size);
96 print_text_buffer(buf, size);
100 static int ls_item(const unsigned char *sha1, const char *base, int baselen,
101 const char *pathname, unsigned int mode, int stage,
106 enum object_type type;
107 unsigned long size = 0;
109 name = xstrdup(pathname);
110 fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "",
111 ctx.qry.path ? "/" : "", name);
113 if (!S_ISGITLINK(mode)) {
114 type = sha1_object_info(sha1, &size);
115 if (type == OBJ_BAD) {
116 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
123 html("<tr><td class='ls-mode'>");
124 cgit_print_filemode(mode);
126 if (S_ISGITLINK(mode)) {
127 htmlf("<a class='ls-mod' href='");
128 html_attr(fmt(ctx.repo->module_link,
134 } else if (S_ISDIR(mode)) {
135 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
138 cgit_tree_link(name, NULL, "ls-blob", ctx.qry.head,
141 htmlf("</td><td class='ls-size'>%li</td>", size);
144 cgit_log_link("log", NULL, "button", ctx.qry.head, curr_rev,
145 fullpath, 0, NULL, NULL, ctx.qry.showmsg);
146 if (ctx.repo->max_stats)
147 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
149 html("</td></tr>\n");
154 static void ls_head()
156 html("<table summary='tree listing' class='list'>\n");
157 html("<tr class='nohover'>");
158 html("<th class='left'>Mode</th>");
159 html("<th class='left'>Name</th>");
160 html("<th class='right'>Size</th>");
166 static void ls_tail()
174 static void ls_tree(const unsigned char *sha1, char *path)
178 tree = parse_tree_indirect(sha1);
180 cgit_print_error(fmt("Not a tree object: %s",
186 read_tree_recursive(tree, "", 0, 1, NULL, ls_item, NULL);
191 static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
192 const char *pathname, unsigned mode, int stage,
196 static char buffer[PATH_MAX];
200 memcpy(buffer, base, baselen);
201 strcpy(buffer+baselen, pathname);
202 url = cgit_pageurl(ctx.qry.repo, "tree",
203 fmt("h=%s&path=%s", curr_rev, buffer));
205 cgit_tree_link(xstrdup(pathname), NULL, NULL, ctx.qry.head,
208 if (strcmp(match_path, buffer))
209 return READ_TREE_RECURSIVE;
214 return READ_TREE_RECURSIVE;
216 print_object(sha1, buffer);
220 ls_item(sha1, base, baselen, pathname, mode, stage, NULL);
226 * Show a tree or a blob
227 * rev: the commit pointing at the root tree object
228 * path: path to tree or blob
230 void cgit_print_tree(const char *rev, char *path)
232 unsigned char sha1[20];
233 struct commit *commit;
234 const char *paths[] = {path, NULL};
239 curr_rev = xstrdup(rev);
240 if (get_sha1(rev, sha1)) {
241 cgit_print_error(fmt("Invalid revision name: %s", rev));
244 commit = lookup_commit_reference(sha1);
245 if (!commit || parse_commit(commit)) {
246 cgit_print_error(fmt("Invalid commit reference: %s", rev));
250 html("path: <a href='");
251 html_attr(cgit_pageurl(ctx.qry.repo, "tree", fmt("h=%s", rev)));
255 ls_tree(commit->tree->object.sha1, NULL);
260 read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL);