blob: 120066c425346f4b48d8061ed5f908da66138fc3 [file] [log] [blame]
Lars Hjemli06fe0c22006-12-13 00:13:27 +01001/* ui-tree.c: functions for tree output
2 *
Lukas Fleischerf7f26f82014-01-08 15:10:49 +01003 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemli06fe0c22006-12-13 00:13:27 +01004 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +010010#include "ui-tree.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010011#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010012#include "ui-shared.h"
Lars Hjemli06fe0c22006-12-13 00:13:27 +010013
Lukas Fleischer210a5712013-03-03 17:22:30 +010014struct walk_tree_context {
15 char *curr_rev;
16 char *match_path;
17 int state;
18};
Lars Hjemli06fe0c22006-12-13 00:13:27 +010019
Lars Hjemli46b7abe2009-07-31 16:55:27 +020020static void print_text_buffer(const char *name, char *buf, unsigned long size)
Lars Hjemliae1d4d72009-01-31 17:45:48 +010021{
22 unsigned long lineno, idx;
Peter Wu4468ec12013-10-03 12:17:23 +020023 const char *numberfmt = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
Lars Hjemliae1d4d72009-01-31 17:45:48 +010024
25 html("<table summary='blob content' class='blob'>\n");
Lars Hjemli46b7abe2009-07-31 16:55:27 +020026
Lars Hjemlib0f946b2009-08-21 14:26:52 +020027 if (ctx.cfg.enable_tree_linenumbers) {
Florian Pritzd67cc7f2009-08-09 20:42:45 +000028 html("<tr><td class='linenumbers'><pre>");
29 idx = 0;
30 lineno = 0;
Lukas Fleischer53bc7472013-03-03 16:04:29 +010031
Florian Pritzd67cc7f2009-08-09 20:42:45 +000032 if (size) {
33 htmlf(numberfmt, ++lineno);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -050034 while (idx < size - 1) { // skip absolute last newline
Florian Pritzd67cc7f2009-08-09 20:42:45 +000035 if (buf[idx] == '\n')
36 htmlf(numberfmt, ++lineno);
37 idx++;
38 }
Eric Wong11297362009-03-14 18:41:47 -070039 }
Florian Pritzd67cc7f2009-08-09 20:42:45 +000040 html("</pre></td>\n");
Lars Hjemliae1d4d72009-01-31 17:45:48 +010041 }
Florian Pritzd67cc7f2009-08-09 20:42:45 +000042 else {
43 html("<tr>\n");
44 }
Florian Pritz03389d62009-08-09 13:43:18 +000045
46 if (ctx.repo->source_filter) {
John Keeping3d8a6502014-01-12 17:13:50 +000047 char *filter_arg = xstrdup(name);
Florian Pritz03389d62009-08-09 13:43:18 +000048 html("<td class='lines'><pre><code>");
John Keeping3d8a6502014-01-12 17:13:50 +000049 cgit_open_filter(ctx.repo->source_filter, filter_arg);
Mark Lodatod187b982010-09-04 14:18:16 -040050 html_raw(buf, size);
Florian Pritz03389d62009-08-09 13:43:18 +000051 cgit_close_filter(ctx.repo->source_filter);
John Keeping3d8a6502014-01-12 17:13:50 +000052 free(filter_arg);
Florian Pritz03389d62009-08-09 13:43:18 +000053 html("</code></pre></td></tr></table>\n");
54 return;
55 }
56
Lars Hjemliae1d4d72009-01-31 17:45:48 +010057 html("<td class='lines'><pre><code>");
58 html_txt(buf);
59 html("</code></pre></td></tr></table>\n");
60}
61
Lars Hjemli6063e7b2009-02-12 11:26:14 +010062#define ROWLEN 32
63
Lars Hjemliae1d4d72009-01-31 17:45:48 +010064static void print_binary_buffer(char *buf, unsigned long size)
65{
66 unsigned long ofs, idx;
Lars Hjemli6063e7b2009-02-12 11:26:14 +010067 static char ascii[ROWLEN + 1];
Lars Hjemliae1d4d72009-01-31 17:45:48 +010068
69 html("<table summary='blob content' class='bin-blob'>\n");
70 html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
Lars Hjemli6063e7b2009-02-12 11:26:14 +010071 for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
Mark Lodatoe4ddc8f2010-09-04 11:30:18 -040072 htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs);
Lars Hjemli6063e7b2009-02-12 11:26:14 +010073 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
Lars Hjemliae1d4d72009-01-31 17:45:48 +010074 htmlf("%*s%02x",
75 idx == 16 ? 4 : 1, "",
76 buf[idx] & 0xff);
77 html(" </td><td class='hex'>");
Lars Hjemli6063e7b2009-02-12 11:26:14 +010078 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);
Lars Hjemliae1d4d72009-01-31 17:45:48 +010082 html("</td></tr>\n");
83 }
84 html("</table>\n");
85}
86
Jason A. Donenfeld23f7dad2016-01-18 15:56:45 +010087static 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
Lukas Fleischerfb5a3732013-03-03 16:45:14 +0100118static void print_object(const unsigned char *sha1, char *path, const char *basename, const char *rev)
Lars Hjemliffc69732007-06-16 20:20:42 +0200119{
120 enum object_type type;
Lars Hjemli0835ffe2007-09-20 00:21:47 +0200121 char *buf;
Lars Hjemliae1d4d72009-01-31 17:45:48 +0100122 unsigned long size;
Lars Hjemliffc69732007-06-16 20:20:42 +0200123
124 type = sha1_object_info(sha1, &size);
125 if (type == OBJ_BAD) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100126 cgit_print_error_page(404, "Not found",
127 "Bad object name: %s", sha1_to_hex(sha1));
Lars Hjemliffc69732007-06-16 20:20:42 +0200128 return;
129 }
130
131 buf = read_sha1_file(sha1, &type, &size);
132 if (!buf) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100133 cgit_print_error_page(500, "Internal server error",
134 "Error reading object %s", sha1_to_hex(sha1));
Lars Hjemliffc69732007-06-16 20:20:42 +0200135 return;
136 }
137
Jason A. Donenfeld23f7dad2016-01-18 15:56:45 +0100138 set_title_from_path(path);
139
John Keeping9c70c0b2015-08-14 12:47:20 +0100140 cgit_print_layout_start();
Johan Herland48f7b982010-06-10 01:09:30 +0200141 htmlf("blob: %s (", sha1_to_hex(sha1));
Lars Hjemli65b7b872008-08-06 11:07:13 +0200142 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
Lukas Fleischerfb5a3732013-03-03 16:45:14 +0100143 rev, path);
Johan Herland48f7b982010-06-10 01:09:30 +0200144 html(")\n");
Michael Krelind6b01da2007-07-21 19:51:47 +0200145
Georg Lukasef07ccc2009-11-28 03:44:33 +0100146 if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
Mark Lodatoe4ddc8f2010-09-04 11:30:18 -0400147 htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
Georg Lukasef07ccc2009-11-28 03:44:33 +0100148 size / 1024, ctx.cfg.max_blob_size);
149 return;
150 }
151
Lars Hjemliae1d4d72009-01-31 17:45:48 +0100152 if (buffer_is_binary(buf, size))
153 print_binary_buffer(buf, size);
154 else
Lars Hjemli46b7abe2009-07-31 16:55:27 +0200155 print_text_buffer(basename, buf, size);
Lars Hjemliffc69732007-06-16 20:20:42 +0200156}
157
158
Christian Hesse7358f632015-02-07 14:18:28 +0100159static int ls_item(const unsigned char *sha1, struct strbuf *base,
160 const char *pathname, unsigned mode, int stage, void *cbdata)
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100161{
Lukas Fleischer210a5712013-03-03 17:22:30 +0100162 struct walk_tree_context *walk_tree_ctx = cbdata;
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100163 char *name;
John Keepingfb3655d2013-04-06 10:28:57 +0100164 struct strbuf fullpath = STRBUF_INIT;
165 struct strbuf class = STRBUF_INIT;
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200166 enum object_type type;
Lars Hjemlided93932007-05-11 12:12:48 +0200167 unsigned long size = 0;
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100168
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200169 name = xstrdup(pathname);
John Keepingfb3655d2013-04-06 10:28:57 +0100170 strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "",
171 ctx.qry.path ? "/" : "", name);
Lars Hjemli44947bf2007-06-17 01:23:08 +0200172
Lars Hjemli08a87572008-05-20 22:32:22 +0200173 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));
Christian Hesse896cd692015-10-09 13:15:44 +0200179 free(name);
Lars Hjemli08a87572008-05-20 22:32:22 +0200180 return 0;
181 }
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100182 }
Lars Hjemli44947bf2007-06-17 01:23:08 +0200183
Lars Hjemli426032f2007-06-17 13:17:00 +0200184 html("<tr><td class='ls-mode'>");
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +0100185 cgit_print_filemode(mode);
Lars Hjemli426032f2007-06-17 13:17:00 +0200186 html("</td><td>");
Jeffrey C. Olliee651cb02007-06-04 12:28:56 -0500187 if (S_ISGITLINK(mode)) {
John Keepingfb3655d2013-04-06 10:28:57 +0100188 cgit_submodule_link("ls-mod", fullpath.buf, sha1_to_hex(sha1));
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200189 } else if (S_ISDIR(mode)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100190 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
John Keepingfb3655d2013-04-06 10:28:57 +0100191 walk_tree_ctx->curr_rev, fullpath.buf);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100192 } else {
John Keepingfb3655d2013-04-06 10:28:57 +0100193 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);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100199 }
Lars Hjemli426032f2007-06-17 13:17:00 +0200200 htmlf("</td><td class='ls-size'>%li</td>", size);
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200201
Lars Hjemli48c487d2007-06-17 13:57:51 +0200202 html("<td>");
Lukas Fleischer210a5712013-03-03 17:22:30 +0100203 cgit_log_link("log", NULL, "button", ctx.qry.head,
John Keepingfb3655d2013-04-06 10:28:57 +0100204 walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL,
John Keeping30304d82015-08-12 15:55:28 +0100205 ctx.qry.showmsg, 0);
Lars Hjemli837d4642008-12-07 13:34:42 +0100206 if (ctx.repo->max_stats)
207 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
John Keepingfb3655d2013-04-06 10:28:57 +0100208 fullpath.buf);
Lars Hjemli6857bec2011-06-15 10:04:13 +0200209 if (!S_ISGITLINK(mode))
Lukas Fleischer210a5712013-03-03 17:22:30 +0100210 cgit_plain_link("plain", NULL, "button", ctx.qry.head,
John Keepingfb3655d2013-04-06 10:28:57 +0100211 walk_tree_ctx->curr_rev, fullpath.buf);
Lars Hjemli48c487d2007-06-17 13:57:51 +0200212 html("</td></tr>\n");
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100213 free(name);
John Keepingfb3655d2013-04-06 10:28:57 +0100214 strbuf_release(&fullpath);
215 strbuf_release(&class);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100216 return 0;
217}
218
John Keepinge3d3fff2015-03-08 16:32:16 +0000219static void ls_head(void)
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100220{
John Keeping9c70c0b2015-08-14 12:47:20 +0100221 cgit_print_layout_start();
Lars Hjemli29154832007-11-11 13:04:28 +0100222 html("<table summary='tree listing' class='list'>\n");
Lars Hjemli777faf72007-01-28 00:39:26 +0100223 html("<tr class='nohover'>");
Lars Hjemlia5304282006-12-17 23:55:53 +0100224 html("<th class='left'>Mode</th>");
225 html("<th class='left'>Name</th>");
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100226 html("<th class='right'>Size</th>");
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200227 html("<th/>");
Lars Hjemlia5304282006-12-17 23:55:53 +0100228 html("</tr>\n");
Lars Hjemliffc69732007-06-16 20:20:42 +0200229}
230
John Keepinge3d3fff2015-03-08 16:32:16 +0000231static void ls_tail(void)
Lars Hjemliffc69732007-06-16 20:20:42 +0200232{
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100233 html("</table>\n");
John Keeping9c70c0b2015-08-14 12:47:20 +0100234 cgit_print_layout_end();
Lars Hjemliffc69732007-06-16 20:20:42 +0200235}
236
Lukas Fleischer210a5712013-03-03 17:22:30 +0100237static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_context *walk_tree_ctx)
Lars Hjemliffc69732007-06-16 20:20:42 +0200238{
239 struct tree *tree;
John Keepingc1633c62013-03-02 12:32:11 +0000240 struct pathspec paths = {
241 .nr = 0
242 };
Lars Hjemliffc69732007-06-16 20:20:42 +0200243
244 tree = parse_tree_indirect(sha1);
245 if (!tree) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100246 cgit_print_error_page(404, "Not found",
247 "Not a tree object: %s", sha1_to_hex(sha1));
Lars Hjemliffc69732007-06-16 20:20:42 +0200248 return;
249 }
250
251 ls_head();
Lukas Fleischer210a5712013-03-03 17:22:30 +0100252 read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
Lars Hjemliffc69732007-06-16 20:20:42 +0200253 ls_tail();
254}
255
256
Christian Hesse7358f632015-02-07 14:18:28 +0100257static int walk_tree(const unsigned char *sha1, struct strbuf *base,
258 const char *pathname, unsigned mode, int stage, void *cbdata)
Lars Hjemliffc69732007-06-16 20:20:42 +0200259{
Lukas Fleischer210a5712013-03-03 17:22:30 +0100260 struct walk_tree_context *walk_tree_ctx = cbdata;
Lars Hjemliffc69732007-06-16 20:20:42 +0200261 static char buffer[PATH_MAX];
Lars Hjemliffc69732007-06-16 20:20:42 +0200262
Lukas Fleischer210a5712013-03-03 17:22:30 +0100263 if (walk_tree_ctx->state == 0) {
Christian Hesse7358f632015-02-07 14:18:28 +0100264 memcpy(buffer, base->buf, base->len);
265 strcpy(buffer + base->len, pathname);
Lukas Fleischer210a5712013-03-03 17:22:30 +0100266 if (strcmp(walk_tree_ctx->match_path, buffer))
Lars Hjemliffc69732007-06-16 20:20:42 +0200267 return READ_TREE_RECURSIVE;
268
269 if (S_ISDIR(mode)) {
Lukas Fleischer210a5712013-03-03 17:22:30 +0100270 walk_tree_ctx->state = 1;
Jason A. Donenfeld23f7dad2016-01-18 15:56:45 +0100271 set_title_from_path(buffer);
Lars Hjemliffc69732007-06-16 20:20:42 +0200272 ls_head();
273 return READ_TREE_RECURSIVE;
274 } else {
John Keeping9c70c0b2015-08-14 12:47:20 +0100275 walk_tree_ctx->state = 2;
Lukas Fleischer210a5712013-03-03 17:22:30 +0100276 print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev);
Lars Hjemliffc69732007-06-16 20:20:42 +0200277 return 0;
278 }
279 }
Christian Hesse7358f632015-02-07 14:18:28 +0100280 ls_item(sha1, base, pathname, mode, stage, walk_tree_ctx);
Lars Hjemliffc69732007-06-16 20:20:42 +0200281 return 0;
282}
283
Lars Hjemliffc69732007-06-16 20:20:42 +0200284/*
285 * Show a tree or a blob
286 * rev: the commit pointing at the root tree object
287 * path: path to tree or blob
288 */
289void cgit_print_tree(const char *rev, char *path)
290{
291 unsigned char sha1[20];
292 struct commit *commit;
John Keepingc1633c62013-03-02 12:32:11 +0000293 struct pathspec_item path_items = {
294 .match = path,
295 .len = path ? strlen(path) : 0
296 };
297 struct pathspec paths = {
298 .nr = path ? 1 : 0,
299 .items = &path_items
300 };
Lukas Fleischer210a5712013-03-03 17:22:30 +0100301 struct walk_tree_context walk_tree_ctx = {
302 .match_path = path,
303 .state = 0
304 };
Lars Hjemliffc69732007-06-16 20:20:42 +0200305
306 if (!rev)
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100307 rev = ctx.qry.head;
Lars Hjemliffc69732007-06-16 20:20:42 +0200308
Lars Hjemliffc69732007-06-16 20:20:42 +0200309 if (get_sha1(rev, sha1)) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100310 cgit_print_error_page(404, "Not found",
311 "Invalid revision name: %s", rev);
Lars Hjemliffc69732007-06-16 20:20:42 +0200312 return;
313 }
314 commit = lookup_commit_reference(sha1);
315 if (!commit || parse_commit(commit)) {
John Keeping9c70c0b2015-08-14 12:47:20 +0100316 cgit_print_error_page(404, "Not found",
317 "Invalid commit reference: %s", rev);
Lars Hjemliffc69732007-06-16 20:20:42 +0200318 return;
319 }
320
Lukas Fleischer985d6ca2013-03-04 13:25:36 +0100321 walk_tree_ctx.curr_rev = xstrdup(rev);
322
Lars Hjemliffc69732007-06-16 20:20:42 +0200323 if (path == NULL) {
Christian Hesse559ab5e2016-01-05 07:38:53 +0100324 ls_tree(commit->tree->object.oid.hash, NULL, &walk_tree_ctx);
Lukas Fleischer985d6ca2013-03-04 13:25:36 +0100325 goto cleanup;
Lars Hjemliffc69732007-06-16 20:20:42 +0200326 }
327
Lukas Fleischer210a5712013-03-03 17:22:30 +0100328 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
329 if (walk_tree_ctx.state == 1)
Lukas Fleischerbfe57662013-03-03 16:55:21 +0100330 ls_tail();
John Keeping9c70c0b2015-08-14 12:47:20 +0100331 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");
Lukas Fleischer985d6ca2013-03-04 13:25:36 +0100335
336cleanup:
337 free(walk_tree_ctx.curr_rev);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100338}