author | Lars Hjemli <hjemli@gmail.com> | 2008-02-23 21:45:33 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-03-18 07:13:10 (UTC) |
commit | b1f9b9c1459cb9a30ebf80721aff6ef788d1f891 (patch) (unidiff) | |
tree | 05796a741faef90c12aadd3a5c92b702ec870c48 /shared.c | |
parent | b88fb016d0209f7041ac7d3b4d2c077318407a4d (diff) | |
download | cgit-b1f9b9c1459cb9a30ebf80721aff6ef788d1f891.zip cgit-b1f9b9c1459cb9a30ebf80721aff6ef788d1f891.tar.gz cgit-b1f9b9c1459cb9a30ebf80721aff6ef788d1f891.tar.bz2 |
Introduce html.h
All html-functions can be quite easily separated from the rest of cgit, so
lets do it; the only issue was html_filemode which uses some git-defined
macros so the function is moved into ui-shared.c::cgit_print_filemode().
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | shared.c | 2 |
1 files changed, 0 insertions, 2 deletions
@@ -1,402 +1,400 @@ | |||
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 | int htmlfd = 0; | ||
18 | |||
19 | void cgit_prepare_context(struct cgit_context *ctx) | 17 | void cgit_prepare_context(struct cgit_context *ctx) |
20 | { | 18 | { |
21 | memset(ctx, 0, sizeof(ctx)); | 19 | memset(ctx, 0, sizeof(ctx)); |
22 | ctx->cfg.agefile = "info/web/last-modified"; | 20 | ctx->cfg.agefile = "info/web/last-modified"; |
23 | ctx->cfg.cache_dynamic_ttl = 5; | 21 | ctx->cfg.cache_dynamic_ttl = 5; |
24 | ctx->cfg.cache_max_create_time = 5; | 22 | ctx->cfg.cache_max_create_time = 5; |
25 | ctx->cfg.cache_repo_ttl = 5; | 23 | ctx->cfg.cache_repo_ttl = 5; |
26 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; | 24 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; |
27 | ctx->cfg.cache_root_ttl = 5; | 25 | ctx->cfg.cache_root_ttl = 5; |
28 | ctx->cfg.cache_static_ttl = -1; | 26 | ctx->cfg.cache_static_ttl = -1; |
29 | ctx->cfg.css = "/cgit.css"; | 27 | ctx->cfg.css = "/cgit.css"; |
30 | ctx->cfg.logo = "/git-logo.png"; | 28 | ctx->cfg.logo = "/git-logo.png"; |
31 | ctx->cfg.max_commit_count = 50; | 29 | ctx->cfg.max_commit_count = 50; |
32 | ctx->cfg.max_lock_attempts = 5; | 30 | ctx->cfg.max_lock_attempts = 5; |
33 | ctx->cfg.max_msg_len = 60; | 31 | ctx->cfg.max_msg_len = 60; |
34 | ctx->cfg.max_repodesc_len = 60; | 32 | ctx->cfg.max_repodesc_len = 60; |
35 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; | 33 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; |
36 | ctx->cfg.renamelimit = -1; | 34 | ctx->cfg.renamelimit = -1; |
37 | ctx->cfg.robots = "index, nofollow"; | 35 | ctx->cfg.robots = "index, nofollow"; |
38 | ctx->cfg.root_title = "Git repository browser"; | 36 | ctx->cfg.root_title = "Git repository browser"; |
39 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; | 37 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; |
40 | } | 38 | } |
41 | 39 | ||
42 | int cgit_get_cmd_index(const char *cmd) | 40 | int cgit_get_cmd_index(const char *cmd) |
43 | { | 41 | { |
44 | static char *cmds[] = {"log", "commit", "diff", "tree", "blob", | 42 | static char *cmds[] = {"log", "commit", "diff", "tree", "blob", |
45 | "snapshot", "tag", "refs", "patch", NULL}; | 43 | "snapshot", "tag", "refs", "patch", NULL}; |
46 | int i; | 44 | int i; |
47 | 45 | ||
48 | for(i = 0; cmds[i]; i++) | 46 | for(i = 0; cmds[i]; i++) |
49 | if (!strcmp(cmd, cmds[i])) | 47 | if (!strcmp(cmd, cmds[i])) |
50 | return i + 1; | 48 | return i + 1; |
51 | return 0; | 49 | return 0; |
52 | } | 50 | } |
53 | 51 | ||
54 | int chk_zero(int result, char *msg) | 52 | int chk_zero(int result, char *msg) |
55 | { | 53 | { |
56 | if (result != 0) | 54 | if (result != 0) |
57 | die("%s: %s", msg, strerror(errno)); | 55 | die("%s: %s", msg, strerror(errno)); |
58 | return result; | 56 | return result; |
59 | } | 57 | } |
60 | 58 | ||
61 | int chk_positive(int result, char *msg) | 59 | int chk_positive(int result, char *msg) |
62 | { | 60 | { |
63 | if (result <= 0) | 61 | if (result <= 0) |
64 | die("%s: %s", msg, strerror(errno)); | 62 | die("%s: %s", msg, strerror(errno)); |
65 | return result; | 63 | return result; |
66 | } | 64 | } |
67 | 65 | ||
68 | int chk_non_negative(int result, char *msg) | 66 | int chk_non_negative(int result, char *msg) |
69 | { | 67 | { |
70 | if (result < 0) | 68 | if (result < 0) |
71 | die("%s: %s",msg, strerror(errno)); | 69 | die("%s: %s",msg, strerror(errno)); |
72 | return result; | 70 | return result; |
73 | } | 71 | } |
74 | 72 | ||
75 | struct cgit_repo *add_repo(const char *url) | 73 | struct cgit_repo *add_repo(const char *url) |
76 | { | 74 | { |
77 | struct cgit_repo *ret; | 75 | struct cgit_repo *ret; |
78 | 76 | ||
79 | if (++cgit_repolist.count > cgit_repolist.length) { | 77 | if (++cgit_repolist.count > cgit_repolist.length) { |
80 | if (cgit_repolist.length == 0) | 78 | if (cgit_repolist.length == 0) |
81 | cgit_repolist.length = 8; | 79 | cgit_repolist.length = 8; |
82 | else | 80 | else |
83 | cgit_repolist.length *= 2; | 81 | cgit_repolist.length *= 2; |
84 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, | 82 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, |
85 | cgit_repolist.length * | 83 | cgit_repolist.length * |
86 | sizeof(struct cgit_repo)); | 84 | sizeof(struct cgit_repo)); |
87 | } | 85 | } |
88 | 86 | ||
89 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; | 87 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; |
90 | ret->url = trim_end(url, '/'); | 88 | ret->url = trim_end(url, '/'); |
91 | ret->name = ret->url; | 89 | ret->name = ret->url; |
92 | ret->path = NULL; | 90 | ret->path = NULL; |
93 | ret->desc = "[no description]"; | 91 | ret->desc = "[no description]"; |
94 | ret->owner = NULL; | 92 | ret->owner = NULL; |
95 | ret->group = ctx.cfg.repo_group; | 93 | ret->group = ctx.cfg.repo_group; |
96 | ret->defbranch = "master"; | 94 | ret->defbranch = "master"; |
97 | ret->snapshots = ctx.cfg.snapshots; | 95 | ret->snapshots = ctx.cfg.snapshots; |
98 | ret->enable_log_filecount = ctx.cfg.enable_log_filecount; | 96 | ret->enable_log_filecount = ctx.cfg.enable_log_filecount; |
99 | ret->enable_log_linecount = ctx.cfg.enable_log_linecount; | 97 | ret->enable_log_linecount = ctx.cfg.enable_log_linecount; |
100 | ret->module_link = ctx.cfg.module_link; | 98 | ret->module_link = ctx.cfg.module_link; |
101 | ret->readme = NULL; | 99 | ret->readme = NULL; |
102 | return ret; | 100 | return ret; |
103 | } | 101 | } |
104 | 102 | ||
105 | struct cgit_repo *cgit_get_repoinfo(const char *url) | 103 | struct cgit_repo *cgit_get_repoinfo(const char *url) |
106 | { | 104 | { |
107 | int i; | 105 | int i; |
108 | struct cgit_repo *repo; | 106 | struct cgit_repo *repo; |
109 | 107 | ||
110 | for (i=0; i<cgit_repolist.count; i++) { | 108 | for (i=0; i<cgit_repolist.count; i++) { |
111 | repo = &cgit_repolist.repos[i]; | 109 | repo = &cgit_repolist.repos[i]; |
112 | if (!strcmp(repo->url, url)) | 110 | if (!strcmp(repo->url, url)) |
113 | return repo; | 111 | return repo; |
114 | } | 112 | } |
115 | return NULL; | 113 | return NULL; |
116 | } | 114 | } |
117 | 115 | ||
118 | void cgit_global_config_cb(const char *name, const char *value) | 116 | void cgit_global_config_cb(const char *name, const char *value) |
119 | { | 117 | { |
120 | if (!strcmp(name, "root-title")) | 118 | if (!strcmp(name, "root-title")) |
121 | ctx.cfg.root_title = xstrdup(value); | 119 | ctx.cfg.root_title = xstrdup(value); |
122 | else if (!strcmp(name, "css")) | 120 | else if (!strcmp(name, "css")) |
123 | ctx.cfg.css = xstrdup(value); | 121 | ctx.cfg.css = xstrdup(value); |
124 | else if (!strcmp(name, "logo")) | 122 | else if (!strcmp(name, "logo")) |
125 | ctx.cfg.logo = xstrdup(value); | 123 | ctx.cfg.logo = xstrdup(value); |
126 | else if (!strcmp(name, "index-header")) | 124 | else if (!strcmp(name, "index-header")) |
127 | ctx.cfg.index_header = xstrdup(value); | 125 | ctx.cfg.index_header = xstrdup(value); |
128 | else if (!strcmp(name, "index-info")) | 126 | else if (!strcmp(name, "index-info")) |
129 | ctx.cfg.index_info = xstrdup(value); | 127 | ctx.cfg.index_info = xstrdup(value); |
130 | else if (!strcmp(name, "logo-link")) | 128 | else if (!strcmp(name, "logo-link")) |
131 | ctx.cfg.logo_link = xstrdup(value); | 129 | ctx.cfg.logo_link = xstrdup(value); |
132 | else if (!strcmp(name, "module-link")) | 130 | else if (!strcmp(name, "module-link")) |
133 | ctx.cfg.module_link = xstrdup(value); | 131 | ctx.cfg.module_link = xstrdup(value); |
134 | else if (!strcmp(name, "virtual-root")) { | 132 | else if (!strcmp(name, "virtual-root")) { |
135 | ctx.cfg.virtual_root = trim_end(value, '/'); | 133 | ctx.cfg.virtual_root = trim_end(value, '/'); |
136 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) | 134 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) |
137 | ctx.cfg.virtual_root = ""; | 135 | ctx.cfg.virtual_root = ""; |
138 | } else if (!strcmp(name, "nocache")) | 136 | } else if (!strcmp(name, "nocache")) |
139 | ctx.cfg.nocache = atoi(value); | 137 | ctx.cfg.nocache = atoi(value); |
140 | else if (!strcmp(name, "snapshots")) | 138 | else if (!strcmp(name, "snapshots")) |
141 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); | 139 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); |
142 | else if (!strcmp(name, "enable-index-links")) | 140 | else if (!strcmp(name, "enable-index-links")) |
143 | ctx.cfg.enable_index_links = atoi(value); | 141 | ctx.cfg.enable_index_links = atoi(value); |
144 | else if (!strcmp(name, "enable-log-filecount")) | 142 | else if (!strcmp(name, "enable-log-filecount")) |
145 | ctx.cfg.enable_log_filecount = atoi(value); | 143 | ctx.cfg.enable_log_filecount = atoi(value); |
146 | else if (!strcmp(name, "enable-log-linecount")) | 144 | else if (!strcmp(name, "enable-log-linecount")) |
147 | ctx.cfg.enable_log_linecount = atoi(value); | 145 | ctx.cfg.enable_log_linecount = atoi(value); |
148 | else if (!strcmp(name, "cache-root")) | 146 | else if (!strcmp(name, "cache-root")) |
149 | ctx.cfg.cache_root = xstrdup(value); | 147 | ctx.cfg.cache_root = xstrdup(value); |
150 | else if (!strcmp(name, "cache-root-ttl")) | 148 | else if (!strcmp(name, "cache-root-ttl")) |
151 | ctx.cfg.cache_root_ttl = atoi(value); | 149 | ctx.cfg.cache_root_ttl = atoi(value); |
152 | else if (!strcmp(name, "cache-repo-ttl")) | 150 | else if (!strcmp(name, "cache-repo-ttl")) |
153 | ctx.cfg.cache_repo_ttl = atoi(value); | 151 | ctx.cfg.cache_repo_ttl = atoi(value); |
154 | else if (!strcmp(name, "cache-static-ttl")) | 152 | else if (!strcmp(name, "cache-static-ttl")) |
155 | ctx.cfg.cache_static_ttl = atoi(value); | 153 | ctx.cfg.cache_static_ttl = atoi(value); |
156 | else if (!strcmp(name, "cache-dynamic-ttl")) | 154 | else if (!strcmp(name, "cache-dynamic-ttl")) |
157 | ctx.cfg.cache_dynamic_ttl = atoi(value); | 155 | ctx.cfg.cache_dynamic_ttl = atoi(value); |
158 | else if (!strcmp(name, "max-message-length")) | 156 | else if (!strcmp(name, "max-message-length")) |
159 | ctx.cfg.max_msg_len = atoi(value); | 157 | ctx.cfg.max_msg_len = atoi(value); |
160 | else if (!strcmp(name, "max-repodesc-length")) | 158 | else if (!strcmp(name, "max-repodesc-length")) |
161 | ctx.cfg.max_repodesc_len = atoi(value); | 159 | ctx.cfg.max_repodesc_len = atoi(value); |
162 | else if (!strcmp(name, "max-commit-count")) | 160 | else if (!strcmp(name, "max-commit-count")) |
163 | ctx.cfg.max_commit_count = atoi(value); | 161 | ctx.cfg.max_commit_count = atoi(value); |
164 | else if (!strcmp(name, "summary-log")) | 162 | else if (!strcmp(name, "summary-log")) |
165 | ctx.cfg.summary_log = atoi(value); | 163 | ctx.cfg.summary_log = atoi(value); |
166 | else if (!strcmp(name, "summary-branches")) | 164 | else if (!strcmp(name, "summary-branches")) |
167 | ctx.cfg.summary_branches = atoi(value); | 165 | ctx.cfg.summary_branches = atoi(value); |
168 | else if (!strcmp(name, "summary-tags")) | 166 | else if (!strcmp(name, "summary-tags")) |
169 | ctx.cfg.summary_tags = atoi(value); | 167 | ctx.cfg.summary_tags = atoi(value); |
170 | else if (!strcmp(name, "agefile")) | 168 | else if (!strcmp(name, "agefile")) |
171 | ctx.cfg.agefile = xstrdup(value); | 169 | ctx.cfg.agefile = xstrdup(value); |
172 | else if (!strcmp(name, "renamelimit")) | 170 | else if (!strcmp(name, "renamelimit")) |
173 | ctx.cfg.renamelimit = atoi(value); | 171 | ctx.cfg.renamelimit = atoi(value); |
174 | else if (!strcmp(name, "robots")) | 172 | else if (!strcmp(name, "robots")) |
175 | ctx.cfg.robots = xstrdup(value); | 173 | ctx.cfg.robots = xstrdup(value); |
176 | else if (!strcmp(name, "clone-prefix")) | 174 | else if (!strcmp(name, "clone-prefix")) |
177 | ctx.cfg.clone_prefix = xstrdup(value); | 175 | ctx.cfg.clone_prefix = xstrdup(value); |
178 | else if (!strcmp(name, "repo.group")) | 176 | else if (!strcmp(name, "repo.group")) |
179 | ctx.cfg.repo_group = xstrdup(value); | 177 | ctx.cfg.repo_group = xstrdup(value); |
180 | else if (!strcmp(name, "repo.url")) | 178 | else if (!strcmp(name, "repo.url")) |
181 | ctx.repo = add_repo(value); | 179 | ctx.repo = add_repo(value); |
182 | else if (!strcmp(name, "repo.name")) | 180 | else if (!strcmp(name, "repo.name")) |
183 | ctx.repo->name = xstrdup(value); | 181 | ctx.repo->name = xstrdup(value); |
184 | else if (ctx.repo && !strcmp(name, "repo.path")) | 182 | else if (ctx.repo && !strcmp(name, "repo.path")) |
185 | ctx.repo->path = trim_end(value, '/'); | 183 | ctx.repo->path = trim_end(value, '/'); |
186 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) | 184 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) |
187 | ctx.repo->clone_url = xstrdup(value); | 185 | ctx.repo->clone_url = xstrdup(value); |
188 | else if (ctx.repo && !strcmp(name, "repo.desc")) | 186 | else if (ctx.repo && !strcmp(name, "repo.desc")) |
189 | ctx.repo->desc = xstrdup(value); | 187 | ctx.repo->desc = xstrdup(value); |
190 | else if (ctx.repo && !strcmp(name, "repo.owner")) | 188 | else if (ctx.repo && !strcmp(name, "repo.owner")) |
191 | ctx.repo->owner = xstrdup(value); | 189 | ctx.repo->owner = xstrdup(value); |
192 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) | 190 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) |
193 | ctx.repo->defbranch = xstrdup(value); | 191 | ctx.repo->defbranch = xstrdup(value); |
194 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) | 192 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) |
195 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ | 193 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ |
196 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) | 194 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) |
197 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); | 195 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); |
198 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) | 196 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) |
199 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); | 197 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); |
200 | else if (ctx.repo && !strcmp(name, "repo.module-link")) | 198 | else if (ctx.repo && !strcmp(name, "repo.module-link")) |
201 | ctx.repo->module_link= xstrdup(value); | 199 | ctx.repo->module_link= xstrdup(value); |
202 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { | 200 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { |
203 | if (*value == '/') | 201 | if (*value == '/') |
204 | ctx.repo->readme = xstrdup(value); | 202 | ctx.repo->readme = xstrdup(value); |
205 | else | 203 | else |
206 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); | 204 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); |
207 | } else if (!strcmp(name, "include")) | 205 | } else if (!strcmp(name, "include")) |
208 | cgit_read_config(value, cgit_global_config_cb); | 206 | cgit_read_config(value, cgit_global_config_cb); |
209 | } | 207 | } |
210 | 208 | ||
211 | void cgit_querystring_cb(const char *name, const char *value) | 209 | void cgit_querystring_cb(const char *name, const char *value) |
212 | { | 210 | { |
213 | if (!strcmp(name,"r")) { | 211 | if (!strcmp(name,"r")) { |
214 | ctx.qry.repo = xstrdup(value); | 212 | ctx.qry.repo = xstrdup(value); |
215 | ctx.repo = cgit_get_repoinfo(value); | 213 | ctx.repo = cgit_get_repoinfo(value); |
216 | } else if (!strcmp(name, "p")) { | 214 | } else if (!strcmp(name, "p")) { |
217 | ctx.qry.page = xstrdup(value); | 215 | ctx.qry.page = xstrdup(value); |
218 | cgit_cmd = cgit_get_cmd_index(value); | 216 | cgit_cmd = cgit_get_cmd_index(value); |
219 | } else if (!strcmp(name, "url")) { | 217 | } else if (!strcmp(name, "url")) { |
220 | cgit_parse_url(value); | 218 | cgit_parse_url(value); |
221 | } else if (!strcmp(name, "qt")) { | 219 | } else if (!strcmp(name, "qt")) { |
222 | ctx.qry.grep = xstrdup(value); | 220 | ctx.qry.grep = xstrdup(value); |
223 | } else if (!strcmp(name, "q")) { | 221 | } else if (!strcmp(name, "q")) { |
224 | ctx.qry.search = xstrdup(value); | 222 | ctx.qry.search = xstrdup(value); |
225 | } else if (!strcmp(name, "h")) { | 223 | } else if (!strcmp(name, "h")) { |
226 | ctx.qry.head = xstrdup(value); | 224 | ctx.qry.head = xstrdup(value); |
227 | ctx.qry.has_symref = 1; | 225 | ctx.qry.has_symref = 1; |
228 | } else if (!strcmp(name, "id")) { | 226 | } else if (!strcmp(name, "id")) { |
229 | ctx.qry.sha1 = xstrdup(value); | 227 | ctx.qry.sha1 = xstrdup(value); |
230 | ctx.qry.has_sha1 = 1; | 228 | ctx.qry.has_sha1 = 1; |
231 | } else if (!strcmp(name, "id2")) { | 229 | } else if (!strcmp(name, "id2")) { |
232 | ctx.qry.sha2 = xstrdup(value); | 230 | ctx.qry.sha2 = xstrdup(value); |
233 | ctx.qry.has_sha1 = 1; | 231 | ctx.qry.has_sha1 = 1; |
234 | } else if (!strcmp(name, "ofs")) { | 232 | } else if (!strcmp(name, "ofs")) { |
235 | ctx.qry.ofs = atoi(value); | 233 | ctx.qry.ofs = atoi(value); |
236 | } else if (!strcmp(name, "path")) { | 234 | } else if (!strcmp(name, "path")) { |
237 | ctx.qry.path = trim_end(value, '/'); | 235 | ctx.qry.path = trim_end(value, '/'); |
238 | } else if (!strcmp(name, "name")) { | 236 | } else if (!strcmp(name, "name")) { |
239 | ctx.qry.name = xstrdup(value); | 237 | ctx.qry.name = xstrdup(value); |
240 | } | 238 | } |
241 | } | 239 | } |
242 | 240 | ||
243 | void *cgit_free_commitinfo(struct commitinfo *info) | 241 | void *cgit_free_commitinfo(struct commitinfo *info) |
244 | { | 242 | { |
245 | free(info->author); | 243 | free(info->author); |
246 | free(info->author_email); | 244 | free(info->author_email); |
247 | free(info->committer); | 245 | free(info->committer); |
248 | free(info->committer_email); | 246 | free(info->committer_email); |
249 | free(info->subject); | 247 | free(info->subject); |
250 | free(info->msg); | 248 | free(info->msg); |
251 | free(info->msg_encoding); | 249 | free(info->msg_encoding); |
252 | free(info); | 250 | free(info); |
253 | return NULL; | 251 | return NULL; |
254 | } | 252 | } |
255 | 253 | ||
256 | int hextoint(char c) | 254 | int hextoint(char c) |
257 | { | 255 | { |
258 | if (c >= 'a' && c <= 'f') | 256 | if (c >= 'a' && c <= 'f') |
259 | return 10 + c - 'a'; | 257 | return 10 + c - 'a'; |
260 | else if (c >= 'A' && c <= 'F') | 258 | else if (c >= 'A' && c <= 'F') |
261 | return 10 + c - 'A'; | 259 | return 10 + c - 'A'; |
262 | else if (c >= '0' && c <= '9') | 260 | else if (c >= '0' && c <= '9') |
263 | return c - '0'; | 261 | return c - '0'; |
264 | else | 262 | else |
265 | return -1; | 263 | return -1; |
266 | } | 264 | } |
267 | 265 | ||
268 | char *trim_end(const char *str, char c) | 266 | char *trim_end(const char *str, char c) |
269 | { | 267 | { |
270 | int len; | 268 | int len; |
271 | char *s, *t; | 269 | char *s, *t; |
272 | 270 | ||
273 | if (str == NULL) | 271 | if (str == NULL) |
274 | return NULL; | 272 | return NULL; |
275 | t = (char *)str; | 273 | t = (char *)str; |
276 | len = strlen(t); | 274 | len = strlen(t); |
277 | while(len > 0 && t[len - 1] == c) | 275 | while(len > 0 && t[len - 1] == c) |
278 | len--; | 276 | len--; |
279 | 277 | ||
280 | if (len == 0) | 278 | if (len == 0) |
281 | return NULL; | 279 | return NULL; |
282 | 280 | ||
283 | c = t[len]; | 281 | c = t[len]; |
284 | t[len] = '\0'; | 282 | t[len] = '\0'; |
285 | s = xstrdup(t); | 283 | s = xstrdup(t); |
286 | t[len] = c; | 284 | t[len] = c; |
287 | return s; | 285 | return s; |
288 | } | 286 | } |
289 | 287 | ||
290 | char *strlpart(char *txt, int maxlen) | 288 | char *strlpart(char *txt, int maxlen) |
291 | { | 289 | { |
292 | char *result; | 290 | char *result; |
293 | 291 | ||
294 | if (!txt) | 292 | if (!txt) |
295 | return txt; | 293 | return txt; |
296 | 294 | ||
297 | if (strlen(txt) <= maxlen) | 295 | if (strlen(txt) <= maxlen) |
298 | return txt; | 296 | return txt; |
299 | result = xmalloc(maxlen + 1); | 297 | result = xmalloc(maxlen + 1); |
300 | memcpy(result, txt, maxlen - 3); | 298 | memcpy(result, txt, maxlen - 3); |
301 | result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; | 299 | result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; |
302 | result[maxlen] = '\0'; | 300 | result[maxlen] = '\0'; |
303 | return result; | 301 | return result; |
304 | } | 302 | } |
305 | 303 | ||
306 | char *strrpart(char *txt, int maxlen) | 304 | char *strrpart(char *txt, int maxlen) |
307 | { | 305 | { |
308 | char *result; | 306 | char *result; |
309 | 307 | ||
310 | if (!txt) | 308 | if (!txt) |
311 | return txt; | 309 | return txt; |
312 | 310 | ||
313 | if (strlen(txt) <= maxlen) | 311 | if (strlen(txt) <= maxlen) |
314 | return txt; | 312 | return txt; |
315 | result = xmalloc(maxlen + 1); | 313 | result = xmalloc(maxlen + 1); |
316 | memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); | 314 | memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); |
317 | result[0] = result[1] = result[2] = '.'; | 315 | result[0] = result[1] = result[2] = '.'; |
318 | return result; | 316 | return result; |
319 | } | 317 | } |
320 | 318 | ||
321 | void cgit_add_ref(struct reflist *list, struct refinfo *ref) | 319 | void cgit_add_ref(struct reflist *list, struct refinfo *ref) |
322 | { | 320 | { |
323 | size_t size; | 321 | size_t size; |
324 | 322 | ||
325 | if (list->count >= list->alloc) { | 323 | if (list->count >= list->alloc) { |
326 | list->alloc += (list->alloc ? list->alloc : 4); | 324 | list->alloc += (list->alloc ? list->alloc : 4); |
327 | size = list->alloc * sizeof(struct refinfo *); | 325 | size = list->alloc * sizeof(struct refinfo *); |
328 | list->refs = xrealloc(list->refs, size); | 326 | list->refs = xrealloc(list->refs, size); |
329 | } | 327 | } |
330 | list->refs[list->count++] = ref; | 328 | list->refs[list->count++] = ref; |
331 | } | 329 | } |
332 | 330 | ||
333 | struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) | 331 | struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) |
334 | { | 332 | { |
335 | struct refinfo *ref; | 333 | struct refinfo *ref; |
336 | 334 | ||
337 | ref = xmalloc(sizeof (struct refinfo)); | 335 | ref = xmalloc(sizeof (struct refinfo)); |
338 | ref->refname = xstrdup(refname); | 336 | ref->refname = xstrdup(refname); |
339 | ref->object = parse_object(sha1); | 337 | ref->object = parse_object(sha1); |
340 | switch (ref->object->type) { | 338 | switch (ref->object->type) { |
341 | case OBJ_TAG: | 339 | case OBJ_TAG: |
342 | ref->tag = cgit_parse_tag((struct tag *)ref->object); | 340 | ref->tag = cgit_parse_tag((struct tag *)ref->object); |
343 | break; | 341 | break; |
344 | case OBJ_COMMIT: | 342 | case OBJ_COMMIT: |
345 | ref->commit = cgit_parse_commit((struct commit *)ref->object); | 343 | ref->commit = cgit_parse_commit((struct commit *)ref->object); |
346 | break; | 344 | break; |
347 | } | 345 | } |
348 | return ref; | 346 | return ref; |
349 | } | 347 | } |
350 | 348 | ||
351 | int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags, | 349 | int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags, |
352 | void *cb_data) | 350 | void *cb_data) |
353 | { | 351 | { |
354 | struct reflist *list = (struct reflist *)cb_data; | 352 | struct reflist *list = (struct reflist *)cb_data; |
355 | struct refinfo *info = cgit_mk_refinfo(refname, sha1); | 353 | struct refinfo *info = cgit_mk_refinfo(refname, sha1); |
356 | 354 | ||
357 | if (info) | 355 | if (info) |
358 | cgit_add_ref(list, info); | 356 | cgit_add_ref(list, info); |
359 | return 0; | 357 | return 0; |
360 | } | 358 | } |
361 | 359 | ||
362 | void cgit_diff_tree_cb(struct diff_queue_struct *q, | 360 | void cgit_diff_tree_cb(struct diff_queue_struct *q, |
363 | struct diff_options *options, void *data) | 361 | struct diff_options *options, void *data) |
364 | { | 362 | { |
365 | int i; | 363 | int i; |
366 | 364 | ||
367 | for (i = 0; i < q->nr; i++) { | 365 | for (i = 0; i < q->nr; i++) { |
368 | if (q->queue[i]->status == 'U') | 366 | if (q->queue[i]->status == 'U') |
369 | continue; | 367 | continue; |
370 | ((filepair_fn)data)(q->queue[i]); | 368 | ((filepair_fn)data)(q->queue[i]); |
371 | } | 369 | } |
372 | } | 370 | } |
373 | 371 | ||
374 | static int load_mmfile(mmfile_t *file, const unsigned char *sha1) | 372 | static int load_mmfile(mmfile_t *file, const unsigned char *sha1) |
375 | { | 373 | { |
376 | enum object_type type; | 374 | enum object_type type; |
377 | 375 | ||
378 | if (is_null_sha1(sha1)) { | 376 | if (is_null_sha1(sha1)) { |
379 | file->ptr = (char *)""; | 377 | file->ptr = (char *)""; |
380 | file->size = 0; | 378 | file->size = 0; |
381 | } else { | 379 | } else { |
382 | file->ptr = read_sha1_file(sha1, &type, | 380 | file->ptr = read_sha1_file(sha1, &type, |
383 | (unsigned long *)&file->size); | 381 | (unsigned long *)&file->size); |
384 | } | 382 | } |
385 | return 1; | 383 | return 1; |
386 | } | 384 | } |
387 | 385 | ||
388 | /* | 386 | /* |
389 | * Receive diff-buffers from xdiff and concatenate them as | 387 | * Receive diff-buffers from xdiff and concatenate them as |
390 | * needed across multiple callbacks. | 388 | * needed across multiple callbacks. |
391 | * | 389 | * |
392 | * This is basically a copy of xdiff-interface.c/xdiff_outf(), | 390 | * This is basically a copy of xdiff-interface.c/xdiff_outf(), |
393 | * ripped from git and modified to use globals instead of | 391 | * ripped from git and modified to use globals instead of |
394 | * a special callback-struct. | 392 | * a special callback-struct. |
395 | */ | 393 | */ |
396 | char *diffbuf = NULL; | 394 | char *diffbuf = NULL; |
397 | int buflen = 0; | 395 | int buflen = 0; |
398 | 396 | ||
399 | int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) | 397 | int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) |
400 | { | 398 | { |
401 | int i; | 399 | int i; |
402 | 400 | ||