Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The output of git diff is not handled correctly in powershell

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.

Problem

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 ä).

Investigation

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!

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.

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:

Workaround

cmd /c "git diff reva revb > mypatch.patch"

Question

How does this work with powershell only?

like image 245
Andreas Avatar asked Sep 06 '18 13:09

Andreas


People also ask

Why is git diff not showing?

In answer to the original question, git diff isn't showing anything because you have a brand new directory, with a newly added file, but there are zero changes in the file for git diff to show. git status is showing that you added a new file, but git diff is for showing changes within files.

What is the output of git diff?

Diffing is a function that takes two input data sets and outputs the changes between them. git diff is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files and more.

What@@ means in git diff?

The fourth line shows you symbol @@ and symbols ahead of it. They are called chunks. Chunks in git diff define the change' summary. In our image below the following chunk can be seen @@ -1,2 +1 @@ This means that lines one and two were changed in the first file and line one was changed in the second file.

What does ++ mean in git diff?

When viewing a combined diff, if the two files you're comparing have a line that's different from what they were merged into, you will see the ++ to represent: one line that was added does not appear in either file1 or file2.


1 Answers

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.

It does not matter if you then use $Env:LESSCHARSET, respectively I believe it's not used anymore.

like image 155
Andreas Avatar answered Sep 22 '22 01:09

Andreas