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 static const char cgit_doctype[] =
12 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
13 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
15 static const char cgit_error[] =
16 "<div class='error'>%s</div>";
18 static const char cgit_lib_error[] =
19 "<div class='error'>%s: %s</div>";
23 char *cgit_root = "/usr/src/git";
24 char *cgit_root_title = "Git repository browser";
25 char *cgit_css = "/cgit.css";
26 char *cgit_logo = "/git-logo.png";
27 char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/";
28 char *cgit_virtual_root = NULL;
30 char *cgit_cache_root = "/var/cache/cgit";
32 int cgit_cache_root_ttl = 5;
33 int cgit_cache_repo_ttl = 5;
34 int cgit_cache_dynamic_ttl = 5;
35 int cgit_cache_static_ttl = -1;
36 int cgit_cache_max_create_time = 5;
38 char *cgit_repo_name = NULL;
39 char *cgit_repo_desc = NULL;
40 char *cgit_repo_owner = NULL;
42 int cgit_query_has_symref = 0;
43 int cgit_query_has_sha1 = 0;
45 char *cgit_querystring = NULL;
46 char *cgit_query_repo = NULL;
47 char *cgit_query_page = NULL;
48 char *cgit_query_head = NULL;
49 char *cgit_query_sha1 = NULL;
51 struct cacheitem cacheitem;
53 int cgit_parse_query(char *txt, configfn fn)
55 char *t, *value = NULL, c;
60 t = txt = xstrdup(txt);
62 while((c=*t) != '\0') {
79 void cgit_global_config_cb(const char *name, const char *value)
81 if (!strcmp(name, "root"))
82 cgit_root = xstrdup(value);
83 else if (!strcmp(name, "root-title"))
84 cgit_root_title = xstrdup(value);
85 else if (!strcmp(name, "css"))
86 cgit_css = xstrdup(value);
87 else if (!strcmp(name, "logo"))
88 cgit_logo = xstrdup(value);
89 else if (!strcmp(name, "logo-link"))
90 cgit_logo_link = xstrdup(value);
91 else if (!strcmp(name, "virtual-root"))
92 cgit_virtual_root = xstrdup(value);
95 void cgit_repo_config_cb(const char *name, const char *value)
97 if (!strcmp(name, "name"))
98 cgit_repo_name = xstrdup(value);
99 else if (!strcmp(name, "desc"))
100 cgit_repo_desc = xstrdup(value);
101 else if (!strcmp(name, "owner"))
102 cgit_repo_owner = xstrdup(value);
105 void cgit_querystring_cb(const char *name, const char *value)
107 if (!strcmp(name,"r"))
108 cgit_query_repo = xstrdup(value);
109 else if (!strcmp(name, "p"))
110 cgit_query_page = xstrdup(value);
111 else if (!strcmp(name, "h")) {
112 cgit_query_head = xstrdup(value);
113 cgit_query_has_symref = 1;
114 } else if (!strcmp(name, "id")) {
115 cgit_query_sha1 = xstrdup(value);
116 cgit_query_has_sha1 = 1;
120 char *cgit_repourl(const char *reponame)
122 if (cgit_virtual_root) {
123 return fmt("%s/%s/", cgit_virtual_root, reponame);
125 return fmt("?r=%s", reponame);
129 char *cgit_pageurl(const char *reponame, const char *pagename,
132 if (cgit_virtual_root) {
133 return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame,
136 return fmt("?r=%s&p=%s&%s", reponame, pagename, query);
140 static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1,
141 int flags, void *cb_data)
143 struct commit *commit;
146 commit = lookup_commit(sha1);
147 if (commit && !parse_commit(commit)){
149 url = cgit_pageurl(cgit_query_repo, "log",
150 fmt("h=%s", refname));
151 html_link_open(url, NULL, NULL);
152 strncpy(buf, refname, sizeof(buf));
156 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, buf,
157 sizeof(buf), 0, NULL, NULL, 0);
159 html("</td></tr>\n");
164 htmlf("*** bad ref %s", sha1_to_hex(sha1));
165 html("</td></tr>\n");
170 /* Sun, 06 Nov 1994 08:49:37 GMT */
171 static char *http_date(time_t t)
173 static char day[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
174 static char month[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
175 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
176 struct tm *tm = gmtime(&t);
177 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
178 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
179 tm->tm_hour, tm->tm_min, tm->tm_sec);
182 static int ttl_seconds(int ttl)
185 return 60 * 60 * 24 * 365;
190 static void cgit_print_docstart(char *title)
192 html("Content-Type: text/html; charset=utf-8\n");
193 htmlf("Last-Modified: %s\n", http_date(cacheitem.st.st_mtime));
194 htmlf("Expires: %s\n", http_date(cacheitem.st.st_mtime +
195 ttl_seconds(cacheitem.ttl)));
203 html("<link rel='stylesheet' type='text/css' href='");
210 static void cgit_print_docend()
212 html("</body>\n</html>\n");
215 static void cgit_print_pageheader(char *title)
217 html("<div id='header'>");
218 htmlf("<a href='%s'>", cgit_logo_link);
219 htmlf("<img id='logo' src='%s'/>\n", cgit_logo);
225 static void cgit_print_repolist()
233 cgit_print_docstart(cgit_root_title);
234 cgit_print_pageheader(cgit_root_title);
236 if (!(d = opendir("."))) {
237 htmlf(cgit_lib_error, "Unable to scan repository directory",
243 html("<h2>Repositories</h2>\n");
244 html("<table class='list'>");
245 html("<tr><th>Name</th><th>Description</th><th>Owner</th></tr>\n");
246 while ((de = readdir(d)) != NULL) {
247 if (de->d_name[0] == '.')
249 if (stat(de->d_name, &st) < 0)
251 if (!S_ISDIR(st.st_mode))
254 cgit_repo_name = cgit_repo_desc = cgit_repo_owner = NULL;
255 name = fmt("%s/info/cgit", de->d_name);
256 if (cgit_read_config(name, cgit_repo_config_cb))
260 html_link_open(cgit_repourl(de->d_name), NULL, NULL);
261 html_txt(cgit_repo_name);
264 html_txt(cgit_repo_desc);
266 html_txt(cgit_repo_owner);
267 html("</td></tr>\n");
274 static void cgit_print_branches()
276 html("<table class='list'>");
277 html("<tr><th>Branch name</th><th>Head commit</th></tr>\n");
278 for_each_branch_ref(cgit_print_branch_cb, NULL);
282 static int get_one_line(char *txt)
286 for(t=txt; *t != '\n' && t != '\0'; t++)
292 static void cgit_print_commit_shortlog(struct commit *commit)
295 char *tree = NULL, *author = NULL, *subject = NULL;
301 h = t = commit->buffer;
303 if (strncmp(h, "tree ", 5))
304 die("Bad commit format: %s",
305 sha1_to_hex(commit->object.sha1));
307 len = get_one_line(h);
311 while (!strncmp(h, "parent ", 7))
312 h += get_one_line(h) + 2;
314 if (!strncmp(h, "author ", 7)) {
316 h += get_one_line(h) + 2;
318 while(t!=h && *t!='<')
322 while(--t!=author && *t==' ')
324 while(++p!=h && *p!='>')
326 while(++p!=h && !isdigit(*p))
330 while(++p && isdigit(*p))
337 while((len = get_one_line(h)) > 0)
341 len = get_one_line(h);
346 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time);
349 char *qry = fmt("id=%s", sha1_to_hex(commit->object.sha1));
350 char *url = cgit_pageurl(cgit_query_repo, "view", qry);
351 html_link_open(url, NULL, NULL);
356 html("</td></tr>\n");
359 static void cgit_print_log(const char *tip, int ofs, int cnt)
362 struct commit *commit;
363 const char *argv[2] = {NULL, tip};
366 init_revisions(&rev, NULL);
367 rev.abbrev = DEFAULT_ABBREV;
368 rev.commit_format = CMIT_FMT_DEFAULT;
369 rev.verbose_header = 1;
370 rev.show_root_diff = 0;
371 setup_revisions(2, argv, &rev, NULL);
372 prepare_revision_walk(&rev);
374 html("<h2>Log</h2>");
375 html("<table class='list'>");
376 html("<tr><th>Date</th><th>Message</th><th>Author</th></tr>\n");
377 while ((commit = get_revision(&rev)) != NULL && n++ < 100) {
378 cgit_print_commit_shortlog(commit);
379 free(commit->buffer);
380 commit->buffer = NULL;
381 free_commit_list(commit->parents);
382 commit->parents = NULL;
387 static void cgit_print_repo_summary()
390 html_txt("Repo summary page");
392 cgit_print_branches();
395 static void cgit_print_object(char *hex)
397 unsigned char sha1[20];
398 //struct object *object;
403 if (get_sha1_hex(hex, sha1)){
404 htmlf(cgit_error, "Bad hex value");
408 if (sha1_object_info(sha1, type, NULL)){
409 htmlf(cgit_error, "Bad object name");
413 buf = read_sha1_file(sha1, type, &size);
415 htmlf(cgit_error, "Error reading object");
420 html("<h2>Object view</h2>");
421 htmlf("sha1=%s<br/>type=%s<br/>size=%i<br/>", hex, type, size);
427 static void cgit_print_repo_page()
429 if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) ||
430 cgit_read_config("info/cgit", cgit_repo_config_cb)) {
431 char *title = fmt("%s - %s", cgit_root_title, "Bad request");
432 cgit_print_docstart(title);
433 cgit_print_pageheader(title);
434 htmlf(cgit_lib_error, "Unable to scan repository",
439 setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1);
440 char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc);
441 cgit_print_docstart(title);
442 cgit_print_pageheader(title);
443 if (!cgit_query_page)
444 cgit_print_repo_summary();
445 else if (!strcmp(cgit_query_page, "log")) {
446 cgit_print_log(cgit_query_head, 0, 100);
447 } else if (!strcmp(cgit_query_page, "view")) {
448 cgit_print_object(cgit_query_sha1);
453 static void cgit_fill_cache(struct cacheitem *item)
456 item->st.st_mtime = time(NULL);
458 cgit_print_repo_page();
460 cgit_print_repolist();
463 static void cgit_refresh_cache(struct cacheitem *item)
466 if (!cache_lookup(item)) {
467 if (cache_lock(item)) {
468 cgit_fill_cache(item);
474 } else if (cache_expired(item)) {
475 if (cache_lock(item)) {
476 cgit_fill_cache(item);
482 static void cgit_print_cache(struct cacheitem *item)
484 static char buf[4096];
487 int fd = open(item->name, O_RDONLY);
489 die("Unable to open cached file %s", item->name);
491 while((i=read(fd, buf, sizeof(buf))) > 0)
492 write(STDOUT_FILENO, buf, i);
497 int main(int argc, const char **argv)
499 cgit_read_config("/etc/cgitrc", cgit_global_config_cb);
500 cgit_querystring = xstrdup(getenv("QUERY_STRING"));
501 cgit_parse_query(cgit_querystring, cgit_querystring_cb);
502 cgit_refresh_cache(&cacheitem);
503 cgit_print_cache(&cacheitem);