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 int cgit_parse_query(char *txt
, configfn fn
)
58 char *t
, *value
= NULL
, c
;
63 t
= txt
= xstrdup(txt
);
65 while((c
=*t
) != '\0') {
82 void cgit_global_config_cb(const char *name
, const char *value
)
84 if (!strcmp(name
, "root"))
85 cgit_root
= xstrdup(value
);
86 else if (!strcmp(name
, "root-title"))
87 cgit_root_title
= xstrdup(value
);
88 else if (!strcmp(name
, "css"))
89 cgit_css
= xstrdup(value
);
90 else if (!strcmp(name
, "logo"))
91 cgit_logo
= xstrdup(value
);
92 else if (!strcmp(name
, "logo-link"))
93 cgit_logo_link
= xstrdup(value
);
94 else if (!strcmp(name
, "virtual-root"))
95 cgit_virtual_root
= xstrdup(value
);
98 void cgit_repo_config_cb(const char *name
, const char *value
)
100 if (!strcmp(name
, "name"))
101 cgit_repo_name
= xstrdup(value
);
102 else if (!strcmp(name
, "desc"))
103 cgit_repo_desc
= xstrdup(value
);
104 else if (!strcmp(name
, "owner"))
105 cgit_repo_owner
= xstrdup(value
);
108 void cgit_querystring_cb(const char *name
, const char *value
)
110 if (!strcmp(name
,"r"))
111 cgit_query_repo
= xstrdup(value
);
112 else if (!strcmp(name
, "p"))
113 cgit_query_page
= xstrdup(value
);
114 else if (!strcmp(name
, "h")) {
115 cgit_query_head
= xstrdup(value
);
116 cgit_query_has_symref
= 1;
117 } else if (!strcmp(name
, "id")) {
118 cgit_query_sha1
= xstrdup(value
);
119 cgit_query_has_sha1
= 1;
123 char *cgit_repourl(const char *reponame
)
125 if (cgit_virtual_root
) {
126 return fmt("%s/%s/", cgit_virtual_root
, reponame
);
128 return fmt("?r=%s", reponame
);
132 char *cgit_pageurl(const char *reponame
, const char *pagename
,
135 if (cgit_virtual_root
) {
136 return fmt("%s/%s/%s/?%s", cgit_virtual_root
, reponame
,
139 return fmt("?r=%s&p=%s&%s", reponame
, pagename
, query
);
143 static int cgit_print_branch_cb(const char *refname
, const unsigned char *sha1
,
144 int flags
, void *cb_data
)
146 struct commit
*commit
;
149 commit
= lookup_commit(sha1
);
150 if (commit
&& !parse_commit(commit
)){
152 url
= cgit_pageurl(cgit_query_repo
, "log",
153 fmt("h=%s", refname
));
154 html_link_open(url
, NULL
, NULL
);
155 strncpy(buf
, refname
, sizeof(buf
));
159 pretty_print_commit(CMIT_FMT_ONELINE
, commit
, ~0, buf
,
160 sizeof(buf
), 0, NULL
, NULL
, 0);
162 html("</td></tr>\n");
167 htmlf("*** bad ref %s", sha1_to_hex(sha1
));
168 html("</td></tr>\n");
173 /* Sun, 06 Nov 1994 08:49:37 GMT */
174 static char *http_date(time_t t
)
176 static char day
[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
177 static char month
[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
178 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
179 struct tm
*tm
= gmtime(&t
);
180 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day
[tm
->tm_wday
],
181 tm
->tm_mday
, month
[tm
->tm_mon
], 1900+tm
->tm_year
,
182 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
185 static int ttl_seconds(int ttl
)
188 return 60 * 60 * 24 * 365;
193 static void cgit_print_docstart(char *title
)
195 html("Content-Type: text/html; charset=utf-8\n");
196 htmlf("Last-Modified: %s\n", http_date(cacheitem
.st
.st_mtime
));
197 htmlf("Expires: %s\n", http_date(cacheitem
.st
.st_mtime
+
198 ttl_seconds(cacheitem
.ttl
)));
206 htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version
);
207 html("<link rel='stylesheet' type='text/css' href='");
214 static void cgit_print_docend()
216 html("</body>\n</html>\n");
219 static void cgit_print_pageheader(char *title
)
221 html("<div id='header'>");
222 htmlf("<a href='%s'>", cgit_logo_link
);
223 htmlf("<img id='logo' src='%s'/>\n", cgit_logo
);
229 static void cgit_print_repolist()
237 cgit_print_docstart(cgit_root_title
);
238 cgit_print_pageheader(cgit_root_title
);
240 if (!(d
= opendir("."))) {
241 htmlf(cgit_lib_error
, "Unable to scan repository directory",
247 html("<h2>Repositories</h2>\n");
248 html("<table class='list'>");
249 html("<tr><th>Name</th><th>Description</th><th>Owner</th></tr>\n");
250 while ((de
= readdir(d
)) != NULL
) {
251 if (de
->d_name
[0] == '.')
253 if (stat(de
->d_name
, &st
) < 0)
255 if (!S_ISDIR(st
.st_mode
))
258 cgit_repo_name
= cgit_repo_desc
= cgit_repo_owner
= NULL
;
259 name
= fmt("%s/info/cgit", de
->d_name
);
260 if (cgit_read_config(name
, cgit_repo_config_cb
))
264 html_link_open(cgit_repourl(de
->d_name
), NULL
, NULL
);
265 html_txt(cgit_repo_name
);
268 html_txt(cgit_repo_desc
);
270 html_txt(cgit_repo_owner
);
271 html("</td></tr>\n");
278 static void cgit_print_branches()
280 html("<table class='list'>");
281 html("<tr><th>Branch name</th><th>Head commit</th></tr>\n");
282 for_each_branch_ref(cgit_print_branch_cb
, NULL
);
286 static int get_one_line(char *txt
)
290 for(t
=txt
; *t
!= '\n' && t
!= '\0'; t
++)
296 static void cgit_print_commit_shortlog(struct commit
*commit
)
299 char *tree
= NULL
, *author
= NULL
, *subject
= NULL
;
305 h
= t
= commit
->buffer
;
307 if (strncmp(h
, "tree ", 5))
308 die("Bad commit format: %s",
309 sha1_to_hex(commit
->object
.sha1
));
311 len
= get_one_line(h
);
315 while (!strncmp(h
, "parent ", 7))
316 h
+= get_one_line(h
) + 2;
318 if (!strncmp(h
, "author ", 7)) {
320 h
+= get_one_line(h
) + 2;
322 while(t
!=h
&& *t
!='<')
326 while(--t
!=author
&& *t
==' ')
328 while(++p
!=h
&& *p
!='>')
330 while(++p
!=h
&& !isdigit(*p
))
334 while(++p
&& isdigit(*p
))
341 while((len
= get_one_line(h
)) > 0)
345 len
= get_one_line(h
);
350 strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", time
);
353 char *qry
= fmt("id=%s", sha1_to_hex(commit
->object
.sha1
));
354 char *url
= cgit_pageurl(cgit_query_repo
, "view", qry
);
355 html_link_open(url
, NULL
, NULL
);
360 html("</td></tr>\n");
363 static void cgit_print_log(const char *tip
, int ofs
, int cnt
)
366 struct commit
*commit
;
367 const char *argv
[2] = {NULL
, tip
};
370 init_revisions(&rev
, NULL
);
371 rev
.abbrev
= DEFAULT_ABBREV
;
372 rev
.commit_format
= CMIT_FMT_DEFAULT
;
373 rev
.verbose_header
= 1;
374 rev
.show_root_diff
= 0;
375 setup_revisions(2, argv
, &rev
, NULL
);
376 prepare_revision_walk(&rev
);
378 html("<h2>Log</h2>");
379 html("<table class='list'>");
380 html("<tr><th>Date</th><th>Message</th><th>Author</th></tr>\n");
381 while ((commit
= get_revision(&rev
)) != NULL
&& n
++ < 100) {
382 cgit_print_commit_shortlog(commit
);
383 free(commit
->buffer
);
384 commit
->buffer
= NULL
;
385 free_commit_list(commit
->parents
);
386 commit
->parents
= NULL
;
391 static void cgit_print_repo_summary()
394 html_txt("Repo summary page");
396 cgit_print_branches();
399 static void cgit_print_object(char *hex
)
401 unsigned char sha1
[20];
402 //struct object *object;
407 if (get_sha1_hex(hex
, sha1
)){
408 htmlf(cgit_error
, "Bad hex value");
412 if (sha1_object_info(sha1
, type
, NULL
)){
413 htmlf(cgit_error
, "Bad object name");
417 buf
= read_sha1_file(sha1
, type
, &size
);
419 htmlf(cgit_error
, "Error reading object");
424 html("<h2>Object view</h2>");
425 htmlf("sha1=%s<br/>type=%s<br/>size=%i<br/>", hex
, type
, size
);
431 static void cgit_print_repo_page()
433 if (chdir(fmt("%s/%s", cgit_root
, cgit_query_repo
)) ||
434 cgit_read_config("info/cgit", cgit_repo_config_cb
)) {
435 char *title
= fmt("%s - %s", cgit_root_title
, "Bad request");
436 cgit_print_docstart(title
);
437 cgit_print_pageheader(title
);
438 htmlf(cgit_lib_error
, "Unable to scan repository",
443 setenv("GIT_DIR", fmt("%s/%s", cgit_root
, cgit_query_repo
), 1);
444 char *title
= fmt("%s - %s", cgit_repo_name
, cgit_repo_desc
);
445 cgit_print_docstart(title
);
446 cgit_print_pageheader(title
);
447 if (!cgit_query_page
)
448 cgit_print_repo_summary();
449 else if (!strcmp(cgit_query_page
, "log")) {
450 cgit_print_log(cgit_query_head
, 0, 100);
451 } else if (!strcmp(cgit_query_page
, "view")) {
452 cgit_print_object(cgit_query_sha1
);
457 static void cgit_fill_cache(struct cacheitem
*item
)
460 item
->st
.st_mtime
= time(NULL
);
462 cgit_print_repo_page();
464 cgit_print_repolist();
467 static void cgit_refresh_cache(struct cacheitem
*item
)
473 if (++i
> cgit_max_lock_attempts
) {
474 die("cgit_refresh_cache: unable to lock %s: %s",
475 item
->name
, strerror(errno
));
477 if (!cache_exist(item
)) {
478 if (!cache_lock(item
)) {
482 if (!cache_exist(item
))
483 cgit_fill_cache(item
);
485 } else if (cache_expired(item
) && cache_lock(item
)) {
486 if (cache_expired(item
))
487 cgit_fill_cache(item
);
492 static void cgit_print_cache(struct cacheitem
*item
)
494 static char buf
[4096];
497 int fd
= open(item
->name
, O_RDONLY
);
499 die("Unable to open cached file %s", item
->name
);
501 while((i
=read(fd
, buf
, sizeof(buf
))) > 0)
502 write(STDOUT_FILENO
, buf
, i
);
507 int main(int argc
, const char **argv
)
509 cgit_read_config("/etc/cgitrc", cgit_global_config_cb
);
510 cgit_querystring
= xstrdup(getenv("QUERY_STRING"));
511 cgit_parse_query(cgit_querystring
, cgit_querystring_cb
);
512 cgit_refresh_cache(&cacheitem
);
513 cgit_print_cache(&cacheitem
);