summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--cache.c9
-rw-r--r--cgit.c60
-rw-r--r--cgit.h16
-rw-r--r--parsing.c43
-rw-r--r--shared.c5
-rw-r--r--ui-shared.c5
6 files changed, 98 insertions, 40 deletions
diff --git a/cache.c b/cache.c
index 8df7c26..7cdea9b 100644
--- a/cache.c
+++ b/cache.c
@@ -1,111 +1,114 @@
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[PATH_MAX];
16 char *s = buf; 16 char *s = buf;
17 char c; 17 char c;
18 18
19 while(unsafe && (c = *unsafe++) != 0) { 19 while(unsafe && (c = *unsafe++) != 0) {
20 if (c == '/' || c == ' ' || c == '&' || c == '|' || 20 if (c == '/' || c == ' ' || c == '&' || c == '|' ||
21 c == '>' || c == '<' || c == '.') 21 c == '>' || c == '<' || c == '.')
22 c = '_'; 22 c = '_';
23 *s++ = c; 23 *s++ = c;
24 } 24 }
25 *s = '\0'; 25 *s = '\0';
26 return buf; 26 return buf;
27} 27}
28 28
29int cache_exist(struct cacheitem *item) 29int cache_exist(struct cacheitem *item)
30{ 30{
31 if (stat(item->name, &item->st)) { 31 if (stat(item->name, &item->st)) {
32 item->st.st_mtime = 0; 32 item->st.st_mtime = 0;
33 return 0; 33 return 0;
34 } 34 }
35 return 1; 35 return 1;
36} 36}
37 37
38int cache_create_dirs() 38int cache_create_dirs()
39{ 39{
40 char *path; 40 char *path;
41 41
42 path = fmt("%s", cgit_cache_root); 42 path = fmt("%s", cgit_cache_root);
43 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 43 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
44 return 0; 44 return 0;
45 45
46 if (!cgit_query_repo) 46 if (!cgit_repo)
47 return 0; 47 return 0;
48 48
49 path = fmt("%s/%s", cgit_cache_root, cgit_query_repo); 49 path = fmt("%s/%s", cgit_cache_root,
50 cache_safe_filename(cgit_repo->url));
51
50 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 52 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
51 return 0; 53 return 0;
52 54
53 if (cgit_query_page) { 55 if (cgit_query_page) {
54 path = fmt("%s/%s/%s", cgit_cache_root, cgit_query_repo, 56 path = fmt("%s/%s/%s", cgit_cache_root,
57 cache_safe_filename(cgit_repo->url),
55 cgit_query_page); 58 cgit_query_page);
56 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 59 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
57 return 0; 60 return 0;
58 } 61 }
59 return 1; 62 return 1;
60} 63}
61 64
62int cache_refill_overdue(const char *lockfile) 65int cache_refill_overdue(const char *lockfile)
63{ 66{
64 struct stat st; 67 struct stat st;
65 68
66 if (stat(lockfile, &st)) 69 if (stat(lockfile, &st))
67 return 0; 70 return 0;
68 else 71 else
69 return (time(NULL) - st.st_mtime > cgit_cache_max_create_time); 72 return (time(NULL) - st.st_mtime > cgit_cache_max_create_time);
70} 73}
71 74
72int cache_lock(struct cacheitem *item) 75int cache_lock(struct cacheitem *item)
73{ 76{
74 int i = 0; 77 int i = 0;
75 char *lockfile = xstrdup(fmt("%s.lock", item->name)); 78 char *lockfile = xstrdup(fmt("%s.lock", item->name));
76 79
77 top: 80 top:
78 if (++i > cgit_max_lock_attempts) 81 if (++i > cgit_max_lock_attempts)
79 die("cache_lock: unable to lock %s: %s", 82 die("cache_lock: unable to lock %s: %s",
80 item->name, strerror(errno)); 83 item->name, strerror(errno));
81 84
82 item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); 85 item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
83 86
84 if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) 87 if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs())
85 goto top; 88 goto top;
86 89
87 if (item->fd == NOLOCK && errno == EEXIST && 90 if (item->fd == NOLOCK && errno == EEXIST &&
88 cache_refill_overdue(lockfile) && !unlink(lockfile)) 91 cache_refill_overdue(lockfile) && !unlink(lockfile))
89 goto top; 92 goto top;
90 93
91 free(lockfile); 94 free(lockfile);
92 return (item->fd > 0); 95 return (item->fd > 0);
93} 96}
94 97
95int cache_unlock(struct cacheitem *item) 98int cache_unlock(struct cacheitem *item)
96{ 99{
97 close(item->fd); 100 close(item->fd);
98 return (rename(fmt("%s.lock", item->name), item->name) == 0); 101 return (rename(fmt("%s.lock", item->name), item->name) == 0);
99} 102}
100 103
101int cache_cancel_lock(struct cacheitem *item) 104int cache_cancel_lock(struct cacheitem *item)
102{ 105{
103 return (unlink(fmt("%s.lock", item->name)) == 0); 106 return (unlink(fmt("%s.lock", item->name)) == 0);
104} 107}
105 108
106int cache_expired(struct cacheitem *item) 109int cache_expired(struct cacheitem *item)
107{ 110{
108 if (item->ttl < 0) 111 if (item->ttl < 0)
109 return 0; 112 return 0;
110 return item->st.st_mtime + item->ttl * 60 < time(NULL); 113 return item->st.st_mtime + item->ttl * 60 < time(NULL);
111} 114}
diff --git a/cgit.c b/cgit.c
index 431e8fb..e5d8fbd 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1,252 +1,254 @@
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 int cgit_prepare_cache(struct cacheitem *item) 14static int cgit_prepare_cache(struct cacheitem *item)
15{ 15{
16 if (!cgit_query_repo) { 16 if (!cgit_repo && cgit_query_repo) {
17 item->name = xstrdup(fmt("%s/index.html", cgit_cache_root));
18 item->ttl = cgit_cache_root_ttl;
19 return 1;
20 }
21 cgit_repo = cgit_get_repoinfo(cgit_query_repo);
22 if (!cgit_repo) {
23 char *title = fmt("%s - %s", cgit_root_title, "Bad request"); 17 char *title = fmt("%s - %s", cgit_root_title, "Bad request");
24 cgit_print_docstart(title, item); 18 cgit_print_docstart(title, item);
25 cgit_print_pageheader(title, 0); 19 cgit_print_pageheader(title, 0);
26 cgit_print_error(fmt("Unknown repo: %s", cgit_query_repo)); 20 cgit_print_error(fmt("Unknown repo: %s", cgit_query_repo));
27 cgit_print_docend(); 21 cgit_print_docend();
28 return 0; 22 return 0;
29 } 23 }
30 24
31 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) {
32 item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root, 32 item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root,
33 cgit_repo->url)); 33 cache_safe_filename(cgit_repo->url)));
34 item->ttl = cgit_cache_repo_ttl; 34 item->ttl = cgit_cache_repo_ttl;
35 } else { 35 } else {
36 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,
37 cgit_repo->url, cgit_query_page, 37 cache_safe_filename(cgit_repo->url), cgit_query_page,
38 cache_safe_filename(cgit_querystring))); 38 cache_safe_filename(cgit_querystring)));
39 if (cgit_query_has_symref) 39 if (cgit_query_has_symref)
40 item->ttl = cgit_cache_dynamic_ttl; 40 item->ttl = cgit_cache_dynamic_ttl;
41 else if (cgit_query_has_sha1) 41 else if (cgit_query_has_sha1)
42 item->ttl = cgit_cache_static_ttl; 42 item->ttl = cgit_cache_static_ttl;
43 else 43 else
44 item->ttl = cgit_cache_repo_ttl; 44 item->ttl = cgit_cache_repo_ttl;
45 } 45 }
46 return 1; 46 return 1;
47} 47}
48 48
49static void cgit_print_repo_page(struct cacheitem *item) 49static void cgit_print_repo_page(struct cacheitem *item)
50{ 50{
51 char *title; 51 char *title;
52 int show_search; 52 int show_search;
53 53
54 if (!cgit_query_head) 54 if (!cgit_query_head)
55 cgit_query_head = cgit_repo->defbranch; 55 cgit_query_head = cgit_repo->defbranch;
56 56
57 if (chdir(cgit_repo->path)) { 57 if (chdir(cgit_repo->path)) {
58 title = fmt("%s - %s", cgit_root_title, "Bad request"); 58 title = fmt("%s - %s", cgit_root_title, "Bad request");
59 cgit_print_docstart(title, item); 59 cgit_print_docstart(title, item);
60 cgit_print_pageheader(title, 0); 60 cgit_print_pageheader(title, 0);
61 cgit_print_error(fmt("Unable to scan repository: %s", 61 cgit_print_error(fmt("Unable to scan repository: %s",
62 strerror(errno))); 62 strerror(errno)));
63 cgit_print_docend(); 63 cgit_print_docend();
64 return; 64 return;
65 } 65 }
66 66
67 title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); 67 title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc);
68 show_search = 0; 68 show_search = 0;
69 setenv("GIT_DIR", cgit_repo->path, 1); 69 setenv("GIT_DIR", cgit_repo->path, 1);
70 70
71 if (cgit_query_page) { 71 if ((cgit_cmd == CMD_SNAPSHOT) && cgit_repo->snapshots) {
72 if (cgit_repo->snapshots && !strcmp(cgit_query_page, "snapshot")) {
73 cgit_print_snapshot(item, cgit_query_sha1, "zip", 72 cgit_print_snapshot(item, cgit_query_sha1, "zip",
74 cgit_repo->url, cgit_query_name); 73 cgit_repo->url, cgit_query_name);
75 return; 74 return;
76 }
77 if (!strcmp(cgit_query_page, "blob")) {
78 cgit_print_blob(item, cgit_query_sha1, cgit_query_path);
79 return;
80 }
81 } 75 }
82 76
83 if (cgit_query_page && !strcmp(cgit_query_page, "log")) 77 if (cgit_cmd == CMD_BLOB) {
84 show_search = 1; 78 cgit_print_blob(item, cgit_query_sha1, cgit_query_path);
79 return;
80 }
85 81
82 show_search = (cgit_cmd == CMD_LOG);
86 cgit_print_docstart(title, item); 83 cgit_print_docstart(title, item);
87 84 if (!cgit_cmd) {
88
89 if (!cgit_query_page) {
90 cgit_print_pageheader("summary", show_search); 85 cgit_print_pageheader("summary", show_search);
91 cgit_print_summary(); 86 cgit_print_summary();
92 cgit_print_docend(); 87 cgit_print_docend();
93 return; 88 return;
94 } 89 }
95 90
96 cgit_print_pageheader(cgit_query_page, show_search); 91 cgit_print_pageheader(cgit_query_page, show_search);
97 92
98 if (!strcmp(cgit_query_page, "log")) { 93 switch(cgit_cmd) {
94 case CMD_LOG:
99 cgit_print_log(cgit_query_head, cgit_query_ofs, 95 cgit_print_log(cgit_query_head, cgit_query_ofs,
100 cgit_max_commit_count, cgit_query_search, 96 cgit_max_commit_count, cgit_query_search,
101 cgit_query_path); 97 cgit_query_path);
102 } else if (!strcmp(cgit_query_page, "tree")) { 98 break;
99 case CMD_TREE:
103 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);
104 } else if (!strcmp(cgit_query_page, "commit")) { 101 break;
102 case CMD_COMMIT:
105 cgit_print_commit(cgit_query_head); 103 cgit_print_commit(cgit_query_head);
106 } else if (!strcmp(cgit_query_page, "view")) { 104 break;
105 case CMD_VIEW:
107 cgit_print_view(cgit_query_sha1, cgit_query_path); 106 cgit_print_view(cgit_query_sha1, cgit_query_path);
108 } else if (!strcmp(cgit_query_page, "diff")) { 107 break;
108 case CMD_DIFF:
109 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,
110 cgit_query_path); 110 cgit_query_path);
111 } else { 111 break;
112 default:
112 cgit_print_error("Invalid request"); 113 cgit_print_error("Invalid request");
113 } 114 }
114 cgit_print_docend(); 115 cgit_print_docend();
115} 116}
116 117
117static void cgit_fill_cache(struct cacheitem *item, int use_cache) 118static void cgit_fill_cache(struct cacheitem *item, int use_cache)
118{ 119{
119 static char buf[PATH_MAX]; 120 static char buf[PATH_MAX];
120 int stdout2; 121 int stdout2;
121 122
122 getcwd(buf, sizeof(buf)); 123 getcwd(buf, sizeof(buf));
123 item->st.st_mtime = time(NULL); 124 item->st.st_mtime = time(NULL);
124 125
125 if (use_cache) { 126 if (use_cache) {
126 stdout2 = chk_positive(dup(STDOUT_FILENO), 127 stdout2 = chk_positive(dup(STDOUT_FILENO),
127 "Preserving STDOUT"); 128 "Preserving STDOUT");
128 chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); 129 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
129 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); 130 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
130 } 131 }
131 132
132 if (cgit_query_repo) 133 if (cgit_repo)
133 cgit_print_repo_page(item); 134 cgit_print_repo_page(item);
134 else 135 else
135 cgit_print_repolist(item); 136 cgit_print_repolist(item);
136 137
137 if (use_cache) { 138 if (use_cache) {
138 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); 139 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
139 chk_positive(dup2(stdout2, STDOUT_FILENO), 140 chk_positive(dup2(stdout2, STDOUT_FILENO),
140 "Restoring original STDOUT"); 141 "Restoring original STDOUT");
141 chk_zero(close(stdout2), "Closing temporary STDOUT"); 142 chk_zero(close(stdout2), "Closing temporary STDOUT");
142 } 143 }
143 144
144 chdir(buf); 145 chdir(buf);
145} 146}
146 147
147static void cgit_check_cache(struct cacheitem *item) 148static void cgit_check_cache(struct cacheitem *item)
148{ 149{
149 int i = 0; 150 int i = 0;
150 151
151 top: 152 top:
152 if (++i > cgit_max_lock_attempts) { 153 if (++i > cgit_max_lock_attempts) {
153 die("cgit_refresh_cache: unable to lock %s: %s", 154 die("cgit_refresh_cache: unable to lock %s: %s",
154 item->name, strerror(errno)); 155 item->name, strerror(errno));
155 } 156 }
156 if (!cache_exist(item)) { 157 if (!cache_exist(item)) {
157 if (!cache_lock(item)) { 158 if (!cache_lock(item)) {
158 sleep(1); 159 sleep(1);
159 goto top; 160 goto top;
160 } 161 }
161 if (!cache_exist(item)) { 162 if (!cache_exist(item)) {
162 cgit_fill_cache(item, 1); 163 cgit_fill_cache(item, 1);
163 cache_unlock(item); 164 cache_unlock(item);
164 } else { 165 } else {
165 cache_cancel_lock(item); 166 cache_cancel_lock(item);
166 } 167 }
167 } else if (cache_expired(item) && cache_lock(item)) { 168 } else if (cache_expired(item) && cache_lock(item)) {
168 if (cache_expired(item)) { 169 if (cache_expired(item)) {
169 cgit_fill_cache(item, 1); 170 cgit_fill_cache(item, 1);
170 cache_unlock(item); 171 cache_unlock(item);
171 } else { 172 } else {
172 cache_cancel_lock(item); 173 cache_cancel_lock(item);
173 } 174 }
174 } 175 }
175} 176}
176 177
177static void cgit_print_cache(struct cacheitem *item) 178static void cgit_print_cache(struct cacheitem *item)
178{ 179{
179 static char buf[4096]; 180 static char buf[4096];
180 ssize_t i; 181 ssize_t i;
181 182
182 int fd = open(item->name, O_RDONLY); 183 int fd = open(item->name, O_RDONLY);
183 if (fd<0) 184 if (fd<0)
184 die("Unable to open cached file %s", item->name); 185 die("Unable to open cached file %s", item->name);
185 186
186 while((i=read(fd, buf, sizeof(buf))) > 0) 187 while((i=read(fd, buf, sizeof(buf))) > 0)
187 write(STDOUT_FILENO, buf, i); 188 write(STDOUT_FILENO, buf, i);
188 189
189 close(fd); 190 close(fd);
190} 191}
191 192
192static void cgit_parse_args(int argc, const char **argv) 193static void cgit_parse_args(int argc, const char **argv)
193{ 194{
194 int i; 195 int i;
195 196
196 for (i = 1; i < argc; i++) { 197 for (i = 1; i < argc; i++) {
197 if (!strncmp(argv[i], "--cache=", 8)) { 198 if (!strncmp(argv[i], "--cache=", 8)) {
198 cgit_cache_root = xstrdup(argv[i]+8); 199 cgit_cache_root = xstrdup(argv[i]+8);
199 } 200 }
200 if (!strcmp(argv[i], "--nocache")) { 201 if (!strcmp(argv[i], "--nocache")) {
201 cgit_nocache = 1; 202 cgit_nocache = 1;
202 } 203 }
203 if (!strncmp(argv[i], "--query=", 8)) { 204 if (!strncmp(argv[i], "--query=", 8)) {
204 cgit_querystring = xstrdup(argv[i]+8); 205 cgit_querystring = xstrdup(argv[i]+8);
205 } 206 }
206 if (!strncmp(argv[i], "--repo=", 7)) { 207 if (!strncmp(argv[i], "--repo=", 7)) {
207 cgit_query_repo = xstrdup(argv[i]+7); 208 cgit_query_repo = xstrdup(argv[i]+7);
208 } 209 }
209 if (!strncmp(argv[i], "--page=", 7)) { 210 if (!strncmp(argv[i], "--page=", 7)) {
210 cgit_query_page = xstrdup(argv[i]+7); 211 cgit_query_page = xstrdup(argv[i]+7);
211 } 212 }
212 if (!strncmp(argv[i], "--head=", 7)) { 213 if (!strncmp(argv[i], "--head=", 7)) {
213 cgit_query_head = xstrdup(argv[i]+7); 214 cgit_query_head = xstrdup(argv[i]+7);
214 cgit_query_has_symref = 1; 215 cgit_query_has_symref = 1;
215 } 216 }
216 if (!strncmp(argv[i], "--sha1=", 7)) { 217 if (!strncmp(argv[i], "--sha1=", 7)) {
217 cgit_query_sha1 = xstrdup(argv[i]+7); 218 cgit_query_sha1 = xstrdup(argv[i]+7);
218 cgit_query_has_sha1 = 1; 219 cgit_query_has_sha1 = 1;
219 } 220 }
220 if (!strncmp(argv[i], "--ofs=", 6)) { 221 if (!strncmp(argv[i], "--ofs=", 6)) {
221 cgit_query_ofs = atoi(argv[i]+6); 222 cgit_query_ofs = atoi(argv[i]+6);
222 } 223 }
223 } 224 }
224} 225}
225 226
226int main(int argc, const char **argv) 227int main(int argc, const char **argv)
227{ 228{
228 struct cacheitem item; 229 struct cacheitem item;
229 230
230 htmlfd = STDOUT_FILENO; 231 htmlfd = STDOUT_FILENO;
231 item.st.st_mtime = time(NULL); 232 item.st.st_mtime = time(NULL);
232 cgit_repolist.length = 0; 233 cgit_repolist.length = 0;
233 cgit_repolist.count = 0; 234 cgit_repolist.count = 0;
234 cgit_repolist.repos = NULL; 235 cgit_repolist.repos = NULL;
235 236
236 cgit_read_config(CGIT_CONFIG, cgit_global_config_cb); 237 cgit_read_config(CGIT_CONFIG, cgit_global_config_cb);
238 cgit_repo = NULL;
237 if (getenv("SCRIPT_NAME")) 239 if (getenv("SCRIPT_NAME"))
238 cgit_script_name = xstrdup(getenv("SCRIPT_NAME")); 240 cgit_script_name = xstrdup(getenv("SCRIPT_NAME"));
239 if (getenv("QUERY_STRING")) 241 if (getenv("QUERY_STRING"))
240 cgit_querystring = xstrdup(getenv("QUERY_STRING")); 242 cgit_querystring = xstrdup(getenv("QUERY_STRING"));
241 cgit_parse_args(argc, argv); 243 cgit_parse_args(argc, argv);
242 cgit_parse_query(cgit_querystring, cgit_querystring_cb); 244 cgit_parse_query(cgit_querystring, cgit_querystring_cb);
243 if (!cgit_prepare_cache(&item)) 245 if (!cgit_prepare_cache(&item))
244 return 0; 246 return 0;
245 if (cgit_nocache) { 247 if (cgit_nocache) {
246 cgit_fill_cache(&item, 0); 248 cgit_fill_cache(&item, 0);
247 } else { 249 } else {
248 cgit_check_cache(&item); 250 cgit_check_cache(&item);
249 cgit_print_cache(&item); 251 cgit_print_cache(&item);
250 } 252 }
251 return 0; 253 return 0;
252} 254}
diff --git a/cgit.h b/cgit.h
index f402466..e5b3f5e 100644
--- a/cgit.h
+++ b/cgit.h
@@ -1,199 +1,201 @@
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/* 21/*
22 * The valid cgit repo-commands 22 * The valid cgit repo-commands
23 */ 23 */
24#define CMD_LOG = 1; 24#define CMD_LOG 1
25#define CMD_COMMIT = 1; 25#define CMD_COMMIT 2
26#define CMD_DIFF = 1; 26#define CMD_DIFF 3
27#define CMD_TREE = 1; 27#define CMD_TREE 4
28#define CMD_VIEW = 1; 28#define CMD_VIEW 5
29#define CMD_BLOB = 1; 29#define CMD_BLOB 6
30#define CMD_SNAPSHOT = 1; 30#define CMD_SNAPSHOT 7
31 31
32typedef void (*configfn)(const char *name, const char *value); 32typedef void (*configfn)(const char *name, const char *value);
33typedef void (*filepair_fn)(struct diff_filepair *pair); 33typedef void (*filepair_fn)(struct diff_filepair *pair);
34typedef void (*linediff_fn)(char *line, int len); 34typedef void (*linediff_fn)(char *line, int len);
35 35
36struct cacheitem { 36struct cacheitem {
37 char *name; 37 char *name;
38 struct stat st; 38 struct stat st;
39 int ttl; 39 int ttl;
40 int fd; 40 int fd;
41}; 41};
42 42
43struct repoinfo { 43struct repoinfo {
44 char *url; 44 char *url;
45 char *name; 45 char *name;
46 char *path; 46 char *path;
47 char *desc; 47 char *desc;
48 char *owner; 48 char *owner;
49 char *defbranch; 49 char *defbranch;
50 char *module_link; 50 char *module_link;
51 int snapshots; 51 int snapshots;
52 int enable_log_filecount; 52 int enable_log_filecount;
53 int enable_log_linecount; 53 int enable_log_linecount;
54}; 54};
55 55
56struct repolist { 56struct repolist {
57 int length; 57 int length;
58 int count; 58 int count;
59 struct repoinfo *repos; 59 struct repoinfo *repos;
60}; 60};
61 61
62struct commitinfo { 62struct commitinfo {
63 struct commit *commit; 63 struct commit *commit;
64 char *author; 64 char *author;
65 char *author_email; 65 char *author_email;
66 unsigned long author_date; 66 unsigned long author_date;
67 char *committer; 67 char *committer;
68 char *committer_email; 68 char *committer_email;
69 unsigned long committer_date; 69 unsigned long committer_date;
70 char *subject; 70 char *subject;
71 char *msg; 71 char *msg;
72}; 72};
73 73
74struct taginfo { 74struct taginfo {
75 char *tagger; 75 char *tagger;
76 char *tagger_email; 76 char *tagger_email;
77 int tagger_date; 77 int tagger_date;
78 char *msg; 78 char *msg;
79}; 79};
80 80
81extern const char cgit_version[]; 81extern const char cgit_version[];
82 82
83extern struct repolist cgit_repolist; 83extern struct repolist cgit_repolist;
84extern struct repoinfo *cgit_repo; 84extern struct repoinfo *cgit_repo;
85extern int cgit_cmd;
85 86
86extern char *cgit_root_title; 87extern char *cgit_root_title;
87extern char *cgit_css; 88extern char *cgit_css;
88extern char *cgit_logo; 89extern char *cgit_logo;
89extern char *cgit_logo_link; 90extern char *cgit_logo_link;
90extern char *cgit_module_link; 91extern char *cgit_module_link;
91extern char *cgit_virtual_root; 92extern char *cgit_virtual_root;
92extern char *cgit_script_name; 93extern char *cgit_script_name;
93extern char *cgit_cache_root; 94extern char *cgit_cache_root;
94 95
95extern int cgit_nocache; 96extern int cgit_nocache;
96extern int cgit_snapshots; 97extern int cgit_snapshots;
97extern int cgit_enable_log_filecount; 98extern int cgit_enable_log_filecount;
98extern int cgit_enable_log_linecount; 99extern int cgit_enable_log_linecount;
99extern int cgit_max_lock_attempts; 100extern int cgit_max_lock_attempts;
100extern int cgit_cache_root_ttl; 101extern int cgit_cache_root_ttl;
101extern int cgit_cache_repo_ttl; 102extern int cgit_cache_repo_ttl;
102extern int cgit_cache_dynamic_ttl; 103extern int cgit_cache_dynamic_ttl;
103extern int cgit_cache_static_ttl; 104extern int cgit_cache_static_ttl;
104extern int cgit_cache_max_create_time; 105extern int cgit_cache_max_create_time;
105 106
106extern int cgit_max_msg_len; 107extern int cgit_max_msg_len;
107extern int cgit_max_repodesc_len; 108extern int cgit_max_repodesc_len;
108extern int cgit_max_commit_count; 109extern int cgit_max_commit_count;
109 110
110extern int cgit_query_has_symref; 111extern int cgit_query_has_symref;
111extern int cgit_query_has_sha1; 112extern int cgit_query_has_sha1;
112 113
113extern char *cgit_querystring; 114extern char *cgit_querystring;
114extern char *cgit_query_repo; 115extern char *cgit_query_repo;
115extern char *cgit_query_page; 116extern char *cgit_query_page;
116extern char *cgit_query_search; 117extern char *cgit_query_search;
117extern char *cgit_query_head; 118extern char *cgit_query_head;
118extern char *cgit_query_sha1; 119extern char *cgit_query_sha1;
119extern char *cgit_query_sha2; 120extern char *cgit_query_sha2;
120extern char *cgit_query_path; 121extern char *cgit_query_path;
121extern char *cgit_query_name; 122extern char *cgit_query_name;
122extern int cgit_query_ofs; 123extern int cgit_query_ofs;
123 124
124extern int htmlfd; 125extern int htmlfd;
125 126
126extern int cgit_get_cmd_index(const char *cmd); 127extern int cgit_get_cmd_index(const char *cmd);
127extern struct repoinfo *cgit_get_repoinfo(const char *url); 128extern struct repoinfo *cgit_get_repoinfo(const char *url);
128extern void cgit_global_config_cb(const char *name, const char *value); 129extern void cgit_global_config_cb(const char *name, const char *value);
129extern void cgit_repo_config_cb(const char *name, const char *value); 130extern void cgit_repo_config_cb(const char *name, const char *value);
130extern void cgit_querystring_cb(const char *name, const char *value); 131extern void cgit_querystring_cb(const char *name, const char *value);
131 132
132extern int chk_zero(int result, char *msg); 133extern int chk_zero(int result, char *msg);
133extern int chk_positive(int result, char *msg); 134extern int chk_positive(int result, char *msg);
134 135
135extern int hextoint(char c); 136extern int hextoint(char c);
136 137
137extern void *cgit_free_commitinfo(struct commitinfo *info); 138extern void *cgit_free_commitinfo(struct commitinfo *info);
138 139
139extern int cgit_diff_files(const unsigned char *old_sha1, 140extern int cgit_diff_files(const unsigned char *old_sha1,
140 const unsigned char *new_sha1, 141 const unsigned char *new_sha1,
141 linediff_fn fn); 142 linediff_fn fn);
142 143
143extern void cgit_diff_tree(const unsigned char *old_sha1, 144extern void cgit_diff_tree(const unsigned char *old_sha1,
144 const unsigned char *new_sha1, 145 const unsigned char *new_sha1,
145 filepair_fn fn); 146 filepair_fn fn);
146 147
147extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); 148extern void cgit_diff_commit(struct commit *commit, filepair_fn fn);
148 149
149extern char *fmt(const char *format,...); 150extern char *fmt(const char *format,...);
150 151
151extern void html(const char *txt); 152extern void html(const char *txt);
152extern void htmlf(const char *format,...); 153extern void htmlf(const char *format,...);
153extern void html_txt(char *txt); 154extern void html_txt(char *txt);
154extern void html_ntxt(int len, char *txt); 155extern void html_ntxt(int len, char *txt);
155extern void html_attr(char *txt); 156extern void html_attr(char *txt);
156extern void html_hidden(char *name, char *value); 157extern void html_hidden(char *name, char *value);
157extern void html_link_open(char *url, char *title, char *class); 158extern void html_link_open(char *url, char *title, char *class);
158extern void html_link_close(void); 159extern void html_link_close(void);
159extern void html_filemode(unsigned short mode); 160extern void html_filemode(unsigned short mode);
160 161
161extern int cgit_read_config(const char *filename, configfn fn); 162extern int cgit_read_config(const char *filename, configfn fn);
162extern int cgit_parse_query(char *txt, configfn fn); 163extern int cgit_parse_query(char *txt, configfn fn);
163extern struct commitinfo *cgit_parse_commit(struct commit *commit); 164extern struct commitinfo *cgit_parse_commit(struct commit *commit);
164extern struct taginfo *cgit_parse_tag(struct tag *tag); 165extern struct taginfo *cgit_parse_tag(struct tag *tag);
166extern void cgit_parse_url(const char *url);
165 167
166extern char *cache_safe_filename(const char *unsafe); 168extern char *cache_safe_filename(const char *unsafe);
167extern int cache_lock(struct cacheitem *item); 169extern int cache_lock(struct cacheitem *item);
168extern int cache_unlock(struct cacheitem *item); 170extern int cache_unlock(struct cacheitem *item);
169extern int cache_cancel_lock(struct cacheitem *item); 171extern int cache_cancel_lock(struct cacheitem *item);
170extern int cache_exist(struct cacheitem *item); 172extern int cache_exist(struct cacheitem *item);
171extern int cache_expired(struct cacheitem *item); 173extern int cache_expired(struct cacheitem *item);
172 174
173extern char *cgit_repourl(const char *reponame); 175extern char *cgit_repourl(const char *reponame);
174extern char *cgit_pageurl(const char *reponame, const char *pagename, 176extern char *cgit_pageurl(const char *reponame, const char *pagename,
175 const char *query); 177 const char *query);
176 178
177extern void cgit_print_error(char *msg); 179extern void cgit_print_error(char *msg);
178extern void cgit_print_date(unsigned long secs); 180extern void cgit_print_date(unsigned long secs);
179extern void cgit_print_docstart(char *title, struct cacheitem *item); 181extern void cgit_print_docstart(char *title, struct cacheitem *item);
180extern void cgit_print_docend(); 182extern void cgit_print_docend();
181extern void cgit_print_pageheader(char *title, int show_search); 183extern void cgit_print_pageheader(char *title, int show_search);
182extern void cgit_print_snapshot_start(const char *mimetype, 184extern void cgit_print_snapshot_start(const char *mimetype,
183 const char *filename, 185 const char *filename,
184 struct cacheitem *item); 186 struct cacheitem *item);
185 187
186extern void cgit_print_repolist(struct cacheitem *item); 188extern void cgit_print_repolist(struct cacheitem *item);
187extern void cgit_print_summary(); 189extern void cgit_print_summary();
188extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *path); 190extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *path);
189extern void cgit_print_view(const char *hex, char *path); 191extern void cgit_print_view(const char *hex, char *path);
190extern void cgit_print_blob(struct cacheitem *item, const char *hex, char *path); 192extern void cgit_print_blob(struct cacheitem *item, const char *hex, char *path);
191extern void cgit_print_tree(const char *rev, const char *hex, char *path); 193extern void cgit_print_tree(const char *rev, const char *hex, char *path);
192extern void cgit_print_commit(const char *hex); 194extern void cgit_print_commit(const char *hex);
193extern void cgit_print_diff(const char *head, const char *old_hex, const char *new_hex, 195extern void cgit_print_diff(const char *head, const char *old_hex, const char *new_hex,
194 char *path); 196 char *path);
195extern void cgit_print_snapshot(struct cacheitem *item, const char *hex, 197extern void cgit_print_snapshot(struct cacheitem *item, const char *hex,
196 const char *format, const char *prefix, 198 const char *format, const char *prefix,
197 const char *filename); 199 const char *filename);
198 200
199#endif /* CGIT_H */ 201#endif /* CGIT_H */
diff --git a/parsing.c b/parsing.c
index 36b0f0c..a028625 100644
--- a/parsing.c
+++ b/parsing.c
@@ -1,249 +1,292 @@
1/* config.c: parsing of config files 1/* config.c: parsing of config files
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
11int next_char(FILE *f) 11int next_char(FILE *f)
12{ 12{
13 int c = fgetc(f); 13 int c = fgetc(f);
14 if (c=='\r') { 14 if (c=='\r') {
15 c = fgetc(f); 15 c = fgetc(f);
16 if (c!='\n') { 16 if (c!='\n') {
17 ungetc(c, f); 17 ungetc(c, f);
18 c = '\r'; 18 c = '\r';
19 } 19 }
20 } 20 }
21 return c; 21 return c;
22} 22}
23 23
24void skip_line(FILE *f) 24void skip_line(FILE *f)
25{ 25{
26 int c; 26 int c;
27 27
28 while((c=next_char(f)) && c!='\n' && c!=EOF) 28 while((c=next_char(f)) && c!='\n' && c!=EOF)
29 ; 29 ;
30} 30}
31 31
32int read_config_line(FILE *f, char *line, const char **value, int bufsize) 32int read_config_line(FILE *f, char *line, const char **value, int bufsize)
33{ 33{
34 int i = 0, isname = 0; 34 int i = 0, isname = 0;
35 35
36 *value = NULL; 36 *value = NULL;
37 while(i<bufsize-1) { 37 while(i<bufsize-1) {
38 int c = next_char(f); 38 int c = next_char(f);
39 if (!isname && (c=='#' || c==';')) { 39 if (!isname && (c=='#' || c==';')) {
40 skip_line(f); 40 skip_line(f);
41 continue; 41 continue;
42 } 42 }
43 if (!isname && isspace(c)) 43 if (!isname && isspace(c))
44 continue; 44 continue;
45 45
46 if (c=='=' && !*value) { 46 if (c=='=' && !*value) {
47 line[i] = 0; 47 line[i] = 0;
48 *value = &line[i+1]; 48 *value = &line[i+1];
49 } else if (c=='\n' && !isname) { 49 } else if (c=='\n' && !isname) {
50 i = 0; 50 i = 0;
51 continue; 51 continue;
52 } else if (c=='\n' || c==EOF) { 52 } else if (c=='\n' || c==EOF) {
53 line[i] = 0; 53 line[i] = 0;
54 break; 54 break;
55 } else { 55 } else {
56 line[i]=c; 56 line[i]=c;
57 } 57 }
58 isname = 1; 58 isname = 1;
59 i++; 59 i++;
60 } 60 }
61 line[i+1] = 0; 61 line[i+1] = 0;
62 return i; 62 return i;
63} 63}
64 64
65int cgit_read_config(const char *filename, configfn fn) 65int cgit_read_config(const char *filename, configfn fn)
66{ 66{
67 static int nesting; 67 static int nesting;
68 int len; 68 int len;
69 char line[256]; 69 char line[256];
70 const char *value; 70 const char *value;
71 FILE *f; 71 FILE *f;
72 72
73 /* cancel deeply nested include-commands */ 73 /* cancel deeply nested include-commands */
74 if (nesting > 8) 74 if (nesting > 8)
75 return -1; 75 return -1;
76 if (!(f = fopen(filename, "r"))) 76 if (!(f = fopen(filename, "r")))
77 return -1; 77 return -1;
78 nesting++; 78 nesting++;
79 while((len = read_config_line(f, line, &value, sizeof(line))) > 0) 79 while((len = read_config_line(f, line, &value, sizeof(line))) > 0)
80 (*fn)(line, value); 80 (*fn)(line, value);
81 nesting--; 81 nesting--;
82 fclose(f); 82 fclose(f);
83 return 0; 83 return 0;
84} 84}
85 85
86char *convert_query_hexchar(char *txt) 86char *convert_query_hexchar(char *txt)
87{ 87{
88 int d1, d2; 88 int d1, d2;
89 if (strlen(txt) < 3) { 89 if (strlen(txt) < 3) {
90 *txt = '\0'; 90 *txt = '\0';
91 return txt-1; 91 return txt-1;
92 } 92 }
93 d1 = hextoint(*(txt+1)); 93 d1 = hextoint(*(txt+1));
94 d2 = hextoint(*(txt+2)); 94 d2 = hextoint(*(txt+2));
95 if (d1<0 || d2<0) { 95 if (d1<0 || d2<0) {
96 strcpy(txt, txt+3); 96 strcpy(txt, txt+3);
97 return txt-1; 97 return txt-1;
98 } else { 98 } else {
99 *txt = d1 * 16 + d2; 99 *txt = d1 * 16 + d2;
100 strcpy(txt+1, txt+3); 100 strcpy(txt+1, txt+3);
101 return txt; 101 return txt;
102 } 102 }
103} 103}
104 104
105int cgit_parse_query(char *txt, configfn fn) 105int cgit_parse_query(char *txt, configfn fn)
106{ 106{
107 char *t, *value = NULL, c; 107 char *t, *value = NULL, c;
108 108
109 if (!txt) 109 if (!txt)
110 return 0; 110 return 0;
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 cgit_query_path = xstrdup(p + 1);
171 }
172 cgit_cmd = cgit_get_cmd_index(cmd + 1);
173 cgit_query_page = xstrdup(cmd + 1);
174 return;
175 }
176}
177
135char *substr(const char *head, const char *tail) 178char *substr(const char *head, const char *tail)
136{ 179{
137 char *buf; 180 char *buf;
138 181
139 buf = xmalloc(tail - head + 1); 182 buf = xmalloc(tail - head + 1);
140 strncpy(buf, head, tail - head); 183 strncpy(buf, head, tail - head);
141 buf[tail - head] = '\0'; 184 buf[tail - head] = '\0';
142 return buf; 185 return buf;
143} 186}
144 187
145struct commitinfo *cgit_parse_commit(struct commit *commit) 188struct commitinfo *cgit_parse_commit(struct commit *commit)
146{ 189{
147 struct commitinfo *ret; 190 struct commitinfo *ret;
148 char *p = commit->buffer, *t = commit->buffer; 191 char *p = commit->buffer, *t = commit->buffer;
149 192
150 ret = xmalloc(sizeof(*ret)); 193 ret = xmalloc(sizeof(*ret));
151 ret->commit = commit; 194 ret->commit = commit;
152 ret->author = NULL; 195 ret->author = NULL;
153 ret->author_email = NULL; 196 ret->author_email = NULL;
154 ret->committer = NULL; 197 ret->committer = NULL;
155 ret->committer_email = NULL; 198 ret->committer_email = NULL;
156 ret->subject = NULL; 199 ret->subject = NULL;
157 ret->msg = NULL; 200 ret->msg = NULL;
158 201
159 if (strncmp(p, "tree ", 5)) 202 if (strncmp(p, "tree ", 5))
160 die("Bad commit: %s", sha1_to_hex(commit->object.sha1)); 203 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
161 else 204 else
162 p += 46; // "tree " + hex[40] + "\n" 205 p += 46; // "tree " + hex[40] + "\n"
163 206
164 while (!strncmp(p, "parent ", 7)) 207 while (!strncmp(p, "parent ", 7))
165 p += 48; // "parent " + hex[40] + "\n" 208 p += 48; // "parent " + hex[40] + "\n"
166 209
167 if (!strncmp(p, "author ", 7)) { 210 if (!strncmp(p, "author ", 7)) {
168 p += 7; 211 p += 7;
169 t = strchr(p, '<') - 1; 212 t = strchr(p, '<') - 1;
170 ret->author = substr(p, t); 213 ret->author = substr(p, t);
171 p = t; 214 p = t;
172 t = strchr(t, '>') + 1; 215 t = strchr(t, '>') + 1;
173 ret->author_email = substr(p, t); 216 ret->author_email = substr(p, t);
174 ret->author_date = atol(++t); 217 ret->author_date = atol(++t);
175 p = strchr(t, '\n') + 1; 218 p = strchr(t, '\n') + 1;
176 } 219 }
177 220
178 if (!strncmp(p, "committer ", 9)) { 221 if (!strncmp(p, "committer ", 9)) {
179 p += 9; 222 p += 9;
180 t = strchr(p, '<') - 1; 223 t = strchr(p, '<') - 1;
181 ret->committer = substr(p, t); 224 ret->committer = substr(p, t);
182 p = t; 225 p = t;
183 t = strchr(t, '>') + 1; 226 t = strchr(t, '>') + 1;
184 ret->committer_email = substr(p, t); 227 ret->committer_email = substr(p, t);
185 ret->committer_date = atol(++t); 228 ret->committer_date = atol(++t);
186 p = strchr(t, '\n') + 1; 229 p = strchr(t, '\n') + 1;
187 } 230 }
188 231
189 while (*p == '\n') 232 while (*p == '\n')
190 p = strchr(p, '\n') + 1; 233 p = strchr(p, '\n') + 1;
191 234
192 t = strchr(p, '\n'); 235 t = strchr(p, '\n');
193 if (t && *t) { 236 if (t && *t) {
194 ret->subject = substr(p, t); 237 ret->subject = substr(p, t);
195 p = t + 1; 238 p = t + 1;
196 239
197 while (*p == '\n') 240 while (*p == '\n')
198 p = strchr(p, '\n') + 1; 241 p = strchr(p, '\n') + 1;
199 ret->msg = p; 242 ret->msg = p;
200 } 243 }
201 return ret; 244 return ret;
202} 245}
203 246
204 247
205struct taginfo *cgit_parse_tag(struct tag *tag) 248struct taginfo *cgit_parse_tag(struct tag *tag)
206{ 249{
207 void *data; 250 void *data;
208 enum object_type type; 251 enum object_type type;
209 unsigned long size; 252 unsigned long size;
210 char *p, *t; 253 char *p, *t;
211 struct taginfo *ret; 254 struct taginfo *ret;
212 255
213 data = read_sha1_file(tag->object.sha1, &type, &size); 256 data = read_sha1_file(tag->object.sha1, &type, &size);
214 if (!data || type != OBJ_TAG) { 257 if (!data || type != OBJ_TAG) {
215 free(data); 258 free(data);
216 return 0; 259 return 0;
217 } 260 }
218 261
219 ret = xmalloc(sizeof(*ret)); 262 ret = xmalloc(sizeof(*ret));
220 ret->tagger = NULL; 263 ret->tagger = NULL;
221 ret->tagger_email = NULL; 264 ret->tagger_email = NULL;
222 ret->tagger_date = 0; 265 ret->tagger_date = 0;
223 ret->msg = NULL; 266 ret->msg = NULL;
224 267
225 p = data; 268 p = data;
226 269
227 while (p && *p) { 270 while (p && *p) {
228 if (*p == '\n') 271 if (*p == '\n')
229 break; 272 break;
230 273
231 if (!strncmp(p, "tagger ", 7)) { 274 if (!strncmp(p, "tagger ", 7)) {
232 p += 7; 275 p += 7;
233 t = strchr(p, '<') - 1; 276 t = strchr(p, '<') - 1;
234 ret->tagger = substr(p, t); 277 ret->tagger = substr(p, t);
235 p = t; 278 p = t;
236 t = strchr(t, '>') + 1; 279 t = strchr(t, '>') + 1;
237 ret->tagger_email = substr(p, t); 280 ret->tagger_email = substr(p, t);
238 ret->tagger_date = atol(++t); 281 ret->tagger_date = atol(++t);
239 } 282 }
240 p = strchr(p, '\n') + 1; 283 p = strchr(p, '\n') + 1;
241 } 284 }
242 285
243 while (p && (*p == '\n')) 286 while (p && (*p == '\n'))
244 p = strchr(p, '\n') + 1; 287 p = strchr(p, '\n') + 1;
245 if (p && *p) 288 if (p && *p)
246 ret->msg = xstrdup(p); 289 ret->msg = xstrdup(p);
247 free(data); 290 free(data);
248 return ret; 291 return ret;
249} 292}
diff --git a/shared.c b/shared.c
index fb05427..0b074da 100644
--- a/shared.c
+++ b/shared.c
@@ -1,353 +1,358 @@
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_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; 18char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/";
18char *cgit_module_link = "./?repo=%s&page=commit&id=%s"; 19char *cgit_module_link = "./?repo=%s&page=commit&id=%s";
19char *cgit_virtual_root = NULL; 20char *cgit_virtual_root = NULL;
20char *cgit_script_name = CGIT_SCRIPT_NAME; 21char *cgit_script_name = CGIT_SCRIPT_NAME;
21char *cgit_cache_root = "/var/cache/cgit"; 22char *cgit_cache_root = "/var/cache/cgit";
22 23
23int cgit_nocache = 0; 24int cgit_nocache = 0;
24int cgit_snapshots = 0; 25int cgit_snapshots = 0;
25int cgit_enable_log_filecount = 0; 26int cgit_enable_log_filecount = 0;
26int cgit_enable_log_linecount = 0; 27int cgit_enable_log_linecount = 0;
27int cgit_max_lock_attempts = 5; 28int cgit_max_lock_attempts = 5;
28int cgit_cache_root_ttl = 5; 29int cgit_cache_root_ttl = 5;
29int cgit_cache_repo_ttl = 5; 30int cgit_cache_repo_ttl = 5;
30int cgit_cache_dynamic_ttl = 5; 31int cgit_cache_dynamic_ttl = 5;
31int cgit_cache_static_ttl = -1; 32int cgit_cache_static_ttl = -1;
32int cgit_cache_max_create_time = 5; 33int cgit_cache_max_create_time = 5;
33 34
34int cgit_max_msg_len = 60; 35int cgit_max_msg_len = 60;
35int cgit_max_repodesc_len = 60; 36int cgit_max_repodesc_len = 60;
36int cgit_max_commit_count = 50; 37int cgit_max_commit_count = 50;
37 38
38int cgit_query_has_symref = 0; 39int cgit_query_has_symref = 0;
39int cgit_query_has_sha1 = 0; 40int cgit_query_has_sha1 = 0;
40 41
41char *cgit_querystring = NULL; 42char *cgit_querystring = NULL;
42char *cgit_query_repo = NULL; 43char *cgit_query_repo = NULL;
43char *cgit_query_page = NULL; 44char *cgit_query_page = NULL;
44char *cgit_query_head = NULL; 45char *cgit_query_head = NULL;
45char *cgit_query_search = NULL; 46char *cgit_query_search = NULL;
46char *cgit_query_sha1 = NULL; 47char *cgit_query_sha1 = NULL;
47char *cgit_query_sha2 = NULL; 48char *cgit_query_sha2 = NULL;
48char *cgit_query_path = NULL; 49char *cgit_query_path = NULL;
49char *cgit_query_name = NULL; 50char *cgit_query_name = NULL;
50int cgit_query_ofs = 0; 51int cgit_query_ofs = 0;
51 52
52int htmlfd = 0; 53int htmlfd = 0;
53 54
54 55
55int cgit_get_cmd_index(const char *cmd) 56int cgit_get_cmd_index(const char *cmd)
56{ 57{
57 static char *cmds[] = {"log", "commit", "diff", "tree", "view", "blob", "snapshot", NULL}; 58 static char *cmds[] = {"log", "commit", "diff", "tree", "view", "blob", "snapshot", NULL};
58 int i; 59 int i;
59 60
60 for(i = 0; cmds[i]; i++) 61 for(i = 0; cmds[i]; i++)
61 if (!strcmp(cmd, cmds[i])) 62 if (!strcmp(cmd, cmds[i]))
62 return i + 1; 63 return i + 1;
63 return 0; 64 return 0;
64} 65}
65 66
66int chk_zero(int result, char *msg) 67int chk_zero(int result, char *msg)
67{ 68{
68 if (result != 0) 69 if (result != 0)
69 die("%s: %s", msg, strerror(errno)); 70 die("%s: %s", msg, strerror(errno));
70 return result; 71 return result;
71} 72}
72 73
73int chk_positive(int result, char *msg) 74int chk_positive(int result, char *msg)
74{ 75{
75 if (result <= 0) 76 if (result <= 0)
76 die("%s: %s", msg, strerror(errno)); 77 die("%s: %s", msg, strerror(errno));
77 return result; 78 return result;
78} 79}
79 80
80struct repoinfo *add_repo(const char *url) 81struct repoinfo *add_repo(const char *url)
81{ 82{
82 struct repoinfo *ret; 83 struct repoinfo *ret;
83 84
84 if (++cgit_repolist.count > cgit_repolist.length) { 85 if (++cgit_repolist.count > cgit_repolist.length) {
85 if (cgit_repolist.length == 0) 86 if (cgit_repolist.length == 0)
86 cgit_repolist.length = 8; 87 cgit_repolist.length = 8;
87 else 88 else
88 cgit_repolist.length *= 2; 89 cgit_repolist.length *= 2;
89 cgit_repolist.repos = xrealloc(cgit_repolist.repos, 90 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
90 cgit_repolist.length * 91 cgit_repolist.length *
91 sizeof(struct repoinfo)); 92 sizeof(struct repoinfo));
92 } 93 }
93 94
94 ret = &cgit_repolist.repos[cgit_repolist.count-1]; 95 ret = &cgit_repolist.repos[cgit_repolist.count-1];
95 ret->url = xstrdup(url); 96 ret->url = xstrdup(url);
96 ret->name = ret->url; 97 ret->name = ret->url;
97 ret->path = NULL; 98 ret->path = NULL;
98 ret->desc = NULL; 99 ret->desc = NULL;
99 ret->owner = NULL; 100 ret->owner = NULL;
100 ret->defbranch = "master"; 101 ret->defbranch = "master";
101 ret->snapshots = cgit_snapshots; 102 ret->snapshots = cgit_snapshots;
102 ret->enable_log_filecount = cgit_enable_log_filecount; 103 ret->enable_log_filecount = cgit_enable_log_filecount;
103 ret->enable_log_linecount = cgit_enable_log_linecount; 104 ret->enable_log_linecount = cgit_enable_log_linecount;
104 ret->module_link = cgit_module_link; 105 ret->module_link = cgit_module_link;
105 return ret; 106 return ret;
106} 107}
107 108
108struct repoinfo *cgit_get_repoinfo(const char *url) 109struct repoinfo *cgit_get_repoinfo(const char *url)
109{ 110{
110 int i; 111 int i;
111 struct repoinfo *repo; 112 struct repoinfo *repo;
112 113
113 for (i=0; i<cgit_repolist.count; i++) { 114 for (i=0; i<cgit_repolist.count; i++) {
114 repo = &cgit_repolist.repos[i]; 115 repo = &cgit_repolist.repos[i];
115 if (!strcmp(repo->url, url)) 116 if (!strcmp(repo->url, url))
116 return repo; 117 return repo;
117 } 118 }
118 return NULL; 119 return NULL;
119} 120}
120 121
121void cgit_global_config_cb(const char *name, const char *value) 122void cgit_global_config_cb(const char *name, const char *value)
122{ 123{
123 if (!strcmp(name, "root-title")) 124 if (!strcmp(name, "root-title"))
124 cgit_root_title = xstrdup(value); 125 cgit_root_title = xstrdup(value);
125 else if (!strcmp(name, "css")) 126 else if (!strcmp(name, "css"))
126 cgit_css = xstrdup(value); 127 cgit_css = xstrdup(value);
127 else if (!strcmp(name, "logo")) 128 else if (!strcmp(name, "logo"))
128 cgit_logo = xstrdup(value); 129 cgit_logo = xstrdup(value);
129 else if (!strcmp(name, "logo-link")) 130 else if (!strcmp(name, "logo-link"))
130 cgit_logo_link = xstrdup(value); 131 cgit_logo_link = xstrdup(value);
131 else if (!strcmp(name, "module-link")) 132 else if (!strcmp(name, "module-link"))
132 cgit_module_link = xstrdup(value); 133 cgit_module_link = xstrdup(value);
133 else if (!strcmp(name, "virtual-root")) 134 else if (!strcmp(name, "virtual-root"))
134 cgit_virtual_root = xstrdup(value); 135 cgit_virtual_root = xstrdup(value);
135 else if (!strcmp(name, "nocache")) 136 else if (!strcmp(name, "nocache"))
136 cgit_nocache = atoi(value); 137 cgit_nocache = atoi(value);
137 else if (!strcmp(name, "snapshots")) 138 else if (!strcmp(name, "snapshots"))
138 cgit_snapshots = atoi(value); 139 cgit_snapshots = atoi(value);
139 else if (!strcmp(name, "enable-log-filecount")) 140 else if (!strcmp(name, "enable-log-filecount"))
140 cgit_enable_log_filecount = atoi(value); 141 cgit_enable_log_filecount = atoi(value);
141 else if (!strcmp(name, "enable-log-linecount")) 142 else if (!strcmp(name, "enable-log-linecount"))
142 cgit_enable_log_linecount = atoi(value); 143 cgit_enable_log_linecount = atoi(value);
143 else if (!strcmp(name, "cache-root")) 144 else if (!strcmp(name, "cache-root"))
144 cgit_cache_root = xstrdup(value); 145 cgit_cache_root = xstrdup(value);
145 else if (!strcmp(name, "cache-root-ttl")) 146 else if (!strcmp(name, "cache-root-ttl"))
146 cgit_cache_root_ttl = atoi(value); 147 cgit_cache_root_ttl = atoi(value);
147 else if (!strcmp(name, "cache-repo-ttl")) 148 else if (!strcmp(name, "cache-repo-ttl"))
148 cgit_cache_repo_ttl = atoi(value); 149 cgit_cache_repo_ttl = atoi(value);
149 else if (!strcmp(name, "cache-static-ttl")) 150 else if (!strcmp(name, "cache-static-ttl"))
150 cgit_cache_static_ttl = atoi(value); 151 cgit_cache_static_ttl = atoi(value);
151 else if (!strcmp(name, "cache-dynamic-ttl")) 152 else if (!strcmp(name, "cache-dynamic-ttl"))
152 cgit_cache_dynamic_ttl = atoi(value); 153 cgit_cache_dynamic_ttl = atoi(value);
153 else if (!strcmp(name, "max-message-length")) 154 else if (!strcmp(name, "max-message-length"))
154 cgit_max_msg_len = atoi(value); 155 cgit_max_msg_len = atoi(value);
155 else if (!strcmp(name, "max-repodesc-length")) 156 else if (!strcmp(name, "max-repodesc-length"))
156 cgit_max_repodesc_len = atoi(value); 157 cgit_max_repodesc_len = atoi(value);
157 else if (!strcmp(name, "max-commit-count")) 158 else if (!strcmp(name, "max-commit-count"))
158 cgit_max_commit_count = atoi(value); 159 cgit_max_commit_count = atoi(value);
159 else if (!strcmp(name, "repo.url")) 160 else if (!strcmp(name, "repo.url"))
160 cgit_repo = add_repo(value); 161 cgit_repo = add_repo(value);
161 else if (!strcmp(name, "repo.name")) 162 else if (!strcmp(name, "repo.name"))
162 cgit_repo->name = xstrdup(value); 163 cgit_repo->name = xstrdup(value);
163 else if (cgit_repo && !strcmp(name, "repo.path")) 164 else if (cgit_repo && !strcmp(name, "repo.path"))
164 cgit_repo->path = xstrdup(value); 165 cgit_repo->path = xstrdup(value);
165 else if (cgit_repo && !strcmp(name, "repo.desc")) 166 else if (cgit_repo && !strcmp(name, "repo.desc"))
166 cgit_repo->desc = xstrdup(value); 167 cgit_repo->desc = xstrdup(value);
167 else if (cgit_repo && !strcmp(name, "repo.owner")) 168 else if (cgit_repo && !strcmp(name, "repo.owner"))
168 cgit_repo->owner = xstrdup(value); 169 cgit_repo->owner = xstrdup(value);
169 else if (cgit_repo && !strcmp(name, "repo.defbranch")) 170 else if (cgit_repo && !strcmp(name, "repo.defbranch"))
170 cgit_repo->defbranch = xstrdup(value); 171 cgit_repo->defbranch = xstrdup(value);
171 else if (cgit_repo && !strcmp(name, "repo.snapshots")) 172 else if (cgit_repo && !strcmp(name, "repo.snapshots"))
172 cgit_repo->snapshots = cgit_snapshots * atoi(value); 173 cgit_repo->snapshots = cgit_snapshots * atoi(value);
173 else if (cgit_repo && !strcmp(name, "repo.enable-log-filecount")) 174 else if (cgit_repo && !strcmp(name, "repo.enable-log-filecount"))
174 cgit_repo->enable_log_filecount = cgit_enable_log_filecount * atoi(value); 175 cgit_repo->enable_log_filecount = cgit_enable_log_filecount * atoi(value);
175 else if (cgit_repo && !strcmp(name, "repo.enable-log-linecount")) 176 else if (cgit_repo && !strcmp(name, "repo.enable-log-linecount"))
176 cgit_repo->enable_log_linecount = cgit_enable_log_linecount * atoi(value); 177 cgit_repo->enable_log_linecount = cgit_enable_log_linecount * atoi(value);
177 else if (cgit_repo && !strcmp(name, "repo.module-link")) 178 else if (cgit_repo && !strcmp(name, "repo.module-link"))
178 cgit_repo->module_link= xstrdup(value); 179 cgit_repo->module_link= xstrdup(value);
179 else if (!strcmp(name, "include")) 180 else if (!strcmp(name, "include"))
180 cgit_read_config(value, cgit_global_config_cb); 181 cgit_read_config(value, cgit_global_config_cb);
181} 182}
182 183
183void cgit_querystring_cb(const char *name, const char *value) 184void cgit_querystring_cb(const char *name, const char *value)
184{ 185{
185 if (!strcmp(name,"r")) { 186 if (!strcmp(name,"r")) {
186 cgit_query_repo = xstrdup(value); 187 cgit_query_repo = xstrdup(value);
188 cgit_repo = cgit_get_repoinfo(value);
187 } else if (!strcmp(name, "p")) { 189 } else if (!strcmp(name, "p")) {
188 cgit_query_page = xstrdup(value); 190 cgit_query_page = xstrdup(value);
191 cgit_cmd = cgit_get_cmd_index(value);
192 } else if (!strcmp(name, "url")) {
193 cgit_parse_url(value);
189 } else if (!strcmp(name, "q")) { 194 } else if (!strcmp(name, "q")) {
190 cgit_query_search = xstrdup(value); 195 cgit_query_search = xstrdup(value);
191 } else if (!strcmp(name, "h")) { 196 } else if (!strcmp(name, "h")) {
192 cgit_query_head = xstrdup(value); 197 cgit_query_head = xstrdup(value);
193 cgit_query_has_symref = 1; 198 cgit_query_has_symref = 1;
194 } else if (!strcmp(name, "id")) { 199 } else if (!strcmp(name, "id")) {
195 cgit_query_sha1 = xstrdup(value); 200 cgit_query_sha1 = xstrdup(value);
196 cgit_query_has_sha1 = 1; 201 cgit_query_has_sha1 = 1;
197 } else if (!strcmp(name, "id2")) { 202 } else if (!strcmp(name, "id2")) {
198 cgit_query_sha2 = xstrdup(value); 203 cgit_query_sha2 = xstrdup(value);
199 cgit_query_has_sha1 = 1; 204 cgit_query_has_sha1 = 1;
200 } else if (!strcmp(name, "ofs")) { 205 } else if (!strcmp(name, "ofs")) {
201 cgit_query_ofs = atoi(value); 206 cgit_query_ofs = atoi(value);
202 } else if (!strcmp(name, "path")) { 207 } else if (!strcmp(name, "path")) {
203 cgit_query_path = xstrdup(value); 208 cgit_query_path = xstrdup(value);
204 } else if (!strcmp(name, "name")) { 209 } else if (!strcmp(name, "name")) {
205 cgit_query_name = xstrdup(value); 210 cgit_query_name = xstrdup(value);
206 } 211 }
207} 212}
208 213
209void *cgit_free_commitinfo(struct commitinfo *info) 214void *cgit_free_commitinfo(struct commitinfo *info)
210{ 215{
211 free(info->author); 216 free(info->author);
212 free(info->author_email); 217 free(info->author_email);
213 free(info->committer); 218 free(info->committer);
214 free(info->committer_email); 219 free(info->committer_email);
215 free(info->subject); 220 free(info->subject);
216 free(info); 221 free(info);
217 return NULL; 222 return NULL;
218} 223}
219 224
220int hextoint(char c) 225int hextoint(char c)
221{ 226{
222 if (c >= 'a' && c <= 'f') 227 if (c >= 'a' && c <= 'f')
223 return 10 + c - 'a'; 228 return 10 + c - 'a';
224 else if (c >= 'A' && c <= 'F') 229 else if (c >= 'A' && c <= 'F')
225 return 10 + c - 'A'; 230 return 10 + c - 'A';
226 else if (c >= '0' && c <= '9') 231 else if (c >= '0' && c <= '9')
227 return c - '0'; 232 return c - '0';
228 else 233 else
229 return -1; 234 return -1;
230} 235}
231 236
232void cgit_diff_tree_cb(struct diff_queue_struct *q, 237void cgit_diff_tree_cb(struct diff_queue_struct *q,
233 struct diff_options *options, void *data) 238 struct diff_options *options, void *data)
234{ 239{
235 int i; 240 int i;
236 241
237 for (i = 0; i < q->nr; i++) { 242 for (i = 0; i < q->nr; i++) {
238 if (q->queue[i]->status == 'U') 243 if (q->queue[i]->status == 'U')
239 continue; 244 continue;
240 ((filepair_fn)data)(q->queue[i]); 245 ((filepair_fn)data)(q->queue[i]);
241 } 246 }
242} 247}
243 248
244static int load_mmfile(mmfile_t *file, const unsigned char *sha1) 249static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
245{ 250{
246 enum object_type type; 251 enum object_type type;
247 252
248 if (is_null_sha1(sha1)) { 253 if (is_null_sha1(sha1)) {
249 file->ptr = (char *)""; 254 file->ptr = (char *)"";
250 file->size = 0; 255 file->size = 0;
251 } else { 256 } else {
252 file->ptr = read_sha1_file(sha1, &type, &file->size); 257 file->ptr = read_sha1_file(sha1, &type, &file->size);
253 } 258 }
254 return 1; 259 return 1;
255} 260}
256 261
257/* 262/*
258 * Receive diff-buffers from xdiff and concatenate them as 263 * Receive diff-buffers from xdiff and concatenate them as
259 * needed across multiple callbacks. 264 * needed across multiple callbacks.
260 * 265 *
261 * This is basically a copy of xdiff-interface.c/xdiff_outf(), 266 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
262 * ripped from git and modified to use globals instead of 267 * ripped from git and modified to use globals instead of
263 * a special callback-struct. 268 * a special callback-struct.
264 */ 269 */
265char *diffbuf = NULL; 270char *diffbuf = NULL;
266int buflen = 0; 271int buflen = 0;
267 272
268int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) 273int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
269{ 274{
270 int i; 275 int i;
271 276
272 for (i = 0; i < nbuf; i++) { 277 for (i = 0; i < nbuf; i++) {
273 if (mb[i].ptr[mb[i].size-1] != '\n') { 278 if (mb[i].ptr[mb[i].size-1] != '\n') {
274 /* Incomplete line */ 279 /* Incomplete line */
275 diffbuf = xrealloc(diffbuf, buflen + mb[i].size); 280 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
276 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); 281 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
277 buflen += mb[i].size; 282 buflen += mb[i].size;
278 continue; 283 continue;
279 } 284 }
280 285
281 /* we have a complete line */ 286 /* we have a complete line */
282 if (!diffbuf) { 287 if (!diffbuf) {
283 ((linediff_fn)priv)(mb[i].ptr, mb[i].size); 288 ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
284 continue; 289 continue;
285 } 290 }
286 diffbuf = xrealloc(diffbuf, buflen + mb[i].size); 291 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
287 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); 292 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
288 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size); 293 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
289 free(diffbuf); 294 free(diffbuf);
290 diffbuf = NULL; 295 diffbuf = NULL;
291 buflen = 0; 296 buflen = 0;
292 } 297 }
293 if (diffbuf) { 298 if (diffbuf) {
294 ((linediff_fn)priv)(diffbuf, buflen); 299 ((linediff_fn)priv)(diffbuf, buflen);
295 free(diffbuf); 300 free(diffbuf);
296 diffbuf = NULL; 301 diffbuf = NULL;
297 buflen = 0; 302 buflen = 0;
298 } 303 }
299 return 0; 304 return 0;
300} 305}
301 306
302int cgit_diff_files(const unsigned char *old_sha1, 307int cgit_diff_files(const unsigned char *old_sha1,
303 const unsigned char *new_sha1, 308 const unsigned char *new_sha1,
304 linediff_fn fn) 309 linediff_fn fn)
305{ 310{
306 mmfile_t file1, file2; 311 mmfile_t file1, file2;
307 xpparam_t diff_params; 312 xpparam_t diff_params;
308 xdemitconf_t emit_params; 313 xdemitconf_t emit_params;
309 xdemitcb_t emit_cb; 314 xdemitcb_t emit_cb;
310 315
311 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1)) 316 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))
312 return 1; 317 return 1;
313 318
314 diff_params.flags = XDF_NEED_MINIMAL; 319 diff_params.flags = XDF_NEED_MINIMAL;
315 emit_params.ctxlen = 3; 320 emit_params.ctxlen = 3;
316 emit_params.flags = XDL_EMIT_FUNCNAMES; 321 emit_params.flags = XDL_EMIT_FUNCNAMES;
317 emit_cb.outf = filediff_cb; 322 emit_cb.outf = filediff_cb;
318 emit_cb.priv = fn; 323 emit_cb.priv = fn;
319 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb); 324 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
320 return 0; 325 return 0;
321} 326}
322 327
323void cgit_diff_tree(const unsigned char *old_sha1, 328void cgit_diff_tree(const unsigned char *old_sha1,
324 const unsigned char *new_sha1, 329 const unsigned char *new_sha1,
325 filepair_fn fn) 330 filepair_fn fn)
326{ 331{
327 struct diff_options opt; 332 struct diff_options opt;
328 int ret; 333 int ret;
329 334
330 diff_setup(&opt); 335 diff_setup(&opt);
331 opt.output_format = DIFF_FORMAT_CALLBACK; 336 opt.output_format = DIFF_FORMAT_CALLBACK;
332 opt.detect_rename = 1; 337 opt.detect_rename = 1;
333 opt.recursive = 1; 338 opt.recursive = 1;
334 opt.format_callback = cgit_diff_tree_cb; 339 opt.format_callback = cgit_diff_tree_cb;
335 opt.format_callback_data = fn; 340 opt.format_callback_data = fn;
336 diff_setup_done(&opt); 341 diff_setup_done(&opt);
337 342
338 if (old_sha1) 343 if (old_sha1)
339 ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt); 344 ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt);
340 else 345 else
341 ret = diff_root_tree_sha1(new_sha1, "", &opt); 346 ret = diff_root_tree_sha1(new_sha1, "", &opt);
342 diffcore_std(&opt); 347 diffcore_std(&opt);
343 diff_flush(&opt); 348 diff_flush(&opt);
344} 349}
345 350
346void cgit_diff_commit(struct commit *commit, filepair_fn fn) 351void cgit_diff_commit(struct commit *commit, filepair_fn fn)
347{ 352{
348 unsigned char *old_sha1 = NULL; 353 unsigned char *old_sha1 = NULL;
349 354
350 if (commit->parents) 355 if (commit->parents)
351 old_sha1 = commit->parents->item->object.sha1; 356 old_sha1 = commit->parents->item->object.sha1;
352 cgit_diff_tree(old_sha1, commit->object.sha1, fn); 357 cgit_diff_tree(old_sha1, commit->object.sha1, fn);
353} 358}
diff --git a/ui-shared.c b/ui-shared.c
index 6211056..c7fbc5e 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -1,177 +1,180 @@
1/* ui-shared.c: common web output functions 1/* ui-shared.c: common web output functions
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10 10
11const char cgit_doctype[] = 11const char cgit_doctype[] =
12"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" 12"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
13" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; 13" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
14 14
15static char *http_date(time_t t) 15static char *http_date(time_t t)
16{ 16{
17 static char day[][4] = 17 static char day[][4] =
18 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; 18 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
19 static char month[][4] = 19 static char month[][4] =
20 {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 20 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
21 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; 21 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
22 struct tm *tm = gmtime(&t); 22 struct tm *tm = gmtime(&t);
23 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], 23 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
24 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, 24 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
25 tm->tm_hour, tm->tm_min, tm->tm_sec); 25 tm->tm_hour, tm->tm_min, tm->tm_sec);
26} 26}
27 27
28static long ttl_seconds(long ttl) 28static long ttl_seconds(long ttl)
29{ 29{
30 if (ttl<0) 30 if (ttl<0)
31 return 60 * 60 * 24 * 365; 31 return 60 * 60 * 24 * 365;
32 else 32 else
33 return ttl * 60; 33 return ttl * 60;
34} 34}
35 35
36void cgit_print_error(char *msg) 36void cgit_print_error(char *msg)
37{ 37{
38 html("<div class='error'>"); 38 html("<div class='error'>");
39 html_txt(msg); 39 html_txt(msg);
40 html("</div>\n"); 40 html("</div>\n");
41} 41}
42 42
43char *cgit_rooturl() 43char *cgit_rooturl()
44{ 44{
45 if (cgit_virtual_root) 45 if (cgit_virtual_root)
46 return fmt("%s/", cgit_virtual_root); 46 return fmt("%s/", cgit_virtual_root);
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);
96} 99}
97 100
98void cgit_print_docstart(char *title, struct cacheitem *item) 101void cgit_print_docstart(char *title, struct cacheitem *item)
99{ 102{
100 html("Content-Type: text/html; charset=utf-8\n"); 103 html("Content-Type: text/html; charset=utf-8\n");
101 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); 104 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime));
102 htmlf("Expires: %s\n", http_date(item->st.st_mtime + 105 htmlf("Expires: %s\n", http_date(item->st.st_mtime +
103 ttl_seconds(item->ttl))); 106 ttl_seconds(item->ttl)));
104 html("\n"); 107 html("\n");
105 html(cgit_doctype); 108 html(cgit_doctype);
106 html("<html>\n"); 109 html("<html>\n");
107 html("<head>\n"); 110 html("<head>\n");
108 html("<title>"); 111 html("<title>");
109 html_txt(title); 112 html_txt(title);
110 html("</title>\n"); 113 html("</title>\n");
111 htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version); 114 htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version);
112 html("<link rel='stylesheet' type='text/css' href='"); 115 html("<link rel='stylesheet' type='text/css' href='");
113 html_attr(cgit_css); 116 html_attr(cgit_css);
114 html("'/>\n"); 117 html("'/>\n");
115 html("</head>\n"); 118 html("</head>\n");
116 html("<body>\n"); 119 html("<body>\n");
117} 120}
118 121
119void cgit_print_docend() 122void cgit_print_docend()
120{ 123{
121 html("</td></tr></table>"); 124 html("</td></tr></table>");
122 html("</body>\n</html>\n"); 125 html("</body>\n</html>\n");
123} 126}
124 127
125void cgit_print_pageheader(char *title, int show_search) 128void cgit_print_pageheader(char *title, int show_search)
126{ 129{
127 html("<table id='layout'>"); 130 html("<table id='layout'>");
128 html("<tr><td id='header'>"); 131 html("<tr><td id='header'>");
129 html(cgit_root_title); 132 html(cgit_root_title);
130 html("</td><td id='logo'>"); 133 html("</td><td id='logo'>");
131 html("<a href='"); 134 html("<a href='");
132 html_attr(cgit_logo_link); 135 html_attr(cgit_logo_link);
133 htmlf("'><img src='%s'/></a>", cgit_logo); 136 htmlf("'><img src='%s'/></a>", cgit_logo);
134 html("</td></tr>"); 137 html("</td></tr>");
135 html("<tr><td id='crumb'>"); 138 html("<tr><td id='crumb'>");
136 htmlf("<a href='%s'>root</a>", cgit_rooturl()); 139 htmlf("<a href='%s'>root</a>", cgit_rooturl());
137 if (cgit_query_repo) { 140 if (cgit_query_repo) {
138 htmlf(" : <a href='%s'>", cgit_repourl(cgit_repo->url)); 141 htmlf(" : <a href='%s'>", cgit_repourl(cgit_repo->url));
139 html_txt(cgit_repo->name); 142 html_txt(cgit_repo->name);
140 htmlf("</a> : %s", title); 143 htmlf("</a> : %s", title);
141 } 144 }
142 html("</td>"); 145 html("</td>");
143 html("<td id='search'>"); 146 html("<td id='search'>");
144 if (show_search) { 147 if (show_search) {
145 html("<form method='get' href='"); 148 html("<form method='get' href='");
146 html_attr(cgit_currurl()); 149 html_attr(cgit_currurl());
147 html("'>"); 150 html("'>");
148 if (!cgit_virtual_root) { 151 if (!cgit_virtual_root) {
149 if (cgit_query_repo) 152 if (cgit_query_repo)
150 html_hidden("r", cgit_query_repo); 153 html_hidden("r", cgit_query_repo);
151 if (cgit_query_page) 154 if (cgit_query_page)
152 html_hidden("p", cgit_query_page); 155 html_hidden("p", cgit_query_page);
153 } 156 }
154 if (cgit_query_head) 157 if (cgit_query_head)
155 html_hidden("h", cgit_query_head); 158 html_hidden("h", cgit_query_head);
156 if (cgit_query_sha1) 159 if (cgit_query_sha1)
157 html_hidden("id", cgit_query_sha1); 160 html_hidden("id", cgit_query_sha1);
158 if (cgit_query_sha2) 161 if (cgit_query_sha2)
159 html_hidden("id2", cgit_query_sha2); 162 html_hidden("id2", cgit_query_sha2);
160 html("<input type='text' name='q' value='"); 163 html("<input type='text' name='q' value='");
161 html_attr(cgit_query_search); 164 html_attr(cgit_query_search);
162 html("'/></form>"); 165 html("'/></form>");
163 } 166 }
164 html("</td></tr>"); 167 html("</td></tr>");
165 html("<tr><td id='content' colspan='2'>"); 168 html("<tr><td id='content' colspan='2'>");
166} 169}
167 170
168void cgit_print_snapshot_start(const char *mimetype, const char *filename, 171void cgit_print_snapshot_start(const char *mimetype, const char *filename,
169 struct cacheitem *item) 172 struct cacheitem *item)
170{ 173{
171 htmlf("Content-Type: %s\n", mimetype); 174 htmlf("Content-Type: %s\n", mimetype);
172 htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename); 175 htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename);
173 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); 176 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime));
174 htmlf("Expires: %s\n", http_date(item->st.st_mtime + 177 htmlf("Expires: %s\n", http_date(item->st.st_mtime +
175 ttl_seconds(item->ttl))); 178 ttl_seconds(item->ttl)));
176 html("\n"); 179 html("\n");
177} 180}