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