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