author | Lars Hjemli <hjemli@gmail.com> | 2008-03-23 23:51:19 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-03-23 23:51:19 (UTC) |
commit | f3c1a187fe2bc33f8423cd535d5045899699995b (patch) (unidiff) | |
tree | b5c553da7b108900535fcfcd24b78bdd0ac62387 | |
parent | b1f9b9c1459cb9a30ebf80721aff6ef788d1f891 (diff) | |
download | cgit-f3c1a187fe2bc33f8423cd535d5045899699995b.zip cgit-f3c1a187fe2bc33f8423cd535d5045899699995b.tar.gz cgit-f3c1a187fe2bc33f8423cd535d5045899699995b.tar.bz2 |
Add struct cgit_page to cgit_context
This struct is used when generating http headers, and as such is another
small step towards the goal of the whole cleanup series; to invoke each
page/view function with a function pointer.
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | cgit.c | 50 | ||||
-rw-r--r-- | cgit.h | 18 | ||||
-rw-r--r-- | shared.c | 3 | ||||
-rw-r--r-- | ui-blob.c | 4 | ||||
-rw-r--r-- | ui-patch.c | 4 | ||||
-rw-r--r-- | ui-repolist.c | 6 | ||||
-rw-r--r-- | ui-shared.c | 112 | ||||
-rw-r--r-- | ui-snapshot.c | 4 |
8 files changed, 113 insertions, 88 deletions
@@ -1,243 +1,259 @@ | |||
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 | 11 | ||
11 | static int cgit_prepare_cache(struct cacheitem *item) | 12 | static int cgit_prepare_cache(struct cacheitem *item) |
12 | { | 13 | { |
13 | if (!ctx.repo && ctx.qry.repo) { | 14 | if (!ctx.repo && ctx.qry.repo) { |
14 | char *title = fmt("%s - %s", ctx.cfg.root_title, "Bad request"); | 15 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, |
15 | cgit_print_docstart(title, item); | 16 | "Bad request"); |
16 | cgit_print_pageheader(title, 0); | 17 | cgit_print_http_headers(&ctx); |
18 | cgit_print_docstart(&ctx); | ||
19 | cgit_print_pageheader(&ctx); | ||
17 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); | 20 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); |
18 | cgit_print_docend(); | 21 | cgit_print_docend(); |
19 | return 0; | 22 | return 0; |
20 | } | 23 | } |
21 | 24 | ||
22 | if (!ctx.repo) { | 25 | if (!ctx.repo) { |
23 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); | 26 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); |
24 | item->ttl = ctx.cfg.cache_root_ttl; | 27 | item->ttl = ctx.cfg.cache_root_ttl; |
25 | return 1; | 28 | return 1; |
26 | } | 29 | } |
27 | 30 | ||
28 | if (!cgit_cmd) { | 31 | if (!cgit_cmd) { |
29 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, | 32 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, |
30 | cache_safe_filename(ctx.repo->url), | 33 | cache_safe_filename(ctx.repo->url), |
31 | cache_safe_filename(ctx.qry.raw))); | 34 | cache_safe_filename(ctx.qry.raw))); |
32 | item->ttl = ctx.cfg.cache_repo_ttl; | 35 | item->ttl = ctx.cfg.cache_repo_ttl; |
33 | } else { | 36 | } else { |
34 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, | 37 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, |
35 | cache_safe_filename(ctx.repo->url), | 38 | cache_safe_filename(ctx.repo->url), |
36 | ctx.qry.page, | 39 | ctx.qry.page, |
37 | cache_safe_filename(ctx.qry.raw))); | 40 | cache_safe_filename(ctx.qry.raw))); |
38 | if (ctx.qry.has_symref) | 41 | if (ctx.qry.has_symref) |
39 | item->ttl = ctx.cfg.cache_dynamic_ttl; | 42 | item->ttl = ctx.cfg.cache_dynamic_ttl; |
40 | else if (ctx.qry.has_sha1) | 43 | else if (ctx.qry.has_sha1) |
41 | item->ttl = ctx.cfg.cache_static_ttl; | 44 | item->ttl = ctx.cfg.cache_static_ttl; |
42 | else | 45 | else |
43 | item->ttl = ctx.cfg.cache_repo_ttl; | 46 | item->ttl = ctx.cfg.cache_repo_ttl; |
44 | } | 47 | } |
45 | return 1; | 48 | return 1; |
46 | } | 49 | } |
47 | 50 | ||
48 | struct refmatch { | 51 | struct refmatch { |
49 | char *req_ref; | 52 | char *req_ref; |
50 | char *first_ref; | 53 | char *first_ref; |
51 | int match; | 54 | int match; |
52 | }; | 55 | }; |
53 | 56 | ||
54 | int find_current_ref(const char *refname, const unsigned char *sha1, | 57 | int find_current_ref(const char *refname, const unsigned char *sha1, |
55 | int flags, void *cb_data) | 58 | int flags, void *cb_data) |
56 | { | 59 | { |
57 | struct refmatch *info; | 60 | struct refmatch *info; |
58 | 61 | ||
59 | info = (struct refmatch *)cb_data; | 62 | info = (struct refmatch *)cb_data; |
60 | if (!strcmp(refname, info->req_ref)) | 63 | if (!strcmp(refname, info->req_ref)) |
61 | info->match = 1; | 64 | info->match = 1; |
62 | if (!info->first_ref) | 65 | if (!info->first_ref) |
63 | info->first_ref = xstrdup(refname); | 66 | info->first_ref = xstrdup(refname); |
64 | return info->match; | 67 | return info->match; |
65 | } | 68 | } |
66 | 69 | ||
67 | char *find_default_branch(struct cgit_repo *repo) | 70 | char *find_default_branch(struct cgit_repo *repo) |
68 | { | 71 | { |
69 | struct refmatch info; | 72 | struct refmatch info; |
70 | 73 | ||
71 | info.req_ref = repo->defbranch; | 74 | info.req_ref = repo->defbranch; |
72 | info.first_ref = NULL; | 75 | info.first_ref = NULL; |
73 | info.match = 0; | 76 | info.match = 0; |
74 | for_each_branch_ref(find_current_ref, &info); | 77 | for_each_branch_ref(find_current_ref, &info); |
75 | if (info.match) | 78 | if (info.match) |
76 | return info.req_ref; | 79 | return info.req_ref; |
77 | else | 80 | else |
78 | return info.first_ref; | 81 | return info.first_ref; |
79 | } | 82 | } |
80 | 83 | ||
81 | static void cgit_print_repo_page(struct cacheitem *item) | 84 | static void cgit_print_repo_page(struct cacheitem *item) |
82 | { | 85 | { |
83 | char *title, *tmp; | 86 | char *tmp; |
84 | int show_search; | 87 | int show_search; |
85 | unsigned char sha1[20]; | 88 | unsigned char sha1[20]; |
86 | int nongit = 0; | 89 | int nongit = 0; |
87 | 90 | ||
88 | setenv("GIT_DIR", ctx.repo->path, 1); | 91 | setenv("GIT_DIR", ctx.repo->path, 1); |
89 | setup_git_directory_gently(&nongit); | 92 | setup_git_directory_gently(&nongit); |
90 | if (nongit) { | 93 | if (nongit) { |
91 | title = fmt("%s - %s", ctx.cfg.root_title, "config error"); | 94 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, |
95 | "config error"); | ||
92 | tmp = fmt("Not a git repository: '%s'", ctx.repo->path); | 96 | tmp = fmt("Not a git repository: '%s'", ctx.repo->path); |
93 | ctx.repo = NULL; | 97 | ctx.repo = NULL; |
94 | cgit_print_docstart(title, item); | 98 | cgit_print_http_headers(&ctx); |
95 | cgit_print_pageheader(title, 0); | 99 | cgit_print_docstart(&ctx); |
100 | cgit_print_pageheader(&ctx); | ||
96 | cgit_print_error(tmp); | 101 | cgit_print_error(tmp); |
97 | cgit_print_docend(); | 102 | cgit_print_docend(); |
98 | return; | 103 | return; |
99 | } | 104 | } |
100 | 105 | ||
101 | title = fmt("%s - %s", ctx.repo->name, ctx.repo->desc); | 106 | ctx.page.title = fmt("%s - %s", ctx.repo->name, ctx.repo->desc); |
102 | show_search = 0; | 107 | show_search = 0; |
103 | 108 | ||
104 | if (!ctx.qry.head) { | 109 | if (!ctx.qry.head) { |
105 | ctx.qry.head = xstrdup(find_default_branch(ctx.repo)); | 110 | ctx.qry.head = xstrdup(find_default_branch(ctx.repo)); |
106 | ctx.repo->defbranch = ctx.qry.head; | 111 | ctx.repo->defbranch = ctx.qry.head; |
107 | } | 112 | } |
108 | 113 | ||
109 | if (!ctx.qry.head) { | 114 | if (!ctx.qry.head) { |
110 | cgit_print_docstart(title, item); | 115 | cgit_print_http_headers(&ctx); |
111 | cgit_print_pageheader(title, 0); | 116 | cgit_print_docstart(&ctx); |
117 | cgit_print_pageheader(&ctx); | ||
112 | cgit_print_error("Repository seems to be empty"); | 118 | cgit_print_error("Repository seems to be empty"); |
113 | cgit_print_docend(); | 119 | cgit_print_docend(); |
114 | return; | 120 | return; |
115 | } | 121 | } |
116 | 122 | ||
117 | if (get_sha1(ctx.qry.head, sha1)) { | 123 | if (get_sha1(ctx.qry.head, sha1)) { |
118 | tmp = xstrdup(ctx.qry.head); | 124 | tmp = xstrdup(ctx.qry.head); |
119 | ctx.qry.head = ctx.repo->defbranch; | 125 | ctx.qry.head = ctx.repo->defbranch; |
120 | cgit_print_docstart(title, item); | 126 | cgit_print_http_headers(&ctx); |
121 | cgit_print_pageheader(title, 0); | 127 | cgit_print_docstart(&ctx); |
128 | cgit_print_pageheader(&ctx); | ||
122 | cgit_print_error(fmt("Invalid branch: %s", tmp)); | 129 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
123 | cgit_print_docend(); | 130 | cgit_print_docend(); |
124 | return; | 131 | return; |
125 | } | 132 | } |
126 | 133 | ||
127 | if ((cgit_cmd == CMD_SNAPSHOT) && ctx.repo->snapshots) { | 134 | if ((cgit_cmd == CMD_SNAPSHOT) && ctx.repo->snapshots) { |
128 | cgit_print_snapshot(item, ctx.qry.head, ctx.qry.sha1, | 135 | cgit_print_snapshot(item, ctx.qry.head, ctx.qry.sha1, |
129 | cgit_repobasename(ctx.repo->url), | 136 | cgit_repobasename(ctx.repo->url), |
130 | ctx.qry.path, | 137 | ctx.qry.path, |
131 | ctx.repo->snapshots ); | 138 | ctx.repo->snapshots ); |
132 | return; | 139 | return; |
133 | } | 140 | } |
134 | 141 | ||
135 | if (cgit_cmd == CMD_PATCH) { | 142 | if (cgit_cmd == CMD_PATCH) { |
136 | cgit_print_patch(ctx.qry.sha1, item); | 143 | cgit_print_patch(ctx.qry.sha1, item); |
137 | return; | 144 | return; |
138 | } | 145 | } |
139 | 146 | ||
140 | if (cgit_cmd == CMD_BLOB) { | 147 | if (cgit_cmd == CMD_BLOB) { |
141 | cgit_print_blob(item, ctx.qry.sha1, ctx.qry.path); | 148 | cgit_print_blob(item, ctx.qry.sha1, ctx.qry.path); |
142 | return; | 149 | return; |
143 | } | 150 | } |
144 | 151 | ||
145 | show_search = (cgit_cmd == CMD_LOG); | 152 | show_search = (cgit_cmd == CMD_LOG); |
146 | cgit_print_docstart(title, item); | 153 | cgit_print_http_headers(&ctx); |
154 | cgit_print_docstart(&ctx); | ||
147 | if (!cgit_cmd) { | 155 | if (!cgit_cmd) { |
148 | cgit_print_pageheader("summary", show_search); | 156 | cgit_print_pageheader(&ctx); |
149 | cgit_print_summary(); | 157 | cgit_print_summary(); |
150 | cgit_print_docend(); | 158 | cgit_print_docend(); |
151 | return; | 159 | return; |
152 | } | 160 | } |
153 | 161 | ||
154 | cgit_print_pageheader(ctx.qry.page, show_search); | 162 | cgit_print_pageheader(&ctx); |
155 | 163 | ||
156 | switch(cgit_cmd) { | 164 | switch(cgit_cmd) { |
157 | case CMD_LOG: | 165 | case CMD_LOG: |
158 | cgit_print_log(ctx.qry.sha1, ctx.qry.ofs, | 166 | cgit_print_log(ctx.qry.sha1, ctx.qry.ofs, |
159 | ctx.cfg.max_commit_count, ctx.qry.grep, ctx.qry.search, | 167 | ctx.cfg.max_commit_count, ctx.qry.grep, ctx.qry.search, |
160 | ctx.qry.path, 1); | 168 | ctx.qry.path, 1); |
161 | break; | 169 | break; |
162 | case CMD_TREE: | 170 | case CMD_TREE: |
163 | cgit_print_tree(ctx.qry.sha1, ctx.qry.path); | 171 | cgit_print_tree(ctx.qry.sha1, ctx.qry.path); |
164 | break; | 172 | break; |
165 | case CMD_COMMIT: | 173 | case CMD_COMMIT: |
166 | cgit_print_commit(ctx.qry.sha1); | 174 | cgit_print_commit(ctx.qry.sha1); |
167 | break; | 175 | break; |
168 | case CMD_REFS: | 176 | case CMD_REFS: |
169 | cgit_print_refs(); | 177 | cgit_print_refs(); |
170 | break; | 178 | break; |
171 | case CMD_TAG: | 179 | case CMD_TAG: |
172 | cgit_print_tag(ctx.qry.sha1); | 180 | cgit_print_tag(ctx.qry.sha1); |
173 | break; | 181 | break; |
174 | case CMD_DIFF: | 182 | case CMD_DIFF: |
175 | cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path); | 183 | cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path); |
176 | break; | 184 | break; |
177 | default: | 185 | default: |
178 | cgit_print_error("Invalid request"); | 186 | cgit_print_error("Invalid request"); |
179 | } | 187 | } |
180 | cgit_print_docend(); | 188 | cgit_print_docend(); |
181 | } | 189 | } |
182 | 190 | ||
191 | static long ttl_seconds(long ttl) | ||
192 | { | ||
193 | if (ttl<0) | ||
194 | return 60 * 60 * 24 * 365; | ||
195 | else | ||
196 | return ttl * 60; | ||
197 | } | ||
198 | |||
183 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) | 199 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) |
184 | { | 200 | { |
185 | int stdout2; | 201 | int stdout2; |
186 | 202 | ||
187 | item->st.st_mtime = time(NULL); | ||
188 | |||
189 | if (use_cache) { | 203 | if (use_cache) { |
190 | stdout2 = chk_positive(dup(STDOUT_FILENO), | 204 | stdout2 = chk_positive(dup(STDOUT_FILENO), |
191 | "Preserving STDOUT"); | 205 | "Preserving STDOUT"); |
192 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); | 206 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); |
193 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); | 207 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); |
194 | } | 208 | } |
195 | 209 | ||
210 | ctx.page.modified = time(NULL); | ||
211 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); | ||
196 | if (ctx.repo) | 212 | if (ctx.repo) |
197 | cgit_print_repo_page(item); | 213 | cgit_print_repo_page(item); |
198 | else | 214 | else |
199 | cgit_print_repolist(item); | 215 | cgit_print_repolist(item); |
200 | 216 | ||
201 | if (use_cache) { | 217 | if (use_cache) { |
202 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); | 218 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); |
203 | chk_positive(dup2(stdout2, STDOUT_FILENO), | 219 | chk_positive(dup2(stdout2, STDOUT_FILENO), |
204 | "Restoring original STDOUT"); | 220 | "Restoring original STDOUT"); |
205 | chk_zero(close(stdout2), "Closing temporary STDOUT"); | 221 | chk_zero(close(stdout2), "Closing temporary STDOUT"); |
206 | } | 222 | } |
207 | } | 223 | } |
208 | 224 | ||
209 | static void cgit_check_cache(struct cacheitem *item) | 225 | static void cgit_check_cache(struct cacheitem *item) |
210 | { | 226 | { |
211 | int i = 0; | 227 | int i = 0; |
212 | 228 | ||
213 | top: | 229 | top: |
214 | if (++i > ctx.cfg.max_lock_attempts) { | 230 | if (++i > ctx.cfg.max_lock_attempts) { |
215 | die("cgit_refresh_cache: unable to lock %s: %s", | 231 | die("cgit_refresh_cache: unable to lock %s: %s", |
216 | item->name, strerror(errno)); | 232 | item->name, strerror(errno)); |
217 | } | 233 | } |
218 | if (!cache_exist(item)) { | 234 | if (!cache_exist(item)) { |
219 | if (!cache_lock(item)) { | 235 | if (!cache_lock(item)) { |
220 | sleep(1); | 236 | sleep(1); |
221 | goto top; | 237 | goto top; |
222 | } | 238 | } |
223 | if (!cache_exist(item)) { | 239 | if (!cache_exist(item)) { |
224 | cgit_fill_cache(item, 1); | 240 | cgit_fill_cache(item, 1); |
225 | cache_unlock(item); | 241 | cache_unlock(item); |
226 | } else { | 242 | } else { |
227 | cache_cancel_lock(item); | 243 | cache_cancel_lock(item); |
228 | } | 244 | } |
229 | } else if (cache_expired(item) && cache_lock(item)) { | 245 | } else if (cache_expired(item) && cache_lock(item)) { |
230 | if (cache_expired(item)) { | 246 | if (cache_expired(item)) { |
231 | cgit_fill_cache(item, 1); | 247 | cgit_fill_cache(item, 1); |
232 | cache_unlock(item); | 248 | cache_unlock(item); |
233 | } else { | 249 | } else { |
234 | cache_cancel_lock(item); | 250 | cache_cancel_lock(item); |
235 | } | 251 | } |
236 | } | 252 | } |
237 | } | 253 | } |
238 | 254 | ||
239 | static void cgit_print_cache(struct cacheitem *item) | 255 | static void cgit_print_cache(struct cacheitem *item) |
240 | { | 256 | { |
241 | static char buf[4096]; | 257 | static char buf[4096]; |
242 | ssize_t i; | 258 | ssize_t i; |
243 | 259 | ||
@@ -129,163 +129,171 @@ struct cgit_query { | |||
129 | char *raw; | 129 | char *raw; |
130 | char *repo; | 130 | char *repo; |
131 | char *page; | 131 | char *page; |
132 | char *search; | 132 | char *search; |
133 | char *grep; | 133 | char *grep; |
134 | char *head; | 134 | char *head; |
135 | char *sha1; | 135 | char *sha1; |
136 | char *sha2; | 136 | char *sha2; |
137 | char *path; | 137 | char *path; |
138 | char *name; | 138 | char *name; |
139 | int ofs; | 139 | int ofs; |
140 | }; | 140 | }; |
141 | 141 | ||
142 | struct cgit_config { | 142 | struct cgit_config { |
143 | char *agefile; | 143 | char *agefile; |
144 | char *cache_root; | 144 | char *cache_root; |
145 | char *clone_prefix; | 145 | char *clone_prefix; |
146 | char *css; | 146 | char *css; |
147 | char *index_header; | 147 | char *index_header; |
148 | char *index_info; | 148 | char *index_info; |
149 | char *logo; | 149 | char *logo; |
150 | char *logo_link; | 150 | char *logo_link; |
151 | char *module_link; | 151 | char *module_link; |
152 | char *repo_group; | 152 | char *repo_group; |
153 | char *robots; | 153 | char *robots; |
154 | char *root_title; | 154 | char *root_title; |
155 | char *script_name; | 155 | char *script_name; |
156 | char *virtual_root; | 156 | char *virtual_root; |
157 | int cache_dynamic_ttl; | 157 | int cache_dynamic_ttl; |
158 | int cache_max_create_time; | 158 | int cache_max_create_time; |
159 | int cache_repo_ttl; | 159 | int cache_repo_ttl; |
160 | int cache_root_ttl; | 160 | int cache_root_ttl; |
161 | int cache_static_ttl; | 161 | int cache_static_ttl; |
162 | int enable_index_links; | 162 | int enable_index_links; |
163 | int enable_log_filecount; | 163 | int enable_log_filecount; |
164 | int enable_log_linecount; | 164 | int enable_log_linecount; |
165 | int max_commit_count; | 165 | int max_commit_count; |
166 | int max_lock_attempts; | 166 | int max_lock_attempts; |
167 | int max_msg_len; | 167 | int max_msg_len; |
168 | int max_repodesc_len; | 168 | int max_repodesc_len; |
169 | int nocache; | 169 | int nocache; |
170 | int renamelimit; | 170 | int renamelimit; |
171 | int snapshots; | 171 | int snapshots; |
172 | int summary_branches; | 172 | int summary_branches; |
173 | int summary_log; | 173 | int summary_log; |
174 | int summary_tags; | 174 | int summary_tags; |
175 | }; | 175 | }; |
176 | 176 | ||
177 | struct cgit_page { | ||
178 | time_t modified; | ||
179 | time_t expires; | ||
180 | char *mimetype; | ||
181 | char *charset; | ||
182 | char *filename; | ||
183 | char *title; | ||
184 | }; | ||
185 | |||
177 | struct cgit_context { | 186 | struct cgit_context { |
178 | struct cgit_query qry; | 187 | struct cgit_query qry; |
179 | struct cgit_config cfg; | 188 | struct cgit_config cfg; |
180 | struct cgit_repo *repo; | 189 | struct cgit_repo *repo; |
190 | struct cgit_page page; | ||
181 | }; | 191 | }; |
182 | 192 | ||
183 | extern const char *cgit_version; | 193 | extern const char *cgit_version; |
184 | 194 | ||
185 | extern struct cgit_repolist cgit_repolist; | 195 | extern struct cgit_repolist cgit_repolist; |
186 | extern struct cgit_context ctx; | 196 | extern struct cgit_context ctx; |
187 | extern int cgit_cmd; | 197 | extern int cgit_cmd; |
188 | 198 | ||
189 | extern void cgit_prepare_context(struct cgit_context *ctx); | 199 | extern void cgit_prepare_context(struct cgit_context *ctx); |
190 | extern int cgit_get_cmd_index(const char *cmd); | 200 | extern int cgit_get_cmd_index(const char *cmd); |
191 | extern struct cgit_repo *cgit_get_repoinfo(const char *url); | 201 | extern struct cgit_repo *cgit_get_repoinfo(const char *url); |
192 | extern void cgit_global_config_cb(const char *name, const char *value); | 202 | extern void cgit_global_config_cb(const char *name, const char *value); |
193 | extern void cgit_repo_config_cb(const char *name, const char *value); | 203 | extern void cgit_repo_config_cb(const char *name, const char *value); |
194 | extern void cgit_querystring_cb(const char *name, const char *value); | 204 | extern void cgit_querystring_cb(const char *name, const char *value); |
195 | 205 | ||
196 | extern int chk_zero(int result, char *msg); | 206 | extern int chk_zero(int result, char *msg); |
197 | extern int chk_positive(int result, char *msg); | 207 | extern int chk_positive(int result, char *msg); |
198 | extern int chk_non_negative(int result, char *msg); | 208 | extern int chk_non_negative(int result, char *msg); |
199 | 209 | ||
200 | extern int hextoint(char c); | 210 | extern int hextoint(char c); |
201 | extern char *trim_end(const char *str, char c); | 211 | extern char *trim_end(const char *str, char c); |
202 | extern char *strlpart(char *txt, int maxlen); | 212 | extern char *strlpart(char *txt, int maxlen); |
203 | extern char *strrpart(char *txt, int maxlen); | 213 | extern char *strrpart(char *txt, int maxlen); |
204 | 214 | ||
205 | extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); | 215 | extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); |
206 | extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, | 216 | extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, |
207 | int flags, void *cb_data); | 217 | int flags, void *cb_data); |
208 | 218 | ||
209 | extern void *cgit_free_commitinfo(struct commitinfo *info); | 219 | extern void *cgit_free_commitinfo(struct commitinfo *info); |
210 | 220 | ||
211 | extern int cgit_diff_files(const unsigned char *old_sha1, | 221 | extern int cgit_diff_files(const unsigned char *old_sha1, |
212 | const unsigned char *new_sha1, | 222 | const unsigned char *new_sha1, |
213 | linediff_fn fn); | 223 | linediff_fn fn); |
214 | 224 | ||
215 | extern void cgit_diff_tree(const unsigned char *old_sha1, | 225 | extern void cgit_diff_tree(const unsigned char *old_sha1, |
216 | const unsigned char *new_sha1, | 226 | const unsigned char *new_sha1, |
217 | filepair_fn fn, const char *prefix); | 227 | filepair_fn fn, const char *prefix); |
218 | 228 | ||
219 | extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); | 229 | extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); |
220 | 230 | ||
221 | extern char *fmt(const char *format,...); | 231 | extern char *fmt(const char *format,...); |
222 | 232 | ||
223 | extern int cgit_read_config(const char *filename, configfn fn); | 233 | extern int cgit_read_config(const char *filename, configfn fn); |
224 | extern int cgit_parse_query(char *txt, configfn fn); | 234 | extern int cgit_parse_query(char *txt, configfn fn); |
225 | extern struct commitinfo *cgit_parse_commit(struct commit *commit); | 235 | extern struct commitinfo *cgit_parse_commit(struct commit *commit); |
226 | extern struct taginfo *cgit_parse_tag(struct tag *tag); | 236 | extern struct taginfo *cgit_parse_tag(struct tag *tag); |
227 | extern void cgit_parse_url(const char *url); | 237 | extern void cgit_parse_url(const char *url); |
228 | 238 | ||
229 | extern char *cache_safe_filename(const char *unsafe); | 239 | extern char *cache_safe_filename(const char *unsafe); |
230 | extern int cache_lock(struct cacheitem *item); | 240 | extern int cache_lock(struct cacheitem *item); |
231 | extern int cache_unlock(struct cacheitem *item); | 241 | extern int cache_unlock(struct cacheitem *item); |
232 | extern int cache_cancel_lock(struct cacheitem *item); | 242 | extern int cache_cancel_lock(struct cacheitem *item); |
233 | extern int cache_exist(struct cacheitem *item); | 243 | extern int cache_exist(struct cacheitem *item); |
234 | extern int cache_expired(struct cacheitem *item); | 244 | extern int cache_expired(struct cacheitem *item); |
235 | 245 | ||
236 | extern char *cgit_repourl(const char *reponame); | 246 | extern char *cgit_repourl(const char *reponame); |
237 | extern char *cgit_fileurl(const char *reponame, const char *pagename, | 247 | extern char *cgit_fileurl(const char *reponame, const char *pagename, |
238 | const char *filename, const char *query); | 248 | const char *filename, const char *query); |
239 | extern char *cgit_pageurl(const char *reponame, const char *pagename, | 249 | extern char *cgit_pageurl(const char *reponame, const char *pagename, |
240 | const char *query); | 250 | const char *query); |
241 | 251 | ||
242 | extern const char *cgit_repobasename(const char *reponame); | 252 | extern const char *cgit_repobasename(const char *reponame); |
243 | 253 | ||
244 | extern void cgit_tree_link(char *name, char *title, char *class, char *head, | 254 | extern void cgit_tree_link(char *name, char *title, char *class, char *head, |
245 | char *rev, char *path); | 255 | char *rev, char *path); |
246 | extern void cgit_log_link(char *name, char *title, char *class, char *head, | 256 | extern void cgit_log_link(char *name, char *title, char *class, char *head, |
247 | char *rev, char *path, int ofs, char *grep, | 257 | char *rev, char *path, int ofs, char *grep, |
248 | char *pattern); | 258 | char *pattern); |
249 | extern void cgit_commit_link(char *name, char *title, char *class, char *head, | 259 | extern void cgit_commit_link(char *name, char *title, char *class, char *head, |
250 | char *rev); | 260 | char *rev); |
251 | extern void cgit_refs_link(char *name, char *title, char *class, char *head, | 261 | extern void cgit_refs_link(char *name, char *title, char *class, char *head, |
252 | char *rev, char *path); | 262 | char *rev, char *path); |
253 | extern void cgit_snapshot_link(char *name, char *title, char *class, | 263 | extern void cgit_snapshot_link(char *name, char *title, char *class, |
254 | char *head, char *rev, char *archivename); | 264 | char *head, char *rev, char *archivename); |
255 | extern void cgit_diff_link(char *name, char *title, char *class, char *head, | 265 | extern void cgit_diff_link(char *name, char *title, char *class, char *head, |
256 | char *new_rev, char *old_rev, char *path); | 266 | char *new_rev, char *old_rev, char *path); |
257 | 267 | ||
258 | extern void cgit_object_link(struct object *obj); | 268 | extern void cgit_object_link(struct object *obj); |
259 | 269 | ||
260 | extern void cgit_print_error(char *msg); | 270 | extern void cgit_print_error(char *msg); |
261 | extern void cgit_print_date(time_t secs, char *format); | 271 | extern void cgit_print_date(time_t secs, char *format); |
262 | extern void cgit_print_age(time_t t, time_t max_relative, char *format); | 272 | extern void cgit_print_age(time_t t, time_t max_relative, char *format); |
263 | extern void cgit_print_docstart(char *title, struct cacheitem *item); | 273 | extern void cgit_print_http_headers(struct cgit_context *ctx); |
274 | extern void cgit_print_docstart(struct cgit_context *ctx); | ||
264 | extern void cgit_print_docend(); | 275 | extern void cgit_print_docend(); |
265 | extern void cgit_print_pageheader(char *title, int show_search); | 276 | extern void cgit_print_pageheader(struct cgit_context *ctx); |
266 | extern void cgit_print_snapshot_start(const char *mimetype, | ||
267 | const char *filename, | ||
268 | struct cacheitem *item); | ||
269 | extern void cgit_print_filemode(unsigned short mode); | 277 | extern void cgit_print_filemode(unsigned short mode); |
270 | extern void cgit_print_branches(int maxcount); | 278 | extern void cgit_print_branches(int maxcount); |
271 | extern void cgit_print_tags(int maxcount); | 279 | extern void cgit_print_tags(int maxcount); |
272 | 280 | ||
273 | extern void cgit_print_repolist(struct cacheitem *item); | 281 | extern void cgit_print_repolist(struct cacheitem *item); |
274 | extern void cgit_print_summary(); | 282 | extern void cgit_print_summary(); |
275 | extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, | 283 | extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, |
276 | char *pattern, char *path, int pager); | 284 | char *pattern, char *path, int pager); |
277 | extern void cgit_print_blob(struct cacheitem *item, const char *hex, char *path); | 285 | extern void cgit_print_blob(struct cacheitem *item, const char *hex, char *path); |
278 | extern void cgit_print_tree(const char *rev, char *path); | 286 | extern void cgit_print_tree(const char *rev, char *path); |
279 | extern void cgit_print_commit(char *hex); | 287 | extern void cgit_print_commit(char *hex); |
280 | extern void cgit_print_refs(); | 288 | extern void cgit_print_refs(); |
281 | extern void cgit_print_tag(char *revname); | 289 | extern void cgit_print_tag(char *revname); |
282 | extern void cgit_print_diff(const char *new_hex, const char *old_hex, const char *prefix); | 290 | extern void cgit_print_diff(const char *new_hex, const char *old_hex, const char *prefix); |
283 | extern void cgit_print_patch(char *hex, struct cacheitem *item); | 291 | extern void cgit_print_patch(char *hex, struct cacheitem *item); |
284 | extern void cgit_print_snapshot(struct cacheitem *item, const char *head, | 292 | extern void cgit_print_snapshot(struct cacheitem *item, const char *head, |
285 | const char *hex, const char *prefix, | 293 | const char *hex, const char *prefix, |
286 | const char *filename, int snapshot); | 294 | const char *filename, int snapshot); |
287 | extern void cgit_print_snapshot_links(const char *repo, const char *head, | 295 | extern void cgit_print_snapshot_links(const char *repo, const char *head, |
288 | const char *hex, int snapshots); | 296 | const char *hex, int snapshots); |
289 | extern int cgit_parse_snapshots_mask(const char *str); | 297 | extern int cgit_parse_snapshots_mask(const char *str); |
290 | 298 | ||
291 | #endif /* CGIT_H */ | 299 | #endif /* CGIT_H */ |
@@ -1,85 +1,88 @@ | |||
1 | /* shared.c: global vars + some callback functions | 1 | /* shared.c: global vars + some callback functions |
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 | 10 | ||
11 | struct cgit_repolist cgit_repolist; | 11 | struct cgit_repolist cgit_repolist; |
12 | struct cgit_context ctx; | 12 | struct cgit_context ctx; |
13 | int cgit_cmd; | 13 | int cgit_cmd; |
14 | 14 | ||
15 | const char *cgit_version = CGIT_VERSION; | 15 | const char *cgit_version = CGIT_VERSION; |
16 | 16 | ||
17 | void cgit_prepare_context(struct cgit_context *ctx) | 17 | void cgit_prepare_context(struct cgit_context *ctx) |
18 | { | 18 | { |
19 | memset(ctx, 0, sizeof(ctx)); | 19 | memset(ctx, 0, sizeof(ctx)); |
20 | ctx->cfg.agefile = "info/web/last-modified"; | 20 | ctx->cfg.agefile = "info/web/last-modified"; |
21 | ctx->cfg.cache_dynamic_ttl = 5; | 21 | ctx->cfg.cache_dynamic_ttl = 5; |
22 | ctx->cfg.cache_max_create_time = 5; | 22 | ctx->cfg.cache_max_create_time = 5; |
23 | ctx->cfg.cache_repo_ttl = 5; | 23 | ctx->cfg.cache_repo_ttl = 5; |
24 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; | 24 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; |
25 | ctx->cfg.cache_root_ttl = 5; | 25 | ctx->cfg.cache_root_ttl = 5; |
26 | ctx->cfg.cache_static_ttl = -1; | 26 | ctx->cfg.cache_static_ttl = -1; |
27 | ctx->cfg.css = "/cgit.css"; | 27 | ctx->cfg.css = "/cgit.css"; |
28 | ctx->cfg.logo = "/git-logo.png"; | 28 | ctx->cfg.logo = "/git-logo.png"; |
29 | ctx->cfg.max_commit_count = 50; | 29 | ctx->cfg.max_commit_count = 50; |
30 | ctx->cfg.max_lock_attempts = 5; | 30 | ctx->cfg.max_lock_attempts = 5; |
31 | ctx->cfg.max_msg_len = 60; | 31 | ctx->cfg.max_msg_len = 60; |
32 | ctx->cfg.max_repodesc_len = 60; | 32 | ctx->cfg.max_repodesc_len = 60; |
33 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; | 33 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; |
34 | ctx->cfg.renamelimit = -1; | 34 | ctx->cfg.renamelimit = -1; |
35 | ctx->cfg.robots = "index, nofollow"; | 35 | ctx->cfg.robots = "index, nofollow"; |
36 | ctx->cfg.root_title = "Git repository browser"; | 36 | ctx->cfg.root_title = "Git repository browser"; |
37 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; | 37 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; |
38 | ctx->page.mimetype = "text/html"; | ||
39 | ctx->page.charset = PAGE_ENCODING; | ||
40 | ctx->page.filename = NULL; | ||
38 | } | 41 | } |
39 | 42 | ||
40 | int cgit_get_cmd_index(const char *cmd) | 43 | int cgit_get_cmd_index(const char *cmd) |
41 | { | 44 | { |
42 | static char *cmds[] = {"log", "commit", "diff", "tree", "blob", | 45 | static char *cmds[] = {"log", "commit", "diff", "tree", "blob", |
43 | "snapshot", "tag", "refs", "patch", NULL}; | 46 | "snapshot", "tag", "refs", "patch", NULL}; |
44 | int i; | 47 | int i; |
45 | 48 | ||
46 | for(i = 0; cmds[i]; i++) | 49 | for(i = 0; cmds[i]; i++) |
47 | if (!strcmp(cmd, cmds[i])) | 50 | if (!strcmp(cmd, cmds[i])) |
48 | return i + 1; | 51 | return i + 1; |
49 | return 0; | 52 | return 0; |
50 | } | 53 | } |
51 | 54 | ||
52 | int chk_zero(int result, char *msg) | 55 | int chk_zero(int result, char *msg) |
53 | { | 56 | { |
54 | if (result != 0) | 57 | if (result != 0) |
55 | die("%s: %s", msg, strerror(errno)); | 58 | die("%s: %s", msg, strerror(errno)); |
56 | return result; | 59 | return result; |
57 | } | 60 | } |
58 | 61 | ||
59 | int chk_positive(int result, char *msg) | 62 | int chk_positive(int result, char *msg) |
60 | { | 63 | { |
61 | if (result <= 0) | 64 | if (result <= 0) |
62 | die("%s: %s", msg, strerror(errno)); | 65 | die("%s: %s", msg, strerror(errno)); |
63 | return result; | 66 | return result; |
64 | } | 67 | } |
65 | 68 | ||
66 | int chk_non_negative(int result, char *msg) | 69 | int chk_non_negative(int result, char *msg) |
67 | { | 70 | { |
68 | if (result < 0) | 71 | if (result < 0) |
69 | die("%s: %s",msg, strerror(errno)); | 72 | die("%s: %s",msg, strerror(errno)); |
70 | return result; | 73 | return result; |
71 | } | 74 | } |
72 | 75 | ||
73 | struct cgit_repo *add_repo(const char *url) | 76 | struct cgit_repo *add_repo(const char *url) |
74 | { | 77 | { |
75 | struct cgit_repo *ret; | 78 | struct cgit_repo *ret; |
76 | 79 | ||
77 | if (++cgit_repolist.count > cgit_repolist.length) { | 80 | if (++cgit_repolist.count > cgit_repolist.length) { |
78 | if (cgit_repolist.length == 0) | 81 | if (cgit_repolist.length == 0) |
79 | cgit_repolist.length = 8; | 82 | cgit_repolist.length = 8; |
80 | else | 83 | else |
81 | cgit_repolist.length *= 2; | 84 | cgit_repolist.length *= 2; |
82 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, | 85 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, |
83 | cgit_repolist.length * | 86 | cgit_repolist.length * |
84 | sizeof(struct cgit_repo)); | 87 | sizeof(struct cgit_repo)); |
85 | } | 88 | } |
@@ -1,40 +1,42 @@ | |||
1 | /* ui-blob.c: show blob content | 1 | /* ui-blob.c: show blob content |
2 | * | 2 | * |
3 | * Copyright (C) 2008 Lars Hjemli | 3 | * Copyright (C) 2008 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 | 11 | ||
12 | void cgit_print_blob(struct cacheitem *item, const char *hex, char *path) | 12 | void cgit_print_blob(struct cacheitem *item, const char *hex, char *path) |
13 | { | 13 | { |
14 | 14 | ||
15 | unsigned char sha1[20]; | 15 | unsigned char sha1[20]; |
16 | enum object_type type; | 16 | enum object_type type; |
17 | unsigned char *buf; | 17 | unsigned char *buf; |
18 | unsigned long size; | 18 | unsigned long size; |
19 | 19 | ||
20 | if (get_sha1_hex(hex, sha1)){ | 20 | if (get_sha1_hex(hex, sha1)){ |
21 | cgit_print_error(fmt("Bad hex value: %s", hex)); | 21 | cgit_print_error(fmt("Bad hex value: %s", hex)); |
22 | return; | 22 | return; |
23 | } | 23 | } |
24 | 24 | ||
25 | type = sha1_object_info(sha1, &size); | 25 | type = sha1_object_info(sha1, &size); |
26 | if (type == OBJ_BAD) { | 26 | if (type == OBJ_BAD) { |
27 | cgit_print_error(fmt("Bad object name: %s", hex)); | 27 | cgit_print_error(fmt("Bad object name: %s", hex)); |
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", hex)); | 33 | cgit_print_error(fmt("Error reading object %s", hex)); |
34 | return; | 34 | return; |
35 | } | 35 | } |
36 | 36 | ||
37 | buf[size] = '\0'; | 37 | buf[size] = '\0'; |
38 | cgit_print_snapshot_start("text/plain", path, item); | 38 | ctx.page.mimetype = "text/plain"; |
39 | ctx.page.filename = path; | ||
40 | cgit_print_http_headers(&ctx); | ||
39 | write(htmlfd, buf, size); | 41 | write(htmlfd, buf, size); |
40 | } | 42 | } |
@@ -47,65 +47,67 @@ static void header(unsigned char *sha1, char *path1, int mode1, | |||
47 | if (mode1 != 0 && mode2 != 0) { | 47 | if (mode1 != 0 && mode2 != 0) { |
48 | htmlf(" %.6o", mode1); | 48 | htmlf(" %.6o", mode1); |
49 | if (mode2 != mode1) | 49 | if (mode2 != mode1) |
50 | htmlf("..%.6o", mode2); | 50 | htmlf("..%.6o", mode2); |
51 | } | 51 | } |
52 | htmlf("\n--- a/%s\n", path1); | 52 | htmlf("\n--- a/%s\n", path1); |
53 | htmlf("+++ b/%s\n", path2); | 53 | htmlf("+++ b/%s\n", path2); |
54 | } | 54 | } |
55 | } | 55 | } |
56 | 56 | ||
57 | static void filepair_cb(struct diff_filepair *pair) | 57 | static void filepair_cb(struct diff_filepair *pair) |
58 | { | 58 | { |
59 | header(pair->one->sha1, pair->one->path, pair->one->mode, | 59 | header(pair->one->sha1, pair->one->path, pair->one->mode, |
60 | pair->two->sha1, pair->two->path, pair->two->mode); | 60 | pair->two->sha1, pair->two->path, pair->two->mode); |
61 | if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { | 61 | if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { |
62 | if (S_ISGITLINK(pair->one->mode)) | 62 | if (S_ISGITLINK(pair->one->mode)) |
63 | print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52); | 63 | print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52); |
64 | if (S_ISGITLINK(pair->two->mode)) | 64 | if (S_ISGITLINK(pair->two->mode)) |
65 | print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52); | 65 | print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52); |
66 | return; | 66 | return; |
67 | } | 67 | } |
68 | if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line)) | 68 | if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line)) |
69 | html("Error running diff"); | 69 | html("Error running diff"); |
70 | } | 70 | } |
71 | 71 | ||
72 | void cgit_print_patch(char *hex, struct cacheitem *item) | 72 | void cgit_print_patch(char *hex, struct cacheitem *item) |
73 | { | 73 | { |
74 | struct commit *commit; | 74 | struct commit *commit; |
75 | struct commitinfo *info; | 75 | struct commitinfo *info; |
76 | unsigned char sha1[20], old_sha1[20]; | 76 | unsigned char sha1[20], old_sha1[20]; |
77 | char *patchname; | 77 | char *patchname; |
78 | 78 | ||
79 | if (!hex) | 79 | if (!hex) |
80 | hex = ctx.qry.head; | 80 | hex = ctx.qry.head; |
81 | 81 | ||
82 | if (get_sha1(hex, sha1)) { | 82 | if (get_sha1(hex, sha1)) { |
83 | cgit_print_error(fmt("Bad object id: %s", hex)); | 83 | cgit_print_error(fmt("Bad object id: %s", hex)); |
84 | return; | 84 | return; |
85 | } | 85 | } |
86 | commit = lookup_commit_reference(sha1); | 86 | commit = lookup_commit_reference(sha1); |
87 | if (!commit) { | 87 | if (!commit) { |
88 | cgit_print_error(fmt("Bad commit reference: %s", hex)); | 88 | cgit_print_error(fmt("Bad commit reference: %s", hex)); |
89 | return; | 89 | return; |
90 | } | 90 | } |
91 | info = cgit_parse_commit(commit); | 91 | info = cgit_parse_commit(commit); |
92 | hashcpy(old_sha1, commit->parents->item->object.sha1); | 92 | hashcpy(old_sha1, commit->parents->item->object.sha1); |
93 | 93 | ||
94 | patchname = fmt("%s.patch", sha1_to_hex(sha1)); | 94 | patchname = fmt("%s.patch", sha1_to_hex(sha1)); |
95 | cgit_print_snapshot_start("text/plain", patchname, item); | 95 | ctx.page.mimetype = "text/plain"; |
96 | ctx.page.filename = patchname; | ||
97 | cgit_print_http_headers(&ctx); | ||
96 | htmlf("From %s Mon Sep 17 00:00:00 2001\n", sha1_to_hex(sha1)); | 98 | htmlf("From %s Mon Sep 17 00:00:00 2001\n", sha1_to_hex(sha1)); |
97 | htmlf("From: %s%s\n", info->author, info->author_email); | 99 | htmlf("From: %s%s\n", info->author, info->author_email); |
98 | html("Date: "); | 100 | html("Date: "); |
99 | cgit_print_date(info->author_date, "%a, %d %b %Y %H:%M:%S %z%n"); | 101 | cgit_print_date(info->author_date, "%a, %d %b %Y %H:%M:%S %z%n"); |
100 | htmlf("Subject: %s\n\n", info->subject); | 102 | htmlf("Subject: %s\n\n", info->subject); |
101 | if (info->msg && *info->msg) { | 103 | if (info->msg && *info->msg) { |
102 | htmlf("%s", info->msg); | 104 | htmlf("%s", info->msg); |
103 | if (info->msg[strlen(info->msg) - 1] != '\n') | 105 | if (info->msg[strlen(info->msg) - 1] != '\n') |
104 | html("\n"); | 106 | html("\n"); |
105 | } | 107 | } |
106 | html("---\n"); | 108 | html("---\n"); |
107 | cgit_diff_tree(old_sha1, sha1, filepair_cb, NULL); | 109 | cgit_diff_tree(old_sha1, sha1, filepair_cb, NULL); |
108 | html("--\n"); | 110 | html("--\n"); |
109 | htmlf("cgit %s\n", CGIT_VERSION); | 111 | htmlf("cgit %s\n", CGIT_VERSION); |
110 | cgit_free_commitinfo(info); | 112 | cgit_free_commitinfo(info); |
111 | } | 113 | } |
diff --git a/ui-repolist.c b/ui-repolist.c index cd4e41d..e663585 100644 --- a/ui-repolist.c +++ b/ui-repolist.c | |||
@@ -6,98 +6,100 @@ | |||
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include <time.h> | 9 | #include <time.h> |
10 | 10 | ||
11 | #include "cgit.h" | 11 | #include "cgit.h" |
12 | #include "html.h" | 12 | #include "html.h" |
13 | 13 | ||
14 | time_t read_agefile(char *path) | 14 | time_t read_agefile(char *path) |
15 | { | 15 | { |
16 | FILE *f; | 16 | FILE *f; |
17 | static char buf[64], buf2[64]; | 17 | static char buf[64], buf2[64]; |
18 | 18 | ||
19 | if (!(f = fopen(path, "r"))) | 19 | if (!(f = fopen(path, "r"))) |
20 | return -1; | 20 | return -1; |
21 | fgets(buf, sizeof(buf), f); | 21 | fgets(buf, sizeof(buf), f); |
22 | fclose(f); | 22 | fclose(f); |
23 | if (parse_date(buf, buf2, sizeof(buf2))) | 23 | if (parse_date(buf, buf2, sizeof(buf2))) |
24 | return strtoul(buf2, NULL, 10); | 24 | return strtoul(buf2, NULL, 10); |
25 | else | 25 | else |
26 | return 0; | 26 | return 0; |
27 | } | 27 | } |
28 | 28 | ||
29 | static void print_modtime(struct cgit_repo *repo) | 29 | static void print_modtime(struct cgit_repo *repo) |
30 | { | 30 | { |
31 | char *path; | 31 | char *path; |
32 | struct stat s; | 32 | struct stat s; |
33 | 33 | ||
34 | path = fmt("%s/%s", repo->path, ctx.cfg.agefile); | 34 | path = fmt("%s/%s", repo->path, ctx.cfg.agefile); |
35 | if (stat(path, &s) == 0) { | 35 | if (stat(path, &s) == 0) { |
36 | cgit_print_age(read_agefile(path), -1, NULL); | 36 | cgit_print_age(read_agefile(path), -1, NULL); |
37 | return; | 37 | return; |
38 | } | 38 | } |
39 | 39 | ||
40 | path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch); | 40 | path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch); |
41 | if (stat(path, &s) != 0) | 41 | if (stat(path, &s) != 0) |
42 | return; | 42 | return; |
43 | cgit_print_age(s.st_mtime, -1, NULL); | 43 | cgit_print_age(s.st_mtime, -1, NULL); |
44 | } | 44 | } |
45 | 45 | ||
46 | void cgit_print_repolist(struct cacheitem *item) | 46 | void cgit_print_repolist(struct cacheitem *item) |
47 | { | 47 | { |
48 | int i, columns = 4; | 48 | int i, columns = 4; |
49 | char *last_group = NULL; | 49 | char *last_group = NULL; |
50 | 50 | ||
51 | if (ctx.cfg.enable_index_links) | 51 | if (ctx.cfg.enable_index_links) |
52 | columns++; | 52 | columns++; |
53 | 53 | ||
54 | cgit_print_docstart(ctx.cfg.root_title, item); | 54 | ctx.page.title = ctx.cfg.root_title; |
55 | cgit_print_pageheader(ctx.cfg.root_title, 0); | 55 | cgit_print_http_headers(&ctx); |
56 | cgit_print_docstart(&ctx); | ||
57 | cgit_print_pageheader(&ctx); | ||
56 | 58 | ||
57 | html("<table summary='repository list' class='list nowrap'>"); | 59 | html("<table summary='repository list' class='list nowrap'>"); |
58 | if (ctx.cfg.index_header) { | 60 | if (ctx.cfg.index_header) { |
59 | htmlf("<tr class='nohover'><td colspan='%d' class='include-block'>", | 61 | htmlf("<tr class='nohover'><td colspan='%d' class='include-block'>", |
60 | columns); | 62 | columns); |
61 | html_include(ctx.cfg.index_header); | 63 | html_include(ctx.cfg.index_header); |
62 | html("</td></tr>"); | 64 | html("</td></tr>"); |
63 | } | 65 | } |
64 | html("<tr class='nohover'>" | 66 | html("<tr class='nohover'>" |
65 | "<th class='left'>Name</th>" | 67 | "<th class='left'>Name</th>" |
66 | "<th class='left'>Description</th>" | 68 | "<th class='left'>Description</th>" |
67 | "<th class='left'>Owner</th>" | 69 | "<th class='left'>Owner</th>" |
68 | "<th class='left'>Idle</th>"); | 70 | "<th class='left'>Idle</th>"); |
69 | if (ctx.cfg.enable_index_links) | 71 | if (ctx.cfg.enable_index_links) |
70 | html("<th>Links</th>"); | 72 | html("<th>Links</th>"); |
71 | html("</tr>\n"); | 73 | html("</tr>\n"); |
72 | 74 | ||
73 | for (i=0; i<cgit_repolist.count; i++) { | 75 | for (i=0; i<cgit_repolist.count; i++) { |
74 | ctx.repo = &cgit_repolist.repos[i]; | 76 | ctx.repo = &cgit_repolist.repos[i]; |
75 | if ((last_group == NULL && ctx.repo->group != NULL) || | 77 | if ((last_group == NULL && ctx.repo->group != NULL) || |
76 | (last_group != NULL && ctx.repo->group == NULL) || | 78 | (last_group != NULL && ctx.repo->group == NULL) || |
77 | (last_group != NULL && ctx.repo->group != NULL && | 79 | (last_group != NULL && ctx.repo->group != NULL && |
78 | strcmp(ctx.repo->group, last_group))) { | 80 | strcmp(ctx.repo->group, last_group))) { |
79 | htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>", | 81 | htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>", |
80 | columns); | 82 | columns); |
81 | html_txt(ctx.repo->group); | 83 | html_txt(ctx.repo->group); |
82 | html("</td></tr>"); | 84 | html("</td></tr>"); |
83 | last_group = ctx.repo->group; | 85 | last_group = ctx.repo->group; |
84 | } | 86 | } |
85 | htmlf("<tr><td class='%s'>", | 87 | htmlf("<tr><td class='%s'>", |
86 | ctx.repo->group ? "sublevel-repo" : "toplevel-repo"); | 88 | ctx.repo->group ? "sublevel-repo" : "toplevel-repo"); |
87 | html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL); | 89 | html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL); |
88 | html_txt(ctx.repo->name); | 90 | html_txt(ctx.repo->name); |
89 | html_link_close(); | 91 | html_link_close(); |
90 | html("</td><td>"); | 92 | html("</td><td>"); |
91 | html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc); | 93 | html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc); |
92 | html("</td><td>"); | 94 | html("</td><td>"); |
93 | html_txt(ctx.repo->owner); | 95 | html_txt(ctx.repo->owner); |
94 | html("</td><td>"); | 96 | html("</td><td>"); |
95 | print_modtime(ctx.repo); | 97 | print_modtime(ctx.repo); |
96 | html("</td>"); | 98 | html("</td>"); |
97 | if (ctx.cfg.enable_index_links) { | 99 | if (ctx.cfg.enable_index_links) { |
98 | html("<td>"); | 100 | html("<td>"); |
99 | html_link_open(cgit_repourl(ctx.repo->url), | 101 | html_link_open(cgit_repourl(ctx.repo->url), |
100 | NULL, "button"); | 102 | NULL, "button"); |
101 | html("summary</a>"); | 103 | html("summary</a>"); |
102 | cgit_log_link("log", NULL, "button", NULL, NULL, NULL, | 104 | cgit_log_link("log", NULL, "button", NULL, NULL, NULL, |
103 | 0, NULL, NULL); | 105 | 0, NULL, NULL); |
diff --git a/ui-shared.c b/ui-shared.c index 2eff79d..2596023 100644 --- a/ui-shared.c +++ b/ui-shared.c | |||
@@ -1,84 +1,76 @@ | |||
1 | /* ui-shared.c: common web output functions | 1 | /* ui-shared.c: common web output functions |
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 | 11 | ||
12 | const char cgit_doctype[] = | 12 | const char cgit_doctype[] = |
13 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" | 13 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" |
14 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; | 14 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; |
15 | 15 | ||
16 | static char *http_date(time_t t) | 16 | static char *http_date(time_t t) |
17 | { | 17 | { |
18 | static char day[][4] = | 18 | static char day[][4] = |
19 | {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; | 19 | {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; |
20 | static char month[][4] = | 20 | static char month[][4] = |
21 | {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | 21 | {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
22 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; | 22 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; |
23 | struct tm *tm = gmtime(&t); | 23 | struct tm *tm = gmtime(&t); |
24 | return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], | 24 | return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], |
25 | tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, | 25 | tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, |
26 | tm->tm_hour, tm->tm_min, tm->tm_sec); | 26 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
27 | } | 27 | } |
28 | 28 | ||
29 | static long ttl_seconds(long ttl) | ||
30 | { | ||
31 | if (ttl<0) | ||
32 | return 60 * 60 * 24 * 365; | ||
33 | else | ||
34 | return ttl * 60; | ||
35 | } | ||
36 | |||
37 | void cgit_print_error(char *msg) | 29 | void cgit_print_error(char *msg) |
38 | { | 30 | { |
39 | html("<div class='error'>"); | 31 | html("<div class='error'>"); |
40 | html_txt(msg); | 32 | html_txt(msg); |
41 | html("</div>\n"); | 33 | html("</div>\n"); |
42 | } | 34 | } |
43 | 35 | ||
44 | char *cgit_rooturl() | 36 | char *cgit_rooturl() |
45 | { | 37 | { |
46 | if (ctx.cfg.virtual_root) | 38 | if (ctx.cfg.virtual_root) |
47 | return fmt("%s/", ctx.cfg.virtual_root); | 39 | return fmt("%s/", ctx.cfg.virtual_root); |
48 | else | 40 | else |
49 | return ctx.cfg.script_name; | 41 | return ctx.cfg.script_name; |
50 | } | 42 | } |
51 | 43 | ||
52 | char *cgit_repourl(const char *reponame) | 44 | char *cgit_repourl(const char *reponame) |
53 | { | 45 | { |
54 | if (ctx.cfg.virtual_root) { | 46 | if (ctx.cfg.virtual_root) { |
55 | return fmt("%s/%s/", ctx.cfg.virtual_root, reponame); | 47 | return fmt("%s/%s/", ctx.cfg.virtual_root, reponame); |
56 | } else { | 48 | } else { |
57 | return fmt("?r=%s", reponame); | 49 | return fmt("?r=%s", reponame); |
58 | } | 50 | } |
59 | } | 51 | } |
60 | 52 | ||
61 | char *cgit_fileurl(const char *reponame, const char *pagename, | 53 | char *cgit_fileurl(const char *reponame, const char *pagename, |
62 | const char *filename, const char *query) | 54 | const char *filename, const char *query) |
63 | { | 55 | { |
64 | char *tmp; | 56 | char *tmp; |
65 | char *delim; | 57 | char *delim; |
66 | 58 | ||
67 | if (ctx.cfg.virtual_root) { | 59 | if (ctx.cfg.virtual_root) { |
68 | tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame, | 60 | tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame, |
69 | pagename, (filename ? filename:"")); | 61 | pagename, (filename ? filename:"")); |
70 | delim = "?"; | 62 | delim = "?"; |
71 | } else { | 63 | } else { |
72 | tmp = fmt("?url=%s/%s/%s", reponame, pagename, | 64 | tmp = fmt("?url=%s/%s/%s", reponame, pagename, |
73 | (filename ? filename : "")); | 65 | (filename ? filename : "")); |
74 | delim = "&"; | 66 | delim = "&"; |
75 | } | 67 | } |
76 | if (query) | 68 | if (query) |
77 | tmp = fmt("%s%s%s", tmp, delim, query); | 69 | tmp = fmt("%s%s%s", tmp, delim, query); |
78 | return tmp; | 70 | return tmp; |
79 | } | 71 | } |
80 | 72 | ||
81 | char *cgit_pageurl(const char *reponame, const char *pagename, | 73 | char *cgit_pageurl(const char *reponame, const char *pagename, |
82 | const char *query) | 74 | const char *query) |
83 | { | 75 | { |
84 | return cgit_fileurl(reponame,pagename,0,query); | 76 | return cgit_fileurl(reponame,pagename,0,query); |
@@ -316,271 +308,269 @@ void cgit_print_date(time_t secs, char *format) | |||
316 | time = gmtime(&secs); | 308 | time = gmtime(&secs); |
317 | strftime(buf, sizeof(buf)-1, format, time); | 309 | strftime(buf, sizeof(buf)-1, format, time); |
318 | html_txt(buf); | 310 | html_txt(buf); |
319 | } | 311 | } |
320 | 312 | ||
321 | void cgit_print_age(time_t t, time_t max_relative, char *format) | 313 | void cgit_print_age(time_t t, time_t max_relative, char *format) |
322 | { | 314 | { |
323 | time_t now, secs; | 315 | time_t now, secs; |
324 | 316 | ||
325 | if (!t) | 317 | if (!t) |
326 | return; | 318 | return; |
327 | time(&now); | 319 | time(&now); |
328 | secs = now - t; | 320 | secs = now - t; |
329 | 321 | ||
330 | if (secs > max_relative && max_relative >= 0) { | 322 | if (secs > max_relative && max_relative >= 0) { |
331 | cgit_print_date(t, format); | 323 | cgit_print_date(t, format); |
332 | return; | 324 | return; |
333 | } | 325 | } |
334 | 326 | ||
335 | if (secs < TM_HOUR * 2) { | 327 | if (secs < TM_HOUR * 2) { |
336 | htmlf("<span class='age-mins'>%.0f min.</span>", | 328 | htmlf("<span class='age-mins'>%.0f min.</span>", |
337 | secs * 1.0 / TM_MIN); | 329 | secs * 1.0 / TM_MIN); |
338 | return; | 330 | return; |
339 | } | 331 | } |
340 | if (secs < TM_DAY * 2) { | 332 | if (secs < TM_DAY * 2) { |
341 | htmlf("<span class='age-hours'>%.0f hours</span>", | 333 | htmlf("<span class='age-hours'>%.0f hours</span>", |
342 | secs * 1.0 / TM_HOUR); | 334 | secs * 1.0 / TM_HOUR); |
343 | return; | 335 | return; |
344 | } | 336 | } |
345 | if (secs < TM_WEEK * 2) { | 337 | if (secs < TM_WEEK * 2) { |
346 | htmlf("<span class='age-days'>%.0f days</span>", | 338 | htmlf("<span class='age-days'>%.0f days</span>", |
347 | secs * 1.0 / TM_DAY); | 339 | secs * 1.0 / TM_DAY); |
348 | return; | 340 | return; |
349 | } | 341 | } |
350 | if (secs < TM_MONTH * 2) { | 342 | if (secs < TM_MONTH * 2) { |
351 | htmlf("<span class='age-weeks'>%.0f weeks</span>", | 343 | htmlf("<span class='age-weeks'>%.0f weeks</span>", |
352 | secs * 1.0 / TM_WEEK); | 344 | secs * 1.0 / TM_WEEK); |
353 | return; | 345 | return; |
354 | } | 346 | } |
355 | if (secs < TM_YEAR * 2) { | 347 | if (secs < TM_YEAR * 2) { |
356 | htmlf("<span class='age-months'>%.0f months</span>", | 348 | htmlf("<span class='age-months'>%.0f months</span>", |
357 | secs * 1.0 / TM_MONTH); | 349 | secs * 1.0 / TM_MONTH); |
358 | return; | 350 | return; |
359 | } | 351 | } |
360 | htmlf("<span class='age-years'>%.0f years</span>", | 352 | htmlf("<span class='age-years'>%.0f years</span>", |
361 | secs * 1.0 / TM_YEAR); | 353 | secs * 1.0 / TM_YEAR); |
362 | } | 354 | } |
363 | 355 | ||
364 | void cgit_print_docstart(char *title, struct cacheitem *item) | 356 | void cgit_print_http_headers(struct cgit_context *ctx) |
365 | { | 357 | { |
366 | html("Content-Type: text/html; charset=" PAGE_ENCODING "\n"); | 358 | if (ctx->page.mimetype && ctx->page.charset) |
367 | htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); | 359 | htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype, |
368 | htmlf("Expires: %s\n", http_date(item->st.st_mtime + | 360 | ctx->page.charset); |
369 | ttl_seconds(item->ttl))); | 361 | else if (ctx->page.mimetype) |
362 | htmlf("Content-Type: %s\n", ctx->page.mimetype); | ||
363 | if (ctx->page.filename) | ||
364 | htmlf("Content-Disposition: inline; filename=\"%s\"\n", | ||
365 | ctx->page.filename); | ||
366 | htmlf("Last-Modified: %s\n", http_date(ctx->page.modified)); | ||
367 | htmlf("Expires: %s\n", http_date(ctx->page.expires)); | ||
370 | html("\n"); | 368 | html("\n"); |
369 | } | ||
370 | |||
371 | void cgit_print_docstart(struct cgit_context *ctx) | ||
372 | { | ||
371 | html(cgit_doctype); | 373 | html(cgit_doctype); |
372 | html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); | 374 | html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); |
373 | html("<head>\n"); | 375 | html("<head>\n"); |
374 | html("<title>"); | 376 | html("<title>"); |
375 | html_txt(title); | 377 | html_txt(ctx->page.title); |
376 | html("</title>\n"); | 378 | html("</title>\n"); |
377 | htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); | 379 | htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); |
378 | if (ctx.cfg.robots && *ctx.cfg.robots) | 380 | if (ctx->cfg.robots && *ctx->cfg.robots) |
379 | htmlf("<meta name='robots' content='%s'/>\n", ctx.cfg.robots); | 381 | htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots); |
380 | html("<link rel='stylesheet' type='text/css' href='"); | 382 | html("<link rel='stylesheet' type='text/css' href='"); |
381 | html_attr(ctx.cfg.css); | 383 | html_attr(ctx->cfg.css); |
382 | html("'/>\n"); | 384 | html("'/>\n"); |
383 | html("</head>\n"); | 385 | html("</head>\n"); |
384 | html("<body>\n"); | 386 | html("<body>\n"); |
385 | } | 387 | } |
386 | 388 | ||
387 | void cgit_print_docend() | 389 | void cgit_print_docend() |
388 | { | 390 | { |
389 | html("</td>\n</tr>\n</table>\n</body>\n</html>\n"); | 391 | html("</td>\n</tr>\n</table>\n</body>\n</html>\n"); |
390 | } | 392 | } |
391 | 393 | ||
392 | int print_branch_option(const char *refname, const unsigned char *sha1, | 394 | int print_branch_option(const char *refname, const unsigned char *sha1, |
393 | int flags, void *cb_data) | 395 | int flags, void *cb_data) |
394 | { | 396 | { |
395 | char *name = (char *)refname; | 397 | char *name = (char *)refname; |
396 | html_option(name, name, ctx.qry.head); | 398 | html_option(name, name, ctx.qry.head); |
397 | return 0; | 399 | return 0; |
398 | } | 400 | } |
399 | 401 | ||
400 | int print_archive_ref(const char *refname, const unsigned char *sha1, | 402 | int print_archive_ref(const char *refname, const unsigned char *sha1, |
401 | int flags, void *cb_data) | 403 | int flags, void *cb_data) |
402 | { | 404 | { |
403 | struct tag *tag; | 405 | struct tag *tag; |
404 | struct taginfo *info; | 406 | struct taginfo *info; |
405 | struct object *obj; | 407 | struct object *obj; |
406 | char buf[256], *url; | 408 | char buf[256], *url; |
407 | unsigned char fileid[20]; | 409 | unsigned char fileid[20]; |
408 | int *header = (int *)cb_data; | 410 | int *header = (int *)cb_data; |
409 | 411 | ||
410 | if (prefixcmp(refname, "refs/archives")) | 412 | if (prefixcmp(refname, "refs/archives")) |
411 | return 0; | 413 | return 0; |
412 | strncpy(buf, refname+14, sizeof(buf)); | 414 | strncpy(buf, refname+14, sizeof(buf)); |
413 | obj = parse_object(sha1); | 415 | obj = parse_object(sha1); |
414 | if (!obj) | 416 | if (!obj) |
415 | return 1; | 417 | return 1; |
416 | if (obj->type == OBJ_TAG) { | 418 | if (obj->type == OBJ_TAG) { |
417 | tag = lookup_tag(sha1); | 419 | tag = lookup_tag(sha1); |
418 | if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) | 420 | if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) |
419 | return 0; | 421 | return 0; |
420 | hashcpy(fileid, tag->tagged->sha1); | 422 | hashcpy(fileid, tag->tagged->sha1); |
421 | } else if (obj->type != OBJ_BLOB) { | 423 | } else if (obj->type != OBJ_BLOB) { |
422 | return 0; | 424 | return 0; |
423 | } else { | 425 | } else { |
424 | hashcpy(fileid, sha1); | 426 | hashcpy(fileid, sha1); |
425 | } | 427 | } |
426 | if (!*header) { | 428 | if (!*header) { |
427 | html("<h1>download</h1>\n"); | 429 | html("<h1>download</h1>\n"); |
428 | *header = 1; | 430 | *header = 1; |
429 | } | 431 | } |
430 | url = cgit_pageurl(ctx.qry.repo, "blob", | 432 | url = cgit_pageurl(ctx.qry.repo, "blob", |
431 | fmt("id=%s&path=%s", sha1_to_hex(fileid), | 433 | fmt("id=%s&path=%s", sha1_to_hex(fileid), |
432 | buf)); | 434 | buf)); |
433 | html_link_open(url, NULL, "menu"); | 435 | html_link_open(url, NULL, "menu"); |
434 | html_txt(strlpart(buf, 20)); | 436 | html_txt(strlpart(buf, 20)); |
435 | html_link_close(); | 437 | html_link_close(); |
436 | return 0; | 438 | return 0; |
437 | } | 439 | } |
438 | 440 | ||
439 | void add_hidden_formfields(int incl_head, int incl_search, char *page) | 441 | void add_hidden_formfields(int incl_head, int incl_search, char *page) |
440 | { | 442 | { |
441 | char *url; | 443 | char *url; |
442 | 444 | ||
443 | if (!ctx.cfg.virtual_root) { | 445 | if (!ctx.cfg.virtual_root) { |
444 | url = fmt("%s/%s", ctx.qry.repo, page); | 446 | url = fmt("%s/%s", ctx.qry.repo, page); |
445 | if (ctx.qry.path) | 447 | if (ctx.qry.path) |
446 | url = fmt("%s/%s", url, ctx.qry.path); | 448 | url = fmt("%s/%s", url, ctx.qry.path); |
447 | html_hidden("url", url); | 449 | html_hidden("url", url); |
448 | } | 450 | } |
449 | 451 | ||
450 | if (incl_head && strcmp(ctx.qry.head, ctx.repo->defbranch)) | 452 | if (incl_head && strcmp(ctx.qry.head, ctx.repo->defbranch)) |
451 | html_hidden("h", ctx.qry.head); | 453 | html_hidden("h", ctx.qry.head); |
452 | 454 | ||
453 | if (ctx.qry.sha1) | 455 | if (ctx.qry.sha1) |
454 | html_hidden("id", ctx.qry.sha1); | 456 | html_hidden("id", ctx.qry.sha1); |
455 | if (ctx.qry.sha2) | 457 | if (ctx.qry.sha2) |
456 | html_hidden("id2", ctx.qry.sha2); | 458 | html_hidden("id2", ctx.qry.sha2); |
457 | 459 | ||
458 | if (incl_search) { | 460 | if (incl_search) { |
459 | if (ctx.qry.grep) | 461 | if (ctx.qry.grep) |
460 | html_hidden("qt", ctx.qry.grep); | 462 | html_hidden("qt", ctx.qry.grep); |
461 | if (ctx.qry.search) | 463 | if (ctx.qry.search) |
462 | html_hidden("q", ctx.qry.search); | 464 | html_hidden("q", ctx.qry.search); |
463 | } | 465 | } |
464 | } | 466 | } |
465 | 467 | ||
466 | void cgit_print_pageheader(char *title, int show_search) | 468 | void cgit_print_pageheader(struct cgit_context *ctx) |
467 | { | 469 | { |
468 | static const char *default_info = "This is cgit, a fast webinterface for git repositories"; | 470 | static const char *default_info = "This is cgit, a fast webinterface for git repositories"; |
469 | int header = 0; | 471 | int header = 0; |
470 | char *url; | 472 | char *url; |
471 | 473 | ||
472 | html("<table id='layout' summary=''>\n"); | 474 | html("<table id='layout' summary=''>\n"); |
473 | html("<tr><td id='sidebar'>\n"); | 475 | html("<tr><td id='sidebar'>\n"); |
474 | html("<table class='sidebar' cellspacing='0' summary=''>\n"); | 476 | html("<table class='sidebar' cellspacing='0' summary=''>\n"); |
475 | html("<tr><td class='sidebar'>\n<a href='"); | 477 | html("<tr><td class='sidebar'>\n<a href='"); |
476 | html_attr(cgit_rooturl()); | 478 | html_attr(cgit_rooturl()); |
477 | htmlf("'><img src='%s' alt='cgit'/></a>\n", | 479 | htmlf("'><img src='%s' alt='cgit'/></a>\n", |
478 | ctx.cfg.logo); | 480 | ctx->cfg.logo); |
479 | html("</td></tr>\n<tr><td class='sidebar'>\n"); | 481 | html("</td></tr>\n<tr><td class='sidebar'>\n"); |
480 | if (ctx.repo) { | 482 | if (ctx->repo) { |
481 | html("<h1 class='first'>"); | 483 | html("<h1 class='first'>"); |
482 | html_txt(strrpart(ctx.repo->name, 20)); | 484 | html_txt(strrpart(ctx->repo->name, 20)); |
483 | html("</h1>\n"); | 485 | html("</h1>\n"); |
484 | html_txt(ctx.repo->desc); | 486 | html_txt(ctx->repo->desc); |
485 | if (ctx.repo->owner) { | 487 | if (ctx->repo->owner) { |
486 | html("<h1>owner</h1>\n"); | 488 | html("<h1>owner</h1>\n"); |
487 | html_txt(ctx.repo->owner); | 489 | html_txt(ctx->repo->owner); |
488 | } | 490 | } |
489 | html("<h1>navigate</h1>\n"); | 491 | html("<h1>navigate</h1>\n"); |
490 | reporevlink(NULL, "summary", NULL, "menu", ctx.qry.head, | 492 | reporevlink(NULL, "summary", NULL, "menu", ctx->qry.head, |
491 | NULL, NULL); | 493 | NULL, NULL); |
492 | cgit_log_link("log", NULL, "menu", ctx.qry.head, NULL, NULL, | 494 | cgit_log_link("log", NULL, "menu", ctx->qry.head, NULL, NULL, |
493 | 0, NULL, NULL); | 495 | 0, NULL, NULL); |
494 | cgit_tree_link("tree", NULL, "menu", ctx.qry.head, | 496 | cgit_tree_link("tree", NULL, "menu", ctx->qry.head, |
495 | ctx.qry.sha1, NULL); | 497 | ctx->qry.sha1, NULL); |
496 | cgit_commit_link("commit", NULL, "menu", ctx.qry.head, | 498 | cgit_commit_link("commit", NULL, "menu", ctx->qry.head, |
497 | ctx.qry.sha1); | 499 | ctx->qry.sha1); |
498 | cgit_diff_link("diff", NULL, "menu", ctx.qry.head, | 500 | cgit_diff_link("diff", NULL, "menu", ctx->qry.head, |
499 | ctx.qry.sha1, ctx.qry.sha2, NULL); | 501 | ctx->qry.sha1, ctx->qry.sha2, NULL); |
500 | cgit_patch_link("patch", NULL, "menu", ctx.qry.head, | 502 | cgit_patch_link("patch", NULL, "menu", ctx->qry.head, |
501 | ctx.qry.sha1); | 503 | ctx->qry.sha1); |
502 | 504 | ||
503 | for_each_ref(print_archive_ref, &header); | 505 | for_each_ref(print_archive_ref, &header); |
504 | 506 | ||
505 | if (ctx.repo->clone_url || ctx.cfg.clone_prefix) { | 507 | if (ctx->repo->clone_url || ctx->cfg.clone_prefix) { |
506 | html("<h1>clone</h1>\n"); | 508 | html("<h1>clone</h1>\n"); |
507 | if (ctx.repo->clone_url) | 509 | if (ctx->repo->clone_url) |
508 | url = ctx.repo->clone_url; | 510 | url = ctx->repo->clone_url; |
509 | else | 511 | else |
510 | url = fmt("%s%s", ctx.cfg.clone_prefix, | 512 | url = fmt("%s%s", ctx->cfg.clone_prefix, |
511 | ctx.repo->url); | 513 | ctx->repo->url); |
512 | html("<a class='menu' href='"); | 514 | html("<a class='menu' href='"); |
513 | html_attr(url); | 515 | html_attr(url); |
514 | html("' title='"); | 516 | html("' title='"); |
515 | html_attr(url); | 517 | html_attr(url); |
516 | html("'>\n"); | 518 | html("'>\n"); |
517 | html_txt(strrpart(url, 20)); | 519 | html_txt(strrpart(url, 20)); |
518 | html("</a>\n"); | 520 | html("</a>\n"); |
519 | } | 521 | } |
520 | 522 | ||
521 | html("<h1>branch</h1>\n"); | 523 | html("<h1>branch</h1>\n"); |
522 | html("<form method='get' action=''>\n"); | 524 | html("<form method='get' action=''>\n"); |
523 | add_hidden_formfields(0, 1, ctx.qry.page); | 525 | add_hidden_formfields(0, 1, ctx->qry.page); |
524 | // html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>"); | 526 | // html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>"); |
525 | html("<select name='h' onchange='this.form.submit();'>\n"); | 527 | html("<select name='h' onchange='this.form.submit();'>\n"); |
526 | for_each_branch_ref(print_branch_option, ctx.qry.head); | 528 | for_each_branch_ref(print_branch_option, ctx->qry.head); |
527 | html("</select>\n"); | 529 | html("</select>\n"); |
528 | // html("</td><td>"); | 530 | // html("</td><td>"); |
529 | html("<noscript><input type='submit' id='switch-btn' value='switch'/></noscript>\n"); | 531 | html("<noscript><input type='submit' id='switch-btn' value='switch'/></noscript>\n"); |
530 | // html("</td></tr></table>"); | 532 | // html("</td></tr></table>"); |
531 | html("</form>\n"); | 533 | html("</form>\n"); |
532 | 534 | ||
533 | html("<h1>search</h1>\n"); | 535 | html("<h1>search</h1>\n"); |
534 | html("<form method='get' action='"); | 536 | html("<form method='get' action='"); |
535 | if (ctx.cfg.virtual_root) | 537 | if (ctx->cfg.virtual_root) |
536 | html_attr(cgit_fileurl(ctx.qry.repo, "log", | 538 | html_attr(cgit_fileurl(ctx->qry.repo, "log", |
537 | ctx.qry.path, NULL)); | 539 | ctx->qry.path, NULL)); |
538 | html("'>\n"); | 540 | html("'>\n"); |
539 | add_hidden_formfields(1, 0, "log"); | 541 | add_hidden_formfields(1, 0, "log"); |
540 | html("<select name='qt'>\n"); | 542 | html("<select name='qt'>\n"); |
541 | html_option("grep", "log msg", ctx.qry.grep); | 543 | html_option("grep", "log msg", ctx->qry.grep); |
542 | html_option("author", "author", ctx.qry.grep); | 544 | html_option("author", "author", ctx->qry.grep); |
543 | html_option("committer", "committer", ctx.qry.grep); | 545 | html_option("committer", "committer", ctx->qry.grep); |
544 | html("</select>\n"); | 546 | html("</select>\n"); |
545 | html("<input class='txt' type='text' name='q' value='"); | 547 | html("<input class='txt' type='text' name='q' value='"); |
546 | html_attr(ctx.qry.search); | 548 | html_attr(ctx->qry.search); |
547 | html("'/>\n"); | 549 | html("'/>\n"); |
548 | html("</form>\n"); | 550 | html("</form>\n"); |
549 | } else { | 551 | } else { |
550 | if (!ctx.cfg.index_info || html_include(ctx.cfg.index_info)) | 552 | if (!ctx->cfg.index_info || html_include(ctx->cfg.index_info)) |
551 | html(default_info); | 553 | html(default_info); |
552 | } | 554 | } |
553 | 555 | ||
554 | html("</td></tr></table></td>\n"); | 556 | html("</td></tr></table></td>\n"); |
555 | 557 | ||
556 | html("<td id='content'>\n"); | 558 | html("<td id='content'>\n"); |
557 | } | 559 | } |
558 | 560 | ||
559 | |||
560 | void cgit_print_snapshot_start(const char *mimetype, const char *filename, | ||
561 | struct cacheitem *item) | ||
562 | { | ||
563 | htmlf("Content-Type: %s\n", mimetype); | ||
564 | htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename); | ||
565 | htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); | ||
566 | htmlf("Expires: %s\n", http_date(item->st.st_mtime + | ||
567 | ttl_seconds(item->ttl))); | ||
568 | html("\n"); | ||
569 | } | ||
570 | |||
571 | void cgit_print_filemode(unsigned short mode) | 561 | void cgit_print_filemode(unsigned short mode) |
572 | { | 562 | { |
573 | if (S_ISDIR(mode)) | 563 | if (S_ISDIR(mode)) |
574 | html("d"); | 564 | html("d"); |
575 | else if (S_ISLNK(mode)) | 565 | else if (S_ISLNK(mode)) |
576 | html("l"); | 566 | html("l"); |
577 | else if (S_ISGITLINK(mode)) | 567 | else if (S_ISGITLINK(mode)) |
578 | html("m"); | 568 | html("m"); |
579 | else | 569 | else |
580 | html("-"); | 570 | html("-"); |
581 | html_fileperm(mode >> 6); | 571 | html_fileperm(mode >> 6); |
582 | html_fileperm(mode >> 3); | 572 | html_fileperm(mode >> 3); |
583 | html_fileperm(mode); | 573 | html_fileperm(mode); |
584 | } | 574 | } |
585 | 575 | ||
586 | /* vim:set sw=8: */ | 576 | /* vim:set sw=8: */ |
diff --git a/ui-snapshot.c b/ui-snapshot.c index 67dbbdd..4449803 100644 --- a/ui-snapshot.c +++ b/ui-snapshot.c | |||
@@ -56,97 +56,99 @@ static int write_tar_bzip2_archive(struct archiver_args *args) | |||
56 | } | 56 | } |
57 | 57 | ||
58 | static const struct snapshot_archive_t { | 58 | static const struct snapshot_archive_t { |
59 | const char *suffix; | 59 | const char *suffix; |
60 | const char *mimetype; | 60 | const char *mimetype; |
61 | write_archive_fn_t write_func; | 61 | write_archive_fn_t write_func; |
62 | int bit; | 62 | int bit; |
63 | }snapshot_archives[] = { | 63 | }snapshot_archives[] = { |
64 | { ".zip", "application/x-zip", write_zip_archive, 0x1 }, | 64 | { ".zip", "application/x-zip", write_zip_archive, 0x1 }, |
65 | { ".tar.gz", "application/x-tar", write_tar_gzip_archive, 0x2 }, | 65 | { ".tar.gz", "application/x-tar", write_tar_gzip_archive, 0x2 }, |
66 | { ".tar.bz2", "application/x-tar", write_tar_bzip2_archive, 0x4 }, | 66 | { ".tar.bz2", "application/x-tar", write_tar_bzip2_archive, 0x4 }, |
67 | { ".tar", "application/x-tar", write_tar_archive, 0x8 } | 67 | { ".tar", "application/x-tar", write_tar_archive, 0x8 } |
68 | }; | 68 | }; |
69 | 69 | ||
70 | #define snapshot_archives_len (sizeof(snapshot_archives) / sizeof(*snapshot_archives)) | 70 | #define snapshot_archives_len (sizeof(snapshot_archives) / sizeof(*snapshot_archives)) |
71 | 71 | ||
72 | void cgit_print_snapshot(struct cacheitem *item, const char *head, | 72 | void cgit_print_snapshot(struct cacheitem *item, const char *head, |
73 | const char *hex, const char *prefix, | 73 | const char *hex, const char *prefix, |
74 | const char *filename, int snapshots) | 74 | const char *filename, int snapshots) |
75 | { | 75 | { |
76 | const struct snapshot_archive_t* sat; | 76 | const struct snapshot_archive_t* sat; |
77 | struct archiver_args args; | 77 | struct archiver_args args; |
78 | struct commit *commit; | 78 | struct commit *commit; |
79 | unsigned char sha1[20]; | 79 | unsigned char sha1[20]; |
80 | int f, sl, fnl = strlen(filename); | 80 | int f, sl, fnl = strlen(filename); |
81 | 81 | ||
82 | for(f=0; f<snapshot_archives_len; f++) { | 82 | for(f=0; f<snapshot_archives_len; f++) { |
83 | sat = &snapshot_archives[f]; | 83 | sat = &snapshot_archives[f]; |
84 | if(!(snapshots & sat->bit)) | 84 | if(!(snapshots & sat->bit)) |
85 | continue; | 85 | continue; |
86 | sl = strlen(sat->suffix); | 86 | sl = strlen(sat->suffix); |
87 | if(fnl<sl || strcmp(&filename[fnl-sl],sat->suffix)) | 87 | if(fnl<sl || strcmp(&filename[fnl-sl],sat->suffix)) |
88 | continue; | 88 | continue; |
89 | if (!hex) | 89 | if (!hex) |
90 | hex = head; | 90 | hex = head; |
91 | if(get_sha1(hex, sha1)) { | 91 | if(get_sha1(hex, sha1)) { |
92 | cgit_print_error(fmt("Bad object id: %s", hex)); | 92 | cgit_print_error(fmt("Bad object id: %s", hex)); |
93 | return; | 93 | return; |
94 | } | 94 | } |
95 | commit = lookup_commit_reference(sha1); | 95 | commit = lookup_commit_reference(sha1); |
96 | if(!commit) { | 96 | if(!commit) { |
97 | cgit_print_error(fmt("Not a commit reference: %s", hex)); | 97 | cgit_print_error(fmt("Not a commit reference: %s", hex)); |
98 | return;; | 98 | return;; |
99 | } | 99 | } |
100 | memset(&args,0,sizeof(args)); | 100 | memset(&args,0,sizeof(args)); |
101 | args.base = fmt("%s/", prefix); | 101 | args.base = fmt("%s/", prefix); |
102 | args.tree = commit->tree; | 102 | args.tree = commit->tree; |
103 | args.time = commit->date; | 103 | args.time = commit->date; |
104 | cgit_print_snapshot_start(sat->mimetype, filename, item); | 104 | ctx.page.mimetype = xstrdup(sat->mimetype); |
105 | ctx.page.filename = xstrdup(filename); | ||
106 | cgit_print_http_headers(&ctx); | ||
105 | (*sat->write_func)(&args); | 107 | (*sat->write_func)(&args); |
106 | return; | 108 | return; |
107 | } | 109 | } |
108 | cgit_print_error(fmt("Unsupported snapshot format: %s", filename)); | 110 | cgit_print_error(fmt("Unsupported snapshot format: %s", filename)); |
109 | } | 111 | } |
110 | 112 | ||
111 | void cgit_print_snapshot_links(const char *repo, const char *head, | 113 | void cgit_print_snapshot_links(const char *repo, const char *head, |
112 | const char *hex, int snapshots) | 114 | const char *hex, int snapshots) |
113 | { | 115 | { |
114 | const struct snapshot_archive_t* sat; | 116 | const struct snapshot_archive_t* sat; |
115 | char *filename; | 117 | char *filename; |
116 | int f; | 118 | int f; |
117 | 119 | ||
118 | for(f=0; f<snapshot_archives_len; f++) { | 120 | for(f=0; f<snapshot_archives_len; f++) { |
119 | sat = &snapshot_archives[f]; | 121 | sat = &snapshot_archives[f]; |
120 | if(!(snapshots & sat->bit)) | 122 | if(!(snapshots & sat->bit)) |
121 | continue; | 123 | continue; |
122 | filename = fmt("%s-%s%s", cgit_repobasename(repo), hex, | 124 | filename = fmt("%s-%s%s", cgit_repobasename(repo), hex, |
123 | sat->suffix); | 125 | sat->suffix); |
124 | cgit_snapshot_link(filename, NULL, NULL, (char *)head, | 126 | cgit_snapshot_link(filename, NULL, NULL, (char *)head, |
125 | (char *)hex, filename); | 127 | (char *)hex, filename); |
126 | html("<br/>"); | 128 | html("<br/>"); |
127 | } | 129 | } |
128 | } | 130 | } |
129 | 131 | ||
130 | int cgit_parse_snapshots_mask(const char *str) | 132 | int cgit_parse_snapshots_mask(const char *str) |
131 | { | 133 | { |
132 | const struct snapshot_archive_t* sat; | 134 | const struct snapshot_archive_t* sat; |
133 | static const char *delim = " \t,:/|;"; | 135 | static const char *delim = " \t,:/|;"; |
134 | int f, tl, sl, rv = 0; | 136 | int f, tl, sl, rv = 0; |
135 | 137 | ||
136 | /* favor legacy setting */ | 138 | /* favor legacy setting */ |
137 | if(atoi(str)) | 139 | if(atoi(str)) |
138 | return 1; | 140 | return 1; |
139 | for(;;) { | 141 | for(;;) { |
140 | str += strspn(str,delim); | 142 | str += strspn(str,delim); |
141 | tl = strcspn(str,delim); | 143 | tl = strcspn(str,delim); |
142 | if(!tl) | 144 | if(!tl) |
143 | break; | 145 | break; |
144 | for(f=0; f<snapshot_archives_len; f++) { | 146 | for(f=0; f<snapshot_archives_len; f++) { |
145 | sat = &snapshot_archives[f]; | 147 | sat = &snapshot_archives[f]; |
146 | sl = strlen(sat->suffix); | 148 | sl = strlen(sat->suffix); |
147 | if((tl == sl && !strncmp(sat->suffix, str, tl)) || | 149 | if((tl == sl && !strncmp(sat->suffix, str, tl)) || |
148 | (tl == sl-1 && !strncmp(sat->suffix+1, str, tl-1))) { | 150 | (tl == sl-1 && !strncmp(sat->suffix+1, str, tl-1))) { |
149 | rv |= sat->bit; | 151 | rv |= sat->bit; |
150 | break; | 152 | break; |
151 | } | 153 | } |
152 | } | 154 | } |