]> gitweb.ps.run Git - ps-cgit/blob - ui-tree.c
ui-patch.c: Fix signature delimiter
[ps-cgit] / ui-tree.c
1 /* ui-tree.c: functions for tree output
2  *
3  * Copyright (C) 2006 Lars Hjemli
4  *
5  * Licensed under GNU General Public License v2
6  *   (see COPYING for full license text)
7  */
8
9 #include <ctype.h>
10 #include "cgit.h"
11 #include "ui-tree.h"
12 #include "html.h"
13 #include "ui-shared.h"
14
15 struct walk_tree_context {
16         char *curr_rev;
17         char *match_path;
18         int state;
19 };
20
21 static void print_text_buffer(const char *name, char *buf, unsigned long size)
22 {
23         unsigned long lineno, idx;
24         const char *numberfmt =
25                 "<a class='no' id='n%1$d' name='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                 html("<td class='lines'><pre><code>");
50                 ctx.repo->source_filter->argv[1] = xstrdup(name);
51                 cgit_open_filter(ctx.repo->source_filter);
52                 html_raw(buf, size);
53                 cgit_close_filter(ctx.repo->source_filter);
54                 free(ctx.repo->source_filter->argv[1]);
55                 ctx.repo->source_filter->argv[1] = NULL;
56                 html("</code></pre></td></tr></table>\n");
57                 return;
58         }
59
60         html("<td class='lines'><pre><code>");
61         html_txt(buf);
62         html("</code></pre></td></tr></table>\n");
63 }
64
65 #define ROWLEN 32
66
67 static void print_binary_buffer(char *buf, unsigned long size)
68 {
69         unsigned long ofs, idx;
70         static char ascii[ROWLEN + 1];
71
72         html("<table summary='blob content' class='bin-blob'>\n");
73         html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
74         for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
75                 htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs);
76                 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
77                         htmlf("%*s%02x",
78                               idx == 16 ? 4 : 1, "",
79                               buf[idx] & 0xff);
80                 html(" </td><td class='hex'>");
81                 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
82                         ascii[idx] = isgraph(buf[idx]) ? buf[idx] : '.';
83                 ascii[idx] = '\0';
84                 html_txt(ascii);
85                 html("</td></tr>\n");
86         }
87         html("</table>\n");
88 }
89
90 static void print_object(const unsigned char *sha1, char *path, const char *basename, const char *rev)
91 {
92         enum object_type type;
93         char *buf;
94         unsigned long size;
95
96         type = sha1_object_info(sha1, &size);
97         if (type == OBJ_BAD) {
98                 cgit_print_error("Bad object name: %s", sha1_to_hex(sha1));
99                 return;
100         }
101
102         buf = read_sha1_file(sha1, &type, &size);
103         if (!buf) {
104                 cgit_print_error("Error reading object %s", sha1_to_hex(sha1));
105                 return;
106         }
107
108         htmlf("blob: %s (", sha1_to_hex(sha1));
109         cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
110                         rev, path);
111         html(")\n");
112
113         if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
114                 htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
115                                 size / 1024, ctx.cfg.max_blob_size);
116                 return;
117         }
118
119         if (buffer_is_binary(buf, size))
120                 print_binary_buffer(buf, size);
121         else
122                 print_text_buffer(basename, buf, size);
123 }
124
125
126 static int ls_item(const unsigned char *sha1, const char *base, int baselen,
127                    const char *pathname, unsigned int mode, int stage,
128                    void *cbdata)
129 {
130         struct walk_tree_context *walk_tree_ctx = cbdata;
131         char *name;
132         struct strbuf fullpath = STRBUF_INIT;
133         struct strbuf class = STRBUF_INIT;
134         enum object_type type;
135         unsigned long size = 0;
136
137         name = xstrdup(pathname);
138         strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "",
139                     ctx.qry.path ? "/" : "", name);
140
141         if (!S_ISGITLINK(mode)) {
142                 type = sha1_object_info(sha1, &size);
143                 if (type == OBJ_BAD) {
144                         htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
145                               name,
146                               sha1_to_hex(sha1));
147                         return 0;
148                 }
149         }
150
151         html("<tr><td class='ls-mode'>");
152         cgit_print_filemode(mode);
153         html("</td><td>");
154         if (S_ISGITLINK(mode)) {
155                 cgit_submodule_link("ls-mod", fullpath.buf, sha1_to_hex(sha1));
156         } else if (S_ISDIR(mode)) {
157                 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
158                                walk_tree_ctx->curr_rev, fullpath.buf);
159         } else {
160                 char *ext = strrchr(name, '.');
161                 strbuf_addstr(&class, "ls-blob");
162                 if (ext)
163                         strbuf_addf(&class, " %s", ext + 1);
164                 cgit_tree_link(name, NULL, class.buf, ctx.qry.head,
165                                walk_tree_ctx->curr_rev, fullpath.buf);
166         }
167         htmlf("</td><td class='ls-size'>%li</td>", size);
168
169         html("<td>");
170         cgit_log_link("log", NULL, "button", ctx.qry.head,
171                       walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL,
172                       ctx.qry.showmsg);
173         if (ctx.repo->max_stats)
174                 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
175                                 fullpath.buf);
176         if (!S_ISGITLINK(mode))
177                 cgit_plain_link("plain", NULL, "button", ctx.qry.head,
178                                 walk_tree_ctx->curr_rev, fullpath.buf);
179         html("</td></tr>\n");
180         free(name);
181         strbuf_release(&fullpath);
182         strbuf_release(&class);
183         return 0;
184 }
185
186 static void ls_head()
187 {
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>");
193         html("<th/>");
194         html("</tr>\n");
195 }
196
197 static void ls_tail()
198 {
199         html("</table>\n");
200 }
201
202 static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_context *walk_tree_ctx)
203 {
204         struct tree *tree;
205         struct pathspec paths = {
206                 .nr = 0
207         };
208
209         tree = parse_tree_indirect(sha1);
210         if (!tree) {
211                 cgit_print_error("Not a tree object: %s", sha1_to_hex(sha1));
212                 return;
213         }
214
215         ls_head();
216         read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
217         ls_tail();
218 }
219
220
221 static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
222                      const char *pathname, unsigned mode, int stage,
223                      void *cbdata)
224 {
225         struct walk_tree_context *walk_tree_ctx = cbdata;
226         static char buffer[PATH_MAX];
227
228         if (walk_tree_ctx->state == 0) {
229                 memcpy(buffer, base, baselen);
230                 strcpy(buffer + baselen, pathname);
231                 if (strcmp(walk_tree_ctx->match_path, buffer))
232                         return READ_TREE_RECURSIVE;
233
234                 if (S_ISDIR(mode)) {
235                         walk_tree_ctx->state = 1;
236                         ls_head();
237                         return READ_TREE_RECURSIVE;
238                 } else {
239                         print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev);
240                         return 0;
241                 }
242         }
243         ls_item(sha1, base, baselen, pathname, mode, stage, walk_tree_ctx);
244         return 0;
245 }
246
247
248 /*
249  * Show a tree or a blob
250  *   rev:  the commit pointing at the root tree object
251  *   path: path to tree or blob
252  */
253 void cgit_print_tree(const char *rev, char *path)
254 {
255         unsigned char sha1[20];
256         struct commit *commit;
257         struct pathspec_item path_items = {
258                 .match = path,
259                 .len = path ? strlen(path) : 0
260         };
261         struct pathspec paths = {
262                 .nr = path ? 1 : 0,
263                 .items = &path_items
264         };
265         struct walk_tree_context walk_tree_ctx = {
266                 .match_path = path,
267                 .state = 0
268         };
269
270         if (!rev)
271                 rev = ctx.qry.head;
272
273         if (get_sha1(rev, sha1)) {
274                 cgit_print_error("Invalid revision name: %s", rev);
275                 return;
276         }
277         commit = lookup_commit_reference(sha1);
278         if (!commit || parse_commit(commit)) {
279                 cgit_print_error("Invalid commit reference: %s", rev);
280                 return;
281         }
282
283         walk_tree_ctx.curr_rev = xstrdup(rev);
284
285         if (path == NULL) {
286                 ls_tree(commit->tree->object.sha1, NULL, &walk_tree_ctx);
287                 goto cleanup;
288         }
289
290         read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
291         if (walk_tree_ctx.state == 1)
292                 ls_tail();
293
294 cleanup:
295         free(walk_tree_ctx.curr_rev);
296 }