Code

git-svn: fix show-ignore
[git.git] / commit.c
index eb06afbbe0f00ac4f553e37c50eca290418a7907..2a58175aca16dc211cdf5a380e82bc9c0f4d1326 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -624,6 +624,48 @@ static char *get_header(const struct commit *commit, const char *key)
        }
 }
 
+static char *replace_encoding_header(char *buf, char *encoding)
+{
+       char *encoding_header = strstr(buf, "\nencoding ");
+       char *end_of_encoding_header;
+       int encoding_header_pos;
+       int encoding_header_len;
+       int new_len;
+       int need_len;
+       int buflen = strlen(buf) + 1;
+
+       if (!encoding_header)
+               return buf; /* should not happen but be defensive */
+       encoding_header++;
+       end_of_encoding_header = strchr(encoding_header, '\n');
+       if (!end_of_encoding_header)
+               return buf; /* should not happen but be defensive */
+       end_of_encoding_header++;
+
+       encoding_header_len = end_of_encoding_header - encoding_header;
+       encoding_header_pos = encoding_header - buf;
+
+       if (is_encoding_utf8(encoding)) {
+               /* we have re-coded to UTF-8; drop the header */
+               memmove(encoding_header, end_of_encoding_header,
+                       buflen - (encoding_header_pos + encoding_header_len));
+               return buf;
+       }
+       new_len = strlen(encoding);
+       need_len = new_len + strlen("encoding \n");
+       if (encoding_header_len < need_len) {
+               buf = xrealloc(buf, buflen + (need_len - encoding_header_len));
+               encoding_header = buf + encoding_header_pos;
+               end_of_encoding_header = encoding_header + encoding_header_len;
+       }
+       memmove(end_of_encoding_header + (need_len - encoding_header_len),
+               end_of_encoding_header,
+               buflen - (encoding_header_pos + encoding_header_len));
+       memcpy(encoding_header + 9, encoding, strlen(encoding));
+       encoding_header[9 + new_len] = '\n';
+       return buf;
+}
+
 static char *logmsg_reencode(const struct commit *commit)
 {
        char *encoding;
@@ -633,13 +675,20 @@ static char *logmsg_reencode(const struct commit *commit)
                                 : git_commit_encoding);
 
        if (!output_encoding)
+               output_encoding = "utf-8";
+       else if (!*output_encoding)
                return NULL;
        encoding = get_header(commit, "encoding");
-       if (!encoding || !strcmp(encoding, output_encoding)) {
-               free(encoding);
+       if (!encoding)
                return NULL;
-       }
-       out = reencode_string(commit->buffer, output_encoding, encoding);
+       if (!strcmp(encoding, output_encoding))
+               out = strdup(commit->buffer);
+       else
+               out = reencode_string(commit->buffer,
+                                     output_encoding, encoding);
+       if (out)
+               out = replace_encoding_header(out, output_encoding);
+
        free(encoding);
        if (!out)
                return NULL;
@@ -654,7 +703,7 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
                                  const char *after_subject,
                                  int relative_date)
 {
-       int hdr = 1, body = 0;
+       int hdr = 1, body = 0, seen_title = 0;
        unsigned long offset = 0;
        int indent = 4;
        int parents_shown = 0;
@@ -760,6 +809,8 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
                        body = 1;
 
                if (is_empty_line(line, &linelen)) {
+                       if (!seen_title)
+                               continue;
                        if (!body)
                                continue;
                        if (subject)
@@ -768,6 +819,7 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
                                break;
                }
 
+               seen_title = 1;
                if (subject) {
                        int slen = strlen(subject);
                        memcpy(buf + offset, subject, slen);