【问题标题】:Git Config Double Quotes in PowerShell Round 2 [duplicate]PowerShell第2轮中的Git Config双引号[重复]
【发布时间】:2021-04-29 12:10:24
【问题描述】:

我重新发布这个,因为它不是重复的。请查看我的编辑。

有人问过here,但没有为 PowerShell 提供解决方案。给出的解决方案不适用于 PowerShell(特别是 VSCode 中的 PowerShell 5.1)。

我试过了

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

但我丢失了双引号(即相应的 .gitconfig 文件中没有双引号)。仅供参考,单引号是传递 $MERGED 作为字符串文字所必需的,并且没有 PowerShell 尝试扩展它。

我也试过

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
'@

但没有任何效果。有没有办法在 PowerShell 中做到这一点?

另外,我研究了this question,以及解决方法

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

也没有

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

也没有

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

也不起作用。 (我不使用 PowerShell Core、v7 或其任何跨平台变体)。

【问题讨论】:

    标签: powershell double-quotes git-config


    【解决方案1】:

    这与其说是一个答案,不如说是一些可以尝试的想法。我无法超越调用 git 所涉及的所有引号,所以我作弊并将 GIT 目录放在我的路径中以使其正常工作。这不是一个持久的变化,但它已经足够了。

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

    完成后,我创建了一个包含命令的 PS 变量并使用 Invoke-Expression 运行它。这允许使用诸如 $merged 之类的变量动态创建命令。

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

    就您而言,看看这是否有助于拉近您的距离。使用 PS 变量将允许 $merged 作为命令的一部分进行扩展。我不知道 $merged 是什么,所以我使用了一个虚拟值。

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

    不确定这是否有帮助,但请尝试一下

    【讨论】:

    • 谢谢,但我实际上不想扩大$MERGED。它必须在文件中按字面意思显示为cmd = "code --wait $MERGED"
    • Invoke-Expression should generally be avoided;绝对是don't use it to invoke an external program or PowerShell script,比如本例中的git.exe。此外,Invoke-Expression 无法帮助解决与将参数传递给外部程序相关的引用问题。
    • 我的错,我一定是看错了。你不能转义字符串中的 $ 吗?我当然可以看到调用命令的引用问题在哪里起作用。我也想了解更多有关安全问题的信息,因此感谢您发布链接。我从来没有想过,因为一切都在用户安全上下文中运行。
    猜你喜欢
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-25
    • 2012-04-15
    • 2010-10-08
    相关资源
    最近更新 更多