blob: 12453c2db289e1c7812de2cbcdbe128275930f92 [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 Hjemli30ccdca2007-05-18 03:00:54 +020023 if (!url || url[0] == '\0')
24 return;
25
John Keeping9d751e72017-10-14 13:02:53 +010026 ctx.qry.page = NULL;
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 }
57}
58
Lukas Fleischerbafab422013-03-04 08:52:33 +010059static char *substr(const char *head, const char *tail)
Lars Hjemli2101e262006-12-15 18:17:36 +010060{
61 char *buf;
62
Jim Meyering61d41472012-04-23 22:06:35 +020063 if (tail < head)
64 return xstrdup("");
Lars Hjemli2101e262006-12-15 18:17:36 +010065 buf = xmalloc(tail - head + 1);
66 strncpy(buf, head, tail - head);
67 buf[tail - head] = '\0';
68 return buf;
69}
70
John Keeping45c87ca2016-01-19 19:33:02 +000071static void parse_user(const char *t, char **name, char **email, unsigned long *date, int *tz)
Lars Hjemlia8305a92008-09-14 09:45:37 +020072{
Lukas Fleischer6f9e8a92014-12-24 08:50:11 +010073 struct ident_split ident;
74 unsigned email_len;
Lars Hjemlia8305a92008-09-14 09:45:37 +020075
Lukas Fleischer3cb8e762015-03-05 12:58:12 +010076 if (!split_ident_line(&ident, t, strchrnul(t, '\n') - t)) {
Lukas Fleischer6f9e8a92014-12-24 08:50:11 +010077 *name = substr(ident.name_begin, ident.name_end);
78
79 email_len = ident.mail_end - ident.mail_begin;
80 *email = xmalloc(strlen("<") + email_len + strlen(">") + 1);
81 sprintf(*email, "<%.*s>", email_len, ident.mail_begin);
82
83 if (ident.date_begin)
84 *date = strtoul(ident.date_begin, NULL, 10);
John Keeping45c87ca2016-01-19 19:33:02 +000085 if (ident.tz_begin)
86 *tz = atoi(ident.tz_begin);
Lars Hjemlia8305a92008-09-14 09:45:37 +020087 }
Lars Hjemlia8305a92008-09-14 09:45:37 +020088}
89
Lars Hjemli14b4e102008-12-05 19:10:28 +010090#ifdef NO_ICONV
91#define reencode(a, b, c)
92#else
Lukas Fleischerbafab422013-03-04 08:52:33 +010093static const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
Lars Hjemlia8305a92008-09-14 09:45:37 +020094{
95 char *tmp;
96
Lukas Fleischera0bf3752011-04-05 10:35:43 +020097 if (!txt)
98 return NULL;
99
100 if (!*txt || !src_enc || !dst_enc)
Lars Hjemlia8305a92008-09-14 09:45:37 +0200101 return *txt;
102
Julius Plenz0a799422011-03-10 17:03:23 +0100103 /* no encoding needed if src_enc equals dst_enc */
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500104 if (!strcasecmp(src_enc, dst_enc))
Julius Plenz0a799422011-03-10 17:03:23 +0100105 return *txt;
106
107 tmp = reencode_string(*txt, dst_enc, src_enc);
Lars Hjemlia8305a92008-09-14 09:45:37 +0200108 if (tmp) {
109 free(*txt);
110 *txt = tmp;
111 }
112 return *txt;
113}
Lars Hjemli14b4e102008-12-05 19:10:28 +0100114#endif
Lars Hjemlia8305a92008-09-14 09:45:37 +0200115
Lukas Fleischer936295c2015-03-03 13:00:07 +0100116static const char *next_header_line(const char *p)
117{
118 p = strchr(p, '\n');
119 if (!p)
120 return NULL;
121 return p + 1;
122}
123
124static int end_of_header(const char *p)
125{
126 return !p || (*p == '\n');
127}
128
Lars Hjemli2101e262006-12-15 18:17:36 +0100129struct commitinfo *cgit_parse_commit(struct commit *commit)
130{
Lukas Fleischer936295c2015-03-03 13:00:07 +0100131 const int sha1hex_len = 40;
Lars Hjemli2101e262006-12-15 18:17:36 +0100132 struct commitinfo *ret;
John Keeping865afe02014-07-27 11:56:19 +0100133 const char *p = get_cached_commit_buffer(commit, NULL);
134 const char *t;
Lars Hjemli2101e262006-12-15 18:17:36 +0100135
Lukas Fleischer936295c2015-03-03 13:00:07 +0100136 ret = xcalloc(1, sizeof(struct commitinfo));
Lars Hjemli2101e262006-12-15 18:17:36 +0100137 ret->commit = commit;
138
Lukas Fleischer936295c2015-03-03 13:00:07 +0100139 if (!p)
Ondrej Jirman61302312007-05-26 03:27:49 +0200140 return ret;
141
Lukas Fleischer936295c2015-03-03 13:00:07 +0100142 if (!skip_prefix(p, "tree ", &p))
Christian Hesse559ab5e2016-01-05 07:38:53 +0100143 die("Bad commit: %s", oid_to_hex(&commit->object.oid));
Lukas Fleischer936295c2015-03-03 13:00:07 +0100144 p += sha1hex_len + 1;
Lars Hjemli2101e262006-12-15 18:17:36 +0100145
Lukas Fleischer936295c2015-03-03 13:00:07 +0100146 while (skip_prefix(p, "parent ", &p))
147 p += sha1hex_len + 1;
Lars Hjemli2101e262006-12-15 18:17:36 +0100148
Lukas Fleischer936295c2015-03-03 13:00:07 +0100149 if (p && skip_prefix(p, "author ", &p)) {
Lukas Fleischer3cb8e762015-03-05 12:58:12 +0100150 parse_user(p, &ret->author, &ret->author_email,
John Keeping45c87ca2016-01-19 19:33:02 +0000151 &ret->author_date, &ret->author_tz);
Lukas Fleischer3cb8e762015-03-05 12:58:12 +0100152 p = next_header_line(p);
Lars Hjemli2101e262006-12-15 18:17:36 +0100153 }
154
Lukas Fleischer936295c2015-03-03 13:00:07 +0100155 if (p && skip_prefix(p, "committer ", &p)) {
Lukas Fleischer3cb8e762015-03-05 12:58:12 +0100156 parse_user(p, &ret->committer, &ret->committer_email,
John Keeping45c87ca2016-01-19 19:33:02 +0000157 &ret->committer_date, &ret->committer_tz);
Lukas Fleischer3cb8e762015-03-05 12:58:12 +0100158 p = next_header_line(p);
Lars Hjemli2101e262006-12-15 18:17:36 +0100159 }
160
Lukas Fleischer936295c2015-03-03 13:00:07 +0100161 if (p && skip_prefix(p, "encoding ", &p)) {
Lars Hjemlia8305a92008-09-14 09:45:37 +0200162 t = strchr(p, '\n');
163 if (t) {
164 ret->msg_encoding = substr(p, t + 1);
165 p = t + 1;
166 }
167 }
Jonathan Bastien-Filiatraultaa5cc322007-10-26 18:10:26 -0400168
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500169 if (!ret->msg_encoding)
Julius Plenz0a799422011-03-10 17:03:23 +0100170 ret->msg_encoding = xstrdup("UTF-8");
171
Lukas Fleischer936295c2015-03-03 13:00:07 +0100172 while (!end_of_header(p))
173 p = next_header_line(p);
Lars Hjemlia8305a92008-09-14 09:45:37 +0200174 while (p && *p == '\n')
175 p++;
Lars Hjemlia8305a92008-09-14 09:45:37 +0200176 if (!p)
177 return ret;
Lars Hjemli2101e262006-12-15 18:17:36 +0100178
Lukas Fleischer936295c2015-03-03 13:00:07 +0100179 t = strchrnul(p, '\n');
180 ret->subject = substr(p, t);
181 while (*t == '\n')
182 t++;
183 ret->msg = xstrdup(t);
Ondrej Jirman3ce6fc12007-05-26 02:19:38 +0200184
Julius Plenz0a799422011-03-10 17:03:23 +0100185 reencode(&ret->author, ret->msg_encoding, PAGE_ENCODING);
186 reencode(&ret->author_email, ret->msg_encoding, PAGE_ENCODING);
187 reencode(&ret->committer, ret->msg_encoding, PAGE_ENCODING);
188 reencode(&ret->committer_email, ret->msg_encoding, PAGE_ENCODING);
189 reencode(&ret->subject, ret->msg_encoding, PAGE_ENCODING);
190 reencode(&ret->msg, ret->msg_encoding, PAGE_ENCODING);
Jonathan Bastien-Filiatrault7858a302007-10-26 18:13:41 -0400191
Lars Hjemli2101e262006-12-15 18:17:36 +0100192 return ret;
193}
Lars Hjemlia69061f2007-01-17 01:09:51 +0100194
Lars Hjemlia69061f2007-01-17 01:09:51 +0100195struct taginfo *cgit_parse_tag(struct tag *tag)
196{
197 void *data;
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200198 enum object_type type;
Lars Hjemlia69061f2007-01-17 01:09:51 +0100199 unsigned long size;
John Keeping93d8ef82014-07-27 11:56:18 +0100200 const char *p;
Lukas Fleischer936295c2015-03-03 13:00:07 +0100201 struct taginfo *ret = NULL;
Lars Hjemlia69061f2007-01-17 01:09:51 +0100202
Christian Hesse255b78f2018-06-04 18:49:28 +0200203 data = read_object_file(&tag->object.oid, &type, &size);
Lukas Fleischer936295c2015-03-03 13:00:07 +0100204 if (!data || type != OBJ_TAG)
205 goto cleanup;
Lars Hjemli47a81c72007-05-15 23:28:40 +0200206
Lukas Fleischer936295c2015-03-03 13:00:07 +0100207 ret = xcalloc(1, sizeof(struct taginfo));
Lars Hjemlia69061f2007-01-17 01:09:51 +0100208
Lukas Fleischer936295c2015-03-03 13:00:07 +0100209 for (p = data; !end_of_header(p); p = next_header_line(p)) {
210 if (skip_prefix(p, "tagger ", &p)) {
Lukas Fleischer3cb8e762015-03-05 12:58:12 +0100211 parse_user(p, &ret->tagger, &ret->tagger_email,
John Keeping45c87ca2016-01-19 19:33:02 +0000212 &ret->tagger_date, &ret->tagger_tz);
Lars Hjemlia69061f2007-01-17 01:09:51 +0100213 }
Lars Hjemlia69061f2007-01-17 01:09:51 +0100214 }
215
Lukas Fleischer4d8ed332015-03-05 12:58:11 +0100216 while (p && *p == '\n')
217 p++;
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}