powershell - Windows 中的 Git Shell : patch's default character encoding is UCS-2 Little Endian - how to change this to ANSI or UTF-8 without BOM?

标签 powershell git encoding github

在 Windows 中使用 Git Shell 创建diff 补丁(使用GitHub for Windows 时),补丁的字符编码 将为UCS-2 Little根据 Notepad++ 的 Endian(参见下面的屏幕截图)。

我怎样才能改变这种行为,并强制 git 使用没有 BOM 字符编码的 ANSI 或 UTF-8 创建补丁?

因为无法应用 UCS-2 Little Endian 编码的补丁而导致问题,我必须手动将其转换为 ANSI。如果我不这样做,我会收到“fatal: unrecognized input”错误。

Creating git patch

Notepad++ screenshot of the character encoding


从那时起,我也意识到我必须在Notepad++中手动将EOL从Windows格式(\r\n)转换为UNIX(\n)(编辑> EOL 转换 > UNIX)。如果我不这样做,我会收到“尾随空格”错误(即使所有空格都被修剪:“TextFX”>“TextFX 编辑”>“修剪尾随空格”)。

因此,要应用补丁我需要执行的步骤:

  1. 创建补丁(here is the result)
  2. convert character encoding to ANSI
  3. EOL conversion to UNIX format
  4. 应用补丁

请看这张截图:

Applying a patch in Windows Powershell with Git is problematic

最佳答案

我不是 Windows 用户,所以请对我的回答持保留态度。根据Windows PowerShell Cookbook , PowerShell 预处理 git diff 的输出,将其分成几行。 Out-File 的文档Cmdlet 建议,>| 相同不带参数的 Out-File。我们还在 PowerShell documentation 中找到此评论:

The results of using the Out-File cmdlet may not be what you expect if you are used to traditional output redirection. To understand its behavior, you must be aware of the context in which the Out-File cmdlet operates.

By default, the Out-File cmdlet creates a Unicode file. This is the best default in the long run, but it means that tools that expect ASCII files will not work correctly with the default output format. You can change the default output format to ASCII by using the Encoding parameter:

[...]

Out-file formats file contents to look like console output. This causes the output to be truncated just as it is in a console window in most circumstances. [...]

To get output that does not force line wraps to match the screen width, you can use the Width parameter to specify line width.

所以,显然选择字符编码的不是 Git,而是 Out-File。这表明 a) PowerShell 重定向真的应该只用于文本和 b)

| Out-File -encoding ASCII -Width 2147483647 my.patch

将避免编码问题。但是,这仍然没有解决 Windows 与 Unix 行尾的问题。有一些 Cmdlet(请参阅 PowerShell Community Extensions)可以转换行尾。

然而,所有这些重新编码并没有增加我对补丁的信心(补丁本身没有编码,只是一串字节)。前述Cookbook包含一个脚本 Invoke-BinaryProcess,可用于重定向未修改命令的输出。

要回避整个问题,另一种方法是使用 git format-patch 而不是 git diffformat-patch 直接写入文件(而不是标准输出),因此它的输出不会被重新编码。但是,它只能从提交中创建补丁,而不是任意差异。

format-patch 采用提交范围(例如 master^10..master^5)或单个提交(例如 X,表示 X..HEAD)和创建格式为 NNNN-SUBJECT.patch 的补丁文件,其中 NNNN 是递增的 4 位数字,主题是补丁的(损坏的)主题。可以使用 -o 指定输出目录。

关于powershell - Windows 中的 Git Shell : patch's default character encoding is UCS-2 Little Endian - how to change this to ANSI or UTF-8 without BOM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46441079/

相关文章:

PowerShell 作业与启动进程

powershell - 我的 DirectorySecurity 对象上的这个 "access"属性来自哪里?

git - 如何自动检测 Git 提交或 rebase 是否包含类似 "<<<<< HEAD"的内容?

git - "pure git"有pull request的概念吗?

gitignore 不工作 .env 文件正在更新

python - 使用 ElementTree 强制对不良 XML 文件进行编码

powershell - 使用 PowerShell 从 FTP 下载最新文件

Golang 结构的 XML 和 JSON 标签?

python - 这个字节串实际占用了多少内存?

sharepoint - 如何从 powershell 发布/批准 SharePoint 2010 中的页面