@matheusbsbs / matheus-bagliano / commits / 53af9816bc
Fork 0

i18n: drop "encoding" header in the output after re-coding.

After re-coding the commit message into the encoding the user specified (either with core.logoutputencidng or --encoding option), this drops the "encoding" header altogether. The output is after re-coding as the user asked (either with the config or --encoding=<encoding> option), and the extra header becomes redundant information. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Dec 30, 2006 at 15:49 UTC 53af9816bcb1d441fef76c3adaf0c4cb858768ac
1 file changed +45
commit.c
+45
@@ -624,6 +624,48 @@ static char *get_header(const struct commit *commit, const char *key)
624 }
625 }
626
627 +static char *replace_encoding_header(char *buf, char *encoding)
628 +{
629 + char *encoding_header = strstr(buf, "\nencoding ");
630 + char *end_of_encoding_header;
631 + int encoding_header_pos;
632 + int encoding_header_len;
633 + int new_len;
634 + int need_len;
635 + int buflen = strlen(buf) + 1;
636 +
637 + if (!encoding_header)
638 + return buf; /* should not happen but be defensive */
639 + encoding_header++;
640 + end_of_encoding_header = strchr(encoding_header, '\n');
641 + if (!end_of_encoding_header)
642 + return buf; /* should not happen but be defensive */
643 + end_of_encoding_header++;
644 +
645 + encoding_header_len = end_of_encoding_header - encoding_header;
646 + encoding_header_pos = encoding_header - buf;
647 +
648 + if (is_encoding_utf8(encoding)) {
649 + /* we have re-coded to UTF-8; drop the header */
650 + memmove(encoding_header, end_of_encoding_header,
651 + buflen - (encoding_header_pos + encoding_header_len));
652 + return buf;
653 + }
654 + new_len = strlen(encoding);
655 + need_len = new_len + strlen("encoding \n");
656 + if (encoding_header_len < need_len) {
657 + buf = xrealloc(buf, buflen + (need_len - encoding_header_len));
658 + encoding_header = buf + encoding_header_pos;
659 + end_of_encoding_header = encoding_header + encoding_header_len;
660 + }
661 + memmove(end_of_encoding_header + (need_len - encoding_header_len),
662 + end_of_encoding_header,
663 + buflen - (encoding_header_pos + encoding_header_len));
664 + memcpy(encoding_header + 9, encoding, strlen(encoding));
665 + encoding_header[9 + new_len] = '\n';
666 + return buf;
667 +}
668 +
669 static char *logmsg_reencode(const struct commit *commit)
670 {
671 char *encoding;
@@ -642,6 +684,9 @@ static char *logmsg_reencode(const struct commit *commit)
684 return NULL;
685 }
686 out = reencode_string(commit->buffer, output_encoding, encoding);
687 + if (out)
688 + out = replace_encoding_header(out, output_encoding);
689 +
690 free(encoding);
691 if (!out)
692 return NULL;