git diff shows unicode symbols in angle brackets

后端 未结 5 1440
情话喂你
情话喂你 2020-12-14 02:39

I have a file with unicode symbols (russian text). When I fix some typo I use git diff --color-words=. to see the changes I\'ve done.

In case of unicode

5条回答
  •  孤街浪徒
    2020-12-14 03:16

    For several platforms setting LANG to C.UTF-8 (or en_US.UTF-8, etc.) would work:

    $ echo '人' >test1.txt && echo '丁' >test2.txt
    $ LANG=C.UTF-8 git diff --no-index --word-diff=plain --word-diff-regex=. -- test1.txt test2.txt
    diff --git a/test1.txt b/test2.txt
    index 3ef0891..3773917 100644
    --- a/test1.txt
    +++ b/test2.txt
    @@ -1 +1 @@
    [-人-]{+丁+}
    

    However, LANG doesn't seem to be honored on some platforms (such as Git for Windows):

    $ echo '人' >test1.txt && echo '丁' >test2.txt
    $ LANG=C.UTF-8 git diff --no-index --word-diff=plain --word-diff-regex=. -- test1.txt test2.txt
    diff --git a/test1.txt b/test2.txt
    index 3ef0891..3773917 100644
    --- a/test1.txt
    +++ b/test2.txt
    @@ -1 +1 @@
    [--]{+<81>+}
    

    A workaround on these platforms is to provide raw bytes for UTF-8 chars (e.g. $'[^\x80-\xBF][\x80-\xBF]*' for '.') to git diff:

    $ echo '人' >test1.txt && echo '丁' >test2.txt
    $ git diff --no-index --word-diff=plain --word-diff-regex=$'[^\x80-\xBF][\x80-\xBF]*' -- test1.txt test2.txt
    diff --git a/test1.txt b/test2.txt
    index 3ef0891..3773917 100644
    --- a/test1.txt
    +++ b/test2.txt
    @@ -1 +1 @@
    [-人-]{+丁+}
    

提交回复
热议问题