首页 > 解决方案 > 带有自定义电源外壳合并驱动程序的“错误:add_cacheinfo 无法刷新路径”

问题描述

我正在尝试在 power shell 中编写一个自定义的 git merge 驱动程序,但我已经在“什么都不做”方面失败了。即打开本地文件,将内容作为合并结果写回它,并返回退出代码 0。

param (
    [Parameter(Mandatory=$true)][string]$baseFile,
    [Parameter(Mandatory=$true)][string]$localFile,
    [Parameter(Mandatory=$true)][string]$remoteFile
)

$PSDefaultParameterValues['*:Encoding'] = 'utf8'

(Get-Content $localFile) | Out-File $localFile
exit 0

在 .git/config 我添加了:

[merge "custom"]
    name = custom powershell merge driver
    driver = c:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy RemoteSigned -Command ./mergeDriver.ps1 -baseFile %O -localFile %A -remoteFile %B

.gitattributes 看起来像这样:

*.txt text
file.txt merge=custom

file.txt 是要合并的 utf8 文本文件。

每次我尝试合并/挑选有冲突的文件时,我都会得到error: add_cacheinfo failed to refresh for path 'file.txt'; merge aborting.

示例存储库

我在https://bitbucket.org/Deeem2031/mergedriver_test/src/master/创建了一个测试存储库 如果你克隆它并将更改添加到 .git/config 你应该能够通过运行重现问题git cherry-pick dbb40ba

我已经注意到的事情

如果您删除$PSDefaultParameterValues['*:Encoding'] = 'utf8'或根本不触摸文件,即删除(Get-Content $localFile) | Out-File $localFile,错误就消失了。

file.txt 是否事先进行了 utf8 编码似乎并不重要。

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/out-file确实Input objects are automatically formatted as they would be in the terminal, [...]表明它可能会改变事情,但我能看到的唯一区别(即使使用十六进制编辑器) 是在文件末尾添加的 CRLF。

我确实找到了这个答案https://stackoverflow.com/a/65967910/6308948这听起来与我的问题非常相似,但我已经将我的结果写入 $localFile。

所以很明显的问题:为什么会发生这个错误?

标签: gitpowershellencodingmerge

解决方案


问题实际上出在合并驱动程序中,即mergeDriver.ps1.

Git 的约定是合并驱动程序接收和写入的文件内容采用“干净”形式,即在您的情况下,只有 LF(或至少没有 CR/LF)行结尾。

但是,(Get-Content $localFile) | Out-File $localFile不会使文件内容保持不变,尽管它可能看起来如何。原因是$localFile在您的情况下根本没有行结束。hexdump -C关于它的内容是这样说的:

00000000  ef bb bf 76 65 72 73 69  6f 6e 20 3d 20 31 35 30  |...version = 150|

Out-File 附加一个 CR/LF

因此,如果您将合并驱动程序更改为调用Out-File -NoNewline,它将在没有该add_cacheinfo错误消息的情况下工作。

也就是说,它将按预期工作:这cherry-pick将表示没有任何更改,并询问您是否仍要提交(这将需要该--allow-empty选项)。


推荐阅读