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