summaryrefslogtreecommitdiffabout
authorLars Hjemli <hjemli@gmail.com>2008-12-06 10:35:49 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2008-12-06 10:35:49 (UTC)
commit9c8be943f72b6f1bda5a31ce401899c3dd734e98 (patch) (unidiff)
tree2279ad0a7d36563df7698d6d4e81967d67eb2562
parente550440233875b298f8574e9273457516791010f (diff)
parent8813170390f3c3a0f4743afbc92ede42953fa3b0 (diff)
downloadcgit-9c8be943f72b6f1bda5a31ce401899c3dd734e98.zip
cgit-9c8be943f72b6f1bda5a31ce401899c3dd734e98.tar.gz
cgit-9c8be943f72b6f1bda5a31ce401899c3dd734e98.tar.bz2
Merge branch 'lh/sort-repolist'
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c2
-rw-r--r--cgit.h2
-rw-r--r--shared.c1
-rw-r--r--ui-repolist.c135
4 files changed, 126 insertions, 14 deletions
diff --git a/cgit.c b/cgit.c
index c82587b..e09c86e 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1,472 +1,474 @@
1/* cgit.c: cgi for the git scm 1/* cgit.c: cgi for the git scm
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10#include "cache.h" 10#include "cache.h"
11#include "cmd.h" 11#include "cmd.h"
12#include "configfile.h" 12#include "configfile.h"
13#include "html.h" 13#include "html.h"
14#include "ui-shared.h" 14#include "ui-shared.h"
15#include "scan-tree.h" 15#include "scan-tree.h"
16 16
17const char *cgit_version = CGIT_VERSION; 17const char *cgit_version = CGIT_VERSION;
18 18
19void config_cb(const char *name, const char *value) 19void config_cb(const char *name, const char *value)
20{ 20{
21 if (!strcmp(name, "root-title")) 21 if (!strcmp(name, "root-title"))
22 ctx.cfg.root_title = xstrdup(value); 22 ctx.cfg.root_title = xstrdup(value);
23 else if (!strcmp(name, "root-desc")) 23 else if (!strcmp(name, "root-desc"))
24 ctx.cfg.root_desc = xstrdup(value); 24 ctx.cfg.root_desc = xstrdup(value);
25 else if (!strcmp(name, "root-readme")) 25 else if (!strcmp(name, "root-readme"))
26 ctx.cfg.root_readme = xstrdup(value); 26 ctx.cfg.root_readme = xstrdup(value);
27 else if (!strcmp(name, "css")) 27 else if (!strcmp(name, "css"))
28 ctx.cfg.css = xstrdup(value); 28 ctx.cfg.css = xstrdup(value);
29 else if (!strcmp(name, "favicon")) 29 else if (!strcmp(name, "favicon"))
30 ctx.cfg.favicon = xstrdup(value); 30 ctx.cfg.favicon = xstrdup(value);
31 else if (!strcmp(name, "footer")) 31 else if (!strcmp(name, "footer"))
32 ctx.cfg.footer = xstrdup(value); 32 ctx.cfg.footer = xstrdup(value);
33 else if (!strcmp(name, "logo")) 33 else if (!strcmp(name, "logo"))
34 ctx.cfg.logo = xstrdup(value); 34 ctx.cfg.logo = xstrdup(value);
35 else if (!strcmp(name, "index-header")) 35 else if (!strcmp(name, "index-header"))
36 ctx.cfg.index_header = xstrdup(value); 36 ctx.cfg.index_header = xstrdup(value);
37 else if (!strcmp(name, "index-info")) 37 else if (!strcmp(name, "index-info"))
38 ctx.cfg.index_info = xstrdup(value); 38 ctx.cfg.index_info = xstrdup(value);
39 else if (!strcmp(name, "logo-link")) 39 else if (!strcmp(name, "logo-link"))
40 ctx.cfg.logo_link = xstrdup(value); 40 ctx.cfg.logo_link = xstrdup(value);
41 else if (!strcmp(name, "module-link")) 41 else if (!strcmp(name, "module-link"))
42 ctx.cfg.module_link = xstrdup(value); 42 ctx.cfg.module_link = xstrdup(value);
43 else if (!strcmp(name, "virtual-root")) { 43 else if (!strcmp(name, "virtual-root")) {
44 ctx.cfg.virtual_root = trim_end(value, '/'); 44 ctx.cfg.virtual_root = trim_end(value, '/');
45 if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) 45 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
46 ctx.cfg.virtual_root = ""; 46 ctx.cfg.virtual_root = "";
47 } else if (!strcmp(name, "nocache")) 47 } else if (!strcmp(name, "nocache"))
48 ctx.cfg.nocache = atoi(value); 48 ctx.cfg.nocache = atoi(value);
49 else if (!strcmp(name, "snapshots")) 49 else if (!strcmp(name, "snapshots"))
50 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); 50 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
51 else if (!strcmp(name, "enable-index-links")) 51 else if (!strcmp(name, "enable-index-links"))
52 ctx.cfg.enable_index_links = atoi(value); 52 ctx.cfg.enable_index_links = atoi(value);
53 else if (!strcmp(name, "enable-log-filecount")) 53 else if (!strcmp(name, "enable-log-filecount"))
54 ctx.cfg.enable_log_filecount = atoi(value); 54 ctx.cfg.enable_log_filecount = atoi(value);
55 else if (!strcmp(name, "enable-log-linecount")) 55 else if (!strcmp(name, "enable-log-linecount"))
56 ctx.cfg.enable_log_linecount = atoi(value); 56 ctx.cfg.enable_log_linecount = atoi(value);
57 else if (!strcmp(name, "cache-size")) 57 else if (!strcmp(name, "cache-size"))
58 ctx.cfg.cache_size = atoi(value); 58 ctx.cfg.cache_size = atoi(value);
59 else if (!strcmp(name, "cache-root")) 59 else if (!strcmp(name, "cache-root"))
60 ctx.cfg.cache_root = xstrdup(value); 60 ctx.cfg.cache_root = xstrdup(value);
61 else if (!strcmp(name, "cache-root-ttl")) 61 else if (!strcmp(name, "cache-root-ttl"))
62 ctx.cfg.cache_root_ttl = atoi(value); 62 ctx.cfg.cache_root_ttl = atoi(value);
63 else if (!strcmp(name, "cache-repo-ttl")) 63 else if (!strcmp(name, "cache-repo-ttl"))
64 ctx.cfg.cache_repo_ttl = atoi(value); 64 ctx.cfg.cache_repo_ttl = atoi(value);
65 else if (!strcmp(name, "cache-static-ttl")) 65 else if (!strcmp(name, "cache-static-ttl"))
66 ctx.cfg.cache_static_ttl = atoi(value); 66 ctx.cfg.cache_static_ttl = atoi(value);
67 else if (!strcmp(name, "cache-dynamic-ttl")) 67 else if (!strcmp(name, "cache-dynamic-ttl"))
68 ctx.cfg.cache_dynamic_ttl = atoi(value); 68 ctx.cfg.cache_dynamic_ttl = atoi(value);
69 else if (!strcmp(name, "max-message-length")) 69 else if (!strcmp(name, "max-message-length"))
70 ctx.cfg.max_msg_len = atoi(value); 70 ctx.cfg.max_msg_len = atoi(value);
71 else if (!strcmp(name, "max-repodesc-length")) 71 else if (!strcmp(name, "max-repodesc-length"))
72 ctx.cfg.max_repodesc_len = atoi(value); 72 ctx.cfg.max_repodesc_len = atoi(value);
73 else if (!strcmp(name, "max-repo-count")) 73 else if (!strcmp(name, "max-repo-count"))
74 ctx.cfg.max_repo_count = atoi(value); 74 ctx.cfg.max_repo_count = atoi(value);
75 else if (!strcmp(name, "max-commit-count")) 75 else if (!strcmp(name, "max-commit-count"))
76 ctx.cfg.max_commit_count = atoi(value); 76 ctx.cfg.max_commit_count = atoi(value);
77 else if (!strcmp(name, "summary-log")) 77 else if (!strcmp(name, "summary-log"))
78 ctx.cfg.summary_log = atoi(value); 78 ctx.cfg.summary_log = atoi(value);
79 else if (!strcmp(name, "summary-branches")) 79 else if (!strcmp(name, "summary-branches"))
80 ctx.cfg.summary_branches = atoi(value); 80 ctx.cfg.summary_branches = atoi(value);
81 else if (!strcmp(name, "summary-tags")) 81 else if (!strcmp(name, "summary-tags"))
82 ctx.cfg.summary_tags = atoi(value); 82 ctx.cfg.summary_tags = atoi(value);
83 else if (!strcmp(name, "agefile")) 83 else if (!strcmp(name, "agefile"))
84 ctx.cfg.agefile = xstrdup(value); 84 ctx.cfg.agefile = xstrdup(value);
85 else if (!strcmp(name, "renamelimit")) 85 else if (!strcmp(name, "renamelimit"))
86 ctx.cfg.renamelimit = atoi(value); 86 ctx.cfg.renamelimit = atoi(value);
87 else if (!strcmp(name, "robots")) 87 else if (!strcmp(name, "robots"))
88 ctx.cfg.robots = xstrdup(value); 88 ctx.cfg.robots = xstrdup(value);
89 else if (!strcmp(name, "clone-prefix")) 89 else if (!strcmp(name, "clone-prefix"))
90 ctx.cfg.clone_prefix = xstrdup(value); 90 ctx.cfg.clone_prefix = xstrdup(value);
91 else if (!strcmp(name, "local-time")) 91 else if (!strcmp(name, "local-time"))
92 ctx.cfg.local_time = atoi(value); 92 ctx.cfg.local_time = atoi(value);
93 else if (!strcmp(name, "repo.group")) 93 else if (!strcmp(name, "repo.group"))
94 ctx.cfg.repo_group = xstrdup(value); 94 ctx.cfg.repo_group = xstrdup(value);
95 else if (!strcmp(name, "repo.url")) 95 else if (!strcmp(name, "repo.url"))
96 ctx.repo = cgit_add_repo(value); 96 ctx.repo = cgit_add_repo(value);
97 else if (!strcmp(name, "repo.name")) 97 else if (!strcmp(name, "repo.name"))
98 ctx.repo->name = xstrdup(value); 98 ctx.repo->name = xstrdup(value);
99 else if (ctx.repo && !strcmp(name, "repo.path")) 99 else if (ctx.repo && !strcmp(name, "repo.path"))
100 ctx.repo->path = trim_end(value, '/'); 100 ctx.repo->path = trim_end(value, '/');
101 else if (ctx.repo && !strcmp(name, "repo.clone-url")) 101 else if (ctx.repo && !strcmp(name, "repo.clone-url"))
102 ctx.repo->clone_url = xstrdup(value); 102 ctx.repo->clone_url = xstrdup(value);
103 else if (ctx.repo && !strcmp(name, "repo.desc")) 103 else if (ctx.repo && !strcmp(name, "repo.desc"))
104 ctx.repo->desc = xstrdup(value); 104 ctx.repo->desc = xstrdup(value);
105 else if (ctx.repo && !strcmp(name, "repo.owner")) 105 else if (ctx.repo && !strcmp(name, "repo.owner"))
106 ctx.repo->owner = xstrdup(value); 106 ctx.repo->owner = xstrdup(value);
107 else if (ctx.repo && !strcmp(name, "repo.defbranch")) 107 else if (ctx.repo && !strcmp(name, "repo.defbranch"))
108 ctx.repo->defbranch = xstrdup(value); 108 ctx.repo->defbranch = xstrdup(value);
109 else if (ctx.repo && !strcmp(name, "repo.snapshots")) 109 else if (ctx.repo && !strcmp(name, "repo.snapshots"))
110 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ 110 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
111 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) 111 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
112 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); 112 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
113 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) 113 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
114 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); 114 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
115 else if (ctx.repo && !strcmp(name, "repo.module-link")) 115 else if (ctx.repo && !strcmp(name, "repo.module-link"))
116 ctx.repo->module_link= xstrdup(value); 116 ctx.repo->module_link= xstrdup(value);
117 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { 117 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
118 if (*value == '/') 118 if (*value == '/')
119 ctx.repo->readme = xstrdup(value); 119 ctx.repo->readme = xstrdup(value);
120 else 120 else
121 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); 121 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
122 } else if (!strcmp(name, "include")) 122 } else if (!strcmp(name, "include"))
123 parse_configfile(value, config_cb); 123 parse_configfile(value, config_cb);
124} 124}
125 125
126static void querystring_cb(const char *name, const char *value) 126static void querystring_cb(const char *name, const char *value)
127{ 127{
128 if (!strcmp(name,"r")) { 128 if (!strcmp(name,"r")) {
129 ctx.qry.repo = xstrdup(value); 129 ctx.qry.repo = xstrdup(value);
130 ctx.repo = cgit_get_repoinfo(value); 130 ctx.repo = cgit_get_repoinfo(value);
131 } else if (!strcmp(name, "p")) { 131 } else if (!strcmp(name, "p")) {
132 ctx.qry.page = xstrdup(value); 132 ctx.qry.page = xstrdup(value);
133 } else if (!strcmp(name, "url")) { 133 } else if (!strcmp(name, "url")) {
134 ctx.qry.url = xstrdup(value); 134 ctx.qry.url = xstrdup(value);
135 cgit_parse_url(value); 135 cgit_parse_url(value);
136 } else if (!strcmp(name, "qt")) { 136 } else if (!strcmp(name, "qt")) {
137 ctx.qry.grep = xstrdup(value); 137 ctx.qry.grep = xstrdup(value);
138 } else if (!strcmp(name, "q")) { 138 } else if (!strcmp(name, "q")) {
139 ctx.qry.search = xstrdup(value); 139 ctx.qry.search = xstrdup(value);
140 } else if (!strcmp(name, "h")) { 140 } else if (!strcmp(name, "h")) {
141 ctx.qry.head = xstrdup(value); 141 ctx.qry.head = xstrdup(value);
142 ctx.qry.has_symref = 1; 142 ctx.qry.has_symref = 1;
143 } else if (!strcmp(name, "id")) { 143 } else if (!strcmp(name, "id")) {
144 ctx.qry.sha1 = xstrdup(value); 144 ctx.qry.sha1 = xstrdup(value);
145 ctx.qry.has_sha1 = 1; 145 ctx.qry.has_sha1 = 1;
146 } else if (!strcmp(name, "id2")) { 146 } else if (!strcmp(name, "id2")) {
147 ctx.qry.sha2 = xstrdup(value); 147 ctx.qry.sha2 = xstrdup(value);
148 ctx.qry.has_sha1 = 1; 148 ctx.qry.has_sha1 = 1;
149 } else if (!strcmp(name, "ofs")) { 149 } else if (!strcmp(name, "ofs")) {
150 ctx.qry.ofs = atoi(value); 150 ctx.qry.ofs = atoi(value);
151 } else if (!strcmp(name, "path")) { 151 } else if (!strcmp(name, "path")) {
152 ctx.qry.path = trim_end(value, '/'); 152 ctx.qry.path = trim_end(value, '/');
153 } else if (!strcmp(name, "name")) { 153 } else if (!strcmp(name, "name")) {
154 ctx.qry.name = xstrdup(value); 154 ctx.qry.name = xstrdup(value);
155 } else if (!strcmp(name, "mimetype")) { 155 } else if (!strcmp(name, "mimetype")) {
156 ctx.qry.mimetype = xstrdup(value); 156 ctx.qry.mimetype = xstrdup(value);
157 } else if (!strcmp(name, "s")){
158 ctx.qry.sort = xstrdup(value);
157 } 159 }
158} 160}
159 161
160static void prepare_context(struct cgit_context *ctx) 162static void prepare_context(struct cgit_context *ctx)
161{ 163{
162 memset(ctx, 0, sizeof(ctx)); 164 memset(ctx, 0, sizeof(ctx));
163 ctx->cfg.agefile = "info/web/last-modified"; 165 ctx->cfg.agefile = "info/web/last-modified";
164 ctx->cfg.nocache = 0; 166 ctx->cfg.nocache = 0;
165 ctx->cfg.cache_size = 0; 167 ctx->cfg.cache_size = 0;
166 ctx->cfg.cache_dynamic_ttl = 5; 168 ctx->cfg.cache_dynamic_ttl = 5;
167 ctx->cfg.cache_max_create_time = 5; 169 ctx->cfg.cache_max_create_time = 5;
168 ctx->cfg.cache_repo_ttl = 5; 170 ctx->cfg.cache_repo_ttl = 5;
169 ctx->cfg.cache_root = CGIT_CACHE_ROOT; 171 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
170 ctx->cfg.cache_root_ttl = 5; 172 ctx->cfg.cache_root_ttl = 5;
171 ctx->cfg.cache_static_ttl = -1; 173 ctx->cfg.cache_static_ttl = -1;
172 ctx->cfg.css = "/cgit.css"; 174 ctx->cfg.css = "/cgit.css";
173 ctx->cfg.logo = "/git-logo.png"; 175 ctx->cfg.logo = "/git-logo.png";
174 ctx->cfg.local_time = 0; 176 ctx->cfg.local_time = 0;
175 ctx->cfg.max_repo_count = 50; 177 ctx->cfg.max_repo_count = 50;
176 ctx->cfg.max_commit_count = 50; 178 ctx->cfg.max_commit_count = 50;
177 ctx->cfg.max_lock_attempts = 5; 179 ctx->cfg.max_lock_attempts = 5;
178 ctx->cfg.max_msg_len = 80; 180 ctx->cfg.max_msg_len = 80;
179 ctx->cfg.max_repodesc_len = 80; 181 ctx->cfg.max_repodesc_len = 80;
180 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; 182 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
181 ctx->cfg.renamelimit = -1; 183 ctx->cfg.renamelimit = -1;
182 ctx->cfg.robots = "index, nofollow"; 184 ctx->cfg.robots = "index, nofollow";
183 ctx->cfg.root_title = "Git repository browser"; 185 ctx->cfg.root_title = "Git repository browser";
184 ctx->cfg.root_desc = "a fast webinterface for the git dscm"; 186 ctx->cfg.root_desc = "a fast webinterface for the git dscm";
185 ctx->cfg.script_name = CGIT_SCRIPT_NAME; 187 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
186 ctx->cfg.summary_branches = 10; 188 ctx->cfg.summary_branches = 10;
187 ctx->cfg.summary_log = 10; 189 ctx->cfg.summary_log = 10;
188 ctx->cfg.summary_tags = 10; 190 ctx->cfg.summary_tags = 10;
189 ctx->page.mimetype = "text/html"; 191 ctx->page.mimetype = "text/html";
190 ctx->page.charset = PAGE_ENCODING; 192 ctx->page.charset = PAGE_ENCODING;
191 ctx->page.filename = NULL; 193 ctx->page.filename = NULL;
192 ctx->page.size = 0; 194 ctx->page.size = 0;
193 ctx->page.modified = time(NULL); 195 ctx->page.modified = time(NULL);
194 ctx->page.expires = ctx->page.modified; 196 ctx->page.expires = ctx->page.modified;
195} 197}
196 198
197struct refmatch { 199struct refmatch {
198 char *req_ref; 200 char *req_ref;
199 char *first_ref; 201 char *first_ref;
200 int match; 202 int match;
201}; 203};
202 204
203int find_current_ref(const char *refname, const unsigned char *sha1, 205int find_current_ref(const char *refname, const unsigned char *sha1,
204 int flags, void *cb_data) 206 int flags, void *cb_data)
205{ 207{
206 struct refmatch *info; 208 struct refmatch *info;
207 209
208 info = (struct refmatch *)cb_data; 210 info = (struct refmatch *)cb_data;
209 if (!strcmp(refname, info->req_ref)) 211 if (!strcmp(refname, info->req_ref))
210 info->match = 1; 212 info->match = 1;
211 if (!info->first_ref) 213 if (!info->first_ref)
212 info->first_ref = xstrdup(refname); 214 info->first_ref = xstrdup(refname);
213 return info->match; 215 return info->match;
214} 216}
215 217
216char *find_default_branch(struct cgit_repo *repo) 218char *find_default_branch(struct cgit_repo *repo)
217{ 219{
218 struct refmatch info; 220 struct refmatch info;
219 char *ref; 221 char *ref;
220 222
221 info.req_ref = repo->defbranch; 223 info.req_ref = repo->defbranch;
222 info.first_ref = NULL; 224 info.first_ref = NULL;
223 info.match = 0; 225 info.match = 0;
224 for_each_branch_ref(find_current_ref, &info); 226 for_each_branch_ref(find_current_ref, &info);
225 if (info.match) 227 if (info.match)
226 ref = info.req_ref; 228 ref = info.req_ref;
227 else 229 else
228 ref = info.first_ref; 230 ref = info.first_ref;
229 if (ref) 231 if (ref)
230 ref = xstrdup(ref); 232 ref = xstrdup(ref);
231 return ref; 233 return ref;
232} 234}
233 235
234static int prepare_repo_cmd(struct cgit_context *ctx) 236static int prepare_repo_cmd(struct cgit_context *ctx)
235{ 237{
236 char *tmp; 238 char *tmp;
237 unsigned char sha1[20]; 239 unsigned char sha1[20];
238 int nongit = 0; 240 int nongit = 0;
239 241
240 setenv("GIT_DIR", ctx->repo->path, 1); 242 setenv("GIT_DIR", ctx->repo->path, 1);
241 setup_git_directory_gently(&nongit); 243 setup_git_directory_gently(&nongit);
242 if (nongit) { 244 if (nongit) {
243 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, 245 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
244 "config error"); 246 "config error");
245 tmp = fmt("Not a git repository: '%s'", ctx->repo->path); 247 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
246 ctx->repo = NULL; 248 ctx->repo = NULL;
247 cgit_print_http_headers(ctx); 249 cgit_print_http_headers(ctx);
248 cgit_print_docstart(ctx); 250 cgit_print_docstart(ctx);
249 cgit_print_pageheader(ctx); 251 cgit_print_pageheader(ctx);
250 cgit_print_error(tmp); 252 cgit_print_error(tmp);
251 cgit_print_docend(); 253 cgit_print_docend();
252 return 1; 254 return 1;
253 } 255 }
254 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); 256 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
255 257
256 if (!ctx->qry.head) { 258 if (!ctx->qry.head) {
257 ctx->qry.nohead = 1; 259 ctx->qry.nohead = 1;
258 ctx->qry.head = find_default_branch(ctx->repo); 260 ctx->qry.head = find_default_branch(ctx->repo);
259 ctx->repo->defbranch = ctx->qry.head; 261 ctx->repo->defbranch = ctx->qry.head;
260 } 262 }
261 263
262 if (!ctx->qry.head) { 264 if (!ctx->qry.head) {
263 cgit_print_http_headers(ctx); 265 cgit_print_http_headers(ctx);
264 cgit_print_docstart(ctx); 266 cgit_print_docstart(ctx);
265 cgit_print_pageheader(ctx); 267 cgit_print_pageheader(ctx);
266 cgit_print_error("Repository seems to be empty"); 268 cgit_print_error("Repository seems to be empty");
267 cgit_print_docend(); 269 cgit_print_docend();
268 return 1; 270 return 1;
269 } 271 }
270 272
271 if (get_sha1(ctx->qry.head, sha1)) { 273 if (get_sha1(ctx->qry.head, sha1)) {
272 tmp = xstrdup(ctx->qry.head); 274 tmp = xstrdup(ctx->qry.head);
273 ctx->qry.head = ctx->repo->defbranch; 275 ctx->qry.head = ctx->repo->defbranch;
274 cgit_print_http_headers(ctx); 276 cgit_print_http_headers(ctx);
275 cgit_print_docstart(ctx); 277 cgit_print_docstart(ctx);
276 cgit_print_pageheader(ctx); 278 cgit_print_pageheader(ctx);
277 cgit_print_error(fmt("Invalid branch: %s", tmp)); 279 cgit_print_error(fmt("Invalid branch: %s", tmp));
278 cgit_print_docend(); 280 cgit_print_docend();
279 return 1; 281 return 1;
280 } 282 }
281 return 0; 283 return 0;
282} 284}
283 285
284static void process_request(void *cbdata) 286static void process_request(void *cbdata)
285{ 287{
286 struct cgit_context *ctx = cbdata; 288 struct cgit_context *ctx = cbdata;
287 struct cgit_cmd *cmd; 289 struct cgit_cmd *cmd;
288 290
289 cmd = cgit_get_cmd(ctx); 291 cmd = cgit_get_cmd(ctx);
290 if (!cmd) { 292 if (!cmd) {
291 ctx->page.title = "cgit error"; 293 ctx->page.title = "cgit error";
292 ctx->repo = NULL; 294 ctx->repo = NULL;
293 cgit_print_http_headers(ctx); 295 cgit_print_http_headers(ctx);
294 cgit_print_docstart(ctx); 296 cgit_print_docstart(ctx);
295 cgit_print_pageheader(ctx); 297 cgit_print_pageheader(ctx);
296 cgit_print_error("Invalid request"); 298 cgit_print_error("Invalid request");
297 cgit_print_docend(); 299 cgit_print_docend();
298 return; 300 return;
299 } 301 }
300 302
301 if (cmd->want_repo && !ctx->repo) { 303 if (cmd->want_repo && !ctx->repo) {
302 cgit_print_http_headers(ctx); 304 cgit_print_http_headers(ctx);
303 cgit_print_docstart(ctx); 305 cgit_print_docstart(ctx);
304 cgit_print_pageheader(ctx); 306 cgit_print_pageheader(ctx);
305 cgit_print_error(fmt("No repository selected")); 307 cgit_print_error(fmt("No repository selected"));
306 cgit_print_docend(); 308 cgit_print_docend();
307 return; 309 return;
308 } 310 }
309 311
310 if (ctx->repo && prepare_repo_cmd(ctx)) 312 if (ctx->repo && prepare_repo_cmd(ctx))
311 return; 313 return;
312 314
313 if (cmd->want_layout) { 315 if (cmd->want_layout) {
314 cgit_print_http_headers(ctx); 316 cgit_print_http_headers(ctx);
315 cgit_print_docstart(ctx); 317 cgit_print_docstart(ctx);
316 cgit_print_pageheader(ctx); 318 cgit_print_pageheader(ctx);
317 } 319 }
318 320
319 cmd->fn(ctx); 321 cmd->fn(ctx);
320 322
321 if (cmd->want_layout) 323 if (cmd->want_layout)
322 cgit_print_docend(); 324 cgit_print_docend();
323} 325}
324 326
325int cmp_repos(const void *a, const void *b) 327int cmp_repos(const void *a, const void *b)
326{ 328{
327 const struct cgit_repo *ra = a, *rb = b; 329 const struct cgit_repo *ra = a, *rb = b;
328 return strcmp(ra->url, rb->url); 330 return strcmp(ra->url, rb->url);
329} 331}
330 332
331void print_repo(struct cgit_repo *repo) 333void print_repo(struct cgit_repo *repo)
332{ 334{
333 printf("repo.url=%s\n", repo->url); 335 printf("repo.url=%s\n", repo->url);
334 printf("repo.name=%s\n", repo->name); 336 printf("repo.name=%s\n", repo->name);
335 printf("repo.path=%s\n", repo->path); 337 printf("repo.path=%s\n", repo->path);
336 if (repo->owner) 338 if (repo->owner)
337 printf("repo.owner=%s\n", repo->owner); 339 printf("repo.owner=%s\n", repo->owner);
338 if (repo->desc) 340 if (repo->desc)
339 printf("repo.desc=%s\n", repo->desc); 341 printf("repo.desc=%s\n", repo->desc);
340 if (repo->readme) 342 if (repo->readme)
341 printf("repo.readme=%s\n", repo->readme); 343 printf("repo.readme=%s\n", repo->readme);
342 printf("\n"); 344 printf("\n");
343} 345}
344 346
345void print_repolist(struct cgit_repolist *list) 347void print_repolist(struct cgit_repolist *list)
346{ 348{
347 int i; 349 int i;
348 350
349 for(i = 0; i < list->count; i++) 351 for(i = 0; i < list->count; i++)
350 print_repo(&list->repos[i]); 352 print_repo(&list->repos[i]);
351} 353}
352 354
353 355
354static void cgit_parse_args(int argc, const char **argv) 356static void cgit_parse_args(int argc, const char **argv)
355{ 357{
356 int i; 358 int i;
357 int scan = 0; 359 int scan = 0;
358 360
359 for (i = 1; i < argc; i++) { 361 for (i = 1; i < argc; i++) {
360 if (!strncmp(argv[i], "--cache=", 8)) { 362 if (!strncmp(argv[i], "--cache=", 8)) {
361 ctx.cfg.cache_root = xstrdup(argv[i]+8); 363 ctx.cfg.cache_root = xstrdup(argv[i]+8);
362 } 364 }
363 if (!strcmp(argv[i], "--nocache")) { 365 if (!strcmp(argv[i], "--nocache")) {
364 ctx.cfg.nocache = 1; 366 ctx.cfg.nocache = 1;
365 } 367 }
366 if (!strncmp(argv[i], "--query=", 8)) { 368 if (!strncmp(argv[i], "--query=", 8)) {
367 ctx.qry.raw = xstrdup(argv[i]+8); 369 ctx.qry.raw = xstrdup(argv[i]+8);
368 } 370 }
369 if (!strncmp(argv[i], "--repo=", 7)) { 371 if (!strncmp(argv[i], "--repo=", 7)) {
370 ctx.qry.repo = xstrdup(argv[i]+7); 372 ctx.qry.repo = xstrdup(argv[i]+7);
371 } 373 }
372 if (!strncmp(argv[i], "--page=", 7)) { 374 if (!strncmp(argv[i], "--page=", 7)) {
373 ctx.qry.page = xstrdup(argv[i]+7); 375 ctx.qry.page = xstrdup(argv[i]+7);
374 } 376 }
375 if (!strncmp(argv[i], "--head=", 7)) { 377 if (!strncmp(argv[i], "--head=", 7)) {
376 ctx.qry.head = xstrdup(argv[i]+7); 378 ctx.qry.head = xstrdup(argv[i]+7);
377 ctx.qry.has_symref = 1; 379 ctx.qry.has_symref = 1;
378 } 380 }
379 if (!strncmp(argv[i], "--sha1=", 7)) { 381 if (!strncmp(argv[i], "--sha1=", 7)) {
380 ctx.qry.sha1 = xstrdup(argv[i]+7); 382 ctx.qry.sha1 = xstrdup(argv[i]+7);
381 ctx.qry.has_sha1 = 1; 383 ctx.qry.has_sha1 = 1;
382 } 384 }
383 if (!strncmp(argv[i], "--ofs=", 6)) { 385 if (!strncmp(argv[i], "--ofs=", 6)) {
384 ctx.qry.ofs = atoi(argv[i]+6); 386 ctx.qry.ofs = atoi(argv[i]+6);
385 } 387 }
386 if (!strncmp(argv[i], "--scan-tree=", 12)) { 388 if (!strncmp(argv[i], "--scan-tree=", 12)) {
387 scan++; 389 scan++;
388 scan_tree(argv[i] + 12); 390 scan_tree(argv[i] + 12);
389 } 391 }
390 } 392 }
391 if (scan) { 393 if (scan) {
392 qsort(cgit_repolist.repos, cgit_repolist.count, 394 qsort(cgit_repolist.repos, cgit_repolist.count,
393 sizeof(struct cgit_repo), cmp_repos); 395 sizeof(struct cgit_repo), cmp_repos);
394 print_repolist(&cgit_repolist); 396 print_repolist(&cgit_repolist);
395 exit(0); 397 exit(0);
396 } 398 }
397} 399}
398 400
399static int calc_ttl() 401static int calc_ttl()
400{ 402{
401 if (!ctx.repo) 403 if (!ctx.repo)
402 return ctx.cfg.cache_root_ttl; 404 return ctx.cfg.cache_root_ttl;
403 405
404 if (!ctx.qry.page) 406 if (!ctx.qry.page)
405 return ctx.cfg.cache_repo_ttl; 407 return ctx.cfg.cache_repo_ttl;
406 408
407 if (ctx.qry.has_symref) 409 if (ctx.qry.has_symref)
408 return ctx.cfg.cache_dynamic_ttl; 410 return ctx.cfg.cache_dynamic_ttl;
409 411
410 if (ctx.qry.has_sha1) 412 if (ctx.qry.has_sha1)
411 return ctx.cfg.cache_static_ttl; 413 return ctx.cfg.cache_static_ttl;
412 414
413 return ctx.cfg.cache_repo_ttl; 415 return ctx.cfg.cache_repo_ttl;
414} 416}
415 417
416int main(int argc, const char **argv) 418int main(int argc, const char **argv)
417{ 419{
418 const char *cgit_config_env = getenv("CGIT_CONFIG"); 420 const char *cgit_config_env = getenv("CGIT_CONFIG");
419 const char *path; 421 const char *path;
420 char *qry; 422 char *qry;
421 int err, ttl; 423 int err, ttl;
422 424
423 prepare_context(&ctx); 425 prepare_context(&ctx);
424 cgit_repolist.length = 0; 426 cgit_repolist.length = 0;
425 cgit_repolist.count = 0; 427 cgit_repolist.count = 0;
426 cgit_repolist.repos = NULL; 428 cgit_repolist.repos = NULL;
427 429
428 if (getenv("SCRIPT_NAME")) 430 if (getenv("SCRIPT_NAME"))
429 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); 431 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
430 if (getenv("QUERY_STRING")) 432 if (getenv("QUERY_STRING"))
431 ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); 433 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
432 cgit_parse_args(argc, argv); 434 cgit_parse_args(argc, argv);
433 parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG, 435 parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
434 config_cb); 436 config_cb);
435 ctx.repo = NULL; 437 ctx.repo = NULL;
436 http_parse_querystring(ctx.qry.raw, querystring_cb); 438 http_parse_querystring(ctx.qry.raw, querystring_cb);
437 439
438 /* If virtual-root isn't specified in cgitrc and no url 440 /* If virtual-root isn't specified in cgitrc and no url
439 * parameter is specified on the querystring, lets pretend 441 * parameter is specified on the querystring, lets pretend
440 * that virtualroot equals SCRIPT_NAME and use PATH_INFO as 442 * that virtualroot equals SCRIPT_NAME and use PATH_INFO as
441 * url. This allows cgit to work with virtual urls without 443 * url. This allows cgit to work with virtual urls without
442 * the need for rewriterules in the webserver (as long as 444 * the need for rewriterules in the webserver (as long as
443 * PATH_INFO is included in the cache lookup key). 445 * PATH_INFO is included in the cache lookup key).
444 */ 446 */
445 if (!ctx.cfg.virtual_root && !ctx.qry.url) { 447 if (!ctx.cfg.virtual_root && !ctx.qry.url) {
446 ctx.cfg.virtual_root = ctx.cfg.script_name; 448 ctx.cfg.virtual_root = ctx.cfg.script_name;
447 path = getenv("PATH_INFO"); 449 path = getenv("PATH_INFO");
448 if (path) { 450 if (path) {
449 if (path[0] == '/') 451 if (path[0] == '/')
450 path++; 452 path++;
451 ctx.qry.url = xstrdup(path); 453 ctx.qry.url = xstrdup(path);
452 if (ctx.qry.raw) { 454 if (ctx.qry.raw) {
453 qry = ctx.qry.raw; 455 qry = ctx.qry.raw;
454 ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry)); 456 ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry));
455 free(qry); 457 free(qry);
456 } else 458 } else
457 ctx.qry.raw = ctx.qry.url; 459 ctx.qry.raw = ctx.qry.url;
458 cgit_parse_url(ctx.qry.url); 460 cgit_parse_url(ctx.qry.url);
459 } 461 }
460 } 462 }
461 463
462 ttl = calc_ttl(); 464 ttl = calc_ttl();
463 ctx.page.expires += ttl*60; 465 ctx.page.expires += ttl*60;
464 if (ctx.cfg.nocache) 466 if (ctx.cfg.nocache)
465 ctx.cfg.cache_size = 0; 467 ctx.cfg.cache_size = 0;
466 err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root, 468 err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root,
467 ctx.qry.raw, ttl, process_request, &ctx); 469 ctx.qry.raw, ttl, process_request, &ctx);
468 if (err) 470 if (err)
469 cgit_print_error(fmt("Error processing page: %s (%d)", 471 cgit_print_error(fmt("Error processing page: %s (%d)",
470 strerror(err), err)); 472 strerror(err), err));
471 return err; 473 return err;
472} 474}
diff --git a/cgit.h b/cgit.h
index 92f0c5a..f1fbeca 100644
--- a/cgit.h
+++ b/cgit.h
@@ -1,237 +1,239 @@
1#ifndef CGIT_H 1#ifndef CGIT_H
2#define CGIT_H 2#define CGIT_H
3 3
4 4
5#include <git-compat-util.h> 5#include <git-compat-util.h>
6#include <cache.h> 6#include <cache.h>
7#include <grep.h> 7#include <grep.h>
8#include <object.h> 8#include <object.h>
9#include <tree.h> 9#include <tree.h>
10#include <commit.h> 10#include <commit.h>
11#include <tag.h> 11#include <tag.h>
12#include <diff.h> 12#include <diff.h>
13#include <diffcore.h> 13#include <diffcore.h>
14#include <refs.h> 14#include <refs.h>
15#include <revision.h> 15#include <revision.h>
16#include <log-tree.h> 16#include <log-tree.h>
17#include <archive.h> 17#include <archive.h>
18#include <xdiff/xdiff.h> 18#include <xdiff/xdiff.h>
19#include <utf8.h> 19#include <utf8.h>
20 20
21 21
22/* 22/*
23 * Dateformats used on misc. pages 23 * Dateformats used on misc. pages
24 */ 24 */
25#define FMT_LONGDATE "%Y-%m-%d %H:%M:%S (%Z)" 25#define FMT_LONGDATE "%Y-%m-%d %H:%M:%S (%Z)"
26#define FMT_SHORTDATE "%Y-%m-%d" 26#define FMT_SHORTDATE "%Y-%m-%d"
27#define FMT_ATOMDATE "%Y-%m-%dT%H:%M:%SZ" 27#define FMT_ATOMDATE "%Y-%m-%dT%H:%M:%SZ"
28 28
29 29
30/* 30/*
31 * Limits used for relative dates 31 * Limits used for relative dates
32 */ 32 */
33#define TM_MIN 60 33#define TM_MIN 60
34#define TM_HOUR (TM_MIN * 60) 34#define TM_HOUR (TM_MIN * 60)
35#define TM_DAY (TM_HOUR * 24) 35#define TM_DAY (TM_HOUR * 24)
36#define TM_WEEK (TM_DAY * 7) 36#define TM_WEEK (TM_DAY * 7)
37#define TM_YEAR (TM_DAY * 365) 37#define TM_YEAR (TM_DAY * 365)
38#define TM_MONTH (TM_YEAR / 12.0) 38#define TM_MONTH (TM_YEAR / 12.0)
39 39
40 40
41/* 41/*
42 * Default encoding 42 * Default encoding
43 */ 43 */
44#define PAGE_ENCODING "UTF-8" 44#define PAGE_ENCODING "UTF-8"
45 45
46typedef void (*configfn)(const char *name, const char *value); 46typedef void (*configfn)(const char *name, const char *value);
47typedef void (*filepair_fn)(struct diff_filepair *pair); 47typedef void (*filepair_fn)(struct diff_filepair *pair);
48typedef void (*linediff_fn)(char *line, int len); 48typedef void (*linediff_fn)(char *line, int len);
49 49
50struct cgit_repo { 50struct cgit_repo {
51 char *url; 51 char *url;
52 char *name; 52 char *name;
53 char *path; 53 char *path;
54 char *desc; 54 char *desc;
55 char *owner; 55 char *owner;
56 char *defbranch; 56 char *defbranch;
57 char *group; 57 char *group;
58 char *module_link; 58 char *module_link;
59 char *readme; 59 char *readme;
60 char *clone_url; 60 char *clone_url;
61 int snapshots; 61 int snapshots;
62 int enable_log_filecount; 62 int enable_log_filecount;
63 int enable_log_linecount; 63 int enable_log_linecount;
64 time_t mtime;
64}; 65};
65 66
66struct cgit_repolist { 67struct cgit_repolist {
67 int length; 68 int length;
68 int count; 69 int count;
69 struct cgit_repo *repos; 70 struct cgit_repo *repos;
70}; 71};
71 72
72struct commitinfo { 73struct commitinfo {
73 struct commit *commit; 74 struct commit *commit;
74 char *author; 75 char *author;
75 char *author_email; 76 char *author_email;
76 unsigned long author_date; 77 unsigned long author_date;
77 char *committer; 78 char *committer;
78 char *committer_email; 79 char *committer_email;
79 unsigned long committer_date; 80 unsigned long committer_date;
80 char *subject; 81 char *subject;
81 char *msg; 82 char *msg;
82 char *msg_encoding; 83 char *msg_encoding;
83}; 84};
84 85
85struct taginfo { 86struct taginfo {
86 char *tagger; 87 char *tagger;
87 char *tagger_email; 88 char *tagger_email;
88 unsigned long tagger_date; 89 unsigned long tagger_date;
89 char *msg; 90 char *msg;
90}; 91};
91 92
92struct refinfo { 93struct refinfo {
93 const char *refname; 94 const char *refname;
94 struct object *object; 95 struct object *object;
95 union { 96 union {
96 struct taginfo *tag; 97 struct taginfo *tag;
97 struct commitinfo *commit; 98 struct commitinfo *commit;
98 }; 99 };
99}; 100};
100 101
101struct reflist { 102struct reflist {
102 struct refinfo **refs; 103 struct refinfo **refs;
103 int alloc; 104 int alloc;
104 int count; 105 int count;
105}; 106};
106 107
107struct cgit_query { 108struct cgit_query {
108 int has_symref; 109 int has_symref;
109 int has_sha1; 110 int has_sha1;
110 char *raw; 111 char *raw;
111 char *repo; 112 char *repo;
112 char *page; 113 char *page;
113 char *search; 114 char *search;
114 char *grep; 115 char *grep;
115 char *head; 116 char *head;
116 char *sha1; 117 char *sha1;
117 char *sha2; 118 char *sha2;
118 char *path; 119 char *path;
119 char *name; 120 char *name;
120 char *mimetype; 121 char *mimetype;
121 char *url; 122 char *url;
122 int ofs; 123 int ofs;
123 int nohead; 124 int nohead;
125 char *sort;
124}; 126};
125 127
126struct cgit_config { 128struct cgit_config {
127 char *agefile; 129 char *agefile;
128 char *cache_root; 130 char *cache_root;
129 char *clone_prefix; 131 char *clone_prefix;
130 char *css; 132 char *css;
131 char *favicon; 133 char *favicon;
132 char *footer; 134 char *footer;
133 char *index_header; 135 char *index_header;
134 char *index_info; 136 char *index_info;
135 char *logo; 137 char *logo;
136 char *logo_link; 138 char *logo_link;
137 char *module_link; 139 char *module_link;
138 char *repo_group; 140 char *repo_group;
139 char *robots; 141 char *robots;
140 char *root_title; 142 char *root_title;
141 char *root_desc; 143 char *root_desc;
142 char *root_readme; 144 char *root_readme;
143 char *script_name; 145 char *script_name;
144 char *virtual_root; 146 char *virtual_root;
145 int cache_size; 147 int cache_size;
146 int cache_dynamic_ttl; 148 int cache_dynamic_ttl;
147 int cache_max_create_time; 149 int cache_max_create_time;
148 int cache_repo_ttl; 150 int cache_repo_ttl;
149 int cache_root_ttl; 151 int cache_root_ttl;
150 int cache_static_ttl; 152 int cache_static_ttl;
151 int enable_index_links; 153 int enable_index_links;
152 int enable_log_filecount; 154 int enable_log_filecount;
153 int enable_log_linecount; 155 int enable_log_linecount;
154 int local_time; 156 int local_time;
155 int max_repo_count; 157 int max_repo_count;
156 int max_commit_count; 158 int max_commit_count;
157 int max_lock_attempts; 159 int max_lock_attempts;
158 int max_msg_len; 160 int max_msg_len;
159 int max_repodesc_len; 161 int max_repodesc_len;
160 int nocache; 162 int nocache;
161 int renamelimit; 163 int renamelimit;
162 int snapshots; 164 int snapshots;
163 int summary_branches; 165 int summary_branches;
164 int summary_log; 166 int summary_log;
165 int summary_tags; 167 int summary_tags;
166}; 168};
167 169
168struct cgit_page { 170struct cgit_page {
169 time_t modified; 171 time_t modified;
170 time_t expires; 172 time_t expires;
171 size_t size; 173 size_t size;
172 char *mimetype; 174 char *mimetype;
173 char *charset; 175 char *charset;
174 char *filename; 176 char *filename;
175 char *title; 177 char *title;
176}; 178};
177 179
178struct cgit_context { 180struct cgit_context {
179 struct cgit_query qry; 181 struct cgit_query qry;
180 struct cgit_config cfg; 182 struct cgit_config cfg;
181 struct cgit_repo *repo; 183 struct cgit_repo *repo;
182 struct cgit_page page; 184 struct cgit_page page;
183}; 185};
184 186
185struct cgit_snapshot_format { 187struct cgit_snapshot_format {
186 const char *suffix; 188 const char *suffix;
187 const char *mimetype; 189 const char *mimetype;
188 write_archive_fn_t write_func; 190 write_archive_fn_t write_func;
189 int bit; 191 int bit;
190}; 192};
191 193
192extern const char *cgit_version; 194extern const char *cgit_version;
193 195
194extern struct cgit_repolist cgit_repolist; 196extern struct cgit_repolist cgit_repolist;
195extern struct cgit_context ctx; 197extern struct cgit_context ctx;
196extern const struct cgit_snapshot_format cgit_snapshot_formats[]; 198extern const struct cgit_snapshot_format cgit_snapshot_formats[];
197 199
198extern struct cgit_repo *cgit_add_repo(const char *url); 200extern struct cgit_repo *cgit_add_repo(const char *url);
199extern struct cgit_repo *cgit_get_repoinfo(const char *url); 201extern struct cgit_repo *cgit_get_repoinfo(const char *url);
200extern void cgit_repo_config_cb(const char *name, const char *value); 202extern void cgit_repo_config_cb(const char *name, const char *value);
201 203
202extern int chk_zero(int result, char *msg); 204extern int chk_zero(int result, char *msg);
203extern int chk_positive(int result, char *msg); 205extern int chk_positive(int result, char *msg);
204extern int chk_non_negative(int result, char *msg); 206extern int chk_non_negative(int result, char *msg);
205 207
206extern char *trim_end(const char *str, char c); 208extern char *trim_end(const char *str, char c);
207extern char *strlpart(char *txt, int maxlen); 209extern char *strlpart(char *txt, int maxlen);
208extern char *strrpart(char *txt, int maxlen); 210extern char *strrpart(char *txt, int maxlen);
209 211
210extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); 212extern void cgit_add_ref(struct reflist *list, struct refinfo *ref);
211extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, 213extern int cgit_refs_cb(const char *refname, const unsigned char *sha1,
212 int flags, void *cb_data); 214 int flags, void *cb_data);
213 215
214extern void *cgit_free_commitinfo(struct commitinfo *info); 216extern void *cgit_free_commitinfo(struct commitinfo *info);
215 217
216extern int cgit_diff_files(const unsigned char *old_sha1, 218extern int cgit_diff_files(const unsigned char *old_sha1,
217 const unsigned char *new_sha1, 219 const unsigned char *new_sha1,
218 linediff_fn fn); 220 linediff_fn fn);
219 221
220extern void cgit_diff_tree(const unsigned char *old_sha1, 222extern void cgit_diff_tree(const unsigned char *old_sha1,
221 const unsigned char *new_sha1, 223 const unsigned char *new_sha1,
222 filepair_fn fn, const char *prefix); 224 filepair_fn fn, const char *prefix);
223 225
224extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); 226extern void cgit_diff_commit(struct commit *commit, filepair_fn fn);
225 227
226extern char *fmt(const char *format,...); 228extern char *fmt(const char *format,...);
227 229
228extern struct commitinfo *cgit_parse_commit(struct commit *commit); 230extern struct commitinfo *cgit_parse_commit(struct commit *commit);
229extern struct taginfo *cgit_parse_tag(struct tag *tag); 231extern struct taginfo *cgit_parse_tag(struct tag *tag);
230extern void cgit_parse_url(const char *url); 232extern void cgit_parse_url(const char *url);
231 233
232extern const char *cgit_repobasename(const char *reponame); 234extern const char *cgit_repobasename(const char *reponame);
233 235
234extern int cgit_parse_snapshots_mask(const char *str); 236extern int cgit_parse_snapshots_mask(const char *str);
235 237
236 238
237#endif /* CGIT_H */ 239#endif /* CGIT_H */
diff --git a/shared.c b/shared.c
index f5875e4..89d1bab 100644
--- a/shared.c
+++ b/shared.c
@@ -1,344 +1,345 @@
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 cgit_repolist cgit_repolist; 11struct cgit_repolist cgit_repolist;
12struct cgit_context ctx; 12struct cgit_context ctx;
13int cgit_cmd; 13int cgit_cmd;
14 14
15int chk_zero(int result, char *msg) 15int chk_zero(int result, char *msg)
16{ 16{
17 if (result != 0) 17 if (result != 0)
18 die("%s: %s", msg, strerror(errno)); 18 die("%s: %s", msg, strerror(errno));
19 return result; 19 return result;
20} 20}
21 21
22int chk_positive(int result, char *msg) 22int chk_positive(int result, char *msg)
23{ 23{
24 if (result <= 0) 24 if (result <= 0)
25 die("%s: %s", msg, strerror(errno)); 25 die("%s: %s", msg, strerror(errno));
26 return result; 26 return result;
27} 27}
28 28
29int chk_non_negative(int result, char *msg) 29int chk_non_negative(int result, char *msg)
30{ 30{
31 if (result < 0) 31 if (result < 0)
32 die("%s: %s",msg, strerror(errno)); 32 die("%s: %s",msg, strerror(errno));
33 return result; 33 return result;
34} 34}
35 35
36struct cgit_repo *cgit_add_repo(const char *url) 36struct cgit_repo *cgit_add_repo(const char *url)
37{ 37{
38 struct cgit_repo *ret; 38 struct cgit_repo *ret;
39 39
40 if (++cgit_repolist.count > cgit_repolist.length) { 40 if (++cgit_repolist.count > cgit_repolist.length) {
41 if (cgit_repolist.length == 0) 41 if (cgit_repolist.length == 0)
42 cgit_repolist.length = 8; 42 cgit_repolist.length = 8;
43 else 43 else
44 cgit_repolist.length *= 2; 44 cgit_repolist.length *= 2;
45 cgit_repolist.repos = xrealloc(cgit_repolist.repos, 45 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
46 cgit_repolist.length * 46 cgit_repolist.length *
47 sizeof(struct cgit_repo)); 47 sizeof(struct cgit_repo));
48 } 48 }
49 49
50 ret = &cgit_repolist.repos[cgit_repolist.count-1]; 50 ret = &cgit_repolist.repos[cgit_repolist.count-1];
51 ret->url = trim_end(url, '/'); 51 ret->url = trim_end(url, '/');
52 ret->name = ret->url; 52 ret->name = ret->url;
53 ret->path = NULL; 53 ret->path = NULL;
54 ret->desc = "[no description]"; 54 ret->desc = "[no description]";
55 ret->owner = NULL; 55 ret->owner = NULL;
56 ret->group = ctx.cfg.repo_group; 56 ret->group = ctx.cfg.repo_group;
57 ret->defbranch = "master"; 57 ret->defbranch = "master";
58 ret->snapshots = ctx.cfg.snapshots; 58 ret->snapshots = ctx.cfg.snapshots;
59 ret->enable_log_filecount = ctx.cfg.enable_log_filecount; 59 ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
60 ret->enable_log_linecount = ctx.cfg.enable_log_linecount; 60 ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
61 ret->module_link = ctx.cfg.module_link; 61 ret->module_link = ctx.cfg.module_link;
62 ret->readme = NULL; 62 ret->readme = NULL;
63 ret->mtime = -1;
63 return ret; 64 return ret;
64} 65}
65 66
66struct cgit_repo *cgit_get_repoinfo(const char *url) 67struct cgit_repo *cgit_get_repoinfo(const char *url)
67{ 68{
68 int i; 69 int i;
69 struct cgit_repo *repo; 70 struct cgit_repo *repo;
70 71
71 for (i=0; i<cgit_repolist.count; i++) { 72 for (i=0; i<cgit_repolist.count; i++) {
72 repo = &cgit_repolist.repos[i]; 73 repo = &cgit_repolist.repos[i];
73 if (!strcmp(repo->url, url)) 74 if (!strcmp(repo->url, url))
74 return repo; 75 return repo;
75 } 76 }
76 return NULL; 77 return NULL;
77} 78}
78 79
79void *cgit_free_commitinfo(struct commitinfo *info) 80void *cgit_free_commitinfo(struct commitinfo *info)
80{ 81{
81 free(info->author); 82 free(info->author);
82 free(info->author_email); 83 free(info->author_email);
83 free(info->committer); 84 free(info->committer);
84 free(info->committer_email); 85 free(info->committer_email);
85 free(info->subject); 86 free(info->subject);
86 free(info->msg); 87 free(info->msg);
87 free(info->msg_encoding); 88 free(info->msg_encoding);
88 free(info); 89 free(info);
89 return NULL; 90 return NULL;
90} 91}
91 92
92char *trim_end(const char *str, char c) 93char *trim_end(const char *str, char c)
93{ 94{
94 int len; 95 int len;
95 char *s, *t; 96 char *s, *t;
96 97
97 if (str == NULL) 98 if (str == NULL)
98 return NULL; 99 return NULL;
99 t = (char *)str; 100 t = (char *)str;
100 len = strlen(t); 101 len = strlen(t);
101 while(len > 0 && t[len - 1] == c) 102 while(len > 0 && t[len - 1] == c)
102 len--; 103 len--;
103 104
104 if (len == 0) 105 if (len == 0)
105 return NULL; 106 return NULL;
106 107
107 c = t[len]; 108 c = t[len];
108 t[len] = '\0'; 109 t[len] = '\0';
109 s = xstrdup(t); 110 s = xstrdup(t);
110 t[len] = c; 111 t[len] = c;
111 return s; 112 return s;
112} 113}
113 114
114char *strlpart(char *txt, int maxlen) 115char *strlpart(char *txt, int maxlen)
115{ 116{
116 char *result; 117 char *result;
117 118
118 if (!txt) 119 if (!txt)
119 return txt; 120 return txt;
120 121
121 if (strlen(txt) <= maxlen) 122 if (strlen(txt) <= maxlen)
122 return txt; 123 return txt;
123 result = xmalloc(maxlen + 1); 124 result = xmalloc(maxlen + 1);
124 memcpy(result, txt, maxlen - 3); 125 memcpy(result, txt, maxlen - 3);
125 result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; 126 result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.';
126 result[maxlen] = '\0'; 127 result[maxlen] = '\0';
127 return result; 128 return result;
128} 129}
129 130
130char *strrpart(char *txt, int maxlen) 131char *strrpart(char *txt, int maxlen)
131{ 132{
132 char *result; 133 char *result;
133 134
134 if (!txt) 135 if (!txt)
135 return txt; 136 return txt;
136 137
137 if (strlen(txt) <= maxlen) 138 if (strlen(txt) <= maxlen)
138 return txt; 139 return txt;
139 result = xmalloc(maxlen + 1); 140 result = xmalloc(maxlen + 1);
140 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); 141 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3);
141 result[0] = result[1] = result[2] = '.'; 142 result[0] = result[1] = result[2] = '.';
142 return result; 143 return result;
143} 144}
144 145
145void cgit_add_ref(struct reflist *list, struct refinfo *ref) 146void cgit_add_ref(struct reflist *list, struct refinfo *ref)
146{ 147{
147 size_t size; 148 size_t size;
148 149
149 if (list->count >= list->alloc) { 150 if (list->count >= list->alloc) {
150 list->alloc += (list->alloc ? list->alloc : 4); 151 list->alloc += (list->alloc ? list->alloc : 4);
151 size = list->alloc * sizeof(struct refinfo *); 152 size = list->alloc * sizeof(struct refinfo *);
152 list->refs = xrealloc(list->refs, size); 153 list->refs = xrealloc(list->refs, size);
153 } 154 }
154 list->refs[list->count++] = ref; 155 list->refs[list->count++] = ref;
155} 156}
156 157
157struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) 158struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1)
158{ 159{
159 struct refinfo *ref; 160 struct refinfo *ref;
160 161
161 ref = xmalloc(sizeof (struct refinfo)); 162 ref = xmalloc(sizeof (struct refinfo));
162 ref->refname = xstrdup(refname); 163 ref->refname = xstrdup(refname);
163 ref->object = parse_object(sha1); 164 ref->object = parse_object(sha1);
164 switch (ref->object->type) { 165 switch (ref->object->type) {
165 case OBJ_TAG: 166 case OBJ_TAG:
166 ref->tag = cgit_parse_tag((struct tag *)ref->object); 167 ref->tag = cgit_parse_tag((struct tag *)ref->object);
167 break; 168 break;
168 case OBJ_COMMIT: 169 case OBJ_COMMIT:
169 ref->commit = cgit_parse_commit((struct commit *)ref->object); 170 ref->commit = cgit_parse_commit((struct commit *)ref->object);
170 break; 171 break;
171 } 172 }
172 return ref; 173 return ref;
173} 174}
174 175
175int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags, 176int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags,
176 void *cb_data) 177 void *cb_data)
177{ 178{
178 struct reflist *list = (struct reflist *)cb_data; 179 struct reflist *list = (struct reflist *)cb_data;
179 struct refinfo *info = cgit_mk_refinfo(refname, sha1); 180 struct refinfo *info = cgit_mk_refinfo(refname, sha1);
180 181
181 if (info) 182 if (info)
182 cgit_add_ref(list, info); 183 cgit_add_ref(list, info);
183 return 0; 184 return 0;
184} 185}
185 186
186void cgit_diff_tree_cb(struct diff_queue_struct *q, 187void cgit_diff_tree_cb(struct diff_queue_struct *q,
187 struct diff_options *options, void *data) 188 struct diff_options *options, void *data)
188{ 189{
189 int i; 190 int i;
190 191
191 for (i = 0; i < q->nr; i++) { 192 for (i = 0; i < q->nr; i++) {
192 if (q->queue[i]->status == 'U') 193 if (q->queue[i]->status == 'U')
193 continue; 194 continue;
194 ((filepair_fn)data)(q->queue[i]); 195 ((filepair_fn)data)(q->queue[i]);
195 } 196 }
196} 197}
197 198
198static int load_mmfile(mmfile_t *file, const unsigned char *sha1) 199static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
199{ 200{
200 enum object_type type; 201 enum object_type type;
201 202
202 if (is_null_sha1(sha1)) { 203 if (is_null_sha1(sha1)) {
203 file->ptr = (char *)""; 204 file->ptr = (char *)"";
204 file->size = 0; 205 file->size = 0;
205 } else { 206 } else {
206 file->ptr = read_sha1_file(sha1, &type, 207 file->ptr = read_sha1_file(sha1, &type,
207 (unsigned long *)&file->size); 208 (unsigned long *)&file->size);
208 } 209 }
209 return 1; 210 return 1;
210} 211}
211 212
212/* 213/*
213 * Receive diff-buffers from xdiff and concatenate them as 214 * Receive diff-buffers from xdiff and concatenate them as
214 * needed across multiple callbacks. 215 * needed across multiple callbacks.
215 * 216 *
216 * This is basically a copy of xdiff-interface.c/xdiff_outf(), 217 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
217 * ripped from git and modified to use globals instead of 218 * ripped from git and modified to use globals instead of
218 * a special callback-struct. 219 * a special callback-struct.
219 */ 220 */
220char *diffbuf = NULL; 221char *diffbuf = NULL;
221int buflen = 0; 222int buflen = 0;
222 223
223int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) 224int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
224{ 225{
225 int i; 226 int i;
226 227
227 for (i = 0; i < nbuf; i++) { 228 for (i = 0; i < nbuf; i++) {
228 if (mb[i].ptr[mb[i].size-1] != '\n') { 229 if (mb[i].ptr[mb[i].size-1] != '\n') {
229 /* Incomplete line */ 230 /* Incomplete line */
230 diffbuf = xrealloc(diffbuf, buflen + mb[i].size); 231 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
231 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); 232 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
232 buflen += mb[i].size; 233 buflen += mb[i].size;
233 continue; 234 continue;
234 } 235 }
235 236
236 /* we have a complete line */ 237 /* we have a complete line */
237 if (!diffbuf) { 238 if (!diffbuf) {
238 ((linediff_fn)priv)(mb[i].ptr, mb[i].size); 239 ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
239 continue; 240 continue;
240 } 241 }
241 diffbuf = xrealloc(diffbuf, buflen + mb[i].size); 242 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
242 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); 243 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
243 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size); 244 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
244 free(diffbuf); 245 free(diffbuf);
245 diffbuf = NULL; 246 diffbuf = NULL;
246 buflen = 0; 247 buflen = 0;
247 } 248 }
248 if (diffbuf) { 249 if (diffbuf) {
249 ((linediff_fn)priv)(diffbuf, buflen); 250 ((linediff_fn)priv)(diffbuf, buflen);
250 free(diffbuf); 251 free(diffbuf);
251 diffbuf = NULL; 252 diffbuf = NULL;
252 buflen = 0; 253 buflen = 0;
253 } 254 }
254 return 0; 255 return 0;
255} 256}
256 257
257int cgit_diff_files(const unsigned char *old_sha1, 258int cgit_diff_files(const unsigned char *old_sha1,
258 const unsigned char *new_sha1, 259 const unsigned char *new_sha1,
259 linediff_fn fn) 260 linediff_fn fn)
260{ 261{
261 mmfile_t file1, file2; 262 mmfile_t file1, file2;
262 xpparam_t diff_params; 263 xpparam_t diff_params;
263 xdemitconf_t emit_params; 264 xdemitconf_t emit_params;
264 xdemitcb_t emit_cb; 265 xdemitcb_t emit_cb;
265 266
266 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1)) 267 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))
267 return 1; 268 return 1;
268 269
269 diff_params.flags = XDF_NEED_MINIMAL; 270 diff_params.flags = XDF_NEED_MINIMAL;
270 emit_params.ctxlen = 3; 271 emit_params.ctxlen = 3;
271 emit_params.flags = XDL_EMIT_FUNCNAMES; 272 emit_params.flags = XDL_EMIT_FUNCNAMES;
272 emit_params.find_func = NULL; 273 emit_params.find_func = NULL;
273 emit_cb.outf = filediff_cb; 274 emit_cb.outf = filediff_cb;
274 emit_cb.priv = fn; 275 emit_cb.priv = fn;
275 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb); 276 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
276 return 0; 277 return 0;
277} 278}
278 279
279void cgit_diff_tree(const unsigned char *old_sha1, 280void cgit_diff_tree(const unsigned char *old_sha1,
280 const unsigned char *new_sha1, 281 const unsigned char *new_sha1,
281 filepair_fn fn, const char *prefix) 282 filepair_fn fn, const char *prefix)
282{ 283{
283 struct diff_options opt; 284 struct diff_options opt;
284 int ret; 285 int ret;
285 int prefixlen; 286 int prefixlen;
286 287
287 diff_setup(&opt); 288 diff_setup(&opt);
288 opt.output_format = DIFF_FORMAT_CALLBACK; 289 opt.output_format = DIFF_FORMAT_CALLBACK;
289 opt.detect_rename = 1; 290 opt.detect_rename = 1;
290 opt.rename_limit = ctx.cfg.renamelimit; 291 opt.rename_limit = ctx.cfg.renamelimit;
291 DIFF_OPT_SET(&opt, RECURSIVE); 292 DIFF_OPT_SET(&opt, RECURSIVE);
292 opt.format_callback = cgit_diff_tree_cb; 293 opt.format_callback = cgit_diff_tree_cb;
293 opt.format_callback_data = fn; 294 opt.format_callback_data = fn;
294 if (prefix) { 295 if (prefix) {
295 opt.nr_paths = 1; 296 opt.nr_paths = 1;
296 opt.paths = &prefix; 297 opt.paths = &prefix;
297 prefixlen = strlen(prefix); 298 prefixlen = strlen(prefix);
298 opt.pathlens = &prefixlen; 299 opt.pathlens = &prefixlen;
299 } 300 }
300 diff_setup_done(&opt); 301 diff_setup_done(&opt);
301 302
302 if (old_sha1 && !is_null_sha1(old_sha1)) 303 if (old_sha1 && !is_null_sha1(old_sha1))
303 ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt); 304 ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt);
304 else 305 else
305 ret = diff_root_tree_sha1(new_sha1, "", &opt); 306 ret = diff_root_tree_sha1(new_sha1, "", &opt);
306 diffcore_std(&opt); 307 diffcore_std(&opt);
307 diff_flush(&opt); 308 diff_flush(&opt);
308} 309}
309 310
310void cgit_diff_commit(struct commit *commit, filepair_fn fn) 311void cgit_diff_commit(struct commit *commit, filepair_fn fn)
311{ 312{
312 unsigned char *old_sha1 = NULL; 313 unsigned char *old_sha1 = NULL;
313 314
314 if (commit->parents) 315 if (commit->parents)
315 old_sha1 = commit->parents->item->object.sha1; 316 old_sha1 = commit->parents->item->object.sha1;
316 cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL); 317 cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL);
317} 318}
318 319
319int cgit_parse_snapshots_mask(const char *str) 320int cgit_parse_snapshots_mask(const char *str)
320{ 321{
321 const struct cgit_snapshot_format *f; 322 const struct cgit_snapshot_format *f;
322 static const char *delim = " \t,:/|;"; 323 static const char *delim = " \t,:/|;";
323 int tl, sl, rv = 0; 324 int tl, sl, rv = 0;
324 325
325 /* favor legacy setting */ 326 /* favor legacy setting */
326 if(atoi(str)) 327 if(atoi(str))
327 return 1; 328 return 1;
328 for(;;) { 329 for(;;) {
329 str += strspn(str,delim); 330 str += strspn(str,delim);
330 tl = strcspn(str,delim); 331 tl = strcspn(str,delim);
331 if (!tl) 332 if (!tl)
332 break; 333 break;
333 for (f = cgit_snapshot_formats; f->suffix; f++) { 334 for (f = cgit_snapshot_formats; f->suffix; f++) {
334 sl = strlen(f->suffix); 335 sl = strlen(f->suffix);
335 if((tl == sl && !strncmp(f->suffix, str, tl)) || 336 if((tl == sl && !strncmp(f->suffix, str, tl)) ||
336 (tl == sl-1 && !strncmp(f->suffix+1, str, tl-1))) { 337 (tl == sl-1 && !strncmp(f->suffix+1, str, tl-1))) {
337 rv |= f->bit; 338 rv |= f->bit;
338 break; 339 break;
339 } 340 }
340 } 341 }
341 str += tl; 342 str += tl;
342 } 343 }
343 return rv; 344 return rv;
344} 345}
diff --git a/ui-repolist.c b/ui-repolist.c
index 2324273..87196f0 100644
--- a/ui-repolist.c
+++ b/ui-repolist.c
@@ -1,171 +1,278 @@
1/* ui-repolist.c: functions for generating the repolist page 1/* ui-repolist.c: functions for generating the repolist page
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/* This is needed for strcasestr to be defined by <string.h> */ 9/* This is needed for strcasestr to be defined by <string.h> */
10#define _GNU_SOURCE 1 10#define _GNU_SOURCE 1
11#include <string.h> 11#include <string.h>
12 12
13#include <time.h> 13#include <time.h>
14 14
15#include "cgit.h" 15#include "cgit.h"
16#include "html.h" 16#include "html.h"
17#include "ui-shared.h" 17#include "ui-shared.h"
18 18
19time_t read_agefile(char *path) 19time_t read_agefile(char *path)
20{ 20{
21 FILE *f; 21 FILE *f;
22 static char buf[64], buf2[64]; 22 static char buf[64], buf2[64];
23 23
24 if (!(f = fopen(path, "r"))) 24 if (!(f = fopen(path, "r")))
25 return -1; 25 return -1;
26 if (fgets(buf, sizeof(buf), f) == NULL) 26 if (fgets(buf, sizeof(buf), f) == NULL)
27 return -1; 27 return -1;
28 fclose(f); 28 fclose(f);
29 if (parse_date(buf, buf2, sizeof(buf2))) 29 if (parse_date(buf, buf2, sizeof(buf2)))
30 return strtoul(buf2, NULL, 10); 30 return strtoul(buf2, NULL, 10);
31 else 31 else
32 return 0; 32 return 0;
33} 33}
34 34
35static void print_modtime(struct cgit_repo *repo) 35static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
36{ 36{
37 char *path; 37 char *path;
38 struct stat s; 38 struct stat s;
39 struct cgit_repo *r = (struct cgit_repo *)repo;
39 40
41 if (repo->mtime != -1) {
42 *mtime = repo->mtime;
43 return 1;
44 }
40 path = fmt("%s/%s", repo->path, ctx.cfg.agefile); 45 path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
41 if (stat(path, &s) == 0) { 46 if (stat(path, &s) == 0) {
42 cgit_print_age(read_agefile(path), -1, NULL); 47 *mtime = read_agefile(path);
43 return; 48 r->mtime = *mtime;
49 return 1;
44 } 50 }
45 51
46 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch); 52 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch);
47 if (stat(path, &s) != 0) 53 if (stat(path, &s) == 0)
48 return; 54 *mtime = s.st_mtime;
49 cgit_print_age(s.st_mtime, -1, NULL); 55 else
56 *mtime = 0;
57
58 r->mtime = *mtime;
59 return (r->mtime != 0);
60}
61
62static void print_modtime(struct cgit_repo *repo)
63{
64 time_t t;
65 if (get_repo_modtime(repo, &t))
66 cgit_print_age(t, -1, NULL);
50} 67}
51 68
52int is_match(struct cgit_repo *repo) 69int is_match(struct cgit_repo *repo)
53{ 70{
54 if (!ctx.qry.search) 71 if (!ctx.qry.search)
55 return 1; 72 return 1;
56 if (repo->url && strcasestr(repo->url, ctx.qry.search)) 73 if (repo->url && strcasestr(repo->url, ctx.qry.search))
57 return 1; 74 return 1;
58 if (repo->name && strcasestr(repo->name, ctx.qry.search)) 75 if (repo->name && strcasestr(repo->name, ctx.qry.search))
59 return 1; 76 return 1;
60 if (repo->desc && strcasestr(repo->desc, ctx.qry.search)) 77 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
61 return 1; 78 return 1;
62 if (repo->owner && strcasestr(repo->owner, ctx.qry.search)) 79 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
63 return 1; 80 return 1;
64 return 0; 81 return 0;
65} 82}
66 83
67int is_in_url(struct cgit_repo *repo) 84int is_in_url(struct cgit_repo *repo)
68{ 85{
69 if (!ctx.qry.url) 86 if (!ctx.qry.url)
70 return 1; 87 return 1;
71 if (repo->url && !prefixcmp(repo->url, ctx.qry.url)) 88 if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
72 return 1; 89 return 1;
73 return 0; 90 return 0;
74} 91}
75 92
93void print_sort_header(const char *title, const char *sort)
94{
95 htmlf("<th class='left'><a href='./?s=%s", sort);
96 if (ctx.qry.search) {
97 html("&q=");
98 html_url_arg(ctx.qry.search);
99 }
100 htmlf("'>%s</a></th>", title);
101}
102
76void print_header(int columns) 103void print_header(int columns)
77{ 104{
78 html("<tr class='nohover'>" 105 html("<tr class='nohover'>");
79 "<th class='left'>Name</th>" 106 print_sort_header("Name", "name");
80 "<th class='left'>Description</th>" 107 print_sort_header("Description", "desc");
81 "<th class='left'>Owner</th>" 108 print_sort_header("Owner", "owner");
82 "<th class='left'>Idle</th>"); 109 print_sort_header("Idle", "idle");
83 if (ctx.cfg.enable_index_links) 110 if (ctx.cfg.enable_index_links)
84 html("<th class='left'>Links</th>"); 111 html("<th class='left'>Links</th>");
85 html("</tr>\n"); 112 html("</tr>\n");
86} 113}
87 114
88 115
89void print_pager(int items, int pagelen, char *search) 116void print_pager(int items, int pagelen, char *search)
90{ 117{
91 int i; 118 int i;
92 html("<div class='pager'>"); 119 html("<div class='pager'>");
93 for(i = 0; i * pagelen < items; i++) 120 for(i = 0; i * pagelen < items; i++)
94 cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL, 121 cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL,
95 search, i * pagelen); 122 search, i * pagelen);
96 html("</div>"); 123 html("</div>");
97} 124}
98 125
126static int cmp(const char *s1, const char *s2)
127{
128 if (s1 && s2)
129 return strcmp(s1, s2);
130 if (s1 && !s2)
131 return -1;
132 if (s2 && !s1)
133 return 1;
134 return 0;
135}
136
137static int sort_name(const void *a, const void *b)
138{
139 const struct cgit_repo *r1 = a;
140 const struct cgit_repo *r2 = b;
141
142 return cmp(r1->name, r2->name);
143}
144
145static int sort_desc(const void *a, const void *b)
146{
147 const struct cgit_repo *r1 = a;
148 const struct cgit_repo *r2 = b;
149
150 return cmp(r1->desc, r2->desc);
151}
152
153static int sort_owner(const void *a, const void *b)
154{
155 const struct cgit_repo *r1 = a;
156 const struct cgit_repo *r2 = b;
157
158 return cmp(r1->owner, r2->owner);
159}
160
161static int sort_idle(const void *a, const void *b)
162{
163 const struct cgit_repo *r1 = a;
164 const struct cgit_repo *r2 = b;
165 time_t t1, t2;
166
167 t1 = t2 = 0;
168 get_repo_modtime(r1, &t1);
169 get_repo_modtime(r2, &t2);
170 return t2 - t1;
171}
172
173struct sortcolumn {
174 const char *name;
175 int (*fn)(const void *a, const void *b);
176};
177
178struct sortcolumn sortcolumn[] = {
179 {"name", sort_name},
180 {"desc", sort_desc},
181 {"owner", sort_owner},
182 {"idle", sort_idle},
183 {NULL, NULL}
184};
185
186int sort_repolist(char *field)
187{
188 struct sortcolumn *column;
189
190 for (column = &sortcolumn[0]; column->name; column++) {
191 if (strcmp(field, column->name))
192 continue;
193 qsort(cgit_repolist.repos, cgit_repolist.count,
194 sizeof(struct cgit_repo), column->fn);
195 return 1;
196 }
197 return 0;
198}
199
200
99void cgit_print_repolist() 201void cgit_print_repolist()
100{ 202{
101 int i, columns = 4, hits = 0, header = 0; 203 int i, columns = 4, hits = 0, header = 0;
102 char *last_group = NULL; 204 char *last_group = NULL;
205 int sorted = 0;
103 206
104 if (ctx.cfg.enable_index_links) 207 if (ctx.cfg.enable_index_links)
105 columns++; 208 columns++;
106 209
107 ctx.page.title = ctx.cfg.root_title; 210 ctx.page.title = ctx.cfg.root_title;
108 cgit_print_http_headers(&ctx); 211 cgit_print_http_headers(&ctx);
109 cgit_print_docstart(&ctx); 212 cgit_print_docstart(&ctx);
110 cgit_print_pageheader(&ctx); 213 cgit_print_pageheader(&ctx);
111 214
112 if (ctx.cfg.index_header) 215 if (ctx.cfg.index_header)
113 html_include(ctx.cfg.index_header); 216 html_include(ctx.cfg.index_header);
114 217
218 if(ctx.qry.sort)
219 sorted = sort_repolist(ctx.qry.sort);
220
115 html("<table summary='repository list' class='list nowrap'>"); 221 html("<table summary='repository list' class='list nowrap'>");
116 for (i=0; i<cgit_repolist.count; i++) { 222 for (i=0; i<cgit_repolist.count; i++) {
117 ctx.repo = &cgit_repolist.repos[i]; 223 ctx.repo = &cgit_repolist.repos[i];
118 if (!(is_match(ctx.repo) && is_in_url(ctx.repo))) 224 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
119 continue; 225 continue;
120 hits++; 226 hits++;
121 if (hits <= ctx.qry.ofs) 227 if (hits <= ctx.qry.ofs)
122 continue; 228 continue;
123 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count) 229 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
124 continue; 230 continue;
125 if (!header++) 231 if (!header++)
126 print_header(columns); 232 print_header(columns);
127 if ((last_group == NULL && ctx.repo->group != NULL) || 233 if (!sorted &&
234 ((last_group == NULL && ctx.repo->group != NULL) ||
128 (last_group != NULL && ctx.repo->group == NULL) || 235 (last_group != NULL && ctx.repo->group == NULL) ||
129 (last_group != NULL && ctx.repo->group != NULL && 236 (last_group != NULL && ctx.repo->group != NULL &&
130 strcmp(ctx.repo->group, last_group))) { 237 strcmp(ctx.repo->group, last_group)))) {
131 htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>", 238 htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>",
132 columns); 239 columns);
133 html_txt(ctx.repo->group); 240 html_txt(ctx.repo->group);
134 html("</td></tr>"); 241 html("</td></tr>");
135 last_group = ctx.repo->group; 242 last_group = ctx.repo->group;
136 } 243 }
137 htmlf("<tr><td class='%s'>", 244 htmlf("<tr><td class='%s'>",
138 ctx.repo->group ? "sublevel-repo" : "toplevel-repo"); 245 !sorted && ctx.repo->group ? "sublevel-repo" : "toplevel-repo");
139 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL); 246 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
140 html("</td><td>"); 247 html("</td><td>");
141 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL); 248 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
142 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc); 249 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
143 html_link_close(); 250 html_link_close();
144 html("</td><td>"); 251 html("</td><td>");
145 html_txt(ctx.repo->owner); 252 html_txt(ctx.repo->owner);
146 html("</td><td>"); 253 html("</td><td>");
147 print_modtime(ctx.repo); 254 print_modtime(ctx.repo);
148 html("</td>"); 255 html("</td>");
149 if (ctx.cfg.enable_index_links) { 256 if (ctx.cfg.enable_index_links) {
150 html("<td>"); 257 html("<td>");
151 cgit_summary_link("summary", NULL, "button", NULL); 258 cgit_summary_link("summary", NULL, "button", NULL);
152 cgit_log_link("log", NULL, "button", NULL, NULL, NULL, 259 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
153 0, NULL, NULL); 260 0, NULL, NULL);
154 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL); 261 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
155 html("</td>"); 262 html("</td>");
156 } 263 }
157 html("</tr>\n"); 264 html("</tr>\n");
158 } 265 }
159 html("</table>"); 266 html("</table>");
160 if (!hits) 267 if (!hits)
161 cgit_print_error("No repositories found"); 268 cgit_print_error("No repositories found");
162 else if (hits > ctx.cfg.max_repo_count) 269 else if (hits > ctx.cfg.max_repo_count)
163 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search); 270 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search);
164 cgit_print_docend(); 271 cgit_print_docend();
165} 272}
166 273
167void cgit_print_site_readme() 274void cgit_print_site_readme()
168{ 275{
169 if (ctx.cfg.root_readme) 276 if (ctx.cfg.root_readme)
170 html_include(ctx.cfg.root_readme); 277 html_include(ctx.cfg.root_readme);
171} 278}