]> git.cameronkatri.com Git - cgit.git/blob - ui-tree.c
c6159ec3df97e059e1cd894202b4173172e9c9c1
[cgit.git] / ui-tree.c
1 /* 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
9 #include <ctype.h>
10 #include "cgit.h"
11 #include "html.h"
12 #include "ui-shared.h"
13
14 char *curr_rev;
15 char *match_path;
16 int header = 0;
17
18 static 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
40 #define ROWLEN 32
41
42 static void print_binary_buffer(char *buf, unsigned long size)
43 {
44 unsigned long ofs, idx;
45 static char ascii[ROWLEN + 1];
46
47 html("<table summary='blob content' class='bin-blob'>\n");
48 html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
49 for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
50 htmlf("<tr><td class='right'>%04x</td><td class='hex'>", ofs);
51 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
52 htmlf("%*s%02x",
53 idx == 16 ? 4 : 1, "",
54 buf[idx] & 0xff);
55 html(" </td><td class='hex'>");
56 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
57 ascii[idx] = isgraph(buf[idx]) ? buf[idx] : '.';
58 ascii[idx] = '\0';
59 html_txt(ascii);
60 html("</td></tr>\n");
61 }
62 html("</table>\n");
63 }
64
65 static void print_object(const unsigned char *sha1, char *path)
66 {
67 enum object_type type;
68 char *buf;
69 unsigned long size;
70
71 type = sha1_object_info(sha1, &size);
72 if (type == OBJ_BAD) {
73 cgit_print_error(fmt("Bad object name: %s",
74 sha1_to_hex(sha1)));
75 return;
76 }
77
78 buf = read_sha1_file(sha1, &type, &size);
79 if (!buf) {
80 cgit_print_error(fmt("Error reading object %s",
81 sha1_to_hex(sha1)));
82 return;
83 }
84
85 html(" (");
86 cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
87 curr_rev, path);
88 htmlf(")<br/>blob: %s\n", sha1_to_hex(sha1));
89
90 if (buffer_is_binary(buf, size))
91 print_binary_buffer(buf, size);
92 else
93 print_text_buffer(buf, size);
94 }
95
96
97 static int ls_item(const unsigned char *sha1, const char *base, int baselen,
98 const char *pathname, unsigned int mode, int stage,
99 void *cbdata)
100 {
101 char *name;
102 char *fullpath;
103 enum object_type type;
104 unsigned long size = 0;
105
106 name = xstrdup(pathname);
107 fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "",
108 ctx.qry.path ? "/" : "", name);
109
110 if (!S_ISGITLINK(mode)) {
111 type = sha1_object_info(sha1, &size);
112 if (type == OBJ_BAD) {
113 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
114 name,
115 sha1_to_hex(sha1));
116 return 0;
117 }
118 }
119
120 html("<tr><td class='ls-mode'>");
121 cgit_print_filemode(mode);
122 html("</td><td>");
123 if (S_ISGITLINK(mode)) {
124 htmlf("<a class='ls-mod' href='");
125 html_attr(fmt(ctx.repo->module_link,
126 name,
127 sha1_to_hex(sha1)));
128 html("'>");
129 html_txt(name);
130 html("</a>");
131 } else if (S_ISDIR(mode)) {
132 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
133 curr_rev, fullpath);
134 } else {
135 cgit_tree_link(name, NULL, "ls-blob", ctx.qry.head,
136 curr_rev, fullpath);
137 }
138 htmlf("</td><td class='ls-size'>%li</td>", size);
139
140 html("<td>");
141 cgit_log_link("log", NULL, "button", ctx.qry.head, curr_rev,
142 fullpath, 0, NULL, NULL, ctx.qry.showmsg);
143 if (ctx.repo->max_stats)
144 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
145 fullpath);
146 html("</td></tr>\n");
147 free(name);
148 return 0;
149 }
150
151 static void ls_head()
152 {
153 html("<table summary='tree listing' class='list'>\n");
154 html("<tr class='nohover'>");
155 html("<th class='left'>Mode</th>");
156 html("<th class='left'>Name</th>");
157 html("<th class='right'>Size</th>");
158 html("<th/>");
159 html("</tr>\n");
160 header = 1;
161 }
162
163 static void ls_tail()
164 {
165 if (!header)
166 return;
167 html("</table>\n");
168 header = 0;
169 }
170
171 static void ls_tree(const unsigned char *sha1, char *path)
172 {
173 struct tree *tree;
174
175 tree = parse_tree_indirect(sha1);
176 if (!tree) {
177 cgit_print_error(fmt("Not a tree object: %s",
178 sha1_to_hex(sha1)));
179 return;
180 }
181
182 ls_head();
183 read_tree_recursive(tree, "", 0, 1, NULL, ls_item, NULL);
184 ls_tail();
185 }
186
187
188 static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
189 const char *pathname, unsigned mode, int stage,
190 void *cbdata)
191 {
192 static int state;
193 static char buffer[PATH_MAX];
194 char *url;
195
196 if (state == 0) {
197 memcpy(buffer, base, baselen);
198 strcpy(buffer+baselen, pathname);
199 url = cgit_pageurl(ctx.qry.repo, "tree",
200 fmt("h=%s&amp;path=%s", curr_rev, buffer));
201 html("/");
202 cgit_tree_link(xstrdup(pathname), NULL, NULL, ctx.qry.head,
203 curr_rev, buffer);
204
205 if (strcmp(match_path, buffer))
206 return READ_TREE_RECURSIVE;
207
208 if (S_ISDIR(mode)) {
209 state = 1;
210 ls_head();
211 return READ_TREE_RECURSIVE;
212 } else {
213 print_object(sha1, buffer);
214 return 0;
215 }
216 }
217 ls_item(sha1, base, baselen, pathname, mode, stage, NULL);
218 return 0;
219 }
220
221
222 /*
223 * Show a tree or a blob
224 * rev: the commit pointing at the root tree object
225 * path: path to tree or blob
226 */
227 void cgit_print_tree(const char *rev, char *path)
228 {
229 unsigned char sha1[20];
230 struct commit *commit;
231 const char *paths[] = {path, NULL};
232
233 if (!rev)
234 rev = ctx.qry.head;
235
236 curr_rev = xstrdup(rev);
237 if (get_sha1(rev, sha1)) {
238 cgit_print_error(fmt("Invalid revision name: %s", rev));
239 return;
240 }
241 commit = lookup_commit_reference(sha1);
242 if (!commit || parse_commit(commit)) {
243 cgit_print_error(fmt("Invalid commit reference: %s", rev));
244 return;
245 }
246
247 html("path: <a href='");
248 html_attr(cgit_pageurl(ctx.qry.repo, "tree", fmt("h=%s", rev)));
249 html("'>root</a>");
250
251 if (path == NULL) {
252 ls_tree(commit->tree->object.sha1, NULL);
253 return;
254 }
255
256 match_path = path;
257 read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL);
258 ls_tail();
259 }