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