|
diff --git a/cgit.c b/cgit.c index 79e0e43..dbb023e 100644 --- a/ cgit.c+++ b/ cgit.c |
|
@@ -1,106 +1,107 @@ |
1 | /* cgit.c: cgi for the git scm |
1 | /* cgit.c: cgi for the git scm |
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 "cmd.h" |
10 | #include "cmd.h" |
| |
11 | #include "ui-shared.h" |
11 | |
12 | |
12 | static int cgit_prepare_cache(struct cacheitem *item) |
13 | static int cgit_prepare_cache(struct cacheitem *item) |
13 | { |
14 | { |
14 | if (!ctx.repo && ctx.qry.repo) { |
15 | if (!ctx.repo && ctx.qry.repo) { |
15 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, |
16 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, |
16 | "Bad request"); |
17 | "Bad request"); |
17 | cgit_print_http_headers(&ctx); |
18 | cgit_print_http_headers(&ctx); |
18 | cgit_print_docstart(&ctx); |
19 | cgit_print_docstart(&ctx); |
19 | cgit_print_pageheader(&ctx); |
20 | cgit_print_pageheader(&ctx); |
20 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); |
21 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); |
21 | cgit_print_docend(); |
22 | cgit_print_docend(); |
22 | return 0; |
23 | return 0; |
23 | } |
24 | } |
24 | |
25 | |
25 | if (!ctx.repo) { |
26 | if (!ctx.repo) { |
26 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); |
27 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); |
27 | item->ttl = ctx.cfg.cache_root_ttl; |
28 | item->ttl = ctx.cfg.cache_root_ttl; |
28 | return 1; |
29 | return 1; |
29 | } |
30 | } |
30 | |
31 | |
31 | if (!cgit_cmd) { |
32 | if (!cgit_cmd) { |
32 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, |
33 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, |
33 | cache_safe_filename(ctx.repo->url), |
34 | cache_safe_filename(ctx.repo->url), |
34 | cache_safe_filename(ctx.qry.raw))); |
35 | cache_safe_filename(ctx.qry.raw))); |
35 | item->ttl = ctx.cfg.cache_repo_ttl; |
36 | item->ttl = ctx.cfg.cache_repo_ttl; |
36 | } else { |
37 | } else { |
37 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, |
38 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, |
38 | cache_safe_filename(ctx.repo->url), |
39 | cache_safe_filename(ctx.repo->url), |
39 | ctx.qry.page, |
40 | ctx.qry.page, |
40 | cache_safe_filename(ctx.qry.raw))); |
41 | cache_safe_filename(ctx.qry.raw))); |
41 | if (ctx.qry.has_symref) |
42 | if (ctx.qry.has_symref) |
42 | item->ttl = ctx.cfg.cache_dynamic_ttl; |
43 | item->ttl = ctx.cfg.cache_dynamic_ttl; |
43 | else if (ctx.qry.has_sha1) |
44 | else if (ctx.qry.has_sha1) |
44 | item->ttl = ctx.cfg.cache_static_ttl; |
45 | item->ttl = ctx.cfg.cache_static_ttl; |
45 | else |
46 | else |
46 | item->ttl = ctx.cfg.cache_repo_ttl; |
47 | item->ttl = ctx.cfg.cache_repo_ttl; |
47 | } |
48 | } |
48 | return 1; |
49 | return 1; |
49 | } |
50 | } |
50 | |
51 | |
51 | struct refmatch { |
52 | struct refmatch { |
52 | char *req_ref; |
53 | char *req_ref; |
53 | char *first_ref; |
54 | char *first_ref; |
54 | int match; |
55 | int match; |
55 | }; |
56 | }; |
56 | |
57 | |
57 | int find_current_ref(const char *refname, const unsigned char *sha1, |
58 | int find_current_ref(const char *refname, const unsigned char *sha1, |
58 | int flags, void *cb_data) |
59 | int flags, void *cb_data) |
59 | { |
60 | { |
60 | struct refmatch *info; |
61 | struct refmatch *info; |
61 | |
62 | |
62 | info = (struct refmatch *)cb_data; |
63 | info = (struct refmatch *)cb_data; |
63 | if (!strcmp(refname, info->req_ref)) |
64 | if (!strcmp(refname, info->req_ref)) |
64 | info->match = 1; |
65 | info->match = 1; |
65 | if (!info->first_ref) |
66 | if (!info->first_ref) |
66 | info->first_ref = xstrdup(refname); |
67 | info->first_ref = xstrdup(refname); |
67 | return info->match; |
68 | return info->match; |
68 | } |
69 | } |
69 | |
70 | |
70 | char *find_default_branch(struct cgit_repo *repo) |
71 | char *find_default_branch(struct cgit_repo *repo) |
71 | { |
72 | { |
72 | struct refmatch info; |
73 | struct refmatch info; |
73 | |
74 | |
74 | info.req_ref = repo->defbranch; |
75 | info.req_ref = repo->defbranch; |
75 | info.first_ref = NULL; |
76 | info.first_ref = NULL; |
76 | info.match = 0; |
77 | info.match = 0; |
77 | for_each_branch_ref(find_current_ref, &info); |
78 | for_each_branch_ref(find_current_ref, &info); |
78 | if (info.match) |
79 | if (info.match) |
79 | return info.req_ref; |
80 | return info.req_ref; |
80 | else |
81 | else |
81 | return info.first_ref; |
82 | return info.first_ref; |
82 | } |
83 | } |
83 | |
84 | |
84 | static int prepare_repo_cmd(struct cgit_context *ctx) |
85 | static int prepare_repo_cmd(struct cgit_context *ctx) |
85 | { |
86 | { |
86 | char *tmp; |
87 | char *tmp; |
87 | unsigned char sha1[20]; |
88 | unsigned char sha1[20]; |
88 | int nongit = 0; |
89 | int nongit = 0; |
89 | |
90 | |
90 | setenv("GIT_DIR", ctx->repo->path, 1); |
91 | setenv("GIT_DIR", ctx->repo->path, 1); |
91 | setup_git_directory_gently(&nongit); |
92 | setup_git_directory_gently(&nongit); |
92 | if (nongit) { |
93 | if (nongit) { |
93 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, |
94 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, |
94 | "config error"); |
95 | "config error"); |
95 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); |
96 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); |
96 | ctx->repo = NULL; |
97 | ctx->repo = NULL; |
97 | cgit_print_http_headers(ctx); |
98 | cgit_print_http_headers(ctx); |
98 | cgit_print_docstart(ctx); |
99 | cgit_print_docstart(ctx); |
99 | cgit_print_pageheader(ctx); |
100 | cgit_print_pageheader(ctx); |
100 | cgit_print_error(tmp); |
101 | cgit_print_error(tmp); |
101 | cgit_print_docend(); |
102 | cgit_print_docend(); |
102 | return 1; |
103 | return 1; |
103 | } |
104 | } |
104 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); |
105 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); |
105 | |
106 | |
106 | if (!ctx->qry.head) { |
107 | if (!ctx->qry.head) { |
|