summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--parsing.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/parsing.c b/parsing.c
index 4420e58..b86467a 100644
--- a/parsing.c
+++ b/parsing.c
@@ -109,185 +109,190 @@ int cgit_parse_query(char *txt, configfn fn)
109 if (!txt) 109 if (!txt)
110 return 0; 110 return 0;
111 111
112 t = txt = xstrdup(txt); 112 t = txt = xstrdup(txt);
113 113
114 while((c=*t) != '\0') { 114 while((c=*t) != '\0') {
115 if (c=='=') { 115 if (c=='=') {
116 *t = '\0'; 116 *t = '\0';
117 value = t+1; 117 value = t+1;
118 } else if (c=='+') { 118 } else if (c=='+') {
119 *t = ' '; 119 *t = ' ';
120 } else if (c=='%') { 120 } else if (c=='%') {
121 t = convert_query_hexchar(t); 121 t = convert_query_hexchar(t);
122 } else if (c=='&') { 122 } else if (c=='&') {
123 *t = '\0'; 123 *t = '\0';
124 (*fn)(txt, value); 124 (*fn)(txt, value);
125 txt = t+1; 125 txt = t+1;
126 value = NULL; 126 value = NULL;
127 } 127 }
128 t++; 128 t++;
129 } 129 }
130 if (t!=txt) 130 if (t!=txt)
131 (*fn)(txt, value); 131 (*fn)(txt, value);
132 return 0; 132 return 0;
133} 133}
134 134
135/* 135/*
136 * url syntax: [repo ['/' cmd [ '/' path]]] 136 * url syntax: [repo ['/' cmd [ '/' path]]]
137 * repo: any valid repo url, may contain '/' 137 * repo: any valid repo url, may contain '/'
138 * cmd: log | commit | diff | tree | view | blob | snapshot 138 * cmd: log | commit | diff | tree | view | blob | snapshot
139 * path: any valid path, may contain '/' 139 * path: any valid path, may contain '/'
140 * 140 *
141 */ 141 */
142void cgit_parse_url(const char *url) 142void cgit_parse_url(const char *url)
143{ 143{
144 char *cmd, *p; 144 char *cmd, *p;
145 145
146 cgit_repo = NULL; 146 cgit_repo = NULL;
147 if (!url || url[0] == '\0') 147 if (!url || url[0] == '\0')
148 return; 148 return;
149 149
150 cgit_repo = cgit_get_repoinfo(url); 150 cgit_repo = cgit_get_repoinfo(url);
151 if (cgit_repo) { 151 if (cgit_repo) {
152 cgit_query_repo = cgit_repo->url; 152 cgit_query_repo = cgit_repo->url;
153 return; 153 return;
154 } 154 }
155 155
156 cmd = strchr(url, '/'); 156 cmd = strchr(url, '/');
157 while (!cgit_repo && cmd) { 157 while (!cgit_repo && cmd) {
158 cmd[0] = '\0'; 158 cmd[0] = '\0';
159 cgit_repo = cgit_get_repoinfo(url); 159 cgit_repo = cgit_get_repoinfo(url);
160 if (cgit_repo == NULL) { 160 if (cgit_repo == NULL) {
161 cmd[0] = '/'; 161 cmd[0] = '/';
162 cmd = strchr(cmd + 1, '/'); 162 cmd = strchr(cmd + 1, '/');
163 continue; 163 continue;
164 } 164 }
165 165
166 cgit_query_repo = cgit_repo->url; 166 cgit_query_repo = cgit_repo->url;
167 p = strchr(cmd + 1, '/'); 167 p = strchr(cmd + 1, '/');
168 if (p) { 168 if (p) {
169 p[0] = '\0'; 169 p[0] = '\0';
170 if (p[1]) 170 if (p[1])
171 cgit_query_path = xstrdup(p + 1); 171 cgit_query_path = xstrdup(p + 1);
172 } 172 }
173 cgit_cmd = cgit_get_cmd_index(cmd + 1); 173 cgit_cmd = cgit_get_cmd_index(cmd + 1);
174 cgit_query_page = xstrdup(cmd + 1); 174 cgit_query_page = xstrdup(cmd + 1);
175 return; 175 return;
176 } 176 }
177} 177}
178 178
179char *substr(const char *head, const char *tail) 179char *substr(const char *head, const char *tail)
180{ 180{
181 char *buf; 181 char *buf;
182 182
183 buf = xmalloc(tail - head + 1); 183 buf = xmalloc(tail - head + 1);
184 strncpy(buf, head, tail - head); 184 strncpy(buf, head, tail - head);
185 buf[tail - head] = '\0'; 185 buf[tail - head] = '\0';
186 return buf; 186 return buf;
187} 187}
188 188
189struct commitinfo *cgit_parse_commit(struct commit *commit) 189struct commitinfo *cgit_parse_commit(struct commit *commit)
190{ 190{
191 struct commitinfo *ret; 191 struct commitinfo *ret;
192 char *p = commit->buffer, *t = commit->buffer; 192 char *p = commit->buffer, *t = commit->buffer;
193 193
194 ret = xmalloc(sizeof(*ret)); 194 ret = xmalloc(sizeof(*ret));
195 ret->commit = commit; 195 ret->commit = commit;
196 ret->author = NULL; 196 ret->author = NULL;
197 ret->author_email = NULL; 197 ret->author_email = NULL;
198 ret->committer = NULL; 198 ret->committer = NULL;
199 ret->committer_email = NULL; 199 ret->committer_email = NULL;
200 ret->subject = NULL; 200 ret->subject = NULL;
201 ret->msg = NULL; 201 ret->msg = NULL;
202 202
203 if (strncmp(p, "tree ", 5)) 203 if (strncmp(p, "tree ", 5))
204 die("Bad commit: %s", sha1_to_hex(commit->object.sha1)); 204 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
205 else 205 else
206 p += 46; // "tree " + hex[40] + "\n" 206 p += 46; // "tree " + hex[40] + "\n"
207 207
208 while (!strncmp(p, "parent ", 7)) 208 while (!strncmp(p, "parent ", 7))
209 p += 48; // "parent " + hex[40] + "\n" 209 p += 48; // "parent " + hex[40] + "\n"
210 210
211 if (!strncmp(p, "author ", 7)) { 211 if (!strncmp(p, "author ", 7)) {
212 p += 7; 212 p += 7;
213 t = strchr(p, '<') - 1; 213 t = strchr(p, '<') - 1;
214 ret->author = substr(p, t); 214 ret->author = substr(p, t);
215 p = t; 215 p = t;
216 t = strchr(t, '>') + 1; 216 t = strchr(t, '>') + 1;
217 ret->author_email = substr(p, t); 217 ret->author_email = substr(p, t);
218 ret->author_date = atol(++t); 218 ret->author_date = atol(++t);
219 p = strchr(t, '\n') + 1; 219 p = strchr(t, '\n') + 1;
220 } 220 }
221 221
222 if (!strncmp(p, "committer ", 9)) { 222 if (!strncmp(p, "committer ", 9)) {
223 p += 9; 223 p += 9;
224 t = strchr(p, '<') - 1; 224 t = strchr(p, '<') - 1;
225 ret->committer = substr(p, t); 225 ret->committer = substr(p, t);
226 p = t; 226 p = t;
227 t = strchr(t, '>') + 1; 227 t = strchr(t, '>') + 1;
228 ret->committer_email = substr(p, t); 228 ret->committer_email = substr(p, t);
229 ret->committer_date = atol(++t); 229 ret->committer_date = atol(++t);
230 p = strchr(t, '\n') + 1; 230 p = strchr(t, '\n') + 1;
231 } 231 }
232 232
233 while (*p == '\n') 233 while (*p == '\n')
234 p = strchr(p, '\n') + 1; 234 p = strchr(p, '\n') + 1;
235 235
236 t = strchr(p, '\n'); 236 t = strchr(p, '\n');
237 if (t && *t) { 237 if (t) {
238 ret->subject = substr(p, t); 238 if (*t == '\0')
239 ret->subject = strdup("** empty **");
240 else
241 ret->subject = substr(p, t);
239 p = t + 1; 242 p = t + 1;
240 243
241 while (*p == '\n') 244 while (*p == '\n')
242 p = strchr(p, '\n') + 1; 245 p = strchr(p, '\n') + 1;
243 ret->msg = p; 246 ret->msg = p;
244 } 247 } else
248 ret->subject = substr(p, p+strlen(p));
249
245 return ret; 250 return ret;
246} 251}
247 252
248 253
249struct taginfo *cgit_parse_tag(struct tag *tag) 254struct taginfo *cgit_parse_tag(struct tag *tag)
250{ 255{
251 void *data; 256 void *data;
252 enum object_type type; 257 enum object_type type;
253 unsigned long size; 258 unsigned long size;
254 char *p, *t; 259 char *p, *t;
255 struct taginfo *ret; 260 struct taginfo *ret;
256 261
257 data = read_sha1_file(tag->object.sha1, &type, &size); 262 data = read_sha1_file(tag->object.sha1, &type, &size);
258 if (!data || type != OBJ_TAG) { 263 if (!data || type != OBJ_TAG) {
259 free(data); 264 free(data);
260 return 0; 265 return 0;
261 } 266 }
262 267
263 ret = xmalloc(sizeof(*ret)); 268 ret = xmalloc(sizeof(*ret));
264 ret->tagger = NULL; 269 ret->tagger = NULL;
265 ret->tagger_email = NULL; 270 ret->tagger_email = NULL;
266 ret->tagger_date = 0; 271 ret->tagger_date = 0;
267 ret->msg = NULL; 272 ret->msg = NULL;
268 273
269 p = data; 274 p = data;
270 275
271 while (p && *p) { 276 while (p && *p) {
272 if (*p == '\n') 277 if (*p == '\n')
273 break; 278 break;
274 279
275 if (!strncmp(p, "tagger ", 7)) { 280 if (!strncmp(p, "tagger ", 7)) {
276 p += 7; 281 p += 7;
277 t = strchr(p, '<') - 1; 282 t = strchr(p, '<') - 1;
278 ret->tagger = substr(p, t); 283 ret->tagger = substr(p, t);
279 p = t; 284 p = t;
280 t = strchr(t, '>') + 1; 285 t = strchr(t, '>') + 1;
281 ret->tagger_email = substr(p, t); 286 ret->tagger_email = substr(p, t);
282 ret->tagger_date = atol(++t); 287 ret->tagger_date = atol(++t);
283 } 288 }
284 p = strchr(p, '\n') + 1; 289 p = strchr(p, '\n') + 1;
285 } 290 }
286 291
287 while (p && (*p == '\n')) 292 while (p && (*p == '\n'))
288 p = strchr(p, '\n') + 1; 293 p = strchr(p, '\n') + 1;
289 if (p && *p) 294 if (p && *p)
290 ret->msg = xstrdup(p); 295 ret->msg = xstrdup(p);
291 free(data); 296 free(data);
292 return ret; 297 return ret;
293} 298}