]> gitweb.ps.run Git - ps-cgit/blob - parsing.c
git: update to v2.42.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 #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  */
18 void cgit_parse_url(const char *url)
19 {
20         char *c, *cmd, *p;
21         struct cgit_repo *repo;
22
23         if (!url || url[0] == '\0')
24                 return;
25
26         ctx.qry.page = NULL;
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         }
57 }
58
59 static char *substr(const char *head, const char *tail)
60 {
61         char *buf;
62
63         if (tail < head)
64                 return xstrdup("");
65         buf = xmalloc(tail - head + 1);
66         strlcpy(buf, head, tail - head + 1);
67         return buf;
68 }
69
70 static void parse_user(const char *t, char **name, char **email, unsigned long *date, int *tz)
71 {
72         struct ident_split ident;
73         unsigned email_len;
74
75         if (!split_ident_line(&ident, t, strchrnul(t, '\n') - t)) {
76                 *name = substr(ident.name_begin, ident.name_end);
77
78                 email_len = ident.mail_end - ident.mail_begin;
79                 *email = xmalloc(strlen("<") + email_len + strlen(">") + 1);
80                 xsnprintf(*email, email_len + 3, "<%.*s>", email_len, ident.mail_begin);
81
82                 if (ident.date_begin)
83                         *date = strtoul(ident.date_begin, NULL, 10);
84                 if (ident.tz_begin)
85                         *tz = atoi(ident.tz_begin);
86         }
87 }
88
89 #ifdef NO_ICONV
90 #define reencode(a, b, c)
91 #else
92 static const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
93 {
94         char *tmp;
95
96         if (!txt)
97                 return NULL;
98
99         if (!*txt || !src_enc || !dst_enc)
100                 return *txt;
101
102         /* no encoding needed if src_enc equals dst_enc */
103         if (!strcasecmp(src_enc, dst_enc))
104                 return *txt;
105
106         tmp = reencode_string(*txt, dst_enc, src_enc);
107         if (tmp) {
108                 free(*txt);
109                 *txt = tmp;
110         }
111         return *txt;
112 }
113 #endif
114
115 static const char *next_header_line(const char *p)
116 {
117         p = strchr(p, '\n');
118         if (!p)
119                 return NULL;
120         return p + 1;
121 }
122
123 static int end_of_header(const char *p)
124 {
125         return !p || (*p == '\n');
126 }
127
128 struct commitinfo *cgit_parse_commit(struct commit *commit)
129 {
130         struct commitinfo *ret;
131         const char *p = repo_get_commit_buffer(the_repository, commit, NULL);
132         const char *t;
133
134         ret = xcalloc(1, sizeof(struct commitinfo));
135         ret->commit = commit;
136
137         if (!p)
138                 return ret;
139
140         if (!skip_prefix(p, "tree ", &p))
141                 die("Bad commit: %s", oid_to_hex(&commit->object.oid));
142         p += the_hash_algo->hexsz + 1;
143
144         while (skip_prefix(p, "parent ", &p))
145                 p += the_hash_algo->hexsz + 1;
146
147         if (p && skip_prefix(p, "author ", &p)) {
148                 parse_user(p, &ret->author, &ret->author_email,
149                         &ret->author_date, &ret->author_tz);
150                 p = next_header_line(p);
151         }
152
153         if (p && skip_prefix(p, "committer ", &p)) {
154                 parse_user(p, &ret->committer, &ret->committer_email,
155                         &ret->committer_date, &ret->committer_tz);
156                 p = next_header_line(p);
157         }
158
159         if (p && skip_prefix(p, "encoding ", &p)) {
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 (!ret->msg_encoding)
168                 ret->msg_encoding = xstrdup("UTF-8");
169
170         while (!end_of_header(p))
171                 p = next_header_line(p);
172         while (p && *p == '\n')
173                 p++;
174         if (!p)
175                 return ret;
176
177         t = strchrnul(p, '\n');
178         ret->subject = substr(p, t);
179         while (*t == '\n')
180                 t++;
181         ret->msg = xstrdup(t);
182
183         reencode(&ret->author, ret->msg_encoding, PAGE_ENCODING);
184         reencode(&ret->author_email, ret->msg_encoding, PAGE_ENCODING);
185         reencode(&ret->committer, ret->msg_encoding, PAGE_ENCODING);
186         reencode(&ret->committer_email, ret->msg_encoding, PAGE_ENCODING);
187         reencode(&ret->subject, ret->msg_encoding, PAGE_ENCODING);
188         reencode(&ret->msg, ret->msg_encoding, PAGE_ENCODING);
189
190         return ret;
191 }
192
193 struct taginfo *cgit_parse_tag(struct tag *tag)
194 {
195         void *data;
196         enum object_type type;
197         unsigned long size;
198         const char *p;
199         struct taginfo *ret = NULL;
200
201         data = repo_read_object_file(the_repository, &tag->object.oid, &type, &size);
202         if (!data || type != OBJ_TAG)
203                 goto cleanup;
204
205         ret = xcalloc(1, sizeof(struct taginfo));
206
207         for (p = data; !end_of_header(p); p = next_header_line(p)) {
208                 if (skip_prefix(p, "tagger ", &p)) {
209                         parse_user(p, &ret->tagger, &ret->tagger_email,
210                                 &ret->tagger_date, &ret->tagger_tz);
211                 }
212         }
213
214         while (p && *p == '\n')
215                 p++;
216
217         if (p && *p)
218                 ret->msg = xstrdup(p);
219
220 cleanup:
221         free(data);
222         return ret;
223 }