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_cache_root_ttl
= 5;
35 int cgit_cache_repo_ttl
= 5;
36 int cgit_cache_dynamic_ttl
= 5;
37 int cgit_cache_static_ttl
= -1;
38 int cgit_cache_max_create_time
= 5;
40 char *cgit_repo_name
= NULL
;
41 char *cgit_repo_desc
= NULL
;
42 char *cgit_repo_owner
= NULL
;
44 int cgit_query_has_symref
= 0;
45 int cgit_query_has_sha1
= 0;
47 char *cgit_querystring
= NULL
;
48 char *cgit_query_repo
= NULL
;
49 char *cgit_query_page
= NULL
;
50 char *cgit_query_head
= NULL
;
51 char *cgit_query_sha1
= NULL
;
53 struct cacheitem cacheitem
;
55 int cgit_parse_query(char *txt
, configfn fn
)
57 char *t
, *value
= NULL
, c
;
62 t
= txt
= xstrdup(txt
);
64 while((c
=*t
) != '\0') {
81 void cgit_global_config_cb(const char *name
, const char *value
)
83 if (!strcmp(name
, "root"))
84 cgit_root
= xstrdup(value
);
85 else if (!strcmp(name
, "root-title"))
86 cgit_root_title
= xstrdup(value
);
87 else if (!strcmp(name
, "css"))
88 cgit_css
= xstrdup(value
);
89 else if (!strcmp(name
, "logo"))
90 cgit_logo
= xstrdup(value
);
91 else if (!strcmp(name
, "logo-link"))
92 cgit_logo_link
= xstrdup(value
);
93 else if (!strcmp(name
, "virtual-root"))
94 cgit_virtual_root
= xstrdup(value
);
97 void cgit_repo_config_cb(const char *name
, const char *value
)
99 if (!strcmp(name
, "name"))
100 cgit_repo_name
= xstrdup(value
);
101 else if (!strcmp(name
, "desc"))
102 cgit_repo_desc
= xstrdup(value
);
103 else if (!strcmp(name
, "owner"))
104 cgit_repo_owner
= xstrdup(value
);
107 void cgit_querystring_cb(const char *name
, const char *value
)
109 if (!strcmp(name
,"r"))
110 cgit_query_repo
= xstrdup(value
);
111 else if (!strcmp(name
, "p"))
112 cgit_query_page
= xstrdup(value
);
113 else if (!strcmp(name
, "h")) {
114 cgit_query_head
= xstrdup(value
);
115 cgit_query_has_symref
= 1;
116 } else if (!strcmp(name
, "id")) {
117 cgit_query_sha1
= xstrdup(value
);
118 cgit_query_has_sha1
= 1;
122 char *cgit_repourl(const char *reponame
)
124 if (cgit_virtual_root
) {
125 return fmt("%s/%s/", cgit_virtual_root
, reponame
);
127 return fmt("?r=%s", reponame
);
131 char *cgit_pageurl(const char *reponame
, const char *pagename
,
134 if (cgit_virtual_root
) {
135 return fmt("%s/%s/%s/?%s", cgit_virtual_root
, reponame
,
138 return fmt("?r=%s&p=%s&%s", reponame
, pagename
, query
);
142 static int cgit_print_branch_cb(const char *refname
, const unsigned char *sha1
,
143 int flags
, void *cb_data
)
145 struct commit
*commit
;
148 commit
= lookup_commit(sha1
);
149 if (commit
&& !parse_commit(commit
)){
151 url
= cgit_pageurl(cgit_query_repo
, "log",
152 fmt("h=%s", refname
));
153 html_link_open(url
, NULL
, NULL
);
154 strncpy(buf
, refname
, sizeof(buf
));
158 pretty_print_commit(CMIT_FMT_ONELINE
, commit
, ~0, buf
,
159 sizeof(buf
), 0, NULL
, NULL
, 0);
161 html("</td></tr>\n");
166 htmlf("*** bad ref %s", sha1_to_hex(sha1
));
167 html("</td></tr>\n");
172 /* Sun, 06 Nov 1994 08:49:37 GMT */
173 static char *http_date(time_t t
)
175 static char day
[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
176 static char month
[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
177 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
178 struct tm
*tm
= gmtime(&t
);
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
,
181 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
184 static int ttl_seconds(int ttl
)
187 return 60 * 60 * 24 * 365;
192 static void cgit_print_docstart(char *title
)
194 html("Content-Type: text/html; charset=utf-8\n");
195 htmlf("Last-Modified: %s\n", http_date(cacheitem
.st
.st_mtime
));
196 htmlf("Expires: %s\n", http_date(cacheitem
.st
.st_mtime
+
197 ttl_seconds(cacheitem
.ttl
)));
205 htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version
);
206 html("<link rel='stylesheet' type='text/css' href='");
213 static void cgit_print_docend()
215 html("</body>\n</html>\n");
218 static void cgit_print_pageheader(char *title
)
220 html("<div id='header'>");
221 htmlf("<a href='%s'>", cgit_logo_link
);
222 htmlf("<img id='logo' src='%s'/>\n", cgit_logo
);
228 static void cgit_print_repolist()
236 cgit_print_docstart(cgit_root_title
);
237 cgit_print_pageheader(cgit_root_title
);
239 if (!(d
= opendir("."))) {
240 htmlf(cgit_lib_error
, "Unable to scan repository directory",
246 html("<h2>Repositories</h2>\n");
247 html("<table class='list'>");
248 html("<tr><th>Name</th><th>Description</th><th>Owner</th></tr>\n");
249 while ((de
= readdir(d
)) != NULL
) {
250 if (de
->d_name
[0] == '.')
252 if (stat(de
->d_name
, &st
) < 0)
254 if (!S_ISDIR(st
.st_mode
))
257 cgit_repo_name
= cgit_repo_desc
= cgit_repo_owner
= NULL
;
258 name
= fmt("%s/info/cgit", de
->d_name
);
259 if (cgit_read_config(name
, cgit_repo_config_cb
))
263 html_link_open(cgit_repourl(de
->d_name
), NULL
, NULL
);
264 html_txt(cgit_repo_name
);
267 html_txt(cgit_repo_desc
);
269 html_txt(cgit_repo_owner
);
270 html("</td></tr>\n");
277 static void cgit_print_branches()
279 html("<table class='list'>");
280 html("<tr><th>Branch name</th><th>Head commit</th></tr>\n");
281 for_each_branch_ref(cgit_print_branch_cb
, NULL
);
285 static int get_one_line(char *txt
)
289 for(t
=txt
; *t
!= '\n' && t
!= '\0'; t
++)
295 static void cgit_print_commit_shortlog(struct commit
*commit
)
298 char *tree
= NULL
, *author
= NULL
, *subject
= NULL
;
304 h
= t
= commit
->buffer
;
306 if (strncmp(h
, "tree ", 5))
307 die("Bad commit format: %s",
308 sha1_to_hex(commit
->object
.sha1
));
310 len
= get_one_line(h
);
314 while (!strncmp(h
, "parent ", 7))
315 h
+= get_one_line(h
) + 2;
317 if (!strncmp(h
, "author ", 7)) {
319 h
+= get_one_line(h
) + 2;
321 while(t
!=h
&& *t
!='<')
325 while(--t
!=author
&& *t
==' ')
327 while(++p
!=h
&& *p
!='>')
329 while(++p
!=h
&& !isdigit(*p
))
333 while(++p
&& isdigit(*p
))
340 while((len
= get_one_line(h
)) > 0)
344 len
= get_one_line(h
);
349 strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", time
);
352 char *qry
= fmt("id=%s", sha1_to_hex(commit
->object
.sha1
));
353 char *url
= cgit_pageurl(cgit_query_repo
, "view", qry
);
354 html_link_open(url
, NULL
, NULL
);
359 html("</td></tr>\n");
362 static void cgit_print_log(const char *tip
, int ofs
, int cnt
)
365 struct commit
*commit
;
366 const char *argv
[2] = {NULL
, tip
};
369 init_revisions(&rev
, NULL
);
370 rev
.abbrev
= DEFAULT_ABBREV
;
371 rev
.commit_format
= CMIT_FMT_DEFAULT
;
372 rev
.verbose_header
= 1;
373 rev
.show_root_diff
= 0;
374 setup_revisions(2, argv
, &rev
, NULL
);
375 prepare_revision_walk(&rev
);
377 html("<h2>Log</h2>");
378 html("<table class='list'>");
379 html("<tr><th>Date</th><th>Message</th><th>Author</th></tr>\n");
380 while ((commit
= get_revision(&rev
)) != NULL
&& n
++ < 100) {
381 cgit_print_commit_shortlog(commit
);
382 free(commit
->buffer
);
383 commit
->buffer
= NULL
;
384 free_commit_list(commit
->parents
);
385 commit
->parents
= NULL
;
390 static void cgit_print_repo_summary()
393 html_txt("Repo summary page");
395 cgit_print_branches();
398 static void cgit_print_object(char *hex
)
400 unsigned char sha1
[20];
401 //struct object *object;
406 if (get_sha1_hex(hex
, sha1
)){
407 htmlf(cgit_error
, "Bad hex value");
411 if (sha1_object_info(sha1
, type
, NULL
)){
412 htmlf(cgit_error
, "Bad object name");
416 buf
= read_sha1_file(sha1
, type
, &size
);
418 htmlf(cgit_error
, "Error reading object");
423 html("<h2>Object view</h2>");
424 htmlf("sha1=%s<br/>type=%s<br/>size=%i<br/>", hex
, type
, size
);
430 static void cgit_print_repo_page()
432 if (chdir(fmt("%s/%s", cgit_root
, cgit_query_repo
)) ||
433 cgit_read_config("info/cgit", cgit_repo_config_cb
)) {
434 char *title
= fmt("%s - %s", cgit_root_title
, "Bad request");
435 cgit_print_docstart(title
);
436 cgit_print_pageheader(title
);
437 htmlf(cgit_lib_error
, "Unable to scan repository",
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
);
444 cgit_print_docstart(title
);
445 cgit_print_pageheader(title
);
446 if (!cgit_query_page
)
447 cgit_print_repo_summary();
448 else if (!strcmp(cgit_query_page
, "log")) {
449 cgit_print_log(cgit_query_head
, 0, 100);
450 } else if (!strcmp(cgit_query_page
, "view")) {
451 cgit_print_object(cgit_query_sha1
);
456 static void cgit_fill_cache(struct cacheitem
*item
)
459 item
->st
.st_mtime
= time(NULL
);
461 cgit_print_repo_page();
463 cgit_print_repolist();
466 static void cgit_refresh_cache(struct cacheitem
*item
)
470 if (!cache_exist(item
)) {
471 if (!cache_lock(item
)) {
475 if (!cache_exist(item
))
476 cgit_fill_cache(item
);
478 } else if (cache_expired(item
) && cache_lock(item
)) {
479 if (cache_expired(item
))
480 cgit_fill_cache(item
);
485 static void cgit_print_cache(struct cacheitem
*item
)
487 static char buf
[4096];
490 int fd
= open(item
->name
, O_RDONLY
);
492 die("Unable to open cached file %s", item
->name
);
494 while((i
=read(fd
, buf
, sizeof(buf
))) > 0)
495 write(STDOUT_FILENO
, buf
, i
);
500 int main(int argc
, const char **argv
)
502 cgit_read_config("/etc/cgitrc", cgit_global_config_cb
);
503 cgit_querystring
= xstrdup(getenv("QUERY_STRING"));
504 cgit_parse_query(cgit_querystring
, cgit_querystring_cb
);
505 cgit_refresh_cache(&cacheitem
);
506 cgit_print_cache(&cacheitem
);