lazygit icon indicating copy to clipboard operation
lazygit copied to clipboard

Custom command containing & does not work on windows

Open worawatt opened this issue 2 years ago • 1 comments

Describe the bug On windows, ampersand (&) in a custom command seems to always be escaped with ^&. As a result, the command sometimes does not function as intended.

For example, running the 'Checkout a remote branch as FETCH_HEAD' (example from https://github.com/jesseduffield/lazygit/blob/master/docs/Custom_Command_Keybindings.md#custom-command-keybindings) command: "git fetch {{index .PromptResponses 0}} {{index .PromptResponses 1}} && git checkout FETCH_HEAD"

Result in an error: fatal: couldn't find remote ref && Command Log show: cmd /c git fetch origin wip/my_work ^&^& git checkout FETCH_HEAD

This also happens with other charactors like %.

For example, command: "echo %cd%" prints %cd% instead of printing the path of the current directory.

Is there any workaround?

Version info: commit=16802a048e0425182f08cf0e9a2bc31480e9a55d, build date=2023-02-01T11:02:43Z, build source=binaryRelease, version=0.37.0, os=windows, arch=amd64, git version=2.38.1.windows.1 git version 2.38.1.windows.1

worawatt avatar Feb 09 '23 09:02 worawatt

Same problem here. Also > and | are escaped by ^.

I solved it by invoking powershell script instead of writing the command inline.

config.yml:

customCommands:
  - key: '<c-f>'
    description: 'Rebase to master'
    context: 'localBranches'
    command: 'pwsh -NoProfile -File "PATH_TO_MY_SCRIPT\RebaseToMaster.ps1" {{.SelectedLocalBranch.Name}}'

PS script:

param([String]$localBranchName) 

git rebase origin/master $localBranchName --autostash
if (!$?)
{
    Write-Error "Rebase was unsuccessful."
    exit -1
}

git submodule update
if (!$?)
{
    Write-Error "Submodule update was unsuccessful."
    exit -1
}

kindermannhubert avatar May 31 '24 08:05 kindermannhubert