1 /* ui-tree.c: functions for tree output
3 * Copyright (C) 2006-2017 cgit Development Team <cgit@lists.zx2c4.com>
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
= "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
25 html("<table summary='blob content' class='blob'>\n");
27 if (ctx
.cfg
.enable_tree_linenumbers
) {
28 html("<tr><td class='linenumbers'><pre>");
33 htmlf(numberfmt
, ++lineno
);
34 while (idx
< size
- 1) { // skip absolute last newline
36 htmlf(numberfmt
, ++lineno
);
40 html("</pre></td>\n");
46 if (ctx
.repo
->source_filter
) {
47 char *filter_arg
= xstrdup(name
);
48 html("<td class='lines'><pre><code>");
49 cgit_open_filter(ctx
.repo
->source_filter
, filter_arg
);
51 cgit_close_filter(ctx
.repo
->source_filter
);
53 html("</code></pre></td></tr></table>\n");
57 html("<td class='lines'><pre><code>");
59 html("</code></pre></td></tr></table>\n");
64 static void print_binary_buffer(char *buf
, unsigned long size
)
66 unsigned long ofs
, idx
;
67 static char ascii
[ROWLEN
+ 1];
69 html("<table summary='blob content' class='bin-blob'>\n");
70 html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
71 for (ofs
= 0; ofs
< size
; ofs
+= ROWLEN
, buf
+= ROWLEN
) {
72 htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs
);
73 for (idx
= 0; idx
< ROWLEN
&& ofs
+ idx
< size
; idx
++)
75 idx
== 16 ? 4 : 1, "",
77 html(" </td><td class='hex'>");
78 for (idx
= 0; idx
< ROWLEN
&& ofs
+ idx
< size
; idx
++)
79 ascii
[idx
] = isgraph(buf
[idx
]) ? buf
[idx
] : '.';
87 static void print_object(const struct object_id
*oid
, const char *path
, const char *basename
, const char *rev
)
89 enum object_type type
;
93 type
= oid_object_info(the_repository
, oid
, &size
);
94 if (type
== OBJ_BAD
) {
95 cgit_print_error_page(404, "Not found",
96 "Bad object name: %s", oid_to_hex(oid
));
100 buf
= read_object_file(oid
, &type
, &size
);
102 cgit_print_error_page(500, "Internal server error",
103 "Error reading object %s", oid_to_hex(oid
));
107 cgit_set_title_from_path(path
);
109 cgit_print_layout_start();
110 htmlf("blob: %s (", oid_to_hex(oid
));
111 cgit_plain_link("plain", NULL
, NULL
, ctx
.qry
.head
,
113 if (ctx
.repo
->enable_blame
) {
115 cgit_blame_link("blame", NULL
, NULL
, ctx
.qry
.head
,
120 if (ctx
.cfg
.max_blob_size
&& size
/ 1024 > ctx
.cfg
.max_blob_size
) {
121 htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
122 size
/ 1024, ctx
.cfg
.max_blob_size
);
126 if (buffer_is_binary(buf
, size
))
127 print_binary_buffer(buf
, size
);
129 print_text_buffer(basename
, buf
, size
);
134 struct single_tree_ctx
{
136 struct object_id oid
;
141 static int single_tree_cb(const struct object_id
*oid
, struct strbuf
*base
,
142 const char *pathname
, unsigned mode
, void *cbdata
)
144 struct single_tree_ctx
*ctx
= cbdata
;
146 if (++ctx
->count
> 1)
149 if (!S_ISDIR(mode
)) {
154 ctx
->name
= xstrdup(pathname
);
155 oidcpy(&ctx
->oid
, oid
);
156 strbuf_addf(ctx
->path
, "/%s", pathname
);
160 static void write_tree_link(const struct object_id
*oid
, char *name
,
161 char *rev
, struct strbuf
*fullpath
)
163 size_t initial_length
= fullpath
->len
;
165 struct single_tree_ctx tree_ctx
= {
169 struct pathspec paths
= {
173 oidcpy(&tree_ctx
.oid
, oid
);
175 while (tree_ctx
.count
== 1) {
176 cgit_tree_link(name
, NULL
, "ls-dir", ctx
.qry
.head
, rev
,
179 tree
= lookup_tree(the_repository
, &tree_ctx
.oid
);
184 tree_ctx
.name
= NULL
;
187 read_tree(the_repository
, tree
, &paths
, single_tree_cb
, &tree_ctx
);
189 if (tree_ctx
.count
!= 1)
193 name
= tree_ctx
.name
;
196 strbuf_setlen(fullpath
, initial_length
);
199 static int ls_item(const struct object_id
*oid
, struct strbuf
*base
,
200 const char *pathname
, unsigned mode
, void *cbdata
)
202 struct walk_tree_context
*walk_tree_ctx
= cbdata
;
204 struct strbuf fullpath
= STRBUF_INIT
;
205 struct strbuf
class = STRBUF_INIT
;
206 enum object_type type
;
207 unsigned long size
= 0;
209 name
= xstrdup(pathname
);
210 strbuf_addf(&fullpath
, "%s%s%s", ctx
.qry
.path
? ctx
.qry
.path
: "",
211 ctx
.qry
.path
? "/" : "", name
);
213 if (!S_ISGITLINK(mode
)) {
214 type
= oid_object_info(the_repository
, oid
, &size
);
215 if (type
== OBJ_BAD
) {
216 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
224 html("<tr><td class='ls-mode'>");
225 cgit_print_filemode(mode
);
227 if (S_ISGITLINK(mode
)) {
228 cgit_submodule_link("ls-mod", fullpath
.buf
, oid_to_hex(oid
));
229 } else if (S_ISDIR(mode
)) {
230 write_tree_link(oid
, name
, walk_tree_ctx
->curr_rev
,
233 char *ext
= strrchr(name
, '.');
234 strbuf_addstr(&class, "ls-blob");
236 strbuf_addf(&class, " %s", ext
+ 1);
237 cgit_tree_link(name
, NULL
, class.buf
, ctx
.qry
.head
,
238 walk_tree_ctx
->curr_rev
, fullpath
.buf
);
240 htmlf("</td><td class='ls-size'>%li</td>", size
);
243 cgit_log_link("log", NULL
, "button", ctx
.qry
.head
,
244 walk_tree_ctx
->curr_rev
, fullpath
.buf
, 0, NULL
, NULL
,
246 if (ctx
.repo
->max_stats
)
247 cgit_stats_link("stats", NULL
, "button", ctx
.qry
.head
,
249 if (!S_ISGITLINK(mode
))
250 cgit_plain_link("plain", NULL
, "button", ctx
.qry
.head
,
251 walk_tree_ctx
->curr_rev
, fullpath
.buf
);
252 if (!S_ISDIR(mode
) && ctx
.repo
->enable_blame
)
253 cgit_blame_link("blame", NULL
, "button", ctx
.qry
.head
,
254 walk_tree_ctx
->curr_rev
, fullpath
.buf
);
255 html("</td></tr>\n");
257 strbuf_release(&fullpath
);
258 strbuf_release(&class);
262 static void ls_head(void)
264 cgit_print_layout_start();
265 html("<table summary='tree listing' class='list'>\n");
266 html("<tr class='nohover'>");
267 html("<th class='left'>Mode</th>");
268 html("<th class='left'>Name</th>");
269 html("<th class='right'>Size</th>");
274 static void ls_tail(void)
277 cgit_print_layout_end();
280 static void ls_tree(const struct object_id
*oid
, const char *path
, struct walk_tree_context
*walk_tree_ctx
)
283 struct pathspec paths
= {
287 tree
= parse_tree_indirect(oid
);
289 cgit_print_error_page(404, "Not found",
290 "Not a tree object: %s", oid_to_hex(oid
));
295 read_tree(the_repository
, tree
, &paths
, ls_item
, walk_tree_ctx
);
300 static int walk_tree(const struct object_id
*oid
, struct strbuf
*base
,
301 const char *pathname
, unsigned mode
, void *cbdata
)
303 struct walk_tree_context
*walk_tree_ctx
= cbdata
;
305 if (walk_tree_ctx
->state
== 0) {
306 struct strbuf buffer
= STRBUF_INIT
;
308 strbuf_addbuf(&buffer
, base
);
309 strbuf_addstr(&buffer
, pathname
);
310 if (strcmp(walk_tree_ctx
->match_path
, buffer
.buf
))
311 return READ_TREE_RECURSIVE
;
314 walk_tree_ctx
->state
= 1;
315 cgit_set_title_from_path(buffer
.buf
);
316 strbuf_release(&buffer
);
318 return READ_TREE_RECURSIVE
;
320 walk_tree_ctx
->state
= 2;
321 print_object(oid
, buffer
.buf
, pathname
, walk_tree_ctx
->curr_rev
);
322 strbuf_release(&buffer
);
326 ls_item(oid
, base
, pathname
, mode
, walk_tree_ctx
);
331 * Show a tree or a blob
332 * rev: the commit pointing at the root tree object
333 * path: path to tree or blob
335 void cgit_print_tree(const char *rev
, char *path
)
337 struct object_id oid
;
338 struct commit
*commit
;
339 struct pathspec_item path_items
= {
341 .len
= path
? strlen(path
) : 0
343 struct pathspec paths
= {
347 struct walk_tree_context walk_tree_ctx
= {
355 if (get_oid(rev
, &oid
)) {
356 cgit_print_error_page(404, "Not found",
357 "Invalid revision name: %s", rev
);
360 commit
= lookup_commit_reference(the_repository
, &oid
);
361 if (!commit
|| parse_commit(commit
)) {
362 cgit_print_error_page(404, "Not found",
363 "Invalid commit reference: %s", rev
);
367 walk_tree_ctx
.curr_rev
= xstrdup(rev
);
370 ls_tree(get_commit_tree_oid(commit
), NULL
, &walk_tree_ctx
);
374 read_tree(the_repository
, repo_get_commit_tree(the_repository
, commit
),
375 &paths
, walk_tree
, &walk_tree_ctx
);
376 if (walk_tree_ctx
.state
== 1)
378 else if (walk_tree_ctx
.state
== 2)
379 cgit_print_layout_end();
381 cgit_print_error_page(404, "Not found", "Path not found");
384 free(walk_tree_ctx
.curr_rev
);