2006-12-10 22:41:14 +01: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 15:18:17 +01:00
|
|
|
#include "cgit.h"
|
|
|
|
|
2007-05-18 03:00:54 +02: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 13:56:09 +01:00
|
|
|
ctx.repo = NULL;
|
2007-05-18 03:00:54 +02:00
|
|
|
if (!url || url[0] == '\0')
|
|
|
|
return;
|
|
|
|
|
2008-02-16 13:56:09 +01:00
|
|
|
ctx.repo = cgit_get_repoinfo(url);
|
|
|
|
if (ctx.repo) {
|
|
|
|
ctx.qry.repo = ctx.repo->url;
|
2007-05-18 03:00:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = strchr(url, '/');
|
2008-02-16 13:56:09 +01:00
|
|
|
while (!ctx.repo && cmd) {
|
2007-05-18 03:00:54 +02:00
|
|
|
cmd[0] = '\0';
|
2008-02-16 13:56:09 +01:00
|
|
|
ctx.repo = cgit_get_repoinfo(url);
|
|
|
|
if (ctx.repo == NULL) {
|
2007-05-18 03:00:54 +02:00
|
|
|
cmd[0] = '/';
|
|
|
|
cmd = strchr(cmd + 1, '/');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-02-16 13:56:09 +01:00
|
|
|
ctx.qry.repo = ctx.repo->url;
|
2007-05-18 03:00:54 +02:00
|
|
|
p = strchr(cmd + 1, '/');
|
|
|
|
if (p) {
|
|
|
|
p[0] = '\0';
|
2007-05-18 13:06:45 +02:00
|
|
|
if (p[1])
|
2008-02-16 11:53:40 +01:00
|
|
|
ctx.qry.path = trim_end(p + 1, '/');
|
2007-05-18 03:00:54 +02:00
|
|
|
}
|
2008-03-24 01:09:39 +01:00
|
|
|
if (cmd[1])
|
|
|
|
ctx.qry.page = xstrdup(cmd + 1);
|
2007-05-18 03:00:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-15 18:17:36 +01: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 18:41:01 +01:00
|
|
|
ret->author = NULL;
|
|
|
|
ret->author_email = NULL;
|
|
|
|
ret->committer = NULL;
|
|
|
|
ret->committer_email = NULL;
|
|
|
|
ret->subject = NULL;
|
|
|
|
ret->msg = NULL;
|
2007-10-27 00:09:06 +02:00
|
|
|
ret->msg_encoding = NULL;
|
2006-12-15 18:17:36 +01:00
|
|
|
|
2007-05-26 03:27:49 +02:00
|
|
|
if (p == NULL)
|
|
|
|
return ret;
|
|
|
|
|
2006-12-15 18:17:36 +01: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"
|
|
|
|
|
|
|
|
if (!strncmp(p, "author ", 7)) {
|
|
|
|
p += 7;
|
|
|
|
t = strchr(p, '<') - 1;
|
|
|
|
ret->author = substr(p, t);
|
2006-12-16 14:25:41 +01:00
|
|
|
p = t;
|
|
|
|
t = strchr(t, '>') + 1;
|
|
|
|
ret->author_email = substr(p, t);
|
2007-12-02 22:11:35 +01:00
|
|
|
ret->author_date = atol(t+1);
|
2006-12-16 14:25:41 +01:00
|
|
|
p = strchr(t, '\n') + 1;
|
2006-12-15 18:17:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!strncmp(p, "committer ", 9)) {
|
|
|
|
p += 9;
|
|
|
|
t = strchr(p, '<') - 1;
|
|
|
|
ret->committer = substr(p, t);
|
2006-12-16 14:25:41 +01:00
|
|
|
p = t;
|
|
|
|
t = strchr(t, '>') + 1;
|
|
|
|
ret->committer_email = substr(p, t);
|
2007-12-02 22:11:35 +01:00
|
|
|
ret->committer_date = atol(t+1);
|
2006-12-16 14:25:41 +01:00
|
|
|
p = strchr(t, '\n') + 1;
|
2006-12-15 18:17:36 +01:00
|
|
|
}
|
|
|
|
|
2007-10-27 00:10:26 +02:00
|
|
|
if (!strncmp(p, "encoding ", 9)) {
|
|
|
|
p += 9;
|
|
|
|
t = strchr(p, '\n') + 1;
|
|
|
|
ret->msg_encoding = substr(p, t);
|
|
|
|
p = t;
|
|
|
|
} else
|
|
|
|
ret->msg_encoding = xstrdup(PAGE_ENCODING);
|
|
|
|
|
2007-10-24 21:14:44 +02:00
|
|
|
while (*p && (*p != '\n'))
|
|
|
|
p = strchr(p, '\n') + 1; // skip unknown header fields
|
|
|
|
|
2006-12-15 18:17:36 +01:00
|
|
|
while (*p == '\n')
|
|
|
|
p = strchr(p, '\n') + 1;
|
|
|
|
|
|
|
|
t = strchr(p, '\n');
|
2007-05-26 02:19:38 +02:00
|
|
|
if (t) {
|
|
|
|
if (*t == '\0')
|
2007-10-27 13:50:18 +02:00
|
|
|
ret->subject = "** empty **";
|
2007-05-26 02:19:38 +02:00
|
|
|
else
|
|
|
|
ret->subject = substr(p, t);
|
2007-01-16 18:41:01 +01:00
|
|
|
p = t + 1;
|
2006-12-15 18:17:36 +01:00
|
|
|
|
2007-01-16 18:41:01 +01:00
|
|
|
while (*p == '\n')
|
|
|
|
p = strchr(p, '\n') + 1;
|
2007-10-27 13:50:18 +02:00
|
|
|
ret->msg = xstrdup(p);
|
2007-05-26 02:19:38 +02:00
|
|
|
} else
|
|
|
|
ret->subject = substr(p, p+strlen(p));
|
|
|
|
|
2007-10-27 00:13:41 +02:00
|
|
|
if(strcmp(ret->msg_encoding, PAGE_ENCODING)) {
|
2007-11-05 22:27:43 +01:00
|
|
|
t = reencode_string(ret->subject, PAGE_ENCODING,
|
|
|
|
ret->msg_encoding);
|
2007-10-27 00:13:41 +02:00
|
|
|
if(t) {
|
|
|
|
free(ret->subject);
|
|
|
|
ret->subject = t;
|
|
|
|
}
|
|
|
|
|
2007-11-05 22:27:43 +01:00
|
|
|
t = reencode_string(ret->msg, PAGE_ENCODING,
|
|
|
|
ret->msg_encoding);
|
2007-10-27 00:13:41 +02:00
|
|
|
if(t) {
|
|
|
|
free(ret->msg);
|
|
|
|
ret->msg = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-15 18:17:36 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2007-01-17 01:09:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
struct taginfo *cgit_parse_tag(struct tag *tag)
|
|
|
|
{
|
|
|
|
void *data;
|
2007-05-08 22:40:59 +02:00
|
|
|
enum object_type type;
|
2007-01-17 01:09:51 +01:00
|
|
|
unsigned long size;
|
|
|
|
char *p, *t;
|
|
|
|
struct taginfo *ret;
|
|
|
|
|
2007-05-08 22:40:59 +02:00
|
|
|
data = read_sha1_file(tag->object.sha1, &type, &size);
|
|
|
|
if (!data || type != OBJ_TAG) {
|
2007-01-17 01:09:51 +01:00
|
|
|
free(data);
|
|
|
|
return 0;
|
|
|
|
}
|
2007-05-15 23:28:40 +02:00
|
|
|
|
2007-01-17 01:09:51 +01:00
|
|
|
ret = xmalloc(sizeof(*ret));
|
|
|
|
ret->tagger = NULL;
|
|
|
|
ret->tagger_email = NULL;
|
|
|
|
ret->tagger_date = 0;
|
|
|
|
ret->msg = NULL;
|
|
|
|
|
|
|
|
p = data;
|
|
|
|
|
2007-02-03 16:11:41 +01:00
|
|
|
while (p && *p) {
|
2007-01-17 01:09:51 +01:00
|
|
|
if (*p == '\n')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!strncmp(p, "tagger ", 7)) {
|
|
|
|
p += 7;
|
|
|
|
t = strchr(p, '<') - 1;
|
|
|
|
ret->tagger = substr(p, t);
|
|
|
|
p = t;
|
|
|
|
t = strchr(t, '>') + 1;
|
|
|
|
ret->tagger_email = substr(p, t);
|
2007-12-02 22:11:35 +01:00
|
|
|
ret->tagger_date = atol(t+1);
|
2007-01-17 01:09:51 +01:00
|
|
|
}
|
|
|
|
p = strchr(p, '\n') + 1;
|
|
|
|
}
|
|
|
|
|
2007-10-24 21:14:44 +02:00
|
|
|
while (p && *p && (*p != '\n'))
|
|
|
|
p = strchr(p, '\n') + 1; // skip unknown tag fields
|
|
|
|
|
2007-01-17 01:09:51 +01:00
|
|
|
while (p && (*p == '\n'))
|
|
|
|
p = strchr(p, '\n') + 1;
|
2007-02-03 16:11:41 +01:00
|
|
|
if (p && *p)
|
2007-01-17 01:09:51 +01:00
|
|
|
ret->msg = xstrdup(p);
|
|
|
|
free(data);
|
|
|
|
return ret;
|
|
|
|
}
|