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