有没有办法以某种方式git gui
显示和显示 UTF16 文件的差异?
我找到了一些信息,但这主要是指命令行而不是 gui。
在 msysGit 人员的帮助下,我一直在研究一个更好的解决方案,并提出了这个清洁/涂抹过滤器。过滤器使用 Gnu 文件和 iconv 命令来确定文件的类型,并将其与 msysGit 的内部 UTF-8 格式相互转换。
这种类型的清洁/涂抹过滤器为您提供更大的灵活性。在大多数情况下,它应该允许 Git 将您的混合格式文件视为 UTF-8 文本:diffs、merge、git-grep 以及 gitattributes 属性,如 eol-conversion、ident-replacement 和内置 diff 模式。
上面概述的差异过滤器解决方案仅适用于差异,因此受到更多限制。
要设置此过滤器:
将以下内容添加到 ~\Git\etc\gitconfig:
[filter "mixedtext"]
clean = iconv -sc -f $(file -b --mime-encoding %f) -t utf-8
smudge = iconv -sc -f utf-8 -t $(file -b --mime-encoding %f)
required
在全局 ~/Git/etc/gitattributes 或本地 ~/.gitattributes 中添加一行以处理混合格式文本,例如:
*.txt filter=mixedtext
我在一个包含 ANSI、UTF-16 和 UTF-8 格式的 sql 文件的目录上使用了它。到目前为止它正在工作。不出意外,这看起来像是 20% 的努力可以涵盖所有 Windows 文本格式问题的 80%。
此方法适用于 MSysGit 1.8.1,并在 Windows XP 上进行了测试。我使用 Git Extensions 2.44,但由于更改是在 Git 级别,它们应该也适用于 Git Gui。脚步:
安装Gnu Iconv。
创建以下脚本,将其命名为astextutf16
,并将其放在 Git 安装的 /bin 目录中(这是基于现有astextplain
脚本):
#!/bin/sh -e
# converts Windows Unicode (UTF-16 / UCS-2) to Git-friendly UTF-8
# notes:
# * requires Gnu iconv:
# http://gnuwin32.sourceforge.net/packages/libiconv.htm
# * this script must be placed in: ~/Git/bin
# * modify global ~/Git/etc/gitconfig or local ~/.git/config:
# [diff "astextutf16"]
# textconv = astextutf16
# * or, from command line:
# $ git config diff.astextutf16.textconv astextutf16
# * modify global ~/Git/etc/gitattributes or local ~/.gitattributes:
# *.txt diff=astextutf16
if test "$#" != 1 ; then
echo "Usage: astextutf16 <file>" 1>&2
exit 1
fi
# -f(rom) utf-16 -t(o) utf-8
"\Program Files\GnuWin32\bin\iconv.exe" -f utf-16 -t utf-8 "$1"
exit 0
修改全局 ~/Git/etc/gitconfig 或本地 ~/.git/config 文件,并添加以下行:
[diff "astextutf16"]
textconv = astextutf16
或者,从命令行:
$ git config diff.astextutf16.textconv astextutf16
修改全局 ~/Git/etc/gitattributes 或本地 ~/.gitattributes 文件,并映射要转换的扩展名:
*.txt diff=astextutf16
测试。现在应该可以看到 UTF-16 文件。
我遇到了类似的问题。
我想改进接受的答案,因为它有一个小缺陷。我遇到的问题是,如果文件不存在,我会收到此错误:
conversion to cannot unsupported
我更改了命令,因此不需要文件。它只使用标准输入/标准输出。这解决了这个问题。我的 .git/config 文件现在看起来像这样:
[filter "mixedtext"]
clean = "GITTMP=$(mktemp);TYPE=$( tee $GITTMP|file -b --mime-encoding - ); cat $GITTMP | iconv -sc -f $TYPE -t utf-8; rm -f $GITTMP"
smudge = "GITTMP=$(mktemp);TYPE=$( tee $GITTMP|file -b --mime-encoding - ); cat $GITTMP | iconv -sc -f utf-8 -t $TYPE; rm -f $GITTMP"
required = true
要在 .git/config 文件中创建条目,请使用以下命令:
git config --replace-all filter.mixedtext.clean 'GITTMP=$(mktemp);TYPE=$( tee $GITTMP|file -b --mime-encoding - ); cat $GITTMP | iconv -sc -f $TYPE -t utf-8; rm -f $GITTMP'
git config --replace-all filter.mixedtext.smudge 'GITTMP=$(mktemp);TYPE=$( tee $GITTMP|file -b --mime-encoding - ); cat $GITTMP | iconv -sc -f utf-8 -t $TYPE; rm -f $GITTMP'
git config --replace-all filter.mixedtext.required true
我的 .gitattributes 文件如下所示:
*.txt filter=mixedtext
*.ps1 filter=mixedtext
*.sql filter=mixedtext
仅指定可能存在问题的文件,否则清洁/涂抹必须做更多工作(临时文件)。
我们还将 git 中的 UTF-16le 文件批量转换为 UTF-8,因为这是 UTF 最紧凑和便携的编码。在 clean 和 smudge 中使用的相同 iconv 命令非常适合永久转换文件。
clean/smudge 命令的好处是,即使使用 UTF-16le 签入文件,差异仍然有效。