blob: 30a9a2a133d43bd1a91f761120c9274ef7e02b26 [file] [log] [blame]
Lars Hjemli7640d902006-12-10 22:41:14 +01001/* cgit.c: cgi for the git scm
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
Lars Hjemli0d169ad2006-12-09 15:18:17 +01009#include "cgit.h"
10
Lars Hjemli76827d82006-12-10 23:50:16 +010011const char cgit_version[] = CGIT_VERSION;
12
Lars Hjemli25105d72006-12-10 22:31:36 +010013int htmlfd = 0;
Lars Hjemli0d169ad2006-12-09 15:18:17 +010014
Lars Hjemli25105d72006-12-10 22:31:36 +010015char *cgit_root = "/usr/src/git";
Lars Hjemli0d169ad2006-12-09 15:18:17 +010016char *cgit_root_title = "Git repository browser";
17char *cgit_css = "/cgit.css";
18char *cgit_logo = "/git-logo.png";
19char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/";
20char *cgit_virtual_root = NULL;
21
Lars Hjemli25105d72006-12-10 22:31:36 +010022char *cgit_cache_root = "/var/cache/cgit";
23
Lars Hjemli318d1062006-12-11 12:10:12 +010024int cgit_max_lock_attempts = 5;
Lars Hjemli25105d72006-12-10 22:31:36 +010025int cgit_cache_root_ttl = 5;
26int cgit_cache_repo_ttl = 5;
27int cgit_cache_dynamic_ttl = 5;
28int cgit_cache_static_ttl = -1;
29int cgit_cache_max_create_time = 5;
30
Lars Hjemli0d169ad2006-12-09 15:18:17 +010031char *cgit_repo_name = NULL;
32char *cgit_repo_desc = NULL;
33char *cgit_repo_owner = NULL;
34
Lars Hjemli25105d72006-12-10 22:31:36 +010035int cgit_query_has_symref = 0;
36int cgit_query_has_sha1 = 0;
37
38char *cgit_querystring = NULL;
Lars Hjemli0d169ad2006-12-09 15:18:17 +010039char *cgit_query_repo = NULL;
40char *cgit_query_page = NULL;
41char *cgit_query_head = NULL;
Lars Hjemli25105d72006-12-10 22:31:36 +010042char *cgit_query_sha1 = NULL;
43
44struct cacheitem cacheitem;
Lars Hjemli0d169ad2006-12-09 15:18:17 +010045
Lars Hjemli0d169ad2006-12-09 15:18:17 +010046void cgit_global_config_cb(const char *name, const char *value)
47{
48 if (!strcmp(name, "root"))
49 cgit_root = xstrdup(value);
50 else if (!strcmp(name, "root-title"))
51 cgit_root_title = xstrdup(value);
52 else if (!strcmp(name, "css"))
53 cgit_css = xstrdup(value);
54 else if (!strcmp(name, "logo"))
55 cgit_logo = xstrdup(value);
56 else if (!strcmp(name, "logo-link"))
57 cgit_logo_link = xstrdup(value);
58 else if (!strcmp(name, "virtual-root"))
59 cgit_virtual_root = xstrdup(value);
60}
61
62void cgit_repo_config_cb(const char *name, const char *value)
63{
64 if (!strcmp(name, "name"))
65 cgit_repo_name = xstrdup(value);
66 else if (!strcmp(name, "desc"))
67 cgit_repo_desc = xstrdup(value);
68 else if (!strcmp(name, "owner"))
69 cgit_repo_owner = xstrdup(value);
70}
71
72void cgit_querystring_cb(const char *name, const char *value)
73{
74 if (!strcmp(name,"r"))
75 cgit_query_repo = xstrdup(value);
76 else if (!strcmp(name, "p"))
77 cgit_query_page = xstrdup(value);
Lars Hjemli25105d72006-12-10 22:31:36 +010078 else if (!strcmp(name, "h")) {
Lars Hjemli0d169ad2006-12-09 15:18:17 +010079 cgit_query_head = xstrdup(value);
Lars Hjemli25105d72006-12-10 22:31:36 +010080 cgit_query_has_symref = 1;
81 } else if (!strcmp(name, "id")) {
82 cgit_query_sha1 = xstrdup(value);
83 cgit_query_has_sha1 = 1;
84 }
Lars Hjemli0d169ad2006-12-09 15:18:17 +010085}
86
Lars Hjemli0d169ad2006-12-09 15:18:17 +010087static void cgit_print_object(char *hex)
88{
89 unsigned char sha1[20];
90 //struct object *object;
91 char type[20];
92 unsigned char *buf;
93 unsigned long size;
94
95 if (get_sha1_hex(hex, sha1)){
Lars Hjemli5a106eb2006-12-11 16:38:30 +010096 cgit_print_error(fmt("Bad hex value: %s", hex));
Lars Hjemli0d169ad2006-12-09 15:18:17 +010097 return;
98 }
99
100 if (sha1_object_info(sha1, type, NULL)){
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100101 cgit_print_error("Bad object name");
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100102 return;
103 }
104
105 buf = read_sha1_file(sha1, type, &size);
106 if (!buf) {
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100107 cgit_print_error("Error reading object");
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100108 return;
109 }
110
111 buf[size] = '\0';
112 html("<h2>Object view</h2>");
113 htmlf("sha1=%s<br/>type=%s<br/>size=%i<br/>", hex, type, size);
114 html("<pre>");
115 html_txt(buf);
116 html("</pre>");
117}
118
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100119static void cgit_print_repo_page(struct cacheitem *item)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100120{
Lars Hjemli25105d72006-12-10 22:31:36 +0100121 if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) ||
122 cgit_read_config("info/cgit", cgit_repo_config_cb)) {
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100123 char *title = fmt("%s - %s", cgit_root_title, "Bad request");
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100124 cgit_print_docstart(title, item);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100125 cgit_print_pageheader(title);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100126 cgit_print_error(fmt("Unable to scan repository: %s",
127 strerror(errno)));
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100128 cgit_print_docend();
129 return;
130 }
Lars Hjemli25105d72006-12-10 22:31:36 +0100131 setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100132 char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100133 cgit_print_docstart(title, item);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100134 cgit_print_pageheader(title);
135 if (!cgit_query_page)
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100136 cgit_print_summary();
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100137 else if (!strcmp(cgit_query_page, "log")) {
138 cgit_print_log(cgit_query_head, 0, 100);
139 } else if (!strcmp(cgit_query_page, "view")) {
Lars Hjemli25105d72006-12-10 22:31:36 +0100140 cgit_print_object(cgit_query_sha1);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100141 }
142 cgit_print_docend();
143}
144
Lars Hjemli25105d72006-12-10 22:31:36 +0100145static void cgit_fill_cache(struct cacheitem *item)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100146{
Lars Hjemli25105d72006-12-10 22:31:36 +0100147 htmlfd = item->fd;
148 item->st.st_mtime = time(NULL);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100149 if (cgit_query_repo)
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100150 cgit_print_repo_page(item);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100151 else
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100152 cgit_print_repolist(item);
Lars Hjemli25105d72006-12-10 22:31:36 +0100153}
154
155static void cgit_refresh_cache(struct cacheitem *item)
156{
Lars Hjemli318d1062006-12-11 12:10:12 +0100157 int i = 0;
158
Lars Hjemlif5069d82006-12-11 09:57:58 +0100159 cache_prepare(item);
Lars Hjemli25105d72006-12-10 22:31:36 +0100160 top:
Lars Hjemli318d1062006-12-11 12:10:12 +0100161 if (++i > cgit_max_lock_attempts) {
162 die("cgit_refresh_cache: unable to lock %s: %s",
163 item->name, strerror(errno));
164 }
Lars Hjemlif5069d82006-12-11 09:57:58 +0100165 if (!cache_exist(item)) {
166 if (!cache_lock(item)) {
Lars Hjemli318d1062006-12-11 12:10:12 +0100167 sleep(1);
Lars Hjemli25105d72006-12-10 22:31:36 +0100168 goto top;
169 }
Lars Hjemlif5069d82006-12-11 09:57:58 +0100170 if (!cache_exist(item))
Lars Hjemli25105d72006-12-10 22:31:36 +0100171 cgit_fill_cache(item);
Lars Hjemlif5069d82006-12-11 09:57:58 +0100172 cache_unlock(item);
173 } else if (cache_expired(item) && cache_lock(item)) {
174 if (cache_expired(item))
175 cgit_fill_cache(item);
176 cache_unlock(item);
Lars Hjemli25105d72006-12-10 22:31:36 +0100177 }
178}
179
180static void cgit_print_cache(struct cacheitem *item)
181{
182 static char buf[4096];
183 ssize_t i;
184
185 int fd = open(item->name, O_RDONLY);
186 if (fd<0)
187 die("Unable to open cached file %s", item->name);
188
189 while((i=read(fd, buf, sizeof(buf))) > 0)
190 write(STDOUT_FILENO, buf, i);
191
192 close(fd);
193}
194
195int main(int argc, const char **argv)
196{
197 cgit_read_config("/etc/cgitrc", cgit_global_config_cb);
198 cgit_querystring = xstrdup(getenv("QUERY_STRING"));
199 cgit_parse_query(cgit_querystring, cgit_querystring_cb);
200 cgit_refresh_cache(&cacheitem);
201 cgit_print_cache(&cacheitem);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100202 return 0;
203}