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;
13 const char cgit_doctype[] =
14 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
15 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
17 const char cgit_error[] =
18 "<div class='error'>%s</div>";
20 const char cgit_lib_error[] =
21 "<div class='error'>%s: %s</div>";
25 char *cgit_root = "/usr/src/git";
26 char *cgit_root_title = "Git repository browser";
27 char *cgit_css = "/cgit.css";
28 char *cgit_logo = "/git-logo.png";
29 char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/";
30 char *cgit_virtual_root = NULL;
32 char *cgit_cache_root = "/var/cache/cgit";
34 int cgit_max_lock_attempts = 5;
35 int cgit_cache_root_ttl = 5;
36 int cgit_cache_repo_ttl = 5;
37 int cgit_cache_dynamic_ttl = 5;
38 int cgit_cache_static_ttl = -1;
39 int cgit_cache_max_create_time = 5;
41 char *cgit_repo_name = NULL;
42 char *cgit_repo_desc = NULL;
43 char *cgit_repo_owner = NULL;
45 int cgit_query_has_symref = 0;
46 int cgit_query_has_sha1 = 0;
48 char *cgit_querystring = NULL;
49 char *cgit_query_repo = NULL;
50 char *cgit_query_page = NULL;
51 char *cgit_query_head = NULL;
52 char *cgit_query_sha1 = NULL;
54 struct cacheitem cacheitem;
56 void cgit_global_config_cb(const char *name, const char *value)
58 if (!strcmp(name, "root"))
59 cgit_root = xstrdup(value);
60 else if (!strcmp(name, "root-title"))
61 cgit_root_title = xstrdup(value);
62 else if (!strcmp(name, "css"))
63 cgit_css = xstrdup(value);
64 else if (!strcmp(name, "logo"))
65 cgit_logo = xstrdup(value);
66 else if (!strcmp(name, "logo-link"))
67 cgit_logo_link = xstrdup(value);
68 else if (!strcmp(name, "virtual-root"))
69 cgit_virtual_root = xstrdup(value);
72 void cgit_repo_config_cb(const char *name, const char *value)
74 if (!strcmp(name, "name"))
75 cgit_repo_name = xstrdup(value);
76 else if (!strcmp(name, "desc"))
77 cgit_repo_desc = xstrdup(value);
78 else if (!strcmp(name, "owner"))
79 cgit_repo_owner = xstrdup(value);
82 void cgit_querystring_cb(const char *name, const char *value)
84 if (!strcmp(name,"r"))
85 cgit_query_repo = xstrdup(value);
86 else if (!strcmp(name, "p"))
87 cgit_query_page = xstrdup(value);
88 else if (!strcmp(name, "h")) {
89 cgit_query_head = xstrdup(value);
90 cgit_query_has_symref = 1;
91 } else if (!strcmp(name, "id")) {
92 cgit_query_sha1 = xstrdup(value);
93 cgit_query_has_sha1 = 1;
97 char *cgit_repourl(const char *reponame)
99 if (cgit_virtual_root) {
100 return fmt("%s/%s/", cgit_virtual_root, reponame);
102 return fmt("?r=%s", reponame);
106 char *cgit_pageurl(const char *reponame, const char *pagename,
109 if (cgit_virtual_root) {
110 return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame,
113 return fmt("?r=%s&p=%s&%s", reponame, pagename, query);
117 static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1,
118 int flags, void *cb_data)
120 struct commit *commit;
123 commit = lookup_commit(sha1);
124 if (commit && !parse_commit(commit)){
126 url = cgit_pageurl(cgit_query_repo, "log",
127 fmt("h=%s", refname));
128 html_link_open(url, NULL, NULL);
129 strncpy(buf, refname, sizeof(buf));
133 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, buf,
134 sizeof(buf), 0, NULL, NULL, 0);
136 html("</td></tr>\n");
141 htmlf("*** bad ref %s", sha1_to_hex(sha1));
142 html("</td></tr>\n");
147 /* Sun, 06 Nov 1994 08:49:37 GMT */
148 static char *http_date(time_t t)
150 static char day[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
151 static char month[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
152 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
153 struct tm *tm = gmtime(&t);
154 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
155 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
156 tm->tm_hour, tm->tm_min, tm->tm_sec);
159 static int ttl_seconds(int ttl)
162 return 60 * 60 * 24 * 365;
167 static void cgit_print_docstart(char *title)
169 html("Content-Type: text/html; charset=utf-8\n");
170 htmlf("Last-Modified: %s\n", http_date(cacheitem.st.st_mtime));
171 htmlf("Expires: %s\n", http_date(cacheitem.st.st_mtime +
172 ttl_seconds(cacheitem.ttl)));
180 htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version);
181 html("<link rel='stylesheet' type='text/css' href='");
188 static void cgit_print_docend()
190 html("</body>\n</html>\n");
193 static void cgit_print_pageheader(char *title)
195 html("<div id='header'>");
196 htmlf("<a href='%s'>", cgit_logo_link);
197 htmlf("<img id='logo' src='%s'/>\n", cgit_logo);
203 static void cgit_print_repolist()
211 cgit_print_docstart(cgit_root_title);
212 cgit_print_pageheader(cgit_root_title);
214 if (!(d = opendir("."))) {
215 htmlf(cgit_lib_error, "Unable to scan repository directory",
221 html("<h2>Repositories</h2>\n");
222 html("<table class='list'>");
223 html("<tr><th>Name</th><th>Description</th><th>Owner</th></tr>\n");
224 while ((de = readdir(d)) != NULL) {
225 if (de->d_name[0] == '.')
227 if (stat(de->d_name, &st) < 0)
229 if (!S_ISDIR(st.st_mode))
232 cgit_repo_name = cgit_repo_desc = cgit_repo_owner = NULL;
233 name = fmt("%s/info/cgit", de->d_name);
234 if (cgit_read_config(name, cgit_repo_config_cb))
238 html_link_open(cgit_repourl(de->d_name), NULL, NULL);
239 html_txt(cgit_repo_name);
242 html_txt(cgit_repo_desc);
244 html_txt(cgit_repo_owner);
245 html("</td></tr>\n");
252 static void cgit_print_branches()
254 html("<table class='list'>");
255 html("<tr><th>Branch name</th><th>Head commit</th></tr>\n");
256 for_each_branch_ref(cgit_print_branch_cb, NULL);
260 static int get_one_line(char *txt)
264 for(t=txt; *t != '\n' && t != '\0'; t++)
270 static void cgit_print_commit_shortlog(struct commit *commit)
273 char *tree = NULL, *author = NULL, *subject = NULL;
279 h = t = commit->buffer;
281 if (strncmp(h, "tree ", 5))
282 die("Bad commit format: %s",
283 sha1_to_hex(commit->object.sha1));
285 len = get_one_line(h);
289 while (!strncmp(h, "parent ", 7))
290 h += get_one_line(h) + 2;
292 if (!strncmp(h, "author ", 7)) {
294 h += get_one_line(h) + 2;
296 while(t!=h && *t!='<')
300 while(--t!=author && *t==' ')
302 while(++p!=h && *p!='>')
304 while(++p!=h && !isdigit(*p))
308 while(++p && isdigit(*p))
315 while((len = get_one_line(h)) > 0)
319 len = get_one_line(h);
324 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time);
327 char *qry = fmt("id=%s", sha1_to_hex(commit->object.sha1));
328 char *url = cgit_pageurl(cgit_query_repo, "view", qry);
329 html_link_open(url, NULL, NULL);
334 html("</td></tr>\n");
337 static void cgit_print_log(const char *tip, int ofs, int cnt)
340 struct commit *commit;
341 const char *argv[2] = {NULL, tip};
344 init_revisions(&rev, NULL);
345 rev.abbrev = DEFAULT_ABBREV;
346 rev.commit_format = CMIT_FMT_DEFAULT;
347 rev.verbose_header = 1;
348 rev.show_root_diff = 0;
349 setup_revisions(2, argv, &rev, NULL);
350 prepare_revision_walk(&rev);
352 html("<h2>Log</h2>");
353 html("<table class='list'>");
354 html("<tr><th>Date</th><th>Message</th><th>Author</th></tr>\n");
355 while ((commit = get_revision(&rev)) != NULL && n++ < 100) {
356 cgit_print_commit_shortlog(commit);
357 free(commit->buffer);
358 commit->buffer = NULL;
359 free_commit_list(commit->parents);
360 commit->parents = NULL;
365 static void cgit_print_repo_summary()
368 html_txt("Repo summary page");
370 cgit_print_branches();
373 static void cgit_print_object(char *hex)
375 unsigned char sha1[20];
376 //struct object *object;
381 if (get_sha1_hex(hex, sha1)){
382 htmlf(cgit_error, "Bad hex value");
386 if (sha1_object_info(sha1, type, NULL)){
387 htmlf(cgit_error, "Bad object name");
391 buf = read_sha1_file(sha1, type, &size);
393 htmlf(cgit_error, "Error reading object");
398 html("<h2>Object view</h2>");
399 htmlf("sha1=%s<br/>type=%s<br/>size=%i<br/>", hex, type, size);
405 static void cgit_print_repo_page()
407 if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) ||
408 cgit_read_config("info/cgit", cgit_repo_config_cb)) {
409 char *title = fmt("%s - %s", cgit_root_title, "Bad request");
410 cgit_print_docstart(title);
411 cgit_print_pageheader(title);
412 htmlf(cgit_lib_error, "Unable to scan repository",
417 setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1);
418 char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc);
419 cgit_print_docstart(title);
420 cgit_print_pageheader(title);
421 if (!cgit_query_page)
422 cgit_print_repo_summary();
423 else if (!strcmp(cgit_query_page, "log")) {
424 cgit_print_log(cgit_query_head, 0, 100);
425 } else if (!strcmp(cgit_query_page, "view")) {
426 cgit_print_object(cgit_query_sha1);
431 static void cgit_fill_cache(struct cacheitem *item)
434 item->st.st_mtime = time(NULL);
436 cgit_print_repo_page();
438 cgit_print_repolist();
441 static void cgit_refresh_cache(struct cacheitem *item)
447 if (++i > cgit_max_lock_attempts) {
448 die("cgit_refresh_cache: unable to lock %s: %s",
449 item->name, strerror(errno));
451 if (!cache_exist(item)) {
452 if (!cache_lock(item)) {
456 if (!cache_exist(item))
457 cgit_fill_cache(item);
459 } else if (cache_expired(item) && cache_lock(item)) {
460 if (cache_expired(item))
461 cgit_fill_cache(item);
466 static void cgit_print_cache(struct cacheitem *item)
468 static char buf[4096];
471 int fd = open(item->name, O_RDONLY);
473 die("Unable to open cached file %s", item->name);
475 while((i=read(fd, buf, sizeof(buf))) > 0)
476 write(STDOUT_FILENO, buf, i);
481 int main(int argc, const char **argv)
483 cgit_read_config("/etc/cgitrc", cgit_global_config_cb);
484 cgit_querystring = xstrdup(getenv("QUERY_STRING"));
485 cgit_parse_query(cgit_querystring, cgit_querystring_cb);
486 cgit_refresh_cache(&cacheitem);
487 cgit_print_cache(&cacheitem);