]> git.cameronkatri.com Git - cgit.git/blob - ui-tree.c
Makefile: drive asciidoc directly for HTML output
[cgit.git] / ui-tree.c
1 /* ui-tree.c: functions for tree output
2 *
3 * Copyright (C) 2006-2017 cgit Development Team <cgit@lists.zx2c4.com>
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9 #include "cgit.h"
10 #include "ui-tree.h"
11 #include "html.h"
12 #include "ui-shared.h"
13
14 struct walk_tree_context {
15 char *curr_rev;
16 char *match_path;
17 int state;
18 };
19
20 static void print_text_buffer(const char *name, char *buf, unsigned long size)
21 {
22 unsigned long lineno, idx;
23 const char *numberfmt = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
24
25 html("<table summary='blob content' class='blob'>\n");
26
27 if (ctx.cfg.enable_tree_linenumbers) {
28 html("<tr><td class='linenumbers'><pre>");
29 idx = 0;
30 lineno = 0;
31
32 if (size) {
33 htmlf(numberfmt, ++lineno);
34 while (idx < size - 1) { // skip absolute last newline
35 if (buf[idx] == '\n')
36 htmlf(numberfmt, ++lineno);
37 idx++;
38 }
39 }
40 html("</pre></td>\n");
41 }
42 else {
43 html("<tr>\n");
44 }
45
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);
50 html_raw(buf, size);
51 cgit_close_filter(ctx.repo->source_filter);
52 free(filter_arg);
53 html("</code></pre></td></tr></table>\n");
54 return;
55 }
56
57 html("<td class='lines'><pre><code>");
58 html_txt(buf);
59 html("</code></pre></td></tr></table>\n");
60 }
61
62 #define ROWLEN 32
63
64 static void print_binary_buffer(char *buf, unsigned long size)
65 {
66 unsigned long ofs, idx;
67 static char ascii[ROWLEN + 1];
68
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++)
74 htmlf("%*s%02x",
75 idx == 16 ? 4 : 1, "",
76 buf[idx] & 0xff);
77 html(" </td><td class='hex'>");
78 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
79 ascii[idx] = isgraph(buf[idx]) ? buf[idx] : '.';
80 ascii[idx] = '\0';
81 html_txt(ascii);
82 html("</td></tr>\n");
83 }
84 html("</table>\n");
85 }
86
87 static void print_object(const unsigned char *sha1, char *path, const char *basename, const char *rev)
88 {
89 enum object_type type;
90 char *buf;
91 unsigned long size;
92
93 type = sha1_object_info(sha1, &size);
94 if (type == OBJ_BAD) {
95 cgit_print_error_page(404, "Not found",
96 "Bad object name: %s", sha1_to_hex(sha1));
97 return;
98 }
99
100 buf = read_sha1_file(sha1, &type, &size);
101 if (!buf) {
102 cgit_print_error_page(500, "Internal server error",
103 "Error reading object %s", sha1_to_hex(sha1));
104 return;
105 }
106
107 cgit_set_title_from_path(path);
108
109 cgit_print_layout_start();
110 htmlf("blob: %s (", sha1_to_hex(sha1));
111 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
112 rev, path);
113 if (ctx.cfg.enable_blame) {
114 html(") (");
115 cgit_blame_link("blame", NULL, NULL, ctx.qry.head,
116 rev, path);
117 }
118 html(")\n");
119
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);
123 return;
124 }
125
126 if (buffer_is_binary(buf, size))
127 print_binary_buffer(buf, size);
128 else
129 print_text_buffer(basename, buf, size);
130 }
131
132 struct single_tree_ctx {
133 struct strbuf *path;
134 struct object_id oid;
135 char *name;
136 size_t count;
137 };
138
139 static int single_tree_cb(const unsigned char *sha1, struct strbuf *base,
140 const char *pathname, unsigned mode, int stage,
141 void *cbdata)
142 {
143 struct single_tree_ctx *ctx = cbdata;
144
145 if (++ctx->count > 1)
146 return -1;
147
148 if (!S_ISDIR(mode)) {
149 ctx->count = 2;
150 return -1;
151 }
152
153 ctx->name = xstrdup(pathname);
154 hashcpy(ctx->oid.hash, sha1);
155 strbuf_addf(ctx->path, "/%s", pathname);
156 return 0;
157 }
158
159 static void write_tree_link(const unsigned char *sha1, char *name,
160 char *rev, struct strbuf *fullpath)
161 {
162 size_t initial_length = fullpath->len;
163 struct tree *tree;
164 struct single_tree_ctx tree_ctx = {
165 .path = fullpath,
166 .count = 1,
167 };
168 struct pathspec paths = {
169 .nr = 0
170 };
171
172 hashcpy(tree_ctx.oid.hash, sha1);
173
174 while (tree_ctx.count == 1) {
175 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head, rev,
176 fullpath->buf);
177
178 tree = lookup_tree(&tree_ctx.oid);
179 if (!tree)
180 return;
181
182 free(tree_ctx.name);
183 tree_ctx.name = NULL;
184 tree_ctx.count = 0;
185
186 read_tree_recursive(tree, "", 0, 1, &paths, single_tree_cb,
187 &tree_ctx);
188
189 if (tree_ctx.count != 1)
190 break;
191
192 html(" / ");
193 name = tree_ctx.name;
194 }
195
196 strbuf_setlen(fullpath, initial_length);
197 }
198
199 static int ls_item(const unsigned char *sha1, struct strbuf *base,
200 const char *pathname, unsigned mode, int stage, void *cbdata)
201 {
202 struct walk_tree_context *walk_tree_ctx = cbdata;
203 char *name;
204 struct strbuf fullpath = STRBUF_INIT;
205 struct strbuf class = STRBUF_INIT;
206 enum object_type type;
207 unsigned long size = 0;
208
209 name = xstrdup(pathname);
210 strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "",
211 ctx.qry.path ? "/" : "", name);
212
213 if (!S_ISGITLINK(mode)) {
214 type = sha1_object_info(sha1, &size);
215 if (type == OBJ_BAD) {
216 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
217 name,
218 sha1_to_hex(sha1));
219 free(name);
220 return 0;
221 }
222 }
223
224 html("<tr><td class='ls-mode'>");
225 cgit_print_filemode(mode);
226 html("</td><td>");
227 if (S_ISGITLINK(mode)) {
228 cgit_submodule_link("ls-mod", fullpath.buf, sha1_to_hex(sha1));
229 } else if (S_ISDIR(mode)) {
230 write_tree_link(sha1, name, walk_tree_ctx->curr_rev,
231 &fullpath);
232 } else {
233 char *ext = strrchr(name, '.');
234 strbuf_addstr(&class, "ls-blob");
235 if (ext)
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);
239 }
240 htmlf("</td><td class='ls-size'>%li</td>", size);
241
242 html("<td>");
243 cgit_log_link("log", NULL, "button", ctx.qry.head,
244 walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL,
245 ctx.qry.showmsg, 0);
246 if (ctx.repo->max_stats)
247 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
248 fullpath.buf);
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.cfg.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");
256 free(name);
257 strbuf_release(&fullpath);
258 strbuf_release(&class);
259 return 0;
260 }
261
262 static void ls_head(void)
263 {
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>");
270 html("<th/>");
271 html("</tr>\n");
272 }
273
274 static void ls_tail(void)
275 {
276 html("</table>\n");
277 cgit_print_layout_end();
278 }
279
280 static void ls_tree(const struct object_id *oid, char *path, struct walk_tree_context *walk_tree_ctx)
281 {
282 struct tree *tree;
283 struct pathspec paths = {
284 .nr = 0
285 };
286
287 tree = parse_tree_indirect(oid);
288 if (!tree) {
289 cgit_print_error_page(404, "Not found",
290 "Not a tree object: %s", sha1_to_hex(oid->hash));
291 return;
292 }
293
294 ls_head();
295 read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
296 ls_tail();
297 }
298
299
300 static int walk_tree(const unsigned char *sha1, struct strbuf *base,
301 const char *pathname, unsigned mode, int stage, void *cbdata)
302 {
303 struct walk_tree_context *walk_tree_ctx = cbdata;
304
305 if (walk_tree_ctx->state == 0) {
306 struct strbuf buffer = STRBUF_INIT;
307
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;
312
313 if (S_ISDIR(mode)) {
314 walk_tree_ctx->state = 1;
315 cgit_set_title_from_path(buffer.buf);
316 strbuf_release(&buffer);
317 ls_head();
318 return READ_TREE_RECURSIVE;
319 } else {
320 walk_tree_ctx->state = 2;
321 print_object(sha1, buffer.buf, pathname, walk_tree_ctx->curr_rev);
322 strbuf_release(&buffer);
323 return 0;
324 }
325 }
326 ls_item(sha1, base, pathname, mode, stage, walk_tree_ctx);
327 return 0;
328 }
329
330 /*
331 * Show a tree or a blob
332 * rev: the commit pointing at the root tree object
333 * path: path to tree or blob
334 */
335 void cgit_print_tree(const char *rev, char *path)
336 {
337 struct object_id oid;
338 struct commit *commit;
339 struct pathspec_item path_items = {
340 .match = path,
341 .len = path ? strlen(path) : 0
342 };
343 struct pathspec paths = {
344 .nr = path ? 1 : 0,
345 .items = &path_items
346 };
347 struct walk_tree_context walk_tree_ctx = {
348 .match_path = path,
349 .state = 0
350 };
351
352 if (!rev)
353 rev = ctx.qry.head;
354
355 if (get_oid(rev, &oid)) {
356 cgit_print_error_page(404, "Not found",
357 "Invalid revision name: %s", rev);
358 return;
359 }
360 commit = lookup_commit_reference(&oid);
361 if (!commit || parse_commit(commit)) {
362 cgit_print_error_page(404, "Not found",
363 "Invalid commit reference: %s", rev);
364 return;
365 }
366
367 walk_tree_ctx.curr_rev = xstrdup(rev);
368
369 if (path == NULL) {
370 ls_tree(&commit->tree->object.oid, NULL, &walk_tree_ctx);
371 goto cleanup;
372 }
373
374 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
375 if (walk_tree_ctx.state == 1)
376 ls_tail();
377 else if (walk_tree_ctx.state == 2)
378 cgit_print_layout_end();
379 else
380 cgit_print_error_page(404, "Not found", "Path not found");
381
382 cleanup:
383 free(walk_tree_ctx.curr_rev);
384 }