2006-12-10 21:41:14 +00:00
|
|
|
/* config.c: parsing of config files
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Lars Hjemli
|
|
|
|
*
|
|
|
|
* Licensed under GNU General Public License v2
|
|
|
|
* (see COPYING for full license text)
|
|
|
|
*/
|
|
|
|
|
2006-12-09 14:18:17 +00:00
|
|
|
#include "cgit.h"
|
|
|
|
|
2007-05-18 01:00:54 +00:00
|
|
|
/*
|
|
|
|
* url syntax: [repo ['/' cmd [ '/' path]]]
|
|
|
|
* repo: any valid repo url, may contain '/'
|
|
|
|
* cmd: log | commit | diff | tree | view | blob | snapshot
|
|
|
|
* path: any valid path, may contain '/'
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void cgit_parse_url(const char *url)
|
|
|
|
{
|
|
|
|
char *cmd, *p;
|
|
|
|
|
2008-02-16 12:56:09 +00:00
|
|
|
ctx.repo = NULL;
|
2007-05-18 01:00:54 +00:00
|
|
|
if (!url || url[0] == '\0')
|
|
|
|
return;
|
|
|
|
|
2008-02-16 12:56:09 +00:00
|
|
|
ctx.repo = cgit_get_repoinfo(url);
|
|
|
|
if (ctx.repo) {
|
|
|
|
ctx.qry.repo = ctx.repo->url;
|
2007-05-18 01:00:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = strchr(url, '/');
|
2008-02-16 12:56:09 +00:00
|
|
|
while (!ctx.repo && cmd) {
|
2007-05-18 01:00:54 +00:00
|
|
|
cmd[0] = '\0';
|
2008-02-16 12:56:09 +00:00
|
|
|
ctx.repo = cgit_get_repoinfo(url);
|
|
|
|
if (ctx.repo == NULL) {
|
2007-05-18 01:00:54 +00:00
|
|
|
cmd[0] = '/';
|
|
|
|
cmd = strchr(cmd + 1, '/');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-02-16 12:56:09 +00:00
|
|
|
ctx.qry.repo = ctx.repo->url;
|
2007-05-18 01:00:54 +00:00
|
|
|
p = strchr(cmd + 1, '/');
|
|
|
|
if (p) {
|
|
|
|
p[0] = '\0';
|
2007-05-18 11:06:45 +00:00
|
|
|
if (p[1])
|
2008-02-16 10:53:40 +00:00
|
|
|
ctx.qry.path = trim_end(p + 1, '/');
|
2007-05-18 01:00:54 +00:00
|
|
|
}
|
2008-03-24 00:09:39 +00:00
|
|
|
if (cmd[1])
|
|
|
|
ctx.qry.page = xstrdup(cmd + 1);
|
2007-05-18 01:00:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-15 17:17:36 +00:00
|
|
|
char *substr(const char *head, const char *tail)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
buf = xmalloc(tail - head + 1);
|
|
|
|
strncpy(buf, head, tail - head);
|
|
|
|
buf[tail - head] = '\0';
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2008-09-14 07:45:37 +00:00
|
|
|
char *parse_user(char *t, char **name, char **email, unsigned long *date)
|
|
|
|
{
|
|
|
|
char *p = t;
|
|
|
|
int mode = 1;
|
|
|
|
|
|
|
|
while (p && *p) {
|
|
|
|
if (mode == 1 && *p == '<') {
|
|
|
|
*name = substr(t, p - 1);
|
|
|
|
t = p;
|
|
|
|
mode++;
|
|
|
|
} else if (mode == 1 && *p == '\n') {
|
|
|
|
*name = substr(t, p);
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
} else if (mode == 2 && *p == '>') {
|
|
|
|
*email = substr(t, p + 1);
|
|
|
|
t = p;
|
|
|
|
mode++;
|
|
|
|
} else if (mode == 2 && *p == '\n') {
|
|
|
|
*email = substr(t, p);
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
} else if (mode == 3 && isdigit(*p)) {
|
|
|
|
*date = atol(p);
|
|
|
|
mode++;
|
|
|
|
} else if (*p == '\n') {
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
if (!txt || !*txt || !src_enc || !dst_enc)
|
|
|
|
return *txt;
|
|
|
|
|
|
|
|
tmp = reencode_string(*txt, src_enc, dst_enc);
|
|
|
|
if (tmp) {
|
|
|
|
free(*txt);
|
|
|
|
*txt = tmp;
|
|
|
|
}
|
|
|
|
return *txt;
|
|
|
|
}
|
|
|
|
|
2006-12-15 17:17:36 +00:00
|
|
|
struct commitinfo *cgit_parse_commit(struct commit *commit)
|
|
|
|
{
|
|
|
|
struct commitinfo *ret;
|
|
|
|
char *p = commit->buffer, *t = commit->buffer;
|
|
|
|
|
|
|
|
ret = xmalloc(sizeof(*ret));
|
|
|
|
ret->commit = commit;
|
2007-01-16 17:41:01 +00:00
|
|
|
ret->author = NULL;
|
|
|
|
ret->author_email = NULL;
|
|
|
|
ret->committer = NULL;
|
|
|
|
ret->committer_email = NULL;
|
|
|
|
ret->subject = NULL;
|
|
|
|
ret->msg = NULL;
|
2007-10-26 22:09:06 +00:00
|
|
|
ret->msg_encoding = NULL;
|
2006-12-15 17:17:36 +00:00
|
|
|
|
2007-05-26 01:27:49 +00:00
|
|
|
if (p == NULL)
|
|
|
|
return ret;
|
|
|
|
|
2006-12-15 17:17:36 +00:00
|
|
|
if (strncmp(p, "tree ", 5))
|
|
|
|
die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
|
|
|
|
else
|
|
|
|
p += 46; // "tree " + hex[40] + "\n"
|
|
|
|
|
|
|
|
while (!strncmp(p, "parent ", 7))
|
|
|
|
p += 48; // "parent " + hex[40] + "\n"
|
|
|
|
|
2008-09-14 07:45:37 +00:00
|
|
|
if (p && !strncmp(p, "author ", 7)) {
|
|
|
|
p = parse_user(p + 7, &ret->author, &ret->author_email,
|
|
|
|
&ret->author_date);
|
2006-12-15 17:17:36 +00:00
|
|
|
}
|
|
|
|
|
2008-09-14 07:45:37 +00:00
|
|
|
if (p && !strncmp(p, "committer ", 9)) {
|
|
|
|
p = parse_user(p + 9, &ret->committer, &ret->committer_email,
|
|
|
|
&ret->committer_date);
|
2006-12-15 17:17:36 +00:00
|
|
|
}
|
|
|
|
|
2008-09-14 07:45:37 +00:00
|
|
|
if (p && !strncmp(p, "encoding ", 9)) {
|
2007-10-26 22:10:26 +00:00
|
|
|
p += 9;
|
2008-09-14 07:45:37 +00:00
|
|
|
t = strchr(p, '\n');
|
|
|
|
if (t) {
|
|
|
|
ret->msg_encoding = substr(p, t + 1);
|
|
|
|
p = t + 1;
|
|
|
|
}
|
|
|
|
}
|
2007-10-26 22:10:26 +00:00
|
|
|
|
2008-09-14 07:45:37 +00:00
|
|
|
// skip unknown header fields
|
|
|
|
while (p && *p && (*p != '\n')) {
|
|
|
|
p = strchr(p, '\n');
|
|
|
|
if (p)
|
|
|
|
p++;
|
|
|
|
}
|
2007-10-24 19:14:44 +00:00
|
|
|
|
2008-09-14 07:45:37 +00:00
|
|
|
// skip empty lines between headers and message
|
|
|
|
while (p && *p == '\n')
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return ret;
|
2006-12-15 17:17:36 +00:00
|
|
|
|
|
|
|
t = strchr(p, '\n');
|
2007-05-26 00:19:38 +00:00
|
|
|
if (t) {
|
2008-09-14 07:45:37 +00:00
|
|
|
ret->subject = substr(p, t);
|
2007-01-16 17:41:01 +00:00
|
|
|
p = t + 1;
|
2006-12-15 17:17:36 +00:00
|
|
|
|
2008-09-14 07:45:37 +00:00
|
|
|
while (p && *p == '\n') {
|
|
|
|
p = strchr(p, '\n');
|
|
|
|
if (p)
|
|
|
|
p++;
|
2007-10-26 22:13:41 +00:00
|
|
|
}
|
2008-09-14 07:45:37 +00:00
|
|
|
if (p)
|
|
|
|
ret->msg = xstrdup(p);
|
|
|
|
} else
|
|
|
|
ret->subject = xstrdup(p);
|
2007-10-26 22:13:41 +00:00
|
|
|
|
2008-09-14 07:45:37 +00:00
|
|
|
if (ret->msg_encoding) {
|
|
|
|
reencode(&ret->subject, PAGE_ENCODING, ret->msg_encoding);
|
|
|
|
reencode(&ret->msg, PAGE_ENCODING, ret->msg_encoding);
|
2007-10-26 22:13:41 +00:00
|
|
|
}
|
|
|
|
|
2006-12-15 17:17:36 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2007-01-17 00:09:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct taginfo *cgit_parse_tag(struct tag *tag)
|
|
|
|
{
|
|
|
|
void *data;
|
2007-05-08 20:40:59 +00:00
|
|
|
enum object_type type;
|
2007-01-17 00:09:51 +00:00
|
|
|
unsigned long size;
|
2008-09-14 07:45:37 +00:00
|
|
|
char *p;
|
2007-01-17 00:09:51 +00:00
|
|
|
struct taginfo *ret;
|
|
|
|
|
2007-05-08 20:40:59 +00:00
|
|
|
data = read_sha1_file(tag->object.sha1, &type, &size);
|
|
|
|
if (!data || type != OBJ_TAG) {
|
2007-01-17 00:09:51 +00:00
|
|
|
free(data);
|
|
|
|
return 0;
|
|
|
|
}
|
2007-05-15 21:28:40 +00:00
|
|
|
|
2007-01-17 00:09:51 +00:00
|
|
|
ret = xmalloc(sizeof(*ret));
|
|
|
|
ret->tagger = NULL;
|
|
|
|
ret->tagger_email = NULL;
|
|
|
|
ret->tagger_date = 0;
|
|
|
|
ret->msg = NULL;
|
|
|
|
|
|
|
|
p = data;
|
|
|
|
|
2007-02-03 15:11:41 +00:00
|
|
|
while (p && *p) {
|
2007-01-17 00:09:51 +00:00
|
|
|
if (*p == '\n')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!strncmp(p, "tagger ", 7)) {
|
2008-09-14 07:45:37 +00:00
|
|
|
p = parse_user(p + 7, &ret->tagger, &ret->tagger_email,
|
|
|
|
&ret->tagger_date);
|
|
|
|
} else {
|
|
|
|
p = strchr(p, '\n');
|
|
|
|
if (p)
|
|
|
|
p++;
|
2007-01-17 00:09:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-14 07:45:37 +00:00
|
|
|
// skip empty lines between headers and message
|
|
|
|
while (p && *p == '\n')
|
|
|
|
p++;
|
2007-10-24 19:14:44 +00:00
|
|
|
|
2007-02-03 15:11:41 +00:00
|
|
|
if (p && *p)
|
2007-01-17 00:09:51 +00:00
|
|
|
ret->msg = xstrdup(p);
|
|
|
|
free(data);
|
|
|
|
return ret;
|
|
|
|
}
|