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