blob: 66e8b3db21fd4369f62f3ba8b0029276ea9e9cd5 [file] [log] [blame]
Lars Hjemli7640d902006-12-10 22:41:14 +01001/* config.c: parsing of config files
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
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{
20 char *cmd, *p;
21
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010022 ctx.repo = NULL;
Lars Hjemli30ccdca2007-05-18 03:00:54 +020023 if (!url || url[0] == '\0')
24 return;
25
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010026 ctx.repo = cgit_get_repoinfo(url);
27 if (ctx.repo) {
28 ctx.qry.repo = ctx.repo->url;
Lars Hjemli30ccdca2007-05-18 03:00:54 +020029 return;
30 }
31
32 cmd = strchr(url, '/');
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010033 while (!ctx.repo && cmd) {
Lars Hjemli30ccdca2007-05-18 03:00:54 +020034 cmd[0] = '\0';
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010035 ctx.repo = cgit_get_repoinfo(url);
36 if (ctx.repo == NULL) {
Lars Hjemli30ccdca2007-05-18 03:00:54 +020037 cmd[0] = '/';
38 cmd = strchr(cmd + 1, '/');
39 continue;
40 }
41
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010042 ctx.qry.repo = ctx.repo->url;
Lars Hjemli30ccdca2007-05-18 03:00:54 +020043 p = strchr(cmd + 1, '/');
44 if (p) {
45 p[0] = '\0';
Lars Hjemli3de63b22007-05-18 13:06:45 +020046 if (p[1])
Lars Hjemlid14d77f2008-02-16 11:53:40 +010047 ctx.qry.path = trim_end(p + 1, '/');
Lars Hjemli30ccdca2007-05-18 03:00:54 +020048 }
Lars Hjemlie0e44782008-03-24 01:09:39 +010049 if (cmd[1])
50 ctx.qry.page = xstrdup(cmd + 1);
Lars Hjemli30ccdca2007-05-18 03:00:54 +020051 return;
52 }
53}
54
Lars Hjemli2101e262006-12-15 18:17:36 +010055char *substr(const char *head, const char *tail)
56{
57 char *buf;
58
59 buf = xmalloc(tail - head + 1);
60 strncpy(buf, head, tail - head);
61 buf[tail - head] = '\0';
62 return buf;
63}
64
65struct commitinfo *cgit_parse_commit(struct commit *commit)
66{
67 struct commitinfo *ret;
68 char *p = commit->buffer, *t = commit->buffer;
69
70 ret = xmalloc(sizeof(*ret));
71 ret->commit = commit;
Lars Hjemli66091f92007-01-16 18:41:01 +010072 ret->author = NULL;
73 ret->author_email = NULL;
74 ret->committer = NULL;
75 ret->committer_email = NULL;
76 ret->subject = NULL;
77 ret->msg = NULL;
Jonathan Bastien-Filiatrault3845e172007-10-26 18:09:06 -040078 ret->msg_encoding = NULL;
Lars Hjemli2101e262006-12-15 18:17:36 +010079
Ondrej Jirman61302312007-05-26 03:27:49 +020080 if (p == NULL)
81 return ret;
82
Lars Hjemli2101e262006-12-15 18:17:36 +010083 if (strncmp(p, "tree ", 5))
84 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
85 else
86 p += 46; // "tree " + hex[40] + "\n"
87
88 while (!strncmp(p, "parent ", 7))
89 p += 48; // "parent " + hex[40] + "\n"
90
91 if (!strncmp(p, "author ", 7)) {
92 p += 7;
93 t = strchr(p, '<') - 1;
94 ret->author = substr(p, t);
Lars Hjemli77078ba2006-12-16 14:25:41 +010095 p = t;
96 t = strchr(t, '>') + 1;
97 ret->author_email = substr(p, t);
Lars Hjemlifc4c4ba2007-12-02 22:11:35 +010098 ret->author_date = atol(t+1);
Lars Hjemli77078ba2006-12-16 14:25:41 +010099 p = strchr(t, '\n') + 1;
Lars Hjemli2101e262006-12-15 18:17:36 +0100100 }
101
102 if (!strncmp(p, "committer ", 9)) {
103 p += 9;
104 t = strchr(p, '<') - 1;
105 ret->committer = substr(p, t);
Lars Hjemli77078ba2006-12-16 14:25:41 +0100106 p = t;
107 t = strchr(t, '>') + 1;
108 ret->committer_email = substr(p, t);
Lars Hjemlifc4c4ba2007-12-02 22:11:35 +0100109 ret->committer_date = atol(t+1);
Lars Hjemli77078ba2006-12-16 14:25:41 +0100110 p = strchr(t, '\n') + 1;
Lars Hjemli2101e262006-12-15 18:17:36 +0100111 }
112
Jonathan Bastien-Filiatraultaa5cc322007-10-26 18:10:26 -0400113 if (!strncmp(p, "encoding ", 9)) {
114 p += 9;
115 t = strchr(p, '\n') + 1;
116 ret->msg_encoding = substr(p, t);
117 p = t;
118 } else
119 ret->msg_encoding = xstrdup(PAGE_ENCODING);
120
Lars Hjemli13d2b0b2007-10-24 21:14:44 +0200121 while (*p && (*p != '\n'))
122 p = strchr(p, '\n') + 1; // skip unknown header fields
123
Lars Hjemli2101e262006-12-15 18:17:36 +0100124 while (*p == '\n')
125 p = strchr(p, '\n') + 1;
126
127 t = strchr(p, '\n');
Ondrej Jirman3ce6fc12007-05-26 02:19:38 +0200128 if (t) {
129 if (*t == '\0')
Lars Hjemlic61360f2007-10-27 13:50:18 +0200130 ret->subject = "** empty **";
Ondrej Jirman3ce6fc12007-05-26 02:19:38 +0200131 else
132 ret->subject = substr(p, t);
Lars Hjemli66091f92007-01-16 18:41:01 +0100133 p = t + 1;
Lars Hjemli2101e262006-12-15 18:17:36 +0100134
Lars Hjemli66091f92007-01-16 18:41:01 +0100135 while (*p == '\n')
136 p = strchr(p, '\n') + 1;
Lars Hjemlic61360f2007-10-27 13:50:18 +0200137 ret->msg = xstrdup(p);
Ondrej Jirman3ce6fc12007-05-26 02:19:38 +0200138 } else
139 ret->subject = substr(p, p+strlen(p));
140
Jonathan Bastien-Filiatrault7858a302007-10-26 18:13:41 -0400141 if(strcmp(ret->msg_encoding, PAGE_ENCODING)) {
Lars Hjemlia2ebbd62007-11-05 22:27:43 +0100142 t = reencode_string(ret->subject, PAGE_ENCODING,
143 ret->msg_encoding);
Jonathan Bastien-Filiatrault7858a302007-10-26 18:13:41 -0400144 if(t) {
145 free(ret->subject);
146 ret->subject = t;
147 }
148
Lars Hjemlia2ebbd62007-11-05 22:27:43 +0100149 t = reencode_string(ret->msg, PAGE_ENCODING,
150 ret->msg_encoding);
Jonathan Bastien-Filiatrault7858a302007-10-26 18:13:41 -0400151 if(t) {
152 free(ret->msg);
153 ret->msg = t;
154 }
155 }
156
Lars Hjemli2101e262006-12-15 18:17:36 +0100157 return ret;
158}
Lars Hjemlia69061f2007-01-17 01:09:51 +0100159
160
161struct taginfo *cgit_parse_tag(struct tag *tag)
162{
163 void *data;
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200164 enum object_type type;
Lars Hjemlia69061f2007-01-17 01:09:51 +0100165 unsigned long size;
166 char *p, *t;
167 struct taginfo *ret;
168
Lars Hjemli61c3ca92007-05-08 22:40:59 +0200169 data = read_sha1_file(tag->object.sha1, &type, &size);
170 if (!data || type != OBJ_TAG) {
Lars Hjemlia69061f2007-01-17 01:09:51 +0100171 free(data);
172 return 0;
173 }
Lars Hjemli47a81c72007-05-15 23:28:40 +0200174
Lars Hjemlia69061f2007-01-17 01:09:51 +0100175 ret = xmalloc(sizeof(*ret));
176 ret->tagger = NULL;
177 ret->tagger_email = NULL;
178 ret->tagger_date = 0;
179 ret->msg = NULL;
180
181 p = data;
182
Lars Hjemliebd7b0f2007-02-03 16:11:41 +0100183 while (p && *p) {
Lars Hjemlia69061f2007-01-17 01:09:51 +0100184 if (*p == '\n')
185 break;
186
187 if (!strncmp(p, "tagger ", 7)) {
188 p += 7;
189 t = strchr(p, '<') - 1;
190 ret->tagger = substr(p, t);
191 p = t;
192 t = strchr(t, '>') + 1;
193 ret->tagger_email = substr(p, t);
Lars Hjemlifc4c4ba2007-12-02 22:11:35 +0100194 ret->tagger_date = atol(t+1);
Lars Hjemlia69061f2007-01-17 01:09:51 +0100195 }
196 p = strchr(p, '\n') + 1;
197 }
198
Lars Hjemli13d2b0b2007-10-24 21:14:44 +0200199 while (p && *p && (*p != '\n'))
200 p = strchr(p, '\n') + 1; // skip unknown tag fields
201
Lars Hjemlia69061f2007-01-17 01:09:51 +0100202 while (p && (*p == '\n'))
203 p = strchr(p, '\n') + 1;
Lars Hjemliebd7b0f2007-02-03 16:11:41 +0100204 if (p && *p)
Lars Hjemlia69061f2007-01-17 01:09:51 +0100205 ret->msg = xstrdup(p);
206 free(data);
207 return ret;
208}