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