]> gitweb.ps.run Git - ps-cgit/blob - cgit.c
Don't show new and old filemode for added/removed files
[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_print_repo_page(struct cacheitem *item)
14 {
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);
20                 cgit_print_error(fmt("Unable to scan repository: %s",
21                                      strerror(errno)));
22                 cgit_print_docend();
23                 return;
24         }
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);
27         cgit_print_docstart(title, item);
28         cgit_print_pageheader(title);
29         if (!cgit_query_page) {
30                 cgit_print_summary();
31         } else if (!strcmp(cgit_query_page, "log")) {
32                 cgit_print_log(cgit_query_head, cgit_query_ofs, 100);
33         } else if (!strcmp(cgit_query_page, "tree")) {
34                 cgit_print_tree(cgit_query_sha1);
35         } else if (!strcmp(cgit_query_page, "commit")) {
36                 cgit_print_commit(cgit_query_sha1);
37         } else if (!strcmp(cgit_query_page, "view")) {
38                 cgit_print_view(cgit_query_sha1);
39         }
40         cgit_print_docend();
41 }
42
43 static void cgit_fill_cache(struct cacheitem *item)
44 {
45         static char buf[PATH_MAX];
46
47         getcwd(buf, sizeof(buf));
48         htmlfd = item->fd;
49         item->st.st_mtime = time(NULL);
50         if (cgit_query_repo)
51                 cgit_print_repo_page(item);
52         else
53                 cgit_print_repolist(item);
54         chdir(buf);
55 }
56
57 static void cgit_check_cache(struct cacheitem *item)
58 {
59         int i = 0;
60
61         cache_prepare(item);
62  top:
63         if (++i > cgit_max_lock_attempts) {
64                 die("cgit_refresh_cache: unable to lock %s: %s",
65                     item->name, strerror(errno));
66         }
67         if (!cache_exist(item)) {
68                 if (!cache_lock(item)) {
69                         sleep(1);
70                         goto top;
71                 }
72                 if (!cache_exist(item)) {
73                         cgit_fill_cache(item);
74                         cache_unlock(item);
75                 } else {
76                         cache_cancel_lock(item);
77                 }
78         } else if (cache_expired(item) && cache_lock(item)) {
79                 if (cache_expired(item)) {
80                         cgit_fill_cache(item);
81                         cache_unlock(item);
82                 } else {
83                         cache_cancel_lock(item);
84                 }
85         }
86 }
87
88 static void cgit_print_cache(struct cacheitem *item)
89 {
90         static char buf[4096];
91         ssize_t i;
92
93         int fd = open(item->name, O_RDONLY);
94         if (fd<0)
95                 die("Unable to open cached file %s", item->name);
96
97         while((i=read(fd, buf, sizeof(buf))) > 0)
98                 write(STDOUT_FILENO, buf, i);
99
100         close(fd);
101 }
102
103 static void cgit_parse_args(int argc, const char **argv)
104 {
105         int i;
106
107         for (i = 1; i < argc; i++) {
108                 if (!strncmp(argv[i], "--root=", 7)) {
109                         cgit_root = xstrdup(argv[i]+7);
110                 }
111                 if (!strncmp(argv[i], "--cache=", 8)) {
112                         cgit_cache_root = xstrdup(argv[i]+8);
113                 }
114                 if (!strcmp(argv[i], "--nocache")) {
115                         cgit_nocache = 1;
116                 }
117                 if (!strncmp(argv[i], "--query=", 8)) {
118                         cgit_querystring = xstrdup(argv[i]+8);
119                 }
120                 if (!strncmp(argv[i], "--repo=", 7)) {
121                         cgit_query_repo = xstrdup(argv[i]+7);
122                 }
123                 if (!strncmp(argv[i], "--page=", 7)) {
124                         cgit_query_page = xstrdup(argv[i]+7);
125                 }
126                 if (!strncmp(argv[i], "--head=", 7)) {
127                         cgit_query_head = xstrdup(argv[i]+7);
128                         cgit_query_has_symref = 1;
129                 }
130                 if (!strncmp(argv[i], "--sha1=", 7)) {
131                         cgit_query_sha1 = xstrdup(argv[i]+7);
132                         cgit_query_has_sha1 = 1;
133                 }
134                 if (!strncmp(argv[i], "--ofs=", 6)) {
135                         cgit_query_ofs = atoi(argv[i]+6);
136                 }
137         }
138 }
139
140 int main(int argc, const char **argv)
141 {
142         struct cacheitem item;
143
144         cgit_read_config("/etc/cgitrc", cgit_global_config_cb);
145         if (getenv("QUERY_STRING"))
146                 cgit_querystring = xstrdup(getenv("QUERY_STRING"));
147         cgit_parse_args(argc, argv);
148         cgit_parse_query(cgit_querystring, cgit_querystring_cb);
149
150         if (cgit_nocache) {
151                 item.fd = STDOUT_FILENO;
152                 cgit_fill_cache(&item);
153         } else {
154                 cgit_check_cache(&item);
155                 cgit_print_cache(&item);
156         }
157         return 0;
158 }