blob: c8d24f653553c4116f5759dabb94b06710de4f06 [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
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;
Peter Wu4468ec12013-10-03 12:17:23 +020024 const char *numberfmt = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
Lars Hjemliae1d4d72009-01-31 17:45:48 +010025
26 html("<table summary='blob content' class='blob'>\n");
Lars Hjemli46b7abe2009-07-31 16:55:27 +020027
Lars Hjemlib0f946b2009-08-21 14:26:52 +020028 if (ctx.cfg.enable_tree_linenumbers) {
Florian Pritzd67cc7f2009-08-09 20:42:45 +000029 html("<tr><td class='linenumbers'><pre>");
30 idx = 0;
31 lineno = 0;
Lukas Fleischer53bc7472013-03-03 16:04:29 +010032
Florian Pritzd67cc7f2009-08-09 20:42:45 +000033 if (size) {
34 htmlf(numberfmt, ++lineno);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -050035 while (idx < size - 1) { // skip absolute last newline
Florian Pritzd67cc7f2009-08-09 20:42:45 +000036 if (buf[idx] == '\n')
37 htmlf(numberfmt, ++lineno);
38 idx++;
39 }
Eric Wong11297362009-03-14 18:41:47 -070040 }
Florian Pritzd67cc7f2009-08-09 20:42:45 +000041 html("</pre></td>\n");
Lars Hjemliae1d4d72009-01-31 17:45:48 +010042 }
Florian Pritzd67cc7f2009-08-09 20:42:45 +000043 else {
44 html("<tr>\n");
45 }
Florian Pritz03389d62009-08-09 13:43:18 +000046
47 if (ctx.repo->source_filter) {
John Keeping3d8a6502014-01-12 17:13:50 +000048 char *filter_arg = xstrdup(name);
Florian Pritz03389d62009-08-09 13:43:18 +000049 html("<td class='lines'><pre><code>");
John Keeping3d8a6502014-01-12 17:13:50 +000050 cgit_open_filter(ctx.repo->source_filter, filter_arg);
Mark Lodatod187b982010-09-04 14:18:16 -040051 html_raw(buf, size);
Florian Pritz03389d62009-08-09 13:43:18 +000052 cgit_close_filter(ctx.repo->source_filter);
John Keeping3d8a6502014-01-12 17:13:50 +000053 free(filter_arg);
Florian Pritz03389d62009-08-09 13:43:18 +000054 html("</code></pre></td></tr></table>\n");
55 return;
56 }
57
Lars Hjemliae1d4d72009-01-31 17:45:48 +010058 html("<td class='lines'><pre><code>");
59 html_txt(buf);
60 html("</code></pre></td></tr></table>\n");
61}
62
Lars Hjemli6063e7b2009-02-12 11:26:14 +010063#define ROWLEN 32
64
Lars Hjemliae1d4d72009-01-31 17:45:48 +010065static void print_binary_buffer(char *buf, unsigned long size)
66{
67 unsigned long ofs, idx;
Lars Hjemli6063e7b2009-02-12 11:26:14 +010068 static char ascii[ROWLEN + 1];
Lars Hjemliae1d4d72009-01-31 17:45:48 +010069
70 html("<table summary='blob content' class='bin-blob'>\n");
71 html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
Lars Hjemli6063e7b2009-02-12 11:26:14 +010072 for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
Mark Lodatoe4ddc8f2010-09-04 11:30:18 -040073 htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs);
Lars Hjemli6063e7b2009-02-12 11:26:14 +010074 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
Lars Hjemliae1d4d72009-01-31 17:45:48 +010075 htmlf("%*s%02x",
76 idx == 16 ? 4 : 1, "",
77 buf[idx] & 0xff);
78 html(" </td><td class='hex'>");
Lars Hjemli6063e7b2009-02-12 11:26:14 +010079 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
80 ascii[idx] = isgraph(buf[idx]) ? buf[idx] : '.';
81 ascii[idx] = '\0';
82 html_txt(ascii);
Lars Hjemliae1d4d72009-01-31 17:45:48 +010083 html("</td></tr>\n");
84 }
85 html("</table>\n");
86}
87
Lukas Fleischerfb5a3732013-03-03 16:45:14 +010088static void print_object(const unsigned char *sha1, char *path, const char *basename, const char *rev)
Lars Hjemliffc69732007-06-16 20:20:42 +020089{
90 enum object_type type;
Lars Hjemli0835ffe2007-09-20 00:21:47 +020091 char *buf;
Lars Hjemliae1d4d72009-01-31 17:45:48 +010092 unsigned long size;
Lars Hjemliffc69732007-06-16 20:20:42 +020093
94 type = sha1_object_info(sha1, &size);
95 if (type == OBJ_BAD) {
John Keepinged5bd302013-04-06 11:23:52 +010096 cgit_print_error("Bad object name: %s", sha1_to_hex(sha1));
Lars Hjemliffc69732007-06-16 20:20:42 +020097 return;
98 }
99
100 buf = read_sha1_file(sha1, &type, &size);
101 if (!buf) {
John Keepinged5bd302013-04-06 11:23:52 +0100102 cgit_print_error("Error reading object %s", sha1_to_hex(sha1));
Lars Hjemliffc69732007-06-16 20:20:42 +0200103 return;
104 }
105
Johan Herland48f7b982010-06-10 01:09:30 +0200106 htmlf("blob: %s (", sha1_to_hex(sha1));
Lars Hjemli65b7b872008-08-06 11:07:13 +0200107 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
Lukas Fleischerfb5a3732013-03-03 16:45:14 +0100108 rev, path);
Johan Herland48f7b982010-06-10 01:09:30 +0200109 html(")\n");
Michael Krelind6b01da2007-07-21 19:51:47 +0200110
Georg Lukasef07ccc2009-11-28 03:44:33 +0100111 if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
Mark Lodatoe4ddc8f2010-09-04 11:30:18 -0400112 htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
Georg Lukasef07ccc2009-11-28 03:44:33 +0100113 size / 1024, ctx.cfg.max_blob_size);
114 return;
115 }
116
Lars Hjemliae1d4d72009-01-31 17:45:48 +0100117 if (buffer_is_binary(buf, size))
118 print_binary_buffer(buf, size);
119 else
Lars Hjemli46b7abe2009-07-31 16:55:27 +0200120 print_text_buffer(basename, buf, size);
Lars Hjemliffc69732007-06-16 20:20:42 +0200121}
122
123
Christian Hesse7358f632015-02-07 14:18:28 +0100124static int ls_item(const unsigned char *sha1, struct strbuf *base,
125 const char *pathname, unsigned mode, int stage, void *cbdata)
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100126{
Lukas Fleischer210a5712013-03-03 17:22:30 +0100127 struct walk_tree_context *walk_tree_ctx = cbdata;
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100128 char *name;
John Keepingfb3655d2013-04-06 10:28:57 +0100129 struct strbuf fullpath = STRBUF_INIT;
130 struct strbuf class = STRBUF_INIT;
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200131 enum object_type type;
Lars Hjemlided93932007-05-11 12:12:48 +0200132 unsigned long size = 0;
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100133
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200134 name = xstrdup(pathname);
John Keepingfb3655d2013-04-06 10:28:57 +0100135 strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "",
136 ctx.qry.path ? "/" : "", name);
Lars Hjemli44947bf2007-06-17 01:23:08 +0200137
Lars Hjemli08a87572008-05-20 22:32:22 +0200138 if (!S_ISGITLINK(mode)) {
139 type = sha1_object_info(sha1, &size);
140 if (type == OBJ_BAD) {
141 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
142 name,
143 sha1_to_hex(sha1));
144 return 0;
145 }
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100146 }
Lars Hjemli44947bf2007-06-17 01:23:08 +0200147
Lars Hjemli426032f2007-06-17 13:17:00 +0200148 html("<tr><td class='ls-mode'>");
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +0100149 cgit_print_filemode(mode);
Lars Hjemli426032f2007-06-17 13:17:00 +0200150 html("</td><td>");
Jeffrey C. Olliee651cb02007-06-04 12:28:56 -0500151 if (S_ISGITLINK(mode)) {
John Keepingfb3655d2013-04-06 10:28:57 +0100152 cgit_submodule_link("ls-mod", fullpath.buf, sha1_to_hex(sha1));
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200153 } else if (S_ISDIR(mode)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100154 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
John Keepingfb3655d2013-04-06 10:28:57 +0100155 walk_tree_ctx->curr_rev, fullpath.buf);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100156 } else {
John Keepingfb3655d2013-04-06 10:28:57 +0100157 char *ext = strrchr(name, '.');
158 strbuf_addstr(&class, "ls-blob");
159 if (ext)
160 strbuf_addf(&class, " %s", ext + 1);
161 cgit_tree_link(name, NULL, class.buf, ctx.qry.head,
162 walk_tree_ctx->curr_rev, fullpath.buf);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100163 }
Lars Hjemli426032f2007-06-17 13:17:00 +0200164 htmlf("</td><td class='ls-size'>%li</td>", size);
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200165
Lars Hjemli48c487d2007-06-17 13:57:51 +0200166 html("<td>");
Lukas Fleischer210a5712013-03-03 17:22:30 +0100167 cgit_log_link("log", NULL, "button", ctx.qry.head,
John Keepingfb3655d2013-04-06 10:28:57 +0100168 walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL,
John Keeping30304d82015-08-12 15:55:28 +0100169 ctx.qry.showmsg, 0);
Lars Hjemli837d4642008-12-07 13:34:42 +0100170 if (ctx.repo->max_stats)
171 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
John Keepingfb3655d2013-04-06 10:28:57 +0100172 fullpath.buf);
Lars Hjemli6857bec2011-06-15 10:04:13 +0200173 if (!S_ISGITLINK(mode))
Lukas Fleischer210a5712013-03-03 17:22:30 +0100174 cgit_plain_link("plain", NULL, "button", ctx.qry.head,
John Keepingfb3655d2013-04-06 10:28:57 +0100175 walk_tree_ctx->curr_rev, fullpath.buf);
Lars Hjemli48c487d2007-06-17 13:57:51 +0200176 html("</td></tr>\n");
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100177 free(name);
John Keepingfb3655d2013-04-06 10:28:57 +0100178 strbuf_release(&fullpath);
179 strbuf_release(&class);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100180 return 0;
181}
182
John Keepinge3d3fff2015-03-08 16:32:16 +0000183static void ls_head(void)
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100184{
Lars Hjemli29154832007-11-11 13:04:28 +0100185 html("<table summary='tree listing' class='list'>\n");
Lars Hjemli777faf72007-01-28 00:39:26 +0100186 html("<tr class='nohover'>");
Lars Hjemlia5304282006-12-17 23:55:53 +0100187 html("<th class='left'>Mode</th>");
188 html("<th class='left'>Name</th>");
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100189 html("<th class='right'>Size</th>");
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200190 html("<th/>");
Lars Hjemlia5304282006-12-17 23:55:53 +0100191 html("</tr>\n");
Lars Hjemliffc69732007-06-16 20:20:42 +0200192}
193
John Keepinge3d3fff2015-03-08 16:32:16 +0000194static void ls_tail(void)
Lars Hjemliffc69732007-06-16 20:20:42 +0200195{
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100196 html("</table>\n");
Lars Hjemliffc69732007-06-16 20:20:42 +0200197}
198
Lukas Fleischer210a5712013-03-03 17:22:30 +0100199static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_context *walk_tree_ctx)
Lars Hjemliffc69732007-06-16 20:20:42 +0200200{
201 struct tree *tree;
John Keepingc1633c62013-03-02 12:32:11 +0000202 struct pathspec paths = {
203 .nr = 0
204 };
Lars Hjemliffc69732007-06-16 20:20:42 +0200205
206 tree = parse_tree_indirect(sha1);
207 if (!tree) {
John Keepinged5bd302013-04-06 11:23:52 +0100208 cgit_print_error("Not a tree object: %s", sha1_to_hex(sha1));
Lars Hjemliffc69732007-06-16 20:20:42 +0200209 return;
210 }
211
212 ls_head();
Lukas Fleischer210a5712013-03-03 17:22:30 +0100213 read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
Lars Hjemliffc69732007-06-16 20:20:42 +0200214 ls_tail();
215}
216
217
Christian Hesse7358f632015-02-07 14:18:28 +0100218static int walk_tree(const unsigned char *sha1, struct strbuf *base,
219 const char *pathname, unsigned mode, int stage, void *cbdata)
Lars Hjemliffc69732007-06-16 20:20:42 +0200220{
Lukas Fleischer210a5712013-03-03 17:22:30 +0100221 struct walk_tree_context *walk_tree_ctx = cbdata;
Lars Hjemliffc69732007-06-16 20:20:42 +0200222 static char buffer[PATH_MAX];
Lars Hjemliffc69732007-06-16 20:20:42 +0200223
Lukas Fleischer210a5712013-03-03 17:22:30 +0100224 if (walk_tree_ctx->state == 0) {
Christian Hesse7358f632015-02-07 14:18:28 +0100225 memcpy(buffer, base->buf, base->len);
226 strcpy(buffer + base->len, pathname);
Lukas Fleischer210a5712013-03-03 17:22:30 +0100227 if (strcmp(walk_tree_ctx->match_path, buffer))
Lars Hjemliffc69732007-06-16 20:20:42 +0200228 return READ_TREE_RECURSIVE;
229
230 if (S_ISDIR(mode)) {
Lukas Fleischer210a5712013-03-03 17:22:30 +0100231 walk_tree_ctx->state = 1;
Lars Hjemliffc69732007-06-16 20:20:42 +0200232 ls_head();
233 return READ_TREE_RECURSIVE;
234 } else {
Lukas Fleischer210a5712013-03-03 17:22:30 +0100235 print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev);
Lars Hjemliffc69732007-06-16 20:20:42 +0200236 return 0;
237 }
238 }
Christian Hesse7358f632015-02-07 14:18:28 +0100239 ls_item(sha1, base, pathname, mode, stage, walk_tree_ctx);
Lars Hjemliffc69732007-06-16 20:20:42 +0200240 return 0;
241}
242
Lars Hjemliffc69732007-06-16 20:20:42 +0200243/*
244 * Show a tree or a blob
245 * rev: the commit pointing at the root tree object
246 * path: path to tree or blob
247 */
248void cgit_print_tree(const char *rev, char *path)
249{
250 unsigned char sha1[20];
251 struct commit *commit;
John Keepingc1633c62013-03-02 12:32:11 +0000252 struct pathspec_item path_items = {
253 .match = path,
254 .len = path ? strlen(path) : 0
255 };
256 struct pathspec paths = {
257 .nr = path ? 1 : 0,
258 .items = &path_items
259 };
Lukas Fleischer210a5712013-03-03 17:22:30 +0100260 struct walk_tree_context walk_tree_ctx = {
261 .match_path = path,
262 .state = 0
263 };
Lars Hjemliffc69732007-06-16 20:20:42 +0200264
265 if (!rev)
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100266 rev = ctx.qry.head;
Lars Hjemliffc69732007-06-16 20:20:42 +0200267
Lars Hjemliffc69732007-06-16 20:20:42 +0200268 if (get_sha1(rev, sha1)) {
John Keepinged5bd302013-04-06 11:23:52 +0100269 cgit_print_error("Invalid revision name: %s", rev);
Lars Hjemliffc69732007-06-16 20:20:42 +0200270 return;
271 }
272 commit = lookup_commit_reference(sha1);
273 if (!commit || parse_commit(commit)) {
John Keepinged5bd302013-04-06 11:23:52 +0100274 cgit_print_error("Invalid commit reference: %s", rev);
Lars Hjemliffc69732007-06-16 20:20:42 +0200275 return;
276 }
277
Lukas Fleischer985d6ca2013-03-04 13:25:36 +0100278 walk_tree_ctx.curr_rev = xstrdup(rev);
279
Lars Hjemliffc69732007-06-16 20:20:42 +0200280 if (path == NULL) {
Lukas Fleischer210a5712013-03-03 17:22:30 +0100281 ls_tree(commit->tree->object.sha1, NULL, &walk_tree_ctx);
Lukas Fleischer985d6ca2013-03-04 13:25:36 +0100282 goto cleanup;
Lars Hjemliffc69732007-06-16 20:20:42 +0200283 }
284
Lukas Fleischer210a5712013-03-03 17:22:30 +0100285 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
286 if (walk_tree_ctx.state == 1)
Lukas Fleischerbfe57662013-03-03 16:55:21 +0100287 ls_tail();
Lukas Fleischer985d6ca2013-03-04 13:25:36 +0100288
289cleanup:
290 free(walk_tree_ctx.curr_rev);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100291}