]> gitweb.ps.run Git - ps-cgit/blob - parsing.c
contrib/hooks: add sample post-receive hook using agefile
[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         ctx.repo = NULL;
24         if (!url || url[0] == '\0')
25                 return;
26
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                 return;
57         }
58 }
59
60 static char *substr(const char *head, const char *tail)
61 {
62         char *buf;
63
64         if (tail < head)
65                 return xstrdup("");
66         buf = xmalloc(tail - head + 1);
67         strncpy(buf, head, tail - head);
68         buf[tail - head] = '\0';
69         return buf;
70 }
71
72 static void parse_user(const char *t, char **name, char **email, unsigned long *date)
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                 sprintf(*email, "<%.*s>", email_len, ident.mail_begin);
83
84                 if (ident.date_begin)
85                         *date = strtoul(ident.date_begin, NULL, 10);
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         const int sha1hex_len = 40;
131         struct commitinfo *ret;
132         const char *p = get_cached_commit_buffer(commit, NULL);
133         const char *t;
134
135         ret = xcalloc(1, sizeof(struct commitinfo));
136         ret->commit = commit;
137
138         if (!p)
139                 return ret;
140
141         if (!skip_prefix(p, "tree ", &p))
142                 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
143         p += sha1hex_len + 1;
144
145         while (skip_prefix(p, "parent ", &p))
146                 p += sha1hex_len + 1;
147
148         if (p && skip_prefix(p, "author ", &p)) {
149                 parse_user(p, &ret->author, &ret->author_email,
150                         &ret->author_date);
151                 p = next_header_line(p);
152         }
153
154         if (p && skip_prefix(p, "committer ", &p)) {
155                 parse_user(p, &ret->committer, &ret->committer_email,
156                         &ret->committer_date);
157                 p = next_header_line(p);
158         }
159
160         if (p && skip_prefix(p, "encoding ", &p)) {
161                 t = strchr(p, '\n');
162                 if (t) {
163                         ret->msg_encoding = substr(p, t + 1);
164                         p = t + 1;
165                 }
166         }
167
168         if (!ret->msg_encoding)
169                 ret->msg_encoding = xstrdup("UTF-8");
170
171         while (!end_of_header(p))
172                 p = next_header_line(p);
173         while (p && *p == '\n')
174                 p++;
175         if (!p)
176                 return ret;
177
178         t = strchrnul(p, '\n');
179         ret->subject = substr(p, t);
180         while (*t == '\n')
181                 t++;
182         ret->msg = xstrdup(t);
183
184         reencode(&ret->author, ret->msg_encoding, PAGE_ENCODING);
185         reencode(&ret->author_email, ret->msg_encoding, PAGE_ENCODING);
186         reencode(&ret->committer, ret->msg_encoding, PAGE_ENCODING);
187         reencode(&ret->committer_email, ret->msg_encoding, PAGE_ENCODING);
188         reencode(&ret->subject, ret->msg_encoding, PAGE_ENCODING);
189         reencode(&ret->msg, ret->msg_encoding, PAGE_ENCODING);
190
191         return ret;
192 }
193
194 struct taginfo *cgit_parse_tag(struct tag *tag)
195 {
196         void *data;
197         enum object_type type;
198         unsigned long size;
199         const char *p;
200         struct taginfo *ret = NULL;
201
202         data = read_sha1_file(tag->object.sha1, &type, &size);
203         if (!data || type != OBJ_TAG)
204                 goto cleanup;
205
206         ret = xcalloc(1, sizeof(struct taginfo));
207
208         for (p = data; !end_of_header(p); p = next_header_line(p)) {
209                 if (skip_prefix(p, "tagger ", &p)) {
210                         parse_user(p, &ret->tagger, &ret->tagger_email,
211                                 &ret->tagger_date);
212                 }
213         }
214
215         while (p && *p == '\n')
216                 p++;
217
218         if (p && *p)
219                 ret->msg = xstrdup(p);
220
221 cleanup:
222         free(data);
223         return ret;
224 }