summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--cache.c6
-rw-r--r--cgit.c18
-rw-r--r--cgit.h3
3 files changed, 16 insertions, 11 deletions
diff --git a/cache.c b/cache.c
index b034c48..0ffa502 100644
--- a/cache.c
+++ b/cache.c
@@ -1,94 +1,98 @@
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
13int cache_lookup(struct cacheitem *item) 13void cache_prepare(struct cacheitem *item)
14{ 14{
15 if (!cgit_query_repo) { 15 if (!cgit_query_repo) {
16 item->name = xstrdup(fmt("%s/index.html", cgit_cache_root)); 16 item->name = xstrdup(fmt("%s/index.html", cgit_cache_root));
17 item->ttl = cgit_cache_root_ttl; 17 item->ttl = cgit_cache_root_ttl;
18 } else if (!cgit_query_page) { 18 } else if (!cgit_query_page) {
19 item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root, 19 item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root,
20 cgit_query_repo)); 20 cgit_query_repo));
21 item->ttl = cgit_cache_repo_ttl; 21 item->ttl = cgit_cache_repo_ttl;
22 } else { 22 } else {
23 item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, 23 item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root,
24 cgit_query_repo, cgit_query_page, 24 cgit_query_repo, cgit_query_page,
25 cgit_querystring)); 25 cgit_querystring));
26 if (cgit_query_has_symref) 26 if (cgit_query_has_symref)
27 item->ttl = cgit_cache_dynamic_ttl; 27 item->ttl = cgit_cache_dynamic_ttl;
28 else if (cgit_query_has_sha1) 28 else if (cgit_query_has_sha1)
29 item->ttl = cgit_cache_static_ttl; 29 item->ttl = cgit_cache_static_ttl;
30 else 30 else
31 item->ttl = cgit_cache_repo_ttl; 31 item->ttl = cgit_cache_repo_ttl;
32 } 32 }
33}
34
35int cache_exist(struct cacheitem *item)
36{
33 if (stat(item->name, &item->st)) { 37 if (stat(item->name, &item->st)) {
34 item->st.st_mtime = 0; 38 item->st.st_mtime = 0;
35 return 0; 39 return 0;
36 } 40 }
37 return 1; 41 return 1;
38} 42}
39 43
40int cache_create_dirs() 44int cache_create_dirs()
41{ 45{
42 char *path; 46 char *path;
43 47
44 if (!cgit_query_repo) 48 if (!cgit_query_repo)
45 return 0; 49 return 0;
46 50
47 path = fmt("%s/%s", cgit_cache_root, cgit_query_repo); 51 path = fmt("%s/%s", cgit_cache_root, cgit_query_repo);
48 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 52 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
49 return 0; 53 return 0;
50 54
51 if (cgit_query_page) { 55 if (cgit_query_page) {
52 path = fmt("%s/%s/%s", cgit_cache_root, cgit_query_repo, 56 path = fmt("%s/%s/%s", cgit_cache_root, cgit_query_repo,
53 cgit_query_page); 57 cgit_query_page);
54 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 58 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
55 return 0; 59 return 0;
56 } 60 }
57 return 1; 61 return 1;
58} 62}
59 63
60int cache_lock(struct cacheitem *item) 64int cache_lock(struct cacheitem *item)
61{ 65{
62 int ret; 66 int ret;
63 char *lockfile = fmt("%s.lock", item->name); 67 char *lockfile = fmt("%s.lock", item->name);
64 68
65 top: 69 top:
66 item->fd = open(lockfile, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR|S_IWUSR); 70 item->fd = open(lockfile, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR|S_IWUSR);
67 if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) 71 if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs())
68 goto top; 72 goto top;
69 if (item->fd == NOLOCK && errno == EEXIST) { 73 if (item->fd == NOLOCK && errno == EEXIST) {
70 struct stat st; 74 struct stat st;
71 time_t t; 75 time_t t;
72 if (stat(lockfile, &st)) 76 if (stat(lockfile, &st))
73 return ret; 77 return ret;
74 t = time(NULL); 78 t = time(NULL);
75 if (t-st.st_mtime > cgit_cache_max_create_time && 79 if (t-st.st_mtime > cgit_cache_max_create_time &&
76 !unlink(lockfile)) 80 !unlink(lockfile))
77 goto top; 81 goto top;
78 return 0; 82 return 0;
79 } 83 }
80 return (item->fd > 0); 84 return (item->fd > 0);
81} 85}
82 86
83int cache_unlock(struct cacheitem *item) 87int cache_unlock(struct cacheitem *item)
84{ 88{
85 close(item->fd); 89 close(item->fd);
86 return (rename(fmt("%s.lock", item->name), item->name) == 0); 90 return (rename(fmt("%s.lock", item->name), item->name) == 0);
87} 91}
88 92
89int cache_expired(struct cacheitem *item) 93int cache_expired(struct cacheitem *item)
90{ 94{
91 if (item->ttl < 0) 95 if (item->ttl < 0)
92 return 0; 96 return 0;
93 return item->st.st_mtime + item->ttl * 60 < time(NULL); 97 return item->st.st_mtime + item->ttl * 60 < time(NULL);
94} 98}
diff --git a/cgit.c b/cgit.c
index 110face..7f14016 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1,508 +1,508 @@
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
13const char cgit_doctype[] = 13const char cgit_doctype[] =
14"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" 14"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
15" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; 15" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
16 16
17const char cgit_error[] = 17const char cgit_error[] =
18"<div class='error'>%s</div>"; 18"<div class='error'>%s</div>";
19 19
20const char cgit_lib_error[] = 20const char cgit_lib_error[] =
21"<div class='error'>%s: %s</div>"; 21"<div class='error'>%s: %s</div>";
22 22
23int htmlfd = 0; 23int htmlfd = 0;
24 24
25char *cgit_root = "/usr/src/git"; 25char *cgit_root = "/usr/src/git";
26char *cgit_root_title = "Git repository browser"; 26char *cgit_root_title = "Git repository browser";
27char *cgit_css = "/cgit.css"; 27char *cgit_css = "/cgit.css";
28char *cgit_logo = "/git-logo.png"; 28char *cgit_logo = "/git-logo.png";
29char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; 29char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/";
30char *cgit_virtual_root = NULL; 30char *cgit_virtual_root = NULL;
31 31
32char *cgit_cache_root = "/var/cache/cgit"; 32char *cgit_cache_root = "/var/cache/cgit";
33 33
34int cgit_cache_root_ttl = 5; 34int cgit_cache_root_ttl = 5;
35int cgit_cache_repo_ttl = 5; 35int cgit_cache_repo_ttl = 5;
36int cgit_cache_dynamic_ttl = 5; 36int cgit_cache_dynamic_ttl = 5;
37int cgit_cache_static_ttl = -1; 37int cgit_cache_static_ttl = -1;
38int cgit_cache_max_create_time = 5; 38int cgit_cache_max_create_time = 5;
39 39
40char *cgit_repo_name = NULL; 40char *cgit_repo_name = NULL;
41char *cgit_repo_desc = NULL; 41char *cgit_repo_desc = NULL;
42char *cgit_repo_owner = NULL; 42char *cgit_repo_owner = NULL;
43 43
44int cgit_query_has_symref = 0; 44int cgit_query_has_symref = 0;
45int cgit_query_has_sha1 = 0; 45int cgit_query_has_sha1 = 0;
46 46
47char *cgit_querystring = NULL; 47char *cgit_querystring = NULL;
48char *cgit_query_repo = NULL; 48char *cgit_query_repo = NULL;
49char *cgit_query_page = NULL; 49char *cgit_query_page = NULL;
50char *cgit_query_head = NULL; 50char *cgit_query_head = NULL;
51char *cgit_query_sha1 = NULL; 51char *cgit_query_sha1 = NULL;
52 52
53struct cacheitem cacheitem; 53struct cacheitem cacheitem;
54 54
55int cgit_parse_query(char *txt, configfn fn) 55int cgit_parse_query(char *txt, configfn fn)
56{ 56{
57 char *t, *value = NULL, c; 57 char *t, *value = NULL, c;
58 58
59 if (!txt) 59 if (!txt)
60 return 0; 60 return 0;
61 61
62 t = txt = xstrdup(txt); 62 t = txt = xstrdup(txt);
63 63
64 while((c=*t) != '\0') { 64 while((c=*t) != '\0') {
65 if (c=='=') { 65 if (c=='=') {
66 *t = '\0'; 66 *t = '\0';
67 value = t+1; 67 value = t+1;
68 } else if (c=='&') { 68 } else if (c=='&') {
69 *t = '\0'; 69 *t = '\0';
70 (*fn)(txt, value); 70 (*fn)(txt, value);
71 txt = t+1; 71 txt = t+1;
72 value = NULL; 72 value = NULL;
73 } 73 }
74 t++; 74 t++;
75 } 75 }
76 if (t!=txt) 76 if (t!=txt)
77 (*fn)(txt, value); 77 (*fn)(txt, value);
78 return 0; 78 return 0;
79} 79}
80 80
81void cgit_global_config_cb(const char *name, const char *value) 81void cgit_global_config_cb(const char *name, const char *value)
82{ 82{
83 if (!strcmp(name, "root")) 83 if (!strcmp(name, "root"))
84 cgit_root = xstrdup(value); 84 cgit_root = xstrdup(value);
85 else if (!strcmp(name, "root-title")) 85 else if (!strcmp(name, "root-title"))
86 cgit_root_title = xstrdup(value); 86 cgit_root_title = xstrdup(value);
87 else if (!strcmp(name, "css")) 87 else if (!strcmp(name, "css"))
88 cgit_css = xstrdup(value); 88 cgit_css = xstrdup(value);
89 else if (!strcmp(name, "logo")) 89 else if (!strcmp(name, "logo"))
90 cgit_logo = xstrdup(value); 90 cgit_logo = xstrdup(value);
91 else if (!strcmp(name, "logo-link")) 91 else if (!strcmp(name, "logo-link"))
92 cgit_logo_link = xstrdup(value); 92 cgit_logo_link = xstrdup(value);
93 else if (!strcmp(name, "virtual-root")) 93 else if (!strcmp(name, "virtual-root"))
94 cgit_virtual_root = xstrdup(value); 94 cgit_virtual_root = xstrdup(value);
95} 95}
96 96
97void cgit_repo_config_cb(const char *name, const char *value) 97void cgit_repo_config_cb(const char *name, const char *value)
98{ 98{
99 if (!strcmp(name, "name")) 99 if (!strcmp(name, "name"))
100 cgit_repo_name = xstrdup(value); 100 cgit_repo_name = xstrdup(value);
101 else if (!strcmp(name, "desc")) 101 else if (!strcmp(name, "desc"))
102 cgit_repo_desc = xstrdup(value); 102 cgit_repo_desc = xstrdup(value);
103 else if (!strcmp(name, "owner")) 103 else if (!strcmp(name, "owner"))
104 cgit_repo_owner = xstrdup(value); 104 cgit_repo_owner = xstrdup(value);
105} 105}
106 106
107void cgit_querystring_cb(const char *name, const char *value) 107void cgit_querystring_cb(const char *name, const char *value)
108{ 108{
109 if (!strcmp(name,"r")) 109 if (!strcmp(name,"r"))
110 cgit_query_repo = xstrdup(value); 110 cgit_query_repo = xstrdup(value);
111 else if (!strcmp(name, "p")) 111 else if (!strcmp(name, "p"))
112 cgit_query_page = xstrdup(value); 112 cgit_query_page = xstrdup(value);
113 else if (!strcmp(name, "h")) { 113 else if (!strcmp(name, "h")) {
114 cgit_query_head = xstrdup(value); 114 cgit_query_head = xstrdup(value);
115 cgit_query_has_symref = 1; 115 cgit_query_has_symref = 1;
116 } else if (!strcmp(name, "id")) { 116 } else if (!strcmp(name, "id")) {
117 cgit_query_sha1 = xstrdup(value); 117 cgit_query_sha1 = xstrdup(value);
118 cgit_query_has_sha1 = 1; 118 cgit_query_has_sha1 = 1;
119 } 119 }
120} 120}
121 121
122char *cgit_repourl(const char *reponame) 122char *cgit_repourl(const char *reponame)
123{ 123{
124 if (cgit_virtual_root) { 124 if (cgit_virtual_root) {
125 return fmt("%s/%s/", cgit_virtual_root, reponame); 125 return fmt("%s/%s/", cgit_virtual_root, reponame);
126 } else { 126 } else {
127 return fmt("?r=%s", reponame); 127 return fmt("?r=%s", reponame);
128 } 128 }
129} 129}
130 130
131char *cgit_pageurl(const char *reponame, const char *pagename, 131char *cgit_pageurl(const char *reponame, const char *pagename,
132 const char *query) 132 const char *query)
133{ 133{
134 if (cgit_virtual_root) { 134 if (cgit_virtual_root) {
135 return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame, 135 return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame,
136 pagename, query); 136 pagename, query);
137 } else { 137 } else {
138 return fmt("?r=%s&p=%s&%s", reponame, pagename, query); 138 return fmt("?r=%s&p=%s&%s", reponame, pagename, query);
139 } 139 }
140} 140}
141 141
142static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1, 142static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1,
143 int flags, void *cb_data) 143 int flags, void *cb_data)
144{ 144{
145 struct commit *commit; 145 struct commit *commit;
146 char buf[256], *url; 146 char buf[256], *url;
147 147
148 commit = lookup_commit(sha1); 148 commit = lookup_commit(sha1);
149 if (commit && !parse_commit(commit)){ 149 if (commit && !parse_commit(commit)){
150 html("<tr><td>"); 150 html("<tr><td>");
151 url = cgit_pageurl(cgit_query_repo, "log", 151 url = cgit_pageurl(cgit_query_repo, "log",
152 fmt("h=%s", refname)); 152 fmt("h=%s", refname));
153 html_link_open(url, NULL, NULL); 153 html_link_open(url, NULL, NULL);
154 strncpy(buf, refname, sizeof(buf)); 154 strncpy(buf, refname, sizeof(buf));
155 html_txt(buf); 155 html_txt(buf);
156 html_link_close(); 156 html_link_close();
157 html("</td><td>"); 157 html("</td><td>");
158 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, buf, 158 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, buf,
159 sizeof(buf), 0, NULL, NULL, 0); 159 sizeof(buf), 0, NULL, NULL, 0);
160 html_txt(buf); 160 html_txt(buf);
161 html("</td></tr>\n"); 161 html("</td></tr>\n");
162 } else { 162 } else {
163 html("<tr><td>"); 163 html("<tr><td>");
164 html_txt(buf); 164 html_txt(buf);
165 html("</td><td>"); 165 html("</td><td>");
166 htmlf("*** bad ref %s", sha1_to_hex(sha1)); 166 htmlf("*** bad ref %s", sha1_to_hex(sha1));
167 html("</td></tr>\n"); 167 html("</td></tr>\n");
168 } 168 }
169 return 0; 169 return 0;
170} 170}
171 171
172/* Sun, 06 Nov 1994 08:49:37 GMT */ 172/* Sun, 06 Nov 1994 08:49:37 GMT */
173static char *http_date(time_t t) 173static char *http_date(time_t t)
174{ 174{
175 static char day[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; 175 static char day[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
176 static char month[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 176 static char month[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
177 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; 177 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
178 struct tm *tm = gmtime(&t); 178 struct tm *tm = gmtime(&t);
179 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], 179 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
180 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, 180 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
181 tm->tm_hour, tm->tm_min, tm->tm_sec); 181 tm->tm_hour, tm->tm_min, tm->tm_sec);
182} 182}
183 183
184static int ttl_seconds(int ttl) 184static int ttl_seconds(int ttl)
185{ 185{
186 if (ttl<0) 186 if (ttl<0)
187 return 60 * 60 * 24 * 365; 187 return 60 * 60 * 24 * 365;
188 else 188 else
189 return ttl * 60; 189 return ttl * 60;
190} 190}
191 191
192static void cgit_print_docstart(char *title) 192static void cgit_print_docstart(char *title)
193{ 193{
194 html("Content-Type: text/html; charset=utf-8\n"); 194 html("Content-Type: text/html; charset=utf-8\n");
195 htmlf("Last-Modified: %s\n", http_date(cacheitem.st.st_mtime)); 195 htmlf("Last-Modified: %s\n", http_date(cacheitem.st.st_mtime));
196 htmlf("Expires: %s\n", http_date(cacheitem.st.st_mtime + 196 htmlf("Expires: %s\n", http_date(cacheitem.st.st_mtime +
197 ttl_seconds(cacheitem.ttl))); 197 ttl_seconds(cacheitem.ttl)));
198 html("\n"); 198 html("\n");
199 html(cgit_doctype); 199 html(cgit_doctype);
200 html("<html>\n"); 200 html("<html>\n");
201 html("<head>\n"); 201 html("<head>\n");
202 html("<title>"); 202 html("<title>");
203 html_txt(title); 203 html_txt(title);
204 html("</title>\n"); 204 html("</title>\n");
205 htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version); 205 htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version);
206 html("<link rel='stylesheet' type='text/css' href='"); 206 html("<link rel='stylesheet' type='text/css' href='");
207 html_attr(cgit_css); 207 html_attr(cgit_css);
208 html("'/>\n"); 208 html("'/>\n");
209 html("</head>\n"); 209 html("</head>\n");
210 html("<body>\n"); 210 html("<body>\n");
211} 211}
212 212
213static void cgit_print_docend() 213static void cgit_print_docend()
214{ 214{
215 html("</body>\n</html>\n"); 215 html("</body>\n</html>\n");
216} 216}
217 217
218static void cgit_print_pageheader(char *title) 218static void cgit_print_pageheader(char *title)
219{ 219{
220 html("<div id='header'>"); 220 html("<div id='header'>");
221 htmlf("<a href='%s'>", cgit_logo_link); 221 htmlf("<a href='%s'>", cgit_logo_link);
222 htmlf("<img id='logo' src='%s'/>\n", cgit_logo); 222 htmlf("<img id='logo' src='%s'/>\n", cgit_logo);
223 htmlf("</a>"); 223 htmlf("</a>");
224 html_txt(title); 224 html_txt(title);
225 html("</div>"); 225 html("</div>");
226} 226}
227 227
228static void cgit_print_repolist() 228static void cgit_print_repolist()
229{ 229{
230 DIR *d; 230 DIR *d;
231 struct dirent *de; 231 struct dirent *de;
232 struct stat st; 232 struct stat st;
233 char *name; 233 char *name;
234 234
235 chdir(cgit_root); 235 chdir(cgit_root);
236 cgit_print_docstart(cgit_root_title); 236 cgit_print_docstart(cgit_root_title);
237 cgit_print_pageheader(cgit_root_title); 237 cgit_print_pageheader(cgit_root_title);
238 238
239 if (!(d = opendir("."))) { 239 if (!(d = opendir("."))) {
240 htmlf(cgit_lib_error, "Unable to scan repository directory", 240 htmlf(cgit_lib_error, "Unable to scan repository directory",
241 strerror(errno)); 241 strerror(errno));
242 cgit_print_docend(); 242 cgit_print_docend();
243 return; 243 return;
244 } 244 }
245 245
246 html("<h2>Repositories</h2>\n"); 246 html("<h2>Repositories</h2>\n");
247 html("<table class='list'>"); 247 html("<table class='list'>");
248 html("<tr><th>Name</th><th>Description</th><th>Owner</th></tr>\n"); 248 html("<tr><th>Name</th><th>Description</th><th>Owner</th></tr>\n");
249 while ((de = readdir(d)) != NULL) { 249 while ((de = readdir(d)) != NULL) {
250 if (de->d_name[0] == '.') 250 if (de->d_name[0] == '.')
251 continue; 251 continue;
252 if (stat(de->d_name, &st) < 0) 252 if (stat(de->d_name, &st) < 0)
253 continue; 253 continue;
254 if (!S_ISDIR(st.st_mode)) 254 if (!S_ISDIR(st.st_mode))
255 continue; 255 continue;
256 256
257 cgit_repo_name = cgit_repo_desc = cgit_repo_owner = NULL; 257 cgit_repo_name = cgit_repo_desc = cgit_repo_owner = NULL;
258 name = fmt("%s/info/cgit", de->d_name); 258 name = fmt("%s/info/cgit", de->d_name);
259 if (cgit_read_config(name, cgit_repo_config_cb)) 259 if (cgit_read_config(name, cgit_repo_config_cb))
260 continue; 260 continue;
261 261
262 html("<tr><td>"); 262 html("<tr><td>");
263 html_link_open(cgit_repourl(de->d_name), NULL, NULL); 263 html_link_open(cgit_repourl(de->d_name), NULL, NULL);
264 html_txt(cgit_repo_name); 264 html_txt(cgit_repo_name);
265 html_link_close(); 265 html_link_close();
266 html("</td><td>"); 266 html("</td><td>");
267 html_txt(cgit_repo_desc); 267 html_txt(cgit_repo_desc);
268 html("</td><td>"); 268 html("</td><td>");
269 html_txt(cgit_repo_owner); 269 html_txt(cgit_repo_owner);
270 html("</td></tr>\n"); 270 html("</td></tr>\n");
271 } 271 }
272 closedir(d); 272 closedir(d);
273 html("</table>"); 273 html("</table>");
274 cgit_print_docend(); 274 cgit_print_docend();
275} 275}
276 276
277static void cgit_print_branches() 277static void cgit_print_branches()
278{ 278{
279 html("<table class='list'>"); 279 html("<table class='list'>");
280 html("<tr><th>Branch name</th><th>Head commit</th></tr>\n"); 280 html("<tr><th>Branch name</th><th>Head commit</th></tr>\n");
281 for_each_branch_ref(cgit_print_branch_cb, NULL); 281 for_each_branch_ref(cgit_print_branch_cb, NULL);
282 html("</table>"); 282 html("</table>");
283} 283}
284 284
285static int get_one_line(char *txt) 285static int get_one_line(char *txt)
286{ 286{
287 char *t; 287 char *t;
288 288
289 for(t=txt; *t != '\n' && t != '\0'; t++) 289 for(t=txt; *t != '\n' && t != '\0'; t++)
290 ; 290 ;
291 *t = '\0'; 291 *t = '\0';
292 return t-txt-1; 292 return t-txt-1;
293} 293}
294 294
295static void cgit_print_commit_shortlog(struct commit *commit) 295static void cgit_print_commit_shortlog(struct commit *commit)
296{ 296{
297 char *h, *t, *p; 297 char *h, *t, *p;
298 char *tree = NULL, *author = NULL, *subject = NULL; 298 char *tree = NULL, *author = NULL, *subject = NULL;
299 int len; 299 int len;
300 time_t sec; 300 time_t sec;
301 struct tm *time; 301 struct tm *time;
302 char buf[32]; 302 char buf[32];
303 303
304 h = t = commit->buffer; 304 h = t = commit->buffer;
305 305
306 if (strncmp(h, "tree ", 5)) 306 if (strncmp(h, "tree ", 5))
307 die("Bad commit format: %s", 307 die("Bad commit format: %s",
308 sha1_to_hex(commit->object.sha1)); 308 sha1_to_hex(commit->object.sha1));
309 309
310 len = get_one_line(h); 310 len = get_one_line(h);
311 tree = h+5; 311 tree = h+5;
312 h += len + 2; 312 h += len + 2;
313 313
314 while (!strncmp(h, "parent ", 7)) 314 while (!strncmp(h, "parent ", 7))
315 h += get_one_line(h) + 2; 315 h += get_one_line(h) + 2;
316 316
317 if (!strncmp(h, "author ", 7)) { 317 if (!strncmp(h, "author ", 7)) {
318 author = h+7; 318 author = h+7;
319 h += get_one_line(h) + 2; 319 h += get_one_line(h) + 2;
320 t = author; 320 t = author;
321 while(t!=h && *t!='<') 321 while(t!=h && *t!='<')
322 t++; 322 t++;
323 *t='\0'; 323 *t='\0';
324 p = t; 324 p = t;
325 while(--t!=author && *t==' ') 325 while(--t!=author && *t==' ')
326 *t='\0'; 326 *t='\0';
327 while(++p!=h && *p!='>') 327 while(++p!=h && *p!='>')
328 ; 328 ;
329 while(++p!=h && !isdigit(*p)) 329 while(++p!=h && !isdigit(*p))
330 ; 330 ;
331 331
332 t = p; 332 t = p;
333 while(++p && isdigit(*p)) 333 while(++p && isdigit(*p))
334 ; 334 ;
335 *p = '\0'; 335 *p = '\0';
336 sec = atoi(t); 336 sec = atoi(t);
337 time = gmtime(&sec); 337 time = gmtime(&sec);
338 } 338 }
339 339
340 while((len = get_one_line(h)) > 0) 340 while((len = get_one_line(h)) > 0)
341 h += len+2; 341 h += len+2;
342 342
343 h++; 343 h++;
344 len = get_one_line(h); 344 len = get_one_line(h);
345 345
346 subject = h; 346 subject = h;
347 347
348 html("<tr><td>"); 348 html("<tr><td>");
349 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time); 349 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time);
350 html_txt(buf); 350 html_txt(buf);
351 html("</td><td>"); 351 html("</td><td>");
352 char *qry = fmt("id=%s", sha1_to_hex(commit->object.sha1)); 352 char *qry = fmt("id=%s", sha1_to_hex(commit->object.sha1));
353 char *url = cgit_pageurl(cgit_query_repo, "view", qry); 353 char *url = cgit_pageurl(cgit_query_repo, "view", qry);
354 html_link_open(url, NULL, NULL); 354 html_link_open(url, NULL, NULL);
355 html_txt(subject); 355 html_txt(subject);
356 html_link_close(); 356 html_link_close();
357 html("</td><td>"); 357 html("</td><td>");
358 html_txt(author); 358 html_txt(author);
359 html("</td></tr>\n"); 359 html("</td></tr>\n");
360} 360}
361 361
362static void cgit_print_log(const char *tip, int ofs, int cnt) 362static void cgit_print_log(const char *tip, int ofs, int cnt)
363{ 363{
364 struct rev_info rev; 364 struct rev_info rev;
365 struct commit *commit; 365 struct commit *commit;
366 const char *argv[2] = {NULL, tip}; 366 const char *argv[2] = {NULL, tip};
367 int n = 0; 367 int n = 0;
368 368
369 init_revisions(&rev, NULL); 369 init_revisions(&rev, NULL);
370 rev.abbrev = DEFAULT_ABBREV; 370 rev.abbrev = DEFAULT_ABBREV;
371 rev.commit_format = CMIT_FMT_DEFAULT; 371 rev.commit_format = CMIT_FMT_DEFAULT;
372 rev.verbose_header = 1; 372 rev.verbose_header = 1;
373 rev.show_root_diff = 0; 373 rev.show_root_diff = 0;
374 setup_revisions(2, argv, &rev, NULL); 374 setup_revisions(2, argv, &rev, NULL);
375 prepare_revision_walk(&rev); 375 prepare_revision_walk(&rev);
376 376
377 html("<h2>Log</h2>"); 377 html("<h2>Log</h2>");
378 html("<table class='list'>"); 378 html("<table class='list'>");
379 html("<tr><th>Date</th><th>Message</th><th>Author</th></tr>\n"); 379 html("<tr><th>Date</th><th>Message</th><th>Author</th></tr>\n");
380 while ((commit = get_revision(&rev)) != NULL && n++ < 100) { 380 while ((commit = get_revision(&rev)) != NULL && n++ < 100) {
381 cgit_print_commit_shortlog(commit); 381 cgit_print_commit_shortlog(commit);
382 free(commit->buffer); 382 free(commit->buffer);
383 commit->buffer = NULL; 383 commit->buffer = NULL;
384 free_commit_list(commit->parents); 384 free_commit_list(commit->parents);
385 commit->parents = NULL; 385 commit->parents = NULL;
386 } 386 }
387 html("</table>\n"); 387 html("</table>\n");
388} 388}
389 389
390static void cgit_print_repo_summary() 390static void cgit_print_repo_summary()
391{ 391{
392 html("<h2>"); 392 html("<h2>");
393 html_txt("Repo summary page"); 393 html_txt("Repo summary page");
394 html("</h2>"); 394 html("</h2>");
395 cgit_print_branches(); 395 cgit_print_branches();
396} 396}
397 397
398static void cgit_print_object(char *hex) 398static void cgit_print_object(char *hex)
399{ 399{
400 unsigned char sha1[20]; 400 unsigned char sha1[20];
401 //struct object *object; 401 //struct object *object;
402 char type[20]; 402 char type[20];
403 unsigned char *buf; 403 unsigned char *buf;
404 unsigned long size; 404 unsigned long size;
405 405
406 if (get_sha1_hex(hex, sha1)){ 406 if (get_sha1_hex(hex, sha1)){
407 htmlf(cgit_error, "Bad hex value"); 407 htmlf(cgit_error, "Bad hex value");
408 return; 408 return;
409 } 409 }
410 410
411 if (sha1_object_info(sha1, type, NULL)){ 411 if (sha1_object_info(sha1, type, NULL)){
412 htmlf(cgit_error, "Bad object name"); 412 htmlf(cgit_error, "Bad object name");
413 return; 413 return;
414 } 414 }
415 415
416 buf = read_sha1_file(sha1, type, &size); 416 buf = read_sha1_file(sha1, type, &size);
417 if (!buf) { 417 if (!buf) {
418 htmlf(cgit_error, "Error reading object"); 418 htmlf(cgit_error, "Error reading object");
419 return; 419 return;
420 } 420 }
421 421
422 buf[size] = '\0'; 422 buf[size] = '\0';
423 html("<h2>Object view</h2>"); 423 html("<h2>Object view</h2>");
424 htmlf("sha1=%s<br/>type=%s<br/>size=%i<br/>", hex, type, size); 424 htmlf("sha1=%s<br/>type=%s<br/>size=%i<br/>", hex, type, size);
425 html("<pre>"); 425 html("<pre>");
426 html_txt(buf); 426 html_txt(buf);
427 html("</pre>"); 427 html("</pre>");
428} 428}
429 429
430static void cgit_print_repo_page() 430static void cgit_print_repo_page()
431{ 431{
432 if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) || 432 if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) ||
433 cgit_read_config("info/cgit", cgit_repo_config_cb)) { 433 cgit_read_config("info/cgit", cgit_repo_config_cb)) {
434 char *title = fmt("%s - %s", cgit_root_title, "Bad request"); 434 char *title = fmt("%s - %s", cgit_root_title, "Bad request");
435 cgit_print_docstart(title); 435 cgit_print_docstart(title);
436 cgit_print_pageheader(title); 436 cgit_print_pageheader(title);
437 htmlf(cgit_lib_error, "Unable to scan repository", 437 htmlf(cgit_lib_error, "Unable to scan repository",
438 strerror(errno)); 438 strerror(errno));
439 cgit_print_docend(); 439 cgit_print_docend();
440 return; 440 return;
441 } 441 }
442 setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1); 442 setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1);
443 char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc); 443 char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc);
444 cgit_print_docstart(title); 444 cgit_print_docstart(title);
445 cgit_print_pageheader(title); 445 cgit_print_pageheader(title);
446 if (!cgit_query_page) 446 if (!cgit_query_page)
447 cgit_print_repo_summary(); 447 cgit_print_repo_summary();
448 else if (!strcmp(cgit_query_page, "log")) { 448 else if (!strcmp(cgit_query_page, "log")) {
449 cgit_print_log(cgit_query_head, 0, 100); 449 cgit_print_log(cgit_query_head, 0, 100);
450 } else if (!strcmp(cgit_query_page, "view")) { 450 } else if (!strcmp(cgit_query_page, "view")) {
451 cgit_print_object(cgit_query_sha1); 451 cgit_print_object(cgit_query_sha1);
452 } 452 }
453 cgit_print_docend(); 453 cgit_print_docend();
454} 454}
455 455
456static void cgit_fill_cache(struct cacheitem *item) 456static void cgit_fill_cache(struct cacheitem *item)
457{ 457{
458 htmlfd = item->fd; 458 htmlfd = item->fd;
459 item->st.st_mtime = time(NULL); 459 item->st.st_mtime = time(NULL);
460 if (cgit_query_repo) 460 if (cgit_query_repo)
461 cgit_print_repo_page(); 461 cgit_print_repo_page();
462 else 462 else
463 cgit_print_repolist(); 463 cgit_print_repolist();
464} 464}
465 465
466static void cgit_refresh_cache(struct cacheitem *item) 466static void cgit_refresh_cache(struct cacheitem *item)
467{ 467{
468 cache_prepare(item);
468 top: 469 top:
469 if (!cache_lookup(item)) { 470 if (!cache_exist(item)) {
470 if (cache_lock(item)) { 471 if (!cache_lock(item)) {
471 cgit_fill_cache(item);
472 cache_unlock(item);
473 } else {
474 sched_yield(); 472 sched_yield();
475 goto top; 473 goto top;
476 } 474 }
477 } else if (cache_expired(item)) { 475 if (!cache_exist(item))
478 if (cache_lock(item)) {
479 cgit_fill_cache(item); 476 cgit_fill_cache(item);
480 cache_unlock(item); 477 cache_unlock(item);
481 } 478 } else if (cache_expired(item) && cache_lock(item)) {
479 if (cache_expired(item))
480 cgit_fill_cache(item);
481 cache_unlock(item);
482 } 482 }
483} 483}
484 484
485static void cgit_print_cache(struct cacheitem *item) 485static void cgit_print_cache(struct cacheitem *item)
486{ 486{
487 static char buf[4096]; 487 static char buf[4096];
488 ssize_t i; 488 ssize_t i;
489 489
490 int fd = open(item->name, O_RDONLY); 490 int fd = open(item->name, O_RDONLY);
491 if (fd<0) 491 if (fd<0)
492 die("Unable to open cached file %s", item->name); 492 die("Unable to open cached file %s", item->name);
493 493
494 while((i=read(fd, buf, sizeof(buf))) > 0) 494 while((i=read(fd, buf, sizeof(buf))) > 0)
495 write(STDOUT_FILENO, buf, i); 495 write(STDOUT_FILENO, buf, i);
496 496
497 close(fd); 497 close(fd);
498} 498}
499 499
500int main(int argc, const char **argv) 500int main(int argc, const char **argv)
501{ 501{
502 cgit_read_config("/etc/cgitrc", cgit_global_config_cb); 502 cgit_read_config("/etc/cgitrc", cgit_global_config_cb);
503 cgit_querystring = xstrdup(getenv("QUERY_STRING")); 503 cgit_querystring = xstrdup(getenv("QUERY_STRING"));
504 cgit_parse_query(cgit_querystring, cgit_querystring_cb); 504 cgit_parse_query(cgit_querystring, cgit_querystring_cb);
505 cgit_refresh_cache(&cacheitem); 505 cgit_refresh_cache(&cacheitem);
506 cgit_print_cache(&cacheitem); 506 cgit_print_cache(&cacheitem);
507 return 0; 507 return 0;
508} 508}
diff --git a/cgit.h b/cgit.h
index 1e084d4..3b0994a 100644
--- a/cgit.h
+++ b/cgit.h
@@ -1,64 +1,65 @@
1#ifndef CGIT_H 1#ifndef CGIT_H
2#define CGIT_H 2#define CGIT_H
3 3
4#include "git.h" 4#include "git.h"
5#include <openssl/sha.h> 5#include <openssl/sha.h>
6#include <ctype.h> 6#include <ctype.h>
7#include <sched.h> 7#include <sched.h>
8 8
9typedef void (*configfn)(const char *name, const char *value); 9typedef void (*configfn)(const char *name, const char *value);
10 10
11struct cacheitem { 11struct cacheitem {
12 char *name; 12 char *name;
13 struct stat st; 13 struct stat st;
14 int ttl; 14 int ttl;
15 int fd; 15 int fd;
16}; 16};
17 17
18extern char *cgit_root; 18extern char *cgit_root;
19extern char *cgit_root_title; 19extern char *cgit_root_title;
20extern char *cgit_css; 20extern char *cgit_css;
21extern char *cgit_logo; 21extern char *cgit_logo;
22extern char *cgit_logo_link; 22extern char *cgit_logo_link;
23extern char *cgit_virtual_root; 23extern char *cgit_virtual_root;
24extern char *cgit_cache_root; 24extern char *cgit_cache_root;
25 25
26extern int cgit_cache_root_ttl; 26extern int cgit_cache_root_ttl;
27extern int cgit_cache_repo_ttl; 27extern int cgit_cache_repo_ttl;
28extern int cgit_cache_dynamic_ttl; 28extern int cgit_cache_dynamic_ttl;
29extern int cgit_cache_static_ttl; 29extern int cgit_cache_static_ttl;
30extern int cgit_cache_max_create_time; 30extern int cgit_cache_max_create_time;
31 31
32extern char *cgit_repo_name; 32extern char *cgit_repo_name;
33extern char *cgit_repo_desc; 33extern char *cgit_repo_desc;
34extern char *cgit_repo_owner; 34extern char *cgit_repo_owner;
35 35
36extern int cgit_query_has_symref; 36extern int cgit_query_has_symref;
37extern int cgit_query_has_sha1; 37extern int cgit_query_has_sha1;
38 38
39extern char *cgit_querystring; 39extern char *cgit_querystring;
40extern char *cgit_query_repo; 40extern char *cgit_query_repo;
41extern char *cgit_query_page; 41extern char *cgit_query_page;
42extern char *cgit_query_head; 42extern char *cgit_query_head;
43extern char *cgit_query_sha1; 43extern char *cgit_query_sha1;
44 44
45extern int htmlfd; 45extern int htmlfd;
46 46
47extern char *fmt(const char *format,...); 47extern char *fmt(const char *format,...);
48 48
49extern void html(const char *txt); 49extern void html(const char *txt);
50extern void htmlf(const char *format,...); 50extern void htmlf(const char *format,...);
51extern void html_txt(char *txt); 51extern void html_txt(char *txt);
52extern void html_attr(char *txt); 52extern void html_attr(char *txt);
53extern void html_link_open(char *url, char *title, char *class); 53extern void html_link_open(char *url, char *title, char *class);
54extern void html_link_close(void); 54extern void html_link_close(void);
55 55
56 56
57extern int cgit_read_config(const char *filename, configfn fn); 57extern int cgit_read_config(const char *filename, configfn fn);
58 58
59extern int cache_lookup(struct cacheitem *item); 59extern void cache_prepare(struct cacheitem *item);
60extern int cache_lock(struct cacheitem *item); 60extern int cache_lock(struct cacheitem *item);
61extern int cache_unlock(struct cacheitem *item); 61extern int cache_unlock(struct cacheitem *item);
62extern int cache_exist(struct cacheitem *item);
62extern int cache_expired(struct cacheitem *item); 63extern int cache_expired(struct cacheitem *item);
63 64
64#endif /* CGIT_H */ 65#endif /* CGIT_H */