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