]>
git.cameronkatri.com Git - cgit.git/blob - cgit.c
3 static const char cgit_doctype
[] =
4 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
5 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
7 static const char cgit_error
[] =
8 "<div class='error'>%s</div>";
10 static const char cgit_lib_error
[] =
11 "<div class='error'>%s: %s</div>";
14 char *cgit_root
= "/var/git";
15 char *cgit_root_title
= "Git repository browser";
16 char *cgit_css
= "/cgit.css";
17 char *cgit_logo
= "/git-logo.png";
18 char *cgit_logo_link
= "http://www.kernel.org/pub/software/scm/git/docs/";
19 char *cgit_virtual_root
= NULL
;
21 char *cgit_repo_name
= NULL
;
22 char *cgit_repo_desc
= NULL
;
23 char *cgit_repo_owner
= NULL
;
25 char *cgit_query_repo
= NULL
;
26 char *cgit_query_page
= NULL
;
27 char *cgit_query_head
= NULL
;
29 int cgit_parse_query(char *txt
, configfn fn
)
31 char *t
= txt
, *value
= NULL
, c
;
36 while((c
=*t
) != '\0') {
53 void cgit_global_config_cb(const char *name
, const char *value
)
55 if (!strcmp(name
, "root"))
56 cgit_root
= xstrdup(value
);
57 else if (!strcmp(name
, "root-title"))
58 cgit_root_title
= xstrdup(value
);
59 else if (!strcmp(name
, "css"))
60 cgit_css
= xstrdup(value
);
61 else if (!strcmp(name
, "logo"))
62 cgit_logo
= xstrdup(value
);
63 else if (!strcmp(name
, "logo-link"))
64 cgit_logo_link
= xstrdup(value
);
65 else if (!strcmp(name
, "virtual-root"))
66 cgit_virtual_root
= xstrdup(value
);
69 void cgit_repo_config_cb(const char *name
, const char *value
)
71 if (!strcmp(name
, "name"))
72 cgit_repo_name
= xstrdup(value
);
73 else if (!strcmp(name
, "desc"))
74 cgit_repo_desc
= xstrdup(value
);
75 else if (!strcmp(name
, "owner"))
76 cgit_repo_owner
= xstrdup(value
);
79 void cgit_querystring_cb(const char *name
, const char *value
)
81 if (!strcmp(name
,"r"))
82 cgit_query_repo
= xstrdup(value
);
83 else if (!strcmp(name
, "p"))
84 cgit_query_page
= xstrdup(value
);
85 else if (!strcmp(name
, "h"))
86 cgit_query_head
= xstrdup(value
);
89 char *cgit_repourl(const char *reponame
)
91 if (cgit_virtual_root
) {
92 return fmt("%s/%s/", cgit_virtual_root
, reponame
);
94 return fmt("?r=%s", reponame
);
98 char *cgit_pageurl(const char *reponame
, const char *pagename
,
101 if (cgit_virtual_root
) {
102 return fmt("%s/%s/%s/?%s", cgit_virtual_root
, reponame
,
105 return fmt("?r=%s&p=%s&%s", reponame
, pagename
, query
);
109 static int cgit_print_branch_cb(const char *refname
, const unsigned char *sha1
,
110 int flags
, void *cb_data
)
112 struct commit
*commit
;
115 commit
= lookup_commit(sha1
);
116 if (commit
&& !parse_commit(commit
)){
118 url
= cgit_pageurl(cgit_query_repo
, "log",
119 fmt("h=%s", refname
));
120 html_link_open(url
, NULL
, NULL
);
121 strncpy(buf
, refname
, sizeof(buf
));
125 pretty_print_commit(CMIT_FMT_ONELINE
, commit
, ~0, buf
,
126 sizeof(buf
), 0, NULL
, NULL
, 0);
128 html("</td></tr>\n");
133 htmlf("*** bad ref %s", sha1_to_hex(sha1
));
134 html("</td></tr>\n");
139 static void cgit_print_docstart(char *title
)
141 html("Content-Type: text/html; charset=utf-8\n");
149 html("<link rel='stylesheet' type='text/css' href='");
156 static void cgit_print_docend()
158 html("</body>\n</html>\n");
161 static void cgit_print_pageheader(char *title
)
163 html("<div id='header'>");
164 htmlf("<a href='%s'>", cgit_logo_link
);
165 htmlf("<img id='logo' src='%s'/>\n", cgit_logo
);
171 static void cgit_print_repolist()
178 cgit_print_docstart(cgit_root_title
);
179 cgit_print_pageheader(cgit_root_title
);
181 if (!(d
= opendir("."))) {
182 htmlf(cgit_lib_error
, "Unable to scan repository directory",
188 html("<h2>Repositories</h2>\n");
189 html("<table class='list'>");
190 html("<tr><th>Name</th><th>Description</th><th>Owner</th></tr>\n");
191 while ((de
= readdir(d
)) != NULL
) {
192 if (de
->d_name
[0] == '.')
194 if (stat(de
->d_name
, &st
) < 0)
196 if (!S_ISDIR(st
.st_mode
))
199 cgit_repo_name
= cgit_repo_desc
= cgit_repo_owner
= NULL
;
200 name
= fmt("%s/.git/info/cgit", de
->d_name
);
201 if (cgit_read_config(name
, cgit_repo_config_cb
))
205 html_link_open(cgit_repourl(de
->d_name
), NULL
, NULL
);
206 html_txt(cgit_repo_name
);
209 html_txt(cgit_repo_desc
);
211 html_txt(cgit_repo_owner
);
212 html("</td></tr>\n");
219 static void cgit_print_branches()
221 html("<table class='list'>");
222 html("<tr><th>Branch name</th><th>Head commit</th></tr>\n");
223 for_each_branch_ref(cgit_print_branch_cb
, NULL
);
227 static int get_one_line(char *txt
)
231 for(t
=txt
; *t
!= '\n' && t
!= '\0'; t
++)
237 static void cgit_print_commit_shortlog(struct commit
*commit
)
240 char *tree
= NULL
, *author
= NULL
, *subject
= NULL
;
246 h
= t
= commit
->buffer
;
248 if (strncmp(h
, "tree ", 5))
249 die("Bad commit format: %s",
250 sha1_to_hex(commit
->object
.sha1
));
252 len
= get_one_line(h
);
256 while (!strncmp(h
, "parent ", 7))
257 h
+= get_one_line(h
) + 2;
259 if (!strncmp(h
, "author ", 7)) {
261 h
+= get_one_line(h
) + 2;
263 while(t
!=h
&& *t
!='<')
267 while(--t
!=author
&& *t
==' ')
269 while(++p
!=h
&& *p
!='>')
271 while(++p
!=h
&& !isdigit(*p
))
275 while(++p
&& isdigit(*p
))
282 while((len
= get_one_line(h
)) > 0)
286 len
= get_one_line(h
);
291 strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", time
);
294 char *qry
= fmt("h=%s", sha1_to_hex(commit
->object
.sha1
));
295 char *url
= cgit_pageurl(cgit_query_repo
, "view", qry
);
296 html_link_open(url
, NULL
, NULL
);
301 html("</td></tr>\n");
304 static void cgit_print_log(const char *tip
, int ofs
, int cnt
)
307 struct commit
*commit
;
308 const char *argv
[2] = {NULL
, tip
};
311 init_revisions(&rev
, NULL
);
312 rev
.abbrev
= DEFAULT_ABBREV
;
313 rev
.commit_format
= CMIT_FMT_DEFAULT
;
314 rev
.verbose_header
= 1;
315 rev
.show_root_diff
= 0;
316 setup_revisions(2, argv
, &rev
, NULL
);
317 prepare_revision_walk(&rev
);
319 html("<h2>Log</h2>");
320 html("<table class='list'>");
321 html("<tr><th>Date</th><th>Message</th><th>Author</th></tr>\n");
322 while ((commit
= get_revision(&rev
)) != NULL
&& n
++ < 100) {
323 cgit_print_commit_shortlog(commit
);
324 free(commit
->buffer
);
325 commit
->buffer
= NULL
;
326 free_commit_list(commit
->parents
);
327 commit
->parents
= NULL
;
332 static void cgit_print_repo_summary()
335 html_txt("Repo summary page");
337 cgit_print_branches();
340 static void cgit_print_object(char *hex
)
342 unsigned char sha1
[20];
343 //struct object *object;
348 if (get_sha1_hex(hex
, sha1
)){
349 htmlf(cgit_error
, "Bad hex value");
353 if (sha1_object_info(sha1
, type
, NULL
)){
354 htmlf(cgit_error
, "Bad object name");
358 buf
= read_sha1_file(sha1
, type
, &size
);
360 htmlf(cgit_error
, "Error reading object");
365 html("<h2>Object view</h2>");
366 htmlf("sha1=%s<br/>type=%s<br/>size=%i<br/>", hex
, type
, size
);
372 static void cgit_print_repo_page()
374 if (chdir(cgit_query_repo
) ||
375 cgit_read_config(".git/info/cgit", cgit_repo_config_cb
)) {
376 char *title
= fmt("%s - %s", cgit_root_title
, "Bad request");
377 cgit_print_docstart(title
);
378 cgit_print_pageheader(title
);
379 htmlf(cgit_lib_error
, "Unable to scan repository",
385 char *title
= fmt("%s - %s", cgit_repo_name
, cgit_repo_desc
);
386 cgit_print_docstart(title
);
387 cgit_print_pageheader(title
);
388 if (!cgit_query_page
)
389 cgit_print_repo_summary();
390 else if (!strcmp(cgit_query_page
, "log")) {
391 cgit_print_log(cgit_query_head
, 0, 100);
392 } else if (!strcmp(cgit_query_page
, "view")) {
393 cgit_print_object(cgit_query_head
);
398 int main(int argc
, const char **argv
)
400 if (cgit_read_config("/etc/cgitrc", cgit_global_config_cb
))
401 die("Error reading config: %d %s", errno
, strerror(errno
));
404 cgit_parse_query(getenv("QUERY_STRING"), cgit_querystring_cb
);
406 cgit_print_repo_page();
408 cgit_print_repolist();