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 static int get_one_line(char *txt
)
91 for(t
=txt
; *t
!= '\n' && t
!= '\0'; t
++)
97 static void cgit_print_commit_shortlog(struct commit
*commit
)
100 char *tree
= NULL
, *author
= NULL
, *subject
= NULL
;
106 h
= t
= commit
->buffer
;
108 if (strncmp(h
, "tree ", 5))
109 die("Bad commit format: %s",
110 sha1_to_hex(commit
->object
.sha1
));
112 len
= get_one_line(h
);
116 while (!strncmp(h
, "parent ", 7))
117 h
+= get_one_line(h
) + 2;
119 if (!strncmp(h
, "author ", 7)) {
121 h
+= get_one_line(h
) + 2;
123 while(t
!=h
&& *t
!='<')
127 while(--t
!=author
&& *t
==' ')
129 while(++p
!=h
&& *p
!='>')
131 while(++p
!=h
&& !isdigit(*p
))
135 while(++p
&& isdigit(*p
))
142 while((len
= get_one_line(h
)) > 0)
146 len
= get_one_line(h
);
151 strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", time
);
154 char *qry
= fmt("id=%s", sha1_to_hex(commit
->object
.sha1
));
155 char *url
= cgit_pageurl(cgit_query_repo
, "view", qry
);
156 html_link_open(url
, NULL
, NULL
);
161 html("</td></tr>\n");
164 static void cgit_print_log(const char *tip
, int ofs
, int cnt
)
167 struct commit
*commit
;
168 const char *argv
[2] = {NULL
, tip
};
171 init_revisions(&rev
, NULL
);
172 rev
.abbrev
= DEFAULT_ABBREV
;
173 rev
.commit_format
= CMIT_FMT_DEFAULT
;
174 rev
.verbose_header
= 1;
175 rev
.show_root_diff
= 0;
176 setup_revisions(2, argv
, &rev
, NULL
);
177 prepare_revision_walk(&rev
);
179 html("<h2>Log</h2>");
180 html("<table class='list'>");
181 html("<tr><th>Date</th><th>Message</th><th>Author</th></tr>\n");
182 while ((commit
= get_revision(&rev
)) != NULL
&& n
++ < 100) {
183 cgit_print_commit_shortlog(commit
);
184 free(commit
->buffer
);
185 commit
->buffer
= NULL
;
186 free_commit_list(commit
->parents
);
187 commit
->parents
= NULL
;
192 static void cgit_print_object(char *hex
)
194 unsigned char sha1
[20];
195 //struct object *object;
200 if (get_sha1_hex(hex
, sha1
)){
201 cgit_print_error(fmt("Bad hex value: %s", hex
));
205 if (sha1_object_info(sha1
, type
, NULL
)){
206 cgit_print_error("Bad object name");
210 buf
= read_sha1_file(sha1
, type
, &size
);
212 cgit_print_error("Error reading object");
217 html("<h2>Object view</h2>");
218 htmlf("sha1=%s<br/>type=%s<br/>size=%i<br/>", hex
, type
, size
);
224 static void cgit_print_repo_page(struct cacheitem
*item
)
226 if (chdir(fmt("%s/%s", cgit_root
, cgit_query_repo
)) ||
227 cgit_read_config("info/cgit", cgit_repo_config_cb
)) {
228 char *title
= fmt("%s - %s", cgit_root_title
, "Bad request");
229 cgit_print_docstart(title
, item
);
230 cgit_print_pageheader(title
);
231 cgit_print_error(fmt("Unable to scan repository: %s",
236 setenv("GIT_DIR", fmt("%s/%s", cgit_root
, cgit_query_repo
), 1);
237 char *title
= fmt("%s - %s", cgit_repo_name
, cgit_repo_desc
);
238 cgit_print_docstart(title
, item
);
239 cgit_print_pageheader(title
);
240 if (!cgit_query_page
)
241 cgit_print_repo_summary();
242 else if (!strcmp(cgit_query_page
, "log")) {
243 cgit_print_log(cgit_query_head
, 0, 100);
244 } else if (!strcmp(cgit_query_page
, "view")) {
245 cgit_print_object(cgit_query_sha1
);
250 static void cgit_fill_cache(struct cacheitem
*item
)
253 item
->st
.st_mtime
= time(NULL
);
255 cgit_print_repo_page(item
);
257 cgit_print_repolist(item
);
260 static void cgit_refresh_cache(struct cacheitem
*item
)
266 if (++i
> cgit_max_lock_attempts
) {
267 die("cgit_refresh_cache: unable to lock %s: %s",
268 item
->name
, strerror(errno
));
270 if (!cache_exist(item
)) {
271 if (!cache_lock(item
)) {
275 if (!cache_exist(item
))
276 cgit_fill_cache(item
);
278 } else if (cache_expired(item
) && cache_lock(item
)) {
279 if (cache_expired(item
))
280 cgit_fill_cache(item
);
285 static void cgit_print_cache(struct cacheitem
*item
)
287 static char buf
[4096];
290 int fd
= open(item
->name
, O_RDONLY
);
292 die("Unable to open cached file %s", item
->name
);
294 while((i
=read(fd
, buf
, sizeof(buf
))) > 0)
295 write(STDOUT_FILENO
, buf
, i
);
300 int main(int argc
, const char **argv
)
302 cgit_read_config("/etc/cgitrc", cgit_global_config_cb
);
303 cgit_querystring
= xstrdup(getenv("QUERY_STRING"));
304 cgit_parse_query(cgit_querystring
, cgit_querystring_cb
);
305 cgit_refresh_cache(&cacheitem
);
306 cgit_print_cache(&cacheitem
);