|
diff --git a/shared.c b/shared.c index fd8b1e3..2c309f5 100644 --- a/ shared.c+++ b/ shared.c |
|
@@ -1,516 +1,521 @@ |
1 | /* shared.c: global vars + some callback functions |
1 | /* shared.c: global vars + some callback functions |
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 | struct repolist cgit_repolist; |
11 | struct repolist cgit_repolist; |
12 | struct repoinfo *cgit_repo; |
12 | struct repoinfo *cgit_repo; |
13 | int cgit_cmd; |
13 | int cgit_cmd; |
14 | |
14 | |
15 | const char *cgit_version = CGIT_VERSION; |
15 | const char *cgit_version = CGIT_VERSION; |
16 | |
16 | |
17 | char *cgit_root_title = "Git repository browser"; |
17 | char *cgit_root_title = "Git repository browser"; |
18 | char *cgit_css = "/cgit.css"; |
18 | char *cgit_css = "/cgit.css"; |
19 | char *cgit_logo = "/git-logo.png"; |
19 | char *cgit_logo = "/git-logo.png"; |
20 | char *cgit_index_header = NULL; |
20 | char *cgit_index_header = NULL; |
21 | char *cgit_index_info = NULL; |
21 | char *cgit_index_info = NULL; |
22 | char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; |
22 | char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; |
23 | char *cgit_module_link = "./?repo=%s&page=commit&id=%s"; |
23 | char *cgit_module_link = "./?repo=%s&page=commit&id=%s"; |
24 | char *cgit_agefile = "info/web/last-modified"; |
24 | char *cgit_agefile = "info/web/last-modified"; |
25 | char *cgit_virtual_root = NULL; |
25 | char *cgit_virtual_root = NULL; |
26 | char *cgit_script_name = CGIT_SCRIPT_NAME; |
26 | char *cgit_script_name = CGIT_SCRIPT_NAME; |
27 | char *cgit_cache_root = CGIT_CACHE_ROOT; |
27 | char *cgit_cache_root = CGIT_CACHE_ROOT; |
28 | char *cgit_repo_group = NULL; |
28 | char *cgit_repo_group = NULL; |
29 | char *cgit_robots = "index, nofollow"; |
29 | char *cgit_robots = "index, nofollow"; |
| |
30 | char *cgit_clone_prefix = NULL; |
30 | |
31 | |
31 | int cgit_nocache = 0; |
32 | int cgit_nocache = 0; |
32 | int cgit_snapshots = 0; |
33 | int cgit_snapshots = 0; |
33 | int cgit_enable_index_links = 0; |
34 | int cgit_enable_index_links = 0; |
34 | int cgit_enable_log_filecount = 0; |
35 | int cgit_enable_log_filecount = 0; |
35 | int cgit_enable_log_linecount = 0; |
36 | int cgit_enable_log_linecount = 0; |
36 | int cgit_max_lock_attempts = 5; |
37 | int cgit_max_lock_attempts = 5; |
37 | int cgit_cache_root_ttl = 5; |
38 | int cgit_cache_root_ttl = 5; |
38 | int cgit_cache_repo_ttl = 5; |
39 | int cgit_cache_repo_ttl = 5; |
39 | int cgit_cache_dynamic_ttl = 5; |
40 | int cgit_cache_dynamic_ttl = 5; |
40 | int cgit_cache_static_ttl = -1; |
41 | int cgit_cache_static_ttl = -1; |
41 | int cgit_cache_max_create_time = 5; |
42 | int cgit_cache_max_create_time = 5; |
42 | int cgit_summary_log = 0; |
43 | int cgit_summary_log = 0; |
43 | int cgit_summary_tags = 0; |
44 | int cgit_summary_tags = 0; |
44 | int cgit_summary_branches = 0; |
45 | int cgit_summary_branches = 0; |
45 | int cgit_renamelimit = -1; |
46 | int cgit_renamelimit = -1; |
46 | |
47 | |
47 | int cgit_max_msg_len = 60; |
48 | int cgit_max_msg_len = 60; |
48 | int cgit_max_repodesc_len = 60; |
49 | int cgit_max_repodesc_len = 60; |
49 | int cgit_max_commit_count = 50; |
50 | int cgit_max_commit_count = 50; |
50 | |
51 | |
51 | int cgit_query_has_symref = 0; |
52 | int cgit_query_has_symref = 0; |
52 | int cgit_query_has_sha1 = 0; |
53 | int cgit_query_has_sha1 = 0; |
53 | |
54 | |
54 | char *cgit_querystring = NULL; |
55 | char *cgit_querystring = NULL; |
55 | char *cgit_query_repo = NULL; |
56 | char *cgit_query_repo = NULL; |
56 | char *cgit_query_page = NULL; |
57 | char *cgit_query_page = NULL; |
57 | char *cgit_query_head = NULL; |
58 | char *cgit_query_head = NULL; |
58 | char *cgit_query_search = NULL; |
59 | char *cgit_query_search = NULL; |
59 | char *cgit_query_grep = NULL; |
60 | char *cgit_query_grep = NULL; |
60 | char *cgit_query_sha1 = NULL; |
61 | char *cgit_query_sha1 = NULL; |
61 | char *cgit_query_sha2 = NULL; |
62 | char *cgit_query_sha2 = NULL; |
62 | char *cgit_query_path = NULL; |
63 | char *cgit_query_path = NULL; |
63 | char *cgit_query_name = NULL; |
64 | char *cgit_query_name = NULL; |
64 | int cgit_query_ofs = 0; |
65 | int cgit_query_ofs = 0; |
65 | |
66 | |
66 | int htmlfd = 0; |
67 | int htmlfd = 0; |
67 | |
68 | |
68 | |
69 | |
69 | int cgit_get_cmd_index(const char *cmd) |
70 | int cgit_get_cmd_index(const char *cmd) |
70 | { |
71 | { |
71 | static char *cmds[] = {"log", "commit", "diff", "tree", "blob", |
72 | static char *cmds[] = {"log", "commit", "diff", "tree", "blob", |
72 | "snapshot", "tag", "refs", NULL}; |
73 | "snapshot", "tag", "refs", NULL}; |
73 | int i; |
74 | int i; |
74 | |
75 | |
75 | for(i = 0; cmds[i]; i++) |
76 | for(i = 0; cmds[i]; i++) |
76 | if (!strcmp(cmd, cmds[i])) |
77 | if (!strcmp(cmd, cmds[i])) |
77 | return i + 1; |
78 | return i + 1; |
78 | return 0; |
79 | return 0; |
79 | } |
80 | } |
80 | |
81 | |
81 | int chk_zero(int result, char *msg) |
82 | int chk_zero(int result, char *msg) |
82 | { |
83 | { |
83 | if (result != 0) |
84 | if (result != 0) |
84 | die("%s: %s", msg, strerror(errno)); |
85 | die("%s: %s", msg, strerror(errno)); |
85 | return result; |
86 | return result; |
86 | } |
87 | } |
87 | |
88 | |
88 | int chk_positive(int result, char *msg) |
89 | int chk_positive(int result, char *msg) |
89 | { |
90 | { |
90 | if (result <= 0) |
91 | if (result <= 0) |
91 | die("%s: %s", msg, strerror(errno)); |
92 | die("%s: %s", msg, strerror(errno)); |
92 | return result; |
93 | return result; |
93 | } |
94 | } |
94 | |
95 | |
95 | int chk_non_negative(int result, char *msg) |
96 | int chk_non_negative(int result, char *msg) |
96 | { |
97 | { |
97 | if (result < 0) |
98 | if (result < 0) |
98 | die("%s: %s",msg, strerror(errno)); |
99 | die("%s: %s",msg, strerror(errno)); |
99 | return result; |
100 | return result; |
100 | } |
101 | } |
101 | |
102 | |
102 | struct repoinfo *add_repo(const char *url) |
103 | struct repoinfo *add_repo(const char *url) |
103 | { |
104 | { |
104 | struct repoinfo *ret; |
105 | struct repoinfo *ret; |
105 | |
106 | |
106 | if (++cgit_repolist.count > cgit_repolist.length) { |
107 | if (++cgit_repolist.count > cgit_repolist.length) { |
107 | if (cgit_repolist.length == 0) |
108 | if (cgit_repolist.length == 0) |
108 | cgit_repolist.length = 8; |
109 | cgit_repolist.length = 8; |
109 | else |
110 | else |
110 | cgit_repolist.length *= 2; |
111 | cgit_repolist.length *= 2; |
111 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, |
112 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, |
112 | cgit_repolist.length * |
113 | cgit_repolist.length * |
113 | sizeof(struct repoinfo)); |
114 | sizeof(struct repoinfo)); |
114 | } |
115 | } |
115 | |
116 | |
116 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; |
117 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; |
117 | ret->url = trim_end(url, '/'); |
118 | ret->url = trim_end(url, '/'); |
118 | ret->name = ret->url; |
119 | ret->name = ret->url; |
119 | ret->path = NULL; |
120 | ret->path = NULL; |
120 | ret->desc = NULL; |
121 | ret->desc = NULL; |
121 | ret->owner = NULL; |
122 | ret->owner = NULL; |
122 | ret->group = cgit_repo_group; |
123 | ret->group = cgit_repo_group; |
123 | ret->defbranch = "master"; |
124 | ret->defbranch = "master"; |
124 | ret->snapshots = cgit_snapshots; |
125 | ret->snapshots = cgit_snapshots; |
125 | ret->enable_log_filecount = cgit_enable_log_filecount; |
126 | ret->enable_log_filecount = cgit_enable_log_filecount; |
126 | ret->enable_log_linecount = cgit_enable_log_linecount; |
127 | ret->enable_log_linecount = cgit_enable_log_linecount; |
127 | ret->module_link = cgit_module_link; |
128 | ret->module_link = cgit_module_link; |
128 | ret->readme = NULL; |
129 | ret->readme = NULL; |
129 | return ret; |
130 | return ret; |
130 | } |
131 | } |
131 | |
132 | |
132 | struct repoinfo *cgit_get_repoinfo(const char *url) |
133 | struct repoinfo *cgit_get_repoinfo(const char *url) |
133 | { |
134 | { |
134 | int i; |
135 | int i; |
135 | struct repoinfo *repo; |
136 | struct repoinfo *repo; |
136 | |
137 | |
137 | for (i=0; i<cgit_repolist.count; i++) { |
138 | for (i=0; i<cgit_repolist.count; i++) { |
138 | repo = &cgit_repolist.repos[i]; |
139 | repo = &cgit_repolist.repos[i]; |
139 | if (!strcmp(repo->url, url)) |
140 | if (!strcmp(repo->url, url)) |
140 | return repo; |
141 | return repo; |
141 | } |
142 | } |
142 | return NULL; |
143 | return NULL; |
143 | } |
144 | } |
144 | |
145 | |
145 | void cgit_global_config_cb(const char *name, const char *value) |
146 | void cgit_global_config_cb(const char *name, const char *value) |
146 | { |
147 | { |
147 | if (!strcmp(name, "root-title")) |
148 | if (!strcmp(name, "root-title")) |
148 | cgit_root_title = xstrdup(value); |
149 | cgit_root_title = xstrdup(value); |
149 | else if (!strcmp(name, "css")) |
150 | else if (!strcmp(name, "css")) |
150 | cgit_css = xstrdup(value); |
151 | cgit_css = xstrdup(value); |
151 | else if (!strcmp(name, "logo")) |
152 | else if (!strcmp(name, "logo")) |
152 | cgit_logo = xstrdup(value); |
153 | cgit_logo = xstrdup(value); |
153 | else if (!strcmp(name, "index-header")) |
154 | else if (!strcmp(name, "index-header")) |
154 | cgit_index_header = xstrdup(value); |
155 | cgit_index_header = xstrdup(value); |
155 | else if (!strcmp(name, "index-info")) |
156 | else if (!strcmp(name, "index-info")) |
156 | cgit_index_info = xstrdup(value); |
157 | cgit_index_info = xstrdup(value); |
157 | else if (!strcmp(name, "logo-link")) |
158 | else if (!strcmp(name, "logo-link")) |
158 | cgit_logo_link = xstrdup(value); |
159 | cgit_logo_link = xstrdup(value); |
159 | else if (!strcmp(name, "module-link")) |
160 | else if (!strcmp(name, "module-link")) |
160 | cgit_module_link = xstrdup(value); |
161 | cgit_module_link = xstrdup(value); |
161 | else if (!strcmp(name, "virtual-root")) { |
162 | else if (!strcmp(name, "virtual-root")) { |
162 | cgit_virtual_root = trim_end(value, '/'); |
163 | cgit_virtual_root = trim_end(value, '/'); |
163 | if (!cgit_virtual_root && (!strcmp(value, "/"))) |
164 | if (!cgit_virtual_root && (!strcmp(value, "/"))) |
164 | cgit_virtual_root = ""; |
165 | cgit_virtual_root = ""; |
165 | } else if (!strcmp(name, "nocache")) |
166 | } else if (!strcmp(name, "nocache")) |
166 | cgit_nocache = atoi(value); |
167 | cgit_nocache = atoi(value); |
167 | else if (!strcmp(name, "snapshots")) |
168 | else if (!strcmp(name, "snapshots")) |
168 | cgit_snapshots = cgit_parse_snapshots_mask(value); |
169 | cgit_snapshots = cgit_parse_snapshots_mask(value); |
169 | else if (!strcmp(name, "enable-index-links")) |
170 | else if (!strcmp(name, "enable-index-links")) |
170 | cgit_enable_index_links = atoi(value); |
171 | cgit_enable_index_links = atoi(value); |
171 | else if (!strcmp(name, "enable-log-filecount")) |
172 | else if (!strcmp(name, "enable-log-filecount")) |
172 | cgit_enable_log_filecount = atoi(value); |
173 | cgit_enable_log_filecount = atoi(value); |
173 | else if (!strcmp(name, "enable-log-linecount")) |
174 | else if (!strcmp(name, "enable-log-linecount")) |
174 | cgit_enable_log_linecount = atoi(value); |
175 | cgit_enable_log_linecount = atoi(value); |
175 | else if (!strcmp(name, "cache-root")) |
176 | else if (!strcmp(name, "cache-root")) |
176 | cgit_cache_root = xstrdup(value); |
177 | cgit_cache_root = xstrdup(value); |
177 | else if (!strcmp(name, "cache-root-ttl")) |
178 | else if (!strcmp(name, "cache-root-ttl")) |
178 | cgit_cache_root_ttl = atoi(value); |
179 | cgit_cache_root_ttl = atoi(value); |
179 | else if (!strcmp(name, "cache-repo-ttl")) |
180 | else if (!strcmp(name, "cache-repo-ttl")) |
180 | cgit_cache_repo_ttl = atoi(value); |
181 | cgit_cache_repo_ttl = atoi(value); |
181 | else if (!strcmp(name, "cache-static-ttl")) |
182 | else if (!strcmp(name, "cache-static-ttl")) |
182 | cgit_cache_static_ttl = atoi(value); |
183 | cgit_cache_static_ttl = atoi(value); |
183 | else if (!strcmp(name, "cache-dynamic-ttl")) |
184 | else if (!strcmp(name, "cache-dynamic-ttl")) |
184 | cgit_cache_dynamic_ttl = atoi(value); |
185 | cgit_cache_dynamic_ttl = atoi(value); |
185 | else if (!strcmp(name, "max-message-length")) |
186 | else if (!strcmp(name, "max-message-length")) |
186 | cgit_max_msg_len = atoi(value); |
187 | cgit_max_msg_len = atoi(value); |
187 | else if (!strcmp(name, "max-repodesc-length")) |
188 | else if (!strcmp(name, "max-repodesc-length")) |
188 | cgit_max_repodesc_len = atoi(value); |
189 | cgit_max_repodesc_len = atoi(value); |
189 | else if (!strcmp(name, "max-commit-count")) |
190 | else if (!strcmp(name, "max-commit-count")) |
190 | cgit_max_commit_count = atoi(value); |
191 | cgit_max_commit_count = atoi(value); |
191 | else if (!strcmp(name, "summary-log")) |
192 | else if (!strcmp(name, "summary-log")) |
192 | cgit_summary_log = atoi(value); |
193 | cgit_summary_log = atoi(value); |
193 | else if (!strcmp(name, "summary-branches")) |
194 | else if (!strcmp(name, "summary-branches")) |
194 | cgit_summary_branches = atoi(value); |
195 | cgit_summary_branches = atoi(value); |
195 | else if (!strcmp(name, "summary-tags")) |
196 | else if (!strcmp(name, "summary-tags")) |
196 | cgit_summary_tags = atoi(value); |
197 | cgit_summary_tags = atoi(value); |
197 | else if (!strcmp(name, "agefile")) |
198 | else if (!strcmp(name, "agefile")) |
198 | cgit_agefile = xstrdup(value); |
199 | cgit_agefile = xstrdup(value); |
199 | else if (!strcmp(name, "renamelimit")) |
200 | else if (!strcmp(name, "renamelimit")) |
200 | cgit_renamelimit = atoi(value); |
201 | cgit_renamelimit = atoi(value); |
201 | else if (!strcmp(name, "robots")) |
202 | else if (!strcmp(name, "robots")) |
202 | cgit_robots = xstrdup(value); |
203 | cgit_robots = xstrdup(value); |
| |
204 | else if (!strcmp(name, "clone-prefix")) |
| |
205 | cgit_clone_prefix = xstrdup(value); |
203 | else if (!strcmp(name, "repo.group")) |
206 | else if (!strcmp(name, "repo.group")) |
204 | cgit_repo_group = xstrdup(value); |
207 | cgit_repo_group = xstrdup(value); |
205 | else if (!strcmp(name, "repo.url")) |
208 | else if (!strcmp(name, "repo.url")) |
206 | cgit_repo = add_repo(value); |
209 | cgit_repo = add_repo(value); |
207 | else if (!strcmp(name, "repo.name")) |
210 | else if (!strcmp(name, "repo.name")) |
208 | cgit_repo->name = xstrdup(value); |
211 | cgit_repo->name = xstrdup(value); |
209 | else if (cgit_repo && !strcmp(name, "repo.path")) |
212 | else if (cgit_repo && !strcmp(name, "repo.path")) |
210 | cgit_repo->path = trim_end(value, '/'); |
213 | cgit_repo->path = trim_end(value, '/'); |
| |
214 | else if (cgit_repo && !strcmp(name, "repo.clone-url")) |
| |
215 | cgit_repo->clone_url = xstrdup(value); |
211 | else if (cgit_repo && !strcmp(name, "repo.desc")) |
216 | else if (cgit_repo && !strcmp(name, "repo.desc")) |
212 | cgit_repo->desc = xstrdup(value); |
217 | cgit_repo->desc = xstrdup(value); |
213 | else if (cgit_repo && !strcmp(name, "repo.owner")) |
218 | else if (cgit_repo && !strcmp(name, "repo.owner")) |
214 | cgit_repo->owner = xstrdup(value); |
219 | cgit_repo->owner = xstrdup(value); |
215 | else if (cgit_repo && !strcmp(name, "repo.defbranch")) |
220 | else if (cgit_repo && !strcmp(name, "repo.defbranch")) |
216 | cgit_repo->defbranch = xstrdup(value); |
221 | cgit_repo->defbranch = xstrdup(value); |
217 | else if (cgit_repo && !strcmp(name, "repo.snapshots")) |
222 | else if (cgit_repo && !strcmp(name, "repo.snapshots")) |
218 | cgit_repo->snapshots = cgit_snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ |
223 | cgit_repo->snapshots = cgit_snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ |
219 | else if (cgit_repo && !strcmp(name, "repo.enable-log-filecount")) |
224 | else if (cgit_repo && !strcmp(name, "repo.enable-log-filecount")) |
220 | cgit_repo->enable_log_filecount = cgit_enable_log_filecount * atoi(value); |
225 | cgit_repo->enable_log_filecount = cgit_enable_log_filecount * atoi(value); |
221 | else if (cgit_repo && !strcmp(name, "repo.enable-log-linecount")) |
226 | else if (cgit_repo && !strcmp(name, "repo.enable-log-linecount")) |
222 | cgit_repo->enable_log_linecount = cgit_enable_log_linecount * atoi(value); |
227 | cgit_repo->enable_log_linecount = cgit_enable_log_linecount * atoi(value); |
223 | else if (cgit_repo && !strcmp(name, "repo.module-link")) |
228 | else if (cgit_repo && !strcmp(name, "repo.module-link")) |
224 | cgit_repo->module_link= xstrdup(value); |
229 | cgit_repo->module_link= xstrdup(value); |
225 | else if (cgit_repo && !strcmp(name, "repo.readme") && value != NULL) { |
230 | else if (cgit_repo && !strcmp(name, "repo.readme") && value != NULL) { |
226 | if (*value == '/') |
231 | if (*value == '/') |
227 | cgit_repo->readme = xstrdup(value); |
232 | cgit_repo->readme = xstrdup(value); |
228 | else |
233 | else |
229 | cgit_repo->readme = xstrdup(fmt("%s/%s", cgit_repo->path, value)); |
234 | cgit_repo->readme = xstrdup(fmt("%s/%s", cgit_repo->path, value)); |
230 | } else if (!strcmp(name, "include")) |
235 | } else if (!strcmp(name, "include")) |
231 | cgit_read_config(value, cgit_global_config_cb); |
236 | cgit_read_config(value, cgit_global_config_cb); |
232 | } |
237 | } |
233 | |
238 | |
234 | void cgit_querystring_cb(const char *name, const char *value) |
239 | void cgit_querystring_cb(const char *name, const char *value) |
235 | { |
240 | { |
236 | if (!strcmp(name,"r")) { |
241 | if (!strcmp(name,"r")) { |
237 | cgit_query_repo = xstrdup(value); |
242 | cgit_query_repo = xstrdup(value); |
238 | cgit_repo = cgit_get_repoinfo(value); |
243 | cgit_repo = cgit_get_repoinfo(value); |
239 | } else if (!strcmp(name, "p")) { |
244 | } else if (!strcmp(name, "p")) { |
240 | cgit_query_page = xstrdup(value); |
245 | cgit_query_page = xstrdup(value); |
241 | cgit_cmd = cgit_get_cmd_index(value); |
246 | cgit_cmd = cgit_get_cmd_index(value); |
242 | } else if (!strcmp(name, "url")) { |
247 | } else if (!strcmp(name, "url")) { |
243 | cgit_parse_url(value); |
248 | cgit_parse_url(value); |
244 | } else if (!strcmp(name, "qt")) { |
249 | } else if (!strcmp(name, "qt")) { |
245 | cgit_query_grep = xstrdup(value); |
250 | cgit_query_grep = xstrdup(value); |
246 | } else if (!strcmp(name, "q")) { |
251 | } else if (!strcmp(name, "q")) { |
247 | cgit_query_search = xstrdup(value); |
252 | cgit_query_search = xstrdup(value); |
248 | } else if (!strcmp(name, "h")) { |
253 | } else if (!strcmp(name, "h")) { |
249 | cgit_query_head = xstrdup(value); |
254 | cgit_query_head = xstrdup(value); |
250 | cgit_query_has_symref = 1; |
255 | cgit_query_has_symref = 1; |
251 | } else if (!strcmp(name, "id")) { |
256 | } else if (!strcmp(name, "id")) { |
252 | cgit_query_sha1 = xstrdup(value); |
257 | cgit_query_sha1 = xstrdup(value); |
253 | cgit_query_has_sha1 = 1; |
258 | cgit_query_has_sha1 = 1; |
254 | } else if (!strcmp(name, "id2")) { |
259 | } else if (!strcmp(name, "id2")) { |
255 | cgit_query_sha2 = xstrdup(value); |
260 | cgit_query_sha2 = xstrdup(value); |
256 | cgit_query_has_sha1 = 1; |
261 | cgit_query_has_sha1 = 1; |
257 | } else if (!strcmp(name, "ofs")) { |
262 | } else if (!strcmp(name, "ofs")) { |
258 | cgit_query_ofs = atoi(value); |
263 | cgit_query_ofs = atoi(value); |
259 | } else if (!strcmp(name, "path")) { |
264 | } else if (!strcmp(name, "path")) { |
260 | cgit_query_path = trim_end(value, '/'); |
265 | cgit_query_path = trim_end(value, '/'); |
261 | } else if (!strcmp(name, "name")) { |
266 | } else if (!strcmp(name, "name")) { |
262 | cgit_query_name = xstrdup(value); |
267 | cgit_query_name = xstrdup(value); |
263 | } |
268 | } |
264 | } |
269 | } |
265 | |
270 | |
266 | void *cgit_free_commitinfo(struct commitinfo *info) |
271 | void *cgit_free_commitinfo(struct commitinfo *info) |
267 | { |
272 | { |
268 | free(info->author); |
273 | free(info->author); |
269 | free(info->author_email); |
274 | free(info->author_email); |
270 | free(info->committer); |
275 | free(info->committer); |
271 | free(info->committer_email); |
276 | free(info->committer_email); |
272 | free(info->subject); |
277 | free(info->subject); |
273 | free(info->msg); |
278 | free(info->msg); |
274 | free(info->msg_encoding); |
279 | free(info->msg_encoding); |
275 | free(info); |
280 | free(info); |
276 | return NULL; |
281 | return NULL; |
277 | } |
282 | } |
278 | |
283 | |
279 | int hextoint(char c) |
284 | int hextoint(char c) |
280 | { |
285 | { |
281 | if (c >= 'a' && c <= 'f') |
286 | if (c >= 'a' && c <= 'f') |
282 | return 10 + c - 'a'; |
287 | return 10 + c - 'a'; |
283 | else if (c >= 'A' && c <= 'F') |
288 | else if (c >= 'A' && c <= 'F') |
284 | return 10 + c - 'A'; |
289 | return 10 + c - 'A'; |
285 | else if (c >= '0' && c <= '9') |
290 | else if (c >= '0' && c <= '9') |
286 | return c - '0'; |
291 | return c - '0'; |
287 | else |
292 | else |
288 | return -1; |
293 | return -1; |
289 | } |
294 | } |
290 | |
295 | |
291 | char *trim_end(const char *str, char c) |
296 | char *trim_end(const char *str, char c) |
292 | { |
297 | { |
293 | int len; |
298 | int len; |
294 | char *s, *t; |
299 | char *s, *t; |
295 | |
300 | |
296 | if (str == NULL) |
301 | if (str == NULL) |
297 | return NULL; |
302 | return NULL; |
298 | t = (char *)str; |
303 | t = (char *)str; |
299 | len = strlen(t); |
304 | len = strlen(t); |
300 | while(len > 0 && t[len - 1] == c) |
305 | while(len > 0 && t[len - 1] == c) |
301 | len--; |
306 | len--; |
302 | |
307 | |
303 | if (len == 0) |
308 | if (len == 0) |
304 | return NULL; |
309 | return NULL; |
305 | |
310 | |
306 | c = t[len]; |
311 | c = t[len]; |
307 | t[len] = '\0'; |
312 | t[len] = '\0'; |
308 | s = xstrdup(t); |
313 | s = xstrdup(t); |
309 | t[len] = c; |
314 | t[len] = c; |
310 | return s; |
315 | return s; |
311 | } |
316 | } |
312 | |
317 | |
313 | char *strlpart(char *txt, int maxlen) |
318 | char *strlpart(char *txt, int maxlen) |
314 | { |
319 | { |
315 | char *result; |
320 | char *result; |
316 | |
321 | |
317 | if (!txt) |
322 | if (!txt) |
318 | return txt; |
323 | return txt; |
319 | |
324 | |
320 | if (strlen(txt) <= maxlen) |
325 | if (strlen(txt) <= maxlen) |
321 | return txt; |
326 | return txt; |
322 | result = xmalloc(maxlen + 1); |
327 | result = xmalloc(maxlen + 1); |
323 | memcpy(result, txt, maxlen - 3); |
328 | memcpy(result, txt, maxlen - 3); |
324 | result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; |
329 | result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; |
325 | result[maxlen] = '\0'; |
330 | result[maxlen] = '\0'; |
326 | return result; |
331 | return result; |
327 | } |
332 | } |
328 | |
333 | |
329 | char *strrpart(char *txt, int maxlen) |
334 | char *strrpart(char *txt, int maxlen) |
330 | { |
335 | { |
331 | char *result; |
336 | char *result; |
332 | |
337 | |
333 | if (!txt) |
338 | if (!txt) |
334 | return txt; |
339 | return txt; |
335 | |
340 | |
336 | if (strlen(txt) <= maxlen) |
341 | if (strlen(txt) <= maxlen) |
337 | return txt; |
342 | return txt; |
338 | result = xmalloc(maxlen + 1); |
343 | result = xmalloc(maxlen + 1); |
339 | memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); |
344 | memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); |
340 | result[0] = result[1] = result[2] = '.'; |
345 | result[0] = result[1] = result[2] = '.'; |
341 | return result; |
346 | return result; |
342 | } |
347 | } |
343 | |
348 | |
344 | void cgit_add_ref(struct reflist *list, struct refinfo *ref) |
349 | void cgit_add_ref(struct reflist *list, struct refinfo *ref) |
345 | { |
350 | { |
346 | size_t size; |
351 | size_t size; |
347 | |
352 | |
348 | if (list->count >= list->alloc) { |
353 | if (list->count >= list->alloc) { |
349 | list->alloc += (list->alloc ? list->alloc : 4); |
354 | list->alloc += (list->alloc ? list->alloc : 4); |
350 | size = list->alloc * sizeof(struct refinfo *); |
355 | size = list->alloc * sizeof(struct refinfo *); |
351 | list->refs = xrealloc(list->refs, size); |
356 | list->refs = xrealloc(list->refs, size); |
352 | } |
357 | } |
353 | list->refs[list->count++] = ref; |
358 | list->refs[list->count++] = ref; |
354 | } |
359 | } |
355 | |
360 | |
356 | struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) |
361 | struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) |
357 | { |
362 | { |
358 | struct refinfo *ref; |
363 | struct refinfo *ref; |
359 | |
364 | |
360 | ref = xmalloc(sizeof (struct refinfo)); |
365 | ref = xmalloc(sizeof (struct refinfo)); |
361 | ref->refname = xstrdup(refname); |
366 | ref->refname = xstrdup(refname); |
362 | ref->object = parse_object(sha1); |
367 | ref->object = parse_object(sha1); |
363 | switch (ref->object->type) { |
368 | switch (ref->object->type) { |
364 | case OBJ_TAG: |
369 | case OBJ_TAG: |
365 | ref->tag = cgit_parse_tag((struct tag *)ref->object); |
370 | ref->tag = cgit_parse_tag((struct tag *)ref->object); |
366 | break; |
371 | break; |
367 | case OBJ_COMMIT: |
372 | case OBJ_COMMIT: |
368 | ref->commit = cgit_parse_commit((struct commit *)ref->object); |
373 | ref->commit = cgit_parse_commit((struct commit *)ref->object); |
369 | break; |
374 | break; |
370 | } |
375 | } |
371 | return ref; |
376 | return ref; |
372 | } |
377 | } |
373 | |
378 | |
374 | int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags, |
379 | int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags, |
375 | void *cb_data) |
380 | void *cb_data) |
376 | { |
381 | { |
377 | struct reflist *list = (struct reflist *)cb_data; |
382 | struct reflist *list = (struct reflist *)cb_data; |
378 | struct refinfo *info = cgit_mk_refinfo(refname, sha1); |
383 | struct refinfo *info = cgit_mk_refinfo(refname, sha1); |
379 | |
384 | |
380 | if (info) |
385 | if (info) |
381 | cgit_add_ref(list, info); |
386 | cgit_add_ref(list, info); |
382 | return 0; |
387 | return 0; |
383 | } |
388 | } |
384 | |
389 | |
385 | void cgit_diff_tree_cb(struct diff_queue_struct *q, |
390 | void cgit_diff_tree_cb(struct diff_queue_struct *q, |
386 | struct diff_options *options, void *data) |
391 | struct diff_options *options, void *data) |
387 | { |
392 | { |
388 | int i; |
393 | int i; |
389 | |
394 | |
390 | for (i = 0; i < q->nr; i++) { |
395 | for (i = 0; i < q->nr; i++) { |
391 | if (q->queue[i]->status == 'U') |
396 | if (q->queue[i]->status == 'U') |
392 | continue; |
397 | continue; |
393 | ((filepair_fn)data)(q->queue[i]); |
398 | ((filepair_fn)data)(q->queue[i]); |
394 | } |
399 | } |
395 | } |
400 | } |
396 | |
401 | |
397 | static int load_mmfile(mmfile_t *file, const unsigned char *sha1) |
402 | static int load_mmfile(mmfile_t *file, const unsigned char *sha1) |
398 | { |
403 | { |
399 | enum object_type type; |
404 | enum object_type type; |
400 | |
405 | |
401 | if (is_null_sha1(sha1)) { |
406 | if (is_null_sha1(sha1)) { |
402 | file->ptr = (char *)""; |
407 | file->ptr = (char *)""; |
403 | file->size = 0; |
408 | file->size = 0; |
404 | } else { |
409 | } else { |
405 | file->ptr = read_sha1_file(sha1, &type, |
410 | file->ptr = read_sha1_file(sha1, &type, |
406 | (unsigned long *)&file->size); |
411 | (unsigned long *)&file->size); |
407 | } |
412 | } |
408 | return 1; |
413 | return 1; |
409 | } |
414 | } |
410 | |
415 | |
411 | /* |
416 | /* |
412 | * Receive diff-buffers from xdiff and concatenate them as |
417 | * Receive diff-buffers from xdiff and concatenate them as |
413 | * needed across multiple callbacks. |
418 | * needed across multiple callbacks. |
414 | * |
419 | * |
415 | * This is basically a copy of xdiff-interface.c/xdiff_outf(), |
420 | * This is basically a copy of xdiff-interface.c/xdiff_outf(), |
416 | * ripped from git and modified to use globals instead of |
421 | * ripped from git and modified to use globals instead of |
417 | * a special callback-struct. |
422 | * a special callback-struct. |
418 | */ |
423 | */ |
419 | char *diffbuf = NULL; |
424 | char *diffbuf = NULL; |
420 | int buflen = 0; |
425 | int buflen = 0; |
421 | |
426 | |
422 | int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) |
427 | int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) |
423 | { |
428 | { |
424 | int i; |
429 | int i; |
425 | |
430 | |
426 | for (i = 0; i < nbuf; i++) { |
431 | for (i = 0; i < nbuf; i++) { |
427 | if (mb[i].ptr[mb[i].size-1] != '\n') { |
432 | if (mb[i].ptr[mb[i].size-1] != '\n') { |
428 | /* Incomplete line */ |
433 | /* Incomplete line */ |
429 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); |
434 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); |
430 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); |
435 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); |
431 | buflen += mb[i].size; |
436 | buflen += mb[i].size; |
432 | continue; |
437 | continue; |
433 | } |
438 | } |
434 | |
439 | |
435 | /* we have a complete line */ |
440 | /* we have a complete line */ |
436 | if (!diffbuf) { |
441 | if (!diffbuf) { |
437 | ((linediff_fn)priv)(mb[i].ptr, mb[i].size); |
442 | ((linediff_fn)priv)(mb[i].ptr, mb[i].size); |
438 | continue; |
443 | continue; |
439 | } |
444 | } |
440 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); |
445 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); |
441 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); |
446 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); |
442 | ((linediff_fn)priv)(diffbuf, buflen + mb[i].size); |
447 | ((linediff_fn)priv)(diffbuf, buflen + mb[i].size); |
443 | free(diffbuf); |
448 | free(diffbuf); |
444 | diffbuf = NULL; |
449 | diffbuf = NULL; |
445 | buflen = 0; |
450 | buflen = 0; |
446 | } |
451 | } |
447 | if (diffbuf) { |
452 | if (diffbuf) { |
448 | ((linediff_fn)priv)(diffbuf, buflen); |
453 | ((linediff_fn)priv)(diffbuf, buflen); |
449 | free(diffbuf); |
454 | free(diffbuf); |
450 | diffbuf = NULL; |
455 | diffbuf = NULL; |
451 | buflen = 0; |
456 | buflen = 0; |
452 | } |
457 | } |
453 | return 0; |
458 | return 0; |
454 | } |
459 | } |
455 | |
460 | |
456 | int cgit_diff_files(const unsigned char *old_sha1, |
461 | int cgit_diff_files(const unsigned char *old_sha1, |
457 | const unsigned char *new_sha1, |
462 | const unsigned char *new_sha1, |
458 | linediff_fn fn) |
463 | linediff_fn fn) |
459 | { |
464 | { |
460 | mmfile_t file1, file2; |
465 | mmfile_t file1, file2; |
461 | xpparam_t diff_params; |
466 | xpparam_t diff_params; |
462 | xdemitconf_t emit_params; |
467 | xdemitconf_t emit_params; |
463 | xdemitcb_t emit_cb; |
468 | xdemitcb_t emit_cb; |
464 | |
469 | |
465 | if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1)) |
470 | if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1)) |
466 | return 1; |
471 | return 1; |
467 | |
472 | |
468 | diff_params.flags = XDF_NEED_MINIMAL; |
473 | diff_params.flags = XDF_NEED_MINIMAL; |
469 | emit_params.ctxlen = 3; |
474 | emit_params.ctxlen = 3; |
470 | emit_params.flags = XDL_EMIT_FUNCNAMES; |
475 | emit_params.flags = XDL_EMIT_FUNCNAMES; |
471 | emit_params.find_func = NULL; |
476 | emit_params.find_func = NULL; |
472 | emit_cb.outf = filediff_cb; |
477 | emit_cb.outf = filediff_cb; |
473 | emit_cb.priv = fn; |
478 | emit_cb.priv = fn; |
474 | xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb); |
479 | xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb); |
475 | return 0; |
480 | return 0; |
476 | } |
481 | } |
477 | |
482 | |
478 | void cgit_diff_tree(const unsigned char *old_sha1, |
483 | void cgit_diff_tree(const unsigned char *old_sha1, |
479 | const unsigned char *new_sha1, |
484 | const unsigned char *new_sha1, |
480 | filepair_fn fn, const char *prefix) |
485 | filepair_fn fn, const char *prefix) |
481 | { |
486 | { |
482 | struct diff_options opt; |
487 | struct diff_options opt; |
483 | int ret; |
488 | int ret; |
484 | int prefixlen; |
489 | int prefixlen; |
485 | |
490 | |
486 | diff_setup(&opt); |
491 | diff_setup(&opt); |
487 | opt.output_format = DIFF_FORMAT_CALLBACK; |
492 | opt.output_format = DIFF_FORMAT_CALLBACK; |
488 | opt.detect_rename = 1; |
493 | opt.detect_rename = 1; |
489 | opt.rename_limit = cgit_renamelimit; |
494 | opt.rename_limit = cgit_renamelimit; |
490 | opt.recursive = 1; |
495 | opt.recursive = 1; |
491 | opt.format_callback = cgit_diff_tree_cb; |
496 | opt.format_callback = cgit_diff_tree_cb; |
492 | opt.format_callback_data = fn; |
497 | opt.format_callback_data = fn; |
493 | if (prefix) { |
498 | if (prefix) { |
494 | opt.nr_paths = 1; |
499 | opt.nr_paths = 1; |
495 | opt.paths = &prefix; |
500 | opt.paths = &prefix; |
496 | prefixlen = strlen(prefix); |
501 | prefixlen = strlen(prefix); |
497 | opt.pathlens = &prefixlen; |
502 | opt.pathlens = &prefixlen; |
498 | } |
503 | } |
499 | diff_setup_done(&opt); |
504 | diff_setup_done(&opt); |
500 | |
505 | |
501 | if (old_sha1 && !is_null_sha1(old_sha1)) |
506 | if (old_sha1 && !is_null_sha1(old_sha1)) |
502 | ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt); |
507 | ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt); |
503 | else |
508 | else |
504 | ret = diff_root_tree_sha1(new_sha1, "", &opt); |
509 | ret = diff_root_tree_sha1(new_sha1, "", &opt); |
505 | diffcore_std(&opt); |
510 | diffcore_std(&opt); |
506 | diff_flush(&opt); |
511 | diff_flush(&opt); |
507 | } |
512 | } |
508 | |
513 | |
509 | void cgit_diff_commit(struct commit *commit, filepair_fn fn) |
514 | void cgit_diff_commit(struct commit *commit, filepair_fn fn) |
510 | { |
515 | { |
511 | unsigned char *old_sha1 = NULL; |
516 | unsigned char *old_sha1 = NULL; |
512 | |
517 | |
513 | if (commit->parents) |
518 | if (commit->parents) |
514 | old_sha1 = commit->parents->item->object.sha1; |
519 | old_sha1 = commit->parents->item->object.sha1; |
515 | cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL); |
520 | cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL); |
516 | } |
521 | } |
|