Search code examples
gitutf-16msysgiticonv

Can't get git to play nice with iconv and utf-16


I'm trying to get git to recognize UTF-16 as text to allow me to diff and patch as text natively, but I'm having trouble getting the textconv parameter to work.

I can manually call

iconv -f utf-16 -t utf-8 some-utf-16-file.rc

and everything is fine. But if I configure my .gitconfig as follows

[diff "utf16"]
    textconv = "iconv -f utf-16le -t utf-8"

and my .gitattributes:

# Custom for MFC
*.rc text eol=crlf diff=utf16

However, if I then if I run git diff, the following is displayed:

iconv: C:/Users/Mahmoud/AppData/Local/Temp/IjLBZ8_OemKey.rc:104:1: incomplete character or shift sequence

With procmon I was able to track it down as creating this process:

sh -c "iconv.exe -f utf-16le -t utf-8 \"$@\"" "iconv.exe -f utf-16le -t utf-8" C:/Users/Mahmoud/AppData/Local/Temp/JLOkVa_OemKey.rc

...which I can actually run fine (on the actual file, though).

Any ideas?

(Please note that I'm aware of the various solutions for getting git to work with UTF-16. I'm specifically trying to address this question of why iconv by itself works but it will not work when called by git. Also, this error was originally encountered while trying one of the linked solutions from the "duplicate" question. Thank you all kindly.)


Solution

  • Use only diff, it should work:

    *.rc diff=utf16
    

    text and eol cause git to substitute end-of-lines before passing data to iconv, after which it is not a valid utf16 anymore, as noted in comments.