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;
24 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);
60 static char *substr(const char *head, const char *tail)
66 buf = xmalloc(tail - head + 1);
67 strncpy(buf, head, tail - head);
68 buf[tail - head] = '\0';
72 static const char *parse_user(const char *t, char **name, char **email, unsigned long *date)
74 const char *line_end = strchrnul(t, '\n');
75 struct ident_split ident;
78 if (!split_ident_line(&ident, t, line_end - t)) {
79 *name = substr(ident.name_begin, ident.name_end);
81 email_len = ident.mail_end - ident.mail_begin;
82 *email = xmalloc(strlen("<") + email_len + strlen(">") + 1);
83 sprintf(*email, "<%.*s>", email_len, ident.mail_begin);
86 *date = strtoul(ident.date_begin, NULL, 10);
96 #define reencode(a, b, c)
98 static const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
105 if (!*txt || !src_enc || !dst_enc)
108 /* no encoding needed if src_enc equals dst_enc */
109 if (!strcasecmp(src_enc, dst_enc))
112 tmp = reencode_string(*txt, dst_enc, src_enc);
121 static const char *next_header_line(const char *p)
129 static int end_of_header(const char *p)
131 return !p || (*p == '\n');
134 struct commitinfo *cgit_parse_commit(struct commit *commit)
136 const int sha1hex_len = 40;
137 struct commitinfo *ret;
138 const char *p = get_cached_commit_buffer(commit, NULL);
141 ret = xcalloc(1, sizeof(struct commitinfo));
142 ret->commit = commit;
147 if (!skip_prefix(p, "tree ", &p))
148 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
149 p += sha1hex_len + 1;
151 while (skip_prefix(p, "parent ", &p))
152 p += sha1hex_len + 1;
154 if (p && skip_prefix(p, "author ", &p)) {
155 p = parse_user(p, &ret->author, &ret->author_email,
159 if (p && skip_prefix(p, "committer ", &p)) {
160 p = parse_user(p, &ret->committer, &ret->committer_email,
161 &ret->committer_date);
164 if (p && skip_prefix(p, "encoding ", &p)) {
167 ret->msg_encoding = substr(p, t + 1);
172 if (!ret->msg_encoding)
173 ret->msg_encoding = xstrdup("UTF-8");
175 while (!end_of_header(p))
176 p = next_header_line(p);
177 while (p && *p == '\n')
182 t = strchrnul(p, '\n');
183 ret->subject = substr(p, t);
186 ret->msg = xstrdup(t);
188 reencode(&ret->author, ret->msg_encoding, PAGE_ENCODING);
189 reencode(&ret->author_email, ret->msg_encoding, PAGE_ENCODING);
190 reencode(&ret->committer, ret->msg_encoding, PAGE_ENCODING);
191 reencode(&ret->committer_email, ret->msg_encoding, PAGE_ENCODING);
192 reencode(&ret->subject, ret->msg_encoding, PAGE_ENCODING);
193 reencode(&ret->msg, ret->msg_encoding, PAGE_ENCODING);
198 struct taginfo *cgit_parse_tag(struct tag *tag)
201 enum object_type type;
204 struct taginfo *ret = NULL;
206 data = read_sha1_file(tag->object.sha1, &type, &size);
207 if (!data || type != OBJ_TAG)
210 ret = xcalloc(1, sizeof(struct taginfo));
212 for (p = data; !end_of_header(p); p = next_header_line(p)) {
213 if (skip_prefix(p, "tagger ", &p)) {
214 p = parse_user(p, &ret->tagger, &ret->tagger_email,
219 while (p && *p == '\n')
223 ret->msg = xstrdup(p);