]> gitweb.ps.run Git - ps-cgit/blob - cgit.c
Update README with install/config information
[ps-cgit] / cgit.c
1 /* cgit.c: cgi for the git scm
2  *
3  * Copyright (C) 2006 Lars Hjemli
4  *
5  * Licensed under GNU General Public License v2
6  *   (see COPYING for full license text)
7  */
8
9 #include "cgit.h"
10
11 const char cgit_version[] = CGIT_VERSION;
12
13 static void cgit_prepare_cache(struct cacheitem *item)
14 {
15         if (!cgit_query_repo) {
16                 item->name = xstrdup(fmt("%s/index.html", cgit_cache_root));
17                 item->ttl = cgit_cache_root_ttl;
18         } else if (!cgit_query_page) {
19                 item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root, 
20                            cgit_query_repo));
21                 item->ttl = cgit_cache_repo_ttl;
22         } else {
23                 item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, 
24                            cgit_query_repo, cgit_query_page, 
25                            cache_safe_filename(cgit_querystring)));
26                 if (cgit_query_has_symref)
27                         item->ttl = cgit_cache_dynamic_ttl;
28                 else if (cgit_query_has_sha1)
29                         item->ttl = cgit_cache_static_ttl;
30                 else
31                         item->ttl = cgit_cache_repo_ttl;
32         }
33 }
34
35 static void cgit_print_repo_page(struct cacheitem *item)
36 {
37         if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) || 
38             cgit_read_config("info/cgit", cgit_repo_config_cb)) {
39                 char *title = fmt("%s - %s", cgit_root_title, "Bad request");
40                 cgit_print_docstart(title, item);
41                 cgit_print_pageheader(title, 0);
42                 cgit_print_error(fmt("Unable to scan repository: %s",
43                                      strerror(errno)));
44                 cgit_print_docend();
45                 return;
46         }
47         setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1);
48         char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc);
49         int show_search = 0;
50         if (cgit_query_page && !strcmp(cgit_query_page, "log"))
51                 show_search = 1;
52         cgit_print_docstart(title, item);
53         cgit_print_pageheader(title, show_search);
54         if (!cgit_query_page) {
55                 cgit_print_summary();
56         } else if (!strcmp(cgit_query_page, "log")) {
57                 cgit_print_log(cgit_query_head, cgit_query_ofs, 100, cgit_query_search);
58         } else if (!strcmp(cgit_query_page, "tree")) {
59                 cgit_print_tree(cgit_query_sha1, cgit_query_path);
60         } else if (!strcmp(cgit_query_page, "commit")) {
61                 cgit_print_commit(cgit_query_sha1);
62         } else if (!strcmp(cgit_query_page, "view")) {
63                 cgit_print_view(cgit_query_sha1);
64         } else if (!strcmp(cgit_query_page, "diff")) {
65                 cgit_print_diff(cgit_query_sha1, cgit_query_sha2);
66         }
67         cgit_print_docend();
68 }
69
70 static void cgit_fill_cache(struct cacheitem *item)
71 {
72         static char buf[PATH_MAX];
73
74         getcwd(buf, sizeof(buf));
75         htmlfd = item->fd;
76         item->st.st_mtime = time(NULL);
77         if (cgit_query_repo)
78                 cgit_print_repo_page(item);
79         else
80                 cgit_print_repolist(item);
81         chdir(buf);
82 }
83
84 static void cgit_check_cache(struct cacheitem *item)
85 {
86         int i = 0;
87
88  top:
89         if (++i > cgit_max_lock_attempts) {
90                 die("cgit_refresh_cache: unable to lock %s: %s",
91                     item->name, strerror(errno));
92         }
93         if (!cache_exist(item)) {
94                 if (!cache_lock(item)) {
95                         sleep(1);
96                         goto top;
97                 }
98                 if (!cache_exist(item)) {
99                         cgit_fill_cache(item);
100                         cache_unlock(item);
101                 } else {
102                         cache_cancel_lock(item);
103                 }
104         } else if (cache_expired(item) && cache_lock(item)) {
105                 if (cache_expired(item)) {
106                         cgit_fill_cache(item);
107                         cache_unlock(item);
108                 } else {
109                         cache_cancel_lock(item);
110                 }
111         }
112 }
113
114 static void cgit_print_cache(struct cacheitem *item)
115 {
116         static char buf[4096];
117         ssize_t i;
118
119         int fd = open(item->name, O_RDONLY);
120         if (fd<0)
121                 die("Unable to open cached file %s", item->name);
122
123         while((i=read(fd, buf, sizeof(buf))) > 0)
124                 write(STDOUT_FILENO, buf, i);
125
126         close(fd);
127 }
128
129 static void cgit_parse_args(int argc, const char **argv)
130 {
131         int i;
132
133         for (i = 1; i < argc; i++) {
134                 if (!strncmp(argv[i], "--root=", 7)) {
135                         cgit_root = xstrdup(argv[i]+7);
136                 }
137                 if (!strncmp(argv[i], "--cache=", 8)) {
138                         cgit_cache_root = xstrdup(argv[i]+8);
139                 }
140                 if (!strcmp(argv[i], "--nocache")) {
141                         cgit_nocache = 1;
142                 }
143                 if (!strncmp(argv[i], "--query=", 8)) {
144                         cgit_querystring = xstrdup(argv[i]+8);
145                 }
146                 if (!strncmp(argv[i], "--repo=", 7)) {
147                         cgit_query_repo = xstrdup(argv[i]+7);
148                 }
149                 if (!strncmp(argv[i], "--page=", 7)) {
150                         cgit_query_page = xstrdup(argv[i]+7);
151                 }
152                 if (!strncmp(argv[i], "--head=", 7)) {
153                         cgit_query_head = xstrdup(argv[i]+7);
154                         cgit_query_has_symref = 1;
155                 }
156                 if (!strncmp(argv[i], "--sha1=", 7)) {
157                         cgit_query_sha1 = xstrdup(argv[i]+7);
158                         cgit_query_has_sha1 = 1;
159                 }
160                 if (!strncmp(argv[i], "--ofs=", 6)) {
161                         cgit_query_ofs = atoi(argv[i]+6);
162                 }
163         }
164 }
165
166 int main(int argc, const char **argv)
167 {
168         struct cacheitem item;
169
170         cgit_read_config("/etc/cgitrc", cgit_global_config_cb);
171         if (getenv("QUERY_STRING"))
172                 cgit_querystring = xstrdup(getenv("QUERY_STRING"));
173         cgit_parse_args(argc, argv);
174         cgit_parse_query(cgit_querystring, cgit_querystring_cb);
175
176         cgit_prepare_cache(&item);
177         if (cgit_nocache) {
178                 item.fd = STDOUT_FILENO;
179                 cgit_fill_cache(&item);
180         } else {
181                 cgit_check_cache(&item);
182                 cgit_print_cache(&item);
183         }
184         return 0;
185 }