1 /* cgit.c: cgi for the git scm
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
11 const char cgit_version[] = CGIT_VERSION;
15 char *cgit_root = "/usr/src/git";
16 char *cgit_root_title = "Git repository browser";
17 char *cgit_css = "/cgit.css";
18 char *cgit_logo = "/git-logo.png";
19 char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/";
20 char *cgit_virtual_root = NULL;
22 char *cgit_cache_root = "/var/cache/cgit";
24 int cgit_max_lock_attempts = 5;
25 int cgit_cache_root_ttl = 5;
26 int cgit_cache_repo_ttl = 5;
27 int cgit_cache_dynamic_ttl = 5;
28 int cgit_cache_static_ttl = -1;
29 int cgit_cache_max_create_time = 5;
31 char *cgit_repo_name = NULL;
32 char *cgit_repo_desc = NULL;
33 char *cgit_repo_owner = NULL;
35 int cgit_query_has_symref = 0;
36 int cgit_query_has_sha1 = 0;
38 char *cgit_querystring = NULL;
39 char *cgit_query_repo = NULL;
40 char *cgit_query_page = NULL;
41 char *cgit_query_head = NULL;
42 char *cgit_query_sha1 = NULL;
44 struct cacheitem cacheitem;
46 void cgit_global_config_cb(const char *name, const char *value)
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);
62 void cgit_repo_config_cb(const char *name, const char *value)
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);
72 void cgit_querystring_cb(const char *name, const char *value)
74 if (!strcmp(name,"r"))
75 cgit_query_repo = xstrdup(value);
76 else if (!strcmp(name, "p"))
77 cgit_query_page = xstrdup(value);
78 else if (!strcmp(name, "h")) {
79 cgit_query_head = xstrdup(value);
80 cgit_query_has_symref = 1;
81 } else if (!strcmp(name, "id")) {
82 cgit_query_sha1 = xstrdup(value);
83 cgit_query_has_sha1 = 1;
87 char *cgit_repourl(const char *reponame)
89 if (cgit_virtual_root) {
90 return fmt("%s/%s/", cgit_virtual_root, reponame);
92 return fmt("?r=%s", reponame);
96 char *cgit_pageurl(const char *reponame, const char *pagename,
99 if (cgit_virtual_root) {
100 return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame,
103 return fmt("?r=%s&p=%s&%s", reponame, pagename, query);
107 static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1,
108 int flags, void *cb_data)
110 struct commit *commit;
113 commit = lookup_commit(sha1);
114 if (commit && !parse_commit(commit)){
116 url = cgit_pageurl(cgit_query_repo, "log",
117 fmt("h=%s", refname));
118 html_link_open(url, NULL, NULL);
119 strncpy(buf, refname, sizeof(buf));
123 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, buf,
124 sizeof(buf), 0, NULL, NULL, 0);
126 html("</td></tr>\n");
131 htmlf("*** bad ref %s", sha1_to_hex(sha1));
132 html("</td></tr>\n");
137 static void cgit_print_repolist(struct cacheitem *item)
145 cgit_print_docstart(cgit_root_title, item);
146 cgit_print_pageheader(cgit_root_title);
148 if (!(d = opendir("."))) {
149 cgit_print_error(fmt("Unable to scan repository directory: %s",
155 html("<h2>Repositories</h2>\n");
156 html("<table class='list'>");
157 html("<tr><th>Name</th><th>Description</th><th>Owner</th></tr>\n");
158 while ((de = readdir(d)) != NULL) {
159 if (de->d_name[0] == '.')
161 if (stat(de->d_name, &st) < 0)
163 if (!S_ISDIR(st.st_mode))
166 cgit_repo_name = cgit_repo_desc = cgit_repo_owner = NULL;
167 name = fmt("%s/info/cgit", de->d_name);
168 if (cgit_read_config(name, cgit_repo_config_cb))
172 html_link_open(cgit_repourl(de->d_name), NULL, NULL);
173 html_txt(cgit_repo_name);
176 html_txt(cgit_repo_desc);
178 html_txt(cgit_repo_owner);
179 html("</td></tr>\n");
186 static void cgit_print_branches()
188 html("<table class='list'>");
189 html("<tr><th>Branch name</th><th>Head commit</th></tr>\n");
190 for_each_branch_ref(cgit_print_branch_cb, NULL);
194 static int get_one_line(char *txt)
198 for(t=txt; *t != '\n' && t != '\0'; t++)
204 static void cgit_print_commit_shortlog(struct commit *commit)
207 char *tree = NULL, *author = NULL, *subject = NULL;
213 h = t = commit->buffer;
215 if (strncmp(h, "tree ", 5))
216 die("Bad commit format: %s",
217 sha1_to_hex(commit->object.sha1));
219 len = get_one_line(h);
223 while (!strncmp(h, "parent ", 7))
224 h += get_one_line(h) + 2;
226 if (!strncmp(h, "author ", 7)) {
228 h += get_one_line(h) + 2;
230 while(t!=h && *t!='<')
234 while(--t!=author && *t==' ')
236 while(++p!=h && *p!='>')
238 while(++p!=h && !isdigit(*p))
242 while(++p && isdigit(*p))
249 while((len = get_one_line(h)) > 0)
253 len = get_one_line(h);
258 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time);
261 char *qry = fmt("id=%s", sha1_to_hex(commit->object.sha1));
262 char *url = cgit_pageurl(cgit_query_repo, "view", qry);
263 html_link_open(url, NULL, NULL);
268 html("</td></tr>\n");
271 static void cgit_print_log(const char *tip, int ofs, int cnt)
274 struct commit *commit;
275 const char *argv[2] = {NULL, tip};
278 init_revisions(&rev, NULL);
279 rev.abbrev = DEFAULT_ABBREV;
280 rev.commit_format = CMIT_FMT_DEFAULT;
281 rev.verbose_header = 1;
282 rev.show_root_diff = 0;
283 setup_revisions(2, argv, &rev, NULL);
284 prepare_revision_walk(&rev);
286 html("<h2>Log</h2>");
287 html("<table class='list'>");
288 html("<tr><th>Date</th><th>Message</th><th>Author</th></tr>\n");
289 while ((commit = get_revision(&rev)) != NULL && n++ < 100) {
290 cgit_print_commit_shortlog(commit);
291 free(commit->buffer);
292 commit->buffer = NULL;
293 free_commit_list(commit->parents);
294 commit->parents = NULL;
299 static void cgit_print_repo_summary()
302 html_txt("Repo summary page");
304 cgit_print_branches();
307 static void cgit_print_object(char *hex)
309 unsigned char sha1[20];
310 //struct object *object;
315 if (get_sha1_hex(hex, sha1)){
316 cgit_print_error(fmt("Bad hex value: %s", hex));
320 if (sha1_object_info(sha1, type, NULL)){
321 cgit_print_error("Bad object name");
325 buf = read_sha1_file(sha1, type, &size);
327 cgit_print_error("Error reading object");
332 html("<h2>Object view</h2>");
333 htmlf("sha1=%s<br/>type=%s<br/>size=%i<br/>", hex, type, size);
339 static void cgit_print_repo_page(struct cacheitem *item)
341 if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) ||
342 cgit_read_config("info/cgit", cgit_repo_config_cb)) {
343 char *title = fmt("%s - %s", cgit_root_title, "Bad request");
344 cgit_print_docstart(title, item);
345 cgit_print_pageheader(title);
346 cgit_print_error(fmt("Unable to scan repository: %s",
351 setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1);
352 char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc);
353 cgit_print_docstart(title, item);
354 cgit_print_pageheader(title);
355 if (!cgit_query_page)
356 cgit_print_repo_summary();
357 else if (!strcmp(cgit_query_page, "log")) {
358 cgit_print_log(cgit_query_head, 0, 100);
359 } else if (!strcmp(cgit_query_page, "view")) {
360 cgit_print_object(cgit_query_sha1);
365 static void cgit_fill_cache(struct cacheitem *item)
368 item->st.st_mtime = time(NULL);
370 cgit_print_repo_page(item);
372 cgit_print_repolist(item);
375 static void cgit_refresh_cache(struct cacheitem *item)
381 if (++i > cgit_max_lock_attempts) {
382 die("cgit_refresh_cache: unable to lock %s: %s",
383 item->name, strerror(errno));
385 if (!cache_exist(item)) {
386 if (!cache_lock(item)) {
390 if (!cache_exist(item))
391 cgit_fill_cache(item);
393 } else if (cache_expired(item) && cache_lock(item)) {
394 if (cache_expired(item))
395 cgit_fill_cache(item);
400 static void cgit_print_cache(struct cacheitem *item)
402 static char buf[4096];
405 int fd = open(item->name, O_RDONLY);
407 die("Unable to open cached file %s", item->name);
409 while((i=read(fd, buf, sizeof(buf))) > 0)
410 write(STDOUT_FILENO, buf, i);
415 int main(int argc, const char **argv)
417 cgit_read_config("/etc/cgitrc", cgit_global_config_cb);
418 cgit_querystring = xstrdup(getenv("QUERY_STRING"));
419 cgit_parse_query(cgit_querystring, cgit_querystring_cb);
420 cgit_refresh_cache(&cacheitem);
421 cgit_print_cache(&cacheitem);