1 /* parsing.c: parsing of config files
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
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 '/'
18 void cgit_parse_url(const char *url)
21 struct cgit_repo *repo;
23 if (!url || url[0] == '\0')
27 ctx.repo = cgit_get_repoinfo(url);
29 ctx.qry.repo = ctx.repo->url;
37 repo = cgit_get_repoinfo(url);
43 c = strchr(c + 1, '/');
47 ctx.qry.repo = ctx.repo->url;
48 p = strchr(cmd + 1, '/');
52 ctx.qry.path = trim_end(p + 1, '/');
55 ctx.qry.page = xstrdup(cmd + 1);
59 static char *substr(const char *head, const char *tail)
65 buf = xmalloc(tail - head + 1);
66 strlcpy(buf, head, tail - head + 1);
70 static void parse_user(const char *t, char **name, char **email, unsigned long *date, int *tz)
72 struct ident_split ident;
75 if (!split_ident_line(&ident, t, strchrnul(t, '\n') - t)) {
76 *name = substr(ident.name_begin, ident.name_end);
78 email_len = ident.mail_end - ident.mail_begin;
79 *email = xmalloc(strlen("<") + email_len + strlen(">") + 1);
80 xsnprintf(*email, email_len + 3, "<%.*s>", email_len, ident.mail_begin);
83 *date = strtoul(ident.date_begin, NULL, 10);
85 *tz = atoi(ident.tz_begin);
90 #define reencode(a, b, c)
92 static const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
99 if (!*txt || !src_enc || !dst_enc)
102 /* no encoding needed if src_enc equals dst_enc */
103 if (!strcasecmp(src_enc, dst_enc))
106 tmp = reencode_string(*txt, dst_enc, src_enc);
115 static const char *next_header_line(const char *p)
123 static int end_of_header(const char *p)
125 return !p || (*p == '\n');
128 struct commitinfo *cgit_parse_commit(struct commit *commit)
130 const int sha1hex_len = 40;
131 struct commitinfo *ret;
132 const char *p = get_cached_commit_buffer(commit, NULL);
135 ret = xcalloc(1, sizeof(struct commitinfo));
136 ret->commit = commit;
141 if (!skip_prefix(p, "tree ", &p))
142 die("Bad commit: %s", oid_to_hex(&commit->object.oid));
143 p += sha1hex_len + 1;
145 while (skip_prefix(p, "parent ", &p))
146 p += sha1hex_len + 1;
148 if (p && skip_prefix(p, "author ", &p)) {
149 parse_user(p, &ret->author, &ret->author_email,
150 &ret->author_date, &ret->author_tz);
151 p = next_header_line(p);
154 if (p && skip_prefix(p, "committer ", &p)) {
155 parse_user(p, &ret->committer, &ret->committer_email,
156 &ret->committer_date, &ret->committer_tz);
157 p = next_header_line(p);
160 if (p && skip_prefix(p, "encoding ", &p)) {
163 ret->msg_encoding = substr(p, t + 1);
168 if (!ret->msg_encoding)
169 ret->msg_encoding = xstrdup("UTF-8");
171 while (!end_of_header(p))
172 p = next_header_line(p);
173 while (p && *p == '\n')
178 t = strchrnul(p, '\n');
179 ret->subject = substr(p, t);
182 ret->msg = xstrdup(t);
184 reencode(&ret->author, ret->msg_encoding, PAGE_ENCODING);
185 reencode(&ret->author_email, ret->msg_encoding, PAGE_ENCODING);
186 reencode(&ret->committer, ret->msg_encoding, PAGE_ENCODING);
187 reencode(&ret->committer_email, ret->msg_encoding, PAGE_ENCODING);
188 reencode(&ret->subject, ret->msg_encoding, PAGE_ENCODING);
189 reencode(&ret->msg, ret->msg_encoding, PAGE_ENCODING);
194 struct taginfo *cgit_parse_tag(struct tag *tag)
197 enum object_type type;
200 struct taginfo *ret = NULL;
202 data = read_object_file(&tag->object.oid, &type, &size);
203 if (!data || type != OBJ_TAG)
206 ret = xcalloc(1, sizeof(struct taginfo));
208 for (p = data; !end_of_header(p); p = next_header_line(p)) {
209 if (skip_prefix(p, "tagger ", &p)) {
210 parse_user(p, &ret->tagger, &ret->tagger_email,
211 &ret->tagger_date, &ret->tagger_tz);
215 while (p && *p == '\n')
219 ret->msg = xstrdup(p);