]> git.cameronkatri.com Git - cgit.git/blobdiff - parsing.c
parsing.c: Remove leading space from committer
[cgit.git] / parsing.c
index f3f3b15f736521f267e87dd474fee0af5c961250..599f61ead66d5c9c4f3c99cf4493d5af691367c2 100644 (file)
--- a/parsing.c
+++ b/parsing.c
@@ -1,6 +1,6 @@
-/* config.c: parsing of config files
+/* parsing.c: parsing of config files
  *
- * Copyright (C) 2006 Lars Hjemli
+ * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
  *
  * Licensed under GNU General Public License v2
  *   (see COPYING for full license text)
@@ -52,17 +52,19 @@ void cgit_parse_url(const char *url)
        }
 }
 
-char *substr(const char *head, const char *tail)
+static char *substr(const char *head, const char *tail)
 {
        char *buf;
 
+       if (tail < head)
+               return xstrdup("");
        buf = xmalloc(tail - head + 1);
        strncpy(buf, head, tail - head);
        buf[tail - head] = '\0';
        return buf;
 }
 
-char *parse_user(char *t, char **name, char **email, unsigned long *date)
+static char *parse_user(char *t, char **name, char **email, unsigned long *date)
 {
        char *p = t;
        int mode = 1;
@@ -99,14 +101,21 @@ char *parse_user(char *t, char **name, char **email, unsigned long *date)
 #ifdef NO_ICONV
 #define reencode(a, b, c)
 #else
-const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
+static const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
 {
        char *tmp;
 
-       if (!txt || !*txt || !src_enc || !dst_enc)
+       if (!txt)
+               return NULL;
+
+       if (!*txt || !src_enc || !dst_enc)
+               return *txt;
+
+       /* no encoding needed if src_enc equals dst_enc */
+       if (!strcasecmp(src_enc, dst_enc))
                return *txt;
 
-       tmp = reencode_string(*txt, src_enc, dst_enc);
+       tmp = reencode_string(*txt, dst_enc, src_enc);
        if (tmp) {
                free(*txt);
                *txt = tmp;
@@ -118,7 +127,7 @@ const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
 struct commitinfo *cgit_parse_commit(struct commit *commit)
 {
        struct commitinfo *ret;
-       char *p = commit->buffer, *t = commit->buffer;
+       char *p = commit->buffer, *t;
 
        ret = xmalloc(sizeof(*ret));
        ret->commit = commit;
@@ -133,25 +142,25 @@ struct commitinfo *cgit_parse_commit(struct commit *commit)
        if (p == NULL)
                return ret;
 
-       if (strncmp(p, "tree ", 5))
+       if (prefixcmp(p, "tree "))
                die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
        else
                p += 46; // "tree " + hex[40] + "\n"
 
-       while (!strncmp(p, "parent ", 7))
+       while (!prefixcmp(p, "parent "))
                p += 48; // "parent " + hex[40] + "\n"
 
-       if (p && !strncmp(p, "author ", 7)) {
+       if (p && !prefixcmp(p, "author ")) {
                p = parse_user(p + 7, &ret->author, &ret->author_email,
                        &ret->author_date);
        }
 
-       if (p && !strncmp(p, "committer ", 9)) {
-               p = parse_user(p + 9, &ret->committer, &ret->committer_email,
+       if (p && !prefixcmp(p, "committer ")) {
+               p = parse_user(p + 10, &ret->committer, &ret->committer_email,
                        &ret->committer_date);
        }
 
-       if (p && !strncmp(p, "encoding ", 9)) {
+       if (p && !prefixcmp(p, "encoding ")) {
                p += 9;
                t = strchr(p, '\n');
                if (t) {
@@ -160,6 +169,10 @@ struct commitinfo *cgit_parse_commit(struct commit *commit)
                }
        }
 
+       /* if no special encoding is found, assume UTF-8 */
+       if (!ret->msg_encoding)
+               ret->msg_encoding = xstrdup("UTF-8");
+
        // skip unknown header fields
        while (p && *p && (*p != '\n')) {
                p = strchr(p, '\n');
@@ -189,10 +202,12 @@ struct commitinfo *cgit_parse_commit(struct commit *commit)
        } else
                ret->subject = xstrdup(p);
 
-       if (ret->msg_encoding) {
-               reencode(&ret->subject, PAGE_ENCODING, ret->msg_encoding);
-               reencode(&ret->msg, PAGE_ENCODING, ret->msg_encoding);
-       }
+       reencode(&ret->author, ret->msg_encoding, PAGE_ENCODING);
+       reencode(&ret->author_email, ret->msg_encoding, PAGE_ENCODING);
+       reencode(&ret->committer, ret->msg_encoding, PAGE_ENCODING);
+       reencode(&ret->committer_email, ret->msg_encoding, PAGE_ENCODING);
+       reencode(&ret->subject, ret->msg_encoding, PAGE_ENCODING);
+       reencode(&ret->msg, ret->msg_encoding, PAGE_ENCODING);
 
        return ret;
 }
@@ -224,7 +239,7 @@ struct taginfo *cgit_parse_tag(struct tag *tag)
                if (*p == '\n')
                        break;
 
-               if (!strncmp(p, "tagger ", 7)) {
+               if (!prefixcmp(p, "tagger ")) {
                        p = parse_user(p + 7, &ret->tagger, &ret->tagger_email,
                                &ret->tagger_date);
                } else {