ui-shared: add homepage to tabs
[cgit.git] / ui-tree.c
blob120066c425346f4b48d8061ed5f908da66138fc3
1 /* ui-tree.c: functions for tree output
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
9 #include "cgit.h"
10 #include "ui-tree.h"
11 #include "html.h"
12 #include "ui-shared.h"
14 struct walk_tree_context {
15 char *curr_rev;
16 char *match_path;
17 int state;
20 static void print_text_buffer(const char *name, char *buf, unsigned long size)
22 unsigned long lineno, idx;
23 const char *numberfmt = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
25 html("<table summary='blob content' class='blob'>\n");
27 if (ctx.cfg.enable_tree_linenumbers) {
28 html("<tr><td class='linenumbers'><pre>");
29 idx = 0;
30 lineno = 0;
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++;
40 html("</pre></td>\n");
42 else {
43 html("<tr>\n");
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;
57 html("<td class='lines'><pre><code>");
58 html_txt(buf);
59 html("</code></pre></td></tr></table>\n");
62 #define ROWLEN 32
64 static void print_binary_buffer(char *buf, unsigned long size)
66 unsigned long ofs, idx;
67 static char ascii[ROWLEN + 1];
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");
84 html("</table>\n");
87 static void set_title_from_path(const char *path)
89 size_t path_len, path_index, path_last_end;
90 char *new_title;
92 if (!path)
93 return;
95 path_len = strlen(path);
96 new_title = xmalloc(path_len + 3 + strlen(ctx.page.title) + 1);
97 new_title[0] = '\0';
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;
105 strncat(new_title, &path[path_index + 1], path_last_end - path_index - 1);
106 strcat(new_title, "\\");
107 path_last_end = path_index;
110 if (path_last_end)
111 strncat(new_title, path, path_last_end);
113 strcat(new_title, " - ");
114 strcat(new_title, ctx.page.title);
115 ctx.page.title = new_title;
118 static void print_object(const unsigned char *sha1, char *path, const char *basename, const char *rev)
120 enum object_type type;
121 char *buf;
122 unsigned long size;
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;
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;
138 set_title_from_path(path);
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");
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;
152 if (buffer_is_binary(buf, size))
153 print_binary_buffer(buf, size);
154 else
155 print_text_buffer(basename, buf, size);
159 static int ls_item(const unsigned char *sha1, struct strbuf *base,
160 const char *pathname, unsigned mode, int stage, void *cbdata)
162 struct walk_tree_context *walk_tree_ctx = cbdata;
163 char *name;
164 struct strbuf fullpath = STRBUF_INIT;
165 struct strbuf class = STRBUF_INIT;
166 enum object_type type;
167 unsigned long size = 0;
169 name = xstrdup(pathname);
170 strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "",
171 ctx.qry.path ? "/" : "", name);
173 if (!S_ISGITLINK(mode)) {
174 type = sha1_object_info(sha1, &size);
175 if (type == OBJ_BAD) {
176 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
177 name,
178 sha1_to_hex(sha1));
179 free(name);
180 return 0;
184 html("<tr><td class='ls-mode'>");
185 cgit_print_filemode(mode);
186 html("</td><td>");
187 if (S_ISGITLINK(mode)) {
188 cgit_submodule_link("ls-mod", fullpath.buf, sha1_to_hex(sha1));
189 } else if (S_ISDIR(mode)) {
190 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
191 walk_tree_ctx->curr_rev, fullpath.buf);
192 } else {
193 char *ext = strrchr(name, '.');
194 strbuf_addstr(&class, "ls-blob");
195 if (ext)
196 strbuf_addf(&class, " %s", ext + 1);
197 cgit_tree_link(name, NULL, class.buf, ctx.qry.head,
198 walk_tree_ctx->curr_rev, fullpath.buf);
200 htmlf("</td><td class='ls-size'>%li</td>", size);
202 html("<td>");
203 cgit_log_link("log", NULL, "button", ctx.qry.head,
204 walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL,
205 ctx.qry.showmsg, 0);
206 if (ctx.repo->max_stats)
207 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
208 fullpath.buf);
209 if (!S_ISGITLINK(mode))
210 cgit_plain_link("plain", NULL, "button", ctx.qry.head,
211 walk_tree_ctx->curr_rev, fullpath.buf);
212 html("</td></tr>\n");
213 free(name);
214 strbuf_release(&fullpath);
215 strbuf_release(&class);
216 return 0;
219 static void ls_head(void)
221 cgit_print_layout_start();
222 html("<table summary='tree listing' class='list'>\n");
223 html("<tr class='nohover'>");
224 html("<th class='left'>Mode</th>");
225 html("<th class='left'>Name</th>");
226 html("<th class='right'>Size</th>");
227 html("<th/>");
228 html("</tr>\n");
231 static void ls_tail(void)
233 html("</table>\n");
234 cgit_print_layout_end();
237 static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_context *walk_tree_ctx)
239 struct tree *tree;
240 struct pathspec paths = {
241 .nr = 0
244 tree = parse_tree_indirect(sha1);
245 if (!tree) {
246 cgit_print_error_page(404, "Not found",
247 "Not a tree object: %s", sha1_to_hex(sha1));
248 return;
251 ls_head();
252 read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
253 ls_tail();
257 static int walk_tree(const unsigned char *sha1, struct strbuf *base,
258 const char *pathname, unsigned mode, int stage, void *cbdata)
260 struct walk_tree_context *walk_tree_ctx = cbdata;
261 static char buffer[PATH_MAX];
263 if (walk_tree_ctx->state == 0) {
264 memcpy(buffer, base->buf, base->len);
265 strcpy(buffer + base->len, pathname);
266 if (strcmp(walk_tree_ctx->match_path, buffer))
267 return READ_TREE_RECURSIVE;
269 if (S_ISDIR(mode)) {
270 walk_tree_ctx->state = 1;
271 set_title_from_path(buffer);
272 ls_head();
273 return READ_TREE_RECURSIVE;
274 } else {
275 walk_tree_ctx->state = 2;
276 print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev);
277 return 0;
280 ls_item(sha1, base, pathname, mode, stage, walk_tree_ctx);
281 return 0;
285 * Show a tree or a blob
286 * rev: the commit pointing at the root tree object
287 * path: path to tree or blob
289 void cgit_print_tree(const char *rev, char *path)
291 unsigned char sha1[20];
292 struct commit *commit;
293 struct pathspec_item path_items = {
294 .match = path,
295 .len = path ? strlen(path) : 0
297 struct pathspec paths = {
298 .nr = path ? 1 : 0,
299 .items = &path_items
301 struct walk_tree_context walk_tree_ctx = {
302 .match_path = path,
303 .state = 0
306 if (!rev)
307 rev = ctx.qry.head;
309 if (get_sha1(rev, sha1)) {
310 cgit_print_error_page(404, "Not found",
311 "Invalid revision name: %s", rev);
312 return;
314 commit = lookup_commit_reference(sha1);
315 if (!commit || parse_commit(commit)) {
316 cgit_print_error_page(404, "Not found",
317 "Invalid commit reference: %s", rev);
318 return;
321 walk_tree_ctx.curr_rev = xstrdup(rev);
323 if (path == NULL) {
324 ls_tree(commit->tree->object.oid.hash, NULL, &walk_tree_ctx);
325 goto cleanup;
328 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
329 if (walk_tree_ctx.state == 1)
330 ls_tail();
331 else if (walk_tree_ctx.state == 2)
332 cgit_print_layout_end();
333 else
334 cgit_print_error_page(404, "Not found", "Path not found");
336 cleanup:
337 free(walk_tree_ctx.curr_rev);