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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
if (!strncmp(p, "author ", 7)) {
|
|
|
|
p += 7;
|
|
|
|
t = strchr(p, '<') - 1;
|
|
|
|
ret->author = substr(p, t);
|
2006-12-16 13:25:41 +00:00
|
|
|
p = t;
|
|
|
|
t = strchr(t, '>') + 1;
|
|
|
|
ret->author_email = substr(p, t);
|
2007-12-02 21:11:35 +00:00
|
|
|
ret->author_date = atol(t+1);
|
2006-12-16 13:25:41 +00:00
|
|
|
p = strchr(t, '\n') + 1;
|
2006-12-15 17:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!strncmp(p, "committer ", 9)) {
|
|
|
|
p += 9;
|
|
|
|
t = strchr(p, '<') - 1;
|
|
|
|
ret->committer = substr(p, t);
|
2006-12-16 13:25:41 +00:00
|
|
|
p = t;
|
|
|
|
t = strchr(t, '>') + 1;
|
|
|
|
ret->committer_email = substr(p, t);
|
2007-12-02 21:11:35 +00:00
|
|
|
ret->committer_date = atol(t+1);
|
2006-12-16 13:25:41 +00:00
|
|
|
p = strchr(t, '\n') + 1;
|
2006-12-15 17:17:36 +00:00
|
|
|
}
|
|
|
|
|
2007-10-26 22:10:26 +00: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 19:14:44 +00:00
|
|
|
while (*p && (*p != '\n'))
|
|
|
|
p = strchr(p, '\n') + 1; // skip unknown header fields
|
|
|
|
|
2006-12-15 17:17:36 +00:00
|
|
|
while (*p == '\n')
|
|
|
|
p = strchr(p, '\n') + 1;
|
|
|
|
|
|
|
|
t = strchr(p, '\n');
|
2007-05-26 00:19:38 +00:00
|
|
|
if (t) {
|
|
|
|
if (*t == '\0')
|
2007-10-27 11:50:18 +00:00
|
|
|
ret->subject = "** empty **";
|
2007-05-26 00:19:38 +00:00
|
|
|
else
|
|
|
|
ret->subject = substr(p, t);
|
2007-01-16 17:41:01 +00:00
|
|
|
p = t + 1;
|
2006-12-15 17:17:36 +00:00
|
|
|
|
2007-01-16 17:41:01 +00:00
|
|
|
while (*p == '\n')
|
|
|
|
p = strchr(p, '\n') + 1;
|
2007-10-27 11:50:18 +00:00
|
|
|
ret->msg = xstrdup(p);
|
2007-05-26 00:19:38 +00:00
|
|
|
} else
|
|
|
|
ret->subject = substr(p, p+strlen(p));
|
|
|
|
|
2007-10-26 22:13:41 +00:00
|
|
|
if(strcmp(ret->msg_encoding, PAGE_ENCODING)) {
|
2007-11-05 21:27:43 +00:00
|
|
|
t = reencode_string(ret->subject, PAGE_ENCODING,
|
|
|
|
ret->msg_encoding);
|
2007-10-26 22:13:41 +00:00
|
|
|
if(t) {
|
|
|
|
free(ret->subject);
|
|
|
|
ret->subject = t;
|
|
|
|
}
|
|
|
|
|
2007-11-05 21:27:43 +00:00
|
|
|
t = reencode_string(ret->msg, PAGE_ENCODING,
|
|
|
|
ret->msg_encoding);
|
2007-10-26 22:13:41 +00:00
|
|
|
if(t) {
|
|
|
|
free(ret->msg);
|
|
|
|
ret->msg = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
char *p, *t;
|
|
|
|
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)) {
|
|
|
|
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 21:11:35 +00:00
|
|
|
ret->tagger_date = atol(t+1);
|
2007-01-17 00:09:51 +00:00
|
|
|
}
|
|
|
|
p = strchr(p, '\n') + 1;
|
|
|
|
}
|
|
|
|
|
2007-10-24 19:14:44 +00:00
|
|
|
while (p && *p && (*p != '\n'))
|
|
|
|
p = strchr(p, '\n') + 1; // skip unknown tag fields
|
|
|
|
|
2007-01-17 00:09:51 +00:00
|
|
|
while (p && (*p == '\n'))
|
|
|
|
p = strchr(p, '\n') + 1;
|
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;
|
|
|
|
}
|