blob: b692b563a850421dd30fc24dc6174f0ed81d3363 [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"
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;
23 const char *numberfmt =
24 "<a class='no' id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a>\n";
25
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) {
48 html("<td class='lines'><pre><code>");
49 ctx.repo->source_filter->argv[1] = xstrdup(name);
Lars Hjemli3ec6b302011-06-06 19:29:58 +000050 cgit_open_filter(ctx.repo->source_filter);
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);
Ferry Huberts3f1ebd32011-03-09 08:16:58 +010053 free(ctx.repo->source_filter->argv[1]);
54 ctx.repo->source_filter->argv[1] = NULL;
Florian Pritz03389d62009-08-09 13:43:18 +000055 html("</code></pre></td></tr></table>\n");
56 return;
57 }
58
Lars Hjemliae1d4d72009-01-31 17:45:48 +010059 html("<td class='lines'><pre><code>");
60 html_txt(buf);
61 html("</code></pre></td></tr></table>\n");
62}
63
Lars Hjemli6063e7b2009-02-12 11:26:14 +010064#define ROWLEN 32
65
Lars Hjemliae1d4d72009-01-31 17:45:48 +010066static void print_binary_buffer(char *buf, unsigned long size)
67{
68 unsigned long ofs, idx;
Lars Hjemli6063e7b2009-02-12 11:26:14 +010069 static char ascii[ROWLEN + 1];
Lars Hjemliae1d4d72009-01-31 17:45:48 +010070
71 html("<table summary='blob content' class='bin-blob'>\n");
72 html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
Lars Hjemli6063e7b2009-02-12 11:26:14 +010073 for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
Mark Lodatoe4ddc8f2010-09-04 11:30:18 -040074 htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs);
Lars Hjemli6063e7b2009-02-12 11:26:14 +010075 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
Lars Hjemliae1d4d72009-01-31 17:45:48 +010076 htmlf("%*s%02x",
77 idx == 16 ? 4 : 1, "",
78 buf[idx] & 0xff);
79 html(" </td><td class='hex'>");
Lars Hjemli6063e7b2009-02-12 11:26:14 +010080 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);
Lars Hjemliae1d4d72009-01-31 17:45:48 +010084 html("</td></tr>\n");
85 }
86 html("</table>\n");
87}
88
Lukas Fleischerfb5a3732013-03-03 16:45:14 +010089static void print_object(const unsigned char *sha1, char *path, const char *basename, const char *rev)
Lars Hjemliffc69732007-06-16 20:20:42 +020090{
91 enum object_type type;
Lars Hjemli0835ffe2007-09-20 00:21:47 +020092 char *buf;
Lars Hjemliae1d4d72009-01-31 17:45:48 +010093 unsigned long size;
Lars Hjemliffc69732007-06-16 20:20:42 +020094
95 type = sha1_object_info(sha1, &size);
96 if (type == OBJ_BAD) {
97 cgit_print_error(fmt("Bad object name: %s",
98 sha1_to_hex(sha1)));
99 return;
100 }
101
102 buf = read_sha1_file(sha1, &type, &size);
103 if (!buf) {
104 cgit_print_error(fmt("Error reading object %s",
105 sha1_to_hex(sha1)));
106 return;
107 }
108
Johan Herland48f7b982010-06-10 01:09:30 +0200109 htmlf("blob: %s (", sha1_to_hex(sha1));
Lars Hjemli65b7b872008-08-06 11:07:13 +0200110 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
Lukas Fleischerfb5a3732013-03-03 16:45:14 +0100111 rev, path);
Johan Herland48f7b982010-06-10 01:09:30 +0200112 html(")\n");
Michael Krelind6b01da2007-07-21 19:51:47 +0200113
Georg Lukasef07ccc2009-11-28 03:44:33 +0100114 if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
Mark Lodatoe4ddc8f2010-09-04 11:30:18 -0400115 htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
Georg Lukasef07ccc2009-11-28 03:44:33 +0100116 size / 1024, ctx.cfg.max_blob_size);
117 return;
118 }
119
Lars Hjemliae1d4d72009-01-31 17:45:48 +0100120 if (buffer_is_binary(buf, size))
121 print_binary_buffer(buf, size);
122 else
Lars Hjemli46b7abe2009-07-31 16:55:27 +0200123 print_text_buffer(basename, buf, size);
Lars Hjemliffc69732007-06-16 20:20:42 +0200124}
125
126
127static int ls_item(const unsigned char *sha1, const char *base, int baselen,
Lars Hjemli566f92b2008-07-21 10:10:48 +0200128 const char *pathname, unsigned int mode, int stage,
129 void *cbdata)
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100130{
Lukas Fleischer210a5712013-03-03 17:22:30 +0100131 struct walk_tree_context *walk_tree_ctx = cbdata;
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100132 char *name;
Lars Hjemli44947bf2007-06-17 01:23:08 +0200133 char *fullpath;
Martin Szuleckib4c35622009-08-07 14:06:02 +0200134 char *class;
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200135 enum object_type type;
Lars Hjemlided93932007-05-11 12:12:48 +0200136 unsigned long size = 0;
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100137
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200138 name = xstrdup(pathname);
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100139 fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "",
140 ctx.qry.path ? "/" : "", name);
Lars Hjemli44947bf2007-06-17 01:23:08 +0200141
Lars Hjemli08a87572008-05-20 22:32:22 +0200142 if (!S_ISGITLINK(mode)) {
143 type = sha1_object_info(sha1, &size);
144 if (type == OBJ_BAD) {
145 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
146 name,
147 sha1_to_hex(sha1));
148 return 0;
149 }
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100150 }
Lars Hjemli44947bf2007-06-17 01:23:08 +0200151
Lars Hjemli426032f2007-06-17 13:17:00 +0200152 html("<tr><td class='ls-mode'>");
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +0100153 cgit_print_filemode(mode);
Lars Hjemli426032f2007-06-17 13:17:00 +0200154 html("</td><td>");
Jeffrey C. Olliee651cb02007-06-04 12:28:56 -0500155 if (S_ISGITLINK(mode)) {
Lars Hjemli6857bec2011-06-15 10:04:13 +0200156 cgit_submodule_link("ls-mod", fullpath, sha1_to_hex(sha1));
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200157 } else if (S_ISDIR(mode)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100158 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
Lukas Fleischer210a5712013-03-03 17:22:30 +0100159 walk_tree_ctx->curr_rev, fullpath);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100160 } else {
Martin Szuleckib4c35622009-08-07 14:06:02 +0200161 class = strrchr(name, '.');
162 if (class != NULL) {
163 class = fmt("ls-blob %s", class + 1);
164 } else
165 class = "ls-blob";
166 cgit_tree_link(name, NULL, class, ctx.qry.head,
Lukas Fleischer210a5712013-03-03 17:22:30 +0100167 walk_tree_ctx->curr_rev, fullpath);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100168 }
Lars Hjemli426032f2007-06-17 13:17:00 +0200169 htmlf("</td><td class='ls-size'>%li</td>", size);
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200170
Lars Hjemli48c487d2007-06-17 13:57:51 +0200171 html("<td>");
Lukas Fleischer210a5712013-03-03 17:22:30 +0100172 cgit_log_link("log", NULL, "button", ctx.qry.head,
173 walk_tree_ctx->curr_rev, fullpath, 0, NULL, NULL,
174 ctx.qry.showmsg);
Lars Hjemli837d4642008-12-07 13:34:42 +0100175 if (ctx.repo->max_stats)
176 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
177 fullpath);
Lars Hjemli6857bec2011-06-15 10:04:13 +0200178 if (!S_ISGITLINK(mode))
Lukas Fleischer210a5712013-03-03 17:22:30 +0100179 cgit_plain_link("plain", NULL, "button", ctx.qry.head,
180 walk_tree_ctx->curr_rev, fullpath);
Lars Hjemli48c487d2007-06-17 13:57:51 +0200181 html("</td></tr>\n");
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100182 free(name);
183 return 0;
184}
185
Lars Hjemliffc69732007-06-16 20:20:42 +0200186static void ls_head()
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100187{
Lars Hjemli29154832007-11-11 13:04:28 +0100188 html("<table summary='tree listing' class='list'>\n");
Lars Hjemli777faf72007-01-28 00:39:26 +0100189 html("<tr class='nohover'>");
Lars Hjemlia5304282006-12-17 23:55:53 +0100190 html("<th class='left'>Mode</th>");
191 html("<th class='left'>Name</th>");
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100192 html("<th class='right'>Size</th>");
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200193 html("<th/>");
Lars Hjemlia5304282006-12-17 23:55:53 +0100194 html("</tr>\n");
Lars Hjemliffc69732007-06-16 20:20:42 +0200195}
196
197static void ls_tail()
198{
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100199 html("</table>\n");
Lars Hjemliffc69732007-06-16 20:20:42 +0200200}
201
Lukas Fleischer210a5712013-03-03 17:22:30 +0100202static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_context *walk_tree_ctx)
Lars Hjemliffc69732007-06-16 20:20:42 +0200203{
204 struct tree *tree;
John Keepingc1633c62013-03-02 12:32:11 +0000205 struct pathspec paths = {
206 .nr = 0
207 };
Lars Hjemliffc69732007-06-16 20:20:42 +0200208
209 tree = parse_tree_indirect(sha1);
210 if (!tree) {
211 cgit_print_error(fmt("Not a tree object: %s",
212 sha1_to_hex(sha1)));
213 return;
214 }
215
216 ls_head();
Lukas Fleischer210a5712013-03-03 17:22:30 +0100217 read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
Lars Hjemliffc69732007-06-16 20:20:42 +0200218 ls_tail();
219}
220
221
222static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
Lars Hjemli566f92b2008-07-21 10:10:48 +0200223 const char *pathname, unsigned mode, int stage,
224 void *cbdata)
Lars Hjemliffc69732007-06-16 20:20:42 +0200225{
Lukas Fleischer210a5712013-03-03 17:22:30 +0100226 struct walk_tree_context *walk_tree_ctx = cbdata;
Lars Hjemliffc69732007-06-16 20:20:42 +0200227 static char buffer[PATH_MAX];
Lars Hjemliffc69732007-06-16 20:20:42 +0200228
Lukas Fleischer210a5712013-03-03 17:22:30 +0100229 if (walk_tree_ctx->state == 0) {
Lars Hjemliffc69732007-06-16 20:20:42 +0200230 memcpy(buffer, base, baselen);
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100231 strcpy(buffer + baselen, pathname);
Lukas Fleischer210a5712013-03-03 17:22:30 +0100232 if (strcmp(walk_tree_ctx->match_path, buffer))
Lars Hjemliffc69732007-06-16 20:20:42 +0200233 return READ_TREE_RECURSIVE;
234
235 if (S_ISDIR(mode)) {
Lukas Fleischer210a5712013-03-03 17:22:30 +0100236 walk_tree_ctx->state = 1;
Lars Hjemliffc69732007-06-16 20:20:42 +0200237 ls_head();
238 return READ_TREE_RECURSIVE;
239 } else {
Lukas Fleischer210a5712013-03-03 17:22:30 +0100240 print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev);
Lars Hjemliffc69732007-06-16 20:20:42 +0200241 return 0;
242 }
243 }
Lukas Fleischer210a5712013-03-03 17:22:30 +0100244 ls_item(sha1, base, baselen, pathname, mode, stage, walk_tree_ctx);
Lars Hjemliffc69732007-06-16 20:20:42 +0200245 return 0;
246}
247
248
249/*
250 * Show a tree or a blob
251 * rev: the commit pointing at the root tree object
252 * path: path to tree or blob
253 */
254void cgit_print_tree(const char *rev, char *path)
255{
256 unsigned char sha1[20];
257 struct commit *commit;
John Keepingc1633c62013-03-02 12:32:11 +0000258 struct pathspec_item path_items = {
259 .match = path,
260 .len = path ? strlen(path) : 0
261 };
262 struct pathspec paths = {
263 .nr = path ? 1 : 0,
264 .items = &path_items
265 };
Lukas Fleischer210a5712013-03-03 17:22:30 +0100266 struct walk_tree_context walk_tree_ctx = {
267 .match_path = path,
268 .state = 0
269 };
Lars Hjemliffc69732007-06-16 20:20:42 +0200270
271 if (!rev)
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100272 rev = ctx.qry.head;
Lars Hjemliffc69732007-06-16 20:20:42 +0200273
Lars Hjemliffc69732007-06-16 20:20:42 +0200274 if (get_sha1(rev, sha1)) {
275 cgit_print_error(fmt("Invalid revision name: %s", rev));
276 return;
277 }
278 commit = lookup_commit_reference(sha1);
279 if (!commit || parse_commit(commit)) {
280 cgit_print_error(fmt("Invalid commit reference: %s", rev));
281 return;
282 }
283
Lukas Fleischer985d6ca2013-03-04 13:25:36 +0100284 walk_tree_ctx.curr_rev = xstrdup(rev);
285
Lars Hjemliffc69732007-06-16 20:20:42 +0200286 if (path == NULL) {
Lukas Fleischer210a5712013-03-03 17:22:30 +0100287 ls_tree(commit->tree->object.sha1, NULL, &walk_tree_ctx);
Lukas Fleischer985d6ca2013-03-04 13:25:36 +0100288 goto cleanup;
Lars Hjemliffc69732007-06-16 20:20:42 +0200289 }
290
Lukas Fleischer210a5712013-03-03 17:22:30 +0100291 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
292 if (walk_tree_ctx.state == 1)
Lukas Fleischerbfe57662013-03-03 16:55:21 +0100293 ls_tail();
Lukas Fleischer985d6ca2013-03-04 13:25:36 +0100294
295cleanup:
296 free(walk_tree_ctx.curr_rev);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100297}