jesseduffield / lazygit

simple terminal UI for git commands
MIT License
52.99k stars 1.85k forks source link

lazygit crashes when directory has a " ' " #3089

Open jericjan opened 1 year ago

jericjan commented 1 year ago

Describe the bug If my current directory has a ' in the name, lazygit will crash

To Reproduce Steps to reproduce the behavior:

  1. Create a folder called test ' hello
  2. Open the folder and initialize a repository
  3. Run lazygit

Expected behavior Lazygit running

Screenshots No screenshot but i have this error:

panic: Starting quote has no ending quote.

goroutine 1 [running]:
github.com/mgutz/str.ToArgv({0xc00029c8d0, 0x23})
        /home/runner/work/lazygit/lazygit/vendor/github.com/mgutz/str/funcsPZ.go:377 +0x51b
github.com/jesseduffield/lazygit/pkg/commands/oscommands.(*CmdObjBuilder).NewShell(0xc0000081f8, {0xc000038e60?, 0x1c?})        /home/runner/work/lazygit/lazygit/pkg/commands/oscommands/cmd_obj_builder.go:55 +0x32d
github.com/jesseduffield/lazygit/pkg/commands/oscommands.(*OSCommand).UpdateWindowTitle(0xc00006e200)
        /home/runner/work/lazygit/lazygit/pkg/commands/oscommands/os.go:344 +0xef
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).loadNewRepo(0xc000428000)
        /home/runner/work/lazygit/lazygit/pkg/gui/gui.go:811 +0x6a
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).onInitialViewsCreationForRepo(0xc000428000)
        /home/runner/work/lazygit/lazygit/pkg/gui/layout.go:196 +0xf1
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).layout(0xc000428000, 0xc000059520)
        /home/runner/work/lazygit/lazygit/pkg/gui/layout.go:124 +0x6b7
github.com/jesseduffield/gocui.ManagerFunc.Layout(0x0?, 0x0?)
        /home/runner/work/lazygit/lazygit/vendor/github.com/jesseduffield/gocui/gui.go:697 +0x1f
github.com/jesseduffield/gocui.(*Gui).flush(0xc000059520)
        /home/runner/work/lazygit/lazygit/vendor/github.com/jesseduffield/gocui/gui.go:1139 +0xc4
github.com/jesseduffield/gocui.(*Gui).processEvent(0xc000059520)
        /home/runner/work/lazygit/lazygit/vendor/github.com/jesseduffield/gocui/gui.go:766 +0x2a5
github.com/jesseduffield/gocui.(*Gui).MainLoop(0xc000059520)
        /home/runner/work/lazygit/lazygit/vendor/github.com/jesseduffield/gocui/gui.go:739 +0xa5
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).Run(0xc000428000, {{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}})
        /home/runner/work/lazygit/lazygit/pkg/gui/gui.go:669 +0x5e5
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).RunAndHandleError.func1()
        /home/runner/work/lazygit/lazygit/pkg/gui/gui.go:675 +0x4c
github.com/jesseduffield/lazygit/pkg/utils.SafeWithError(0x0?)
        /home/runner/work/lazygit/lazygit/pkg/utils/utils.go:108 +0x67
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).RunAndHandleError(0xc000428000, {{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}})
        /home/runner/work/lazygit/lazygit/pkg/gui/gui.go:674 +0xd4
github.com/jesseduffield/lazygit/pkg/app.(*App).Run(...)
        /home/runner/work/lazygit/lazygit/pkg/app/app.go:263
github.com/jesseduffield/lazygit/pkg/app.Run({0x18b9a70?, 0xc000230820?}, 0xc000250540, {{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}})
        /home/runner/work/lazygit/lazygit/pkg/app/app.go:48 +0xe9
github.com/jesseduffield/lazygit/pkg/app.Start(0xc0000c5f30, {0x0, 0x0})
        /home/runner/work/lazygit/lazygit/pkg/app/entry_point.go:150 +0xd26
main.main()
        /home/runner/work/lazygit/lazygit/main.go:23 +0x9e

Version info: Run lazygit --version and paste the result here commit=5e388e21c8ca6aa883dbcbe45c47f6fdd5116815, build date=2023-08-07T14:05:48Z, build source=binaryRelease, version=0.40.2, os=windows, arch=amd64, git version=2.33.0.windows.2

Run git --version and paste the result here git version 2.33.0.windows.2

Additional context This was brought up on #890, and was "fixed" but it apparently still happens

I haven't tried compiling from the latest master branch, since I don't know anything about Go.

AzraelSec commented 1 year ago

Hello @jericjan 👋🏻

I cannot reproduce the issue with lazygit v0.40.2 on Linux, but the escaping mechanism differs from one OS to another: image commit=v0.40.2, build date=2023-08-12T17:47:33Z, build source=binaryRelease, version=0.40.2, os=linux, arch=amd64, git version=2.42.0


I don't have a Windows machine at the moment, but I'm wondering if applying something similar to this patch would solve the issue 🤔

From 196d9a5780a54132373f075682e73a69c6f061e6 Mon Sep 17 00:00:00 2001
From: AzraelSec <me@azraelsec.sh>
Date: Sun, 5 Nov 2023 12:13:01 +0100
Subject: [PATCH] wip

---
 pkg/commands/oscommands/cmd_obj_builder.go | 3 ++-
 pkg/commands/oscommands/os_test.go         | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/pkg/commands/oscommands/cmd_obj_builder.go b/pkg/commands/oscommands/cmd_obj_builder.go
index 77fb7e7c..0b8ed55c 100644
--- a/pkg/commands/oscommands/cmd_obj_builder.go
+++ b/pkg/commands/oscommands/cmd_obj_builder.go
@@ -66,7 +66,7 @@ func (self *CmdObjBuilder) CloneWithNewRunner(decorate func(ICmdObjRunner) ICmdO
    }
 }

-const CHARS_REQUIRING_QUOTES = "\"\\$` "
+const CHARS_REQUIRING_QUOTES = "\"\\$` '"

 // If you update this method, be sure to update CHARS_REQUIRING_QUOTES
 func (self *CmdObjBuilder) Quote(message string) string {
@@ -76,6 +76,7 @@ func (self *CmdObjBuilder) Quote(message string) string {
        message = strings.NewReplacer(
            `"`, `"'"'"`,
            `\"`, `\\"`,
+           `'`, `\'`,
        ).Replace(message)
    } else {
        quote = `"`
diff --git a/pkg/commands/oscommands/os_test.go b/pkg/commands/oscommands/os_test.go
index fbae7685..9f5c84db 100644
--- a/pkg/commands/oscommands/os_test.go
+++ b/pkg/commands/oscommands/os_test.go
@@ -75,7 +75,7 @@ func TestOSCommandQuoteWindows(t *testing.T) {

    actual := osCommand.Quote(`hello "test" 'test2'`)

-   expected := `\"hello "'"'"test"'"'" 'test2'\"`
+   expected := `\"hello "'"'"test"'"'" \'test2\'\"`

    assert.EqualValues(t, expected, actual)
 }
-- 
2.42.0
mark2185 commented 1 year ago

@AzraelSec unfortunately that didn't solve it, but I'll try to dig there as well.

Btw try out https://github.com/quickemu-project/quickemu for a Windows VM, it basically boils down to two shell commands.

stefanhaller commented 1 year ago

This patch fixes it for me:

diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go
index fc13eedf..815838d9 100644
--- a/pkg/commands/oscommands/os.go
+++ b/pkg/commands/oscommands/os.go
@@ -1,7 +1,6 @@
 package oscommands

 import (
-   "fmt"
    "io"
    "os"
    "os/exec"
@@ -340,6 +339,6 @@ func (c *OSCommand) UpdateWindowTitle() error {
    if getWdErr != nil {
        return getWdErr
    }
-   argString := fmt.Sprint("title ", filepath.Base(path), " - Lazygit")
-   return c.Cmd.NewShell(argString).Run()
+   args := []string{"cmd", "/c", "title " + filepath.Base(path) + " - Lazygit"}
+   return c.Cmd.New(args).Run()
 }

I didn't test this extensively, e.g. what happens when there are other special characters in the string, or double quotes, or whatever.

I don't have time to turn this into a PR, if anybody feels like picking it up from here, feel free!