summaryrefslogtreecommitdiffabout
authorLars Hjemli <hjemli@gmail.com>2006-12-11 16:25:41 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2006-12-11 16:25:51 (UTC)
commit44923f8953c66dc9b852316b655ab3b5aec9478e (patch) (unidiff)
treee129ec8db3f8ffcf7f82f94052f430e576a9714c
parentdf63119302910587e280d91dce67f6537a671f74 (diff)
downloadcgit-44923f8953c66dc9b852316b655ab3b5aec9478e.zip
cgit-44923f8953c66dc9b852316b655ab3b5aec9478e.tar.gz
cgit-44923f8953c66dc9b852316b655ab3b5aec9478e.tar.bz2
Move global variables + callback functions into shared.c
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--Makefile4
-rw-r--r--cgit.c83
-rw-r--r--cgit.h7
-rw-r--r--shared.c74
4 files changed, 86 insertions, 82 deletions
diff --git a/Makefile b/Makefile
index 4e532d0..c029637 100644
--- a/Makefile
+++ b/Makefile
@@ -1,28 +1,28 @@
1CGIT_VERSION = 0.1-pre 1CGIT_VERSION = 0.1-pre
2 2
3INSTALL_BIN = /var/www/htdocs/cgit.cgi 3INSTALL_BIN = /var/www/htdocs/cgit.cgi
4INSTALL_CSS = /var/www/htdocs/cgit.css 4INSTALL_CSS = /var/www/htdocs/cgit.css
5CACHE_ROOT = /var/cache/cgit 5CACHE_ROOT = /var/cache/cgit
6 6
7EXTLIBS = ../git/libgit.a ../git/xdiff/lib.a -lz -lcrypto 7EXTLIBS = ../git/libgit.a ../git/xdiff/lib.a -lz -lcrypto
8OBJECTS = cache.o parsing.o html.o ui-shared.o ui-repolist.o ui-summary.o \ 8OBJECTS = shared.o cache.o parsing.o html.o ui-shared.o ui-repolist.o \
9 ui-log.o ui-view.c 9 ui-summary.o ui-log.o ui-view.c
10 10
11CFLAGS += -Wall 11CFLAGS += -Wall
12 12
13all: cgit 13all: cgit
14 14
15install: all 15install: all
16 install cgit $(INSTALL_BIN) 16 install cgit $(INSTALL_BIN)
17 install cgit.css $(INSTALL_CSS) 17 install cgit.css $(INSTALL_CSS)
18 rm -rf $(CACHE_ROOT)/* 18 rm -rf $(CACHE_ROOT)/*
19 19
20cgit: cgit.c cgit.h git.h $(OBJECTS) 20cgit: cgit.c cgit.h git.h $(OBJECTS)
21 $(CC) $(CFLAGS) -DCGIT_VERSION='"$(CGIT_VERSION)"' cgit.c -o cgit \ 21 $(CC) $(CFLAGS) -DCGIT_VERSION='"$(CGIT_VERSION)"' cgit.c -o cgit \
22 $(OBJECTS) $(EXTLIBS) 22 $(OBJECTS) $(EXTLIBS)
23 23
24$(OBJECTS): cgit.h git.h 24$(OBJECTS): cgit.h git.h
25 25
26.PHONY: clean 26.PHONY: clean
27clean: 27clean:
28 rm -f cgit *.o 28 rm -f cgit *.o
diff --git a/cgit.c b/cgit.c
index 52d9bd3..d1abaa0 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1,171 +1,100 @@
1/* cgit.c: cgi for the git scm 1/* cgit.c: cgi for the git scm
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10 10
11const char cgit_version[] = CGIT_VERSION; 11const char cgit_version[] = CGIT_VERSION;
12 12
13int htmlfd = 0;
14
15char *cgit_root = "/usr/src/git";
16char *cgit_root_title = "Git repository browser";
17char *cgit_css = "/cgit.css";
18char *cgit_logo = "/git-logo.png";
19char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/";
20char *cgit_virtual_root = NULL;
21
22char *cgit_cache_root = "/var/cache/cgit";
23
24int cgit_max_lock_attempts = 5;
25int cgit_cache_root_ttl = 5;
26int cgit_cache_repo_ttl = 5;
27int cgit_cache_dynamic_ttl = 5;
28int cgit_cache_static_ttl = -1;
29int cgit_cache_max_create_time = 5;
30
31char *cgit_repo_name = NULL;
32char *cgit_repo_desc = NULL;
33char *cgit_repo_owner = NULL;
34
35int cgit_query_has_symref = 0;
36int cgit_query_has_sha1 = 0;
37
38char *cgit_querystring = NULL;
39char *cgit_query_repo = NULL;
40char *cgit_query_page = NULL;
41char *cgit_query_head = NULL;
42char *cgit_query_sha1 = NULL;
43
44struct cacheitem cacheitem;
45
46void cgit_global_config_cb(const char *name, const char *value)
47{
48 if (!strcmp(name, "root"))
49 cgit_root = xstrdup(value);
50 else if (!strcmp(name, "root-title"))
51 cgit_root_title = xstrdup(value);
52 else if (!strcmp(name, "css"))
53 cgit_css = xstrdup(value);
54 else if (!strcmp(name, "logo"))
55 cgit_logo = xstrdup(value);
56 else if (!strcmp(name, "logo-link"))
57 cgit_logo_link = xstrdup(value);
58 else if (!strcmp(name, "virtual-root"))
59 cgit_virtual_root = xstrdup(value);
60}
61
62void cgit_repo_config_cb(const char *name, const char *value)
63{
64 if (!strcmp(name, "name"))
65 cgit_repo_name = xstrdup(value);
66 else if (!strcmp(name, "desc"))
67 cgit_repo_desc = xstrdup(value);
68 else if (!strcmp(name, "owner"))
69 cgit_repo_owner = xstrdup(value);
70}
71
72void cgit_querystring_cb(const char *name, const char *value)
73{
74 if (!strcmp(name,"r"))
75 cgit_query_repo = xstrdup(value);
76 else if (!strcmp(name, "p"))
77 cgit_query_page = xstrdup(value);
78 else if (!strcmp(name, "h")) {
79 cgit_query_head = xstrdup(value);
80 cgit_query_has_symref = 1;
81 } else if (!strcmp(name, "id")) {
82 cgit_query_sha1 = xstrdup(value);
83 cgit_query_has_sha1 = 1;
84 }
85}
86
87static void cgit_print_repo_page(struct cacheitem *item) 13static void cgit_print_repo_page(struct cacheitem *item)
88{ 14{
89 if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) || 15 if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) ||
90 cgit_read_config("info/cgit", cgit_repo_config_cb)) { 16 cgit_read_config("info/cgit", cgit_repo_config_cb)) {
91 char *title = fmt("%s - %s", cgit_root_title, "Bad request"); 17 char *title = fmt("%s - %s", cgit_root_title, "Bad request");
92 cgit_print_docstart(title, item); 18 cgit_print_docstart(title, item);
93 cgit_print_pageheader(title); 19 cgit_print_pageheader(title);
94 cgit_print_error(fmt("Unable to scan repository: %s", 20 cgit_print_error(fmt("Unable to scan repository: %s",
95 strerror(errno))); 21 strerror(errno)));
96 cgit_print_docend(); 22 cgit_print_docend();
97 return; 23 return;
98 } 24 }
99 setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1); 25 setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1);
100 char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc); 26 char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc);
101 cgit_print_docstart(title, item); 27 cgit_print_docstart(title, item);
102 cgit_print_pageheader(title); 28 cgit_print_pageheader(title);
103 if (!cgit_query_page) 29 if (!cgit_query_page)
104 cgit_print_summary(); 30 cgit_print_summary();
105 else if (!strcmp(cgit_query_page, "log")) { 31 else if (!strcmp(cgit_query_page, "log")) {
106 cgit_print_log(cgit_query_head, 0, 100); 32 cgit_print_log(cgit_query_head, 0, 100);
107 } else if (!strcmp(cgit_query_page, "view")) { 33 } else if (!strcmp(cgit_query_page, "view")) {
108 cgit_print_view(cgit_query_sha1); 34 cgit_print_view(cgit_query_sha1);
109 } 35 }
110 cgit_print_docend(); 36 cgit_print_docend();
111} 37}
112 38
113static void cgit_fill_cache(struct cacheitem *item) 39static void cgit_fill_cache(struct cacheitem *item)
114{ 40{
115 htmlfd = item->fd; 41 htmlfd = item->fd;
116 item->st.st_mtime = time(NULL); 42 item->st.st_mtime = time(NULL);
117 if (cgit_query_repo) 43 if (cgit_query_repo)
118 cgit_print_repo_page(item); 44 cgit_print_repo_page(item);
119 else 45 else
120 cgit_print_repolist(item); 46 cgit_print_repolist(item);
121} 47}
122 48
123static void cgit_refresh_cache(struct cacheitem *item) 49static void cgit_check_cache(struct cacheitem *item)
124{ 50{
125 int i = 0; 51 int i = 0;
126 52
127 cache_prepare(item); 53 cache_prepare(item);
128 top: 54 top:
129 if (++i > cgit_max_lock_attempts) { 55 if (++i > cgit_max_lock_attempts) {
130 die("cgit_refresh_cache: unable to lock %s: %s", 56 die("cgit_refresh_cache: unable to lock %s: %s",
131 item->name, strerror(errno)); 57 item->name, strerror(errno));
132 } 58 }
133 if (!cache_exist(item)) { 59 if (!cache_exist(item)) {
134 if (!cache_lock(item)) { 60 if (!cache_lock(item)) {
135 sleep(1); 61 sleep(1);
136 goto top; 62 goto top;
137 } 63 }
138 if (!cache_exist(item)) 64 if (!cache_exist(item))
139 cgit_fill_cache(item); 65 cgit_fill_cache(item);
140 cache_unlock(item); 66 cache_unlock(item);
141 } else if (cache_expired(item) && cache_lock(item)) { 67 } else if (cache_expired(item) && cache_lock(item)) {
142 if (cache_expired(item)) 68 if (cache_expired(item))
143 cgit_fill_cache(item); 69 cgit_fill_cache(item);
144 cache_unlock(item); 70 cache_unlock(item);
145 } 71 }
146} 72}
147 73
148static void cgit_print_cache(struct cacheitem *item) 74static void cgit_print_cache(struct cacheitem *item)
149{ 75{
150 static char buf[4096]; 76 static char buf[4096];
151 ssize_t i; 77 ssize_t i;
152 78
153 int fd = open(item->name, O_RDONLY); 79 int fd = open(item->name, O_RDONLY);
154 if (fd<0) 80 if (fd<0)
155 die("Unable to open cached file %s", item->name); 81 die("Unable to open cached file %s", item->name);
156 82
157 while((i=read(fd, buf, sizeof(buf))) > 0) 83 while((i=read(fd, buf, sizeof(buf))) > 0)
158 write(STDOUT_FILENO, buf, i); 84 write(STDOUT_FILENO, buf, i);
159 85
160 close(fd); 86 close(fd);
161} 87}
162 88
163int main(int argc, const char **argv) 89int main(int argc, const char **argv)
164{ 90{
91 struct cacheitem item;
92
165 cgit_read_config("/etc/cgitrc", cgit_global_config_cb); 93 cgit_read_config("/etc/cgitrc", cgit_global_config_cb);
166 cgit_querystring = xstrdup(getenv("QUERY_STRING")); 94 cgit_querystring = xstrdup(getenv("QUERY_STRING"));
167 cgit_parse_query(cgit_querystring, cgit_querystring_cb); 95 cgit_parse_query(cgit_querystring, cgit_querystring_cb);
168 cgit_refresh_cache(&cacheitem); 96
169 cgit_print_cache(&cacheitem); 97 cgit_check_cache(&item);
98 cgit_print_cache(&item);
170 return 0; 99 return 0;
171} 100}
diff --git a/cgit.h b/cgit.h
index 9c7f0b4..c1dcc97 100644
--- a/cgit.h
+++ b/cgit.h
@@ -18,68 +18,69 @@ struct cacheitem {
18extern const char cgit_version[]; 18extern const char cgit_version[];
19 19
20extern char *cgit_root; 20extern char *cgit_root;
21extern char *cgit_root_title; 21extern char *cgit_root_title;
22extern char *cgit_css; 22extern char *cgit_css;
23extern char *cgit_logo; 23extern char *cgit_logo;
24extern char *cgit_logo_link; 24extern char *cgit_logo_link;
25extern char *cgit_virtual_root; 25extern char *cgit_virtual_root;
26extern char *cgit_cache_root; 26extern char *cgit_cache_root;
27 27
28extern int cgit_max_lock_attempts; 28extern int cgit_max_lock_attempts;
29extern int cgit_cache_root_ttl; 29extern int cgit_cache_root_ttl;
30extern int cgit_cache_repo_ttl; 30extern int cgit_cache_repo_ttl;
31extern int cgit_cache_dynamic_ttl; 31extern int cgit_cache_dynamic_ttl;
32extern int cgit_cache_static_ttl; 32extern int cgit_cache_static_ttl;
33extern int cgit_cache_max_create_time; 33extern int cgit_cache_max_create_time;
34 34
35extern char *cgit_repo_name; 35extern char *cgit_repo_name;
36extern char *cgit_repo_desc; 36extern char *cgit_repo_desc;
37extern char *cgit_repo_owner; 37extern char *cgit_repo_owner;
38 38
39extern int cgit_query_has_symref; 39extern int cgit_query_has_symref;
40extern int cgit_query_has_sha1; 40extern int cgit_query_has_sha1;
41 41
42extern char *cgit_querystring; 42extern char *cgit_querystring;
43extern char *cgit_query_repo; 43extern char *cgit_query_repo;
44extern char *cgit_query_page; 44extern char *cgit_query_page;
45extern char *cgit_query_head; 45extern char *cgit_query_head;
46extern char *cgit_query_sha1; 46extern char *cgit_query_sha1;
47 47
48extern int htmlfd; 48extern int htmlfd;
49 49
50extern void cgit_global_config_cb(const char *name, const char *value);
51extern void cgit_repo_config_cb(const char *name, const char *value);
52extern void cgit_querystring_cb(const char *name, const char *value);
53
50extern char *fmt(const char *format,...); 54extern char *fmt(const char *format,...);
51 55
52extern void html(const char *txt); 56extern void html(const char *txt);
53extern void htmlf(const char *format,...); 57extern void htmlf(const char *format,...);
54extern void html_txt(char *txt); 58extern void html_txt(char *txt);
55extern void html_attr(char *txt); 59extern void html_attr(char *txt);
56extern void html_link_open(char *url, char *title, char *class); 60extern void html_link_open(char *url, char *title, char *class);
57extern void html_link_close(void); 61extern void html_link_close(void);
58 62
59extern int cgit_read_config(const char *filename, configfn fn); 63extern int cgit_read_config(const char *filename, configfn fn);
60extern int cgit_parse_query(char *txt, configfn fn); 64extern int cgit_parse_query(char *txt, configfn fn);
61 65
62extern void cache_prepare(struct cacheitem *item); 66extern void cache_prepare(struct cacheitem *item);
63extern int cache_lock(struct cacheitem *item); 67extern int cache_lock(struct cacheitem *item);
64extern int cache_unlock(struct cacheitem *item); 68extern int cache_unlock(struct cacheitem *item);
65extern int cache_exist(struct cacheitem *item); 69extern int cache_exist(struct cacheitem *item);
66extern int cache_expired(struct cacheitem *item); 70extern int cache_expired(struct cacheitem *item);
67 71
68extern char *cgit_repourl(const char *reponame); 72extern char *cgit_repourl(const char *reponame);
69extern char *cgit_pageurl(const char *reponame, const char *pagename, 73extern char *cgit_pageurl(const char *reponame, const char *pagename,
70 const char *query); 74 const char *query);
71 75
72extern void cgit_print_error(char *msg); 76extern void cgit_print_error(char *msg);
73extern void cgit_print_docstart(char *title, struct cacheitem *item); 77extern void cgit_print_docstart(char *title, struct cacheitem *item);
74extern void cgit_print_docend(); 78extern void cgit_print_docend();
75extern void cgit_print_pageheader(char *title); 79extern void cgit_print_pageheader(char *title);
76 80
77extern void cgit_print_repolist(struct cacheitem *item); 81extern void cgit_print_repolist(struct cacheitem *item);
78extern void cgit_print_summary(); 82extern void cgit_print_summary();
79extern void cgit_print_log(const char *tip, int ofs, int cnt); 83extern void cgit_print_log(const char *tip, int ofs, int cnt);
80extern void cgit_print_view(char *hex); 84extern void cgit_print_view(char *hex);
81 85
82extern void cgit_repo_config_cb(const char *name, const char *value);
83
84
85#endif /* CGIT_H */ 86#endif /* CGIT_H */
diff --git a/shared.c b/shared.c
new file mode 100644
index 0000000..c58a2ff
--- a/dev/null
+++ b/shared.c
@@ -0,0 +1,74 @@
1#include "cgit.h"
2
3char *cgit_root = "/usr/src/git";
4char *cgit_root_title = "Git repository browser";
5char *cgit_css = "/cgit.css";
6char *cgit_logo = "/git-logo.png";
7char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/";
8char *cgit_virtual_root = NULL;
9
10char *cgit_cache_root = "/var/cache/cgit";
11
12int cgit_max_lock_attempts = 5;
13int cgit_cache_root_ttl = 5;
14int cgit_cache_repo_ttl = 5;
15int cgit_cache_dynamic_ttl = 5;
16int cgit_cache_static_ttl = -1;
17int cgit_cache_max_create_time = 5;
18
19char *cgit_repo_name = NULL;
20char *cgit_repo_desc = NULL;
21char *cgit_repo_owner = NULL;
22
23int cgit_query_has_symref = 0;
24int cgit_query_has_sha1 = 0;
25
26char *cgit_querystring = NULL;
27char *cgit_query_repo = NULL;
28char *cgit_query_page = NULL;
29char *cgit_query_head = NULL;
30char *cgit_query_sha1 = NULL;
31
32int htmlfd = 0;
33
34void cgit_global_config_cb(const char *name, const char *value)
35{
36 if (!strcmp(name, "root"))
37 cgit_root = xstrdup(value);
38 else if (!strcmp(name, "root-title"))
39 cgit_root_title = xstrdup(value);
40 else if (!strcmp(name, "css"))
41 cgit_css = xstrdup(value);
42 else if (!strcmp(name, "logo"))
43 cgit_logo = xstrdup(value);
44 else if (!strcmp(name, "logo-link"))
45 cgit_logo_link = xstrdup(value);
46 else if (!strcmp(name, "virtual-root"))
47 cgit_virtual_root = xstrdup(value);
48}
49
50void cgit_repo_config_cb(const char *name, const char *value)
51{
52 if (!strcmp(name, "name"))
53 cgit_repo_name = xstrdup(value);
54 else if (!strcmp(name, "desc"))
55 cgit_repo_desc = xstrdup(value);
56 else if (!strcmp(name, "owner"))
57 cgit_repo_owner = xstrdup(value);
58}
59
60void cgit_querystring_cb(const char *name, const char *value)
61{
62 if (!strcmp(name,"r"))
63 cgit_query_repo = xstrdup(value);
64 else if (!strcmp(name, "p"))
65 cgit_query_page = xstrdup(value);
66 else if (!strcmp(name, "h")) {
67 cgit_query_head = xstrdup(value);
68 cgit_query_has_symref = 1;
69 } else if (!strcmp(name, "id")) {
70 cgit_query_sha1 = xstrdup(value);
71 cgit_query_has_sha1 = 1;
72 }
73}
74