I'm trying to write a custom git merge driver in power shell but I already fail at "doing nothing". That is opening the local file, writing the content back to it as the merge result and returning an exit code of 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
To the .git/config I added:
[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
And the .gitattributes looks like this:
*.txt text
file.txt merge=custom
With file.txt being a utf8 text file to merge.
Every time I try to merge/cherry-pick the file with conflicts I get error: add_cacheinfo failed to refresh for path 'file.txt'; merge aborting.
Example repository
I create a test repository at https://bitbucket.org/Deeem2031/mergedriver_test/src/master/
If you clone it and add the change to .git/config you should be able to reproduce the problem by running git cherry-pick dbb40ba
Things I already noticed
If you remove $PSDefaultParameterValues['*:Encoding'] = 'utf8'
or don't touch the file at all i.e. remove (Get-Content $localFile) | Out-File $localFile
the error is gone.
It doesn't seem to matter if file.txt is utf8 encoded beforehand or not.
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/out-file does state Input objects are automatically formatted as they would be in the terminal, [...]
which does imply it might change things but the only difference I can see (even with a hex-editor) is a added CRLF at the end of the file.
I did find this answer https://stackoverflow.com/a/65967910/6308948 which does sound very similar to my problem but I'm already writing my result to $localFile.
So the obvious question: Why does this error happen?