]> gitweb.ps.run Git - ps-cgit/blob - ui-tree.c
tree: allow skipping through single-child trees
[ps-cgit] / ui-tree.c
1 /* ui-tree.c: functions for tree output
2  *
3  * Copyright (C) 2006-2014 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 set_title_from_path(const char *path)
88 {
89         size_t path_len, path_index, path_last_end;
90         char *new_title;
91
92         if (!path)
93                 return;
94
95         path_len = strlen(path);
96         new_title = xmalloc(path_len + 3 + strlen(ctx.page.title) + 1);
97         new_title[0] = '\0';
98
99         for (path_index = path_len, path_last_end = path_len; path_index-- > 0;) {
100                 if (path[path_index] == '/') {
101                         if (path_index == path_len - 1) {
102                                 path_last_end = path_index - 1;
103                                 continue;
104                         }
105                         strncat(new_title, &path[path_index + 1], path_last_end - path_index - 1);
106                         strcat(new_title, "\\");
107                         path_last_end = path_index;
108                 }
109         }
110         if (path_last_end)
111                 strncat(new_title, path, path_last_end);
112
113         strcat(new_title, " - ");
114         strcat(new_title, ctx.page.title);
115         ctx.page.title = new_title;
116 }
117
118 static void print_object(const unsigned char *sha1, char *path, const char *basename, const char *rev)
119 {
120         enum object_type type;
121         char *buf;
122         unsigned long size;
123
124         type = sha1_object_info(sha1, &size);
125         if (type == OBJ_BAD) {
126                 cgit_print_error_page(404, "Not found",
127                         "Bad object name: %s", sha1_to_hex(sha1));
128                 return;
129         }
130
131         buf = read_sha1_file(sha1, &type, &size);
132         if (!buf) {
133                 cgit_print_error_page(500, "Internal server error",
134                         "Error reading object %s", sha1_to_hex(sha1));
135                 return;
136         }
137
138         set_title_from_path(path);
139
140         cgit_print_layout_start();
141         htmlf("blob: %s (", sha1_to_hex(sha1));
142         cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
143                         rev, path);
144         html(")\n");
145
146         if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
147                 htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
148                                 size / 1024, ctx.cfg.max_blob_size);
149                 return;
150         }
151
152         if (buffer_is_binary(buf, size))
153                 print_binary_buffer(buf, size);
154         else
155                 print_text_buffer(basename, buf, size);
156 }
157
158 struct single_tree_ctx {
159         struct strbuf *path;
160         unsigned char sha1[GIT_SHA1_RAWSZ];
161         char *name;
162         size_t count;
163 };
164
165 static int single_tree_cb(const unsigned char *sha1, struct strbuf *base,
166                           const char *pathname, unsigned mode, int stage,
167                           void *cbdata)
168 {
169         struct single_tree_ctx *ctx = cbdata;
170
171         if (++ctx->count > 1)
172                 return -1;
173
174         if (!S_ISDIR(mode)) {
175                 ctx->count = 2;
176                 return -1;
177         }
178
179         ctx->name = xstrdup(pathname);
180         hashcpy(ctx->sha1, sha1);
181         strbuf_addf(ctx->path, "/%s", pathname);
182         return 0;
183 }
184
185 static void write_tree_link(const unsigned char *sha1, char *name,
186                             char *rev, struct strbuf *fullpath)
187 {
188         size_t initial_length = fullpath->len;
189         struct tree *tree;
190         struct single_tree_ctx tree_ctx = {
191                 .path = fullpath,
192                 .count = 1,
193         };
194         struct pathspec paths = {
195                 .nr = 0
196         };
197
198         hashcpy(tree_ctx.sha1, sha1);
199
200         while (tree_ctx.count == 1) {
201                 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head, rev,
202                                fullpath->buf);
203
204                 tree = lookup_tree(tree_ctx.sha1);
205                 if (!tree)
206                         return;
207
208                 free(tree_ctx.name);
209                 tree_ctx.name = NULL;
210                 tree_ctx.count = 0;
211
212                 read_tree_recursive(tree, "", 0, 1, &paths, single_tree_cb,
213                                     &tree_ctx);
214
215                 if (tree_ctx.count != 1)
216                         break;
217
218                 html(" / ");
219                 name = tree_ctx.name;
220         }
221
222         strbuf_setlen(fullpath, initial_length);
223 }
224
225 static int ls_item(const unsigned char *sha1, struct strbuf *base,
226                 const char *pathname, unsigned mode, int stage, void *cbdata)
227 {
228         struct walk_tree_context *walk_tree_ctx = cbdata;
229         char *name;
230         struct strbuf fullpath = STRBUF_INIT;
231         struct strbuf class = STRBUF_INIT;
232         enum object_type type;
233         unsigned long size = 0;
234
235         name = xstrdup(pathname);
236         strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "",
237                     ctx.qry.path ? "/" : "", name);
238
239         if (!S_ISGITLINK(mode)) {
240                 type = sha1_object_info(sha1, &size);
241                 if (type == OBJ_BAD) {
242                         htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
243                               name,
244                               sha1_to_hex(sha1));
245                         free(name);
246                         return 0;
247                 }
248         }
249
250         html("<tr><td class='ls-mode'>");
251         cgit_print_filemode(mode);
252         html("</td><td>");
253         if (S_ISGITLINK(mode)) {
254                 cgit_submodule_link("ls-mod", fullpath.buf, sha1_to_hex(sha1));
255         } else if (S_ISDIR(mode)) {
256                 write_tree_link(sha1, name, walk_tree_ctx->curr_rev,
257                                 &fullpath);
258         } else {
259                 char *ext = strrchr(name, '.');
260                 strbuf_addstr(&class, "ls-blob");
261                 if (ext)
262                         strbuf_addf(&class, " %s", ext + 1);
263                 cgit_tree_link(name, NULL, class.buf, ctx.qry.head,
264                                walk_tree_ctx->curr_rev, fullpath.buf);
265         }
266         htmlf("</td><td class='ls-size'>%li</td>", size);
267
268         html("<td>");
269         cgit_log_link("log", NULL, "button", ctx.qry.head,
270                       walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL,
271                       ctx.qry.showmsg, 0);
272         if (ctx.repo->max_stats)
273                 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
274                                 fullpath.buf);
275         if (!S_ISGITLINK(mode))
276                 cgit_plain_link("plain", NULL, "button", ctx.qry.head,
277                                 walk_tree_ctx->curr_rev, fullpath.buf);
278         html("</td></tr>\n");
279         free(name);
280         strbuf_release(&fullpath);
281         strbuf_release(&class);
282         return 0;
283 }
284
285 static void ls_head(void)
286 {
287         cgit_print_layout_start();
288         html("<table summary='tree listing' class='list'>\n");
289         html("<tr class='nohover'>");
290         html("<th class='left'>Mode</th>");
291         html("<th class='left'>Name</th>");
292         html("<th class='right'>Size</th>");
293         html("<th/>");
294         html("</tr>\n");
295 }
296
297 static void ls_tail(void)
298 {
299         html("</table>\n");
300         cgit_print_layout_end();
301 }
302
303 static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_context *walk_tree_ctx)
304 {
305         struct tree *tree;
306         struct pathspec paths = {
307                 .nr = 0
308         };
309
310         tree = parse_tree_indirect(sha1);
311         if (!tree) {
312                 cgit_print_error_page(404, "Not found",
313                         "Not a tree object: %s", sha1_to_hex(sha1));
314                 return;
315         }
316
317         ls_head();
318         read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
319         ls_tail();
320 }
321
322
323 static int walk_tree(const unsigned char *sha1, struct strbuf *base,
324                 const char *pathname, unsigned mode, int stage, void *cbdata)
325 {
326         struct walk_tree_context *walk_tree_ctx = cbdata;
327         static char buffer[PATH_MAX];
328
329         if (walk_tree_ctx->state == 0) {
330                 memcpy(buffer, base->buf, base->len);
331                 strcpy(buffer + base->len, pathname);
332                 if (strcmp(walk_tree_ctx->match_path, buffer))
333                         return READ_TREE_RECURSIVE;
334
335                 if (S_ISDIR(mode)) {
336                         walk_tree_ctx->state = 1;
337                         set_title_from_path(buffer);
338                         ls_head();
339                         return READ_TREE_RECURSIVE;
340                 } else {
341                         walk_tree_ctx->state = 2;
342                         print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev);
343                         return 0;
344                 }
345         }
346         ls_item(sha1, base, pathname, mode, stage, walk_tree_ctx);
347         return 0;
348 }
349
350 /*
351  * Show a tree or a blob
352  *   rev:  the commit pointing at the root tree object
353  *   path: path to tree or blob
354  */
355 void cgit_print_tree(const char *rev, char *path)
356 {
357         unsigned char sha1[20];
358         struct commit *commit;
359         struct pathspec_item path_items = {
360                 .match = path,
361                 .len = path ? strlen(path) : 0
362         };
363         struct pathspec paths = {
364                 .nr = path ? 1 : 0,
365                 .items = &path_items
366         };
367         struct walk_tree_context walk_tree_ctx = {
368                 .match_path = path,
369                 .state = 0
370         };
371
372         if (!rev)
373                 rev = ctx.qry.head;
374
375         if (get_sha1(rev, sha1)) {
376                 cgit_print_error_page(404, "Not found",
377                         "Invalid revision name: %s", rev);
378                 return;
379         }
380         commit = lookup_commit_reference(sha1);
381         if (!commit || parse_commit(commit)) {
382                 cgit_print_error_page(404, "Not found",
383                         "Invalid commit reference: %s", rev);
384                 return;
385         }
386
387         walk_tree_ctx.curr_rev = xstrdup(rev);
388
389         if (path == NULL) {
390                 ls_tree(commit->tree->object.oid.hash, NULL, &walk_tree_ctx);
391                 goto cleanup;
392         }
393
394         read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
395         if (walk_tree_ctx.state == 1)
396                 ls_tail();
397         else if (walk_tree_ctx.state == 2)
398                 cgit_print_layout_end();
399         else
400                 cgit_print_error_page(404, "Not found", "Path not found");
401
402 cleanup:
403         free(walk_tree_ctx.curr_rev);
404 }