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