summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--cache.c4
-rw-r--r--cgit.c72
-rw-r--r--cgit.h35
-rw-r--r--parsing.c8
-rw-r--r--shared.c43
-rw-r--r--ui-commit.c14
-rw-r--r--ui-diff.c6
-rw-r--r--ui-log.c20
-rw-r--r--ui-patch.c2
-rw-r--r--ui-refs.c4
-rw-r--r--ui-shared.c86
-rw-r--r--ui-summary.c6
-rw-r--r--ui-tree.c20
13 files changed, 156 insertions, 164 deletions
diff --git a/cache.c b/cache.c
index 372e38d..b162952 100644
--- a/cache.c
+++ b/cache.c
@@ -28,68 +28,68 @@ char *cache_safe_filename(const char *unsafe)
28 *s++ = c; 28 *s++ = c;
29 } 29 }
30 *s = '\0'; 30 *s = '\0';
31 return buf[bufidx]; 31 return buf[bufidx];
32} 32}
33 33
34int cache_exist(struct cacheitem *item) 34int cache_exist(struct cacheitem *item)
35{ 35{
36 if (stat(item->name, &item->st)) { 36 if (stat(item->name, &item->st)) {
37 item->st.st_mtime = 0; 37 item->st.st_mtime = 0;
38 return 0; 38 return 0;
39 } 39 }
40 return 1; 40 return 1;
41} 41}
42 42
43int cache_create_dirs() 43int cache_create_dirs()
44{ 44{
45 char *path; 45 char *path;
46 46
47 path = fmt("%s", cgit_cache_root); 47 path = fmt("%s", cgit_cache_root);
48 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 48 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
49 return 0; 49 return 0;
50 50
51 if (!cgit_repo) 51 if (!cgit_repo)
52 return 0; 52 return 0;
53 53
54 path = fmt("%s/%s", cgit_cache_root, 54 path = fmt("%s/%s", cgit_cache_root,
55 cache_safe_filename(cgit_repo->url)); 55 cache_safe_filename(cgit_repo->url));
56 56
57 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 57 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
58 return 0; 58 return 0;
59 59
60 if (cgit_query_page) { 60 if (ctx.qry.page) {
61 path = fmt("%s/%s/%s", cgit_cache_root, 61 path = fmt("%s/%s/%s", cgit_cache_root,
62 cache_safe_filename(cgit_repo->url), 62 cache_safe_filename(cgit_repo->url),
63 cgit_query_page); 63 ctx.qry.page);
64 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 64 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
65 return 0; 65 return 0;
66 } 66 }
67 return 1; 67 return 1;
68} 68}
69 69
70int cache_refill_overdue(const char *lockfile) 70int cache_refill_overdue(const char *lockfile)
71{ 71{
72 struct stat st; 72 struct stat st;
73 73
74 if (stat(lockfile, &st)) 74 if (stat(lockfile, &st))
75 return 0; 75 return 0;
76 else 76 else
77 return (time(NULL) - st.st_mtime > cgit_cache_max_create_time); 77 return (time(NULL) - st.st_mtime > cgit_cache_max_create_time);
78} 78}
79 79
80int cache_lock(struct cacheitem *item) 80int cache_lock(struct cacheitem *item)
81{ 81{
82 int i = 0; 82 int i = 0;
83 char *lockfile = xstrdup(fmt("%s.lock", item->name)); 83 char *lockfile = xstrdup(fmt("%s.lock", item->name));
84 84
85 top: 85 top:
86 if (++i > cgit_max_lock_attempts) 86 if (++i > cgit_max_lock_attempts)
87 die("cache_lock: unable to lock %s: %s", 87 die("cache_lock: unable to lock %s: %s",
88 item->name, strerror(errno)); 88 item->name, strerror(errno));
89 89
90 item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); 90 item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
91 91
92 if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) 92 if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs())
93 goto top; 93 goto top;
94 94
95 if (item->fd == NOLOCK && errno == EEXIST && 95 if (item->fd == NOLOCK && errno == EEXIST &&
diff --git a/cgit.c b/cgit.c
index e8acc03..8a30c3c 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1,204 +1,204 @@
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 10
11static int cgit_prepare_cache(struct cacheitem *item) 11static int cgit_prepare_cache(struct cacheitem *item)
12{ 12{
13 if (!cgit_repo && cgit_query_repo) { 13 if (!cgit_repo && ctx.qry.repo) {
14 char *title = fmt("%s - %s", cgit_root_title, "Bad request"); 14 char *title = fmt("%s - %s", cgit_root_title, "Bad request");
15 cgit_print_docstart(title, item); 15 cgit_print_docstart(title, item);
16 cgit_print_pageheader(title, 0); 16 cgit_print_pageheader(title, 0);
17 cgit_print_error(fmt("Unknown repo: %s", cgit_query_repo)); 17 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
18 cgit_print_docend(); 18 cgit_print_docend();
19 return 0; 19 return 0;
20 } 20 }
21 21
22 if (!cgit_repo) { 22 if (!cgit_repo) {
23 item->name = xstrdup(fmt("%s/index.html", cgit_cache_root)); 23 item->name = xstrdup(fmt("%s/index.html", cgit_cache_root));
24 item->ttl = cgit_cache_root_ttl; 24 item->ttl = cgit_cache_root_ttl;
25 return 1; 25 return 1;
26 } 26 }
27 27
28 if (!cgit_cmd) { 28 if (!cgit_cmd) {
29 item->name = xstrdup(fmt("%s/%s/index.%s.html", cgit_cache_root, 29 item->name = xstrdup(fmt("%s/%s/index.%s.html", cgit_cache_root,
30 cache_safe_filename(cgit_repo->url), 30 cache_safe_filename(cgit_repo->url),
31 cache_safe_filename(cgit_querystring))); 31 cache_safe_filename(ctx.qry.raw)));
32 item->ttl = cgit_cache_repo_ttl; 32 item->ttl = cgit_cache_repo_ttl;
33 } else { 33 } else {
34 item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, 34 item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root,
35 cache_safe_filename(cgit_repo->url), 35 cache_safe_filename(cgit_repo->url),
36 cgit_query_page, 36 ctx.qry.page,
37 cache_safe_filename(cgit_querystring))); 37 cache_safe_filename(ctx.qry.raw)));
38 if (cgit_query_has_symref) 38 if (ctx.qry.has_symref)
39 item->ttl = cgit_cache_dynamic_ttl; 39 item->ttl = cgit_cache_dynamic_ttl;
40 else if (cgit_query_has_sha1) 40 else if (ctx.qry.has_sha1)
41 item->ttl = cgit_cache_static_ttl; 41 item->ttl = cgit_cache_static_ttl;
42 else 42 else
43 item->ttl = cgit_cache_repo_ttl; 43 item->ttl = cgit_cache_repo_ttl;
44 } 44 }
45 return 1; 45 return 1;
46} 46}
47 47
48struct refmatch { 48struct refmatch {
49 char *req_ref; 49 char *req_ref;
50 char *first_ref; 50 char *first_ref;
51 int match; 51 int match;
52}; 52};
53 53
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 repoinfo *repo) 67char *find_default_branch(struct repoinfo *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 86
87 if (chdir(cgit_repo->path)) { 87 if (chdir(cgit_repo->path)) {
88 title = fmt("%s - %s", cgit_root_title, "Bad request"); 88 title = fmt("%s - %s", cgit_root_title, "Bad request");
89 cgit_print_docstart(title, item); 89 cgit_print_docstart(title, item);
90 cgit_print_pageheader(title, 0); 90 cgit_print_pageheader(title, 0);
91 cgit_print_error(fmt("Unable to scan repository: %s", 91 cgit_print_error(fmt("Unable to scan repository: %s",
92 strerror(errno))); 92 strerror(errno)));
93 cgit_print_docend(); 93 cgit_print_docend();
94 return; 94 return;
95 } 95 }
96 96
97 title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); 97 title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc);
98 show_search = 0; 98 show_search = 0;
99 setenv("GIT_DIR", cgit_repo->path, 1); 99 setenv("GIT_DIR", cgit_repo->path, 1);
100 100
101 if (!cgit_query_head) { 101 if (!ctx.qry.head) {
102 cgit_query_head = xstrdup(find_default_branch(cgit_repo)); 102 ctx.qry.head = xstrdup(find_default_branch(cgit_repo));
103 cgit_repo->defbranch = cgit_query_head; 103 cgit_repo->defbranch = ctx.qry.head;
104 } 104 }
105 105
106 if (!cgit_query_head) { 106 if (!ctx.qry.head) {
107 cgit_print_docstart(title, item); 107 cgit_print_docstart(title, item);
108 cgit_print_pageheader(title, 0); 108 cgit_print_pageheader(title, 0);
109 cgit_print_error("Repository seems to be empty"); 109 cgit_print_error("Repository seems to be empty");
110 cgit_print_docend(); 110 cgit_print_docend();
111 return; 111 return;
112 } 112 }
113 113
114 if (get_sha1(cgit_query_head, sha1)) { 114 if (get_sha1(ctx.qry.head, sha1)) {
115 tmp = xstrdup(cgit_query_head); 115 tmp = xstrdup(ctx.qry.head);
116 cgit_query_head = cgit_repo->defbranch; 116 ctx.qry.head = cgit_repo->defbranch;
117 cgit_print_docstart(title, item); 117 cgit_print_docstart(title, item);
118 cgit_print_pageheader(title, 0); 118 cgit_print_pageheader(title, 0);
119 cgit_print_error(fmt("Invalid branch: %s", tmp)); 119 cgit_print_error(fmt("Invalid branch: %s", tmp));
120 cgit_print_docend(); 120 cgit_print_docend();
121 return; 121 return;
122 } 122 }
123 123
124 if ((cgit_cmd == CMD_SNAPSHOT) && cgit_repo->snapshots) { 124 if ((cgit_cmd == CMD_SNAPSHOT) && cgit_repo->snapshots) {
125 cgit_print_snapshot(item, cgit_query_head, cgit_query_sha1, 125 cgit_print_snapshot(item, ctx.qry.head, ctx.qry.sha1,
126 cgit_repobasename(cgit_repo->url), 126 cgit_repobasename(cgit_repo->url),
127 cgit_query_path, 127 ctx.qry.path,
128 cgit_repo->snapshots ); 128 cgit_repo->snapshots );
129 return; 129 return;
130 } 130 }
131 131
132 if (cgit_cmd == CMD_PATCH) { 132 if (cgit_cmd == CMD_PATCH) {
133 cgit_print_patch(cgit_query_sha1, item); 133 cgit_print_patch(ctx.qry.sha1, item);
134 return; 134 return;
135 } 135 }
136 136
137 if (cgit_cmd == CMD_BLOB) { 137 if (cgit_cmd == CMD_BLOB) {
138 cgit_print_blob(item, cgit_query_sha1, cgit_query_path); 138 cgit_print_blob(item, ctx.qry.sha1, ctx.qry.path);
139 return; 139 return;
140 } 140 }
141 141
142 show_search = (cgit_cmd == CMD_LOG); 142 show_search = (cgit_cmd == CMD_LOG);
143 cgit_print_docstart(title, item); 143 cgit_print_docstart(title, item);
144 if (!cgit_cmd) { 144 if (!cgit_cmd) {
145 cgit_print_pageheader("summary", show_search); 145 cgit_print_pageheader("summary", show_search);
146 cgit_print_summary(); 146 cgit_print_summary();
147 cgit_print_docend(); 147 cgit_print_docend();
148 return; 148 return;
149 } 149 }
150 150
151 cgit_print_pageheader(cgit_query_page, show_search); 151 cgit_print_pageheader(ctx.qry.page, show_search);
152 152
153 switch(cgit_cmd) { 153 switch(cgit_cmd) {
154 case CMD_LOG: 154 case CMD_LOG:
155 cgit_print_log(cgit_query_sha1, cgit_query_ofs, 155 cgit_print_log(ctx.qry.sha1, ctx.qry.ofs,
156 cgit_max_commit_count, cgit_query_grep, cgit_query_search, 156 cgit_max_commit_count, ctx.qry.grep, ctx.qry.search,
157 cgit_query_path, 1); 157 ctx.qry.path, 1);
158 break; 158 break;
159 case CMD_TREE: 159 case CMD_TREE:
160 cgit_print_tree(cgit_query_sha1, cgit_query_path); 160 cgit_print_tree(ctx.qry.sha1, ctx.qry.path);
161 break; 161 break;
162 case CMD_COMMIT: 162 case CMD_COMMIT:
163 cgit_print_commit(cgit_query_sha1); 163 cgit_print_commit(ctx.qry.sha1);
164 break; 164 break;
165 case CMD_REFS: 165 case CMD_REFS:
166 cgit_print_refs(); 166 cgit_print_refs();
167 break; 167 break;
168 case CMD_TAG: 168 case CMD_TAG:
169 cgit_print_tag(cgit_query_sha1); 169 cgit_print_tag(ctx.qry.sha1);
170 break; 170 break;
171 case CMD_DIFF: 171 case CMD_DIFF:
172 cgit_print_diff(cgit_query_sha1, cgit_query_sha2, cgit_query_path); 172 cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path);
173 break; 173 break;
174 default: 174 default:
175 cgit_print_error("Invalid request"); 175 cgit_print_error("Invalid request");
176 } 176 }
177 cgit_print_docend(); 177 cgit_print_docend();
178} 178}
179 179
180static void cgit_fill_cache(struct cacheitem *item, int use_cache) 180static void cgit_fill_cache(struct cacheitem *item, int use_cache)
181{ 181{
182 static char buf[PATH_MAX]; 182 static char buf[PATH_MAX];
183 int stdout2; 183 int stdout2;
184 184
185 getcwd(buf, sizeof(buf)); 185 getcwd(buf, sizeof(buf));
186 item->st.st_mtime = time(NULL); 186 item->st.st_mtime = time(NULL);
187 187
188 if (use_cache) { 188 if (use_cache) {
189 stdout2 = chk_positive(dup(STDOUT_FILENO), 189 stdout2 = chk_positive(dup(STDOUT_FILENO),
190 "Preserving STDOUT"); 190 "Preserving STDOUT");
191 chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); 191 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
192 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); 192 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
193 } 193 }
194 194
195 if (cgit_repo) 195 if (cgit_repo)
196 cgit_print_repo_page(item); 196 cgit_print_repo_page(item);
197 else 197 else
198 cgit_print_repolist(item); 198 cgit_print_repolist(item);
199 199
200 if (use_cache) { 200 if (use_cache) {
201 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); 201 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
202 chk_positive(dup2(stdout2, STDOUT_FILENO), 202 chk_positive(dup2(stdout2, STDOUT_FILENO),
203 "Restoring original STDOUT"); 203 "Restoring original STDOUT");
204 chk_zero(close(stdout2), "Closing temporary STDOUT"); 204 chk_zero(close(stdout2), "Closing temporary STDOUT");
@@ -235,84 +235,84 @@ static void cgit_check_cache(struct cacheitem *item)
235 cache_cancel_lock(item); 235 cache_cancel_lock(item);
236 } 236 }
237 } 237 }
238} 238}
239 239
240static void cgit_print_cache(struct cacheitem *item) 240static void cgit_print_cache(struct cacheitem *item)
241{ 241{
242 static char buf[4096]; 242 static char buf[4096];
243 ssize_t i; 243 ssize_t i;
244 244
245 int fd = open(item->name, O_RDONLY); 245 int fd = open(item->name, O_RDONLY);
246 if (fd<0) 246 if (fd<0)
247 die("Unable to open cached file %s", item->name); 247 die("Unable to open cached file %s", item->name);
248 248
249 while((i=read(fd, buf, sizeof(buf))) > 0) 249 while((i=read(fd, buf, sizeof(buf))) > 0)
250 write(STDOUT_FILENO, buf, i); 250 write(STDOUT_FILENO, buf, i);
251 251
252 close(fd); 252 close(fd);
253} 253}
254 254
255static void cgit_parse_args(int argc, const char **argv) 255static void cgit_parse_args(int argc, const char **argv)
256{ 256{
257 int i; 257 int i;
258 258
259 for (i = 1; i < argc; i++) { 259 for (i = 1; i < argc; i++) {
260 if (!strncmp(argv[i], "--cache=", 8)) { 260 if (!strncmp(argv[i], "--cache=", 8)) {
261 cgit_cache_root = xstrdup(argv[i]+8); 261 cgit_cache_root = xstrdup(argv[i]+8);
262 } 262 }
263 if (!strcmp(argv[i], "--nocache")) { 263 if (!strcmp(argv[i], "--nocache")) {
264 cgit_nocache = 1; 264 cgit_nocache = 1;
265 } 265 }
266 if (!strncmp(argv[i], "--query=", 8)) { 266 if (!strncmp(argv[i], "--query=", 8)) {
267 cgit_querystring = xstrdup(argv[i]+8); 267 ctx.qry.raw = xstrdup(argv[i]+8);
268 } 268 }
269 if (!strncmp(argv[i], "--repo=", 7)) { 269 if (!strncmp(argv[i], "--repo=", 7)) {
270 cgit_query_repo = xstrdup(argv[i]+7); 270 ctx.qry.repo = xstrdup(argv[i]+7);
271 } 271 }
272 if (!strncmp(argv[i], "--page=", 7)) { 272 if (!strncmp(argv[i], "--page=", 7)) {
273 cgit_query_page = xstrdup(argv[i]+7); 273 ctx.qry.page = xstrdup(argv[i]+7);
274 } 274 }
275 if (!strncmp(argv[i], "--head=", 7)) { 275 if (!strncmp(argv[i], "--head=", 7)) {
276 cgit_query_head = xstrdup(argv[i]+7); 276 ctx.qry.head = xstrdup(argv[i]+7);
277 cgit_query_has_symref = 1; 277 ctx.qry.has_symref = 1;
278 } 278 }
279 if (!strncmp(argv[i], "--sha1=", 7)) { 279 if (!strncmp(argv[i], "--sha1=", 7)) {
280 cgit_query_sha1 = xstrdup(argv[i]+7); 280 ctx.qry.sha1 = xstrdup(argv[i]+7);
281 cgit_query_has_sha1 = 1; 281 ctx.qry.has_sha1 = 1;
282 } 282 }
283 if (!strncmp(argv[i], "--ofs=", 6)) { 283 if (!strncmp(argv[i], "--ofs=", 6)) {
284 cgit_query_ofs = atoi(argv[i]+6); 284 ctx.qry.ofs = atoi(argv[i]+6);
285 } 285 }
286 } 286 }
287} 287}
288 288
289int main(int argc, const char **argv) 289int main(int argc, const char **argv)
290{ 290{
291 struct cacheitem item; 291 struct cacheitem item;
292 const char *cgit_config_env = getenv("CGIT_CONFIG"); 292 const char *cgit_config_env = getenv("CGIT_CONFIG");
293 293
294 htmlfd = STDOUT_FILENO; 294 htmlfd = STDOUT_FILENO;
295 item.st.st_mtime = time(NULL); 295 item.st.st_mtime = time(NULL);
296 cgit_repolist.length = 0; 296 cgit_repolist.length = 0;
297 cgit_repolist.count = 0; 297 cgit_repolist.count = 0;
298 cgit_repolist.repos = NULL; 298 cgit_repolist.repos = NULL;
299 299
300 cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, 300 cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
301 cgit_global_config_cb); 301 cgit_global_config_cb);
302 cgit_repo = NULL; 302 cgit_repo = NULL;
303 if (getenv("SCRIPT_NAME")) 303 if (getenv("SCRIPT_NAME"))
304 cgit_script_name = xstrdup(getenv("SCRIPT_NAME")); 304 cgit_script_name = xstrdup(getenv("SCRIPT_NAME"));
305 if (getenv("QUERY_STRING")) 305 if (getenv("QUERY_STRING"))
306 cgit_querystring = xstrdup(getenv("QUERY_STRING")); 306 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
307 cgit_parse_args(argc, argv); 307 cgit_parse_args(argc, argv);
308 cgit_parse_query(cgit_querystring, cgit_querystring_cb); 308 cgit_parse_query(ctx.qry.raw, cgit_querystring_cb);
309 if (!cgit_prepare_cache(&item)) 309 if (!cgit_prepare_cache(&item))
310 return 0; 310 return 0;
311 if (cgit_nocache) { 311 if (cgit_nocache) {
312 cgit_fill_cache(&item, 0); 312 cgit_fill_cache(&item, 0);
313 } else { 313 } else {
314 cgit_check_cache(&item); 314 cgit_check_cache(&item);
315 cgit_print_cache(&item); 315 cgit_print_cache(&item);
316 } 316 }
317 return 0; 317 return 0;
318} 318}
diff --git a/cgit.h b/cgit.h
index 66c40b9..4576efb 100644
--- a/cgit.h
+++ b/cgit.h
@@ -94,118 +94,125 @@ struct commitinfo {
94 char *author_email; 94 char *author_email;
95 unsigned long author_date; 95 unsigned long author_date;
96 char *committer; 96 char *committer;
97 char *committer_email; 97 char *committer_email;
98 unsigned long committer_date; 98 unsigned long committer_date;
99 char *subject; 99 char *subject;
100 char *msg; 100 char *msg;
101 char *msg_encoding; 101 char *msg_encoding;
102}; 102};
103 103
104struct taginfo { 104struct taginfo {
105 char *tagger; 105 char *tagger;
106 char *tagger_email; 106 char *tagger_email;
107 int tagger_date; 107 int tagger_date;
108 char *msg; 108 char *msg;
109}; 109};
110 110
111struct refinfo { 111struct refinfo {
112 const char *refname; 112 const char *refname;
113 struct object *object; 113 struct object *object;
114 union { 114 union {
115 struct taginfo *tag; 115 struct taginfo *tag;
116 struct commitinfo *commit; 116 struct commitinfo *commit;
117 }; 117 };
118}; 118};
119 119
120struct reflist { 120struct reflist {
121 struct refinfo **refs; 121 struct refinfo **refs;
122 int alloc; 122 int alloc;
123 int count; 123 int count;
124}; 124};
125 125
126struct cgit_query {
127 int has_symref;
128 int has_sha1;
129 char *raw;
130 char *repo;
131 char *page;
132 char *search;
133 char *grep;
134 char *head;
135 char *sha1;
136 char *sha2;
137 char *path;
138 char *name;
139 int ofs;
140};
141
142struct cgit_context {
143 struct cgit_query qry;
144};
145
126extern const char *cgit_version; 146extern const char *cgit_version;
127 147
128extern struct repolist cgit_repolist; 148extern struct repolist cgit_repolist;
129extern struct repoinfo *cgit_repo; 149extern struct repoinfo *cgit_repo;
150extern struct cgit_context ctx;
130extern int cgit_cmd; 151extern int cgit_cmd;
131 152
132extern char *cgit_root_title; 153extern char *cgit_root_title;
133extern char *cgit_css; 154extern char *cgit_css;
134extern char *cgit_logo; 155extern char *cgit_logo;
135extern char *cgit_index_header; 156extern char *cgit_index_header;
136extern char *cgit_index_info; 157extern char *cgit_index_info;
137extern char *cgit_logo_link; 158extern char *cgit_logo_link;
138extern char *cgit_module_link; 159extern char *cgit_module_link;
139extern char *cgit_agefile; 160extern char *cgit_agefile;
140extern char *cgit_virtual_root; 161extern char *cgit_virtual_root;
141extern char *cgit_script_name; 162extern char *cgit_script_name;
142extern char *cgit_cache_root; 163extern char *cgit_cache_root;
143extern char *cgit_repo_group; 164extern char *cgit_repo_group;
144extern char *cgit_robots; 165extern char *cgit_robots;
145extern char *cgit_clone_prefix; 166extern char *cgit_clone_prefix;
146 167
147extern int cgit_nocache; 168extern int cgit_nocache;
148extern int cgit_snapshots; 169extern int cgit_snapshots;
149extern int cgit_enable_index_links; 170extern int cgit_enable_index_links;
150extern int cgit_enable_log_filecount; 171extern int cgit_enable_log_filecount;
151extern int cgit_enable_log_linecount; 172extern int cgit_enable_log_linecount;
152extern int cgit_max_lock_attempts; 173extern int cgit_max_lock_attempts;
153extern int cgit_cache_root_ttl; 174extern int cgit_cache_root_ttl;
154extern int cgit_cache_repo_ttl; 175extern int cgit_cache_repo_ttl;
155extern int cgit_cache_dynamic_ttl; 176extern int cgit_cache_dynamic_ttl;
156extern int cgit_cache_static_ttl; 177extern int cgit_cache_static_ttl;
157extern int cgit_cache_max_create_time; 178extern int cgit_cache_max_create_time;
158extern int cgit_summary_log; 179extern int cgit_summary_log;
159extern int cgit_summary_tags; 180extern int cgit_summary_tags;
160extern int cgit_summary_branches; 181extern int cgit_summary_branches;
161 182
162extern int cgit_max_msg_len; 183extern int cgit_max_msg_len;
163extern int cgit_max_repodesc_len; 184extern int cgit_max_repodesc_len;
164extern int cgit_max_commit_count; 185extern int cgit_max_commit_count;
165 186
166extern int cgit_query_has_symref;
167extern int cgit_query_has_sha1;
168
169extern char *cgit_querystring;
170extern char *cgit_query_repo;
171extern char *cgit_query_page;
172extern char *cgit_query_search;
173extern char *cgit_query_grep;
174extern char *cgit_query_head;
175extern char *cgit_query_sha1;
176extern char *cgit_query_sha2;
177extern char *cgit_query_path;
178extern char *cgit_query_name;
179extern int cgit_query_ofs;
180 187
181extern int htmlfd; 188extern int htmlfd;
182 189
183extern int cgit_get_cmd_index(const char *cmd); 190extern int cgit_get_cmd_index(const char *cmd);
184extern struct repoinfo *cgit_get_repoinfo(const char *url); 191extern struct repoinfo *cgit_get_repoinfo(const char *url);
185extern void cgit_global_config_cb(const char *name, const char *value); 192extern void cgit_global_config_cb(const char *name, const char *value);
186extern void cgit_repo_config_cb(const char *name, const char *value); 193extern void cgit_repo_config_cb(const char *name, const char *value);
187extern void cgit_querystring_cb(const char *name, const char *value); 194extern void cgit_querystring_cb(const char *name, const char *value);
188 195
189extern int chk_zero(int result, char *msg); 196extern int chk_zero(int result, char *msg);
190extern int chk_positive(int result, char *msg); 197extern int chk_positive(int result, char *msg);
191extern int chk_non_negative(int result, char *msg); 198extern int chk_non_negative(int result, char *msg);
192 199
193extern int hextoint(char c); 200extern int hextoint(char c);
194extern char *trim_end(const char *str, char c); 201extern char *trim_end(const char *str, char c);
195extern char *strlpart(char *txt, int maxlen); 202extern char *strlpart(char *txt, int maxlen);
196extern char *strrpart(char *txt, int maxlen); 203extern char *strrpart(char *txt, int maxlen);
197 204
198extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); 205extern void cgit_add_ref(struct reflist *list, struct refinfo *ref);
199extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, 206extern int cgit_refs_cb(const char *refname, const unsigned char *sha1,
200 int flags, void *cb_data); 207 int flags, void *cb_data);
201 208
202extern void *cgit_free_commitinfo(struct commitinfo *info); 209extern void *cgit_free_commitinfo(struct commitinfo *info);
203 210
204extern int cgit_diff_files(const unsigned char *old_sha1, 211extern int cgit_diff_files(const unsigned char *old_sha1,
205 const unsigned char *new_sha1, 212 const unsigned char *new_sha1,
206 linediff_fn fn); 213 linediff_fn fn);
207 214
208extern void cgit_diff_tree(const unsigned char *old_sha1, 215extern void cgit_diff_tree(const unsigned char *old_sha1,
209 const unsigned char *new_sha1, 216 const unsigned char *new_sha1,
210 filepair_fn fn, const char *prefix); 217 filepair_fn fn, const char *prefix);
211 218
diff --git a/parsing.c b/parsing.c
index 5093b8b..8cf56a4 100644
--- a/parsing.c
+++ b/parsing.c
@@ -120,87 +120,87 @@ int cgit_parse_query(char *txt, configfn fn)
120 } else if (c=='%') { 120 } else if (c=='%') {
121 t = convert_query_hexchar(t); 121 t = convert_query_hexchar(t);
122 } else if (c=='&') { 122 } else if (c=='&') {
123 *t = '\0'; 123 *t = '\0';
124 (*fn)(txt, value); 124 (*fn)(txt, value);
125 txt = t+1; 125 txt = t+1;
126 value = NULL; 126 value = NULL;
127 } 127 }
128 t++; 128 t++;
129 } 129 }
130 if (t!=txt) 130 if (t!=txt)
131 (*fn)(txt, value); 131 (*fn)(txt, value);
132 return 0; 132 return 0;
133} 133}
134 134
135/* 135/*
136 * url syntax: [repo ['/' cmd [ '/' path]]] 136 * url syntax: [repo ['/' cmd [ '/' path]]]
137 * repo: any valid repo url, may contain '/' 137 * repo: any valid repo url, may contain '/'
138 * cmd: log | commit | diff | tree | view | blob | snapshot 138 * cmd: log | commit | diff | tree | view | blob | snapshot
139 * path: any valid path, may contain '/' 139 * path: any valid path, may contain '/'
140 * 140 *
141 */ 141 */
142void cgit_parse_url(const char *url) 142void cgit_parse_url(const char *url)
143{ 143{
144 char *cmd, *p; 144 char *cmd, *p;
145 145
146 cgit_repo = NULL; 146 cgit_repo = NULL;
147 if (!url || url[0] == '\0') 147 if (!url || url[0] == '\0')
148 return; 148 return;
149 149
150 cgit_repo = cgit_get_repoinfo(url); 150 cgit_repo = cgit_get_repoinfo(url);
151 if (cgit_repo) { 151 if (cgit_repo) {
152 cgit_query_repo = cgit_repo->url; 152 ctx.qry.repo = cgit_repo->url;
153 return; 153 return;
154 } 154 }
155 155
156 cmd = strchr(url, '/'); 156 cmd = strchr(url, '/');
157 while (!cgit_repo && cmd) { 157 while (!cgit_repo && cmd) {
158 cmd[0] = '\0'; 158 cmd[0] = '\0';
159 cgit_repo = cgit_get_repoinfo(url); 159 cgit_repo = cgit_get_repoinfo(url);
160 if (cgit_repo == NULL) { 160 if (cgit_repo == NULL) {
161 cmd[0] = '/'; 161 cmd[0] = '/';
162 cmd = strchr(cmd + 1, '/'); 162 cmd = strchr(cmd + 1, '/');
163 continue; 163 continue;
164 } 164 }
165 165
166 cgit_query_repo = cgit_repo->url; 166 ctx.qry.repo = cgit_repo->url;
167 p = strchr(cmd + 1, '/'); 167 p = strchr(cmd + 1, '/');
168 if (p) { 168 if (p) {
169 p[0] = '\0'; 169 p[0] = '\0';
170 if (p[1]) 170 if (p[1])
171 cgit_query_path = trim_end(p + 1, '/'); 171 ctx.qry.path = trim_end(p + 1, '/');
172 } 172 }
173 cgit_cmd = cgit_get_cmd_index(cmd + 1); 173 cgit_cmd = cgit_get_cmd_index(cmd + 1);
174 cgit_query_page = xstrdup(cmd + 1); 174 ctx.qry.page = xstrdup(cmd + 1);
175 return; 175 return;
176 } 176 }
177} 177}
178 178
179char *substr(const char *head, const char *tail) 179char *substr(const char *head, const char *tail)
180{ 180{
181 char *buf; 181 char *buf;
182 182
183 buf = xmalloc(tail - head + 1); 183 buf = xmalloc(tail - head + 1);
184 strncpy(buf, head, tail - head); 184 strncpy(buf, head, tail - head);
185 buf[tail - head] = '\0'; 185 buf[tail - head] = '\0';
186 return buf; 186 return buf;
187} 187}
188 188
189struct commitinfo *cgit_parse_commit(struct commit *commit) 189struct commitinfo *cgit_parse_commit(struct commit *commit)
190{ 190{
191 struct commitinfo *ret; 191 struct commitinfo *ret;
192 char *p = commit->buffer, *t = commit->buffer; 192 char *p = commit->buffer, *t = commit->buffer;
193 193
194 ret = xmalloc(sizeof(*ret)); 194 ret = xmalloc(sizeof(*ret));
195 ret->commit = commit; 195 ret->commit = commit;
196 ret->author = NULL; 196 ret->author = NULL;
197 ret->author_email = NULL; 197 ret->author_email = NULL;
198 ret->committer = NULL; 198 ret->committer = NULL;
199 ret->committer_email = NULL; 199 ret->committer_email = NULL;
200 ret->subject = NULL; 200 ret->subject = NULL;
201 ret->msg = NULL; 201 ret->msg = NULL;
202 ret->msg_encoding = NULL; 202 ret->msg_encoding = NULL;
203 203
204 if (p == NULL) 204 if (p == NULL)
205 return ret; 205 return ret;
206 206
diff --git a/shared.c b/shared.c
index f063894..6c1a762 100644
--- a/shared.c
+++ b/shared.c
@@ -1,101 +1,86 @@
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
11struct repolist cgit_repolist; 11struct repolist cgit_repolist;
12struct repoinfo *cgit_repo; 12struct repoinfo *cgit_repo;
13struct cgit_context ctx;
13int cgit_cmd; 14int cgit_cmd;
14 15
15const char *cgit_version = CGIT_VERSION; 16const char *cgit_version = CGIT_VERSION;
16 17
17char *cgit_root_title = "Git repository browser"; 18char *cgit_root_title = "Git repository browser";
18char *cgit_css = "/cgit.css"; 19char *cgit_css = "/cgit.css";
19char *cgit_logo = "/git-logo.png"; 20char *cgit_logo = "/git-logo.png";
20char *cgit_index_header = NULL; 21char *cgit_index_header = NULL;
21char *cgit_index_info = NULL; 22char *cgit_index_info = NULL;
22char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; 23char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/";
23char *cgit_module_link = "./?repo=%s&page=commit&id=%s"; 24char *cgit_module_link = "./?repo=%s&page=commit&id=%s";
24char *cgit_agefile = "info/web/last-modified"; 25char *cgit_agefile = "info/web/last-modified";
25char *cgit_virtual_root = NULL; 26char *cgit_virtual_root = NULL;
26char *cgit_script_name = CGIT_SCRIPT_NAME; 27char *cgit_script_name = CGIT_SCRIPT_NAME;
27char *cgit_cache_root = CGIT_CACHE_ROOT; 28char *cgit_cache_root = CGIT_CACHE_ROOT;
28char *cgit_repo_group = NULL; 29char *cgit_repo_group = NULL;
29char *cgit_robots = "index, nofollow"; 30char *cgit_robots = "index, nofollow";
30char *cgit_clone_prefix = NULL; 31char *cgit_clone_prefix = NULL;
31 32
32int cgit_nocache = 0; 33int cgit_nocache = 0;
33int cgit_snapshots = 0; 34int cgit_snapshots = 0;
34int cgit_enable_index_links = 0; 35int cgit_enable_index_links = 0;
35int cgit_enable_log_filecount = 0; 36int cgit_enable_log_filecount = 0;
36int cgit_enable_log_linecount = 0; 37int cgit_enable_log_linecount = 0;
37int cgit_max_lock_attempts = 5; 38int cgit_max_lock_attempts = 5;
38int cgit_cache_root_ttl = 5; 39int cgit_cache_root_ttl = 5;
39int cgit_cache_repo_ttl = 5; 40int cgit_cache_repo_ttl = 5;
40int cgit_cache_dynamic_ttl = 5; 41int cgit_cache_dynamic_ttl = 5;
41int cgit_cache_static_ttl = -1; 42int cgit_cache_static_ttl = -1;
42int cgit_cache_max_create_time = 5; 43int cgit_cache_max_create_time = 5;
43int cgit_summary_log = 0; 44int cgit_summary_log = 0;
44int cgit_summary_tags = 0; 45int cgit_summary_tags = 0;
45int cgit_summary_branches = 0; 46int cgit_summary_branches = 0;
46int cgit_renamelimit = -1; 47int cgit_renamelimit = -1;
47 48
48int cgit_max_msg_len = 60; 49int cgit_max_msg_len = 60;
49int cgit_max_repodesc_len = 60; 50int cgit_max_repodesc_len = 60;
50int cgit_max_commit_count = 50; 51int cgit_max_commit_count = 50;
51 52
52int cgit_query_has_symref = 0;
53int cgit_query_has_sha1 = 0;
54
55char *cgit_querystring = NULL;
56char *cgit_query_repo = NULL;
57char *cgit_query_page = NULL;
58char *cgit_query_head = NULL;
59char *cgit_query_search = NULL;
60char *cgit_query_grep = NULL;
61char *cgit_query_sha1 = NULL;
62char *cgit_query_sha2 = NULL;
63char *cgit_query_path = NULL;
64char *cgit_query_name = NULL;
65int cgit_query_ofs = 0;
66
67int htmlfd = 0; 53int htmlfd = 0;
68 54
69
70int cgit_get_cmd_index(const char *cmd) 55int cgit_get_cmd_index(const char *cmd)
71{ 56{
72 static char *cmds[] = {"log", "commit", "diff", "tree", "blob", 57 static char *cmds[] = {"log", "commit", "diff", "tree", "blob",
73 "snapshot", "tag", "refs", "patch", NULL}; 58 "snapshot", "tag", "refs", "patch", NULL};
74 int i; 59 int i;
75 60
76 for(i = 0; cmds[i]; i++) 61 for(i = 0; cmds[i]; i++)
77 if (!strcmp(cmd, cmds[i])) 62 if (!strcmp(cmd, cmds[i]))
78 return i + 1; 63 return i + 1;
79 return 0; 64 return 0;
80} 65}
81 66
82int chk_zero(int result, char *msg) 67int chk_zero(int result, char *msg)
83{ 68{
84 if (result != 0) 69 if (result != 0)
85 die("%s: %s", msg, strerror(errno)); 70 die("%s: %s", msg, strerror(errno));
86 return result; 71 return result;
87} 72}
88 73
89int chk_positive(int result, char *msg) 74int chk_positive(int result, char *msg)
90{ 75{
91 if (result <= 0) 76 if (result <= 0)
92 die("%s: %s", msg, strerror(errno)); 77 die("%s: %s", msg, strerror(errno));
93 return result; 78 return result;
94} 79}
95 80
96int chk_non_negative(int result, char *msg) 81int chk_non_negative(int result, char *msg)
97{ 82{
98 if (result < 0) 83 if (result < 0)
99 die("%s: %s",msg, strerror(errno)); 84 die("%s: %s",msg, strerror(errno));
100 return result; 85 return result;
101} 86}
@@ -210,90 +195,90 @@ void cgit_global_config_cb(const char *name, const char *value)
210 else if (!strcmp(name, "repo.name")) 195 else if (!strcmp(name, "repo.name"))
211 cgit_repo->name = xstrdup(value); 196 cgit_repo->name = xstrdup(value);
212 else if (cgit_repo && !strcmp(name, "repo.path")) 197 else if (cgit_repo && !strcmp(name, "repo.path"))
213 cgit_repo->path = trim_end(value, '/'); 198 cgit_repo->path = trim_end(value, '/');
214 else if (cgit_repo && !strcmp(name, "repo.clone-url")) 199 else if (cgit_repo && !strcmp(name, "repo.clone-url"))
215 cgit_repo->clone_url = xstrdup(value); 200 cgit_repo->clone_url = xstrdup(value);
216 else if (cgit_repo && !strcmp(name, "repo.desc")) 201 else if (cgit_repo && !strcmp(name, "repo.desc"))
217 cgit_repo->desc = xstrdup(value); 202 cgit_repo->desc = xstrdup(value);
218 else if (cgit_repo && !strcmp(name, "repo.owner")) 203 else if (cgit_repo && !strcmp(name, "repo.owner"))
219 cgit_repo->owner = xstrdup(value); 204 cgit_repo->owner = xstrdup(value);
220 else if (cgit_repo && !strcmp(name, "repo.defbranch")) 205 else if (cgit_repo && !strcmp(name, "repo.defbranch"))
221 cgit_repo->defbranch = xstrdup(value); 206 cgit_repo->defbranch = xstrdup(value);
222 else if (cgit_repo && !strcmp(name, "repo.snapshots")) 207 else if (cgit_repo && !strcmp(name, "repo.snapshots"))
223 cgit_repo->snapshots = cgit_snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ 208 cgit_repo->snapshots = cgit_snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
224 else if (cgit_repo && !strcmp(name, "repo.enable-log-filecount")) 209 else if (cgit_repo && !strcmp(name, "repo.enable-log-filecount"))
225 cgit_repo->enable_log_filecount = cgit_enable_log_filecount * atoi(value); 210 cgit_repo->enable_log_filecount = cgit_enable_log_filecount * atoi(value);
226 else if (cgit_repo && !strcmp(name, "repo.enable-log-linecount")) 211 else if (cgit_repo && !strcmp(name, "repo.enable-log-linecount"))
227 cgit_repo->enable_log_linecount = cgit_enable_log_linecount * atoi(value); 212 cgit_repo->enable_log_linecount = cgit_enable_log_linecount * atoi(value);
228 else if (cgit_repo && !strcmp(name, "repo.module-link")) 213 else if (cgit_repo && !strcmp(name, "repo.module-link"))
229 cgit_repo->module_link= xstrdup(value); 214 cgit_repo->module_link= xstrdup(value);
230 else if (cgit_repo && !strcmp(name, "repo.readme") && value != NULL) { 215 else if (cgit_repo && !strcmp(name, "repo.readme") && value != NULL) {
231 if (*value == '/') 216 if (*value == '/')
232 cgit_repo->readme = xstrdup(value); 217 cgit_repo->readme = xstrdup(value);
233 else 218 else
234 cgit_repo->readme = xstrdup(fmt("%s/%s", cgit_repo->path, value)); 219 cgit_repo->readme = xstrdup(fmt("%s/%s", cgit_repo->path, value));
235 } else if (!strcmp(name, "include")) 220 } else if (!strcmp(name, "include"))
236 cgit_read_config(value, cgit_global_config_cb); 221 cgit_read_config(value, cgit_global_config_cb);
237} 222}
238 223
239void cgit_querystring_cb(const char *name, const char *value) 224void cgit_querystring_cb(const char *name, const char *value)
240{ 225{
241 if (!strcmp(name,"r")) { 226 if (!strcmp(name,"r")) {
242 cgit_query_repo = xstrdup(value); 227 ctx.qry.repo = xstrdup(value);
243 cgit_repo = cgit_get_repoinfo(value); 228 cgit_repo = cgit_get_repoinfo(value);
244 } else if (!strcmp(name, "p")) { 229 } else if (!strcmp(name, "p")) {
245 cgit_query_page = xstrdup(value); 230 ctx.qry.page = xstrdup(value);
246 cgit_cmd = cgit_get_cmd_index(value); 231 cgit_cmd = cgit_get_cmd_index(value);
247 } else if (!strcmp(name, "url")) { 232 } else if (!strcmp(name, "url")) {
248 cgit_parse_url(value); 233 cgit_parse_url(value);
249 } else if (!strcmp(name, "qt")) { 234 } else if (!strcmp(name, "qt")) {
250 cgit_query_grep = xstrdup(value); 235 ctx.qry.grep = xstrdup(value);
251 } else if (!strcmp(name, "q")) { 236 } else if (!strcmp(name, "q")) {
252 cgit_query_search = xstrdup(value); 237 ctx.qry.search = xstrdup(value);
253 } else if (!strcmp(name, "h")) { 238 } else if (!strcmp(name, "h")) {
254 cgit_query_head = xstrdup(value); 239 ctx.qry.head = xstrdup(value);
255 cgit_query_has_symref = 1; 240 ctx.qry.has_symref = 1;
256 } else if (!strcmp(name, "id")) { 241 } else if (!strcmp(name, "id")) {
257 cgit_query_sha1 = xstrdup(value); 242 ctx.qry.sha1 = xstrdup(value);
258 cgit_query_has_sha1 = 1; 243 ctx.qry.has_sha1 = 1;
259 } else if (!strcmp(name, "id2")) { 244 } else if (!strcmp(name, "id2")) {
260 cgit_query_sha2 = xstrdup(value); 245 ctx.qry.sha2 = xstrdup(value);
261 cgit_query_has_sha1 = 1; 246 ctx.qry.has_sha1 = 1;
262 } else if (!strcmp(name, "ofs")) { 247 } else if (!strcmp(name, "ofs")) {
263 cgit_query_ofs = atoi(value); 248 ctx.qry.ofs = atoi(value);
264 } else if (!strcmp(name, "path")) { 249 } else if (!strcmp(name, "path")) {
265 cgit_query_path = trim_end(value, '/'); 250 ctx.qry.path = trim_end(value, '/');
266 } else if (!strcmp(name, "name")) { 251 } else if (!strcmp(name, "name")) {
267 cgit_query_name = xstrdup(value); 252 ctx.qry.name = xstrdup(value);
268 } 253 }
269} 254}
270 255
271void *cgit_free_commitinfo(struct commitinfo *info) 256void *cgit_free_commitinfo(struct commitinfo *info)
272{ 257{
273 free(info->author); 258 free(info->author);
274 free(info->author_email); 259 free(info->author_email);
275 free(info->committer); 260 free(info->committer);
276 free(info->committer_email); 261 free(info->committer_email);
277 free(info->subject); 262 free(info->subject);
278 free(info->msg); 263 free(info->msg);
279 free(info->msg_encoding); 264 free(info->msg_encoding);
280 free(info); 265 free(info);
281 return NULL; 266 return NULL;
282} 267}
283 268
284int hextoint(char c) 269int hextoint(char c)
285{ 270{
286 if (c >= 'a' && c <= 'f') 271 if (c >= 'a' && c <= 'f')
287 return 10 + c - 'a'; 272 return 10 + c - 'a';
288 else if (c >= 'A' && c <= 'F') 273 else if (c >= 'A' && c <= 'F')
289 return 10 + c - 'A'; 274 return 10 + c - 'A';
290 else if (c >= '0' && c <= '9') 275 else if (c >= '0' && c <= '9')
291 return c - '0'; 276 return c - '0';
292 else 277 else
293 return -1; 278 return -1;
294} 279}
295 280
296char *trim_end(const char *str, char c) 281char *trim_end(const char *str, char c)
297{ 282{
298 int len; 283 int len;
299 char *s, *t; 284 char *s, *t;
diff --git a/ui-commit.c b/ui-commit.c
index bd55a33..3b0919b 100644
--- a/ui-commit.c
+++ b/ui-commit.c
@@ -46,65 +46,65 @@ void print_fileinfo(struct fileinfo *info)
46 case DIFF_STATUS_RENAMED: 46 case DIFF_STATUS_RENAMED:
47 class = "mov"; 47 class = "mov";
48 break; 48 break;
49 case DIFF_STATUS_TYPE_CHANGED: 49 case DIFF_STATUS_TYPE_CHANGED:
50 class = "typ"; 50 class = "typ";
51 break; 51 break;
52 case DIFF_STATUS_UNKNOWN: 52 case DIFF_STATUS_UNKNOWN:
53 class = "unk"; 53 class = "unk";
54 break; 54 break;
55 case DIFF_STATUS_UNMERGED: 55 case DIFF_STATUS_UNMERGED:
56 class = "stg"; 56 class = "stg";
57 break; 57 break;
58 default: 58 default:
59 die("bug: unhandled diff status %c", info->status); 59 die("bug: unhandled diff status %c", info->status);
60 } 60 }
61 61
62 html("<tr>"); 62 html("<tr>");
63 htmlf("<td class='mode'>"); 63 htmlf("<td class='mode'>");
64 if (is_null_sha1(info->new_sha1)) { 64 if (is_null_sha1(info->new_sha1)) {
65 html_filemode(info->old_mode); 65 html_filemode(info->old_mode);
66 } else { 66 } else {
67 html_filemode(info->new_mode); 67 html_filemode(info->new_mode);
68 } 68 }
69 69
70 if (info->old_mode != info->new_mode && 70 if (info->old_mode != info->new_mode &&
71 !is_null_sha1(info->old_sha1) && 71 !is_null_sha1(info->old_sha1) &&
72 !is_null_sha1(info->new_sha1)) { 72 !is_null_sha1(info->new_sha1)) {
73 html("<span class='modechange'>["); 73 html("<span class='modechange'>[");
74 html_filemode(info->old_mode); 74 html_filemode(info->old_mode);
75 html("]</span>"); 75 html("]</span>");
76 } 76 }
77 htmlf("</td><td class='%s'>", class); 77 htmlf("</td><td class='%s'>", class);
78 cgit_diff_link(info->new_path, NULL, NULL, cgit_query_head, curr_rev, 78 cgit_diff_link(info->new_path, NULL, NULL, ctx.qry.head, curr_rev,
79 NULL, info->new_path); 79 NULL, info->new_path);
80 if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED) 80 if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED)
81 htmlf(" (%s from %s)", 81 htmlf(" (%s from %s)",
82 info->status == DIFF_STATUS_COPIED ? "copied" : "renamed", 82 info->status == DIFF_STATUS_COPIED ? "copied" : "renamed",
83 info->old_path); 83 info->old_path);
84 html("</td><td class='right'>"); 84 html("</td><td class='right'>");
85 htmlf("%d", info->added + info->removed); 85 htmlf("%d", info->added + info->removed);
86 html("</td><td class='graph'>"); 86 html("</td><td class='graph'>");
87 htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes > 100 ? 100 : max_changes)); 87 htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes > 100 ? 100 : max_changes));
88 htmlf("<td class='add' style='width: %.1f%%;'/>", 88 htmlf("<td class='add' style='width: %.1f%%;'/>",
89 info->added * 100.0 / max_changes); 89 info->added * 100.0 / max_changes);
90 htmlf("<td class='rem' style='width: %.1f%%;'/>", 90 htmlf("<td class='rem' style='width: %.1f%%;'/>",
91 info->removed * 100.0 / max_changes); 91 info->removed * 100.0 / max_changes);
92 htmlf("<td class='none' style='width: %.1f%%;'/>", 92 htmlf("<td class='none' style='width: %.1f%%;'/>",
93 (max_changes - info->removed - info->added) * 100.0 / max_changes); 93 (max_changes - info->removed - info->added) * 100.0 / max_changes);
94 html("</tr></table></td></tr>\n"); 94 html("</tr></table></td></tr>\n");
95} 95}
96 96
97void cgit_count_diff_lines(char *line, int len) 97void cgit_count_diff_lines(char *line, int len)
98{ 98{
99 if (line && (len > 0)) { 99 if (line && (len > 0)) {
100 if (line[0] == '+') 100 if (line[0] == '+')
101 lines_added++; 101 lines_added++;
102 else if (line[0] == '-') 102 else if (line[0] == '-')
103 lines_removed++; 103 lines_removed++;
104 } 104 }
105} 105}
106 106
107void inspect_filepair(struct diff_filepair *pair) 107void inspect_filepair(struct diff_filepair *pair)
108{ 108{
109 files++; 109 files++;
110 lines_added = 0; 110 lines_added = 0;
@@ -114,113 +114,113 @@ void inspect_filepair(struct diff_filepair *pair)
114 if (slots == 0) 114 if (slots == 0)
115 slots = 4; 115 slots = 4;
116 else 116 else
117 slots = slots * 2; 117 slots = slots * 2;
118 items = xrealloc(items, slots * sizeof(struct fileinfo)); 118 items = xrealloc(items, slots * sizeof(struct fileinfo));
119 } 119 }
120 items[files-1].status = pair->status; 120 items[files-1].status = pair->status;
121 hashcpy(items[files-1].old_sha1, pair->one->sha1); 121 hashcpy(items[files-1].old_sha1, pair->one->sha1);
122 hashcpy(items[files-1].new_sha1, pair->two->sha1); 122 hashcpy(items[files-1].new_sha1, pair->two->sha1);
123 items[files-1].old_mode = pair->one->mode; 123 items[files-1].old_mode = pair->one->mode;
124 items[files-1].new_mode = pair->two->mode; 124 items[files-1].new_mode = pair->two->mode;
125 items[files-1].old_path = xstrdup(pair->one->path); 125 items[files-1].old_path = xstrdup(pair->one->path);
126 items[files-1].new_path = xstrdup(pair->two->path); 126 items[files-1].new_path = xstrdup(pair->two->path);
127 items[files-1].added = lines_added; 127 items[files-1].added = lines_added;
128 items[files-1].removed = lines_removed; 128 items[files-1].removed = lines_removed;
129 if (lines_added + lines_removed > max_changes) 129 if (lines_added + lines_removed > max_changes)
130 max_changes = lines_added + lines_removed; 130 max_changes = lines_added + lines_removed;
131 total_adds += lines_added; 131 total_adds += lines_added;
132 total_rems += lines_removed; 132 total_rems += lines_removed;
133} 133}
134 134
135 135
136void cgit_print_commit(char *hex) 136void cgit_print_commit(char *hex)
137{ 137{
138 struct commit *commit, *parent; 138 struct commit *commit, *parent;
139 struct commitinfo *info; 139 struct commitinfo *info;
140 struct commit_list *p; 140 struct commit_list *p;
141 unsigned char sha1[20]; 141 unsigned char sha1[20];
142 char *tmp; 142 char *tmp;
143 int i; 143 int i;
144 144
145 if (!hex) 145 if (!hex)
146 hex = cgit_query_head; 146 hex = ctx.qry.head;
147 curr_rev = hex; 147 curr_rev = hex;
148 148
149 if (get_sha1(hex, sha1)) { 149 if (get_sha1(hex, sha1)) {
150 cgit_print_error(fmt("Bad object id: %s", hex)); 150 cgit_print_error(fmt("Bad object id: %s", hex));
151 return; 151 return;
152 } 152 }
153 commit = lookup_commit_reference(sha1); 153 commit = lookup_commit_reference(sha1);
154 if (!commit) { 154 if (!commit) {
155 cgit_print_error(fmt("Bad commit reference: %s", hex)); 155 cgit_print_error(fmt("Bad commit reference: %s", hex));
156 return; 156 return;
157 } 157 }
158 info = cgit_parse_commit(commit); 158 info = cgit_parse_commit(commit);
159 159
160 html("<table summary='commit info' class='commit-info'>\n"); 160 html("<table summary='commit info' class='commit-info'>\n");
161 html("<tr><th>author</th><td>"); 161 html("<tr><th>author</th><td>");
162 html_txt(info->author); 162 html_txt(info->author);
163 html(" "); 163 html(" ");
164 html_txt(info->author_email); 164 html_txt(info->author_email);
165 html("</td><td class='right'>"); 165 html("</td><td class='right'>");
166 cgit_print_date(info->author_date, FMT_LONGDATE); 166 cgit_print_date(info->author_date, FMT_LONGDATE);
167 html("</td></tr>\n"); 167 html("</td></tr>\n");
168 html("<tr><th>committer</th><td>"); 168 html("<tr><th>committer</th><td>");
169 html_txt(info->committer); 169 html_txt(info->committer);
170 html(" "); 170 html(" ");
171 html_txt(info->committer_email); 171 html_txt(info->committer_email);
172 html("</td><td class='right'>"); 172 html("</td><td class='right'>");
173 cgit_print_date(info->committer_date, FMT_LONGDATE); 173 cgit_print_date(info->committer_date, FMT_LONGDATE);
174 html("</td></tr>\n"); 174 html("</td></tr>\n");
175 html("<tr><th>tree</th><td colspan='2' class='sha1'>"); 175 html("<tr><th>tree</th><td colspan='2' class='sha1'>");
176 tmp = xstrdup(hex); 176 tmp = xstrdup(hex);
177 cgit_tree_link(sha1_to_hex(commit->tree->object.sha1), NULL, NULL, 177 cgit_tree_link(sha1_to_hex(commit->tree->object.sha1), NULL, NULL,
178 cgit_query_head, tmp, NULL); 178 ctx.qry.head, tmp, NULL);
179 html("</td></tr>\n"); 179 html("</td></tr>\n");
180 for (p = commit->parents; p ; p = p->next) { 180 for (p = commit->parents; p ; p = p->next) {
181 parent = lookup_commit_reference(p->item->object.sha1); 181 parent = lookup_commit_reference(p->item->object.sha1);
182 if (!parent) { 182 if (!parent) {
183 html("<tr><td colspan='3'>"); 183 html("<tr><td colspan='3'>");
184 cgit_print_error("Error reading parent commit"); 184 cgit_print_error("Error reading parent commit");
185 html("</td></tr>"); 185 html("</td></tr>");
186 continue; 186 continue;
187 } 187 }
188 html("<tr><th>parent</th>" 188 html("<tr><th>parent</th>"
189 "<td colspan='2' class='sha1'>"); 189 "<td colspan='2' class='sha1'>");
190 cgit_commit_link(sha1_to_hex(p->item->object.sha1), NULL, NULL, 190 cgit_commit_link(sha1_to_hex(p->item->object.sha1), NULL, NULL,
191 cgit_query_head, sha1_to_hex(p->item->object.sha1)); 191 ctx.qry.head, sha1_to_hex(p->item->object.sha1));
192 html(" ("); 192 html(" (");
193 cgit_diff_link("diff", NULL, NULL, cgit_query_head, hex, 193 cgit_diff_link("diff", NULL, NULL, ctx.qry.head, hex,
194 sha1_to_hex(p->item->object.sha1), NULL); 194 sha1_to_hex(p->item->object.sha1), NULL);
195 html(")</td></tr>"); 195 html(")</td></tr>");
196 } 196 }
197 if (cgit_repo->snapshots) { 197 if (cgit_repo->snapshots) {
198 html("<tr><th>download</th><td colspan='2' class='sha1'>"); 198 html("<tr><th>download</th><td colspan='2' class='sha1'>");
199 cgit_print_snapshot_links(cgit_query_repo, cgit_query_head, 199 cgit_print_snapshot_links(ctx.qry.repo, ctx.qry.head,
200 hex, cgit_repo->snapshots); 200 hex, cgit_repo->snapshots);
201 html("</td></tr>"); 201 html("</td></tr>");
202 } 202 }
203 html("</table>\n"); 203 html("</table>\n");
204 html("<div class='commit-subject'>"); 204 html("<div class='commit-subject'>");
205 html_txt(info->subject); 205 html_txt(info->subject);
206 html("</div>"); 206 html("</div>");
207 html("<div class='commit-msg'>"); 207 html("<div class='commit-msg'>");
208 html_txt(info->msg); 208 html_txt(info->msg);
209 html("</div>"); 209 html("</div>");
210 if (!(commit->parents && commit->parents->next && commit->parents->next->next)) { 210 if (!(commit->parents && commit->parents->next && commit->parents->next->next)) {
211 html("<div class='diffstat-header'>Diffstat</div>"); 211 html("<div class='diffstat-header'>Diffstat</div>");
212 html("<table summary='diffstat' class='diffstat'>"); 212 html("<table summary='diffstat' class='diffstat'>");
213 max_changes = 0; 213 max_changes = 0;
214 cgit_diff_commit(commit, inspect_filepair); 214 cgit_diff_commit(commit, inspect_filepair);
215 for(i = 0; i<files; i++) 215 for(i = 0; i<files; i++)
216 print_fileinfo(&items[i]); 216 print_fileinfo(&items[i]);
217 html("</table>"); 217 html("</table>");
218 html("<div class='diffstat-summary'>"); 218 html("<div class='diffstat-summary'>");
219 htmlf("%d files changed, %d insertions, %d deletions (", 219 htmlf("%d files changed, %d insertions, %d deletions (",
220 files, total_adds, total_rems); 220 files, total_adds, total_rems);
221 cgit_diff_link("show diff", NULL, NULL, cgit_query_head, hex, 221 cgit_diff_link("show diff", NULL, NULL, ctx.qry.head, hex,
222 NULL, NULL); 222 NULL, NULL);
223 html(")</div>"); 223 html(")</div>");
224 } 224 }
225 cgit_free_commitinfo(info); 225 cgit_free_commitinfo(info);
226} 226}
diff --git a/ui-diff.c b/ui-diff.c
index 4fcf852..263a7ba 100644
--- a/ui-diff.c
+++ b/ui-diff.c
@@ -42,101 +42,101 @@ static void header(unsigned char *sha1, char *path1, int mode1,
42 42
43 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2)); 43 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
44 html("<div class='head'>"); 44 html("<div class='head'>");
45 html("diff --git a/"); 45 html("diff --git a/");
46 html_txt(path1); 46 html_txt(path1);
47 html(" b/"); 47 html(" b/");
48 html_txt(path2); 48 html_txt(path2);
49 49
50 if (is_null_sha1(sha1)) 50 if (is_null_sha1(sha1))
51 path1 = "dev/null"; 51 path1 = "dev/null";
52 if (is_null_sha1(sha2)) 52 if (is_null_sha1(sha2))
53 path2 = "dev/null"; 53 path2 = "dev/null";
54 54
55 if (mode1 == 0) 55 if (mode1 == 0)
56 htmlf("<br/>new file mode %.6o", mode2); 56 htmlf("<br/>new file mode %.6o", mode2);
57 57
58 if (mode2 == 0) 58 if (mode2 == 0)
59 htmlf("<br/>deleted file mode %.6o", mode1); 59 htmlf("<br/>deleted file mode %.6o", mode1);
60 60
61 if (!subproject) { 61 if (!subproject) {
62 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); 62 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
63 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV)); 63 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
64 htmlf("<br/>index %s..%s", abbrev1, abbrev2); 64 htmlf("<br/>index %s..%s", abbrev1, abbrev2);
65 free(abbrev1); 65 free(abbrev1);
66 free(abbrev2); 66 free(abbrev2);
67 if (mode1 != 0 && mode2 != 0) { 67 if (mode1 != 0 && mode2 != 0) {
68 htmlf(" %.6o", mode1); 68 htmlf(" %.6o", mode1);
69 if (mode2 != mode1) 69 if (mode2 != mode1)
70 htmlf("..%.6o", mode2); 70 htmlf("..%.6o", mode2);
71 } 71 }
72 html("<br/>--- a/"); 72 html("<br/>--- a/");
73 if (mode1 != 0) 73 if (mode1 != 0)
74 cgit_tree_link(path1, NULL, NULL, cgit_query_head, 74 cgit_tree_link(path1, NULL, NULL, ctx.qry.head,
75 sha1_to_hex(old_rev_sha1), path1); 75 sha1_to_hex(old_rev_sha1), path1);
76 else 76 else
77 html_txt(path1); 77 html_txt(path1);
78 html("<br/>+++ b/"); 78 html("<br/>+++ b/");
79 if (mode2 != 0) 79 if (mode2 != 0)
80 cgit_tree_link(path2, NULL, NULL, cgit_query_head, 80 cgit_tree_link(path2, NULL, NULL, ctx.qry.head,
81 sha1_to_hex(new_rev_sha1), path2); 81 sha1_to_hex(new_rev_sha1), path2);
82 else 82 else
83 html_txt(path2); 83 html_txt(path2);
84 } 84 }
85 html("</div>"); 85 html("</div>");
86} 86}
87 87
88static void filepair_cb(struct diff_filepair *pair) 88static void filepair_cb(struct diff_filepair *pair)
89{ 89{
90 header(pair->one->sha1, pair->one->path, pair->one->mode, 90 header(pair->one->sha1, pair->one->path, pair->one->mode,
91 pair->two->sha1, pair->two->path, pair->two->mode); 91 pair->two->sha1, pair->two->path, pair->two->mode);
92 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { 92 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
93 if (S_ISGITLINK(pair->one->mode)) 93 if (S_ISGITLINK(pair->one->mode))
94 print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52); 94 print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
95 if (S_ISGITLINK(pair->two->mode)) 95 if (S_ISGITLINK(pair->two->mode))
96 print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52); 96 print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
97 return; 97 return;
98 } 98 }
99 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line)) 99 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line))
100 cgit_print_error("Error running diff"); 100 cgit_print_error("Error running diff");
101} 101}
102 102
103void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix) 103void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix)
104{ 104{
105 enum object_type type; 105 enum object_type type;
106 unsigned long size; 106 unsigned long size;
107 struct commit *commit, *commit2; 107 struct commit *commit, *commit2;
108 108
109 if (!new_rev) 109 if (!new_rev)
110 new_rev = cgit_query_head; 110 new_rev = ctx.qry.head;
111 get_sha1(new_rev, new_rev_sha1); 111 get_sha1(new_rev, new_rev_sha1);
112 type = sha1_object_info(new_rev_sha1, &size); 112 type = sha1_object_info(new_rev_sha1, &size);
113 if (type == OBJ_BAD) { 113 if (type == OBJ_BAD) {
114 cgit_print_error(fmt("Bad object name: %s", new_rev)); 114 cgit_print_error(fmt("Bad object name: %s", new_rev));
115 return; 115 return;
116 } 116 }
117 if (type != OBJ_COMMIT) { 117 if (type != OBJ_COMMIT) {
118 cgit_print_error(fmt("Unhandled object type: %s", 118 cgit_print_error(fmt("Unhandled object type: %s",
119 typename(type))); 119 typename(type)));
120 return; 120 return;
121 } 121 }
122 122
123 commit = lookup_commit_reference(new_rev_sha1); 123 commit = lookup_commit_reference(new_rev_sha1);
124 if (!commit || parse_commit(commit)) 124 if (!commit || parse_commit(commit))
125 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1))); 125 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1)));
126 126
127 if (old_rev) 127 if (old_rev)
128 get_sha1(old_rev, old_rev_sha1); 128 get_sha1(old_rev, old_rev_sha1);
129 else if (commit->parents && commit->parents->item) 129 else if (commit->parents && commit->parents->item)
130 hashcpy(old_rev_sha1, commit->parents->item->object.sha1); 130 hashcpy(old_rev_sha1, commit->parents->item->object.sha1);
131 else 131 else
132 hashclr(old_rev_sha1); 132 hashclr(old_rev_sha1);
133 133
134 if (!is_null_sha1(old_rev_sha1)) { 134 if (!is_null_sha1(old_rev_sha1)) {
135 type = sha1_object_info(old_rev_sha1, &size); 135 type = sha1_object_info(old_rev_sha1, &size);
136 if (type == OBJ_BAD) { 136 if (type == OBJ_BAD) {
137 cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1))); 137 cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1)));
138 return; 138 return;
139 } 139 }
140 commit2 = lookup_commit_reference(old_rev_sha1); 140 commit2 = lookup_commit_reference(old_rev_sha1);
141 if (!commit2 || parse_commit(commit2)) 141 if (!commit2 || parse_commit(commit2))
142 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1))); 142 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1)));
diff --git a/ui-log.c b/ui-log.c
index a41d2b2..a39474b 100644
--- a/ui-log.c
+++ b/ui-log.c
@@ -8,133 +8,133 @@
8 8
9#include "cgit.h" 9#include "cgit.h"
10 10
11int files, add_lines, rem_lines; 11int files, add_lines, rem_lines;
12 12
13void count_lines(char *line, int size) 13void count_lines(char *line, int size)
14{ 14{
15 if (size <= 0) 15 if (size <= 0)
16 return; 16 return;
17 17
18 if (line[0] == '+') 18 if (line[0] == '+')
19 add_lines++; 19 add_lines++;
20 20
21 else if (line[0] == '-') 21 else if (line[0] == '-')
22 rem_lines++; 22 rem_lines++;
23} 23}
24 24
25void inspect_files(struct diff_filepair *pair) 25void inspect_files(struct diff_filepair *pair)
26{ 26{
27 files++; 27 files++;
28 if (cgit_repo->enable_log_linecount) 28 if (cgit_repo->enable_log_linecount)
29 cgit_diff_files(pair->one->sha1, pair->two->sha1, count_lines); 29 cgit_diff_files(pair->one->sha1, pair->two->sha1, count_lines);
30} 30}
31 31
32void print_commit(struct commit *commit) 32void print_commit(struct commit *commit)
33{ 33{
34 struct commitinfo *info; 34 struct commitinfo *info;
35 35
36 info = cgit_parse_commit(commit); 36 info = cgit_parse_commit(commit);
37 html("<tr><td>"); 37 html("<tr><td>");
38 cgit_print_age(commit->date, TM_WEEK * 2, FMT_SHORTDATE); 38 cgit_print_age(commit->date, TM_WEEK * 2, FMT_SHORTDATE);
39 html("</td><td>"); 39 html("</td><td>");
40 cgit_commit_link(info->subject, NULL, NULL, cgit_query_head, 40 cgit_commit_link(info->subject, NULL, NULL, ctx.qry.head,
41 sha1_to_hex(commit->object.sha1)); 41 sha1_to_hex(commit->object.sha1));
42 if (cgit_repo->enable_log_filecount) { 42 if (cgit_repo->enable_log_filecount) {
43 files = 0; 43 files = 0;
44 add_lines = 0; 44 add_lines = 0;
45 rem_lines = 0; 45 rem_lines = 0;
46 cgit_diff_commit(commit, inspect_files); 46 cgit_diff_commit(commit, inspect_files);
47 html("</td><td class='right'>"); 47 html("</td><td class='right'>");
48 htmlf("%d", files); 48 htmlf("%d", files);
49 if (cgit_repo->enable_log_linecount) { 49 if (cgit_repo->enable_log_linecount) {
50 html("</td><td class='right'>"); 50 html("</td><td class='right'>");
51 htmlf("-%d/+%d", rem_lines, add_lines); 51 htmlf("-%d/+%d", rem_lines, add_lines);
52 } 52 }
53 } 53 }
54 html("</td><td>"); 54 html("</td><td>");
55 html_txt(info->author); 55 html_txt(info->author);
56 html("</td></tr>\n"); 56 html("</td></tr>\n");
57 cgit_free_commitinfo(info); 57 cgit_free_commitinfo(info);
58} 58}
59 59
60 60
61void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern, char *path, int pager) 61void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern, char *path, int pager)
62{ 62{
63 struct rev_info rev; 63 struct rev_info rev;
64 struct commit *commit; 64 struct commit *commit;
65 const char *argv[] = {NULL, tip, NULL, NULL, NULL}; 65 const char *argv[] = {NULL, tip, NULL, NULL, NULL};
66 int argc = 2; 66 int argc = 2;
67 int i; 67 int i;
68 68
69 if (!tip) 69 if (!tip)
70 argv[1] = cgit_query_head; 70 argv[1] = ctx.qry.head;
71 71
72 if (grep && pattern && (!strcmp(grep, "grep") || 72 if (grep && pattern && (!strcmp(grep, "grep") ||
73 !strcmp(grep, "author") || 73 !strcmp(grep, "author") ||
74 !strcmp(grep, "committer"))) 74 !strcmp(grep, "committer")))
75 argv[argc++] = fmt("--%s=%s", grep, pattern); 75 argv[argc++] = fmt("--%s=%s", grep, pattern);
76 76
77 if (path) { 77 if (path) {
78 argv[argc++] = "--"; 78 argv[argc++] = "--";
79 argv[argc++] = path; 79 argv[argc++] = path;
80 } 80 }
81 init_revisions(&rev, NULL); 81 init_revisions(&rev, NULL);
82 rev.abbrev = DEFAULT_ABBREV; 82 rev.abbrev = DEFAULT_ABBREV;
83 rev.commit_format = CMIT_FMT_DEFAULT; 83 rev.commit_format = CMIT_FMT_DEFAULT;
84 rev.verbose_header = 1; 84 rev.verbose_header = 1;
85 rev.show_root_diff = 0; 85 rev.show_root_diff = 0;
86 setup_revisions(argc, argv, &rev, NULL); 86 setup_revisions(argc, argv, &rev, NULL);
87 if (rev.grep_filter) { 87 if (rev.grep_filter) {
88 rev.grep_filter->regflags |= REG_ICASE; 88 rev.grep_filter->regflags |= REG_ICASE;
89 compile_grep_patterns(rev.grep_filter); 89 compile_grep_patterns(rev.grep_filter);
90 } 90 }
91 prepare_revision_walk(&rev); 91 prepare_revision_walk(&rev);
92 92
93 html("<table summary='log' class='list nowrap'>"); 93 html("<table summary='log' class='list nowrap'>");
94 html("<tr class='nohover'><th class='left'>Age</th>" 94 html("<tr class='nohover'><th class='left'>Age</th>"
95 "<th class='left'>Message</th>"); 95 "<th class='left'>Message</th>");
96 96
97 if (cgit_repo->enable_log_filecount) { 97 if (cgit_repo->enable_log_filecount) {
98 html("<th class='right'>Files</th>"); 98 html("<th class='right'>Files</th>");
99 if (cgit_repo->enable_log_linecount) 99 if (cgit_repo->enable_log_linecount)
100 html("<th class='right'>Lines</th>"); 100 html("<th class='right'>Lines</th>");
101 } 101 }
102 html("<th class='left'>Author</th></tr>\n"); 102 html("<th class='left'>Author</th></tr>\n");
103 103
104 if (ofs<0) 104 if (ofs<0)
105 ofs = 0; 105 ofs = 0;
106 106
107 for (i = 0; i < ofs && (commit = get_revision(&rev)) != NULL; i++) { 107 for (i = 0; i < ofs && (commit = get_revision(&rev)) != NULL; i++) {
108 free(commit->buffer); 108 free(commit->buffer);
109 commit->buffer = NULL; 109 commit->buffer = NULL;
110 free_commit_list(commit->parents); 110 free_commit_list(commit->parents);
111 commit->parents = NULL; 111 commit->parents = NULL;
112 } 112 }
113 113
114 for (i = 0; i < cnt && (commit = get_revision(&rev)) != NULL; i++) { 114 for (i = 0; i < cnt && (commit = get_revision(&rev)) != NULL; i++) {
115 print_commit(commit); 115 print_commit(commit);
116 free(commit->buffer); 116 free(commit->buffer);
117 commit->buffer = NULL; 117 commit->buffer = NULL;
118 free_commit_list(commit->parents); 118 free_commit_list(commit->parents);
119 commit->parents = NULL; 119 commit->parents = NULL;
120 } 120 }
121 html("</table>\n"); 121 html("</table>\n");
122 122
123 if (pager) { 123 if (pager) {
124 html("<div class='pager'>"); 124 html("<div class='pager'>");
125 if (ofs > 0) { 125 if (ofs > 0) {
126 cgit_log_link("[prev]", NULL, NULL, cgit_query_head, 126 cgit_log_link("[prev]", NULL, NULL, ctx.qry.head,
127 cgit_query_sha1, cgit_query_path, 127 ctx.qry.sha1, ctx.qry.path,
128 ofs - cnt, cgit_query_grep, 128 ofs - cnt, ctx.qry.grep,
129 cgit_query_search); 129 ctx.qry.search);
130 html("&nbsp;"); 130 html("&nbsp;");
131 } 131 }
132 if ((commit = get_revision(&rev)) != NULL) { 132 if ((commit = get_revision(&rev)) != NULL) {
133 cgit_log_link("[next]", NULL, NULL, cgit_query_head, 133 cgit_log_link("[next]", NULL, NULL, ctx.qry.head,
134 cgit_query_sha1, cgit_query_path, 134 ctx.qry.sha1, ctx.qry.path,
135 ofs + cnt, cgit_query_grep, 135 ofs + cnt, ctx.qry.grep,
136 cgit_query_search); 136 ctx.qry.search);
137 } 137 }
138 html("</div>"); 138 html("</div>");
139 } 139 }
140} 140}
diff --git a/ui-patch.c b/ui-patch.c
index e7a010a..1da0a27 100644
--- a/ui-patch.c
+++ b/ui-patch.c
@@ -47,64 +47,64 @@ static void header(unsigned char *sha1, char *path1, int mode1,
47 htmlf(" %.6o", mode1); 47 htmlf(" %.6o", mode1);
48 if (mode2 != mode1) 48 if (mode2 != mode1)
49 htmlf("..%.6o", mode2); 49 htmlf("..%.6o", mode2);
50 } 50 }
51 htmlf("\n--- a/%s\n", path1); 51 htmlf("\n--- a/%s\n", path1);
52 htmlf("+++ b/%s\n", path2); 52 htmlf("+++ b/%s\n", path2);
53 } 53 }
54} 54}
55 55
56static void filepair_cb(struct diff_filepair *pair) 56static void filepair_cb(struct diff_filepair *pair)
57{ 57{
58 header(pair->one->sha1, pair->one->path, pair->one->mode, 58 header(pair->one->sha1, pair->one->path, pair->one->mode,
59 pair->two->sha1, pair->two->path, pair->two->mode); 59 pair->two->sha1, pair->two->path, pair->two->mode);
60 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { 60 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
61 if (S_ISGITLINK(pair->one->mode)) 61 if (S_ISGITLINK(pair->one->mode))
62 print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52); 62 print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
63 if (S_ISGITLINK(pair->two->mode)) 63 if (S_ISGITLINK(pair->two->mode))
64 print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52); 64 print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
65 return; 65 return;
66 } 66 }
67 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line)) 67 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line))
68 html("Error running diff"); 68 html("Error running diff");
69} 69}
70 70
71void cgit_print_patch(char *hex, struct cacheitem *item) 71void cgit_print_patch(char *hex, struct cacheitem *item)
72{ 72{
73 struct commit *commit; 73 struct commit *commit;
74 struct commitinfo *info; 74 struct commitinfo *info;
75 unsigned char sha1[20], old_sha1[20]; 75 unsigned char sha1[20], old_sha1[20];
76 char *patchname; 76 char *patchname;
77 77
78 if (!hex) 78 if (!hex)
79 hex = cgit_query_head; 79 hex = ctx.qry.head;
80 80
81 if (get_sha1(hex, sha1)) { 81 if (get_sha1(hex, sha1)) {
82 cgit_print_error(fmt("Bad object id: %s", hex)); 82 cgit_print_error(fmt("Bad object id: %s", hex));
83 return; 83 return;
84 } 84 }
85 commit = lookup_commit_reference(sha1); 85 commit = lookup_commit_reference(sha1);
86 if (!commit) { 86 if (!commit) {
87 cgit_print_error(fmt("Bad commit reference: %s", hex)); 87 cgit_print_error(fmt("Bad commit reference: %s", hex));
88 return; 88 return;
89 } 89 }
90 info = cgit_parse_commit(commit); 90 info = cgit_parse_commit(commit);
91 hashcpy(old_sha1, commit->parents->item->object.sha1); 91 hashcpy(old_sha1, commit->parents->item->object.sha1);
92 92
93 patchname = fmt("%s.patch", sha1_to_hex(sha1)); 93 patchname = fmt("%s.patch", sha1_to_hex(sha1));
94 cgit_print_snapshot_start("text/plain", patchname, item); 94 cgit_print_snapshot_start("text/plain", patchname, item);
95 htmlf("From %s Mon Sep 17 00:00:00 2001\n", sha1_to_hex(sha1)); 95 htmlf("From %s Mon Sep 17 00:00:00 2001\n", sha1_to_hex(sha1));
96 htmlf("From: %s%s\n", info->author, info->author_email); 96 htmlf("From: %s%s\n", info->author, info->author_email);
97 html("Date: "); 97 html("Date: ");
98 cgit_print_date(info->author_date, "%a, %d %b %Y %H:%M:%S %z%n"); 98 cgit_print_date(info->author_date, "%a, %d %b %Y %H:%M:%S %z%n");
99 htmlf("Subject: %s\n\n", info->subject); 99 htmlf("Subject: %s\n\n", info->subject);
100 if (info->msg && *info->msg) { 100 if (info->msg && *info->msg) {
101 htmlf("%s", info->msg); 101 htmlf("%s", info->msg);
102 if (info->msg[strlen(info->msg) - 1] != '\n') 102 if (info->msg[strlen(info->msg) - 1] != '\n')
103 html("\n"); 103 html("\n");
104 } 104 }
105 html("---\n"); 105 html("---\n");
106 cgit_diff_tree(old_sha1, sha1, filepair_cb, NULL); 106 cgit_diff_tree(old_sha1, sha1, filepair_cb, NULL);
107 html("--\n"); 107 html("--\n");
108 htmlf("cgit %s\n", CGIT_VERSION); 108 htmlf("cgit %s\n", CGIT_VERSION);
109 cgit_free_commitinfo(info); 109 cgit_free_commitinfo(info);
110} 110}
diff --git a/ui-refs.c b/ui-refs.c
index 295f5ba..ba513c1 100644
--- a/ui-refs.c
+++ b/ui-refs.c
@@ -1,30 +1,30 @@
1/* ui-refs.c: browse symbolic refs 1/* ui-refs.c: browse symbolic refs
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 11
12 12
13 13
14void cgit_print_refs() 14void cgit_print_refs()
15{ 15{
16 16
17 html("<table class='list nowrap'>"); 17 html("<table class='list nowrap'>");
18 18
19 if (cgit_query_path && !strncmp(cgit_query_path, "heads", 5)) 19 if (ctx.qry.path && !strncmp(ctx.qry.path, "heads", 5))
20 cgit_print_branches(0); 20 cgit_print_branches(0);
21 else if (cgit_query_path && !strncmp(cgit_query_path, "tags", 4)) 21 else if (ctx.qry.path && !strncmp(ctx.qry.path, "tags", 4))
22 cgit_print_tags(0); 22 cgit_print_tags(0);
23 else { 23 else {
24 cgit_print_branches(0); 24 cgit_print_branches(0);
25 html("<tr class='nohover'><td colspan='4'>&nbsp;</td></tr>"); 25 html("<tr class='nohover'><td colspan='4'>&nbsp;</td></tr>");
26 cgit_print_tags(0); 26 cgit_print_tags(0);
27 } 27 }
28 28
29 html("</table>"); 29 html("</table>");
30} 30}
diff --git a/ui-shared.c b/ui-shared.c
index 60aa2e3..6a41fb0 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -83,250 +83,250 @@ char *cgit_pageurl(const char *reponame, const char *pagename,
83 return cgit_fileurl(reponame,pagename,0,query); 83 return cgit_fileurl(reponame,pagename,0,query);
84} 84}
85 85
86const char *cgit_repobasename(const char *reponame) 86const char *cgit_repobasename(const char *reponame)
87{ 87{
88 /* I assume we don't need to store more than one repo basename */ 88 /* I assume we don't need to store more than one repo basename */
89 static char rvbuf[1024]; 89 static char rvbuf[1024];
90 int p; 90 int p;
91 const char *rv; 91 const char *rv;
92 strncpy(rvbuf,reponame,sizeof(rvbuf)); 92 strncpy(rvbuf,reponame,sizeof(rvbuf));
93 if(rvbuf[sizeof(rvbuf)-1]) 93 if(rvbuf[sizeof(rvbuf)-1])
94 die("cgit_repobasename: truncated repository name '%s'", reponame); 94 die("cgit_repobasename: truncated repository name '%s'", reponame);
95 p = strlen(rvbuf)-1; 95 p = strlen(rvbuf)-1;
96 /* strip trailing slashes */ 96 /* strip trailing slashes */
97 while(p && rvbuf[p]=='/') rvbuf[p--]=0; 97 while(p && rvbuf[p]=='/') rvbuf[p--]=0;
98 /* strip trailing .git */ 98 /* strip trailing .git */
99 if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) { 99 if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) {
100 p -= 3; rvbuf[p--] = 0; 100 p -= 3; rvbuf[p--] = 0;
101 } 101 }
102 /* strip more trailing slashes if any */ 102 /* strip more trailing slashes if any */
103 while( p && rvbuf[p]=='/') rvbuf[p--]=0; 103 while( p && rvbuf[p]=='/') rvbuf[p--]=0;
104 /* find last slash in the remaining string */ 104 /* find last slash in the remaining string */
105 rv = strrchr(rvbuf,'/'); 105 rv = strrchr(rvbuf,'/');
106 if(rv) 106 if(rv)
107 return ++rv; 107 return ++rv;
108 return rvbuf; 108 return rvbuf;
109} 109}
110 110
111char *cgit_currurl() 111char *cgit_currurl()
112{ 112{
113 if (!cgit_virtual_root) 113 if (!cgit_virtual_root)
114 return cgit_script_name; 114 return cgit_script_name;
115 else if (cgit_query_page) 115 else if (ctx.qry.page)
116 return fmt("%s/%s/%s/", cgit_virtual_root, cgit_query_repo, cgit_query_page); 116 return fmt("%s/%s/%s/", cgit_virtual_root, ctx.qry.repo, ctx.qry.page);
117 else if (cgit_query_repo) 117 else if (ctx.qry.repo)
118 return fmt("%s/%s/", cgit_virtual_root, cgit_query_repo); 118 return fmt("%s/%s/", cgit_virtual_root, ctx.qry.repo);
119 else 119 else
120 return fmt("%s/", cgit_virtual_root); 120 return fmt("%s/", cgit_virtual_root);
121} 121}
122 122
123static char *repolink(char *title, char *class, char *page, char *head, 123static char *repolink(char *title, char *class, char *page, char *head,
124 char *path) 124 char *path)
125{ 125{
126 char *delim = "?"; 126 char *delim = "?";
127 127
128 html("<a"); 128 html("<a");
129 if (title) { 129 if (title) {
130 html(" title='"); 130 html(" title='");
131 html_attr(title); 131 html_attr(title);
132 html("'"); 132 html("'");
133 } 133 }
134 if (class) { 134 if (class) {
135 html(" class='"); 135 html(" class='");
136 html_attr(class); 136 html_attr(class);
137 html("'"); 137 html("'");
138 } 138 }
139 html(" href='"); 139 html(" href='");
140 if (cgit_virtual_root) { 140 if (cgit_virtual_root) {
141 html_attr(cgit_virtual_root); 141 html_attr(cgit_virtual_root);
142 if (cgit_virtual_root[strlen(cgit_virtual_root) - 1] != '/') 142 if (cgit_virtual_root[strlen(cgit_virtual_root) - 1] != '/')
143 html("/"); 143 html("/");
144 html_attr(cgit_repo->url); 144 html_attr(cgit_repo->url);
145 if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') 145 if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/')
146 html("/"); 146 html("/");
147 if (page) { 147 if (page) {
148 html(page); 148 html(page);
149 html("/"); 149 html("/");
150 if (path) 150 if (path)
151 html_attr(path); 151 html_attr(path);
152 } 152 }
153 } else { 153 } else {
154 html(cgit_script_name); 154 html(cgit_script_name);
155 html("?url="); 155 html("?url=");
156 html_attr(cgit_repo->url); 156 html_attr(cgit_repo->url);
157 if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') 157 if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/')
158 html("/"); 158 html("/");
159 if (page) { 159 if (page) {
160 html(page); 160 html(page);
161 html("/"); 161 html("/");
162 if (path) 162 if (path)
163 html_attr(path); 163 html_attr(path);
164 } 164 }
165 delim = "&amp;"; 165 delim = "&amp;";
166 } 166 }
167 if (head && strcmp(head, cgit_repo->defbranch)) { 167 if (head && strcmp(head, cgit_repo->defbranch)) {
168 html(delim); 168 html(delim);
169 html("h="); 169 html("h=");
170 html_attr(head); 170 html_attr(head);
171 delim = "&amp;"; 171 delim = "&amp;";
172 } 172 }
173 return fmt("%s", delim); 173 return fmt("%s", delim);
174} 174}
175 175
176static void reporevlink(char *page, char *name, char *title, char *class, 176static void reporevlink(char *page, char *name, char *title, char *class,
177 char *head, char *rev, char *path) 177 char *head, char *rev, char *path)
178{ 178{
179 char *delim; 179 char *delim;
180 180
181 delim = repolink(title, class, page, head, path); 181 delim = repolink(title, class, page, head, path);
182 if (rev && strcmp(rev, cgit_query_head)) { 182 if (rev && strcmp(rev, ctx.qry.head)) {
183 html(delim); 183 html(delim);
184 html("id="); 184 html("id=");
185 html_attr(rev); 185 html_attr(rev);
186 } 186 }
187 html("'>"); 187 html("'>");
188 html_txt(name); 188 html_txt(name);
189 html("</a>"); 189 html("</a>");
190} 190}
191 191
192void cgit_tree_link(char *name, char *title, char *class, char *head, 192void cgit_tree_link(char *name, char *title, char *class, char *head,
193 char *rev, char *path) 193 char *rev, char *path)
194{ 194{
195 reporevlink("tree", name, title, class, head, rev, path); 195 reporevlink("tree", name, title, class, head, rev, path);
196} 196}
197 197
198void cgit_log_link(char *name, char *title, char *class, char *head, 198void cgit_log_link(char *name, char *title, char *class, char *head,
199 char *rev, char *path, int ofs, char *grep, char *pattern) 199 char *rev, char *path, int ofs, char *grep, char *pattern)
200{ 200{
201 char *delim; 201 char *delim;
202 202
203 delim = repolink(title, class, "log", head, path); 203 delim = repolink(title, class, "log", head, path);
204 if (rev && strcmp(rev, cgit_query_head)) { 204 if (rev && strcmp(rev, ctx.qry.head)) {
205 html(delim); 205 html(delim);
206 html("id="); 206 html("id=");
207 html_attr(rev); 207 html_attr(rev);
208 delim = "&"; 208 delim = "&";
209 } 209 }
210 if (grep && pattern) { 210 if (grep && pattern) {
211 html(delim); 211 html(delim);
212 html("qt="); 212 html("qt=");
213 html_attr(grep); 213 html_attr(grep);
214 delim = "&"; 214 delim = "&";
215 html(delim); 215 html(delim);
216 html("q="); 216 html("q=");
217 html_attr(pattern); 217 html_attr(pattern);
218 } 218 }
219 if (ofs > 0) { 219 if (ofs > 0) {
220 html(delim); 220 html(delim);
221 html("ofs="); 221 html("ofs=");
222 htmlf("%d", ofs); 222 htmlf("%d", ofs);
223 } 223 }
224 html("'>"); 224 html("'>");
225 html_txt(name); 225 html_txt(name);
226 html("</a>"); 226 html("</a>");
227} 227}
228 228
229void cgit_commit_link(char *name, char *title, char *class, char *head, 229void cgit_commit_link(char *name, char *title, char *class, char *head,
230 char *rev) 230 char *rev)
231{ 231{
232 if (strlen(name) > cgit_max_msg_len && cgit_max_msg_len >= 15) { 232 if (strlen(name) > cgit_max_msg_len && cgit_max_msg_len >= 15) {
233 name[cgit_max_msg_len] = '\0'; 233 name[cgit_max_msg_len] = '\0';
234 name[cgit_max_msg_len - 1] = '.'; 234 name[cgit_max_msg_len - 1] = '.';
235 name[cgit_max_msg_len - 2] = '.'; 235 name[cgit_max_msg_len - 2] = '.';
236 name[cgit_max_msg_len - 3] = '.'; 236 name[cgit_max_msg_len - 3] = '.';
237 } 237 }
238 reporevlink("commit", name, title, class, head, rev, NULL); 238 reporevlink("commit", name, title, class, head, rev, NULL);
239} 239}
240 240
241void cgit_refs_link(char *name, char *title, char *class, char *head, 241void cgit_refs_link(char *name, char *title, char *class, char *head,
242 char *rev, char *path) 242 char *rev, char *path)
243{ 243{
244 reporevlink("refs", name, title, class, head, rev, path); 244 reporevlink("refs", name, title, class, head, rev, path);
245} 245}
246 246
247void cgit_snapshot_link(char *name, char *title, char *class, char *head, 247void cgit_snapshot_link(char *name, char *title, char *class, char *head,
248 char *rev, char *archivename) 248 char *rev, char *archivename)
249{ 249{
250 reporevlink("snapshot", name, title, class, head, rev, archivename); 250 reporevlink("snapshot", name, title, class, head, rev, archivename);
251} 251}
252 252
253void cgit_diff_link(char *name, char *title, char *class, char *head, 253void cgit_diff_link(char *name, char *title, char *class, char *head,
254 char *new_rev, char *old_rev, char *path) 254 char *new_rev, char *old_rev, char *path)
255{ 255{
256 char *delim; 256 char *delim;
257 257
258 delim = repolink(title, class, "diff", head, path); 258 delim = repolink(title, class, "diff", head, path);
259 if (new_rev && strcmp(new_rev, cgit_query_head)) { 259 if (new_rev && strcmp(new_rev, ctx.qry.head)) {
260 html(delim); 260 html(delim);
261 html("id="); 261 html("id=");
262 html_attr(new_rev); 262 html_attr(new_rev);
263 delim = "&amp;"; 263 delim = "&amp;";
264 } 264 }
265 if (old_rev) { 265 if (old_rev) {
266 html(delim); 266 html(delim);
267 html("id2="); 267 html("id2=");
268 html_attr(old_rev); 268 html_attr(old_rev);
269 } 269 }
270 html("'>"); 270 html("'>");
271 html_txt(name); 271 html_txt(name);
272 html("</a>"); 272 html("</a>");
273} 273}
274 274
275void cgit_patch_link(char *name, char *title, char *class, char *head, 275void cgit_patch_link(char *name, char *title, char *class, char *head,
276 char *rev) 276 char *rev)
277{ 277{
278 reporevlink("patch", name, title, class, head, rev, NULL); 278 reporevlink("patch", name, title, class, head, rev, NULL);
279} 279}
280 280
281void cgit_object_link(struct object *obj) 281void cgit_object_link(struct object *obj)
282{ 282{
283 char *page, *arg, *url; 283 char *page, *arg, *url;
284 284
285 if (obj->type == OBJ_COMMIT) { 285 if (obj->type == OBJ_COMMIT) {
286 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL, 286 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL,
287 cgit_query_head, sha1_to_hex(obj->sha1)); 287 ctx.qry.head, sha1_to_hex(obj->sha1));
288 return; 288 return;
289 } else if (obj->type == OBJ_TREE) { 289 } else if (obj->type == OBJ_TREE) {
290 page = "tree"; 290 page = "tree";
291 arg = "id"; 291 arg = "id";
292 } else if (obj->type == OBJ_TAG) { 292 } else if (obj->type == OBJ_TAG) {
293 page = "tag"; 293 page = "tag";
294 arg = "id"; 294 arg = "id";
295 } else { 295 } else {
296 page = "blob"; 296 page = "blob";
297 arg = "id"; 297 arg = "id";
298 } 298 }
299 299
300 url = cgit_pageurl(cgit_query_repo, page, 300 url = cgit_pageurl(ctx.qry.repo, page,
301 fmt("%s=%s", arg, sha1_to_hex(obj->sha1))); 301 fmt("%s=%s", arg, sha1_to_hex(obj->sha1)));
302 html_link_open(url, NULL, NULL); 302 html_link_open(url, NULL, NULL);
303 htmlf("%s %s", typename(obj->type), 303 htmlf("%s %s", typename(obj->type),
304 sha1_to_hex(obj->sha1)); 304 sha1_to_hex(obj->sha1));
305 html_link_close(); 305 html_link_close();
306} 306}
307 307
308void cgit_print_date(time_t secs, char *format) 308void cgit_print_date(time_t secs, char *format)
309{ 309{
310 char buf[64]; 310 char buf[64];
311 struct tm *time; 311 struct tm *time;
312 312
313 if (!secs) 313 if (!secs)
314 return; 314 return;
315 time = gmtime(&secs); 315 time = gmtime(&secs);
316 strftime(buf, sizeof(buf)-1, format, time); 316 strftime(buf, sizeof(buf)-1, format, time);
317 html_txt(buf); 317 html_txt(buf);
318} 318}
319 319
320void cgit_print_age(time_t t, time_t max_relative, char *format) 320void cgit_print_age(time_t t, time_t max_relative, char *format)
321{ 321{
322 time_t now, secs; 322 time_t now, secs;
323 323
324 if (!t) 324 if (!t)
325 return; 325 return;
326 time(&now); 326 time(&now);
327 secs = now - t; 327 secs = now - t;
328 328
329 if (secs > max_relative && max_relative >= 0) { 329 if (secs > max_relative && max_relative >= 0) {
330 cgit_print_date(t, format); 330 cgit_print_date(t, format);
331 return; 331 return;
332 } 332 }
@@ -363,208 +363,208 @@ void cgit_print_age(time_t t, time_t max_relative, char *format)
363void cgit_print_docstart(char *title, struct cacheitem *item) 363void cgit_print_docstart(char *title, struct cacheitem *item)
364{ 364{
365 html("Content-Type: text/html; charset=" PAGE_ENCODING "\n"); 365 html("Content-Type: text/html; charset=" PAGE_ENCODING "\n");
366 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); 366 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime));
367 htmlf("Expires: %s\n", http_date(item->st.st_mtime + 367 htmlf("Expires: %s\n", http_date(item->st.st_mtime +
368 ttl_seconds(item->ttl))); 368 ttl_seconds(item->ttl)));
369 html("\n"); 369 html("\n");
370 html(cgit_doctype); 370 html(cgit_doctype);
371 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); 371 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
372 html("<head>\n"); 372 html("<head>\n");
373 html("<title>"); 373 html("<title>");
374 html_txt(title); 374 html_txt(title);
375 html("</title>\n"); 375 html("</title>\n");
376 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); 376 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
377 if (cgit_robots && *cgit_robots) 377 if (cgit_robots && *cgit_robots)
378 htmlf("<meta name='robots' content='%s'/>\n", cgit_robots); 378 htmlf("<meta name='robots' content='%s'/>\n", cgit_robots);
379 html("<link rel='stylesheet' type='text/css' href='"); 379 html("<link rel='stylesheet' type='text/css' href='");
380 html_attr(cgit_css); 380 html_attr(cgit_css);
381 html("'/>\n"); 381 html("'/>\n");
382 html("</head>\n"); 382 html("</head>\n");
383 html("<body>\n"); 383 html("<body>\n");
384} 384}
385 385
386void cgit_print_docend() 386void cgit_print_docend()
387{ 387{
388 html("</td>\n</tr>\n</table>\n</body>\n</html>\n"); 388 html("</td>\n</tr>\n</table>\n</body>\n</html>\n");
389} 389}
390 390
391int print_branch_option(const char *refname, const unsigned char *sha1, 391int print_branch_option(const char *refname, const unsigned char *sha1,
392 int flags, void *cb_data) 392 int flags, void *cb_data)
393{ 393{
394 char *name = (char *)refname; 394 char *name = (char *)refname;
395 html_option(name, name, cgit_query_head); 395 html_option(name, name, ctx.qry.head);
396 return 0; 396 return 0;
397} 397}
398 398
399int print_archive_ref(const char *refname, const unsigned char *sha1, 399int print_archive_ref(const char *refname, const unsigned char *sha1,
400 int flags, void *cb_data) 400 int flags, void *cb_data)
401{ 401{
402 struct tag *tag; 402 struct tag *tag;
403 struct taginfo *info; 403 struct taginfo *info;
404 struct object *obj; 404 struct object *obj;
405 char buf[256], *url; 405 char buf[256], *url;
406 unsigned char fileid[20]; 406 unsigned char fileid[20];
407 int *header = (int *)cb_data; 407 int *header = (int *)cb_data;
408 408
409 if (prefixcmp(refname, "refs/archives")) 409 if (prefixcmp(refname, "refs/archives"))
410 return 0; 410 return 0;
411 strncpy(buf, refname+14, sizeof(buf)); 411 strncpy(buf, refname+14, sizeof(buf));
412 obj = parse_object(sha1); 412 obj = parse_object(sha1);
413 if (!obj) 413 if (!obj)
414 return 1; 414 return 1;
415 if (obj->type == OBJ_TAG) { 415 if (obj->type == OBJ_TAG) {
416 tag = lookup_tag(sha1); 416 tag = lookup_tag(sha1);
417 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) 417 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag)))
418 return 0; 418 return 0;
419 hashcpy(fileid, tag->tagged->sha1); 419 hashcpy(fileid, tag->tagged->sha1);
420 } else if (obj->type != OBJ_BLOB) { 420 } else if (obj->type != OBJ_BLOB) {
421 return 0; 421 return 0;
422 } else { 422 } else {
423 hashcpy(fileid, sha1); 423 hashcpy(fileid, sha1);
424 } 424 }
425 if (!*header) { 425 if (!*header) {
426 html("<h1>download</h1>\n"); 426 html("<h1>download</h1>\n");
427 *header = 1; 427 *header = 1;
428 } 428 }
429 url = cgit_pageurl(cgit_query_repo, "blob", 429 url = cgit_pageurl(ctx.qry.repo, "blob",
430 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid), 430 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid),
431 buf)); 431 buf));
432 html_link_open(url, NULL, "menu"); 432 html_link_open(url, NULL, "menu");
433 html_txt(strlpart(buf, 20)); 433 html_txt(strlpart(buf, 20));
434 html_link_close(); 434 html_link_close();
435 return 0; 435 return 0;
436} 436}
437 437
438void add_hidden_formfields(int incl_head, int incl_search, char *page) 438void add_hidden_formfields(int incl_head, int incl_search, char *page)
439{ 439{
440 char *url; 440 char *url;
441 441
442 if (!cgit_virtual_root) { 442 if (!cgit_virtual_root) {
443 url = fmt("%s/%s", cgit_query_repo, page); 443 url = fmt("%s/%s", ctx.qry.repo, page);
444 if (cgit_query_path) 444 if (ctx.qry.path)
445 url = fmt("%s/%s", url, cgit_query_path); 445 url = fmt("%s/%s", url, ctx.qry.path);
446 html_hidden("url", url); 446 html_hidden("url", url);
447 } 447 }
448 448
449 if (incl_head && strcmp(cgit_query_head, cgit_repo->defbranch)) 449 if (incl_head && strcmp(ctx.qry.head, cgit_repo->defbranch))
450 html_hidden("h", cgit_query_head); 450 html_hidden("h", ctx.qry.head);
451 451
452 if (cgit_query_sha1) 452 if (ctx.qry.sha1)
453 html_hidden("id", cgit_query_sha1); 453 html_hidden("id", ctx.qry.sha1);
454 if (cgit_query_sha2) 454 if (ctx.qry.sha2)
455 html_hidden("id2", cgit_query_sha2); 455 html_hidden("id2", ctx.qry.sha2);
456 456
457 if (incl_search) { 457 if (incl_search) {
458 if (cgit_query_grep) 458 if (ctx.qry.grep)
459 html_hidden("qt", cgit_query_grep); 459 html_hidden("qt", ctx.qry.grep);
460 if (cgit_query_search) 460 if (ctx.qry.search)
461 html_hidden("q", cgit_query_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 cgit_logo); 477 cgit_logo);
478 html("</td></tr>\n<tr><td class='sidebar'>\n"); 478 html("</td></tr>\n<tr><td class='sidebar'>\n");
479 if (cgit_query_repo) { 479 if (ctx.qry.repo) {
480 html("<h1 class='first'>"); 480 html("<h1 class='first'>");
481 html_txt(strrpart(cgit_repo->name, 20)); 481 html_txt(strrpart(cgit_repo->name, 20));
482 html("</h1>\n"); 482 html("</h1>\n");
483 html_txt(cgit_repo->desc); 483 html_txt(cgit_repo->desc);
484 if (cgit_repo->owner) { 484 if (cgit_repo->owner) {
485 html("<h1>owner</h1>\n"); 485 html("<h1>owner</h1>\n");
486 html_txt(cgit_repo->owner); 486 html_txt(cgit_repo->owner);
487 } 487 }
488 html("<h1>navigate</h1>\n"); 488 html("<h1>navigate</h1>\n");
489 reporevlink(NULL, "summary", NULL, "menu", cgit_query_head, 489 reporevlink(NULL, "summary", NULL, "menu", ctx.qry.head,
490 NULL, NULL); 490 NULL, NULL);
491 cgit_log_link("log", NULL, "menu", cgit_query_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", cgit_query_head, 493 cgit_tree_link("tree", NULL, "menu", ctx.qry.head,
494 cgit_query_sha1, NULL); 494 ctx.qry.sha1, NULL);
495 cgit_commit_link("commit", NULL, "menu", cgit_query_head, 495 cgit_commit_link("commit", NULL, "menu", ctx.qry.head,
496 cgit_query_sha1); 496 ctx.qry.sha1);
497 cgit_diff_link("diff", NULL, "menu", cgit_query_head, 497 cgit_diff_link("diff", NULL, "menu", ctx.qry.head,
498 cgit_query_sha1, cgit_query_sha2, NULL); 498 ctx.qry.sha1, ctx.qry.sha2, NULL);
499 cgit_patch_link("patch", NULL, "menu", cgit_query_head, 499 cgit_patch_link("patch", NULL, "menu", ctx.qry.head,
500 cgit_query_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 (cgit_repo->clone_url || cgit_clone_prefix) { 504 if (cgit_repo->clone_url || cgit_clone_prefix) {
505 html("<h1>clone</h1>\n"); 505 html("<h1>clone</h1>\n");
506 if (cgit_repo->clone_url) 506 if (cgit_repo->clone_url)
507 url = cgit_repo->clone_url; 507 url = cgit_repo->clone_url;
508 else 508 else
509 url = fmt("%s%s", cgit_clone_prefix, 509 url = fmt("%s%s", cgit_clone_prefix,
510 cgit_repo->url); 510 cgit_repo->url);
511 html("<a class='menu' href='"); 511 html("<a class='menu' href='");
512 html_attr(url); 512 html_attr(url);
513 html("' title='"); 513 html("' title='");
514 html_attr(url); 514 html_attr(url);
515 html("'>\n"); 515 html("'>\n");
516 html_txt(strrpart(url, 20)); 516 html_txt(strrpart(url, 20));
517 html("</a>\n"); 517 html("</a>\n");
518 } 518 }
519 519
520 html("<h1>branch</h1>\n"); 520 html("<h1>branch</h1>\n");
521 html("<form method='get' action=''>\n"); 521 html("<form method='get' action=''>\n");
522 add_hidden_formfields(0, 1, cgit_query_page); 522 add_hidden_formfields(0, 1, ctx.qry.page);
523 // html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>"); 523 // html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>");
524 html("<select name='h' onchange='this.form.submit();'>\n"); 524 html("<select name='h' onchange='this.form.submit();'>\n");
525 for_each_branch_ref(print_branch_option, cgit_query_head); 525 for_each_branch_ref(print_branch_option, ctx.qry.head);
526 html("</select>\n"); 526 html("</select>\n");
527 // html("</td><td>"); 527 // html("</td><td>");
528 html("<noscript><input type='submit' id='switch-btn' value='switch'/></noscript>\n"); 528 html("<noscript><input type='submit' id='switch-btn' value='switch'/></noscript>\n");
529 // html("</td></tr></table>"); 529 // html("</td></tr></table>");
530 html("</form>\n"); 530 html("</form>\n");
531 531
532 html("<h1>search</h1>\n"); 532 html("<h1>search</h1>\n");
533 html("<form method='get' action='"); 533 html("<form method='get' action='");
534 if (cgit_virtual_root) 534 if (cgit_virtual_root)
535 html_attr(cgit_fileurl(cgit_query_repo, "log", 535 html_attr(cgit_fileurl(ctx.qry.repo, "log",
536 cgit_query_path, NULL)); 536 ctx.qry.path, NULL));
537 html("'>\n"); 537 html("'>\n");
538 add_hidden_formfields(1, 0, "log"); 538 add_hidden_formfields(1, 0, "log");
539 html("<select name='qt'>\n"); 539 html("<select name='qt'>\n");
540 html_option("grep", "log msg", cgit_query_grep); 540 html_option("grep", "log msg", ctx.qry.grep);
541 html_option("author", "author", cgit_query_grep); 541 html_option("author", "author", ctx.qry.grep);
542 html_option("committer", "committer", cgit_query_grep); 542 html_option("committer", "committer", ctx.qry.grep);
543 html("</select>\n"); 543 html("</select>\n");
544 html("<input class='txt' type='text' name='q' value='"); 544 html("<input class='txt' type='text' name='q' value='");
545 html_attr(cgit_query_search); 545 html_attr(ctx.qry.search);
546 html("'/>\n"); 546 html("'/>\n");
547 html("</form>\n"); 547 html("</form>\n");
548 } else { 548 } else {
549 if (!cgit_index_info || html_include(cgit_index_info)) 549 if (!cgit_index_info || html_include(cgit_index_info))
550 html(default_info); 550 html(default_info);
551 } 551 }
552 552
553 html("</td></tr></table></td>\n"); 553 html("</td></tr></table></td>\n");
554 554
555 html("<td id='content'>\n"); 555 html("<td id='content'>\n");
556} 556}
557 557
558 558
559void cgit_print_snapshot_start(const char *mimetype, const char *filename, 559void cgit_print_snapshot_start(const char *mimetype, const char *filename,
560 struct cacheitem *item) 560 struct cacheitem *item)
561{ 561{
562 htmlf("Content-Type: %s\n", mimetype); 562 htmlf("Content-Type: %s\n", mimetype);
563 htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename); 563 htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename);
564 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); 564 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime));
565 htmlf("Expires: %s\n", http_date(item->st.st_mtime + 565 htmlf("Expires: %s\n", http_date(item->st.st_mtime +
566 ttl_seconds(item->ttl))); 566 ttl_seconds(item->ttl)));
567 html("\n"); 567 html("\n");
568} 568}
569 569
570/* vim:set sw=8: */ 570/* vim:set sw=8: */
diff --git a/ui-summary.c b/ui-summary.c
index b96414e..bbd4464 100644
--- a/ui-summary.c
+++ b/ui-summary.c
@@ -65,136 +65,136 @@ static int print_branch(struct refinfo *ref)
65 html_txt(info->author); 65 html_txt(info->author);
66 html("</td><td>"); 66 html("</td><td>");
67 cgit_commit_link(info->subject, NULL, NULL, name, NULL); 67 cgit_commit_link(info->subject, NULL, NULL, name, NULL);
68 } else { 68 } else {
69 html("</td><td></td><td>"); 69 html("</td><td></td><td>");
70 cgit_object_link(ref->object); 70 cgit_object_link(ref->object);
71 } 71 }
72 html("</td></tr>\n"); 72 html("</td></tr>\n");
73 return 0; 73 return 0;
74} 74}
75 75
76static void print_tag_header() 76static void print_tag_header()
77{ 77{
78 html("<tr class='nohover'><th class='left'>Tag</th>" 78 html("<tr class='nohover'><th class='left'>Tag</th>"
79 "<th class='left'>Age</th>" 79 "<th class='left'>Age</th>"
80 "<th class='left'>Author</th>" 80 "<th class='left'>Author</th>"
81 "<th class='left'>Reference</th></tr>\n"); 81 "<th class='left'>Reference</th></tr>\n");
82 header = 1; 82 header = 1;
83} 83}
84 84
85static int print_tag(struct refinfo *ref) 85static int print_tag(struct refinfo *ref)
86{ 86{
87 struct tag *tag; 87 struct tag *tag;
88 struct taginfo *info; 88 struct taginfo *info;
89 char *url, *name = (char *)ref->refname; 89 char *url, *name = (char *)ref->refname;
90 90
91 if (ref->object->type == OBJ_TAG) { 91 if (ref->object->type == OBJ_TAG) {
92 tag = (struct tag *)ref->object; 92 tag = (struct tag *)ref->object;
93 info = ref->tag; 93 info = ref->tag;
94 if (!tag || !info) 94 if (!tag || !info)
95 return 1; 95 return 1;
96 html("<tr><td>"); 96 html("<tr><td>");
97 url = cgit_pageurl(cgit_query_repo, "tag", 97 url = cgit_pageurl(ctx.qry.repo, "tag",
98 fmt("id=%s", name)); 98 fmt("id=%s", name));
99 html_link_open(url, NULL, NULL); 99 html_link_open(url, NULL, NULL);
100 html_txt(name); 100 html_txt(name);
101 html_link_close(); 101 html_link_close();
102 html("</td><td>"); 102 html("</td><td>");
103 if (info->tagger_date > 0) 103 if (info->tagger_date > 0)
104 cgit_print_age(info->tagger_date, -1, NULL); 104 cgit_print_age(info->tagger_date, -1, NULL);
105 html("</td><td>"); 105 html("</td><td>");
106 if (info->tagger) 106 if (info->tagger)
107 html(info->tagger); 107 html(info->tagger);
108 html("</td><td>"); 108 html("</td><td>");
109 cgit_object_link(tag->tagged); 109 cgit_object_link(tag->tagged);
110 html("</td></tr>\n"); 110 html("</td></tr>\n");
111 } else { 111 } else {
112 if (!header) 112 if (!header)
113 print_tag_header(); 113 print_tag_header();
114 html("<tr><td>"); 114 html("<tr><td>");
115 html_txt(name); 115 html_txt(name);
116 html("</td><td colspan='2'/><td>"); 116 html("</td><td colspan='2'/><td>");
117 cgit_object_link(ref->object); 117 cgit_object_link(ref->object);
118 html("</td></tr>\n"); 118 html("</td></tr>\n");
119 } 119 }
120 return 0; 120 return 0;
121} 121}
122 122
123static void print_refs_link(char *path) 123static void print_refs_link(char *path)
124{ 124{
125 html("<tr class='nohover'><td colspan='4'>"); 125 html("<tr class='nohover'><td colspan='4'>");
126 cgit_refs_link("[...]", NULL, NULL, cgit_query_head, NULL, path); 126 cgit_refs_link("[...]", NULL, NULL, ctx.qry.head, NULL, path);
127 html("</td></tr>"); 127 html("</td></tr>");
128} 128}
129 129
130void cgit_print_branches(int maxcount) 130void cgit_print_branches(int maxcount)
131{ 131{
132 struct reflist list; 132 struct reflist list;
133 int i; 133 int i;
134 134
135 html("<tr class='nohover'><th class='left'>Branch</th>" 135 html("<tr class='nohover'><th class='left'>Branch</th>"
136 "<th class='left'>Idle</th>" 136 "<th class='left'>Idle</th>"
137 "<th class='left'>Author</th>" 137 "<th class='left'>Author</th>"
138 "<th class='left'>Head commit</th></tr>\n"); 138 "<th class='left'>Head commit</th></tr>\n");
139 139
140 list.refs = NULL; 140 list.refs = NULL;
141 list.alloc = list.count = 0; 141 list.alloc = list.count = 0;
142 for_each_branch_ref(cgit_refs_cb, &list); 142 for_each_branch_ref(cgit_refs_cb, &list);
143 143
144 if (maxcount == 0 || maxcount > list.count) 144 if (maxcount == 0 || maxcount > list.count)
145 maxcount = list.count; 145 maxcount = list.count;
146 146
147 if (maxcount < list.count) { 147 if (maxcount < list.count) {
148 qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age); 148 qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age);
149 qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name); 149 qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name);
150 } 150 }
151 151
152 for(i=0; i<maxcount; i++) 152 for(i=0; i<maxcount; i++)
153 print_branch(list.refs[i]); 153 print_branch(list.refs[i]);
154 154
155 if (maxcount < list.count) 155 if (maxcount < list.count)
156 print_refs_link("heads"); 156 print_refs_link("heads");
157} 157}
158 158
159void cgit_print_tags(int maxcount) 159void cgit_print_tags(int maxcount)
160{ 160{
161 struct reflist list; 161 struct reflist list;
162 int i; 162 int i;
163 163
164 header = 0; 164 header = 0;
165 list.refs = NULL; 165 list.refs = NULL;
166 list.alloc = list.count = 0; 166 list.alloc = list.count = 0;
167 for_each_tag_ref(cgit_refs_cb, &list); 167 for_each_tag_ref(cgit_refs_cb, &list);
168 if (list.count == 0) 168 if (list.count == 0)
169 return; 169 return;
170 qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age); 170 qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age);
171 if (!maxcount) 171 if (!maxcount)
172 maxcount = list.count; 172 maxcount = list.count;
173 else if (maxcount > list.count) 173 else if (maxcount > list.count)
174 maxcount = list.count; 174 maxcount = list.count;
175 print_tag_header(); 175 print_tag_header();
176 for(i=0; i<maxcount; i++) 176 for(i=0; i<maxcount; i++)
177 print_tag(list.refs[i]); 177 print_tag(list.refs[i]);
178 178
179 if (maxcount < list.count) 179 if (maxcount < list.count)
180 print_refs_link("tags"); 180 print_refs_link("tags");
181} 181}
182 182
183void cgit_print_summary() 183void cgit_print_summary()
184{ 184{
185 if (cgit_repo->readme) { 185 if (cgit_repo->readme) {
186 html("<div id='summary'>"); 186 html("<div id='summary'>");
187 html_include(cgit_repo->readme); 187 html_include(cgit_repo->readme);
188 html("</div>"); 188 html("</div>");
189 } 189 }
190 if (cgit_summary_log > 0) 190 if (cgit_summary_log > 0)
191 cgit_print_log(cgit_query_head, 0, cgit_summary_log, NULL, 191 cgit_print_log(ctx.qry.head, 0, cgit_summary_log, NULL,
192 NULL, NULL, 0); 192 NULL, NULL, 0);
193 html("<table summary='repository info' class='list nowrap'>"); 193 html("<table summary='repository info' class='list nowrap'>");
194 if (cgit_summary_log > 0) 194 if (cgit_summary_log > 0)
195 html("<tr class='nohover'><td colspan='4'>&nbsp;</td></tr>"); 195 html("<tr class='nohover'><td colspan='4'>&nbsp;</td></tr>");
196 cgit_print_branches(cgit_summary_branches); 196 cgit_print_branches(cgit_summary_branches);
197 html("<tr class='nohover'><td colspan='4'>&nbsp;</td></tr>"); 197 html("<tr class='nohover'><td colspan='4'>&nbsp;</td></tr>");
198 cgit_print_tags(cgit_summary_tags); 198 cgit_print_tags(cgit_summary_tags);
199 html("</table>"); 199 html("</table>");
200} 200}
diff --git a/ui-tree.c b/ui-tree.c
index c138877..7cae85a 100644
--- a/ui-tree.c
+++ b/ui-tree.c
@@ -5,212 +5,212 @@
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
11char *curr_rev; 11char *curr_rev;
12char *match_path; 12char *match_path;
13int header = 0; 13int header = 0;
14 14
15static void print_object(const unsigned char *sha1, char *path) 15static void print_object(const unsigned char *sha1, char *path)
16{ 16{
17 enum object_type type; 17 enum object_type type;
18 char *buf; 18 char *buf;
19 unsigned long size, lineno, start, idx; 19 unsigned long size, lineno, start, idx;
20 const char *linefmt = "<tr><td class='no'><a id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a></td><td class='txt'>"; 20 const char *linefmt = "<tr><td class='no'><a id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a></td><td class='txt'>";
21 21
22 type = sha1_object_info(sha1, &size); 22 type = sha1_object_info(sha1, &size);
23 if (type == OBJ_BAD) { 23 if (type == OBJ_BAD) {
24 cgit_print_error(fmt("Bad object name: %s", 24 cgit_print_error(fmt("Bad object name: %s",
25 sha1_to_hex(sha1))); 25 sha1_to_hex(sha1)));
26 return; 26 return;
27 } 27 }
28 28
29 buf = read_sha1_file(sha1, &type, &size); 29 buf = read_sha1_file(sha1, &type, &size);
30 if (!buf) { 30 if (!buf) {
31 cgit_print_error(fmt("Error reading object %s", 31 cgit_print_error(fmt("Error reading object %s",
32 sha1_to_hex(sha1))); 32 sha1_to_hex(sha1)));
33 return; 33 return;
34 } 34 }
35 35
36 html(" blob: <a href='"); 36 html(" blob: <a href='");
37 html_attr(cgit_pageurl(cgit_query_repo, "blob", fmt("id=%s", sha1_to_hex(sha1)))); 37 html_attr(cgit_pageurl(ctx.qry.repo, "blob", fmt("id=%s", sha1_to_hex(sha1))));
38 htmlf("'>%s</a>",sha1_to_hex(sha1)); 38 htmlf("'>%s</a>",sha1_to_hex(sha1));
39 39
40 html("<table summary='blob content' class='blob'>\n"); 40 html("<table summary='blob content' class='blob'>\n");
41 idx = 0; 41 idx = 0;
42 start = 0; 42 start = 0;
43 lineno = 0; 43 lineno = 0;
44 while(idx < size) { 44 while(idx < size) {
45 if (buf[idx] == '\n') { 45 if (buf[idx] == '\n') {
46 buf[idx] = '\0'; 46 buf[idx] = '\0';
47 htmlf(linefmt, ++lineno); 47 htmlf(linefmt, ++lineno);
48 html_txt(buf + start); 48 html_txt(buf + start);
49 html("</td></tr>\n"); 49 html("</td></tr>\n");
50 start = idx + 1; 50 start = idx + 1;
51 } 51 }
52 idx++; 52 idx++;
53 } 53 }
54 htmlf(linefmt, ++lineno); 54 htmlf(linefmt, ++lineno);
55 html_txt(buf + start); 55 html_txt(buf + start);
56 html("</td></tr>\n"); 56 html("</td></tr>\n");
57 html("</table>\n"); 57 html("</table>\n");
58} 58}
59 59
60 60
61static int ls_item(const unsigned char *sha1, const char *base, int baselen, 61static int ls_item(const unsigned char *sha1, const char *base, int baselen,
62 const char *pathname, unsigned int mode, int stage) 62 const char *pathname, unsigned int mode, int stage)
63{ 63{
64 char *name; 64 char *name;
65 char *fullpath; 65 char *fullpath;
66 enum object_type type; 66 enum object_type type;
67 unsigned long size = 0; 67 unsigned long size = 0;
68 68
69 name = xstrdup(pathname); 69 name = xstrdup(pathname);
70 fullpath = fmt("%s%s%s", cgit_query_path ? cgit_query_path : "", 70 fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "",
71 cgit_query_path ? "/" : "", name); 71 ctx.qry.path ? "/" : "", name);
72 72
73 type = sha1_object_info(sha1, &size); 73 type = sha1_object_info(sha1, &size);
74 if (type == OBJ_BAD && !S_ISGITLINK(mode)) { 74 if (type == OBJ_BAD && !S_ISGITLINK(mode)) {
75 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>", 75 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
76 name, 76 name,
77 sha1_to_hex(sha1)); 77 sha1_to_hex(sha1));
78 return 0; 78 return 0;
79 } 79 }
80 80
81 html("<tr><td class='ls-mode'>"); 81 html("<tr><td class='ls-mode'>");
82 html_filemode(mode); 82 html_filemode(mode);
83 html("</td><td>"); 83 html("</td><td>");
84 if (S_ISGITLINK(mode)) { 84 if (S_ISGITLINK(mode)) {
85 htmlf("<a class='ls-mod' href='"); 85 htmlf("<a class='ls-mod' href='");
86 html_attr(fmt(cgit_repo->module_link, 86 html_attr(fmt(cgit_repo->module_link,
87 name, 87 name,
88 sha1_to_hex(sha1))); 88 sha1_to_hex(sha1)));
89 html("'>"); 89 html("'>");
90 html_txt(name); 90 html_txt(name);
91 html("</a>"); 91 html("</a>");
92 } else if (S_ISDIR(mode)) { 92 } else if (S_ISDIR(mode)) {
93 cgit_tree_link(name, NULL, "ls-dir", cgit_query_head, 93 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
94 curr_rev, fullpath); 94 curr_rev, fullpath);
95 } else { 95 } else {
96 cgit_tree_link(name, NULL, "ls-blob", cgit_query_head, 96 cgit_tree_link(name, NULL, "ls-blob", ctx.qry.head,
97 curr_rev, fullpath); 97 curr_rev, fullpath);
98 } 98 }
99 htmlf("</td><td class='ls-size'>%li</td>", size); 99 htmlf("</td><td class='ls-size'>%li</td>", size);
100 100
101 html("<td>"); 101 html("<td>");
102 cgit_log_link("log", NULL, "button", cgit_query_head, curr_rev, 102 cgit_log_link("log", NULL, "button", ctx.qry.head, curr_rev,
103 fullpath, 0, NULL, NULL); 103 fullpath, 0, NULL, NULL);
104 html("</td></tr>\n"); 104 html("</td></tr>\n");
105 free(name); 105 free(name);
106 return 0; 106 return 0;
107} 107}
108 108
109static void ls_head() 109static void ls_head()
110{ 110{
111 html("<table summary='tree listing' class='list'>\n"); 111 html("<table summary='tree listing' class='list'>\n");
112 html("<tr class='nohover'>"); 112 html("<tr class='nohover'>");
113 html("<th class='left'>Mode</th>"); 113 html("<th class='left'>Mode</th>");
114 html("<th class='left'>Name</th>"); 114 html("<th class='left'>Name</th>");
115 html("<th class='right'>Size</th>"); 115 html("<th class='right'>Size</th>");
116 html("<th/>"); 116 html("<th/>");
117 html("</tr>\n"); 117 html("</tr>\n");
118 header = 1; 118 header = 1;
119} 119}
120 120
121static void ls_tail() 121static void ls_tail()
122{ 122{
123 if (!header) 123 if (!header)
124 return; 124 return;
125 html("</table>\n"); 125 html("</table>\n");
126 header = 0; 126 header = 0;
127} 127}
128 128
129static void ls_tree(const unsigned char *sha1, char *path) 129static void ls_tree(const unsigned char *sha1, char *path)
130{ 130{
131 struct tree *tree; 131 struct tree *tree;
132 132
133 tree = parse_tree_indirect(sha1); 133 tree = parse_tree_indirect(sha1);
134 if (!tree) { 134 if (!tree) {
135 cgit_print_error(fmt("Not a tree object: %s", 135 cgit_print_error(fmt("Not a tree object: %s",
136 sha1_to_hex(sha1))); 136 sha1_to_hex(sha1)));
137 return; 137 return;
138 } 138 }
139 139
140 ls_head(); 140 ls_head();
141 read_tree_recursive(tree, "", 0, 1, NULL, ls_item); 141 read_tree_recursive(tree, "", 0, 1, NULL, ls_item);
142 ls_tail(); 142 ls_tail();
143} 143}
144 144
145 145
146static int walk_tree(const unsigned char *sha1, const char *base, int baselen, 146static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
147 const char *pathname, unsigned mode, int stage) 147 const char *pathname, unsigned mode, int stage)
148{ 148{
149 static int state; 149 static int state;
150 static char buffer[PATH_MAX]; 150 static char buffer[PATH_MAX];
151 char *url; 151 char *url;
152 152
153 if (state == 0) { 153 if (state == 0) {
154 memcpy(buffer, base, baselen); 154 memcpy(buffer, base, baselen);
155 strcpy(buffer+baselen, pathname); 155 strcpy(buffer+baselen, pathname);
156 url = cgit_pageurl(cgit_query_repo, "tree", 156 url = cgit_pageurl(ctx.qry.repo, "tree",
157 fmt("h=%s&amp;path=%s", curr_rev, buffer)); 157 fmt("h=%s&amp;path=%s", curr_rev, buffer));
158 html("/"); 158 html("/");
159 cgit_tree_link(xstrdup(pathname), NULL, NULL, cgit_query_head, 159 cgit_tree_link(xstrdup(pathname), NULL, NULL, ctx.qry.head,
160 curr_rev, buffer); 160 curr_rev, buffer);
161 161
162 if (strcmp(match_path, buffer)) 162 if (strcmp(match_path, buffer))
163 return READ_TREE_RECURSIVE; 163 return READ_TREE_RECURSIVE;
164 164
165 if (S_ISDIR(mode)) { 165 if (S_ISDIR(mode)) {
166 state = 1; 166 state = 1;
167 ls_head(); 167 ls_head();
168 return READ_TREE_RECURSIVE; 168 return READ_TREE_RECURSIVE;
169 } else { 169 } else {
170 print_object(sha1, buffer); 170 print_object(sha1, buffer);
171 return 0; 171 return 0;
172 } 172 }
173 } 173 }
174 ls_item(sha1, base, baselen, pathname, mode, stage); 174 ls_item(sha1, base, baselen, pathname, mode, stage);
175 return 0; 175 return 0;
176} 176}
177 177
178 178
179/* 179/*
180 * Show a tree or a blob 180 * Show a tree or a blob
181 * rev: the commit pointing at the root tree object 181 * rev: the commit pointing at the root tree object
182 * path: path to tree or blob 182 * path: path to tree or blob
183 */ 183 */
184void cgit_print_tree(const char *rev, char *path) 184void cgit_print_tree(const char *rev, char *path)
185{ 185{
186 unsigned char sha1[20]; 186 unsigned char sha1[20];
187 struct commit *commit; 187 struct commit *commit;
188 const char *paths[] = {path, NULL}; 188 const char *paths[] = {path, NULL};
189 189
190 if (!rev) 190 if (!rev)
191 rev = cgit_query_head; 191 rev = ctx.qry.head;
192 192
193 curr_rev = xstrdup(rev); 193 curr_rev = xstrdup(rev);
194 if (get_sha1(rev, sha1)) { 194 if (get_sha1(rev, sha1)) {
195 cgit_print_error(fmt("Invalid revision name: %s", rev)); 195 cgit_print_error(fmt("Invalid revision name: %s", rev));
196 return; 196 return;
197 } 197 }
198 commit = lookup_commit_reference(sha1); 198 commit = lookup_commit_reference(sha1);
199 if (!commit || parse_commit(commit)) { 199 if (!commit || parse_commit(commit)) {
200 cgit_print_error(fmt("Invalid commit reference: %s", rev)); 200 cgit_print_error(fmt("Invalid commit reference: %s", rev));
201 return; 201 return;
202 } 202 }
203 203
204 html("path: <a href='"); 204 html("path: <a href='");
205 html_attr(cgit_pageurl(cgit_query_repo, "tree", fmt("h=%s", rev))); 205 html_attr(cgit_pageurl(ctx.qry.repo, "tree", fmt("h=%s", rev)));
206 html("'>root</a>"); 206 html("'>root</a>");
207 207
208 if (path == NULL) { 208 if (path == NULL) {
209 ls_tree(commit->tree->object.sha1, NULL); 209 ls_tree(commit->tree->object.sha1, NULL);
210 return; 210 return;
211 } 211 }
212 212
213 match_path = path; 213 match_path = path;
214 read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree); 214 read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree);
215 ls_tail(); 215 ls_tail();
216} 216}