blob: 9dacb16619a1e8202e517342b3ab06db2f4bf53b [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 Keeping45c87ca2016-01-19 19:33:02 +000072static void parse_user(const char *t, char **name, char **email, unsigned long *date, int *tz)
Lars Hjemlia8305a92008-09-14 09:45:37 +020073{
Lukas Fleischer6f9e8a92014-12-24 08:50:11 +010074 struct ident_split ident;
75 unsigned email_len;
Lars Hjemlia8305a92008-09-14 09:45:37 +020076
Lukas Fleischer3cb8e762015-03-05 12:58:12 +010077 if (!split_ident_line(&ident, t, strchrnul(t, '\n') - t)) {
Lukas Fleischer6f9e8a92014-12-24 08:50:11 +010078 *name = substr(ident.name_begin, ident.name_end);
79
80 email_len = ident.mail_end - ident.mail_begin;
81 *email = xmalloc(strlen("<") + email_len + strlen(">") + 1);
82 sprintf(*email, "<%.*s>", email_len, ident.mail_begin);
83
84 if (ident.date_begin)
85 *date = strtoul(ident.date_begin, NULL, 10);
John Keeping45c87ca2016-01-19 19:33:02 +000086 if (ident.tz_begin)
87 *tz = atoi(ident.tz_begin);
Lars Hjemlia8305a92008-09-14 09:45:37 +020088 }
Lars Hjemlia8305a92008-09-14 09:45:37 +020089}
90
Lars Hjemli14b4e102008-12-05 19:10:28 +010091#ifdef NO_ICONV
92#define reencode(a, b, c)
93#else
Lukas Fleischerbafab422013-03-04 08:52:33 +010094static const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
Lars Hjemlia8305a92008-09-14 09:45:37 +020095{
96 char *tmp;
97
Lukas Fleischera0bf3752011-04-05 10:35:43 +020098 if (!txt)
99 return NULL;
100
101 if (!*txt || !src_enc || !dst_enc)
Lars Hjemlia8305a92008-09-14 09:45:37 +0200102 return *txt;
103
Julius Plenz0a799422011-03-10 17:03:23 +0100104 /* no encoding needed if src_enc equals dst_enc */
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500105 if (!strcasecmp(src_enc, dst_enc))
Julius Plenz0a799422011-03-10 17:03:23 +0100106 return *txt;
107
108 tmp = reencode_string(*txt, dst_enc, src_enc);
Lars Hjemlia8305a92008-09-14 09:45:37 +0200109 if (tmp) {
110 free(*txt);
111 *txt = tmp;
112 }
113 return *txt;
114}
Lars Hjemli14b4e102008-12-05 19:10:28 +0100115#endif
Lars Hjemlia8305a92008-09-14 09:45:37 +0200116
Lukas Fleischer936295c2015-03-03 13:00:07 +0100117static const char *next_header_line(const char *p)
118{
119 p = strchr(p, '\n');
120 if (!p)
121 return NULL;
122 return p + 1;
123}
124
125static int end_of_header(const char *p)
126{
127 return !p || (*p == '\n');
128}
129
Lars Hjemli2101e262006-12-15 18:17:36 +0100130struct commitinfo *cgit_parse_commit(struct commit *commit)
131{
Lukas Fleischer936295c2015-03-03 13:00:07 +0100132 const int sha1hex_len = 40;
Lars Hjemli2101e262006-12-15 18:17:36 +0100133 struct commitinfo *ret;
John Keeping865afe02014-07-27 11:56:19 +0100134 const char *p = get_cached_commit_buffer(commit, NULL);
135 const char *t;
Lars Hjemli2101e262006-12-15 18:17:36 +0100136
Lukas Fleischer936295c2015-03-03 13:00:07 +0100137 ret = xcalloc(1, sizeof(struct commitinfo));
Lars Hjemli2101e262006-12-15 18:17:36 +0100138 ret->commit = commit;
139
Lukas Fleischer936295c2015-03-03 13:00:07 +0100140 if (!p)
Ondrej Jirman61302312007-05-26 03:27:49 +0200141 return ret;
142
Lukas Fleischer936295c2015-03-03 13:00:07 +0100143 if (!skip_prefix(p, "tree ", &p))
Christian Hesse559ab5e2016-01-05 07:38:53 +0100144 die("Bad commit: %s", oid_to_hex(&commit->object.oid));
Lukas Fleischer936295c2015-03-03 13:00:07 +0100145 p += sha1hex_len + 1;
Lars Hjemli2101e262006-12-15 18:17:36 +0100146
Lukas Fleischer936295c2015-03-03 13:00:07 +0100147 while (skip_prefix(p, "parent ", &p))
148 p += sha1hex_len + 1;
Lars Hjemli2101e262006-12-15 18:17:36 +0100149
Lukas Fleischer936295c2015-03-03 13:00:07 +0100150 if (p && skip_prefix(p, "author ", &p)) {
Lukas Fleischer3cb8e762015-03-05 12:58:12 +0100151 parse_user(p, &ret->author, &ret->author_email,
John Keeping45c87ca2016-01-19 19:33:02 +0000152 &ret->author_date, &ret->author_tz);
Lukas Fleischer3cb8e762015-03-05 12:58:12 +0100153 p = next_header_line(p);
Lars Hjemli2101e262006-12-15 18:17:36 +0100154 }
155
Lukas Fleischer936295c2015-03-03 13:00:07 +0100156 if (p && skip_prefix(p, "committer ", &p)) {
Lukas Fleischer3cb8e762015-03-05 12:58:12 +0100157 parse_user(p, &ret->committer, &ret->committer_email,
John Keeping45c87ca2016-01-19 19:33:02 +0000158 &ret->committer_date, &ret->committer_tz);
Lukas Fleischer3cb8e762015-03-05 12:58:12 +0100159 p = next_header_line(p);
Lars Hjemli2101e262006-12-15 18:17:36 +0100160 }
161
Lukas Fleischer936295c2015-03-03 13:00:07 +0100162 if (p && skip_prefix(p, "encoding ", &p)) {
Lars Hjemlia8305a92008-09-14 09:45:37 +0200163 t = strchr(p, '\n');
164 if (t) {
165 ret->msg_encoding = substr(p, t + 1);
166 p = t + 1;
167 }
168 }
Jonathan Bastien-Filiatraultaa5cc322007-10-26 18:10:26 -0400169
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500170 if (!ret->msg_encoding)
Julius Plenz0a799422011-03-10 17:03:23 +0100171 ret->msg_encoding = xstrdup("UTF-8");
172
Lukas Fleischer936295c2015-03-03 13:00:07 +0100173 while (!end_of_header(p))
174 p = next_header_line(p);
Lars Hjemlia8305a92008-09-14 09:45:37 +0200175 while (p && *p == '\n')
176 p++;
Lars Hjemlia8305a92008-09-14 09:45:37 +0200177 if (!p)
178 return ret;
Lars Hjemli2101e262006-12-15 18:17:36 +0100179
Lukas Fleischer936295c2015-03-03 13:00:07 +0100180 t = strchrnul(p, '\n');
181 ret->subject = substr(p, t);
182 while (*t == '\n')
183 t++;
184 ret->msg = xstrdup(t);
Ondrej Jirman3ce6fc12007-05-26 02:19:38 +0200185
Julius Plenz0a799422011-03-10 17:03:23 +0100186 reencode(&ret->author, ret->msg_encoding, PAGE_ENCODING);
187 reencode(&ret->author_email, ret->msg_encoding, PAGE_ENCODING);
188 reencode(&ret->committer, ret->msg_encoding, PAGE_ENCODING);
189 reencode(&ret->committer_email, ret->msg_encoding, PAGE_ENCODING);
190 reencode(&ret->subject, ret->msg_encoding, PAGE_ENCODING);
191 reencode(&ret->msg, ret->msg_encoding, PAGE_ENCODING);
Jonathan Bastien-Filiatrault7858a302007-10-26 18:13:41 -0400192
Lars Hjemli2101e262006-12-15 18:17:36 +0100193 return ret;
194}
Lars Hjemlia69061f2007-01-17 01:09:51 +0100195
Lars Hjemlia69061f2007-01-17 01:09:51 +0100196struct taginfo *cgit_parse_tag(struct tag *tag)
197{
198 void *data;
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200199 enum object_type type;
Lars Hjemlia69061f2007-01-17 01:09:51 +0100200 unsigned long size;
John Keeping93d8ef82014-07-27 11:56:18 +0100201 const char *p;
Lukas Fleischer936295c2015-03-03 13:00:07 +0100202 struct taginfo *ret = NULL;
Lars Hjemlia69061f2007-01-17 01:09:51 +0100203
Christian Hesse559ab5e2016-01-05 07:38:53 +0100204 data = read_sha1_file(tag->object.oid.hash, &type, &size);
Lukas Fleischer936295c2015-03-03 13:00:07 +0100205 if (!data || type != OBJ_TAG)
206 goto cleanup;
Lars Hjemli47a81c72007-05-15 23:28:40 +0200207
Lukas Fleischer936295c2015-03-03 13:00:07 +0100208 ret = xcalloc(1, sizeof(struct taginfo));
Lars Hjemlia69061f2007-01-17 01:09:51 +0100209
Lukas Fleischer936295c2015-03-03 13:00:07 +0100210 for (p = data; !end_of_header(p); p = next_header_line(p)) {
211 if (skip_prefix(p, "tagger ", &p)) {
Lukas Fleischer3cb8e762015-03-05 12:58:12 +0100212 parse_user(p, &ret->tagger, &ret->tagger_email,
John Keeping45c87ca2016-01-19 19:33:02 +0000213 &ret->tagger_date, &ret->tagger_tz);
Lars Hjemlia69061f2007-01-17 01:09:51 +0100214 }
Lars Hjemlia69061f2007-01-17 01:09:51 +0100215 }
216
Lukas Fleischer4d8ed332015-03-05 12:58:11 +0100217 while (p && *p == '\n')
218 p++;
219
Lars Hjemliebd7b0f2007-02-03 16:11:41 +0100220 if (p && *p)
Lars Hjemlia69061f2007-01-17 01:09:51 +0100221 ret->msg = xstrdup(p);
Lukas Fleischer936295c2015-03-03 13:00:07 +0100222
223cleanup:
Lars Hjemlia69061f2007-01-17 01:09:51 +0100224 free(data);
225 return ret;
226}