summaryrefslogtreecommitdiffabout
authorLars Hjemli <hjemli@gmail.com>2008-02-16 20:16:53 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2008-02-16 20:48:19 (UTC)
commitb88fb016d0209f7041ac7d3b4d2c077318407a4d (patch) (unidiff)
tree777e9cd042c3da9caaefe1f63363a52b56601521
parentd1f3bbe9d22029f45a77bb938c176ccc0c827d46 (diff)
downloadcgit-b88fb016d0209f7041ac7d3b4d2c077318407a4d.zip
cgit-b88fb016d0209f7041ac7d3b4d2c077318407a4d.tar.gz
cgit-b88fb016d0209f7041ac7d3b4d2c077318407a4d.tar.bz2
Improve initialization of git directory
Using the functions offered by libgit feels like the right thing to do. Also, make sure that config errors gets properly reported. Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c17
-rw-r--r--ui-shared.c2
2 files changed, 9 insertions, 10 deletions
diff --git a/cgit.c b/cgit.c
index b270fdc..2c933dc 100644
--- a/cgit.c
+++ b/cgit.c
@@ -54,78 +54,81 @@ struct refmatch {
54int find_current_ref(const char *refname, const unsigned char *sha1, 54int find_current_ref(const char *refname, const unsigned char *sha1,
55 int flags, void *cb_data) 55 int flags, void *cb_data)
56{ 56{
57 struct refmatch *info; 57 struct refmatch *info;
58 58
59 info = (struct refmatch *)cb_data; 59 info = (struct refmatch *)cb_data;
60 if (!strcmp(refname, info->req_ref)) 60 if (!strcmp(refname, info->req_ref))
61 info->match = 1; 61 info->match = 1;
62 if (!info->first_ref) 62 if (!info->first_ref)
63 info->first_ref = xstrdup(refname); 63 info->first_ref = xstrdup(refname);
64 return info->match; 64 return info->match;
65} 65}
66 66
67char *find_default_branch(struct cgit_repo *repo) 67char *find_default_branch(struct cgit_repo *repo)
68{ 68{
69 struct refmatch info; 69 struct refmatch info;
70 70
71 info.req_ref = repo->defbranch; 71 info.req_ref = repo->defbranch;
72 info.first_ref = NULL; 72 info.first_ref = NULL;
73 info.match = 0; 73 info.match = 0;
74 for_each_branch_ref(find_current_ref, &info); 74 for_each_branch_ref(find_current_ref, &info);
75 if (info.match) 75 if (info.match)
76 return info.req_ref; 76 return info.req_ref;
77 else 77 else
78 return info.first_ref; 78 return info.first_ref;
79} 79}
80 80
81static void cgit_print_repo_page(struct cacheitem *item) 81static void cgit_print_repo_page(struct cacheitem *item)
82{ 82{
83 char *title, *tmp; 83 char *title, *tmp;
84 int show_search; 84 int show_search;
85 unsigned char sha1[20]; 85 unsigned char sha1[20];
86 int nongit = 0;
86 87
87 if (chdir(ctx.repo->path)) { 88 setenv("GIT_DIR", ctx.repo->path, 1);
88 title = fmt("%s - %s", ctx.cfg.root_title, "Bad request"); 89 setup_git_directory_gently(&nongit);
90 if (nongit) {
91 title = fmt("%s - %s", ctx.cfg.root_title, "config error");
92 tmp = fmt("Not a git repository: '%s'", ctx.repo->path);
93 ctx.repo = NULL;
89 cgit_print_docstart(title, item); 94 cgit_print_docstart(title, item);
90 cgit_print_pageheader(title, 0); 95 cgit_print_pageheader(title, 0);
91 cgit_print_error(fmt("Unable to scan repository: %s", 96 cgit_print_error(tmp);
92 strerror(errno)));
93 cgit_print_docend(); 97 cgit_print_docend();
94 return; 98 return;
95 } 99 }
96 100
97 title = fmt("%s - %s", ctx.repo->name, ctx.repo->desc); 101 title = fmt("%s - %s", ctx.repo->name, ctx.repo->desc);
98 show_search = 0; 102 show_search = 0;
99 setenv("GIT_DIR", ctx.repo->path, 1);
100 103
101 if (!ctx.qry.head) { 104 if (!ctx.qry.head) {
102 ctx.qry.head = xstrdup(find_default_branch(ctx.repo)); 105 ctx.qry.head = xstrdup(find_default_branch(ctx.repo));
103 ctx.repo->defbranch = ctx.qry.head; 106 ctx.repo->defbranch = ctx.qry.head;
104 } 107 }
105 108
106 if (!ctx.qry.head) { 109 if (!ctx.qry.head) {
107 cgit_print_docstart(title, item); 110 cgit_print_docstart(title, item);
108 cgit_print_pageheader(title, 0); 111 cgit_print_pageheader(title, 0);
109 cgit_print_error("Repository seems to be empty"); 112 cgit_print_error("Repository seems to be empty");
110 cgit_print_docend(); 113 cgit_print_docend();
111 return; 114 return;
112 } 115 }
113 116
114 if (get_sha1(ctx.qry.head, sha1)) { 117 if (get_sha1(ctx.qry.head, sha1)) {
115 tmp = xstrdup(ctx.qry.head); 118 tmp = xstrdup(ctx.qry.head);
116 ctx.qry.head = ctx.repo->defbranch; 119 ctx.qry.head = ctx.repo->defbranch;
117 cgit_print_docstart(title, item); 120 cgit_print_docstart(title, item);
118 cgit_print_pageheader(title, 0); 121 cgit_print_pageheader(title, 0);
119 cgit_print_error(fmt("Invalid branch: %s", tmp)); 122 cgit_print_error(fmt("Invalid branch: %s", tmp));
120 cgit_print_docend(); 123 cgit_print_docend();
121 return; 124 return;
122 } 125 }
123 126
124 if ((cgit_cmd == CMD_SNAPSHOT) && ctx.repo->snapshots) { 127 if ((cgit_cmd == CMD_SNAPSHOT) && ctx.repo->snapshots) {
125 cgit_print_snapshot(item, ctx.qry.head, ctx.qry.sha1, 128 cgit_print_snapshot(item, ctx.qry.head, ctx.qry.sha1,
126 cgit_repobasename(ctx.repo->url), 129 cgit_repobasename(ctx.repo->url),
127 ctx.qry.path, 130 ctx.qry.path,
128 ctx.repo->snapshots ); 131 ctx.repo->snapshots );
129 return; 132 return;
130 } 133 }
131 134
@@ -150,90 +153,86 @@ static void cgit_print_repo_page(struct cacheitem *item)
150 153
151 cgit_print_pageheader(ctx.qry.page, show_search); 154 cgit_print_pageheader(ctx.qry.page, show_search);
152 155
153 switch(cgit_cmd) { 156 switch(cgit_cmd) {
154 case CMD_LOG: 157 case CMD_LOG:
155 cgit_print_log(ctx.qry.sha1, ctx.qry.ofs, 158 cgit_print_log(ctx.qry.sha1, ctx.qry.ofs,
156 ctx.cfg.max_commit_count, ctx.qry.grep, ctx.qry.search, 159 ctx.cfg.max_commit_count, ctx.qry.grep, ctx.qry.search,
157 ctx.qry.path, 1); 160 ctx.qry.path, 1);
158 break; 161 break;
159 case CMD_TREE: 162 case CMD_TREE:
160 cgit_print_tree(ctx.qry.sha1, ctx.qry.path); 163 cgit_print_tree(ctx.qry.sha1, ctx.qry.path);
161 break; 164 break;
162 case CMD_COMMIT: 165 case CMD_COMMIT:
163 cgit_print_commit(ctx.qry.sha1); 166 cgit_print_commit(ctx.qry.sha1);
164 break; 167 break;
165 case CMD_REFS: 168 case CMD_REFS:
166 cgit_print_refs(); 169 cgit_print_refs();
167 break; 170 break;
168 case CMD_TAG: 171 case CMD_TAG:
169 cgit_print_tag(ctx.qry.sha1); 172 cgit_print_tag(ctx.qry.sha1);
170 break; 173 break;
171 case CMD_DIFF: 174 case CMD_DIFF:
172 cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path); 175 cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path);
173 break; 176 break;
174 default: 177 default:
175 cgit_print_error("Invalid request"); 178 cgit_print_error("Invalid request");
176 } 179 }
177 cgit_print_docend(); 180 cgit_print_docend();
178} 181}
179 182
180static void cgit_fill_cache(struct cacheitem *item, int use_cache) 183static void cgit_fill_cache(struct cacheitem *item, int use_cache)
181{ 184{
182 static char buf[PATH_MAX];
183 int stdout2; 185 int stdout2;
184 186
185 getcwd(buf, sizeof(buf));
186 item->st.st_mtime = time(NULL); 187 item->st.st_mtime = time(NULL);
187 188
188 if (use_cache) { 189 if (use_cache) {
189 stdout2 = chk_positive(dup(STDOUT_FILENO), 190 stdout2 = chk_positive(dup(STDOUT_FILENO),
190 "Preserving STDOUT"); 191 "Preserving STDOUT");
191 chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); 192 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
192 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); 193 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
193 } 194 }
194 195
195 if (ctx.repo) 196 if (ctx.repo)
196 cgit_print_repo_page(item); 197 cgit_print_repo_page(item);
197 else 198 else
198 cgit_print_repolist(item); 199 cgit_print_repolist(item);
199 200
200 if (use_cache) { 201 if (use_cache) {
201 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); 202 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
202 chk_positive(dup2(stdout2, STDOUT_FILENO), 203 chk_positive(dup2(stdout2, STDOUT_FILENO),
203 "Restoring original STDOUT"); 204 "Restoring original STDOUT");
204 chk_zero(close(stdout2), "Closing temporary STDOUT"); 205 chk_zero(close(stdout2), "Closing temporary STDOUT");
205 } 206 }
206
207 chdir(buf);
208} 207}
209 208
210static void cgit_check_cache(struct cacheitem *item) 209static void cgit_check_cache(struct cacheitem *item)
211{ 210{
212 int i = 0; 211 int i = 0;
213 212
214 top: 213 top:
215 if (++i > ctx.cfg.max_lock_attempts) { 214 if (++i > ctx.cfg.max_lock_attempts) {
216 die("cgit_refresh_cache: unable to lock %s: %s", 215 die("cgit_refresh_cache: unable to lock %s: %s",
217 item->name, strerror(errno)); 216 item->name, strerror(errno));
218 } 217 }
219 if (!cache_exist(item)) { 218 if (!cache_exist(item)) {
220 if (!cache_lock(item)) { 219 if (!cache_lock(item)) {
221 sleep(1); 220 sleep(1);
222 goto top; 221 goto top;
223 } 222 }
224 if (!cache_exist(item)) { 223 if (!cache_exist(item)) {
225 cgit_fill_cache(item, 1); 224 cgit_fill_cache(item, 1);
226 cache_unlock(item); 225 cache_unlock(item);
227 } else { 226 } else {
228 cache_cancel_lock(item); 227 cache_cancel_lock(item);
229 } 228 }
230 } else if (cache_expired(item) && cache_lock(item)) { 229 } else if (cache_expired(item) && cache_lock(item)) {
231 if (cache_expired(item)) { 230 if (cache_expired(item)) {
232 cgit_fill_cache(item, 1); 231 cgit_fill_cache(item, 1);
233 cache_unlock(item); 232 cache_unlock(item);
234 } else { 233 } else {
235 cache_cancel_lock(item); 234 cache_cancel_lock(item);
236 } 235 }
237 } 236 }
238} 237}
239 238
diff --git a/ui-shared.c b/ui-shared.c
index b9f487a..cc1ab8b 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -447,65 +447,65 @@ void add_hidden_formfields(int incl_head, int incl_search, char *page)
447 } 447 }
448 448
449 if (incl_head && strcmp(ctx.qry.head, ctx.repo->defbranch)) 449 if (incl_head && strcmp(ctx.qry.head, ctx.repo->defbranch))
450 html_hidden("h", ctx.qry.head); 450 html_hidden("h", ctx.qry.head);
451 451
452 if (ctx.qry.sha1) 452 if (ctx.qry.sha1)
453 html_hidden("id", ctx.qry.sha1); 453 html_hidden("id", ctx.qry.sha1);
454 if (ctx.qry.sha2) 454 if (ctx.qry.sha2)
455 html_hidden("id2", ctx.qry.sha2); 455 html_hidden("id2", ctx.qry.sha2);
456 456
457 if (incl_search) { 457 if (incl_search) {
458 if (ctx.qry.grep) 458 if (ctx.qry.grep)
459 html_hidden("qt", ctx.qry.grep); 459 html_hidden("qt", ctx.qry.grep);
460 if (ctx.qry.search) 460 if (ctx.qry.search)
461 html_hidden("q", ctx.qry.search); 461 html_hidden("q", ctx.qry.search);
462 } 462 }
463} 463}
464 464
465void cgit_print_pageheader(char *title, int show_search) 465void cgit_print_pageheader(char *title, int show_search)
466{ 466{
467 static const char *default_info = "This is cgit, a fast webinterface for git repositories"; 467 static const char *default_info = "This is cgit, a fast webinterface for git repositories";
468 int header = 0; 468 int header = 0;
469 char *url; 469 char *url;
470 470
471 html("<table id='layout' summary=''>\n"); 471 html("<table id='layout' summary=''>\n");
472 html("<tr><td id='sidebar'>\n"); 472 html("<tr><td id='sidebar'>\n");
473 html("<table class='sidebar' cellspacing='0' summary=''>\n"); 473 html("<table class='sidebar' cellspacing='0' summary=''>\n");
474 html("<tr><td class='sidebar'>\n<a href='"); 474 html("<tr><td class='sidebar'>\n<a href='");
475 html_attr(cgit_rooturl()); 475 html_attr(cgit_rooturl());
476 htmlf("'><img src='%s' alt='cgit'/></a>\n", 476 htmlf("'><img src='%s' alt='cgit'/></a>\n",
477 ctx.cfg.logo); 477 ctx.cfg.logo);
478 html("</td></tr>\n<tr><td class='sidebar'>\n"); 478 html("</td></tr>\n<tr><td class='sidebar'>\n");
479 if (ctx.qry.repo) { 479 if (ctx.repo) {
480 html("<h1 class='first'>"); 480 html("<h1 class='first'>");
481 html_txt(strrpart(ctx.repo->name, 20)); 481 html_txt(strrpart(ctx.repo->name, 20));
482 html("</h1>\n"); 482 html("</h1>\n");
483 html_txt(ctx.repo->desc); 483 html_txt(ctx.repo->desc);
484 if (ctx.repo->owner) { 484 if (ctx.repo->owner) {
485 html("<h1>owner</h1>\n"); 485 html("<h1>owner</h1>\n");
486 html_txt(ctx.repo->owner); 486 html_txt(ctx.repo->owner);
487 } 487 }
488 html("<h1>navigate</h1>\n"); 488 html("<h1>navigate</h1>\n");
489 reporevlink(NULL, "summary", NULL, "menu", ctx.qry.head, 489 reporevlink(NULL, "summary", NULL, "menu", ctx.qry.head,
490 NULL, NULL); 490 NULL, NULL);
491 cgit_log_link("log", NULL, "menu", ctx.qry.head, NULL, NULL, 491 cgit_log_link("log", NULL, "menu", ctx.qry.head, NULL, NULL,
492 0, NULL, NULL); 492 0, NULL, NULL);
493 cgit_tree_link("tree", NULL, "menu", ctx.qry.head, 493 cgit_tree_link("tree", NULL, "menu", ctx.qry.head,
494 ctx.qry.sha1, NULL); 494 ctx.qry.sha1, NULL);
495 cgit_commit_link("commit", NULL, "menu", ctx.qry.head, 495 cgit_commit_link("commit", NULL, "menu", ctx.qry.head,
496 ctx.qry.sha1); 496 ctx.qry.sha1);
497 cgit_diff_link("diff", NULL, "menu", ctx.qry.head, 497 cgit_diff_link("diff", NULL, "menu", ctx.qry.head,
498 ctx.qry.sha1, ctx.qry.sha2, NULL); 498 ctx.qry.sha1, ctx.qry.sha2, NULL);
499 cgit_patch_link("patch", NULL, "menu", ctx.qry.head, 499 cgit_patch_link("patch", NULL, "menu", ctx.qry.head,
500 ctx.qry.sha1); 500 ctx.qry.sha1);
501 501
502 for_each_ref(print_archive_ref, &header); 502 for_each_ref(print_archive_ref, &header);
503 503
504 if (ctx.repo->clone_url || ctx.cfg.clone_prefix) { 504 if (ctx.repo->clone_url || ctx.cfg.clone_prefix) {
505 html("<h1>clone</h1>\n"); 505 html("<h1>clone</h1>\n");
506 if (ctx.repo->clone_url) 506 if (ctx.repo->clone_url)
507 url = ctx.repo->clone_url; 507 url = ctx.repo->clone_url;
508 else 508 else
509 url = fmt("%s%s", ctx.cfg.clone_prefix, 509 url = fmt("%s%s", ctx.cfg.clone_prefix,
510 ctx.repo->url); 510 ctx.repo->url);
511 html("<a class='menu' href='"); 511 html("<a class='menu' href='");