git diff的输出未在Powershell中正确处理 [英] The output of git diff is not handled correctly in powershell
问题描述
我已经阅读了几个有关此问题的QA,但没有一个提供答案。有一种解决方法,我在此再次说明,但我想理解并解决问题。
I have read several QAs to this problem, but none provided an answer. There is a workaround, which I state here again, but I want to understand and solve the problem.
问题
在Powershell中,档案外的mypatch.patch 会产生「垃圾字元」,例如
The issue is that executing the command git diff reva revb | Out-File mypatch.patch
in powershell produces "garbage characters" in place of e.g. German umlauts (├ñ instead of ä).
调查
我在德国变音符号(├ñ而不是ä)。按照某些QA中的建议执行 $ Env:LESSCHARSET = utf8
,我的确在终端中获得了正确的输出,但是一旦将其重定向到文件 mypatch.patch
变音符(和其他字符)已变形。即使 git --no-pager diff reva revb
也会在终端中产生正确的输出。但是,一旦您想将其通过管道传输到文件,那是错误的。您看到的是不是得到的!
When I perform $Env:LESSCHARSET="utf8"
as suggested in some QAs, I do get correct output in the terminal, but once it is redirected to the file mypatch.patch
the umlauts (and other characters) are mangled. Even git --no-pager diff reva revb
results in correct output in the terminal. But as soon as you want to pipe that to a file, it is wrong. What you see is not what you get!
在我看来, Out-File的输入
已被破坏,因此设置 -Encoding
参数不会更改任何内容。我认为 Out-File
不应归咎于此。例如,命令 $ mypatch = git diff reva revb
(即使在diff之前添加了--no-pager)也会导致变量例如在终端上打印该变量时,欧元符号或变音符号出现乱码(Ôé¼而不是€)。
It seems to me that the input to Out-File
is already mangled and thus setting the -Encoding
argument does not change anything. I don't think Out-File
is to blame here. For instance, the command $mypatch = git diff reva revb
(even with --no-pager added before diff) results in a variable where e.g. Euro symbol or umlauts appear mangled (Ôé¼ instead of €) when that variable is printed to the terminal.
我尝试使用powershell 5.1和开放源代码powershell core 6.0.4 Windows 10(1709)。我使用git 2.18.0.windows.1。它可以在Windows命令行(cmd)上正常工作,因此简单的解决方法是从powershell控制台调用:
I tried powershell 5.1 and the open source powershell core 6.0.4 on Windows 10 (1709). I use git 2.18.0.windows.1. It works fine with the windows commandline (cmd), thus the simple workaround is to call from the powershell console:
解决方法
cmd / c git diff reva revb> mypatch.patch
问题
这仅适用于Powershell吗?
How does this work with powershell only?
推荐答案
问题似乎是由于 [Console] :: OutputEncoding
的错误设置引起的。如果未将其设置为UTF8,请尝试将其设置为: [Console] :: OutputEncoding = [System.Text.Encoding] :: UTF8
。
The problem seems to be caused by a wrong setting of [Console]::OutputEncoding
. If it is not set to UTF8, try setting it: [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
.
然后分别使用 $ Env:LESSCHARSET
没关系,我相信它已不再使用。
It does not matter if you then use $Env:LESSCHARSET
, respectively I believe it's not used anymore.
这篇关于git diff的输出未在Powershell中正确处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!