aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLukas Fleischer2013-03-03 17:22:30 +0100
committerJason A. Donenfeld2013-03-04 09:12:55 -0500
commit210a5711ad8135de025b1a058079eead3d680a67 (patch)
treeae8f59c0b2e73a4a708afe9f81cbc727270ad047
parentbfe576624541febd6005ad6a4633680369a7dc34 (diff)
downloadcgit-210a5711ad8135de025b1a058079eead3d680a67.tar.gz
cgit-210a5711ad8135de025b1a058079eead3d680a67.tar.bz2
cgit-210a5711ad8135de025b1a058079eead3d680a67.zip
ui-tree.c: Use a context structure in walk_tree()
Use the context pointer to pass context information instead of misusing global variables, as we already did in "ui-blob.c" and in "ui-plain.c". In addition to the fixes to walk_tree(), pass the same structure to ls_tree() and ls_item() which is read_tree_recursive()-based as well. Signed-off-by: Lukas Fleischer <cgit@cryptocrack.de>
-rw-r--r--ui-tree.c51
1 files changed, 29 insertions, 22 deletions
diff --git a/ui-tree.c b/ui-tree.c
index 26f06e3..561f9e7 100644
--- a/ui-tree.c
+++ b/ui-tree.c
@@ -11,9 +11,11 @@
11#include "html.h" 11#include "html.h"
12#include "ui-shared.h" 12#include "ui-shared.h"
13 13
14char *curr_rev; 14struct walk_tree_context {
15char *match_path; 15 char *curr_rev;
16static int state; 16 char *match_path;
17 int state;
18};
17 19
18static void print_text_buffer(const char *name, char *buf, unsigned long size) 20static void print_text_buffer(const char *name, char *buf, unsigned long size)
19{ 21{
@@ -126,6 +128,7 @@ static int ls_item(const unsigned char *sha1, const char *base, int baselen,
126 const char *pathname, unsigned int mode, int stage, 128 const char *pathname, unsigned int mode, int stage,
127 void *cbdata) 129 void *cbdata)
128{ 130{
131 struct walk_tree_context *walk_tree_ctx = cbdata;
129 char *name; 132 char *name;
130 char *fullpath; 133 char *fullpath;
131 char *class; 134 char *class;
@@ -153,7 +156,7 @@ static int ls_item(const unsigned char *sha1, const char *base, int baselen,
153 cgit_submodule_link("ls-mod", fullpath, sha1_to_hex(sha1)); 156 cgit_submodule_link("ls-mod", fullpath, sha1_to_hex(sha1));
154 } else if (S_ISDIR(mode)) { 157 } else if (S_ISDIR(mode)) {
155 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head, 158 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
156 curr_rev, fullpath); 159 walk_tree_ctx->curr_rev, fullpath);
157 } else { 160 } else {
158 class = strrchr(name, '.'); 161 class = strrchr(name, '.');
159 if (class != NULL) { 162 if (class != NULL) {
@@ -161,19 +164,20 @@ static int ls_item(const unsigned char *sha1, const char *base, int baselen,
161 } else 164 } else
162 class = "ls-blob"; 165 class = "ls-blob";
163 cgit_tree_link(name, NULL, class, ctx.qry.head, 166 cgit_tree_link(name, NULL, class, ctx.qry.head,
164 curr_rev, fullpath); 167 walk_tree_ctx->curr_rev, fullpath);
165 } 168 }
166 htmlf("</td><td class='ls-size'>%li</td>", size); 169 htmlf("</td><td class='ls-size'>%li</td>", size);
167 170
168 html("<td>"); 171 html("<td>");
169 cgit_log_link("log", NULL, "button", ctx.qry.head, curr_rev, 172 cgit_log_link("log", NULL, "button", ctx.qry.head,
170 fullpath, 0, NULL, NULL, ctx.qry.showmsg); 173 walk_tree_ctx->curr_rev, fullpath, 0, NULL, NULL,
174 ctx.qry.showmsg);
171 if (ctx.repo->max_stats) 175 if (ctx.repo->max_stats)
172 cgit_stats_link("stats", NULL, "button", ctx.qry.head, 176 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
173 fullpath); 177 fullpath);
174 if (!S_ISGITLINK(mode)) 178 if (!S_ISGITLINK(mode))
175 cgit_plain_link("plain", NULL, "button", ctx.qry.head, curr_rev, 179 cgit_plain_link("plain", NULL, "button", ctx.qry.head,
176 fullpath); 180 walk_tree_ctx->curr_rev, fullpath);
177 html("</td></tr>\n"); 181 html("</td></tr>\n");
178 free(name); 182 free(name);
179 return 0; 183 return 0;
@@ -195,7 +199,7 @@ static void ls_tail()
195 html("</table>\n"); 199 html("</table>\n");
196} 200}
197 201
198static void ls_tree(const unsigned char *sha1, char *path) 202static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_context *walk_tree_ctx)
199{ 203{
200 struct tree *tree; 204 struct tree *tree;
201 struct pathspec paths = { 205 struct pathspec paths = {
@@ -210,7 +214,7 @@ static void ls_tree(const unsigned char *sha1, char *path)
210 } 214 }
211 215
212 ls_head(); 216 ls_head();
213 read_tree_recursive(tree, "", 0, 1, &paths, ls_item, NULL); 217 read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
214 ls_tail(); 218 ls_tail();
215} 219}
216 220
@@ -219,24 +223,25 @@ static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
219 const char *pathname, unsigned mode, int stage, 223 const char *pathname, unsigned mode, int stage,
220 void *cbdata) 224 void *cbdata)
221{ 225{
226 struct walk_tree_context *walk_tree_ctx = cbdata;
222 static char buffer[PATH_MAX]; 227 static char buffer[PATH_MAX];
223 228
224 if (state == 0) { 229 if (walk_tree_ctx->state == 0) {
225 memcpy(buffer, base, baselen); 230 memcpy(buffer, base, baselen);
226 strcpy(buffer + baselen, pathname); 231 strcpy(buffer + baselen, pathname);
227 if (strcmp(match_path, buffer)) 232 if (strcmp(walk_tree_ctx->match_path, buffer))
228 return READ_TREE_RECURSIVE; 233 return READ_TREE_RECURSIVE;
229 234
230 if (S_ISDIR(mode)) { 235 if (S_ISDIR(mode)) {
231 state = 1; 236 walk_tree_ctx->state = 1;
232 ls_head(); 237 ls_head();
233 return READ_TREE_RECURSIVE; 238 return READ_TREE_RECURSIVE;
234 } else { 239 } else {
235 print_object(sha1, buffer, pathname, curr_rev); 240 print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev);
236 return 0; 241 return 0;
237 } 242 }
238 } 243 }
239 ls_item(sha1, base, baselen, pathname, mode, stage, NULL); 244 ls_item(sha1, base, baselen, pathname, mode, stage, walk_tree_ctx);
240 return 0; 245 return 0;
241} 246}
242 247
@@ -258,11 +263,15 @@ void cgit_print_tree(const char *rev, char *path)
258 .nr = path ? 1 : 0, 263 .nr = path ? 1 : 0,
259 .items = &path_items 264 .items = &path_items
260 }; 265 };
266 struct walk_tree_context walk_tree_ctx = {
267 .match_path = path,
268 .state = 0
269 };
261 270
262 if (!rev) 271 if (!rev)
263 rev = ctx.qry.head; 272 rev = ctx.qry.head;
264 273
265 curr_rev = xstrdup(rev); 274 walk_tree_ctx.curr_rev = xstrdup(rev);
266 if (get_sha1(rev, sha1)) { 275 if (get_sha1(rev, sha1)) {
267 cgit_print_error(fmt("Invalid revision name: %s", rev)); 276 cgit_print_error(fmt("Invalid revision name: %s", rev));
268 return; 277 return;
@@ -274,13 +283,11 @@ void cgit_print_tree(const char *rev, char *path)
274 } 283 }
275 284
276 if (path == NULL) { 285 if (path == NULL) {
277 ls_tree(commit->tree->object.sha1, NULL); 286 ls_tree(commit->tree->object.sha1, NULL, &walk_tree_ctx);
278 return; 287 return;
279 } 288 }
280 289
281 match_path = path; 290 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
282 state = 0; 291 if (walk_tree_ctx.state == 1)
283 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, NULL);
284 if (state == 1)
285 ls_tail(); 292 ls_tail();
286} 293}