blob: a37a4e59c56f1bfcbe057bb7dde74abfe272e146 [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
Lars Hjemli9fb53af2007-05-14 11:10:59 +020014char *curr_rev;
Lars Hjemliffc69732007-06-16 20:20:42 +020015char *match_path;
16int header = 0;
Lars Hjemli06fe0c22006-12-13 00:13:27 +010017
Lars Hjemliae1d4d72009-01-31 17:45:48 +010018static void print_text_buffer(char *buf, unsigned long size)
19{
20 unsigned long lineno, idx;
21 const char *numberfmt =
22 "<a class='no' id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a>\n";
23
24 html("<table summary='blob content' class='blob'>\n");
25 html("<tr><td class='linenumbers'><pre>");
26 idx = 0;
27 lineno = 0;
28 htmlf(numberfmt, ++lineno);
29 while(idx < size - 1) { // skip absolute last newline
30 if (buf[idx] == '\n')
31 htmlf(numberfmt, ++lineno);
32 idx++;
33 }
34 html("</pre></td>\n");
35 html("<td class='lines'><pre><code>");
36 html_txt(buf);
37 html("</code></pre></td></tr></table>\n");
38}
39
40static void print_binary_buffer(char *buf, unsigned long size)
41{
42 unsigned long ofs, idx;
43
44 html("<table summary='blob content' class='bin-blob'>\n");
45 html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
46 for (ofs = 0; ofs < size; ofs += 32, buf += 32) {
47 htmlf("<tr><td class='right'>%04x</td><td class='hex'>", ofs);
48 for (idx = 0; idx < 32 && ofs + idx < size; idx++)
49 htmlf("%*s%02x",
50 idx == 16 ? 4 : 1, "",
51 buf[idx] & 0xff);
52 html(" </td><td class='hex'>");
53 for (idx = 0; idx < 32 && ofs + idx < size; idx++)
54 htmlf("%c", isgraph(buf[idx]) ? buf[idx] : '.');
55 html("</td></tr>\n");
56 }
57 html("</table>\n");
58}
59
Lars Hjemliffc69732007-06-16 20:20:42 +020060static void print_object(const unsigned char *sha1, char *path)
61{
62 enum object_type type;
Lars Hjemli0835ffe2007-09-20 00:21:47 +020063 char *buf;
Lars Hjemliae1d4d72009-01-31 17:45:48 +010064 unsigned long size;
Lars Hjemliffc69732007-06-16 20:20:42 +020065
66 type = sha1_object_info(sha1, &size);
67 if (type == OBJ_BAD) {
68 cgit_print_error(fmt("Bad object name: %s",
69 sha1_to_hex(sha1)));
70 return;
71 }
72
73 buf = read_sha1_file(sha1, &type, &size);
74 if (!buf) {
75 cgit_print_error(fmt("Error reading object %s",
76 sha1_to_hex(sha1)));
77 return;
78 }
79
Lars Hjemli65b7b872008-08-06 11:07:13 +020080 html(" (");
81 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
82 curr_rev, path);
Onne Gortera90e2aa2009-01-07 13:56:14 +010083 htmlf(")<br/>blob: %s\n", sha1_to_hex(sha1));
Michael Krelind6b01da2007-07-21 19:51:47 +020084
Lars Hjemliae1d4d72009-01-31 17:45:48 +010085 if (buffer_is_binary(buf, size))
86 print_binary_buffer(buf, size);
87 else
88 print_text_buffer(buf, size);
Lars Hjemliffc69732007-06-16 20:20:42 +020089}
90
91
92static int ls_item(const unsigned char *sha1, const char *base, int baselen,
Lars Hjemli566f92b2008-07-21 10:10:48 +020093 const char *pathname, unsigned int mode, int stage,
94 void *cbdata)
Lars Hjemli06fe0c22006-12-13 00:13:27 +010095{
96 char *name;
Lars Hjemli44947bf2007-06-17 01:23:08 +020097 char *fullpath;
Lars Hjemli61c3ca92007-05-08 22:40:59 +020098 enum object_type type;
Lars Hjemlided93932007-05-11 12:12:48 +020099 unsigned long size = 0;
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100100
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200101 name = xstrdup(pathname);
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100102 fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "",
103 ctx.qry.path ? "/" : "", name);
Lars Hjemli44947bf2007-06-17 01:23:08 +0200104
Lars Hjemli08a87572008-05-20 22:32:22 +0200105 if (!S_ISGITLINK(mode)) {
106 type = sha1_object_info(sha1, &size);
107 if (type == OBJ_BAD) {
108 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
109 name,
110 sha1_to_hex(sha1));
111 return 0;
112 }
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100113 }
Lars Hjemli44947bf2007-06-17 01:23:08 +0200114
Lars Hjemli426032f2007-06-17 13:17:00 +0200115 html("<tr><td class='ls-mode'>");
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +0100116 cgit_print_filemode(mode);
Lars Hjemli426032f2007-06-17 13:17:00 +0200117 html("</td><td>");
Jeffrey C. Olliee651cb02007-06-04 12:28:56 -0500118 if (S_ISGITLINK(mode)) {
Lars Hjemli426032f2007-06-17 13:17:00 +0200119 htmlf("<a class='ls-mod' href='");
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100120 html_attr(fmt(ctx.repo->module_link,
Lars Hjemlided93932007-05-11 12:12:48 +0200121 name,
122 sha1_to_hex(sha1)));
Lars Hjemli44947bf2007-06-17 01:23:08 +0200123 html("'>");
124 html_txt(name);
125 html("</a>");
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200126 } else if (S_ISDIR(mode)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100127 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
Lars Hjemli44947bf2007-06-17 01:23:08 +0200128 curr_rev, fullpath);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100129 } else {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100130 cgit_tree_link(name, NULL, "ls-blob", ctx.qry.head,
Lars Hjemli44947bf2007-06-17 01:23:08 +0200131 curr_rev, fullpath);
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100132 }
Lars Hjemli426032f2007-06-17 13:17:00 +0200133 htmlf("</td><td class='ls-size'>%li</td>", size);
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200134
Lars Hjemli48c487d2007-06-17 13:57:51 +0200135 html("<td>");
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100136 cgit_log_link("log", NULL, "button", ctx.qry.head, curr_rev,
Lars Hjemli0274b572008-11-29 18:39:41 +0100137 fullpath, 0, NULL, NULL, ctx.qry.showmsg);
Lars Hjemli837d4642008-12-07 13:34:42 +0100138 if (ctx.repo->max_stats)
139 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
140 fullpath);
Lars Hjemli48c487d2007-06-17 13:57:51 +0200141 html("</td></tr>\n");
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100142 free(name);
143 return 0;
144}
145
Lars Hjemliffc69732007-06-16 20:20:42 +0200146static void ls_head()
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100147{
Lars Hjemli29154832007-11-11 13:04:28 +0100148 html("<table summary='tree listing' class='list'>\n");
Lars Hjemli777faf72007-01-28 00:39:26 +0100149 html("<tr class='nohover'>");
Lars Hjemlia5304282006-12-17 23:55:53 +0100150 html("<th class='left'>Mode</th>");
151 html("<th class='left'>Name</th>");
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100152 html("<th class='right'>Size</th>");
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200153 html("<th/>");
Lars Hjemlia5304282006-12-17 23:55:53 +0100154 html("</tr>\n");
Lars Hjemliffc69732007-06-16 20:20:42 +0200155 header = 1;
156}
157
158static void ls_tail()
159{
160 if (!header)
161 return;
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100162 html("</table>\n");
Lars Hjemliffc69732007-06-16 20:20:42 +0200163 header = 0;
164}
165
166static void ls_tree(const unsigned char *sha1, char *path)
167{
168 struct tree *tree;
169
170 tree = parse_tree_indirect(sha1);
171 if (!tree) {
172 cgit_print_error(fmt("Not a tree object: %s",
173 sha1_to_hex(sha1)));
174 return;
175 }
176
177 ls_head();
Lars Hjemli566f92b2008-07-21 10:10:48 +0200178 read_tree_recursive(tree, "", 0, 1, NULL, ls_item, NULL);
Lars Hjemliffc69732007-06-16 20:20:42 +0200179 ls_tail();
180}
181
182
183static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
Lars Hjemli566f92b2008-07-21 10:10:48 +0200184 const char *pathname, unsigned mode, int stage,
185 void *cbdata)
Lars Hjemliffc69732007-06-16 20:20:42 +0200186{
187 static int state;
188 static char buffer[PATH_MAX];
189 char *url;
190
191 if (state == 0) {
192 memcpy(buffer, base, baselen);
193 strcpy(buffer+baselen, pathname);
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100194 url = cgit_pageurl(ctx.qry.repo, "tree",
Lars Hjemliffc69732007-06-16 20:20:42 +0200195 fmt("h=%s&amp;path=%s", curr_rev, buffer));
Lars Hjemli44947bf2007-06-17 01:23:08 +0200196 html("/");
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100197 cgit_tree_link(xstrdup(pathname), NULL, NULL, ctx.qry.head,
Lars Hjemli44947bf2007-06-17 01:23:08 +0200198 curr_rev, buffer);
Lars Hjemliffc69732007-06-16 20:20:42 +0200199
200 if (strcmp(match_path, buffer))
201 return READ_TREE_RECURSIVE;
202
203 if (S_ISDIR(mode)) {
204 state = 1;
205 ls_head();
206 return READ_TREE_RECURSIVE;
207 } else {
208 print_object(sha1, buffer);
209 return 0;
210 }
211 }
Lars Hjemli566f92b2008-07-21 10:10:48 +0200212 ls_item(sha1, base, baselen, pathname, mode, stage, NULL);
Lars Hjemliffc69732007-06-16 20:20:42 +0200213 return 0;
214}
215
216
217/*
218 * Show a tree or a blob
219 * rev: the commit pointing at the root tree object
220 * path: path to tree or blob
221 */
222void cgit_print_tree(const char *rev, char *path)
223{
224 unsigned char sha1[20];
225 struct commit *commit;
226 const char *paths[] = {path, NULL};
227
228 if (!rev)
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100229 rev = ctx.qry.head;
Lars Hjemliffc69732007-06-16 20:20:42 +0200230
231 curr_rev = xstrdup(rev);
232 if (get_sha1(rev, sha1)) {
233 cgit_print_error(fmt("Invalid revision name: %s", rev));
234 return;
235 }
236 commit = lookup_commit_reference(sha1);
237 if (!commit || parse_commit(commit)) {
238 cgit_print_error(fmt("Invalid commit reference: %s", rev));
239 return;
240 }
241
242 html("path: <a href='");
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100243 html_attr(cgit_pageurl(ctx.qry.repo, "tree", fmt("h=%s", rev)));
Lars Hjemliffc69732007-06-16 20:20:42 +0200244 html("'>root</a>");
245
246 if (path == NULL) {
247 ls_tree(commit->tree->object.sha1, NULL);
248 return;
249 }
250
251 match_path = path;
Lars Hjemli566f92b2008-07-21 10:10:48 +0200252 read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL);
Lars Hjemliffc69732007-06-16 20:20:42 +0200253 ls_tail();
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100254}