blob: 0db181bdf7e1c1b339a8f5d152ee2bde9b45f9b3 [file] [log] [blame]
Jason A. Donenfeldd01a6ee2014-01-10 04:59:34 +01001/* parsing.c: parsing of config files
Lars Hjemli7640d902006-12-10 22:41:14 +01002 *
Lukas Fleischerf7f26f82014-01-08 15:10:49 +01003 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemli7640d902006-12-10 22:41:14 +01004 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
Lars Hjemli0d169ad2006-12-09 15:18:17 +01009#include "cgit.h"
10
Lars Hjemli30ccdca2007-05-18 03:00:54 +020011/*
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{
Julian Maurice10451792014-03-28 23:18:29 +010020 char *c, *cmd, *p;
21 struct cgit_repo *repo;
Lars Hjemli30ccdca2007-05-18 03:00:54 +020022
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010023 ctx.repo = NULL;
Lars Hjemli30ccdca2007-05-18 03:00:54 +020024 if (!url || url[0] == '\0')
25 return;
26
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010027 ctx.repo = cgit_get_repoinfo(url);
28 if (ctx.repo) {
29 ctx.qry.repo = ctx.repo->url;
Lars Hjemli30ccdca2007-05-18 03:00:54 +020030 return;
31 }
32
Julian Maurice10451792014-03-28 23:18:29 +010033 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;
Lars Hjemli30ccdca2007-05-18 03:00:54 +020041 }
Julian Maurice10451792014-03-28 23:18:29 +010042 c[0] = '/';
43 c = strchr(c + 1, '/');
44 }
Lars Hjemli30ccdca2007-05-18 03:00:54 +020045
Julian Maurice10451792014-03-28 23:18:29 +010046 if (ctx.repo) {
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010047 ctx.qry.repo = ctx.repo->url;
Lars Hjemli30ccdca2007-05-18 03:00:54 +020048 p = strchr(cmd + 1, '/');
49 if (p) {
50 p[0] = '\0';
Lars Hjemli3de63b22007-05-18 13:06:45 +020051 if (p[1])
Lars Hjemlid14d77f2008-02-16 11:53:40 +010052 ctx.qry.path = trim_end(p + 1, '/');
Lars Hjemli30ccdca2007-05-18 03:00:54 +020053 }
Lars Hjemlie0e44782008-03-24 01:09:39 +010054 if (cmd[1])
55 ctx.qry.page = xstrdup(cmd + 1);
Lars Hjemli30ccdca2007-05-18 03:00:54 +020056 return;
57 }
58}
59
Lukas Fleischerbafab422013-03-04 08:52:33 +010060static char *substr(const char *head, const char *tail)
Lars Hjemli2101e262006-12-15 18:17:36 +010061{
62 char *buf;
63
Jim Meyering61d41472012-04-23 22:06:35 +020064 if (tail < head)
65 return xstrdup("");
Lars Hjemli2101e262006-12-15 18:17:36 +010066 buf = xmalloc(tail - head + 1);
67 strncpy(buf, head, tail - head);
68 buf[tail - head] = '\0';
69 return buf;
70}
71
John Keeping93d8ef82014-07-27 11:56:18 +010072static const char *parse_user(const char *t, char **name, char **email, unsigned long *date)
Lars Hjemlia8305a92008-09-14 09:45:37 +020073{
Lukas Fleischer6f9e8a92014-12-24 08:50:11 +010074 const char *line_end = strchrnul(t, '\n');
75 struct ident_split ident;
76 unsigned email_len;
Lars Hjemlia8305a92008-09-14 09:45:37 +020077
Lukas Fleischer6f9e8a92014-12-24 08:50:11 +010078 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);
Lars Hjemlia8305a92008-09-14 09:45:37 +020087 }
Lukas Fleischer6f9e8a92014-12-24 08:50:11 +010088
89 if (*line_end)
90 return line_end + 1;
91 else
92 return line_end;
Lars Hjemlia8305a92008-09-14 09:45:37 +020093}
94
Lars Hjemli14b4e102008-12-05 19:10:28 +010095#ifdef NO_ICONV
96#define reencode(a, b, c)
97#else
Lukas Fleischerbafab422013-03-04 08:52:33 +010098static const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
Lars Hjemlia8305a92008-09-14 09:45:37 +020099{
100 char *tmp;
101
Lukas Fleischera0bf3752011-04-05 10:35:43 +0200102 if (!txt)
103 return NULL;
104
105 if (!*txt || !src_enc || !dst_enc)
Lars Hjemlia8305a92008-09-14 09:45:37 +0200106 return *txt;
107
Julius Plenz0a799422011-03-10 17:03:23 +0100108 /* no encoding needed if src_enc equals dst_enc */
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500109 if (!strcasecmp(src_enc, dst_enc))
Julius Plenz0a799422011-03-10 17:03:23 +0100110 return *txt;
111
112 tmp = reencode_string(*txt, dst_enc, src_enc);
Lars Hjemlia8305a92008-09-14 09:45:37 +0200113 if (tmp) {
114 free(*txt);
115 *txt = tmp;
116 }
117 return *txt;
118}
Lars Hjemli14b4e102008-12-05 19:10:28 +0100119#endif
Lars Hjemlia8305a92008-09-14 09:45:37 +0200120
Lukas Fleischer936295c2015-03-03 13:00:07 +0100121static const char *next_header_line(const char *p)
122{
123 p = strchr(p, '\n');
124 if (!p)
125 return NULL;
126 return p + 1;
127}
128
129static int end_of_header(const char *p)
130{
131 return !p || (*p == '\n');
132}
133
Lars Hjemli2101e262006-12-15 18:17:36 +0100134struct commitinfo *cgit_parse_commit(struct commit *commit)
135{
Lukas Fleischer936295c2015-03-03 13:00:07 +0100136 const int sha1hex_len = 40;
Lars Hjemli2101e262006-12-15 18:17:36 +0100137 struct commitinfo *ret;
John Keeping865afe02014-07-27 11:56:19 +0100138 const char *p = get_cached_commit_buffer(commit, NULL);
139 const char *t;
Lars Hjemli2101e262006-12-15 18:17:36 +0100140
Lukas Fleischer936295c2015-03-03 13:00:07 +0100141 ret = xcalloc(1, sizeof(struct commitinfo));
Lars Hjemli2101e262006-12-15 18:17:36 +0100142 ret->commit = commit;
143
Lukas Fleischer936295c2015-03-03 13:00:07 +0100144 if (!p)
Ondrej Jirman61302312007-05-26 03:27:49 +0200145 return ret;
146
Lukas Fleischer936295c2015-03-03 13:00:07 +0100147 if (!skip_prefix(p, "tree ", &p))
Lars Hjemli2101e262006-12-15 18:17:36 +0100148 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
Lukas Fleischer936295c2015-03-03 13:00:07 +0100149 p += sha1hex_len + 1;
Lars Hjemli2101e262006-12-15 18:17:36 +0100150
Lukas Fleischer936295c2015-03-03 13:00:07 +0100151 while (skip_prefix(p, "parent ", &p))
152 p += sha1hex_len + 1;
Lars Hjemli2101e262006-12-15 18:17:36 +0100153
Lukas Fleischer936295c2015-03-03 13:00:07 +0100154 if (p && skip_prefix(p, "author ", &p)) {
155 p = parse_user(p, &ret->author, &ret->author_email,
Lars Hjemlia8305a92008-09-14 09:45:37 +0200156 &ret->author_date);
Lars Hjemli2101e262006-12-15 18:17:36 +0100157 }
158
Lukas Fleischer936295c2015-03-03 13:00:07 +0100159 if (p && skip_prefix(p, "committer ", &p)) {
160 p = parse_user(p, &ret->committer, &ret->committer_email,
Lars Hjemlia8305a92008-09-14 09:45:37 +0200161 &ret->committer_date);
Lars Hjemli2101e262006-12-15 18:17:36 +0100162 }
163
Lukas Fleischer936295c2015-03-03 13:00:07 +0100164 if (p && skip_prefix(p, "encoding ", &p)) {
Lars Hjemlia8305a92008-09-14 09:45:37 +0200165 t = strchr(p, '\n');
166 if (t) {
167 ret->msg_encoding = substr(p, t + 1);
168 p = t + 1;
169 }
170 }
Jonathan Bastien-Filiatraultaa5cc322007-10-26 18:10:26 -0400171
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500172 if (!ret->msg_encoding)
Julius Plenz0a799422011-03-10 17:03:23 +0100173 ret->msg_encoding = xstrdup("UTF-8");
174
Lukas Fleischer936295c2015-03-03 13:00:07 +0100175 while (!end_of_header(p))
176 p = next_header_line(p);
Lars Hjemlia8305a92008-09-14 09:45:37 +0200177 while (p && *p == '\n')
178 p++;
Lars Hjemlia8305a92008-09-14 09:45:37 +0200179 if (!p)
180 return ret;
Lars Hjemli2101e262006-12-15 18:17:36 +0100181
Lukas Fleischer936295c2015-03-03 13:00:07 +0100182 t = strchrnul(p, '\n');
183 ret->subject = substr(p, t);
184 while (*t == '\n')
185 t++;
186 ret->msg = xstrdup(t);
Ondrej Jirman3ce6fc12007-05-26 02:19:38 +0200187
Julius Plenz0a799422011-03-10 17:03:23 +0100188 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);
Jonathan Bastien-Filiatrault7858a302007-10-26 18:13:41 -0400194
Lars Hjemli2101e262006-12-15 18:17:36 +0100195 return ret;
196}
Lars Hjemlia69061f2007-01-17 01:09:51 +0100197
Lars Hjemlia69061f2007-01-17 01:09:51 +0100198struct taginfo *cgit_parse_tag(struct tag *tag)
199{
200 void *data;
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200201 enum object_type type;
Lars Hjemlia69061f2007-01-17 01:09:51 +0100202 unsigned long size;
John Keeping93d8ef82014-07-27 11:56:18 +0100203 const char *p;
Lukas Fleischer936295c2015-03-03 13:00:07 +0100204 struct taginfo *ret = NULL;
Lars Hjemlia69061f2007-01-17 01:09:51 +0100205
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200206 data = read_sha1_file(tag->object.sha1, &type, &size);
Lukas Fleischer936295c2015-03-03 13:00:07 +0100207 if (!data || type != OBJ_TAG)
208 goto cleanup;
Lars Hjemli47a81c72007-05-15 23:28:40 +0200209
Lukas Fleischer936295c2015-03-03 13:00:07 +0100210 ret = xcalloc(1, sizeof(struct taginfo));
Lars Hjemlia69061f2007-01-17 01:09:51 +0100211
Lukas Fleischer936295c2015-03-03 13:00:07 +0100212 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,
Lars Hjemlia8305a92008-09-14 09:45:37 +0200215 &ret->tagger_date);
Lars Hjemlia69061f2007-01-17 01:09:51 +0100216 }
Lars Hjemlia69061f2007-01-17 01:09:51 +0100217 }
218
Lars Hjemliebd7b0f2007-02-03 16:11:41 +0100219 if (p && *p)
Lars Hjemlia69061f2007-01-17 01:09:51 +0100220 ret->msg = xstrdup(p);
Lukas Fleischer936295c2015-03-03 13:00:07 +0100221
222cleanup:
Lars Hjemlia69061f2007-01-17 01:09:51 +0100223 free(data);
224 return ret;
225}