]> gitweb.ps.run Git - ps-cgit/blob - parsing.c
git: update to v2.46.0
[ps-cgit] / parsing.c
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 #define USE_THE_REPOSITORY_VARIABLE
10
11 #include "cgit.h"
12
13 /*
14  * url syntax: [repo ['/' cmd [ '/' path]]]
15  *   repo: any valid repo url, may contain '/'
16  *   cmd:  log | commit | diff | tree | view | blob | snapshot
17  *   path: any valid path, may contain '/'
18  *
19  */
20 void cgit_parse_url(const char *url)
21 {
22         char *c, *cmd, *p;
23         struct cgit_repo *repo;
24
25         if (!url || url[0] == '\0')
26                 return;
27
28         ctx.qry.page = NULL;
29         ctx.repo = cgit_get_repoinfo(url);
30         if (ctx.repo) {
31                 ctx.qry.repo = ctx.repo->url;
32                 return;
33         }
34
35         cmd = NULL;
36         c = strchr(url, '/');
37         while (c) {
38                 c[0] = '\0';
39                 repo = cgit_get_repoinfo(url);
40                 if (repo) {
41                         ctx.repo = repo;
42                         cmd = c;
43                 }
44                 c[0] = '/';
45                 c = strchr(c + 1, '/');
46         }
47
48         if (ctx.repo) {
49                 ctx.qry.repo = ctx.repo->url;
50                 p = strchr(cmd + 1, '/');
51                 if (p) {
52                         p[0] = '\0';
53                         if (p[1])
54                                 ctx.qry.path = trim_end(p + 1, '/');
55                 }
56                 if (cmd[1])
57                         ctx.qry.page = xstrdup(cmd + 1);
58         }
59 }
60
61 static char *substr(const char *head, const char *tail)
62 {
63         char *buf;
64
65         if (tail < head)
66                 return xstrdup("");
67         buf = xmalloc(tail - head + 1);
68         strlcpy(buf, head, tail - head + 1);
69         return buf;
70 }
71
72 static void parse_user(const char *t, char **name, char **email, unsigned long *date, int *tz)
73 {
74         struct ident_split ident;
75         unsigned email_len;
76
77         if (!split_ident_line(&ident, t, strchrnul(t, '\n') - t)) {
78                 *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                 xsnprintf(*email, email_len + 3, "<%.*s>", email_len, ident.mail_begin);
83
84                 if (ident.date_begin)
85                         *date = strtoul(ident.date_begin, NULL, 10);
86                 if (ident.tz_begin)
87                         *tz = atoi(ident.tz_begin);
88         }
89 }
90
91 #ifdef NO_ICONV
92 #define reencode(a, b, c)
93 #else
94 static const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
95 {
96         char *tmp;
97
98         if (!txt)
99                 return NULL;
100
101         if (!*txt || !src_enc || !dst_enc)
102                 return *txt;
103
104         /* no encoding needed if src_enc equals dst_enc */
105         if (!strcasecmp(src_enc, dst_enc))
106                 return *txt;
107
108         tmp = reencode_string(*txt, dst_enc, src_enc);
109         if (tmp) {
110                 free(*txt);
111                 *txt = tmp;
112         }
113         return *txt;
114 }
115 #endif
116
117 static 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
125 static int end_of_header(const char *p)
126 {
127         return !p || (*p == '\n');
128 }
129
130 struct commitinfo *cgit_parse_commit(struct commit *commit)
131 {
132         struct commitinfo *ret;
133         const char *p = repo_get_commit_buffer(the_repository, commit, NULL);
134         const char *t;
135
136         ret = xcalloc(1, sizeof(struct commitinfo));
137         ret->commit = commit;
138
139         if (!p)
140                 return ret;
141
142         if (!skip_prefix(p, "tree ", &p))
143                 die("Bad commit: %s", oid_to_hex(&commit->object.oid));
144         p += the_hash_algo->hexsz + 1;
145
146         while (skip_prefix(p, "parent ", &p))
147                 p += the_hash_algo->hexsz + 1;
148
149         if (p && skip_prefix(p, "author ", &p)) {
150                 parse_user(p, &ret->author, &ret->author_email,
151                         &ret->author_date, &ret->author_tz);
152                 p = next_header_line(p);
153         }
154
155         if (p && skip_prefix(p, "committer ", &p)) {
156                 parse_user(p, &ret->committer, &ret->committer_email,
157                         &ret->committer_date, &ret->committer_tz);
158                 p = next_header_line(p);
159         }
160
161         if (p && skip_prefix(p, "encoding ", &p)) {
162                 t = strchr(p, '\n');
163                 if (t) {
164                         ret->msg_encoding = substr(p, t + 1);
165                         p = t + 1;
166                 }
167         }
168
169         if (!ret->msg_encoding)
170                 ret->msg_encoding = xstrdup("UTF-8");
171
172         while (!end_of_header(p))
173                 p = next_header_line(p);
174         while (p && *p == '\n')
175                 p++;
176         if (!p)
177                 return ret;
178
179         t = strchrnul(p, '\n');
180         ret->subject = substr(p, t);
181         while (*t == '\n')
182                 t++;
183         ret->msg = xstrdup(t);
184
185         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);
191
192         return ret;
193 }
194
195 struct taginfo *cgit_parse_tag(struct tag *tag)
196 {
197         void *data;
198         enum object_type type;
199         unsigned long size;
200         const char *p;
201         struct taginfo *ret = NULL;
202
203         data = repo_read_object_file(the_repository, &tag->object.oid, &type, &size);
204         if (!data || type != OBJ_TAG)
205                 goto cleanup;
206
207         ret = xcalloc(1, sizeof(struct taginfo));
208
209         for (p = data; !end_of_header(p); p = next_header_line(p)) {
210                 if (skip_prefix(p, "tagger ", &p)) {
211                         parse_user(p, &ret->tagger, &ret->tagger_email,
212                                 &ret->tagger_date, &ret->tagger_tz);
213                 }
214         }
215
216         while (p && *p == '\n')
217                 p++;
218
219         if (p && *p)
220                 ret->msg = xstrdup(p);
221
222 cleanup:
223         free(data);
224         return ret;
225 }