我有一个带有unicode符号的文件(俄文)。
当我修正一些拼写错误时,我会使用git diff --color-words=.
来查看我所做的更改。
在unicode(西里尔文)符号的情况下,我得到一些像尖括号一样混乱:
$ cat p1
привет
$ cat p2
Привет
$ git diff --color-words=. --no-index p1 p2
diff --git 1/p1 2/p2
index d0f56e1..d84c480 100644
--- 1/p1
+++ 2/p2
@@ -1 +1 @@
<D0><BF><9F>ривет
看起来git diff --color-words=.
正在检查字节之间的差异,而不是像我期望的那样检查符号之间的区别。
有没有办法告诉git
使用unicode符号正常工作?
UPD 关于我的环境:我在Mac OS和Linux主机上也是如此。
我的shell vars是:
BASH=/bin/bash
HOSTTYPE=x86_64
LANG=ru_RU.UTF-8
OSTYPE=darwin10.0
PS1='\h:\W \u\$ '
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
TERM=xterm-256color
TERM_PROGRAM=iTerm.app
_=-l
我已将git config重置为默认设置,如下所示:
$ git config -l
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
git version
$ git --version
git version 1.7.3.5
答案 0 :(得分:25)
对我而言{gid寻呼机 - 应该受到责备(感谢@kostix)。通过完全禁用寻呼机进行实验:
less
我的案例是包含表情符号的提交消息;它虽然基本上是同样的问题。
git --no-pager diff p1 p2
NB :$ git log --oneline
93a1866 <U+1F43C>
$ git --no-pager log --oneline
93a1866
$ export LESS='--raw-control-chars'
$ git log --oneline
93a1866
$ git config --global core.pager 'less --raw-control-chars'
$ git log --oneline
93a1866
选项会导致--RAW-CONTROL-CHARS
通过ANSI颜色转义,但仍会使其他控制字符(包括表情符号)。我的less
全局配置为less
,我的git寻呼机配置为--RAW-CONTROL-CHARS
,如上所述。
答案 1 :(得分:12)
对我来说,最好的解决方法是设置export LESSCHARSET=utf-8
。
在这种情况下,git log -p
和git diff
都会显示unicode而不会出现问题。
答案 2 :(得分:2)
我的解决方案是使用git difftool。
我根据https://github.com/chestozo/dmp编写了此工具https://code.google.com/p/google-diff-match-patch/。
与git diff --color-words=.
相比,有时它也会提供更好的差异:)
答案 3 :(得分:1)
对于将LANG
设置为C.UTF-8
(或en_US.UTF-8
等)的多个平台,可以使用:
$ 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 @@
[-人-]{+丁+}
但是,LANG
在某些平台(例如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 @@
<E4>[-<BA><BA>-]{+<B8><81>+}
这些平台上的解决方法是为UTF-8字符提供原始字节(例如$'[^\x80-\xBF][\x80-\xBF]*'
'.'
)到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 @@
[-人-]{+丁+}
答案 4 :(得分:1)
toolbear 的回答对我不起作用,因为即使使用 git --no-pager diff
我也看到了不可读的字符(不是括号,而是不可读),所以 less
不是核心问题。
我尝试了很多东西,但唯一有帮助的是将从 Cyrillic 到 utf-8 的显式转换包含到 .git\config 中(我使用的是 Windows 7)
[pager]
diff = iconv.exe -f cp1251 -t utf-8 | less
注意,我在这里专门更改了 pager.diff
,因为我只在使用 diff
命令时遇到了编码问题。出于某种奇怪的原因,log
和 reflog
对我来说很好用。但是,如果其他命令也有编码问题,则应该为所有命令更改寻呼机,如下所示:
[core]
...
pager = iconv.exe -f cp1251 -t utf-8 | less
答案 5 :(得分:0)
我见过很多报道,在某些情况下,xterm实际上无法打印Unicode字符。也许至少是解决方案的起点。