]>
git.cameronkatri.com Git - cgit.git/blob - cgit.c
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 static void cgit_print_repo_page(struct cacheitem
*item
)
15 if (chdir(fmt("%s/%s", cgit_root
, cgit_query_repo
)) ||
16 cgit_read_config("info/cgit", cgit_repo_config_cb
)) {
17 char *title
= fmt("%s - %s", cgit_root_title
, "Bad request");
18 cgit_print_docstart(title
, item
);
19 cgit_print_pageheader(title
, 0);
20 cgit_print_error(fmt("Unable to scan repository: %s",
25 setenv("GIT_DIR", fmt("%s/%s", cgit_root
, cgit_query_repo
), 1);
26 char *title
= fmt("%s - %s", cgit_repo_name
, cgit_repo_desc
);
28 if (cgit_query_page
&& !strcmp(cgit_query_page
, "log"))
30 cgit_print_docstart(title
, item
);
31 cgit_print_pageheader(title
, show_search
);
32 if (!cgit_query_page
) {
34 } else if (!strcmp(cgit_query_page
, "log")) {
35 cgit_print_log(cgit_query_head
, cgit_query_ofs
, 100, cgit_query_search
);
36 } else if (!strcmp(cgit_query_page
, "tree")) {
37 cgit_print_tree(cgit_query_sha1
);
38 } else if (!strcmp(cgit_query_page
, "commit")) {
39 cgit_print_commit(cgit_query_sha1
);
40 } else if (!strcmp(cgit_query_page
, "view")) {
41 cgit_print_view(cgit_query_sha1
);
42 } else if (!strcmp(cgit_query_page
, "diff")) {
43 cgit_print_diff(cgit_query_sha1
, cgit_query_sha2
);
48 static void cgit_fill_cache(struct cacheitem
*item
)
50 static char buf
[PATH_MAX
];
52 getcwd(buf
, sizeof(buf
));
54 item
->st
.st_mtime
= time(NULL
);
56 cgit_print_repo_page(item
);
58 cgit_print_repolist(item
);
62 static void cgit_check_cache(struct cacheitem
*item
)
68 if (++i
> cgit_max_lock_attempts
) {
69 die("cgit_refresh_cache: unable to lock %s: %s",
70 item
->name
, strerror(errno
));
72 if (!cache_exist(item
)) {
73 if (!cache_lock(item
)) {
77 if (!cache_exist(item
)) {
78 cgit_fill_cache(item
);
81 cache_cancel_lock(item
);
83 } else if (cache_expired(item
) && cache_lock(item
)) {
84 if (cache_expired(item
)) {
85 cgit_fill_cache(item
);
88 cache_cancel_lock(item
);
93 static void cgit_print_cache(struct cacheitem
*item
)
95 static char buf
[4096];
98 int fd
= open(item
->name
, O_RDONLY
);
100 die("Unable to open cached file %s", item
->name
);
102 while((i
=read(fd
, buf
, sizeof(buf
))) > 0)
103 write(STDOUT_FILENO
, buf
, i
);
108 static void cgit_parse_args(int argc
, const char **argv
)
112 for (i
= 1; i
< argc
; i
++) {
113 if (!strncmp(argv
[i
], "--root=", 7)) {
114 cgit_root
= xstrdup(argv
[i
]+7);
116 if (!strncmp(argv
[i
], "--cache=", 8)) {
117 cgit_cache_root
= xstrdup(argv
[i
]+8);
119 if (!strcmp(argv
[i
], "--nocache")) {
122 if (!strncmp(argv
[i
], "--query=", 8)) {
123 cgit_querystring
= xstrdup(argv
[i
]+8);
125 if (!strncmp(argv
[i
], "--repo=", 7)) {
126 cgit_query_repo
= xstrdup(argv
[i
]+7);
128 if (!strncmp(argv
[i
], "--page=", 7)) {
129 cgit_query_page
= xstrdup(argv
[i
]+7);
131 if (!strncmp(argv
[i
], "--head=", 7)) {
132 cgit_query_head
= xstrdup(argv
[i
]+7);
133 cgit_query_has_symref
= 1;
135 if (!strncmp(argv
[i
], "--sha1=", 7)) {
136 cgit_query_sha1
= xstrdup(argv
[i
]+7);
137 cgit_query_has_sha1
= 1;
139 if (!strncmp(argv
[i
], "--ofs=", 6)) {
140 cgit_query_ofs
= atoi(argv
[i
]+6);
145 int main(int argc
, const char **argv
)
147 struct cacheitem item
;
149 cgit_read_config("/etc/cgitrc", cgit_global_config_cb
);
150 if (getenv("QUERY_STRING"))
151 cgit_querystring
= xstrdup(getenv("QUERY_STRING"));
152 cgit_parse_args(argc
, argv
);
153 cgit_parse_query(cgit_querystring
, cgit_querystring_cb
);
156 cache_prepare(&item
);
157 item
.fd
= STDOUT_FILENO
;
158 cgit_fill_cache(&item
);
160 cgit_check_cache(&item
);
161 cgit_print_cache(&item
);