author | Lars Hjemli <hjemli@gmail.com> | 2008-02-16 12:56:09 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-02-16 12:56:09 (UTC) |
commit | d1f3bbe9d22029f45a77bb938c176ccc0c827d46 (patch) (unidiff) | |
tree | 3f1741c012763cbc5485f31377abdd9241fbac6b /shared.c | |
parent | b228d4ff82a65fdcd4a7364759fe36a0bdda5978 (diff) | |
download | cgit-d1f3bbe9d22029f45a77bb938c176ccc0c827d46.zip cgit-d1f3bbe9d22029f45a77bb938c176ccc0c827d46.tar.gz cgit-d1f3bbe9d22029f45a77bb938c176ccc0c827d46.tar.bz2 |
Move cgit_repo into cgit_context
This removes the global variable which is used to keep track of the
currently selected repository, and adds a new variable in the cgit_context
structure.
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | shared.c | 61 |
1 files changed, 30 insertions, 31 deletions
@@ -1,344 +1,343 @@ | |||
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 cgit_repolist cgit_repolist; |
12 | struct repoinfo *cgit_repo; | ||
13 | struct cgit_context ctx; | 12 | struct cgit_context ctx; |
14 | int cgit_cmd; | 13 | int cgit_cmd; |
15 | 14 | ||
16 | const char *cgit_version = CGIT_VERSION; | 15 | const char *cgit_version = CGIT_VERSION; |
17 | 16 | ||
18 | int htmlfd = 0; | 17 | int htmlfd = 0; |
19 | 18 | ||
20 | void cgit_prepare_context(struct cgit_context *ctx) | 19 | void cgit_prepare_context(struct cgit_context *ctx) |
21 | { | 20 | { |
22 | memset(ctx, 0, sizeof(ctx)); | 21 | memset(ctx, 0, sizeof(ctx)); |
23 | ctx->cfg.agefile = "info/web/last-modified"; | 22 | ctx->cfg.agefile = "info/web/last-modified"; |
24 | ctx->cfg.cache_dynamic_ttl = 5; | 23 | ctx->cfg.cache_dynamic_ttl = 5; |
25 | ctx->cfg.cache_max_create_time = 5; | 24 | ctx->cfg.cache_max_create_time = 5; |
26 | ctx->cfg.cache_repo_ttl = 5; | 25 | ctx->cfg.cache_repo_ttl = 5; |
27 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; | 26 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; |
28 | ctx->cfg.cache_root_ttl = 5; | 27 | ctx->cfg.cache_root_ttl = 5; |
29 | ctx->cfg.cache_static_ttl = -1; | 28 | ctx->cfg.cache_static_ttl = -1; |
30 | ctx->cfg.css = "/cgit.css"; | 29 | ctx->cfg.css = "/cgit.css"; |
31 | ctx->cfg.logo = "/git-logo.png"; | 30 | ctx->cfg.logo = "/git-logo.png"; |
32 | ctx->cfg.max_commit_count = 50; | 31 | ctx->cfg.max_commit_count = 50; |
33 | ctx->cfg.max_lock_attempts = 5; | 32 | ctx->cfg.max_lock_attempts = 5; |
34 | ctx->cfg.max_msg_len = 60; | 33 | ctx->cfg.max_msg_len = 60; |
35 | ctx->cfg.max_repodesc_len = 60; | 34 | ctx->cfg.max_repodesc_len = 60; |
36 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; | 35 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; |
37 | ctx->cfg.renamelimit = -1; | 36 | ctx->cfg.renamelimit = -1; |
38 | ctx->cfg.robots = "index, nofollow"; | 37 | ctx->cfg.robots = "index, nofollow"; |
39 | ctx->cfg.root_title = "Git repository browser"; | 38 | ctx->cfg.root_title = "Git repository browser"; |
40 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; | 39 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; |
41 | } | 40 | } |
42 | 41 | ||
43 | int cgit_get_cmd_index(const char *cmd) | 42 | int cgit_get_cmd_index(const char *cmd) |
44 | { | 43 | { |
45 | static char *cmds[] = {"log", "commit", "diff", "tree", "blob", | 44 | static char *cmds[] = {"log", "commit", "diff", "tree", "blob", |
46 | "snapshot", "tag", "refs", "patch", NULL}; | 45 | "snapshot", "tag", "refs", "patch", NULL}; |
47 | int i; | 46 | int i; |
48 | 47 | ||
49 | for(i = 0; cmds[i]; i++) | 48 | for(i = 0; cmds[i]; i++) |
50 | if (!strcmp(cmd, cmds[i])) | 49 | if (!strcmp(cmd, cmds[i])) |
51 | return i + 1; | 50 | return i + 1; |
52 | return 0; | 51 | return 0; |
53 | } | 52 | } |
54 | 53 | ||
55 | int chk_zero(int result, char *msg) | 54 | int chk_zero(int result, char *msg) |
56 | { | 55 | { |
57 | if (result != 0) | 56 | if (result != 0) |
58 | die("%s: %s", msg, strerror(errno)); | 57 | die("%s: %s", msg, strerror(errno)); |
59 | return result; | 58 | return result; |
60 | } | 59 | } |
61 | 60 | ||
62 | int chk_positive(int result, char *msg) | 61 | int chk_positive(int result, char *msg) |
63 | { | 62 | { |
64 | if (result <= 0) | 63 | if (result <= 0) |
65 | die("%s: %s", msg, strerror(errno)); | 64 | die("%s: %s", msg, strerror(errno)); |
66 | return result; | 65 | return result; |
67 | } | 66 | } |
68 | 67 | ||
69 | int chk_non_negative(int result, char *msg) | 68 | int chk_non_negative(int result, char *msg) |
70 | { | 69 | { |
71 | if (result < 0) | 70 | if (result < 0) |
72 | die("%s: %s",msg, strerror(errno)); | 71 | die("%s: %s",msg, strerror(errno)); |
73 | return result; | 72 | return result; |
74 | } | 73 | } |
75 | 74 | ||
76 | struct repoinfo *add_repo(const char *url) | 75 | struct cgit_repo *add_repo(const char *url) |
77 | { | 76 | { |
78 | struct repoinfo *ret; | 77 | struct cgit_repo *ret; |
79 | 78 | ||
80 | if (++cgit_repolist.count > cgit_repolist.length) { | 79 | if (++cgit_repolist.count > cgit_repolist.length) { |
81 | if (cgit_repolist.length == 0) | 80 | if (cgit_repolist.length == 0) |
82 | cgit_repolist.length = 8; | 81 | cgit_repolist.length = 8; |
83 | else | 82 | else |
84 | cgit_repolist.length *= 2; | 83 | cgit_repolist.length *= 2; |
85 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, | 84 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, |
86 | cgit_repolist.length * | 85 | cgit_repolist.length * |
87 | sizeof(struct repoinfo)); | 86 | sizeof(struct cgit_repo)); |
88 | } | 87 | } |
89 | 88 | ||
90 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; | 89 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; |
91 | ret->url = trim_end(url, '/'); | 90 | ret->url = trim_end(url, '/'); |
92 | ret->name = ret->url; | 91 | ret->name = ret->url; |
93 | ret->path = NULL; | 92 | ret->path = NULL; |
94 | ret->desc = "[no description]"; | 93 | ret->desc = "[no description]"; |
95 | ret->owner = NULL; | 94 | ret->owner = NULL; |
96 | ret->group = ctx.cfg.repo_group; | 95 | ret->group = ctx.cfg.repo_group; |
97 | ret->defbranch = "master"; | 96 | ret->defbranch = "master"; |
98 | ret->snapshots = ctx.cfg.snapshots; | 97 | ret->snapshots = ctx.cfg.snapshots; |
99 | ret->enable_log_filecount = ctx.cfg.enable_log_filecount; | 98 | ret->enable_log_filecount = ctx.cfg.enable_log_filecount; |
100 | ret->enable_log_linecount = ctx.cfg.enable_log_linecount; | 99 | ret->enable_log_linecount = ctx.cfg.enable_log_linecount; |
101 | ret->module_link = ctx.cfg.module_link; | 100 | ret->module_link = ctx.cfg.module_link; |
102 | ret->readme = NULL; | 101 | ret->readme = NULL; |
103 | return ret; | 102 | return ret; |
104 | } | 103 | } |
105 | 104 | ||
106 | struct repoinfo *cgit_get_repoinfo(const char *url) | 105 | struct cgit_repo *cgit_get_repoinfo(const char *url) |
107 | { | 106 | { |
108 | int i; | 107 | int i; |
109 | struct repoinfo *repo; | 108 | struct cgit_repo *repo; |
110 | 109 | ||
111 | for (i=0; i<cgit_repolist.count; i++) { | 110 | for (i=0; i<cgit_repolist.count; i++) { |
112 | repo = &cgit_repolist.repos[i]; | 111 | repo = &cgit_repolist.repos[i]; |
113 | if (!strcmp(repo->url, url)) | 112 | if (!strcmp(repo->url, url)) |
114 | return repo; | 113 | return repo; |
115 | } | 114 | } |
116 | return NULL; | 115 | return NULL; |
117 | } | 116 | } |
118 | 117 | ||
119 | void cgit_global_config_cb(const char *name, const char *value) | 118 | void cgit_global_config_cb(const char *name, const char *value) |
120 | { | 119 | { |
121 | if (!strcmp(name, "root-title")) | 120 | if (!strcmp(name, "root-title")) |
122 | ctx.cfg.root_title = xstrdup(value); | 121 | ctx.cfg.root_title = xstrdup(value); |
123 | else if (!strcmp(name, "css")) | 122 | else if (!strcmp(name, "css")) |
124 | ctx.cfg.css = xstrdup(value); | 123 | ctx.cfg.css = xstrdup(value); |
125 | else if (!strcmp(name, "logo")) | 124 | else if (!strcmp(name, "logo")) |
126 | ctx.cfg.logo = xstrdup(value); | 125 | ctx.cfg.logo = xstrdup(value); |
127 | else if (!strcmp(name, "index-header")) | 126 | else if (!strcmp(name, "index-header")) |
128 | ctx.cfg.index_header = xstrdup(value); | 127 | ctx.cfg.index_header = xstrdup(value); |
129 | else if (!strcmp(name, "index-info")) | 128 | else if (!strcmp(name, "index-info")) |
130 | ctx.cfg.index_info = xstrdup(value); | 129 | ctx.cfg.index_info = xstrdup(value); |
131 | else if (!strcmp(name, "logo-link")) | 130 | else if (!strcmp(name, "logo-link")) |
132 | ctx.cfg.logo_link = xstrdup(value); | 131 | ctx.cfg.logo_link = xstrdup(value); |
133 | else if (!strcmp(name, "module-link")) | 132 | else if (!strcmp(name, "module-link")) |
134 | ctx.cfg.module_link = xstrdup(value); | 133 | ctx.cfg.module_link = xstrdup(value); |
135 | else if (!strcmp(name, "virtual-root")) { | 134 | else if (!strcmp(name, "virtual-root")) { |
136 | ctx.cfg.virtual_root = trim_end(value, '/'); | 135 | ctx.cfg.virtual_root = trim_end(value, '/'); |
137 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) | 136 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) |
138 | ctx.cfg.virtual_root = ""; | 137 | ctx.cfg.virtual_root = ""; |
139 | } else if (!strcmp(name, "nocache")) | 138 | } else if (!strcmp(name, "nocache")) |
140 | ctx.cfg.nocache = atoi(value); | 139 | ctx.cfg.nocache = atoi(value); |
141 | else if (!strcmp(name, "snapshots")) | 140 | else if (!strcmp(name, "snapshots")) |
142 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); | 141 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); |
143 | else if (!strcmp(name, "enable-index-links")) | 142 | else if (!strcmp(name, "enable-index-links")) |
144 | ctx.cfg.enable_index_links = atoi(value); | 143 | ctx.cfg.enable_index_links = atoi(value); |
145 | else if (!strcmp(name, "enable-log-filecount")) | 144 | else if (!strcmp(name, "enable-log-filecount")) |
146 | ctx.cfg.enable_log_filecount = atoi(value); | 145 | ctx.cfg.enable_log_filecount = atoi(value); |
147 | else if (!strcmp(name, "enable-log-linecount")) | 146 | else if (!strcmp(name, "enable-log-linecount")) |
148 | ctx.cfg.enable_log_linecount = atoi(value); | 147 | ctx.cfg.enable_log_linecount = atoi(value); |
149 | else if (!strcmp(name, "cache-root")) | 148 | else if (!strcmp(name, "cache-root")) |
150 | ctx.cfg.cache_root = xstrdup(value); | 149 | ctx.cfg.cache_root = xstrdup(value); |
151 | else if (!strcmp(name, "cache-root-ttl")) | 150 | else if (!strcmp(name, "cache-root-ttl")) |
152 | ctx.cfg.cache_root_ttl = atoi(value); | 151 | ctx.cfg.cache_root_ttl = atoi(value); |
153 | else if (!strcmp(name, "cache-repo-ttl")) | 152 | else if (!strcmp(name, "cache-repo-ttl")) |
154 | ctx.cfg.cache_repo_ttl = atoi(value); | 153 | ctx.cfg.cache_repo_ttl = atoi(value); |
155 | else if (!strcmp(name, "cache-static-ttl")) | 154 | else if (!strcmp(name, "cache-static-ttl")) |
156 | ctx.cfg.cache_static_ttl = atoi(value); | 155 | ctx.cfg.cache_static_ttl = atoi(value); |
157 | else if (!strcmp(name, "cache-dynamic-ttl")) | 156 | else if (!strcmp(name, "cache-dynamic-ttl")) |
158 | ctx.cfg.cache_dynamic_ttl = atoi(value); | 157 | ctx.cfg.cache_dynamic_ttl = atoi(value); |
159 | else if (!strcmp(name, "max-message-length")) | 158 | else if (!strcmp(name, "max-message-length")) |
160 | ctx.cfg.max_msg_len = atoi(value); | 159 | ctx.cfg.max_msg_len = atoi(value); |
161 | else if (!strcmp(name, "max-repodesc-length")) | 160 | else if (!strcmp(name, "max-repodesc-length")) |
162 | ctx.cfg.max_repodesc_len = atoi(value); | 161 | ctx.cfg.max_repodesc_len = atoi(value); |
163 | else if (!strcmp(name, "max-commit-count")) | 162 | else if (!strcmp(name, "max-commit-count")) |
164 | ctx.cfg.max_commit_count = atoi(value); | 163 | ctx.cfg.max_commit_count = atoi(value); |
165 | else if (!strcmp(name, "summary-log")) | 164 | else if (!strcmp(name, "summary-log")) |
166 | ctx.cfg.summary_log = atoi(value); | 165 | ctx.cfg.summary_log = atoi(value); |
167 | else if (!strcmp(name, "summary-branches")) | 166 | else if (!strcmp(name, "summary-branches")) |
168 | ctx.cfg.summary_branches = atoi(value); | 167 | ctx.cfg.summary_branches = atoi(value); |
169 | else if (!strcmp(name, "summary-tags")) | 168 | else if (!strcmp(name, "summary-tags")) |
170 | ctx.cfg.summary_tags = atoi(value); | 169 | ctx.cfg.summary_tags = atoi(value); |
171 | else if (!strcmp(name, "agefile")) | 170 | else if (!strcmp(name, "agefile")) |
172 | ctx.cfg.agefile = xstrdup(value); | 171 | ctx.cfg.agefile = xstrdup(value); |
173 | else if (!strcmp(name, "renamelimit")) | 172 | else if (!strcmp(name, "renamelimit")) |
174 | ctx.cfg.renamelimit = atoi(value); | 173 | ctx.cfg.renamelimit = atoi(value); |
175 | else if (!strcmp(name, "robots")) | 174 | else if (!strcmp(name, "robots")) |
176 | ctx.cfg.robots = xstrdup(value); | 175 | ctx.cfg.robots = xstrdup(value); |
177 | else if (!strcmp(name, "clone-prefix")) | 176 | else if (!strcmp(name, "clone-prefix")) |
178 | ctx.cfg.clone_prefix = xstrdup(value); | 177 | ctx.cfg.clone_prefix = xstrdup(value); |
179 | else if (!strcmp(name, "repo.group")) | 178 | else if (!strcmp(name, "repo.group")) |
180 | ctx.cfg.repo_group = xstrdup(value); | 179 | ctx.cfg.repo_group = xstrdup(value); |
181 | else if (!strcmp(name, "repo.url")) | 180 | else if (!strcmp(name, "repo.url")) |
182 | cgit_repo = add_repo(value); | 181 | ctx.repo = add_repo(value); |
183 | else if (!strcmp(name, "repo.name")) | 182 | else if (!strcmp(name, "repo.name")) |
184 | cgit_repo->name = xstrdup(value); | 183 | ctx.repo->name = xstrdup(value); |
185 | else if (cgit_repo && !strcmp(name, "repo.path")) | 184 | else if (ctx.repo && !strcmp(name, "repo.path")) |
186 | cgit_repo->path = trim_end(value, '/'); | 185 | ctx.repo->path = trim_end(value, '/'); |
187 | else if (cgit_repo && !strcmp(name, "repo.clone-url")) | 186 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) |
188 | cgit_repo->clone_url = xstrdup(value); | 187 | ctx.repo->clone_url = xstrdup(value); |
189 | else if (cgit_repo && !strcmp(name, "repo.desc")) | 188 | else if (ctx.repo && !strcmp(name, "repo.desc")) |
190 | cgit_repo->desc = xstrdup(value); | 189 | ctx.repo->desc = xstrdup(value); |
191 | else if (cgit_repo && !strcmp(name, "repo.owner")) | 190 | else if (ctx.repo && !strcmp(name, "repo.owner")) |
192 | cgit_repo->owner = xstrdup(value); | 191 | ctx.repo->owner = xstrdup(value); |
193 | else if (cgit_repo && !strcmp(name, "repo.defbranch")) | 192 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) |
194 | cgit_repo->defbranch = xstrdup(value); | 193 | ctx.repo->defbranch = xstrdup(value); |
195 | else if (cgit_repo && !strcmp(name, "repo.snapshots")) | 194 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) |
196 | cgit_repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ | 195 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ |
197 | else if (cgit_repo && !strcmp(name, "repo.enable-log-filecount")) | 196 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) |
198 | cgit_repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); | 197 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); |
199 | else if (cgit_repo && !strcmp(name, "repo.enable-log-linecount")) | 198 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) |
200 | cgit_repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); | 199 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); |
201 | else if (cgit_repo && !strcmp(name, "repo.module-link")) | 200 | else if (ctx.repo && !strcmp(name, "repo.module-link")) |
202 | cgit_repo->module_link= xstrdup(value); | 201 | ctx.repo->module_link= xstrdup(value); |
203 | else if (cgit_repo && !strcmp(name, "repo.readme") && value != NULL) { | 202 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { |
204 | if (*value == '/') | 203 | if (*value == '/') |
205 | cgit_repo->readme = xstrdup(value); | 204 | ctx.repo->readme = xstrdup(value); |
206 | else | 205 | else |
207 | cgit_repo->readme = xstrdup(fmt("%s/%s", cgit_repo->path, value)); | 206 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); |
208 | } else if (!strcmp(name, "include")) | 207 | } else if (!strcmp(name, "include")) |
209 | cgit_read_config(value, cgit_global_config_cb); | 208 | cgit_read_config(value, cgit_global_config_cb); |
210 | } | 209 | } |
211 | 210 | ||
212 | void cgit_querystring_cb(const char *name, const char *value) | 211 | void cgit_querystring_cb(const char *name, const char *value) |
213 | { | 212 | { |
214 | if (!strcmp(name,"r")) { | 213 | if (!strcmp(name,"r")) { |
215 | ctx.qry.repo = xstrdup(value); | 214 | ctx.qry.repo = xstrdup(value); |
216 | cgit_repo = cgit_get_repoinfo(value); | 215 | ctx.repo = cgit_get_repoinfo(value); |
217 | } else if (!strcmp(name, "p")) { | 216 | } else if (!strcmp(name, "p")) { |
218 | ctx.qry.page = xstrdup(value); | 217 | ctx.qry.page = xstrdup(value); |
219 | cgit_cmd = cgit_get_cmd_index(value); | 218 | cgit_cmd = cgit_get_cmd_index(value); |
220 | } else if (!strcmp(name, "url")) { | 219 | } else if (!strcmp(name, "url")) { |
221 | cgit_parse_url(value); | 220 | cgit_parse_url(value); |
222 | } else if (!strcmp(name, "qt")) { | 221 | } else if (!strcmp(name, "qt")) { |
223 | ctx.qry.grep = xstrdup(value); | 222 | ctx.qry.grep = xstrdup(value); |
224 | } else if (!strcmp(name, "q")) { | 223 | } else if (!strcmp(name, "q")) { |
225 | ctx.qry.search = xstrdup(value); | 224 | ctx.qry.search = xstrdup(value); |
226 | } else if (!strcmp(name, "h")) { | 225 | } else if (!strcmp(name, "h")) { |
227 | ctx.qry.head = xstrdup(value); | 226 | ctx.qry.head = xstrdup(value); |
228 | ctx.qry.has_symref = 1; | 227 | ctx.qry.has_symref = 1; |
229 | } else if (!strcmp(name, "id")) { | 228 | } else if (!strcmp(name, "id")) { |
230 | ctx.qry.sha1 = xstrdup(value); | 229 | ctx.qry.sha1 = xstrdup(value); |
231 | ctx.qry.has_sha1 = 1; | 230 | ctx.qry.has_sha1 = 1; |
232 | } else if (!strcmp(name, "id2")) { | 231 | } else if (!strcmp(name, "id2")) { |
233 | ctx.qry.sha2 = xstrdup(value); | 232 | ctx.qry.sha2 = xstrdup(value); |
234 | ctx.qry.has_sha1 = 1; | 233 | ctx.qry.has_sha1 = 1; |
235 | } else if (!strcmp(name, "ofs")) { | 234 | } else if (!strcmp(name, "ofs")) { |
236 | ctx.qry.ofs = atoi(value); | 235 | ctx.qry.ofs = atoi(value); |
237 | } else if (!strcmp(name, "path")) { | 236 | } else if (!strcmp(name, "path")) { |
238 | ctx.qry.path = trim_end(value, '/'); | 237 | ctx.qry.path = trim_end(value, '/'); |
239 | } else if (!strcmp(name, "name")) { | 238 | } else if (!strcmp(name, "name")) { |
240 | ctx.qry.name = xstrdup(value); | 239 | ctx.qry.name = xstrdup(value); |
241 | } | 240 | } |
242 | } | 241 | } |
243 | 242 | ||
244 | void *cgit_free_commitinfo(struct commitinfo *info) | 243 | void *cgit_free_commitinfo(struct commitinfo *info) |
245 | { | 244 | { |
246 | free(info->author); | 245 | free(info->author); |
247 | free(info->author_email); | 246 | free(info->author_email); |
248 | free(info->committer); | 247 | free(info->committer); |
249 | free(info->committer_email); | 248 | free(info->committer_email); |
250 | free(info->subject); | 249 | free(info->subject); |
251 | free(info->msg); | 250 | free(info->msg); |
252 | free(info->msg_encoding); | 251 | free(info->msg_encoding); |
253 | free(info); | 252 | free(info); |
254 | return NULL; | 253 | return NULL; |
255 | } | 254 | } |
256 | 255 | ||
257 | int hextoint(char c) | 256 | int hextoint(char c) |
258 | { | 257 | { |
259 | if (c >= 'a' && c <= 'f') | 258 | if (c >= 'a' && c <= 'f') |
260 | return 10 + c - 'a'; | 259 | return 10 + c - 'a'; |
261 | else if (c >= 'A' && c <= 'F') | 260 | else if (c >= 'A' && c <= 'F') |
262 | return 10 + c - 'A'; | 261 | return 10 + c - 'A'; |
263 | else if (c >= '0' && c <= '9') | 262 | else if (c >= '0' && c <= '9') |
264 | return c - '0'; | 263 | return c - '0'; |
265 | else | 264 | else |
266 | return -1; | 265 | return -1; |
267 | } | 266 | } |
268 | 267 | ||
269 | char *trim_end(const char *str, char c) | 268 | char *trim_end(const char *str, char c) |
270 | { | 269 | { |
271 | int len; | 270 | int len; |
272 | char *s, *t; | 271 | char *s, *t; |
273 | 272 | ||
274 | if (str == NULL) | 273 | if (str == NULL) |
275 | return NULL; | 274 | return NULL; |
276 | t = (char *)str; | 275 | t = (char *)str; |
277 | len = strlen(t); | 276 | len = strlen(t); |
278 | while(len > 0 && t[len - 1] == c) | 277 | while(len > 0 && t[len - 1] == c) |
279 | len--; | 278 | len--; |
280 | 279 | ||
281 | if (len == 0) | 280 | if (len == 0) |
282 | return NULL; | 281 | return NULL; |
283 | 282 | ||
284 | c = t[len]; | 283 | c = t[len]; |
285 | t[len] = '\0'; | 284 | t[len] = '\0'; |
286 | s = xstrdup(t); | 285 | s = xstrdup(t); |
287 | t[len] = c; | 286 | t[len] = c; |
288 | return s; | 287 | return s; |
289 | } | 288 | } |
290 | 289 | ||
291 | char *strlpart(char *txt, int maxlen) | 290 | char *strlpart(char *txt, int maxlen) |
292 | { | 291 | { |
293 | char *result; | 292 | char *result; |
294 | 293 | ||
295 | if (!txt) | 294 | if (!txt) |
296 | return txt; | 295 | return txt; |
297 | 296 | ||
298 | if (strlen(txt) <= maxlen) | 297 | if (strlen(txt) <= maxlen) |
299 | return txt; | 298 | return txt; |
300 | result = xmalloc(maxlen + 1); | 299 | result = xmalloc(maxlen + 1); |
301 | memcpy(result, txt, maxlen - 3); | 300 | memcpy(result, txt, maxlen - 3); |
302 | result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; | 301 | result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; |
303 | result[maxlen] = '\0'; | 302 | result[maxlen] = '\0'; |
304 | return result; | 303 | return result; |
305 | } | 304 | } |
306 | 305 | ||
307 | char *strrpart(char *txt, int maxlen) | 306 | char *strrpart(char *txt, int maxlen) |
308 | { | 307 | { |
309 | char *result; | 308 | char *result; |
310 | 309 | ||
311 | if (!txt) | 310 | if (!txt) |
312 | return txt; | 311 | return txt; |
313 | 312 | ||
314 | if (strlen(txt) <= maxlen) | 313 | if (strlen(txt) <= maxlen) |
315 | return txt; | 314 | return txt; |
316 | result = xmalloc(maxlen + 1); | 315 | result = xmalloc(maxlen + 1); |
317 | memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); | 316 | memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); |
318 | result[0] = result[1] = result[2] = '.'; | 317 | result[0] = result[1] = result[2] = '.'; |
319 | return result; | 318 | return result; |
320 | } | 319 | } |
321 | 320 | ||
322 | void cgit_add_ref(struct reflist *list, struct refinfo *ref) | 321 | void cgit_add_ref(struct reflist *list, struct refinfo *ref) |
323 | { | 322 | { |
324 | size_t size; | 323 | size_t size; |
325 | 324 | ||
326 | if (list->count >= list->alloc) { | 325 | if (list->count >= list->alloc) { |
327 | list->alloc += (list->alloc ? list->alloc : 4); | 326 | list->alloc += (list->alloc ? list->alloc : 4); |
328 | size = list->alloc * sizeof(struct refinfo *); | 327 | size = list->alloc * sizeof(struct refinfo *); |
329 | list->refs = xrealloc(list->refs, size); | 328 | list->refs = xrealloc(list->refs, size); |
330 | } | 329 | } |
331 | list->refs[list->count++] = ref; | 330 | list->refs[list->count++] = ref; |
332 | } | 331 | } |
333 | 332 | ||
334 | struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) | 333 | struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) |
335 | { | 334 | { |
336 | struct refinfo *ref; | 335 | struct refinfo *ref; |
337 | 336 | ||
338 | ref = xmalloc(sizeof (struct refinfo)); | 337 | ref = xmalloc(sizeof (struct refinfo)); |
339 | ref->refname = xstrdup(refname); | 338 | ref->refname = xstrdup(refname); |
340 | ref->object = parse_object(sha1); | 339 | ref->object = parse_object(sha1); |
341 | switch (ref->object->type) { | 340 | switch (ref->object->type) { |
342 | case OBJ_TAG: | 341 | case OBJ_TAG: |
343 | ref->tag = cgit_parse_tag((struct tag *)ref->object); | 342 | ref->tag = cgit_parse_tag((struct tag *)ref->object); |
344 | break; | 343 | break; |