]> git.ipfire.org Git - thirdparty/git.git/commitdiff
logmsg_reencode(): warn when iconv() fails
authorJeff King <peff@peff.net>
Fri, 27 Aug 2021 18:30:15 +0000 (14:30 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 27 Aug 2021 19:43:22 +0000 (12:43 -0700)
If the user asks for a pretty-printed commit to be converted (either
explicitly with --encoding=foo, or implicitly because the commit is
non-utf8 and we want to convert it), we pass it through iconv(). If that
fails, we fall back to showing the input verbatim, but don't tell the
user that the output may be bogus.

Let's add a warning to do so, along with a mention in the documentation
for --encoding. Two things to note about the implementation:

  - we could produce the warning closer to the call to iconv() in
    reencode_string_len(), which would let us relay the value of errno.
    But this is not actually very helpful. reencode_string_len() does
    not know we are operating on a commit, and indeed does not know that
    the caller won't produce an error of its own. And the errno values
    from iconv() are seldom helpful (iconv_open() only ever produces
    EINVAL; perhaps EILSEQ from iconv() might be illuminating, but it
    can also return EINVAL for incomplete sequences).

  - if the reason for the failure is that the output charset is not
    supported, then the user will see this warning for every commit we
    try to display. That might be ugly and overwhelming, but on the
    other hand it is making it clear that every one of them has not been
    converted (and the likely outcome anyway is to re-try the command
    with a supported output encoding).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/pretty-options.txt
pretty.c
t/t4210-log-i18n.sh

index 27ddaf84a195f46edc7b3f102eb1caf1adf68765..42b227bc4011f391e27ac2f921b66c993ede3eed 100644 (file)
@@ -40,7 +40,9 @@ people using 80-column terminals.
        defaults to UTF-8. Note that if an object claims to be encoded
        in `X` and we are outputting in `X`, we will output the object
        verbatim; this means that invalid sequences in the original
-       commit may be copied to the output.
+       commit may be copied to the output. Likewise, if iconv(3) fails
+       to convert the commit, we will output the original object
+       verbatim, along with a warning.
 
 --expand-tabs=<n>::
 --expand-tabs::
index 9631529c10a072a21acb76d65545d61a80b337d4..73b5ead5099d3efdb3e1999865ab868da5053ea6 100644 (file)
--- a/pretty.c
+++ b/pretty.c
@@ -671,7 +671,11 @@ const char *repo_logmsg_reencode(struct repository *r,
         * If the re-encoding failed, out might be NULL here; in that
         * case we just return the commit message verbatim.
         */
-       return out ? out : msg;
+       if (!out) {
+               warning("unable to reencode commit to '%s'", output_encoding);
+               return msg;
+       }
+       return out;
 }
 
 static int mailmap_name(const char **email, size_t *email_len,
index d2dfcf164e25b8771dd852d2aefc4aee4bd3f5ea..0141f36e338188c87ca6765871e67f091b7240f8 100755 (executable)
@@ -131,4 +131,11 @@ do
        fi
 done
 
+test_expect_success 'log shows warning when conversion fails' '
+       enc=this-encoding-does-not-exist &&
+       git log -1 --encoding=$enc 2>err &&
+       echo "warning: unable to reencode commit to ${SQ}${enc}${SQ}" >expect &&
+       test_cmp expect err
+'
+
 test_done