-1

I'm reposting this as it is not a duplicate. Please see my edit.

This was asked here, but no solution was provided for PowerShell. The solution given does not work in PowerShell (specifically, PowerShell 5.1 within VSCode).

I have tried

git config --global mergetool.vscode.cmd '"code --wait $MERGED"'

but I lose the double-quotes (i.e. there are no double-quotes in the corresponding .gitconfig file). FYI, the single-quotes are necessary to pass $MERGED as a string literal and not have PowerShell attempt to expand it.

I've also tried

echo '"code --wait $MERGED"' | git config --global mergetool.vscode.cmd
git config --global mergetool.vscode.cmd '`"code --wait $MERGED`"'
git config --global mergetool.vscode.cmd @'
code --wait $MERGED
'@

but nothing works. Is there no way to do this from within PowerShell?

Additionally, I've researched this question, and the solution of

git config --global mergetool.vscode.cmd '\"code --wait $MERGED\"'

nor

git config --global mergetool.vscode.cmd "\"code --wait $MERGED\""

nor

git config --global mergetool.vscode.cmd ('"code --wait $MERGED"' -replace '"', '\"')

does not work either. (I do not use PowerShell Core, v7, or any cross-platform variant thereof).

adam.hendry
  • 4,458
  • 5
  • 24
  • 51

1 Answers1

0

This isn't so much of an answer as it is some ideas to try. I couldn't get past all the quotes involved in calling git so I cheated and put the GIT directory in my path to get this to work. This is not a persistent change but it was enough to play around with it a bit.

$ENV:Path+=";C:\Program Files\Git\cmd"

Once that was done, I created a PS variable containing the command and ran it using Invoke-Expression. This allows the command to be created using variables like $merged on the fly.

$myCMD = "GIT Status"
Invoke-Expression $CMD

In your case, see if this helps to get you closer. Using the PS variable will allow $merged to be expanded as part of the command. I don't know what $merged is so I'm using a dummy value.

$Merged = "FooBar"
$Command = "GIT Config --global mergetool.vscode.cmd --wait $Merged"
Invoke-Expression $Command

Not sure if this helps but, give it a try

Ernest Correale
  • 451
  • 3
  • 5
  • Thanks, but I actually don't want `$MERGED` to be expanded. It must appear literally in the file as ```cmd = "code --wait $MERGED"``` – adam.hendry Apr 29 '21 at 04:20
  • 1
    [`Invoke-Expression` should generally be avoided](https://stackoverflow.com/a/51252636/45375); definitely [don't use it to invoke an external program or PowerShell script](https://stackoverflow.com/a/57966347/45375), such as `git.exe` in this case. Also, `Invoke-Expression` cannot help with the quoting problems associated with passing arguments to external programs. – mklement0 Apr 29 '21 at 09:51
  • My bad, I must have misread it. Can’t you Escape the $ in the string?. I can certainly see where the quoting issues come into play with the invoke command. I’d like to know more about the security concerns as well so thank you for posting the link. It never occurred to me since everything is running in the users security context. – Ernest Correale Apr 30 '21 at 05:23