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,
107 enum object_type type;
108 unsigned long size = 0;
110 name = xstrdup(pathname);
111 fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "",
112 ctx.qry.path ? "/" : "", name);
114 if (!S_ISGITLINK(mode)) {
115 type = sha1_object_info(sha1, &size);
116 if (type == OBJ_BAD) {
117 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
124 html("<tr><td class='ls-mode'>");
125 cgit_print_filemode(mode);
127 if (S_ISGITLINK(mode)) {
128 htmlf("<a class='ls-mod' href='");
129 html_attr(fmt(ctx.repo->module_link,
135 } else if (S_ISDIR(mode)) {
136 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
139 class = strrchr(name, '.');
141 class = fmt("ls-blob %s", class + 1);
144 cgit_tree_link(name, NULL, class, ctx.qry.head,
147 htmlf("</td><td class='ls-size'>%li</td>", size);
150 cgit_log_link("log", NULL, "button", ctx.qry.head, curr_rev,
151 fullpath, 0, NULL, NULL, ctx.qry.showmsg);
152 if (ctx.repo->max_stats)
153 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
155 html("</td></tr>\n");
160 static void ls_head()
162 html("<table summary='tree listing' class='list'>\n");
163 html("<tr class='nohover'>");
164 html("<th class='left'>Mode</th>");
165 html("<th class='left'>Name</th>");
166 html("<th class='right'>Size</th>");
172 static void ls_tail()
180 static void ls_tree(const unsigned char *sha1, char *path)
184 tree = parse_tree_indirect(sha1);
186 cgit_print_error(fmt("Not a tree object: %s",
192 read_tree_recursive(tree, "", 0, 1, NULL, ls_item, NULL);
197 static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
198 const char *pathname, unsigned mode, int stage,
202 static char buffer[PATH_MAX];
206 memcpy(buffer, base, baselen);
207 strcpy(buffer+baselen, pathname);
208 url = cgit_pageurl(ctx.qry.repo, "tree",
209 fmt("h=%s&path=%s", curr_rev, buffer));
211 cgit_tree_link(xstrdup(pathname), NULL, NULL, ctx.qry.head,
214 if (strcmp(match_path, buffer))
215 return READ_TREE_RECURSIVE;
220 return READ_TREE_RECURSIVE;
222 print_object(sha1, buffer);
226 ls_item(sha1, base, baselen, pathname, mode, stage, NULL);
232 * Show a tree or a blob
233 * rev: the commit pointing at the root tree object
234 * path: path to tree or blob
236 void cgit_print_tree(const char *rev, char *path)
238 unsigned char sha1[20];
239 struct commit *commit;
240 const char *paths[] = {path, NULL};
245 curr_rev = xstrdup(rev);
246 if (get_sha1(rev, sha1)) {
247 cgit_print_error(fmt("Invalid revision name: %s", rev));
250 commit = lookup_commit_reference(sha1);
251 if (!commit || parse_commit(commit)) {
252 cgit_print_error(fmt("Invalid commit reference: %s", rev));
256 html("path: <a href='");
257 html_attr(cgit_pageurl(ctx.qry.repo, "tree", fmt("h=%s", rev)));
261 ls_tree(commit->tree->object.sha1, NULL);
266 read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL);