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"
14 struct walk_tree_context
{
20 static void print_text_buffer(const char *name
, char *buf
, unsigned long size
)
22 unsigned long lineno
, idx
;
23 const char *numberfmt
=
24 "<a class='no' id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a>\n";
26 html("<table summary='blob content' class='blob'>\n");
28 if (ctx
.cfg
.enable_tree_linenumbers
) {
29 html("<tr><td class='linenumbers'><pre>");
34 htmlf(numberfmt
, ++lineno
);
35 while (idx
< size
- 1) { // skip absolute last newline
37 htmlf(numberfmt
, ++lineno
);
41 html("</pre></td>\n");
47 if (ctx
.repo
->source_filter
) {
48 html("<td class='lines'><pre><code>");
49 ctx
.repo
->source_filter
->argv
[1] = xstrdup(name
);
50 cgit_open_filter(ctx
.repo
->source_filter
);
52 cgit_close_filter(ctx
.repo
->source_filter
);
53 free(ctx
.repo
->source_filter
->argv
[1]);
54 ctx
.repo
->source_filter
->argv
[1] = NULL
;
55 html("</code></pre></td></tr></table>\n");
59 html("<td class='lines'><pre><code>");
61 html("</code></pre></td></tr></table>\n");
66 static void print_binary_buffer(char *buf
, unsigned long size
)
68 unsigned long ofs
, idx
;
69 static char ascii
[ROWLEN
+ 1];
71 html("<table summary='blob content' class='bin-blob'>\n");
72 html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
73 for (ofs
= 0; ofs
< size
; ofs
+= ROWLEN
, buf
+= ROWLEN
) {
74 htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs
);
75 for (idx
= 0; idx
< ROWLEN
&& ofs
+ idx
< size
; idx
++)
77 idx
== 16 ? 4 : 1, "",
79 html(" </td><td class='hex'>");
80 for (idx
= 0; idx
< ROWLEN
&& ofs
+ idx
< size
; idx
++)
81 ascii
[idx
] = isgraph(buf
[idx
]) ? buf
[idx
] : '.';
89 static void print_object(const unsigned char *sha1
, char *path
, const char *basename
, const char *rev
)
91 enum object_type type
;
95 type
= sha1_object_info(sha1
, &size
);
96 if (type
== OBJ_BAD
) {
97 cgit_print_error(fmt("Bad object name: %s",
102 buf
= read_sha1_file(sha1
, &type
, &size
);
104 cgit_print_error(fmt("Error reading object %s",
109 htmlf("blob: %s (", sha1_to_hex(sha1
));
110 cgit_plain_link("plain", NULL
, NULL
, ctx
.qry
.head
,
114 if (ctx
.cfg
.max_blob_size
&& size
/ 1024 > ctx
.cfg
.max_blob_size
) {
115 htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
116 size
/ 1024, ctx
.cfg
.max_blob_size
);
120 if (buffer_is_binary(buf
, size
))
121 print_binary_buffer(buf
, size
);
123 print_text_buffer(basename
, buf
, size
);
127 static int ls_item(const unsigned char *sha1
, const char *base
, int baselen
,
128 const char *pathname
, unsigned int mode
, int stage
,
131 struct walk_tree_context
*walk_tree_ctx
= cbdata
;
135 enum object_type type
;
136 unsigned long size
= 0;
138 name
= xstrdup(pathname
);
139 fullpath
= fmt("%s%s%s", ctx
.qry
.path
? ctx
.qry
.path
: "",
140 ctx
.qry
.path
? "/" : "", name
);
142 if (!S_ISGITLINK(mode
)) {
143 type
= sha1_object_info(sha1
, &size
);
144 if (type
== OBJ_BAD
) {
145 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
152 html("<tr><td class='ls-mode'>");
153 cgit_print_filemode(mode
);
155 if (S_ISGITLINK(mode
)) {
156 cgit_submodule_link("ls-mod", fullpath
, sha1_to_hex(sha1
));
157 } else if (S_ISDIR(mode
)) {
158 cgit_tree_link(name
, NULL
, "ls-dir", ctx
.qry
.head
,
159 walk_tree_ctx
->curr_rev
, fullpath
);
161 class = strrchr(name
, '.');
163 class = fmt("ls-blob %s", class + 1);
166 cgit_tree_link(name
, NULL
, class, ctx
.qry
.head
,
167 walk_tree_ctx
->curr_rev
, fullpath
);
169 htmlf("</td><td class='ls-size'>%li</td>", size
);
172 cgit_log_link("log", NULL
, "button", ctx
.qry
.head
,
173 walk_tree_ctx
->curr_rev
, fullpath
, 0, NULL
, NULL
,
175 if (ctx
.repo
->max_stats
)
176 cgit_stats_link("stats", NULL
, "button", ctx
.qry
.head
,
178 if (!S_ISGITLINK(mode
))
179 cgit_plain_link("plain", NULL
, "button", ctx
.qry
.head
,
180 walk_tree_ctx
->curr_rev
, fullpath
);
181 html("</td></tr>\n");
186 static void ls_head()
188 html("<table summary='tree listing' class='list'>\n");
189 html("<tr class='nohover'>");
190 html("<th class='left'>Mode</th>");
191 html("<th class='left'>Name</th>");
192 html("<th class='right'>Size</th>");
197 static void ls_tail()
202 static void ls_tree(const unsigned char *sha1
, char *path
, struct walk_tree_context
*walk_tree_ctx
)
205 struct pathspec paths
= {
209 tree
= parse_tree_indirect(sha1
);
211 cgit_print_error(fmt("Not a tree object: %s",
217 read_tree_recursive(tree
, "", 0, 1, &paths
, ls_item
, walk_tree_ctx
);
222 static int walk_tree(const unsigned char *sha1
, const char *base
, int baselen
,
223 const char *pathname
, unsigned mode
, int stage
,
226 struct walk_tree_context
*walk_tree_ctx
= cbdata
;
227 static char buffer
[PATH_MAX
];
229 if (walk_tree_ctx
->state
== 0) {
230 memcpy(buffer
, base
, baselen
);
231 strcpy(buffer
+ baselen
, pathname
);
232 if (strcmp(walk_tree_ctx
->match_path
, buffer
))
233 return READ_TREE_RECURSIVE
;
236 walk_tree_ctx
->state
= 1;
238 return READ_TREE_RECURSIVE
;
240 print_object(sha1
, buffer
, pathname
, walk_tree_ctx
->curr_rev
);
244 ls_item(sha1
, base
, baselen
, pathname
, mode
, stage
, walk_tree_ctx
);
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
= {
266 struct walk_tree_context walk_tree_ctx
= {
274 walk_tree_ctx
.curr_rev
= xstrdup(rev
);
275 if (get_sha1(rev
, sha1
)) {
276 cgit_print_error(fmt("Invalid revision name: %s", rev
));
279 commit
= lookup_commit_reference(sha1
);
280 if (!commit
|| parse_commit(commit
)) {
281 cgit_print_error(fmt("Invalid commit reference: %s", rev
));
286 ls_tree(commit
->tree
->object
.sha1
, NULL
, &walk_tree_ctx
);
290 read_tree_recursive(commit
->tree
, "", 0, 0, &paths
, walk_tree
, &walk_tree_ctx
);
291 if (walk_tree_ctx
.state
== 1)