blob: d7135532baf8c6f69da6fe2b5c01599f75b351ee [file] [log] [blame]
Lars Hjemli06fe0c22006-12-13 00:13:27 +01001/* 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
Lars Hjemliae1d4d72009-01-31 17:45:48 +01009#include <ctype.h>
Lars Hjemli06fe0c22006-12-13 00:13:27 +010010#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +010011#include "ui-tree.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010012#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010013#include "ui-shared.h"
Lars Hjemli06fe0c22006-12-13 00:13:27 +010014
Lukas Fleischer210a5712013-03-03 17:22:30 +010015struct walk_tree_context {
16 char *curr_rev;
17 char *match_path;
18 int state;
19};
Lars Hjemli06fe0c22006-12-13 00:13:27 +010020
Lars Hjemli46b7abe2009-07-31 16:55:27 +020021static void print_text_buffer(const char *name, char *buf, unsigned long size)
Lars Hjemliae1d4d72009-01-31 17:45:48 +010022{
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");
Lars Hjemli46b7abe2009-07-31 16:55:27 +020028
Lars Hjemlib0f946b2009-08-21 14:26:52 +020029 if (ctx.cfg.enable_tree_linenumbers) {
Florian Pritzd67cc7f2009-08-09 20:42:45 +000030 html("<tr><td class='linenumbers'><pre>");
31 idx = 0;
32 lineno = 0;
Lukas Fleischer53bc7472013-03-03 16:04:29 +010033
Florian Pritzd67cc7f2009-08-09 20:42:45 +000034 if (size) {
35 htmlf(numberfmt, ++lineno);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -050036 while (idx < size - 1) { // skip absolute last newline
Florian Pritzd67cc7f2009-08-09 20:42:45 +000037 if (buf[idx] == '\n')
38 htmlf(numberfmt, ++lineno);
39 idx++;
40 }
Eric Wong11297362009-03-14 18:41:47 -070041 }
Florian Pritzd67cc7f2009-08-09 20:42:45 +000042 html("</pre></td>\n");
Lars Hjemliae1d4d72009-01-31 17:45:48 +010043 }
Florian Pritzd67cc7f2009-08-09 20:42:45 +000044 else {
45 html("<tr>\n");
46 }
Florian Pritz03389d62009-08-09 13:43:18 +000047
48 if (ctx.repo->source_filter) {
49 html("<td class='lines'><pre><code>");
50 ctx.repo->source_filter->argv[1] = xstrdup(name);
Lars Hjemli3ec6b302011-06-06 19:29:58 +000051 cgit_open_filter(ctx.repo->source_filter);
Mark Lodatod187b982010-09-04 14:18:16 -040052 html_raw(buf, size);
Florian Pritz03389d62009-08-09 13:43:18 +000053 cgit_close_filter(ctx.repo->source_filter);
Ferry Huberts3f1ebd32011-03-09 08:16:58 +010054 free(ctx.repo->source_filter->argv[1]);
55 ctx.repo->source_filter->argv[1] = NULL;
Florian Pritz03389d62009-08-09 13:43:18 +000056 html("</code></pre></td></tr></table>\n");
57 return;
58 }
59
Lars Hjemliae1d4d72009-01-31 17:45:48 +010060 html("<td class='lines'><pre><code>");
61 html_txt(buf);
62 html("</code></pre></td></tr></table>\n");
63}
64
Lars Hjemli6063e7b2009-02-12 11:26:14 +010065#define ROWLEN 32
66
Lars Hjemliae1d4d72009-01-31 17:45:48 +010067static void print_binary_buffer(char *buf, unsigned long size)
68{
69 unsigned long ofs, idx;
Lars Hjemli6063e7b2009-02-12 11:26:14 +010070 static char ascii[ROWLEN + 1];
Lars Hjemliae1d4d72009-01-31 17:45:48 +010071
72 html("<table summary='blob content' class='bin-blob'>\n");
73 html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
Lars Hjemli6063e7b2009-02-12 11:26:14 +010074 for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
Mark Lodatoe4ddc8f2010-09-04 11:30:18 -040075 htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs);
Lars Hjemli6063e7b2009-02-12 11:26:14 +010076 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
Lars Hjemliae1d4d72009-01-31 17:45:48 +010077 htmlf("%*s%02x",
78 idx == 16 ? 4 : 1, "",
79 buf[idx] & 0xff);
80 html(" </td><td class='hex'>");
Lars Hjemli6063e7b2009-02-12 11:26:14 +010081 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);
Lars Hjemliae1d4d72009-01-31 17:45:48 +010085 html("</td></tr>\n");
86 }
87 html("</table>\n");
88}
89
Lukas Fleischerfb5a3732013-03-03 16:45:14 +010090static void print_object(const unsigned char *sha1, char *path, const char *basename, const char *rev)
Lars Hjemliffc69732007-06-16 20:20:42 +020091{
92 enum object_type type;
Lars Hjemli0835ffe2007-09-20 00:21:47 +020093 char *buf;
Lars Hjemliae1d4d72009-01-31 17:45:48 +010094 unsigned long size;
Lars Hjemliffc69732007-06-16 20:20:42 +020095
96 type = sha1_object_info(sha1, &size);
97 if (type == OBJ_BAD) {
98 cgit_print_error(fmt("Bad object name: %s",
99 sha1_to_hex(sha1)));
100 return;
101 }
102
103 buf = read_sha1_file(sha1, &type, &size);
104 if (!buf) {
105 cgit_print_error(fmt("Error reading object %s",
106 sha1_to_hex(sha1)));
107 return;
108 }
109
Johan Herland48f7b982010-06-10 01:09:30 +0200110 htmlf("blob: %s (", sha1_to_hex(sha1));
Lars Hjemli65b7b872008-08-06 11:07:13 +0200111 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
Lukas Fleischerfb5a3732013-03-03 16:45:14 +0100112 rev, path);
Johan Herland48f7b982010-06-10 01:09:30 +0200113 html(")\n");
Michael Krelind6b01da2007-07-21 19:51:47 +0200114
Georg Lukasef07ccc2009-11-28 03:44:33 +0100115 if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
Mark Lodatoe4ddc8f2010-09-04 11:30:18 -0400116 htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
Georg Lukasef07ccc2009-11-28 03:44:33 +0100117 size / 1024, ctx.cfg.max_blob_size);
118 return;
119 }
120
Lars Hjemliae1d4d72009-01-31 17:45:48 +0100121 if (buffer_is_binary(buf, size))
122 print_binary_buffer(buf, size);
123 else
Lars Hjemli46b7abe2009-07-31 16:55:27 +0200124 print_text_buffer(basename, buf, size);
Lars Hjemliffc69732007-06-16 20:20:42 +0200125}
126
127
128static int ls_item(const unsigned char *sha1, const char *base, int baselen,
Lars Hjemli566f92b2008-07-21 10:10:48 +0200129 const char *pathname, unsigned int mode, int stage,
130 void *cbdata)
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100131{
Lukas Fleischer210a5712013-03-03 17:22:30 +0100132 struct walk_tree_context *walk_tree_ctx = cbdata;
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100133 char *name;
Lars Hjemli44947bf2007-06-17 01:23:08 +0200134 char *fullpath;
Martin Szuleckib4c35622009-08-07 14:06:02 +0200135 char *class;
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200136 enum object_type type;
Lars Hjemlided93932007-05-11 12:12:48 +0200137 unsigned long size = 0;
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100138
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200139 name = xstrdup(pathname);
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100140 fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "",
141 ctx.qry.path ? "/" : "", name);
Lars Hjemli44947bf2007-06-17 01:23:08 +0200142
Lars Hjemli08a87572008-05-20 22:32:22 +0200143 if (!S_ISGITLINK(mode)) {
144 type = sha1_object_info(sha1, &size);
145 if (type == OBJ_BAD) {
146 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
147 name,
148 sha1_to_hex(sha1));
149 return 0;
150 }
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100151 }
Lars Hjemli44947bf2007-06-17 01:23:08 +0200152
Lars Hjemli426032f2007-06-17 13:17:00 +0200153 html("<tr><td class='ls-mode'>");
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +0100154 cgit_print_filemode(mode);
Lars Hjemli426032f2007-06-17 13:17:00 +0200155 html("</td><td>");
Jeffrey C. Olliee651cb02007-06-04 12:28:56 -0500156 if (S_ISGITLINK(mode)) {
Lars Hjemli6857bec2011-06-15 10:04:13 +0200157 cgit_submodule_link("ls-mod", fullpath, sha1_to_hex(sha1));
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200158 } else if (S_ISDIR(mode)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100159 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
Lukas Fleischer210a5712013-03-03 17:22:30 +0100160 walk_tree_ctx->curr_rev, fullpath);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100161 } else {
Martin Szuleckib4c35622009-08-07 14:06:02 +0200162 class = strrchr(name, '.');
163 if (class != NULL) {
164 class = fmt("ls-blob %s", class + 1);
165 } else
166 class = "ls-blob";
167 cgit_tree_link(name, NULL, class, ctx.qry.head,
Lukas Fleischer210a5712013-03-03 17:22:30 +0100168 walk_tree_ctx->curr_rev, fullpath);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100169 }
Lars Hjemli426032f2007-06-17 13:17:00 +0200170 htmlf("</td><td class='ls-size'>%li</td>", size);
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200171
Lars Hjemli48c487d2007-06-17 13:57:51 +0200172 html("<td>");
Lukas Fleischer210a5712013-03-03 17:22:30 +0100173 cgit_log_link("log", NULL, "button", ctx.qry.head,
174 walk_tree_ctx->curr_rev, fullpath, 0, NULL, NULL,
175 ctx.qry.showmsg);
Lars Hjemli837d4642008-12-07 13:34:42 +0100176 if (ctx.repo->max_stats)
177 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
178 fullpath);
Lars Hjemli6857bec2011-06-15 10:04:13 +0200179 if (!S_ISGITLINK(mode))
Lukas Fleischer210a5712013-03-03 17:22:30 +0100180 cgit_plain_link("plain", NULL, "button", ctx.qry.head,
181 walk_tree_ctx->curr_rev, fullpath);
Lars Hjemli48c487d2007-06-17 13:57:51 +0200182 html("</td></tr>\n");
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100183 free(name);
184 return 0;
185}
186
Lars Hjemliffc69732007-06-16 20:20:42 +0200187static void ls_head()
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100188{
Lars Hjemli29154832007-11-11 13:04:28 +0100189 html("<table summary='tree listing' class='list'>\n");
Lars Hjemli777faf72007-01-28 00:39:26 +0100190 html("<tr class='nohover'>");
Lars Hjemlia5304282006-12-17 23:55:53 +0100191 html("<th class='left'>Mode</th>");
192 html("<th class='left'>Name</th>");
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100193 html("<th class='right'>Size</th>");
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200194 html("<th/>");
Lars Hjemlia5304282006-12-17 23:55:53 +0100195 html("</tr>\n");
Lars Hjemliffc69732007-06-16 20:20:42 +0200196}
197
198static void ls_tail()
199{
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100200 html("</table>\n");
Lars Hjemliffc69732007-06-16 20:20:42 +0200201}
202
Lukas Fleischer210a5712013-03-03 17:22:30 +0100203static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_context *walk_tree_ctx)
Lars Hjemliffc69732007-06-16 20:20:42 +0200204{
205 struct tree *tree;
John Keepingc1633c62013-03-02 12:32:11 +0000206 struct pathspec paths = {
207 .nr = 0
208 };
Lars Hjemliffc69732007-06-16 20:20:42 +0200209
210 tree = parse_tree_indirect(sha1);
211 if (!tree) {
212 cgit_print_error(fmt("Not a tree object: %s",
213 sha1_to_hex(sha1)));
214 return;
215 }
216
217 ls_head();
Lukas Fleischer210a5712013-03-03 17:22:30 +0100218 read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
Lars Hjemliffc69732007-06-16 20:20:42 +0200219 ls_tail();
220}
221
222
223static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
Lars Hjemli566f92b2008-07-21 10:10:48 +0200224 const char *pathname, unsigned mode, int stage,
225 void *cbdata)
Lars Hjemliffc69732007-06-16 20:20:42 +0200226{
Lukas Fleischer210a5712013-03-03 17:22:30 +0100227 struct walk_tree_context *walk_tree_ctx = cbdata;
Lars Hjemliffc69732007-06-16 20:20:42 +0200228 static char buffer[PATH_MAX];
Lars Hjemliffc69732007-06-16 20:20:42 +0200229
Lukas Fleischer210a5712013-03-03 17:22:30 +0100230 if (walk_tree_ctx->state == 0) {
Lars Hjemliffc69732007-06-16 20:20:42 +0200231 memcpy(buffer, base, baselen);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100232 strcpy(buffer + baselen, pathname);
Lukas Fleischer210a5712013-03-03 17:22:30 +0100233 if (strcmp(walk_tree_ctx->match_path, buffer))
Lars Hjemliffc69732007-06-16 20:20:42 +0200234 return READ_TREE_RECURSIVE;
235
236 if (S_ISDIR(mode)) {
Lukas Fleischer210a5712013-03-03 17:22:30 +0100237 walk_tree_ctx->state = 1;
Lars Hjemliffc69732007-06-16 20:20:42 +0200238 ls_head();
239 return READ_TREE_RECURSIVE;
240 } else {
Lukas Fleischer210a5712013-03-03 17:22:30 +0100241 print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev);
Lars Hjemliffc69732007-06-16 20:20:42 +0200242 return 0;
243 }
244 }
Lukas Fleischer210a5712013-03-03 17:22:30 +0100245 ls_item(sha1, base, baselen, pathname, mode, stage, walk_tree_ctx);
Lars Hjemliffc69732007-06-16 20:20:42 +0200246 return 0;
247}
248
249
250/*
251 * Show a tree or a blob
252 * rev: the commit pointing at the root tree object
253 * path: path to tree or blob
254 */
255void cgit_print_tree(const char *rev, char *path)
256{
257 unsigned char sha1[20];
258 struct commit *commit;
John Keepingc1633c62013-03-02 12:32:11 +0000259 struct pathspec_item path_items = {
260 .match = path,
261 .len = path ? strlen(path) : 0
262 };
263 struct pathspec paths = {
264 .nr = path ? 1 : 0,
265 .items = &path_items
266 };
Lukas Fleischer210a5712013-03-03 17:22:30 +0100267 struct walk_tree_context walk_tree_ctx = {
268 .match_path = path,
269 .state = 0
270 };
Lars Hjemliffc69732007-06-16 20:20:42 +0200271
272 if (!rev)
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100273 rev = ctx.qry.head;
Lars Hjemliffc69732007-06-16 20:20:42 +0200274
Lars Hjemliffc69732007-06-16 20:20:42 +0200275 if (get_sha1(rev, sha1)) {
276 cgit_print_error(fmt("Invalid revision name: %s", rev));
277 return;
278 }
279 commit = lookup_commit_reference(sha1);
280 if (!commit || parse_commit(commit)) {
281 cgit_print_error(fmt("Invalid commit reference: %s", rev));
282 return;
283 }
284
Lukas Fleischer985d6ca2013-03-04 13:25:36 +0100285 walk_tree_ctx.curr_rev = xstrdup(rev);
286
Lars Hjemliffc69732007-06-16 20:20:42 +0200287 if (path == NULL) {
Lukas Fleischer210a5712013-03-03 17:22:30 +0100288 ls_tree(commit->tree->object.sha1, NULL, &walk_tree_ctx);
Lukas Fleischer985d6ca2013-03-04 13:25:36 +0100289 goto cleanup;
Lars Hjemliffc69732007-06-16 20:20:42 +0200290 }
291
Lukas Fleischer210a5712013-03-03 17:22:30 +0100292 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
293 if (walk_tree_ctx.state == 1)
Lukas Fleischerbfe57662013-03-03 16:55:21 +0100294 ls_tail();
Lukas Fleischer985d6ca2013-03-04 13:25:36 +0100295
296cleanup:
297 free(walk_tree_ctx.curr_rev);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100298}