]> gitweb.ps.run Git - ps-cgit/blob - parsing.c
git: update to v2.0.3
[ps-cgit] / parsing.c
1 /* parsing.c: parsing of config files
2  *
3  * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4  *
5  * Licensed under GNU General Public License v2
6  *   (see COPYING for full license text)
7  */
8
9 #include "cgit.h"
10
11 /*
12  * url syntax: [repo ['/' cmd [ '/' path]]]
13  *   repo: any valid repo url, may contain '/'
14  *   cmd:  log | commit | diff | tree | view | blob | snapshot
15  *   path: any valid path, may contain '/'
16  *
17  */
18 void cgit_parse_url(const char *url)
19 {
20         char *c, *cmd, *p;
21         struct cgit_repo *repo;
22
23         ctx.repo = NULL;
24         if (!url || url[0] == '\0')
25                 return;
26
27         ctx.repo = cgit_get_repoinfo(url);
28         if (ctx.repo) {
29                 ctx.qry.repo = ctx.repo->url;
30                 return;
31         }
32
33         cmd = NULL;
34         c = strchr(url, '/');
35         while (c) {
36                 c[0] = '\0';
37                 repo = cgit_get_repoinfo(url);
38                 if (repo) {
39                         ctx.repo = repo;
40                         cmd = c;
41                 }
42                 c[0] = '/';
43                 c = strchr(c + 1, '/');
44         }
45
46         if (ctx.repo) {
47                 ctx.qry.repo = ctx.repo->url;
48                 p = strchr(cmd + 1, '/');
49                 if (p) {
50                         p[0] = '\0';
51                         if (p[1])
52                                 ctx.qry.path = trim_end(p + 1, '/');
53                 }
54                 if (cmd[1])
55                         ctx.qry.page = xstrdup(cmd + 1);
56                 return;
57         }
58 }
59
60 static char *substr(const char *head, const char *tail)
61 {
62         char *buf;
63
64         if (tail < head)
65                 return xstrdup("");
66         buf = xmalloc(tail - head + 1);
67         strncpy(buf, head, tail - head);
68         buf[tail - head] = '\0';
69         return buf;
70 }
71
72 static const char *parse_user(const char *t, char **name, char **email, unsigned long *date)
73 {
74         const char *p = t;
75         int mode = 1;
76
77         while (p && *p) {
78                 if (mode == 1 && *p == '<') {
79                         *name = substr(t, p - 1);
80                         t = p;
81                         mode++;
82                 } else if (mode == 1 && *p == '\n') {
83                         *name = substr(t, p);
84                         p++;
85                         break;
86                 } else if (mode == 2 && *p == '>') {
87                         *email = substr(t, p + 1);
88                         t = p;
89                         mode++;
90                 } else if (mode == 2 && *p == '\n') {
91                         *email = substr(t, p);
92                         p++;
93                         break;
94                 } else if (mode == 3 && isdigit(*p)) {
95                         *date = atol(p);
96                         mode++;
97                 } else if (*p == '\n') {
98                         p++;
99                         break;
100                 }
101                 p++;
102         }
103         return p;
104 }
105
106 #ifdef NO_ICONV
107 #define reencode(a, b, c)
108 #else
109 static const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
110 {
111         char *tmp;
112
113         if (!txt)
114                 return NULL;
115
116         if (!*txt || !src_enc || !dst_enc)
117                 return *txt;
118
119         /* no encoding needed if src_enc equals dst_enc */
120         if (!strcasecmp(src_enc, dst_enc))
121                 return *txt;
122
123         tmp = reencode_string(*txt, dst_enc, src_enc);
124         if (tmp) {
125                 free(*txt);
126                 *txt = tmp;
127         }
128         return *txt;
129 }
130 #endif
131
132 struct commitinfo *cgit_parse_commit(struct commit *commit)
133 {
134         struct commitinfo *ret;
135         const char *p = get_cached_commit_buffer(commit, NULL);
136         const char *t;
137
138         ret = xmalloc(sizeof(*ret));
139         ret->commit = commit;
140         ret->author = NULL;
141         ret->author_email = NULL;
142         ret->committer = NULL;
143         ret->committer_email = NULL;
144         ret->subject = NULL;
145         ret->msg = NULL;
146         ret->msg_encoding = NULL;
147
148         if (p == NULL)
149                 return ret;
150
151         if (!starts_with(p, "tree "))
152                 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
153         else
154                 p += 46; // "tree " + hex[40] + "\n"
155
156         while (starts_with(p, "parent "))
157                 p += 48; // "parent " + hex[40] + "\n"
158
159         if (p && starts_with(p, "author ")) {
160                 p = parse_user(p + 7, &ret->author, &ret->author_email,
161                         &ret->author_date);
162         }
163
164         if (p && starts_with(p, "committer ")) {
165                 p = parse_user(p + 10, &ret->committer, &ret->committer_email,
166                         &ret->committer_date);
167         }
168
169         if (p && starts_with(p, "encoding ")) {
170                 p += 9;
171                 t = strchr(p, '\n');
172                 if (t) {
173                         ret->msg_encoding = substr(p, t + 1);
174                         p = t + 1;
175                 }
176         }
177
178         /* if no special encoding is found, assume UTF-8 */
179         if (!ret->msg_encoding)
180                 ret->msg_encoding = xstrdup("UTF-8");
181
182         // skip unknown header fields
183         while (p && *p && (*p != '\n')) {
184                 p = strchr(p, '\n');
185                 if (p)
186                         p++;
187         }
188
189         // skip empty lines between headers and message
190         while (p && *p == '\n')
191                 p++;
192
193         if (!p)
194                 return ret;
195
196         t = strchr(p, '\n');
197         if (t) {
198                 ret->subject = substr(p, t);
199                 p = t + 1;
200
201                 while (p && *p == '\n') {
202                         p = strchr(p, '\n');
203                         if (p)
204                                 p++;
205                 }
206                 if (p)
207                         ret->msg = xstrdup(p);
208         } else
209                 ret->subject = xstrdup(p);
210
211         reencode(&ret->author, ret->msg_encoding, PAGE_ENCODING);
212         reencode(&ret->author_email, ret->msg_encoding, PAGE_ENCODING);
213         reencode(&ret->committer, ret->msg_encoding, PAGE_ENCODING);
214         reencode(&ret->committer_email, ret->msg_encoding, PAGE_ENCODING);
215         reencode(&ret->subject, ret->msg_encoding, PAGE_ENCODING);
216         reencode(&ret->msg, ret->msg_encoding, PAGE_ENCODING);
217
218         return ret;
219 }
220
221
222 struct taginfo *cgit_parse_tag(struct tag *tag)
223 {
224         void *data;
225         enum object_type type;
226         unsigned long size;
227         const char *p;
228         struct taginfo *ret;
229
230         data = read_sha1_file(tag->object.sha1, &type, &size);
231         if (!data || type != OBJ_TAG) {
232                 free(data);
233                 return 0;
234         }
235
236         ret = xmalloc(sizeof(*ret));
237         ret->tagger = NULL;
238         ret->tagger_email = NULL;
239         ret->tagger_date = 0;
240         ret->msg = NULL;
241
242         p = data;
243
244         while (p && *p) {
245                 if (*p == '\n')
246                         break;
247
248                 if (starts_with(p, "tagger ")) {
249                         p = parse_user(p + 7, &ret->tagger, &ret->tagger_email,
250                                 &ret->tagger_date);
251                 } else {
252                         p = strchr(p, '\n');
253                         if (p)
254                                 p++;
255                 }
256         }
257
258         // skip empty lines between headers and message
259         while (p && *p == '\n')
260                 p++;
261
262         if (p && *p)
263                 ret->msg = xstrdup(p);
264         free(data);
265         return ret;
266 }