summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--cache.c22
-rw-r--r--cgit.c74
-rw-r--r--cgit.h15
-rw-r--r--parsing.c44
-rw-r--r--shared.c30
-rw-r--r--ui-shared.c5
6 files changed, 139 insertions, 51 deletions
diff --git a/cache.c b/cache.c
index 8df7c26..372e38d 100644
--- a/cache.c
+++ b/cache.c
@@ -1,78 +1,86 @@
1/* cache.c: cache management 1/* cache.c: cache management
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
11const int NOLOCK = -1; 11const int NOLOCK = -1;
12 12
13char *cache_safe_filename(const char *unsafe) 13char *cache_safe_filename(const char *unsafe)
14{ 14{
15 static char buf[PATH_MAX]; 15 static char buf[4][PATH_MAX];
16 char *s = buf; 16 static int bufidx;
17 char *s;
17 char c; 18 char c;
18 19
20 bufidx++;
21 bufidx &= 3;
22 s = buf[bufidx];
23
19 while(unsafe && (c = *unsafe++) != 0) { 24 while(unsafe && (c = *unsafe++) != 0) {
20 if (c == '/' || c == ' ' || c == '&' || c == '|' || 25 if (c == '/' || c == ' ' || c == '&' || c == '|' ||
21 c == '>' || c == '<' || c == '.') 26 c == '>' || c == '<' || c == '.')
22 c = '_'; 27 c = '_';
23 *s++ = c; 28 *s++ = c;
24 } 29 }
25 *s = '\0'; 30 *s = '\0';
26 return buf; 31 return buf[bufidx];
27} 32}
28 33
29int cache_exist(struct cacheitem *item) 34int cache_exist(struct cacheitem *item)
30{ 35{
31 if (stat(item->name, &item->st)) { 36 if (stat(item->name, &item->st)) {
32 item->st.st_mtime = 0; 37 item->st.st_mtime = 0;
33 return 0; 38 return 0;
34 } 39 }
35 return 1; 40 return 1;
36} 41}
37 42
38int cache_create_dirs() 43int cache_create_dirs()
39{ 44{
40 char *path; 45 char *path;
41 46
42 path = fmt("%s", cgit_cache_root); 47 path = fmt("%s", cgit_cache_root);
43 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 48 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
44 return 0; 49 return 0;
45 50
46 if (!cgit_query_repo) 51 if (!cgit_repo)
47 return 0; 52 return 0;
48 53
49 path = fmt("%s/%s", cgit_cache_root, cgit_query_repo); 54 path = fmt("%s/%s", cgit_cache_root,
55 cache_safe_filename(cgit_repo->url));
56
50 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 57 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
51 return 0; 58 return 0;
52 59
53 if (cgit_query_page) { 60 if (cgit_query_page) {
54 path = fmt("%s/%s/%s", cgit_cache_root, cgit_query_repo, 61 path = fmt("%s/%s/%s", cgit_cache_root,
62 cache_safe_filename(cgit_repo->url),
55 cgit_query_page); 63 cgit_query_page);
56 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 64 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
57 return 0; 65 return 0;
58 } 66 }
59 return 1; 67 return 1;
60} 68}
61 69
62int cache_refill_overdue(const char *lockfile) 70int cache_refill_overdue(const char *lockfile)
63{ 71{
64 struct stat st; 72 struct stat st;
65 73
66 if (stat(lockfile, &st)) 74 if (stat(lockfile, &st))
67 return 0; 75 return 0;
68 else 76 else
69 return (time(NULL) - st.st_mtime > cgit_cache_max_create_time); 77 return (time(NULL) - st.st_mtime > cgit_cache_max_create_time);
70} 78}
71 79
72int cache_lock(struct cacheitem *item) 80int cache_lock(struct cacheitem *item)
73{ 81{
74 int i = 0; 82 int i = 0;
75 char *lockfile = xstrdup(fmt("%s.lock", item->name)); 83 char *lockfile = xstrdup(fmt("%s.lock", item->name));
76 84
77 top: 85 top:
78 if (++i > cgit_max_lock_attempts) 86 if (++i > cgit_max_lock_attempts)
diff --git a/cgit.c b/cgit.c
index 3e7e595..e5d8fbd 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1,170 +1,157 @@
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
11const char cgit_version[] = CGIT_VERSION; 11const char cgit_version[] = CGIT_VERSION;
12 12
13 13
14static struct repoinfo *cgit_get_repoinfo(char *url)
15{
16 int i;
17 struct repoinfo *repo;
18
19 for (i=0; i<cgit_repolist.count; i++) {
20 repo = &cgit_repolist.repos[i];
21 if (!strcmp(repo->url, url))
22 return repo;
23 }
24 return NULL;
25}
26
27
28static int cgit_prepare_cache(struct cacheitem *item) 14static int cgit_prepare_cache(struct cacheitem *item)
29{ 15{
30 if (!cgit_query_repo) { 16 if (!cgit_repo && cgit_query_repo) {
31 item->name = xstrdup(fmt("%s/index.html", cgit_cache_root));
32 item->ttl = cgit_cache_root_ttl;
33 return 1;
34 }
35 cgit_repo = cgit_get_repoinfo(cgit_query_repo);
36 if (!cgit_repo) {
37 char *title = fmt("%s - %s", cgit_root_title, "Bad request"); 17 char *title = fmt("%s - %s", cgit_root_title, "Bad request");
38 cgit_print_docstart(title, item); 18 cgit_print_docstart(title, item);
39 cgit_print_pageheader(title, 0); 19 cgit_print_pageheader(title, 0);
40 cgit_print_error(fmt("Unknown repo: %s", cgit_query_repo)); 20 cgit_print_error(fmt("Unknown repo: %s", cgit_query_repo));
41 cgit_print_docend(); 21 cgit_print_docend();
42 return 0; 22 return 0;
43 } 23 }
44 24
45 if (!cgit_query_page) { 25 if (!cgit_repo) {
26 item->name = xstrdup(fmt("%s/index.html", cgit_cache_root));
27 item->ttl = cgit_cache_root_ttl;
28 return 1;
29 }
30
31 if (!cgit_cmd) {
46 item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root, 32 item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root,
47 cgit_repo->url)); 33 cache_safe_filename(cgit_repo->url)));
48 item->ttl = cgit_cache_repo_ttl; 34 item->ttl = cgit_cache_repo_ttl;
49 } else { 35 } else {
50 item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, 36 item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root,
51 cgit_repo->url, cgit_query_page, 37 cache_safe_filename(cgit_repo->url), cgit_query_page,
52 cache_safe_filename(cgit_querystring))); 38 cache_safe_filename(cgit_querystring)));
53 if (cgit_query_has_symref) 39 if (cgit_query_has_symref)
54 item->ttl = cgit_cache_dynamic_ttl; 40 item->ttl = cgit_cache_dynamic_ttl;
55 else if (cgit_query_has_sha1) 41 else if (cgit_query_has_sha1)
56 item->ttl = cgit_cache_static_ttl; 42 item->ttl = cgit_cache_static_ttl;
57 else 43 else
58 item->ttl = cgit_cache_repo_ttl; 44 item->ttl = cgit_cache_repo_ttl;
59 } 45 }
60 return 1; 46 return 1;
61} 47}
62 48
63static void cgit_print_repo_page(struct cacheitem *item) 49static void cgit_print_repo_page(struct cacheitem *item)
64{ 50{
65 char *title; 51 char *title;
66 int show_search; 52 int show_search;
67 53
68 if (!cgit_query_head) 54 if (!cgit_query_head)
69 cgit_query_head = cgit_repo->defbranch; 55 cgit_query_head = cgit_repo->defbranch;
70 56
71 if (chdir(cgit_repo->path)) { 57 if (chdir(cgit_repo->path)) {
72 title = fmt("%s - %s", cgit_root_title, "Bad request"); 58 title = fmt("%s - %s", cgit_root_title, "Bad request");
73 cgit_print_docstart(title, item); 59 cgit_print_docstart(title, item);
74 cgit_print_pageheader(title, 0); 60 cgit_print_pageheader(title, 0);
75 cgit_print_error(fmt("Unable to scan repository: %s", 61 cgit_print_error(fmt("Unable to scan repository: %s",
76 strerror(errno))); 62 strerror(errno)));
77 cgit_print_docend(); 63 cgit_print_docend();
78 return; 64 return;
79 } 65 }
80 66
81 title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); 67 title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc);
82 show_search = 0; 68 show_search = 0;
83 setenv("GIT_DIR", cgit_repo->path, 1); 69 setenv("GIT_DIR", cgit_repo->path, 1);
84 70
85 if (cgit_query_page) { 71 if ((cgit_cmd == CMD_SNAPSHOT) && cgit_repo->snapshots) {
86 if (cgit_repo->snapshots && !strcmp(cgit_query_page, "snapshot")) {
87 cgit_print_snapshot(item, cgit_query_sha1, "zip", 72 cgit_print_snapshot(item, cgit_query_sha1, "zip",
88 cgit_repo->url, cgit_query_name); 73 cgit_repo->url, cgit_query_name);
89 return; 74 return;
90 }
91 if (!strcmp(cgit_query_page, "blob")) {
92 cgit_print_blob(item, cgit_query_sha1, cgit_query_path);
93 return;
94 }
95 } 75 }
96 76
97 if (cgit_query_page && !strcmp(cgit_query_page, "log")) 77 if (cgit_cmd == CMD_BLOB) {
98 show_search = 1; 78 cgit_print_blob(item, cgit_query_sha1, cgit_query_path);
79 return;
80 }
99 81
82 show_search = (cgit_cmd == CMD_LOG);
100 cgit_print_docstart(title, item); 83 cgit_print_docstart(title, item);
101 84 if (!cgit_cmd) {
102
103 if (!cgit_query_page) {
104 cgit_print_pageheader("summary", show_search); 85 cgit_print_pageheader("summary", show_search);
105 cgit_print_summary(); 86 cgit_print_summary();
106 cgit_print_docend(); 87 cgit_print_docend();
107 return; 88 return;
108 } 89 }
109 90
110 cgit_print_pageheader(cgit_query_page, show_search); 91 cgit_print_pageheader(cgit_query_page, show_search);
111 92
112 if (!strcmp(cgit_query_page, "log")) { 93 switch(cgit_cmd) {
94 case CMD_LOG:
113 cgit_print_log(cgit_query_head, cgit_query_ofs, 95 cgit_print_log(cgit_query_head, cgit_query_ofs,
114 cgit_max_commit_count, cgit_query_search, 96 cgit_max_commit_count, cgit_query_search,
115 cgit_query_path); 97 cgit_query_path);
116 } else if (!strcmp(cgit_query_page, "tree")) { 98 break;
99 case CMD_TREE:
117 cgit_print_tree(cgit_query_head, cgit_query_sha1, cgit_query_path); 100 cgit_print_tree(cgit_query_head, cgit_query_sha1, cgit_query_path);
118 } else if (!strcmp(cgit_query_page, "commit")) { 101 break;
102 case CMD_COMMIT:
119 cgit_print_commit(cgit_query_head); 103 cgit_print_commit(cgit_query_head);
120 } else if (!strcmp(cgit_query_page, "view")) { 104 break;
105 case CMD_VIEW:
121 cgit_print_view(cgit_query_sha1, cgit_query_path); 106 cgit_print_view(cgit_query_sha1, cgit_query_path);
122 } else if (!strcmp(cgit_query_page, "diff")) { 107 break;
108 case CMD_DIFF:
123 cgit_print_diff(cgit_query_head, cgit_query_sha1, cgit_query_sha2, 109 cgit_print_diff(cgit_query_head, cgit_query_sha1, cgit_query_sha2,
124 cgit_query_path); 110 cgit_query_path);
125 } else { 111 break;
112 default:
126 cgit_print_error("Invalid request"); 113 cgit_print_error("Invalid request");
127 } 114 }
128 cgit_print_docend(); 115 cgit_print_docend();
129} 116}
130 117
131static void cgit_fill_cache(struct cacheitem *item, int use_cache) 118static void cgit_fill_cache(struct cacheitem *item, int use_cache)
132{ 119{
133 static char buf[PATH_MAX]; 120 static char buf[PATH_MAX];
134 int stdout2; 121 int stdout2;
135 122
136 getcwd(buf, sizeof(buf)); 123 getcwd(buf, sizeof(buf));
137 item->st.st_mtime = time(NULL); 124 item->st.st_mtime = time(NULL);
138 125
139 if (use_cache) { 126 if (use_cache) {
140 stdout2 = chk_positive(dup(STDOUT_FILENO), 127 stdout2 = chk_positive(dup(STDOUT_FILENO),
141 "Preserving STDOUT"); 128 "Preserving STDOUT");
142 chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); 129 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
143 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); 130 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
144 } 131 }
145 132
146 if (cgit_query_repo) 133 if (cgit_repo)
147 cgit_print_repo_page(item); 134 cgit_print_repo_page(item);
148 else 135 else
149 cgit_print_repolist(item); 136 cgit_print_repolist(item);
150 137
151 if (use_cache) { 138 if (use_cache) {
152 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); 139 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
153 chk_positive(dup2(stdout2, STDOUT_FILENO), 140 chk_positive(dup2(stdout2, STDOUT_FILENO),
154 "Restoring original STDOUT"); 141 "Restoring original STDOUT");
155 chk_zero(close(stdout2), "Closing temporary STDOUT"); 142 chk_zero(close(stdout2), "Closing temporary STDOUT");
156 } 143 }
157 144
158 chdir(buf); 145 chdir(buf);
159} 146}
160 147
161static void cgit_check_cache(struct cacheitem *item) 148static void cgit_check_cache(struct cacheitem *item)
162{ 149{
163 int i = 0; 150 int i = 0;
164 151
165 top: 152 top:
166 if (++i > cgit_max_lock_attempts) { 153 if (++i > cgit_max_lock_attempts) {
167 die("cgit_refresh_cache: unable to lock %s: %s", 154 die("cgit_refresh_cache: unable to lock %s: %s",
168 item->name, strerror(errno)); 155 item->name, strerror(errno));
169 } 156 }
170 if (!cache_exist(item)) { 157 if (!cache_exist(item)) {
@@ -227,40 +214,41 @@ static void cgit_parse_args(int argc, const char **argv)
227 cgit_query_head = xstrdup(argv[i]+7); 214 cgit_query_head = xstrdup(argv[i]+7);
228 cgit_query_has_symref = 1; 215 cgit_query_has_symref = 1;
229 } 216 }
230 if (!strncmp(argv[i], "--sha1=", 7)) { 217 if (!strncmp(argv[i], "--sha1=", 7)) {
231 cgit_query_sha1 = xstrdup(argv[i]+7); 218 cgit_query_sha1 = xstrdup(argv[i]+7);
232 cgit_query_has_sha1 = 1; 219 cgit_query_has_sha1 = 1;
233 } 220 }
234 if (!strncmp(argv[i], "--ofs=", 6)) { 221 if (!strncmp(argv[i], "--ofs=", 6)) {
235 cgit_query_ofs = atoi(argv[i]+6); 222 cgit_query_ofs = atoi(argv[i]+6);
236 } 223 }
237 } 224 }
238} 225}
239 226
240int main(int argc, const char **argv) 227int main(int argc, const char **argv)
241{ 228{
242 struct cacheitem item; 229 struct cacheitem item;
243 230
244 htmlfd = STDOUT_FILENO; 231 htmlfd = STDOUT_FILENO;
245 item.st.st_mtime = time(NULL); 232 item.st.st_mtime = time(NULL);
246 cgit_repolist.length = 0; 233 cgit_repolist.length = 0;
247 cgit_repolist.count = 0; 234 cgit_repolist.count = 0;
248 cgit_repolist.repos = NULL; 235 cgit_repolist.repos = NULL;
249 236
250 cgit_read_config(CGIT_CONFIG, cgit_global_config_cb); 237 cgit_read_config(CGIT_CONFIG, cgit_global_config_cb);
238 cgit_repo = NULL;
251 if (getenv("SCRIPT_NAME")) 239 if (getenv("SCRIPT_NAME"))
252 cgit_script_name = xstrdup(getenv("SCRIPT_NAME")); 240 cgit_script_name = xstrdup(getenv("SCRIPT_NAME"));
253 if (getenv("QUERY_STRING")) 241 if (getenv("QUERY_STRING"))
254 cgit_querystring = xstrdup(getenv("QUERY_STRING")); 242 cgit_querystring = xstrdup(getenv("QUERY_STRING"));
255 cgit_parse_args(argc, argv); 243 cgit_parse_args(argc, argv);
256 cgit_parse_query(cgit_querystring, cgit_querystring_cb); 244 cgit_parse_query(cgit_querystring, cgit_querystring_cb);
257 if (!cgit_prepare_cache(&item)) 245 if (!cgit_prepare_cache(&item))
258 return 0; 246 return 0;
259 if (cgit_nocache) { 247 if (cgit_nocache) {
260 cgit_fill_cache(&item, 0); 248 cgit_fill_cache(&item, 0);
261 } else { 249 } else {
262 cgit_check_cache(&item); 250 cgit_check_cache(&item);
263 cgit_print_cache(&item); 251 cgit_print_cache(&item);
264 } 252 }
265 return 0; 253 return 0;
266} 254}
diff --git a/cgit.h b/cgit.h
index 3ee11bb..e0879bd 100644
--- a/cgit.h
+++ b/cgit.h
@@ -1,44 +1,55 @@
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 19
20 20
21/*
22 * The valid cgit repo-commands
23 */
24#define CMD_LOG 1
25#define CMD_COMMIT 2
26#define CMD_DIFF 3
27#define CMD_TREE 4
28#define CMD_VIEW 5
29#define CMD_BLOB 6
30#define CMD_SNAPSHOT 7
31
21typedef void (*configfn)(const char *name, const char *value); 32typedef void (*configfn)(const char *name, const char *value);
22typedef void (*filepair_fn)(struct diff_filepair *pair); 33typedef void (*filepair_fn)(struct diff_filepair *pair);
23typedef void (*linediff_fn)(char *line, int len); 34typedef void (*linediff_fn)(char *line, int len);
24 35
25struct cacheitem { 36struct cacheitem {
26 char *name; 37 char *name;
27 struct stat st; 38 struct stat st;
28 int ttl; 39 int ttl;
29 int fd; 40 int fd;
30}; 41};
31 42
32struct repoinfo { 43struct repoinfo {
33 char *url; 44 char *url;
34 char *name; 45 char *name;
35 char *path; 46 char *path;
36 char *desc; 47 char *desc;
37 char *owner; 48 char *owner;
38 char *defbranch; 49 char *defbranch;
39 char *module_link; 50 char *module_link;
40 int snapshots; 51 int snapshots;
41 int enable_log_filecount; 52 int enable_log_filecount;
42 int enable_log_linecount; 53 int enable_log_linecount;
43}; 54};
44 55
@@ -50,128 +61,132 @@ struct repolist {
50 61
51struct commitinfo { 62struct commitinfo {
52 struct commit *commit; 63 struct commit *commit;
53 char *author; 64 char *author;
54 char *author_email; 65 char *author_email;
55 unsigned long author_date; 66 unsigned long author_date;
56 char *committer; 67 char *committer;
57 char *committer_email; 68 char *committer_email;
58 unsigned long committer_date; 69 unsigned long committer_date;
59 char *subject; 70 char *subject;
60 char *msg; 71 char *msg;
61}; 72};
62 73
63struct taginfo { 74struct taginfo {
64 char *tagger; 75 char *tagger;
65 char *tagger_email; 76 char *tagger_email;
66 int tagger_date; 77 int tagger_date;
67 char *msg; 78 char *msg;
68}; 79};
69 80
70extern const char cgit_version[]; 81extern const char cgit_version[];
71 82
72extern struct repolist cgit_repolist; 83extern struct repolist cgit_repolist;
73extern struct repoinfo *cgit_repo; 84extern struct repoinfo *cgit_repo;
85extern int cgit_cmd;
74 86
75extern char *cgit_root_title; 87extern char *cgit_root_title;
76extern char *cgit_css; 88extern char *cgit_css;
77extern char *cgit_logo; 89extern char *cgit_logo;
78extern char *cgit_index_header; 90extern char *cgit_index_header;
79extern char *cgit_logo_link; 91extern char *cgit_logo_link;
80extern char *cgit_module_link; 92extern char *cgit_module_link;
81extern char *cgit_virtual_root; 93extern char *cgit_virtual_root;
82extern char *cgit_script_name; 94extern char *cgit_script_name;
83extern char *cgit_cache_root; 95extern char *cgit_cache_root;
84 96
85extern int cgit_nocache; 97extern int cgit_nocache;
86extern int cgit_snapshots; 98extern int cgit_snapshots;
87extern int cgit_enable_log_filecount; 99extern int cgit_enable_log_filecount;
88extern int cgit_enable_log_linecount; 100extern int cgit_enable_log_linecount;
89extern int cgit_max_lock_attempts; 101extern int cgit_max_lock_attempts;
90extern int cgit_cache_root_ttl; 102extern int cgit_cache_root_ttl;
91extern int cgit_cache_repo_ttl; 103extern int cgit_cache_repo_ttl;
92extern int cgit_cache_dynamic_ttl; 104extern int cgit_cache_dynamic_ttl;
93extern int cgit_cache_static_ttl; 105extern int cgit_cache_static_ttl;
94extern int cgit_cache_max_create_time; 106extern int cgit_cache_max_create_time;
95 107
96extern int cgit_max_msg_len; 108extern int cgit_max_msg_len;
97extern int cgit_max_repodesc_len; 109extern int cgit_max_repodesc_len;
98extern int cgit_max_commit_count; 110extern int cgit_max_commit_count;
99 111
100extern int cgit_query_has_symref; 112extern int cgit_query_has_symref;
101extern int cgit_query_has_sha1; 113extern int cgit_query_has_sha1;
102 114
103extern char *cgit_querystring; 115extern char *cgit_querystring;
104extern char *cgit_query_repo; 116extern char *cgit_query_repo;
105extern char *cgit_query_page; 117extern char *cgit_query_page;
106extern char *cgit_query_search; 118extern char *cgit_query_search;
107extern char *cgit_query_head; 119extern char *cgit_query_head;
108extern char *cgit_query_sha1; 120extern char *cgit_query_sha1;
109extern char *cgit_query_sha2; 121extern char *cgit_query_sha2;
110extern char *cgit_query_path; 122extern char *cgit_query_path;
111extern char *cgit_query_name; 123extern char *cgit_query_name;
112extern int cgit_query_ofs; 124extern int cgit_query_ofs;
113 125
114extern int htmlfd; 126extern int htmlfd;
115 127
128extern int cgit_get_cmd_index(const char *cmd);
129extern struct repoinfo *cgit_get_repoinfo(const char *url);
116extern void cgit_global_config_cb(const char *name, const char *value); 130extern void cgit_global_config_cb(const char *name, const char *value);
117extern void cgit_repo_config_cb(const char *name, const char *value); 131extern void cgit_repo_config_cb(const char *name, const char *value);
118extern void cgit_querystring_cb(const char *name, const char *value); 132extern void cgit_querystring_cb(const char *name, const char *value);
119 133
120extern int chk_zero(int result, char *msg); 134extern int chk_zero(int result, char *msg);
121extern int chk_positive(int result, char *msg); 135extern int chk_positive(int result, char *msg);
122 136
123extern int hextoint(char c); 137extern int hextoint(char c);
124 138
125extern void *cgit_free_commitinfo(struct commitinfo *info); 139extern void *cgit_free_commitinfo(struct commitinfo *info);
126 140
127extern int cgit_diff_files(const unsigned char *old_sha1, 141extern int cgit_diff_files(const unsigned char *old_sha1,
128 const unsigned char *new_sha1, 142 const unsigned char *new_sha1,
129 linediff_fn fn); 143 linediff_fn fn);
130 144
131extern void cgit_diff_tree(const unsigned char *old_sha1, 145extern void cgit_diff_tree(const unsigned char *old_sha1,
132 const unsigned char *new_sha1, 146 const unsigned char *new_sha1,
133 filepair_fn fn); 147 filepair_fn fn);
134 148
135extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); 149extern void cgit_diff_commit(struct commit *commit, filepair_fn fn);
136 150
137extern char *fmt(const char *format,...); 151extern char *fmt(const char *format,...);
138 152
139extern void html(const char *txt); 153extern void html(const char *txt);
140extern void htmlf(const char *format,...); 154extern void htmlf(const char *format,...);
141extern void html_txt(char *txt); 155extern void html_txt(char *txt);
142extern void html_ntxt(int len, char *txt); 156extern void html_ntxt(int len, char *txt);
143extern void html_attr(char *txt); 157extern void html_attr(char *txt);
144extern void html_hidden(char *name, char *value); 158extern void html_hidden(char *name, char *value);
145extern void html_link_open(char *url, char *title, char *class); 159extern void html_link_open(char *url, char *title, char *class);
146extern void html_link_close(void); 160extern void html_link_close(void);
147extern void html_filemode(unsigned short mode); 161extern void html_filemode(unsigned short mode);
148extern int html_include(const char *filename); 162extern int html_include(const char *filename);
149 163
150extern int cgit_read_config(const char *filename, configfn fn); 164extern int cgit_read_config(const char *filename, configfn fn);
151extern int cgit_parse_query(char *txt, configfn fn); 165extern int cgit_parse_query(char *txt, configfn fn);
152extern struct commitinfo *cgit_parse_commit(struct commit *commit); 166extern struct commitinfo *cgit_parse_commit(struct commit *commit);
153extern struct taginfo *cgit_parse_tag(struct tag *tag); 167extern struct taginfo *cgit_parse_tag(struct tag *tag);
168extern void cgit_parse_url(const char *url);
154 169
155extern char *cache_safe_filename(const char *unsafe); 170extern char *cache_safe_filename(const char *unsafe);
156extern int cache_lock(struct cacheitem *item); 171extern int cache_lock(struct cacheitem *item);
157extern int cache_unlock(struct cacheitem *item); 172extern int cache_unlock(struct cacheitem *item);
158extern int cache_cancel_lock(struct cacheitem *item); 173extern int cache_cancel_lock(struct cacheitem *item);
159extern int cache_exist(struct cacheitem *item); 174extern int cache_exist(struct cacheitem *item);
160extern int cache_expired(struct cacheitem *item); 175extern int cache_expired(struct cacheitem *item);
161 176
162extern char *cgit_repourl(const char *reponame); 177extern char *cgit_repourl(const char *reponame);
163extern char *cgit_pageurl(const char *reponame, const char *pagename, 178extern char *cgit_pageurl(const char *reponame, const char *pagename,
164 const char *query); 179 const char *query);
165 180
166extern void cgit_print_error(char *msg); 181extern void cgit_print_error(char *msg);
167extern void cgit_print_date(unsigned long secs); 182extern void cgit_print_date(unsigned long secs);
168extern void cgit_print_docstart(char *title, struct cacheitem *item); 183extern void cgit_print_docstart(char *title, struct cacheitem *item);
169extern void cgit_print_docend(); 184extern void cgit_print_docend();
170extern void cgit_print_pageheader(char *title, int show_search); 185extern void cgit_print_pageheader(char *title, int show_search);
171extern void cgit_print_snapshot_start(const char *mimetype, 186extern void cgit_print_snapshot_start(const char *mimetype,
172 const char *filename, 187 const char *filename,
173 struct cacheitem *item); 188 struct cacheitem *item);
174 189
175extern void cgit_print_repolist(struct cacheitem *item); 190extern void cgit_print_repolist(struct cacheitem *item);
176extern void cgit_print_summary(); 191extern void cgit_print_summary();
177extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *path); 192extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *path);
diff --git a/parsing.c b/parsing.c
index 36b0f0c..4420e58 100644
--- a/parsing.c
+++ b/parsing.c
@@ -111,48 +111,92 @@ int cgit_parse_query(char *txt, configfn fn)
111 111
112 t = txt = xstrdup(txt); 112 t = txt = xstrdup(txt);
113 113
114 while((c=*t) != '\0') { 114 while((c=*t) != '\0') {
115 if (c=='=') { 115 if (c=='=') {
116 *t = '\0'; 116 *t = '\0';
117 value = t+1; 117 value = t+1;
118 } else if (c=='+') { 118 } else if (c=='+') {
119 *t = ' '; 119 *t = ' ';
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/*
136 * url syntax: [repo ['/' cmd [ '/' path]]]
137 * repo: any valid repo url, may contain '/'
138 * cmd: log | commit | diff | tree | view | blob | snapshot
139 * path: any valid path, may contain '/'
140 *
141 */
142void cgit_parse_url(const char *url)
143{
144 char *cmd, *p;
145
146 cgit_repo = NULL;
147 if (!url || url[0] == '\0')
148 return;
149
150 cgit_repo = cgit_get_repoinfo(url);
151 if (cgit_repo) {
152 cgit_query_repo = cgit_repo->url;
153 return;
154 }
155
156 cmd = strchr(url, '/');
157 while (!cgit_repo && cmd) {
158 cmd[0] = '\0';
159 cgit_repo = cgit_get_repoinfo(url);
160 if (cgit_repo == NULL) {
161 cmd[0] = '/';
162 cmd = strchr(cmd + 1, '/');
163 continue;
164 }
165
166 cgit_query_repo = cgit_repo->url;
167 p = strchr(cmd + 1, '/');
168 if (p) {
169 p[0] = '\0';
170 if (p[1])
171 cgit_query_path = xstrdup(p + 1);
172 }
173 cgit_cmd = cgit_get_cmd_index(cmd + 1);
174 cgit_query_page = xstrdup(cmd + 1);
175 return;
176 }
177}
178
135char *substr(const char *head, const char *tail) 179char *substr(const char *head, const char *tail)
136{ 180{
137 char *buf; 181 char *buf;
138 182
139 buf = xmalloc(tail - head + 1); 183 buf = xmalloc(tail - head + 1);
140 strncpy(buf, head, tail - head); 184 strncpy(buf, head, tail - head);
141 buf[tail - head] = '\0'; 185 buf[tail - head] = '\0';
142 return buf; 186 return buf;
143} 187}
144 188
145struct commitinfo *cgit_parse_commit(struct commit *commit) 189struct commitinfo *cgit_parse_commit(struct commit *commit)
146{ 190{
147 struct commitinfo *ret; 191 struct commitinfo *ret;
148 char *p = commit->buffer, *t = commit->buffer; 192 char *p = commit->buffer, *t = commit->buffer;
149 193
150 ret = xmalloc(sizeof(*ret)); 194 ret = xmalloc(sizeof(*ret));
151 ret->commit = commit; 195 ret->commit = commit;
152 ret->author = NULL; 196 ret->author = NULL;
153 ret->author_email = NULL; 197 ret->author_email = NULL;
154 ret->committer = NULL; 198 ret->committer = NULL;
155 ret->committer_email = NULL; 199 ret->committer_email = NULL;
156 ret->subject = NULL; 200 ret->subject = NULL;
157 ret->msg = NULL; 201 ret->msg = NULL;
158 202
diff --git a/shared.c b/shared.c
index 54b1813..45fde7f 100644
--- a/shared.c
+++ b/shared.c
@@ -1,120 +1,146 @@
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;
13int cgit_cmd;
13 14
14char *cgit_root_title = "Git repository browser"; 15char *cgit_root_title = "Git repository browser";
15char *cgit_css = "/cgit.css"; 16char *cgit_css = "/cgit.css";
16char *cgit_logo = "/git-logo.png"; 17char *cgit_logo = "/git-logo.png";
17char *cgit_index_header = NULL; 18char *cgit_index_header = NULL;
18char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; 19char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/";
19char *cgit_module_link = "./?repo=%s&page=commit&id=%s"; 20char *cgit_module_link = "./?repo=%s&page=commit&id=%s";
20char *cgit_virtual_root = NULL; 21char *cgit_virtual_root = NULL;
21char *cgit_script_name = CGIT_SCRIPT_NAME; 22char *cgit_script_name = CGIT_SCRIPT_NAME;
22char *cgit_cache_root = "/var/cache/cgit"; 23char *cgit_cache_root = "/var/cache/cgit";
23 24
24int cgit_nocache = 0; 25int cgit_nocache = 0;
25int cgit_snapshots = 0; 26int cgit_snapshots = 0;
26int cgit_enable_log_filecount = 0; 27int cgit_enable_log_filecount = 0;
27int cgit_enable_log_linecount = 0; 28int cgit_enable_log_linecount = 0;
28int cgit_max_lock_attempts = 5; 29int cgit_max_lock_attempts = 5;
29int cgit_cache_root_ttl = 5; 30int cgit_cache_root_ttl = 5;
30int cgit_cache_repo_ttl = 5; 31int cgit_cache_repo_ttl = 5;
31int cgit_cache_dynamic_ttl = 5; 32int cgit_cache_dynamic_ttl = 5;
32int cgit_cache_static_ttl = -1; 33int cgit_cache_static_ttl = -1;
33int cgit_cache_max_create_time = 5; 34int cgit_cache_max_create_time = 5;
34 35
35int cgit_max_msg_len = 60; 36int cgit_max_msg_len = 60;
36int cgit_max_repodesc_len = 60; 37int cgit_max_repodesc_len = 60;
37int cgit_max_commit_count = 50; 38int cgit_max_commit_count = 50;
38 39
39int cgit_query_has_symref = 0; 40int cgit_query_has_symref = 0;
40int cgit_query_has_sha1 = 0; 41int cgit_query_has_sha1 = 0;
41 42
42char *cgit_querystring = NULL; 43char *cgit_querystring = NULL;
43char *cgit_query_repo = NULL; 44char *cgit_query_repo = NULL;
44char *cgit_query_page = NULL; 45char *cgit_query_page = NULL;
45char *cgit_query_head = NULL; 46char *cgit_query_head = NULL;
46char *cgit_query_search = NULL; 47char *cgit_query_search = NULL;
47char *cgit_query_sha1 = NULL; 48char *cgit_query_sha1 = NULL;
48char *cgit_query_sha2 = NULL; 49char *cgit_query_sha2 = NULL;
49char *cgit_query_path = NULL; 50char *cgit_query_path = NULL;
50char *cgit_query_name = NULL; 51char *cgit_query_name = NULL;
51int cgit_query_ofs = 0; 52int cgit_query_ofs = 0;
52 53
53int htmlfd = 0; 54int htmlfd = 0;
54 55
56
57int cgit_get_cmd_index(const char *cmd)
58{
59 static char *cmds[] = {"log", "commit", "diff", "tree", "view", "blob", "snapshot", NULL};
60 int i;
61
62 for(i = 0; cmds[i]; i++)
63 if (!strcmp(cmd, cmds[i]))
64 return i + 1;
65 return 0;
66}
67
55int chk_zero(int result, char *msg) 68int chk_zero(int result, char *msg)
56{ 69{
57 if (result != 0) 70 if (result != 0)
58 die("%s: %s", msg, strerror(errno)); 71 die("%s: %s", msg, strerror(errno));
59 return result; 72 return result;
60} 73}
61 74
62int chk_positive(int result, char *msg) 75int chk_positive(int result, char *msg)
63{ 76{
64 if (result <= 0) 77 if (result <= 0)
65 die("%s: %s", msg, strerror(errno)); 78 die("%s: %s", msg, strerror(errno));
66 return result; 79 return result;
67} 80}
68 81
69struct repoinfo *add_repo(const char *url) 82struct repoinfo *add_repo(const char *url)
70{ 83{
71 struct repoinfo *ret; 84 struct repoinfo *ret;
72 85
73 if (++cgit_repolist.count > cgit_repolist.length) { 86 if (++cgit_repolist.count > cgit_repolist.length) {
74 if (cgit_repolist.length == 0) 87 if (cgit_repolist.length == 0)
75 cgit_repolist.length = 8; 88 cgit_repolist.length = 8;
76 else 89 else
77 cgit_repolist.length *= 2; 90 cgit_repolist.length *= 2;
78 cgit_repolist.repos = xrealloc(cgit_repolist.repos, 91 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
79 cgit_repolist.length * 92 cgit_repolist.length *
80 sizeof(struct repoinfo)); 93 sizeof(struct repoinfo));
81 } 94 }
82 95
83 ret = &cgit_repolist.repos[cgit_repolist.count-1]; 96 ret = &cgit_repolist.repos[cgit_repolist.count-1];
84 ret->url = xstrdup(url); 97 ret->url = xstrdup(url);
85 ret->name = ret->url; 98 ret->name = ret->url;
86 ret->path = NULL; 99 ret->path = NULL;
87 ret->desc = NULL; 100 ret->desc = NULL;
88 ret->owner = NULL; 101 ret->owner = NULL;
89 ret->defbranch = "master"; 102 ret->defbranch = "master";
90 ret->snapshots = cgit_snapshots; 103 ret->snapshots = cgit_snapshots;
91 ret->enable_log_filecount = cgit_enable_log_filecount; 104 ret->enable_log_filecount = cgit_enable_log_filecount;
92 ret->enable_log_linecount = cgit_enable_log_linecount; 105 ret->enable_log_linecount = cgit_enable_log_linecount;
93 ret->module_link = cgit_module_link; 106 ret->module_link = cgit_module_link;
94 return ret; 107 return ret;
95} 108}
96 109
110struct repoinfo *cgit_get_repoinfo(const char *url)
111{
112 int i;
113 struct repoinfo *repo;
114
115 for (i=0; i<cgit_repolist.count; i++) {
116 repo = &cgit_repolist.repos[i];
117 if (!strcmp(repo->url, url))
118 return repo;
119 }
120 return NULL;
121}
122
97void cgit_global_config_cb(const char *name, const char *value) 123void cgit_global_config_cb(const char *name, const char *value)
98{ 124{
99 if (!strcmp(name, "root-title")) 125 if (!strcmp(name, "root-title"))
100 cgit_root_title = xstrdup(value); 126 cgit_root_title = xstrdup(value);
101 else if (!strcmp(name, "css")) 127 else if (!strcmp(name, "css"))
102 cgit_css = xstrdup(value); 128 cgit_css = xstrdup(value);
103 else if (!strcmp(name, "logo")) 129 else if (!strcmp(name, "logo"))
104 cgit_logo = xstrdup(value); 130 cgit_logo = xstrdup(value);
105 else if (!strcmp(name, "index-header")) 131 else if (!strcmp(name, "index-header"))
106 cgit_index_header = xstrdup(value); 132 cgit_index_header = xstrdup(value);
107 else if (!strcmp(name, "logo-link")) 133 else if (!strcmp(name, "logo-link"))
108 cgit_logo_link = xstrdup(value); 134 cgit_logo_link = xstrdup(value);
109 else if (!strcmp(name, "module-link")) 135 else if (!strcmp(name, "module-link"))
110 cgit_module_link = xstrdup(value); 136 cgit_module_link = xstrdup(value);
111 else if (!strcmp(name, "virtual-root")) 137 else if (!strcmp(name, "virtual-root"))
112 cgit_virtual_root = xstrdup(value); 138 cgit_virtual_root = xstrdup(value);
113 else if (!strcmp(name, "nocache")) 139 else if (!strcmp(name, "nocache"))
114 cgit_nocache = atoi(value); 140 cgit_nocache = atoi(value);
115 else if (!strcmp(name, "snapshots")) 141 else if (!strcmp(name, "snapshots"))
116 cgit_snapshots = atoi(value); 142 cgit_snapshots = atoi(value);
117 else if (!strcmp(name, "enable-log-filecount")) 143 else if (!strcmp(name, "enable-log-filecount"))
118 cgit_enable_log_filecount = atoi(value); 144 cgit_enable_log_filecount = atoi(value);
119 else if (!strcmp(name, "enable-log-linecount")) 145 else if (!strcmp(name, "enable-log-linecount"))
120 cgit_enable_log_linecount = atoi(value); 146 cgit_enable_log_linecount = atoi(value);
@@ -141,50 +167,54 @@ void cgit_global_config_cb(const char *name, const char *value)
141 else if (cgit_repo && !strcmp(name, "repo.path")) 167 else if (cgit_repo && !strcmp(name, "repo.path"))
142 cgit_repo->path = xstrdup(value); 168 cgit_repo->path = xstrdup(value);
143 else if (cgit_repo && !strcmp(name, "repo.desc")) 169 else if (cgit_repo && !strcmp(name, "repo.desc"))
144 cgit_repo->desc = xstrdup(value); 170 cgit_repo->desc = xstrdup(value);
145 else if (cgit_repo && !strcmp(name, "repo.owner")) 171 else if (cgit_repo && !strcmp(name, "repo.owner"))
146 cgit_repo->owner = xstrdup(value); 172 cgit_repo->owner = xstrdup(value);
147 else if (cgit_repo && !strcmp(name, "repo.defbranch")) 173 else if (cgit_repo && !strcmp(name, "repo.defbranch"))
148 cgit_repo->defbranch = xstrdup(value); 174 cgit_repo->defbranch = xstrdup(value);
149 else if (cgit_repo && !strcmp(name, "repo.snapshots")) 175 else if (cgit_repo && !strcmp(name, "repo.snapshots"))
150 cgit_repo->snapshots = cgit_snapshots * atoi(value); 176 cgit_repo->snapshots = cgit_snapshots * atoi(value);
151 else if (cgit_repo && !strcmp(name, "repo.enable-log-filecount")) 177 else if (cgit_repo && !strcmp(name, "repo.enable-log-filecount"))
152 cgit_repo->enable_log_filecount = cgit_enable_log_filecount * atoi(value); 178 cgit_repo->enable_log_filecount = cgit_enable_log_filecount * atoi(value);
153 else if (cgit_repo && !strcmp(name, "repo.enable-log-linecount")) 179 else if (cgit_repo && !strcmp(name, "repo.enable-log-linecount"))
154 cgit_repo->enable_log_linecount = cgit_enable_log_linecount * atoi(value); 180 cgit_repo->enable_log_linecount = cgit_enable_log_linecount * atoi(value);
155 else if (cgit_repo && !strcmp(name, "repo.module-link")) 181 else if (cgit_repo && !strcmp(name, "repo.module-link"))
156 cgit_repo->module_link= xstrdup(value); 182 cgit_repo->module_link= xstrdup(value);
157 else if (!strcmp(name, "include")) 183 else if (!strcmp(name, "include"))
158 cgit_read_config(value, cgit_global_config_cb); 184 cgit_read_config(value, cgit_global_config_cb);
159} 185}
160 186
161void cgit_querystring_cb(const char *name, const char *value) 187void cgit_querystring_cb(const char *name, const char *value)
162{ 188{
163 if (!strcmp(name,"r")) { 189 if (!strcmp(name,"r")) {
164 cgit_query_repo = xstrdup(value); 190 cgit_query_repo = xstrdup(value);
191 cgit_repo = cgit_get_repoinfo(value);
165 } else if (!strcmp(name, "p")) { 192 } else if (!strcmp(name, "p")) {
166 cgit_query_page = xstrdup(value); 193 cgit_query_page = xstrdup(value);
194 cgit_cmd = cgit_get_cmd_index(value);
195 } else if (!strcmp(name, "url")) {
196 cgit_parse_url(value);
167 } else if (!strcmp(name, "q")) { 197 } else if (!strcmp(name, "q")) {
168 cgit_query_search = xstrdup(value); 198 cgit_query_search = xstrdup(value);
169 } else if (!strcmp(name, "h")) { 199 } else if (!strcmp(name, "h")) {
170 cgit_query_head = xstrdup(value); 200 cgit_query_head = xstrdup(value);
171 cgit_query_has_symref = 1; 201 cgit_query_has_symref = 1;
172 } else if (!strcmp(name, "id")) { 202 } else if (!strcmp(name, "id")) {
173 cgit_query_sha1 = xstrdup(value); 203 cgit_query_sha1 = xstrdup(value);
174 cgit_query_has_sha1 = 1; 204 cgit_query_has_sha1 = 1;
175 } else if (!strcmp(name, "id2")) { 205 } else if (!strcmp(name, "id2")) {
176 cgit_query_sha2 = xstrdup(value); 206 cgit_query_sha2 = xstrdup(value);
177 cgit_query_has_sha1 = 1; 207 cgit_query_has_sha1 = 1;
178 } else if (!strcmp(name, "ofs")) { 208 } else if (!strcmp(name, "ofs")) {
179 cgit_query_ofs = atoi(value); 209 cgit_query_ofs = atoi(value);
180 } else if (!strcmp(name, "path")) { 210 } else if (!strcmp(name, "path")) {
181 cgit_query_path = xstrdup(value); 211 cgit_query_path = xstrdup(value);
182 } else if (!strcmp(name, "name")) { 212 } else if (!strcmp(name, "name")) {
183 cgit_query_name = xstrdup(value); 213 cgit_query_name = xstrdup(value);
184 } 214 }
185} 215}
186 216
187void *cgit_free_commitinfo(struct commitinfo *info) 217void *cgit_free_commitinfo(struct commitinfo *info)
188{ 218{
189 free(info->author); 219 free(info->author);
190 free(info->author_email); 220 free(info->author_email);
diff --git a/ui-shared.c b/ui-shared.c
index 6211056..c7fbc5e 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -47,49 +47,52 @@ char *cgit_rooturl()
47 else 47 else
48 return cgit_script_name; 48 return cgit_script_name;
49} 49}
50 50
51char *cgit_repourl(const char *reponame) 51char *cgit_repourl(const char *reponame)
52{ 52{
53 if (cgit_virtual_root) { 53 if (cgit_virtual_root) {
54 return fmt("%s/%s/", cgit_virtual_root, reponame); 54 return fmt("%s/%s/", cgit_virtual_root, reponame);
55 } else { 55 } else {
56 return fmt("?r=%s", reponame); 56 return fmt("?r=%s", reponame);
57 } 57 }
58} 58}
59 59
60char *cgit_pageurl(const char *reponame, const char *pagename, 60char *cgit_pageurl(const char *reponame, const char *pagename,
61 const char *query) 61 const char *query)
62{ 62{
63 if (cgit_virtual_root) { 63 if (cgit_virtual_root) {
64 if (query) 64 if (query)
65 return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame, 65 return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame,
66 pagename, query); 66 pagename, query);
67 else 67 else
68 return fmt("%s/%s/%s/", cgit_virtual_root, reponame, 68 return fmt("%s/%s/%s/", cgit_virtual_root, reponame,
69 pagename); 69 pagename);
70 } else { 70 } else {
71 return fmt("?r=%s&p=%s&%s", reponame, pagename, query); 71 if (query)
72 return fmt("?r=%s&p=%s&%s", reponame, pagename, query);
73 else
74 return fmt("?r=%s&p=%s", reponame, pagename);
72 } 75 }
73} 76}
74 77
75char *cgit_currurl() 78char *cgit_currurl()
76{ 79{
77 if (!cgit_virtual_root) 80 if (!cgit_virtual_root)
78 return cgit_script_name; 81 return cgit_script_name;
79 else if (cgit_query_page) 82 else if (cgit_query_page)
80 return fmt("%s/%s/%s/", cgit_virtual_root, cgit_query_repo, cgit_query_page); 83 return fmt("%s/%s/%s/", cgit_virtual_root, cgit_query_repo, cgit_query_page);
81 else if (cgit_query_repo) 84 else if (cgit_query_repo)
82 return fmt("%s/%s/", cgit_virtual_root, cgit_query_repo); 85 return fmt("%s/%s/", cgit_virtual_root, cgit_query_repo);
83 else 86 else
84 return fmt("%s/", cgit_virtual_root); 87 return fmt("%s/", cgit_virtual_root);
85} 88}
86 89
87 90
88void cgit_print_date(unsigned long secs) 91void cgit_print_date(unsigned long secs)
89{ 92{
90 char buf[32]; 93 char buf[32];
91 struct tm *time; 94 struct tm *time;
92 95
93 time = gmtime(&secs); 96 time = gmtime(&secs);
94 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time); 97 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time);
95 html_txt(buf); 98 html_txt(buf);