author | Lars Hjemli <hjemli@gmail.com> | 2007-11-05 23:38:18 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2007-11-05 23:38:18 (UTC) |
commit | 55ac326ecb01161bf62865ae3350acf85db97d63 (patch) (side-by-side diff) | |
tree | e37bcd8fef5523aa627ec9fdcd3401105f8e579d | |
parent | d04c4734bcf40b1d17c55b18fba2aa8344678e8f (diff) | |
parent | a2ebbd6948da96172108db5e9c02c141923ad05c (diff) | |
download | cgit-55ac326ecb01161bf62865ae3350acf85db97d63.zip cgit-55ac326ecb01161bf62865ae3350acf85db97d63.tar.gz cgit-55ac326ecb01161bf62865ae3350acf85db97d63.tar.bz2 |
Merge branch 'iconv-rebased' of http://x2a.org/pub/git/cgit
* 'iconv-rebased' of http://x2a.org/pub/git/cgit:
Use utf8::reencode_string from git
Convert subject and message with iconv_msg.
Add iconv_msg function.
Set msg_encoding according to the header.
Add commit->msg_encoding, allocate msg dynamicly.
-rw-r--r-- | cgit.h | 7 | ||||
-rw-r--r-- | parsing.c | 25 | ||||
-rw-r--r-- | shared.c | 2 | ||||
-rw-r--r-- | ui-shared.c | 2 |
4 files changed, 35 insertions, 1 deletions
@@ -1,140 +1,147 @@ #ifndef CGIT_H #define CGIT_H #include <git-compat-util.h> #include <cache.h> #include <grep.h> #include <object.h> #include <tree.h> #include <commit.h> #include <tag.h> #include <diff.h> #include <diffcore.h> #include <refs.h> #include <revision.h> #include <log-tree.h> #include <archive.h> #include <xdiff/xdiff.h> +#include <utf8.h> /* * The valid cgit repo-commands */ #define CMD_LOG 1 #define CMD_COMMIT 2 #define CMD_DIFF 3 #define CMD_TREE 4 #define CMD_BLOB 5 #define CMD_SNAPSHOT 6 #define CMD_TAG 7 #define CMD_REFS 8 /* * Dateformats used on misc. pages */ #define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" #define FMT_SHORTDATE "%Y-%m-%d" /* * Limits used for relative dates */ #define TM_MIN 60 #define TM_HOUR (TM_MIN * 60) #define TM_DAY (TM_HOUR * 24) #define TM_WEEK (TM_DAY * 7) #define TM_YEAR (TM_DAY * 365) #define TM_MONTH (TM_YEAR / 12.0) +/* + * Default encoding + */ +#define PAGE_ENCODING "UTF-8" + typedef void (*configfn)(const char *name, const char *value); typedef void (*filepair_fn)(struct diff_filepair *pair); typedef void (*linediff_fn)(char *line, int len); struct cacheitem { char *name; struct stat st; int ttl; int fd; }; struct repoinfo { char *url; char *name; char *path; char *desc; char *owner; char *defbranch; char *group; char *module_link; char *readme; int snapshots; int enable_log_filecount; int enable_log_linecount; }; struct repolist { int length; int count; struct repoinfo *repos; }; struct commitinfo { struct commit *commit; char *author; char *author_email; unsigned long author_date; char *committer; char *committer_email; unsigned long committer_date; char *subject; char *msg; + char *msg_encoding; }; struct taginfo { char *tagger; char *tagger_email; int tagger_date; char *msg; }; struct refinfo { const char *refname; struct object *object; union { struct taginfo *tag; struct commitinfo *commit; }; }; struct reflist { struct refinfo **refs; int alloc; int count; }; extern const char *cgit_version; extern struct repolist cgit_repolist; extern struct repoinfo *cgit_repo; extern int cgit_cmd; extern char *cgit_root_title; extern char *cgit_css; extern char *cgit_logo; extern char *cgit_index_header; extern char *cgit_index_info; extern char *cgit_logo_link; extern char *cgit_module_link; extern char *cgit_agefile; extern char *cgit_virtual_root; extern char *cgit_script_name; extern char *cgit_cache_root; extern char *cgit_repo_group; extern int cgit_nocache; extern int cgit_snapshots; extern int cgit_enable_index_links; extern int cgit_enable_log_filecount; extern int cgit_enable_log_linecount; @@ -154,150 +154,175 @@ void cgit_parse_url(const char *url) } cmd = strchr(url, '/'); while (!cgit_repo && cmd) { cmd[0] = '\0'; cgit_repo = cgit_get_repoinfo(url); if (cgit_repo == NULL) { cmd[0] = '/'; cmd = strchr(cmd + 1, '/'); continue; } cgit_query_repo = cgit_repo->url; p = strchr(cmd + 1, '/'); if (p) { p[0] = '\0'; if (p[1]) cgit_query_path = trim_end(p + 1, '/'); } cgit_cmd = cgit_get_cmd_index(cmd + 1); cgit_query_page = xstrdup(cmd + 1); return; } } 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; ret->author = NULL; ret->author_email = NULL; ret->committer = NULL; ret->committer_email = NULL; ret->subject = NULL; ret->msg = NULL; + ret->msg_encoding = NULL; if (p == NULL) return ret; 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); p = t; t = strchr(t, '>') + 1; ret->author_email = substr(p, t); ret->author_date = atol(++t); p = strchr(t, '\n') + 1; } if (!strncmp(p, "committer ", 9)) { p += 9; t = strchr(p, '<') - 1; ret->committer = substr(p, t); p = t; t = strchr(t, '>') + 1; ret->committer_email = substr(p, t); ret->committer_date = atol(++t); p = strchr(t, '\n') + 1; } + 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); + while (*p && (*p != '\n')) p = strchr(p, '\n') + 1; // skip unknown header fields while (*p == '\n') p = strchr(p, '\n') + 1; t = strchr(p, '\n'); if (t) { if (*t == '\0') ret->subject = "** empty **"; else ret->subject = substr(p, t); p = t + 1; while (*p == '\n') p = strchr(p, '\n') + 1; ret->msg = xstrdup(p); } else ret->subject = substr(p, p+strlen(p)); + if(strcmp(ret->msg_encoding, PAGE_ENCODING)) { + t = reencode_string(ret->subject, PAGE_ENCODING, + ret->msg_encoding); + if(t) { + free(ret->subject); + ret->subject = t; + } + + t = reencode_string(ret->msg, PAGE_ENCODING, + ret->msg_encoding); + if(t) { + free(ret->msg); + ret->msg = t; + } + } + return ret; } struct taginfo *cgit_parse_tag(struct tag *tag) { void *data; enum object_type type; unsigned long size; char *p, *t; struct taginfo *ret; data = read_sha1_file(tag->object.sha1, &type, &size); if (!data || type != OBJ_TAG) { free(data); return 0; } ret = xmalloc(sizeof(*ret)); ret->tagger = NULL; ret->tagger_email = NULL; ret->tagger_date = 0; ret->msg = NULL; p = data; while (p && *p) { 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); ret->tagger_date = atol(++t); } p = strchr(p, '\n') + 1; } while (p && *p && (*p != '\n')) p = strchr(p, '\n') + 1; // skip unknown tag fields while (p && (*p == '\n')) p = strchr(p, '\n') + 1; if (p && *p) @@ -220,96 +220,98 @@ void cgit_global_config_cb(const char *name, const char *value) else if (cgit_repo && !strcmp(name, "repo.readme") && value != NULL) { if (*value == '/') cgit_repo->readme = xstrdup(value); else cgit_repo->readme = xstrdup(fmt("%s/%s", cgit_repo->path, value)); } else if (!strcmp(name, "include")) cgit_read_config(value, cgit_global_config_cb); } void cgit_querystring_cb(const char *name, const char *value) { if (!strcmp(name,"r")) { cgit_query_repo = xstrdup(value); cgit_repo = cgit_get_repoinfo(value); } else if (!strcmp(name, "p")) { cgit_query_page = xstrdup(value); cgit_cmd = cgit_get_cmd_index(value); } else if (!strcmp(name, "url")) { cgit_parse_url(value); } else if (!strcmp(name, "qt")) { cgit_query_grep = xstrdup(value); } else if (!strcmp(name, "q")) { cgit_query_search = xstrdup(value); } else if (!strcmp(name, "h")) { cgit_query_head = xstrdup(value); cgit_query_has_symref = 1; } else if (!strcmp(name, "id")) { cgit_query_sha1 = xstrdup(value); cgit_query_has_sha1 = 1; } else if (!strcmp(name, "id2")) { cgit_query_sha2 = xstrdup(value); cgit_query_has_sha1 = 1; } else if (!strcmp(name, "ofs")) { cgit_query_ofs = atoi(value); } else if (!strcmp(name, "path")) { cgit_query_path = trim_end(value, '/'); } else if (!strcmp(name, "name")) { cgit_query_name = xstrdup(value); } } void *cgit_free_commitinfo(struct commitinfo *info) { free(info->author); free(info->author_email); free(info->committer); free(info->committer_email); free(info->subject); + free(info->msg); + free(info->msg_encoding); free(info); return NULL; } int hextoint(char c) { if (c >= 'a' && c <= 'f') return 10 + c - 'a'; else if (c >= 'A' && c <= 'F') return 10 + c - 'A'; else if (c >= '0' && c <= '9') return c - '0'; else return -1; } char *trim_end(const char *str, char c) { int len; char *s, *t; if (str == NULL) return NULL; t = (char *)str; len = strlen(t); while(len > 0 && t[len - 1] == c) len--; if (len == 0) return NULL; c = t[len]; t[len] = '\0'; s = xstrdup(t); t[len] = c; return s; } char *strlpart(char *txt, int maxlen) { char *result; if (!txt) return txt; if (strlen(txt) <= maxlen) return txt; result = xmalloc(maxlen + 1); diff --git a/ui-shared.c b/ui-shared.c index 72a7b44..7c69f60 100644 --- a/ui-shared.c +++ b/ui-shared.c @@ -307,97 +307,97 @@ void cgit_print_date(time_t secs, char *format) time = gmtime(&secs); strftime(buf, sizeof(buf)-1, format, time); html_txt(buf); } void cgit_print_age(time_t t, time_t max_relative, char *format) { time_t now, secs; time(&now); secs = now - t; if (secs > max_relative && max_relative >= 0) { cgit_print_date(t, format); return; } if (secs < TM_HOUR * 2) { htmlf("<span class='age-mins'>%.0f min.</span>", secs * 1.0 / TM_MIN); return; } if (secs < TM_DAY * 2) { htmlf("<span class='age-hours'>%.0f hours</span>", secs * 1.0 / TM_HOUR); return; } if (secs < TM_WEEK * 2) { htmlf("<span class='age-days'>%.0f days</span>", secs * 1.0 / TM_DAY); return; } if (secs < TM_MONTH * 2) { htmlf("<span class='age-weeks'>%.0f weeks</span>", secs * 1.0 / TM_WEEK); return; } if (secs < TM_YEAR * 2) { htmlf("<span class='age-months'>%.0f months</span>", secs * 1.0 / TM_MONTH); return; } htmlf("<span class='age-years'>%.0f years</span>", secs * 1.0 / TM_YEAR); } void cgit_print_docstart(char *title, struct cacheitem *item) { - html("Content-Type: text/html; charset=utf-8\n"); + html("Content-Type: text/html; charset=" PAGE_ENCODING "\n"); htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); htmlf("Expires: %s\n", http_date(item->st.st_mtime + ttl_seconds(item->ttl))); html("\n"); html(cgit_doctype); html("<html>\n"); html("<head>\n"); html("<title>"); html_txt(title); html("</title>\n"); htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); html("<link rel='stylesheet' type='text/css' href='"); html_attr(cgit_css); html("'/>\n"); html("</head>\n"); html("<body>\n"); } void cgit_print_docend() { html("</td>\n</tr>\n<table>\n</body>\n</html>\n"); } int print_branch_option(const char *refname, const unsigned char *sha1, int flags, void *cb_data) { char *name = (char *)refname; html_option(name, name, cgit_query_head); return 0; } int print_archive_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data) { struct tag *tag; struct taginfo *info; struct object *obj; char buf[256], *url; unsigned char fileid[20]; int *header = (int *)cb_data; if (prefixcmp(refname, "refs/archives")) return 0; strncpy(buf, refname+14, sizeof(buf)); obj = parse_object(sha1); if (!obj) return 1; if (obj->type == OBJ_TAG) { |