Teach log family --encoding
Updated commit objects record the encoding used in their encoding header. This updates the log family to reencode it into the encoding specified in i18n.commitencoding (or the default, which is "utf-8") upon output. To force a specific encoding that is different, log family takes command line flag --encoding=<encoding>; giving --encoding=none entirely disables the reencoding and lets you view log messges in their original encoding. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Dec 25, 2006 at 11:48 UTC
52883fbd767f8a79a6f98a08907d0a9f6ba1ece1
3 files changed
+76
-5
builtin-log.c
+17
-2
@@ -20,6 +20,8 @@ void add_head(struct rev_info *revs);
20
static void cmd_log_init(int argc, const char **argv, const char *prefix,
21
struct rev_info *rev)
22
{
23
+ int i;
24
+
25
rev->abbrev = DEFAULT_ABBREV;
26
rev->commit_format = CMIT_FMT_DEFAULT;
27
rev->verbose_header = 1;
@@ -27,8 +29,21 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
29
argc = setup_revisions(argc, argv, rev, "HEAD");
30
if (rev->diffopt.pickaxe || rev->diffopt.filter)
31
rev->always_show_header = 0;
30
- if (argc > 1)
31
- die("unrecognized argument: %s", argv[1]);
32
+ for (i = 1; i < argc; i++) {
33
+ const char *arg = argv[i];
34
+ if (!strncmp(arg, "--encoding=", 11)) {
35
+ arg += 11;
36
+ if (MAX_ENCODING_LENGTH <= strlen(arg))
37
+ die(" Value of output encoding '%s' too long",
38
+ arg);
39
+ if (strcmp(arg, "none"))
40
+ strcpy(git_commit_encoding, arg);
41
+ else
42
+ git_commit_encoding[0] = 0;
43
+ }
44
+ else
45
+ die("unrecognized argument: %s", arg);
46
+ }
47
}
48
49
static int cmd_log_walk(struct rev_info *rev)
commit.c
+58
-3
@@ -1,6 +1,7 @@
1
#include "cache.h"
2
#include "tag.h"
3
#include "commit.h"
4
+#include "utf8.h"
5
6
int save_commit_buffer = 1;
7
@@ -563,10 +564,53 @@ static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *com
564
return offset;
565
}
566
566
-unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
567
- unsigned long len, char *buf, unsigned long space,
567
+static char *get_header(const struct commit *commit, const char *key)
568
+{
569
+ int key_len = strlen(key);
570
+ const char *line = commit->buffer;
571
+
572
+ for (;;) {
573
+ const char *eol = strchr(line, '\n'), *next;
574
+
575
+ if (line == eol)
576
+ return NULL;
577
+ if (!eol) {
578
+ eol = line + strlen(line);
579
+ next = NULL;
580
+ } else
581
+ next = eol + 1;
582
+ if (!strncmp(line, key, key_len) && line[key_len] == ' ') {
583
+ int len = eol - line - key_len;
584
+ char *ret = xmalloc(len);
585
+ memcpy(ret, line + key_len + 1, len - 1);
586
+ ret[len - 1] = '\0';
587
+ return ret;
588
+ }
589
+ line = next;
590
+ }
591
+}
592
+
593
+static char *logmsg_reencode(const struct commit *commit)
594
+{
595
+ char *encoding = get_header(commit, "encoding");
596
+ char *out;
597
+
598
+ if (!encoding || !strcmp(encoding, git_commit_encoding))
599
+ return NULL;
600
+ out = reencode_string(commit->buffer, git_commit_encoding, encoding);
601
+ free(encoding);
602
+ if (!out)
603
+ return NULL;
604
+ return out;
605
+}
606
+
607
+unsigned long pretty_print_commit(enum cmit_fmt fmt,
608
+ const struct commit *commit,
609
+ unsigned long len,
610
+ char *buf, unsigned long space,
611
int abbrev, const char *subject,
569
- const char *after_subject, int relative_date)
612
+ const char *after_subject,
613
+ int relative_date)
614
{
615
int hdr = 1, body = 0;
616
unsigned long offset = 0;
@@ -574,6 +618,15 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit
618
int parents_shown = 0;
619
const char *msg = commit->buffer;
620
int plain_non_ascii = 0;
621
+ char *reencoded = NULL;
622
+
623
+ if (*git_commit_encoding) {
624
+ reencoded = logmsg_reencode(commit);
625
+ if (reencoded) {
626
+ msg = reencoded;
627
+ len = strlen(msg);
628
+ }
629
+ }
630
631
if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
632
indent = 0;
@@ -721,6 +774,8 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit
774
if (fmt == CMIT_FMT_EMAIL && !body)
775
buf[offset++] = '\n';
776
buf[offset] = '\0';
777
+
778
+ free(reencoded);
779
return offset;
780
}
781
revision.h
+1
@@ -72,6 +72,7 @@ struct rev_info {
72
const char *ref_message_id;
73
const char *add_signoff;
74
const char *extra_headers;
75
+ const char *log_reencode;
76
77
/* Filter by commit log message */
78
struct grep_opt *grep_filter;