]> git.cameronkatri.com Git - cgit.git/blob - ui-tree.c
filter: make exit status local
[cgit.git] / 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 <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 = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
25
26 html("<table summary='blob content' class='blob'>\n");
27
28 if (ctx.cfg.enable_tree_linenumbers) {
29 html("<tr><td class='linenumbers'><pre>");
30 idx = 0;
31 lineno = 0;
32
33 if (size) {
34 htmlf(numberfmt, ++lineno);
35 while (idx < size - 1) { // skip absolute last newline
36 if (buf[idx] == '\n')
37 htmlf(numberfmt, ++lineno);
38 idx++;
39 }
40 }
41 html("</pre></td>\n");
42 }
43 else {
44 html("<tr>\n");
45 }
46
47 if (ctx.repo->source_filter) {
48 html("<td class='lines'><pre><code>");
49 ctx.repo->source_filter->argv[1] = xstrdup(name);
50 cgit_open_filter(ctx.repo->source_filter);
51 html_raw(buf, size);
52 cgit_close_filter(ctx.repo->source_filter);
53 free(ctx.repo->source_filter->argv[1]);
54 ctx.repo->source_filter->argv[1] = NULL;
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 unsigned char *sha1, char *path, const char *basename, const char *rev)
90 {
91 enum object_type type;
92 char *buf;
93 unsigned long size;
94
95 type = sha1_object_info(sha1, &size);
96 if (type == OBJ_BAD) {
97 cgit_print_error("Bad object name: %s", sha1_to_hex(sha1));
98 return;
99 }
100
101 buf = read_sha1_file(sha1, &type, &size);
102 if (!buf) {
103 cgit_print_error("Error reading object %s", sha1_to_hex(sha1));
104 return;
105 }
106
107 htmlf("blob: %s (", sha1_to_hex(sha1));
108 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
109 rev, path);
110 html(")\n");
111
112 if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
113 htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
114 size / 1024, ctx.cfg.max_blob_size);
115 return;
116 }
117
118 if (buffer_is_binary(buf, size))
119 print_binary_buffer(buf, size);
120 else
121 print_text_buffer(basename, buf, size);
122 }
123
124
125 static int ls_item(const unsigned char *sha1, const char *base, int baselen,
126 const char *pathname, unsigned int mode, int stage,
127 void *cbdata)
128 {
129 struct walk_tree_context *walk_tree_ctx = cbdata;
130 char *name;
131 struct strbuf fullpath = STRBUF_INIT;
132 struct strbuf class = STRBUF_INIT;
133 enum object_type type;
134 unsigned long size = 0;
135
136 name = xstrdup(pathname);
137 strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "",
138 ctx.qry.path ? "/" : "", name);
139
140 if (!S_ISGITLINK(mode)) {
141 type = sha1_object_info(sha1, &size);
142 if (type == OBJ_BAD) {
143 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
144 name,
145 sha1_to_hex(sha1));
146 return 0;
147 }
148 }
149
150 html("<tr><td class='ls-mode'>");
151 cgit_print_filemode(mode);
152 html("</td><td>");
153 if (S_ISGITLINK(mode)) {
154 cgit_submodule_link("ls-mod", fullpath.buf, sha1_to_hex(sha1));
155 } else if (S_ISDIR(mode)) {
156 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
157 walk_tree_ctx->curr_rev, fullpath.buf);
158 } else {
159 char *ext = strrchr(name, '.');
160 strbuf_addstr(&class, "ls-blob");
161 if (ext)
162 strbuf_addf(&class, " %s", ext + 1);
163 cgit_tree_link(name, NULL, class.buf, ctx.qry.head,
164 walk_tree_ctx->curr_rev, fullpath.buf);
165 }
166 htmlf("</td><td class='ls-size'>%li</td>", size);
167
168 html("<td>");
169 cgit_log_link("log", NULL, "button", ctx.qry.head,
170 walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL,
171 ctx.qry.showmsg);
172 if (ctx.repo->max_stats)
173 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
174 fullpath.buf);
175 if (!S_ISGITLINK(mode))
176 cgit_plain_link("plain", NULL, "button", ctx.qry.head,
177 walk_tree_ctx->curr_rev, fullpath.buf);
178 html("</td></tr>\n");
179 free(name);
180 strbuf_release(&fullpath);
181 strbuf_release(&class);
182 return 0;
183 }
184
185 static void ls_head()
186 {
187 html("<table summary='tree listing' class='list'>\n");
188 html("<tr class='nohover'>");
189 html("<th class='left'>Mode</th>");
190 html("<th class='left'>Name</th>");
191 html("<th class='right'>Size</th>");
192 html("<th/>");
193 html("</tr>\n");
194 }
195
196 static void ls_tail()
197 {
198 html("</table>\n");
199 }
200
201 static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_context *walk_tree_ctx)
202 {
203 struct tree *tree;
204 struct pathspec paths = {
205 .nr = 0
206 };
207
208 tree = parse_tree_indirect(sha1);
209 if (!tree) {
210 cgit_print_error("Not a tree object: %s", sha1_to_hex(sha1));
211 return;
212 }
213
214 ls_head();
215 read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
216 ls_tail();
217 }
218
219
220 static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
221 const char *pathname, unsigned mode, int stage,
222 void *cbdata)
223 {
224 struct walk_tree_context *walk_tree_ctx = cbdata;
225 static char buffer[PATH_MAX];
226
227 if (walk_tree_ctx->state == 0) {
228 memcpy(buffer, base, baselen);
229 strcpy(buffer + baselen, pathname);
230 if (strcmp(walk_tree_ctx->match_path, buffer))
231 return READ_TREE_RECURSIVE;
232
233 if (S_ISDIR(mode)) {
234 walk_tree_ctx->state = 1;
235 ls_head();
236 return READ_TREE_RECURSIVE;
237 } else {
238 print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev);
239 return 0;
240 }
241 }
242 ls_item(sha1, base, baselen, pathname, mode, stage, walk_tree_ctx);
243 return 0;
244 }
245
246
247 /*
248 * Show a tree or a blob
249 * rev: the commit pointing at the root tree object
250 * path: path to tree or blob
251 */
252 void cgit_print_tree(const char *rev, char *path)
253 {
254 unsigned char sha1[20];
255 struct commit *commit;
256 struct pathspec_item path_items = {
257 .match = path,
258 .len = path ? strlen(path) : 0
259 };
260 struct pathspec paths = {
261 .nr = path ? 1 : 0,
262 .items = &path_items
263 };
264 struct walk_tree_context walk_tree_ctx = {
265 .match_path = path,
266 .state = 0
267 };
268
269 if (!rev)
270 rev = ctx.qry.head;
271
272 if (get_sha1(rev, sha1)) {
273 cgit_print_error("Invalid revision name: %s", rev);
274 return;
275 }
276 commit = lookup_commit_reference(sha1);
277 if (!commit || parse_commit(commit)) {
278 cgit_print_error("Invalid commit reference: %s", rev);
279 return;
280 }
281
282 walk_tree_ctx.curr_rev = xstrdup(rev);
283
284 if (path == NULL) {
285 ls_tree(commit->tree->object.sha1, NULL, &walk_tree_ctx);
286 goto cleanup;
287 }
288
289 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
290 if (walk_tree_ctx.state == 1)
291 ls_tail();
292
293 cleanup:
294 free(walk_tree_ctx.curr_rev);
295 }