|
diff --git a/ui-tree.c b/ui-tree.c index 7912784..9be3140 100644 --- a/ ui-tree.c+++ b/ ui-tree.c |
|
@@ -1,217 +1,218 @@ |
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 "cgit.h" |
9 | #include "cgit.h" |
10 | #include "html.h" |
10 | #include "html.h" |
| |
11 | #include "ui-shared.h" |
11 | |
12 | |
12 | char *curr_rev; |
13 | char *curr_rev; |
13 | char *match_path; |
14 | char *match_path; |
14 | int header = 0; |
15 | int header = 0; |
15 | |
16 | |
16 | static void print_object(const unsigned char *sha1, char *path) |
17 | static void print_object(const unsigned char *sha1, char *path) |
17 | { |
18 | { |
18 | enum object_type type; |
19 | enum object_type type; |
19 | char *buf; |
20 | char *buf; |
20 | unsigned long size, lineno, start, idx; |
21 | unsigned long size, lineno, start, idx; |
21 | const char *linefmt = "<tr><td class='no'><a id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a></td><td class='txt'>"; |
22 | const char *linefmt = "<tr><td class='no'><a id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a></td><td class='txt'>"; |
22 | |
23 | |
23 | type = sha1_object_info(sha1, &size); |
24 | type = sha1_object_info(sha1, &size); |
24 | if (type == OBJ_BAD) { |
25 | if (type == OBJ_BAD) { |
25 | cgit_print_error(fmt("Bad object name: %s", |
26 | cgit_print_error(fmt("Bad object name: %s", |
26 | sha1_to_hex(sha1))); |
27 | sha1_to_hex(sha1))); |
27 | return; |
28 | return; |
28 | } |
29 | } |
29 | |
30 | |
30 | buf = read_sha1_file(sha1, &type, &size); |
31 | buf = read_sha1_file(sha1, &type, &size); |
31 | if (!buf) { |
32 | if (!buf) { |
32 | cgit_print_error(fmt("Error reading object %s", |
33 | cgit_print_error(fmt("Error reading object %s", |
33 | sha1_to_hex(sha1))); |
34 | sha1_to_hex(sha1))); |
34 | return; |
35 | return; |
35 | } |
36 | } |
36 | |
37 | |
37 | html(" blob: <a href='"); |
38 | html(" blob: <a href='"); |
38 | html_attr(cgit_pageurl(ctx.qry.repo, "blob", fmt("id=%s", sha1_to_hex(sha1)))); |
39 | html_attr(cgit_pageurl(ctx.qry.repo, "blob", fmt("id=%s", sha1_to_hex(sha1)))); |
39 | htmlf("'>%s</a>",sha1_to_hex(sha1)); |
40 | htmlf("'>%s</a>",sha1_to_hex(sha1)); |
40 | |
41 | |
41 | html("<table summary='blob content' class='blob'>\n"); |
42 | html("<table summary='blob content' class='blob'>\n"); |
42 | idx = 0; |
43 | idx = 0; |
43 | start = 0; |
44 | start = 0; |
44 | lineno = 0; |
45 | lineno = 0; |
45 | while(idx < size) { |
46 | while(idx < size) { |
46 | if (buf[idx] == '\n') { |
47 | if (buf[idx] == '\n') { |
47 | buf[idx] = '\0'; |
48 | buf[idx] = '\0'; |
48 | htmlf(linefmt, ++lineno); |
49 | htmlf(linefmt, ++lineno); |
49 | html_txt(buf + start); |
50 | html_txt(buf + start); |
50 | html("</td></tr>\n"); |
51 | html("</td></tr>\n"); |
51 | start = idx + 1; |
52 | start = idx + 1; |
52 | } |
53 | } |
53 | idx++; |
54 | idx++; |
54 | } |
55 | } |
55 | htmlf(linefmt, ++lineno); |
56 | htmlf(linefmt, ++lineno); |
56 | html_txt(buf + start); |
57 | html_txt(buf + start); |
57 | html("</td></tr>\n"); |
58 | html("</td></tr>\n"); |
58 | html("</table>\n"); |
59 | html("</table>\n"); |
59 | } |
60 | } |
60 | |
61 | |
61 | |
62 | |
62 | static int ls_item(const unsigned char *sha1, const char *base, int baselen, |
63 | static int ls_item(const unsigned char *sha1, const char *base, int baselen, |
63 | const char *pathname, unsigned int mode, int stage) |
64 | const char *pathname, unsigned int mode, int stage) |
64 | { |
65 | { |
65 | char *name; |
66 | char *name; |
66 | char *fullpath; |
67 | char *fullpath; |
67 | enum object_type type; |
68 | enum object_type type; |
68 | unsigned long size = 0; |
69 | unsigned long size = 0; |
69 | |
70 | |
70 | name = xstrdup(pathname); |
71 | name = xstrdup(pathname); |
71 | fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "", |
72 | fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "", |
72 | ctx.qry.path ? "/" : "", name); |
73 | ctx.qry.path ? "/" : "", name); |
73 | |
74 | |
74 | type = sha1_object_info(sha1, &size); |
75 | type = sha1_object_info(sha1, &size); |
75 | if (type == OBJ_BAD && !S_ISGITLINK(mode)) { |
76 | if (type == OBJ_BAD && !S_ISGITLINK(mode)) { |
76 | htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>", |
77 | htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>", |
77 | name, |
78 | name, |
78 | sha1_to_hex(sha1)); |
79 | sha1_to_hex(sha1)); |
79 | return 0; |
80 | return 0; |
80 | } |
81 | } |
81 | |
82 | |
82 | html("<tr><td class='ls-mode'>"); |
83 | html("<tr><td class='ls-mode'>"); |
83 | cgit_print_filemode(mode); |
84 | cgit_print_filemode(mode); |
84 | html("</td><td>"); |
85 | html("</td><td>"); |
85 | if (S_ISGITLINK(mode)) { |
86 | if (S_ISGITLINK(mode)) { |
86 | htmlf("<a class='ls-mod' href='"); |
87 | htmlf("<a class='ls-mod' href='"); |
87 | html_attr(fmt(ctx.repo->module_link, |
88 | html_attr(fmt(ctx.repo->module_link, |
88 | name, |
89 | name, |
89 | sha1_to_hex(sha1))); |
90 | sha1_to_hex(sha1))); |
90 | html("'>"); |
91 | html("'>"); |
91 | html_txt(name); |
92 | html_txt(name); |
92 | html("</a>"); |
93 | html("</a>"); |
93 | } else if (S_ISDIR(mode)) { |
94 | } else if (S_ISDIR(mode)) { |
94 | cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head, |
95 | cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head, |
95 | curr_rev, fullpath); |
96 | curr_rev, fullpath); |
96 | } else { |
97 | } else { |
97 | cgit_tree_link(name, NULL, "ls-blob", ctx.qry.head, |
98 | cgit_tree_link(name, NULL, "ls-blob", ctx.qry.head, |
98 | curr_rev, fullpath); |
99 | curr_rev, fullpath); |
99 | } |
100 | } |
100 | htmlf("</td><td class='ls-size'>%li</td>", size); |
101 | htmlf("</td><td class='ls-size'>%li</td>", size); |
101 | |
102 | |
102 | html("<td>"); |
103 | html("<td>"); |
103 | cgit_log_link("log", NULL, "button", ctx.qry.head, curr_rev, |
104 | cgit_log_link("log", NULL, "button", ctx.qry.head, curr_rev, |
104 | fullpath, 0, NULL, NULL); |
105 | fullpath, 0, NULL, NULL); |
105 | html("</td></tr>\n"); |
106 | html("</td></tr>\n"); |
106 | free(name); |
107 | free(name); |
107 | return 0; |
108 | return 0; |
108 | } |
109 | } |
109 | |
110 | |
110 | static void ls_head() |
111 | static void ls_head() |
111 | { |
112 | { |
112 | html("<table summary='tree listing' class='list'>\n"); |
113 | html("<table summary='tree listing' class='list'>\n"); |
113 | html("<tr class='nohover'>"); |
114 | html("<tr class='nohover'>"); |
114 | html("<th class='left'>Mode</th>"); |
115 | html("<th class='left'>Mode</th>"); |
115 | html("<th class='left'>Name</th>"); |
116 | html("<th class='left'>Name</th>"); |
116 | html("<th class='right'>Size</th>"); |
117 | html("<th class='right'>Size</th>"); |
117 | html("<th/>"); |
118 | html("<th/>"); |
118 | html("</tr>\n"); |
119 | html("</tr>\n"); |
119 | header = 1; |
120 | header = 1; |
120 | } |
121 | } |
121 | |
122 | |
122 | static void ls_tail() |
123 | static void ls_tail() |
123 | { |
124 | { |
124 | if (!header) |
125 | if (!header) |
125 | return; |
126 | return; |
126 | html("</table>\n"); |
127 | html("</table>\n"); |
127 | header = 0; |
128 | header = 0; |
128 | } |
129 | } |
129 | |
130 | |
130 | static void ls_tree(const unsigned char *sha1, char *path) |
131 | static void ls_tree(const unsigned char *sha1, char *path) |
131 | { |
132 | { |
132 | struct tree *tree; |
133 | struct tree *tree; |
133 | |
134 | |
134 | tree = parse_tree_indirect(sha1); |
135 | tree = parse_tree_indirect(sha1); |
135 | if (!tree) { |
136 | if (!tree) { |
136 | cgit_print_error(fmt("Not a tree object: %s", |
137 | cgit_print_error(fmt("Not a tree object: %s", |
137 | sha1_to_hex(sha1))); |
138 | sha1_to_hex(sha1))); |
138 | return; |
139 | return; |
139 | } |
140 | } |
140 | |
141 | |
141 | ls_head(); |
142 | ls_head(); |
142 | read_tree_recursive(tree, "", 0, 1, NULL, ls_item); |
143 | read_tree_recursive(tree, "", 0, 1, NULL, ls_item); |
143 | ls_tail(); |
144 | ls_tail(); |
144 | } |
145 | } |
145 | |
146 | |
146 | |
147 | |
147 | static int walk_tree(const unsigned char *sha1, const char *base, int baselen, |
148 | static int walk_tree(const unsigned char *sha1, const char *base, int baselen, |
148 | const char *pathname, unsigned mode, int stage) |
149 | const char *pathname, unsigned mode, int stage) |
149 | { |
150 | { |
150 | static int state; |
151 | static int state; |
151 | static char buffer[PATH_MAX]; |
152 | static char buffer[PATH_MAX]; |
152 | char *url; |
153 | char *url; |
153 | |
154 | |
154 | if (state == 0) { |
155 | if (state == 0) { |
155 | memcpy(buffer, base, baselen); |
156 | memcpy(buffer, base, baselen); |
156 | strcpy(buffer+baselen, pathname); |
157 | strcpy(buffer+baselen, pathname); |
157 | url = cgit_pageurl(ctx.qry.repo, "tree", |
158 | url = cgit_pageurl(ctx.qry.repo, "tree", |
158 | fmt("h=%s&path=%s", curr_rev, buffer)); |
159 | fmt("h=%s&path=%s", curr_rev, buffer)); |
159 | html("/"); |
160 | html("/"); |
160 | cgit_tree_link(xstrdup(pathname), NULL, NULL, ctx.qry.head, |
161 | cgit_tree_link(xstrdup(pathname), NULL, NULL, ctx.qry.head, |
161 | curr_rev, buffer); |
162 | curr_rev, buffer); |
162 | |
163 | |
163 | if (strcmp(match_path, buffer)) |
164 | if (strcmp(match_path, buffer)) |
164 | return READ_TREE_RECURSIVE; |
165 | return READ_TREE_RECURSIVE; |
165 | |
166 | |
166 | if (S_ISDIR(mode)) { |
167 | if (S_ISDIR(mode)) { |
167 | state = 1; |
168 | state = 1; |
168 | ls_head(); |
169 | ls_head(); |
169 | return READ_TREE_RECURSIVE; |
170 | return READ_TREE_RECURSIVE; |
170 | } else { |
171 | } else { |
171 | print_object(sha1, buffer); |
172 | print_object(sha1, buffer); |
172 | return 0; |
173 | return 0; |
173 | } |
174 | } |
174 | } |
175 | } |
175 | ls_item(sha1, base, baselen, pathname, mode, stage); |
176 | ls_item(sha1, base, baselen, pathname, mode, stage); |
176 | return 0; |
177 | return 0; |
177 | } |
178 | } |
178 | |
179 | |
179 | |
180 | |
180 | /* |
181 | /* |
181 | * Show a tree or a blob |
182 | * Show a tree or a blob |
182 | * rev: the commit pointing at the root tree object |
183 | * rev: the commit pointing at the root tree object |
183 | * path: path to tree or blob |
184 | * path: path to tree or blob |
184 | */ |
185 | */ |
185 | void cgit_print_tree(const char *rev, char *path) |
186 | void cgit_print_tree(const char *rev, char *path) |
186 | { |
187 | { |
187 | unsigned char sha1[20]; |
188 | unsigned char sha1[20]; |
188 | struct commit *commit; |
189 | struct commit *commit; |
189 | const char *paths[] = {path, NULL}; |
190 | const char *paths[] = {path, NULL}; |
190 | |
191 | |
191 | if (!rev) |
192 | if (!rev) |
192 | rev = ctx.qry.head; |
193 | rev = ctx.qry.head; |
193 | |
194 | |
194 | curr_rev = xstrdup(rev); |
195 | curr_rev = xstrdup(rev); |
195 | if (get_sha1(rev, sha1)) { |
196 | if (get_sha1(rev, sha1)) { |
196 | cgit_print_error(fmt("Invalid revision name: %s", rev)); |
197 | cgit_print_error(fmt("Invalid revision name: %s", rev)); |
197 | return; |
198 | return; |
198 | } |
199 | } |
199 | commit = lookup_commit_reference(sha1); |
200 | commit = lookup_commit_reference(sha1); |
200 | if (!commit || parse_commit(commit)) { |
201 | if (!commit || parse_commit(commit)) { |
201 | cgit_print_error(fmt("Invalid commit reference: %s", rev)); |
202 | cgit_print_error(fmt("Invalid commit reference: %s", rev)); |
202 | return; |
203 | return; |
203 | } |
204 | } |
204 | |
205 | |
205 | html("path: <a href='"); |
206 | html("path: <a href='"); |
206 | html_attr(cgit_pageurl(ctx.qry.repo, "tree", fmt("h=%s", rev))); |
207 | html_attr(cgit_pageurl(ctx.qry.repo, "tree", fmt("h=%s", rev))); |
207 | html("'>root</a>"); |
208 | html("'>root</a>"); |
208 | |
209 | |
209 | if (path == NULL) { |
210 | if (path == NULL) { |
210 | ls_tree(commit->tree->object.sha1, NULL); |
211 | ls_tree(commit->tree->object.sha1, NULL); |
211 | return; |
212 | return; |
212 | } |
213 | } |
213 | |
214 | |
214 | match_path = path; |
215 | match_path = path; |
215 | read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree); |
216 | read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree); |
216 | ls_tail(); |
217 | ls_tail(); |
217 | } |
218 | } |
|