git分支名称中的Powershell和德语变音符 [英] Powershell and german umlauts in git branch names

查看:119
本文介绍了git分支名称中的Powershell和德语变音符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个批处理文件,该文件使用powershell命令删除除保留的本地git分支外的所有本地git分支.如果分支名称中使用德国变音符号,则无法使用.

I have written a batch file which uses a powershell command to delete all local git branches except one to keep. If there are german umlauts used in branch names, it does not work.

切换到分支"master"

Switched to branch 'master'

您的分支机构的最新信息是来源/主".

Your branch is up-to-date with 'origin/master'.

已删除分支DEV_API_StartenDesWorkers(原为61bec6d883b).

Deleted branch DEV_API_StartenDesWorkers (was 61bec6d883b).

错误:找不到分支'DEV_✜£ersicht_Drucken'.

error: branch 'DEV_â"œÂ£ersicht_Drucken' not found.

错误:找不到分支'test_prâ" –â€"fung".

error: branch 'test_prâ"œâ•�fung' not found.

正确的名称是DEV_Übersicht_druckentest_prüfung.

如何也可以删除这些分支?

How can I achieve to delete these branches too?

这是脚本:

@echo off
set KEEP=%1
IF [%KEEP%] == [] (goto eof)

git checkout %KEEP%

powershell "git branch -D @(git branch | select-string -NotMatch %KEEP% | ForEach-Object {$_.Line.Trim() })"

:eof

推荐答案

免责声明:我绝不是这方面的专家-以下答案可以为我解决症状,但您的行驶里程可能会有所不同.精通Windows代码页之类的其他人也许可以给出更好的答案...

Disclaimer: I'm by no means an expert in this stuff - the answer below fixes the symptoms for me, but your mileage may vary. Someone else with deeper knowledge of Windows codepages and the like might be able to give a better answer...

从我所读的内容来看,问题的核心是git在utf8中写入其输出,如@lorek和@LeGEC在注释中指出的那样,但是它被命令提示符所使用的Windows代码页所破坏

From what I've read, the core of the problem is that git is writing its output in utf8 as noted by @lorek and @LeGEC in the comments, but it's being mangled by the Windows codepage in use by the command prompt.

您可以在使用和不使用PowerShell的情况下重现该行为:

You can reproduce the behaviour with and without PowerShell:

c:\repo> git status
On branch test_prüfung
nothing to commit, working tree clean

c:\repo> git branch
* test_pr<C3><BC>fung

c:\repo> git branch | more
* test_pr├╝fung

c:\repo> powershell "$x = git branch; write-host $x"
* test_pr├╝fung

c:\repo> powershell "git branch -D @(git branch | select-string -NotMatch master | ForEach-Object {$_.Line.Trim() })"
error: branch '* test_pr├╝fung' not found.

发生的事情是git将其输出编码为utf8字节,然后外壳程序使用不同的编码对 进行解码-像这样:

What's happening is git is encoding its output into utf8 bytes and then the shell is decoding that using a different encoding - something like this:

$branch = "test_prüfung";
$utf8 = [System.Text.Encoding]::Utf8.GetBytes($branch);
$mangled = [System.Text.Encoding]::GetEncoding(437).GetString($utf8);
write-host $mangled

输出:

test_pr├╝fung

在我的案例中,神奇的编码437"是通过调用chcp来获取外壳程序的当前代码页来确定的:

In my case, the magic "encoding 437" was determined by calling chcp to get the shell's current codepage:

C:\> chcp
Active code page: 437

chcp文档告诉我437是United States的代码页.

And the documentation for chcp tells me that 437 is the codepage for United States.

似乎为我解决此问题的方法是使用代码页65001 (即UTF8),则您得到:

What seems to fix the issue for me is to use codepage 65001 (i.e. UTF8) and then you get:

C:\repo> chcp 65001
Active code page: 65001

c:\repo> powershell "$x = git branch; write-host $x"
* test_prüfung

现在这也可以正常工作:

And this now works fine too:

c:\repo> powershell "git branch -D @(git branch | select-string -NotMatch master | ForEach-Object {$_.Line.Trim() })"
Deleted branch test_prüfung (was 1e9bc02).

希望这会有所帮助...

Hope this helps a little bit...

这篇关于git分支名称中的Powershell和德语变音符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆