all repos — cgit @ 1a9e56607eae2df2f4522b41294d94cb09fc4e5c

a hyperfast web frontend for git written in c

parsing.c (view raw)

  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 */
 18void 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
 60static 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
 72static const char *parse_user(const char *t, char **name, char **email, unsigned long *date)
 73{
 74	const char *line_end = strchrnul(t, '\n');
 75	struct ident_split ident;
 76	unsigned email_len;
 77
 78	if (!split_ident_line(&ident, t, line_end - t)) {
 79		*name = substr(ident.name_begin, ident.name_end);
 80
 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);
 84
 85		if (ident.date_begin)
 86			*date = strtoul(ident.date_begin, NULL, 10);
 87	}
 88
 89	if (*line_end)
 90		return line_end + 1;
 91	else
 92		return line_end;
 93}
 94
 95#ifdef NO_ICONV
 96#define reencode(a, b, c)
 97#else
 98static const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
 99{
100	char *tmp;
101
102	if (!txt)
103		return NULL;
104
105	if (!*txt || !src_enc || !dst_enc)
106		return *txt;
107
108	/* no encoding needed if src_enc equals dst_enc */
109	if (!strcasecmp(src_enc, dst_enc))
110		return *txt;
111
112	tmp = reencode_string(*txt, dst_enc, src_enc);
113	if (tmp) {
114		free(*txt);
115		*txt = tmp;
116	}
117	return *txt;
118}
119#endif
120
121struct commitinfo *cgit_parse_commit(struct commit *commit)
122{
123	struct commitinfo *ret;
124	const char *p = get_cached_commit_buffer(commit, NULL);
125	const char *t;
126
127	ret = xmalloc(sizeof(*ret));
128	ret->commit = commit;
129	ret->author = NULL;
130	ret->author_email = NULL;
131	ret->committer = NULL;
132	ret->committer_email = NULL;
133	ret->subject = NULL;
134	ret->msg = NULL;
135	ret->msg_encoding = NULL;
136
137	if (p == NULL)
138		return ret;
139
140	if (!starts_with(p, "tree "))
141		die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
142	else
143		p += 46; // "tree " + hex[40] + "\n"
144
145	while (starts_with(p, "parent "))
146		p += 48; // "parent " + hex[40] + "\n"
147
148	if (p && starts_with(p, "author ")) {
149		p = parse_user(p + 7, &ret->author, &ret->author_email,
150			&ret->author_date);
151	}
152
153	if (p && starts_with(p, "committer ")) {
154		p = parse_user(p + 10, &ret->committer, &ret->committer_email,
155			&ret->committer_date);
156	}
157
158	if (p && starts_with(p, "encoding ")) {
159		p += 9;
160		t = strchr(p, '\n');
161		if (t) {
162			ret->msg_encoding = substr(p, t + 1);
163			p = t + 1;
164		}
165	}
166
167	/* if no special encoding is found, assume UTF-8 */
168	if (!ret->msg_encoding)
169		ret->msg_encoding = xstrdup("UTF-8");
170
171	// skip unknown header fields
172	while (p && *p && (*p != '\n')) {
173		p = strchr(p, '\n');
174		if (p)
175			p++;
176	}
177
178	// skip empty lines between headers and message
179	while (p && *p == '\n')
180		p++;
181
182	if (!p)
183		return ret;
184
185	t = strchr(p, '\n');
186	if (t) {
187		ret->subject = substr(p, t);
188		p = t + 1;
189
190		while (p && *p == '\n') {
191			p = strchr(p, '\n');
192			if (p)
193				p++;
194		}
195		if (p)
196			ret->msg = xstrdup(p);
197	} else
198		ret->subject = xstrdup(p);
199
200	reencode(&ret->author, ret->msg_encoding, PAGE_ENCODING);
201	reencode(&ret->author_email, ret->msg_encoding, PAGE_ENCODING);
202	reencode(&ret->committer, ret->msg_encoding, PAGE_ENCODING);
203	reencode(&ret->committer_email, ret->msg_encoding, PAGE_ENCODING);
204	reencode(&ret->subject, ret->msg_encoding, PAGE_ENCODING);
205	reencode(&ret->msg, ret->msg_encoding, PAGE_ENCODING);
206
207	return ret;
208}
209
210
211struct taginfo *cgit_parse_tag(struct tag *tag)
212{
213	void *data;
214	enum object_type type;
215	unsigned long size;
216	const char *p;
217	struct taginfo *ret;
218
219	data = read_sha1_file(tag->object.sha1, &type, &size);
220	if (!data || type != OBJ_TAG) {
221		free(data);
222		return 0;
223	}
224
225	ret = xmalloc(sizeof(*ret));
226	ret->tagger = NULL;
227	ret->tagger_email = NULL;
228	ret->tagger_date = 0;
229	ret->msg = NULL;
230
231	p = data;
232
233	while (p && *p) {
234		if (*p == '\n')
235			break;
236
237		if (starts_with(p, "tagger ")) {
238			p = parse_user(p + 7, &ret->tagger, &ret->tagger_email,
239				&ret->tagger_date);
240		} else {
241			p = strchr(p, '\n');
242			if (p)
243				p++;
244		}
245	}
246
247	// skip empty lines between headers and message
248	while (p && *p == '\n')
249		p++;
250
251	if (p && *p)
252		ret->msg = xstrdup(p);
253	free(data);
254	return ret;
255}