blob: ac434410a484d9f06c4bec3cf96c0af352c0a4eb [file] [log] [blame]
Lars Hjemli7640d902006-12-10 22:41:14 +01001/* 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
Lars Hjemli0d169ad2006-12-09 15:18:17 +01009#include "cgit.h"
10
Lars Hjemli76827d82006-12-10 23:50:16 +010011const char cgit_version[] = CGIT_VERSION;
12
Lars Hjemli5a106eb2006-12-11 16:38:30 +010013static void cgit_print_repo_page(struct cacheitem *item)
Lars Hjemli0d169ad2006-12-09 15:18:17 +010014{
Lars Hjemli25105d72006-12-10 22:31:36 +010015 if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) ||
16 cgit_read_config("info/cgit", cgit_repo_config_cb)) {
Lars Hjemli0d169ad2006-12-09 15:18:17 +010017 char *title = fmt("%s - %s", cgit_root_title, "Bad request");
Lars Hjemli5a106eb2006-12-11 16:38:30 +010018 cgit_print_docstart(title, item);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010019 cgit_print_pageheader(title);
Lars Hjemli5a106eb2006-12-11 16:38:30 +010020 cgit_print_error(fmt("Unable to scan repository: %s",
21 strerror(errno)));
Lars Hjemli0d169ad2006-12-09 15:18:17 +010022 cgit_print_docend();
23 return;
24 }
Lars Hjemli25105d72006-12-10 22:31:36 +010025 setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010026 char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc);
Lars Hjemli5a106eb2006-12-11 16:38:30 +010027 cgit_print_docstart(title, item);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010028 cgit_print_pageheader(title);
Lars Hjemli06fe0c22006-12-13 00:13:27 +010029 if (!cgit_query_page) {
Lars Hjemlid14c5f62006-12-11 17:04:19 +010030 cgit_print_summary();
Lars Hjemli06fe0c22006-12-13 00:13:27 +010031 } else if (!strcmp(cgit_query_page, "log")) {
Lars Hjemli420712a2006-12-14 00:40:34 +010032 cgit_print_log(cgit_query_head, cgit_query_ofs, 100);
Lars Hjemli06fe0c22006-12-13 00:13:27 +010033 } else if (!strcmp(cgit_query_page, "tree")) {
34 cgit_print_tree(cgit_query_sha1);
Lars Hjemli9a8f8862006-12-16 00:19:56 +010035 } else if (!strcmp(cgit_query_page, "commit")) {
36 cgit_print_commit(cgit_query_sha1);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010037 } else if (!strcmp(cgit_query_page, "view")) {
Lars Hjemlidf631192006-12-11 17:12:26 +010038 cgit_print_view(cgit_query_sha1);
Lars Hjemli36aba002006-12-20 22:48:27 +010039 } else if (!strcmp(cgit_query_page, "diff")) {
40 cgit_print_diff(cgit_query_sha1, cgit_query_sha2);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010041 }
42 cgit_print_docend();
43}
44
Lars Hjemli25105d72006-12-10 22:31:36 +010045static void cgit_fill_cache(struct cacheitem *item)
Lars Hjemli0d169ad2006-12-09 15:18:17 +010046{
Lars Hjemli7c849d92006-12-16 13:55:58 +010047 static char buf[PATH_MAX];
48
49 getcwd(buf, sizeof(buf));
Lars Hjemli25105d72006-12-10 22:31:36 +010050 htmlfd = item->fd;
51 item->st.st_mtime = time(NULL);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010052 if (cgit_query_repo)
Lars Hjemli5a106eb2006-12-11 16:38:30 +010053 cgit_print_repo_page(item);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010054 else
Lars Hjemli5a106eb2006-12-11 16:38:30 +010055 cgit_print_repolist(item);
Lars Hjemli7c849d92006-12-16 13:55:58 +010056 chdir(buf);
Lars Hjemli25105d72006-12-10 22:31:36 +010057}
58
Lars Hjemli44923f82006-12-11 17:25:41 +010059static void cgit_check_cache(struct cacheitem *item)
Lars Hjemli25105d72006-12-10 22:31:36 +010060{
Lars Hjemli318d1062006-12-11 12:10:12 +010061 int i = 0;
62
Lars Hjemlif5069d82006-12-11 09:57:58 +010063 cache_prepare(item);
Lars Hjemli25105d72006-12-10 22:31:36 +010064 top:
Lars Hjemli318d1062006-12-11 12:10:12 +010065 if (++i > cgit_max_lock_attempts) {
66 die("cgit_refresh_cache: unable to lock %s: %s",
67 item->name, strerror(errno));
68 }
Lars Hjemlif5069d82006-12-11 09:57:58 +010069 if (!cache_exist(item)) {
70 if (!cache_lock(item)) {
Lars Hjemli318d1062006-12-11 12:10:12 +010071 sleep(1);
Lars Hjemli25105d72006-12-10 22:31:36 +010072 goto top;
73 }
Lars Hjemlifbaf1172006-12-11 22:53:50 +010074 if (!cache_exist(item)) {
Lars Hjemli25105d72006-12-10 22:31:36 +010075 cgit_fill_cache(item);
Lars Hjemlifbaf1172006-12-11 22:53:50 +010076 cache_unlock(item);
77 } else {
78 cache_cancel_lock(item);
79 }
Lars Hjemlif5069d82006-12-11 09:57:58 +010080 } else if (cache_expired(item) && cache_lock(item)) {
Lars Hjemlifbaf1172006-12-11 22:53:50 +010081 if (cache_expired(item)) {
Lars Hjemlif5069d82006-12-11 09:57:58 +010082 cgit_fill_cache(item);
Lars Hjemlifbaf1172006-12-11 22:53:50 +010083 cache_unlock(item);
84 } else {
85 cache_cancel_lock(item);
86 }
Lars Hjemli25105d72006-12-10 22:31:36 +010087 }
88}
89
90static void cgit_print_cache(struct cacheitem *item)
91{
92 static char buf[4096];
93 ssize_t i;
94
95 int fd = open(item->name, O_RDONLY);
96 if (fd<0)
97 die("Unable to open cached file %s", item->name);
98
99 while((i=read(fd, buf, sizeof(buf))) > 0)
100 write(STDOUT_FILENO, buf, i);
101
102 close(fd);
103}
104
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100105static void cgit_parse_args(int argc, const char **argv)
106{
107 int i;
108
109 for (i = 1; i < argc; i++) {
110 if (!strncmp(argv[i], "--root=", 7)) {
111 cgit_root = xstrdup(argv[i]+7);
112 }
113 if (!strncmp(argv[i], "--cache=", 8)) {
114 cgit_cache_root = xstrdup(argv[i]+8);
115 }
116 if (!strcmp(argv[i], "--nocache")) {
117 cgit_nocache = 1;
118 }
119 if (!strncmp(argv[i], "--query=", 8)) {
120 cgit_querystring = xstrdup(argv[i]+8);
121 }
122 if (!strncmp(argv[i], "--repo=", 7)) {
123 cgit_query_repo = xstrdup(argv[i]+7);
124 }
125 if (!strncmp(argv[i], "--page=", 7)) {
126 cgit_query_page = xstrdup(argv[i]+7);
127 }
128 if (!strncmp(argv[i], "--head=", 7)) {
129 cgit_query_head = xstrdup(argv[i]+7);
130 cgit_query_has_symref = 1;
131 }
132 if (!strncmp(argv[i], "--sha1=", 7)) {
133 cgit_query_sha1 = xstrdup(argv[i]+7);
134 cgit_query_has_sha1 = 1;
135 }
136 if (!strncmp(argv[i], "--ofs=", 6)) {
137 cgit_query_ofs = atoi(argv[i]+6);
138 }
139 }
140}
141
Lars Hjemli25105d72006-12-10 22:31:36 +0100142int main(int argc, const char **argv)
143{
Lars Hjemli44923f82006-12-11 17:25:41 +0100144 struct cacheitem item;
145
Lars Hjemli25105d72006-12-10 22:31:36 +0100146 cgit_read_config("/etc/cgitrc", cgit_global_config_cb);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100147 if (getenv("QUERY_STRING"))
148 cgit_querystring = xstrdup(getenv("QUERY_STRING"));
149 cgit_parse_args(argc, argv);
Lars Hjemli25105d72006-12-10 22:31:36 +0100150 cgit_parse_query(cgit_querystring, cgit_querystring_cb);
Lars Hjemli44923f82006-12-11 17:25:41 +0100151
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100152 if (cgit_nocache) {
153 item.fd = STDOUT_FILENO;
154 cgit_fill_cache(&item);
155 } else {
156 cgit_check_cache(&item);
157 cgit_print_cache(&item);
158 }
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100159 return 0;
160}