crash if opened from a .git folder
Describe the bug Lazygit crashes when invoked from the .git folder of a regular local Git repository.
Way to Reproduce it The following commands
$ cd /my/local/repository/
$ cd .git/
$ lazygit
result in the following error:
2024/04/20 23:57:12 An error occurred! Please create an issue at: https://github.com/jesseduffield/lazygit/issues
*errors.errorString Error getting repo paths: 'git rev-parse --path-format=absolute --show-toplevel --absolute-git-dir --git-common-dir --show-superproject-working-tree' failed: fatal: this operation must be run in a work tree
/home/federico/go/pkg/mod/github.com/jesseduffield/[email protected]/pkg/commands/git.go:65 (0x9a7fc9)
NewGitCommand: return nil, errors.Errorf("Error getting repo paths: %v", err)
/home/federico/go/pkg/mod/github.com/jesseduffield/[email protected]/pkg/gui/gui.go:300 (0xb312d5)
(*Gui).onNewRepo: gui.git, err = commands.NewGitCommand(
/home/federico/go/pkg/mod/github.com/jesseduffield/[email protected]/pkg/gui/gui.go:692 (0xb34078)
(*Gui).Run: if err := gui.onNewRepo(startArgs, context.NO_CONTEXT); err != nil {
/home/federico/go/pkg/mod/github.com/jesseduffield/[email protected]/pkg/gui/gui.go:711 (0xb34705)
(*Gui).RunAndHandleError.func1: if err := gui.Run(startArgs); err != nil {
/home/federico/go/pkg/mod/github.com/jesseduffield/[email protected]/pkg/utils/utils.go:117 (0x82837c)
SafeWithError: err := f()
/home/federico/go/pkg/mod/github.com/jesseduffield/[email protected]/pkg/gui/gui.go:710 (0xb3464e)
(*Gui).RunAndHandleError: return utils.SafeWithError(func() error {
/home/federico/go/pkg/mod/github.com/jesseduffield/[email protected]/pkg/app/app.go:266 (0xb62396)
Run: err := app.Gui.RunAndHandleError(startArgs)
/home/federico/go/pkg/mod/github.com/jesseduffield/[email protected]/pkg/app/app.go:49 (0xb62346)
Run: err = app.Run(startArgs)
/home/federico/go/pkg/mod/github.com/jesseduffield/[email protected]/pkg/app/entry_point.go:150 (0xb64565)
Start: Run(appConfig, common, appTypes.NewStartArgs(cliArgs.FilterPath, parsedGitArg, integrationTest))
/home/federico/go/pkg/mod/github.com/jesseduffield/[email protected]/main.go:23 (0xb65d98)
main: app.Start(ldFlagsBuildInfo, nil)
/home/federico/.go/go1.22.1/src/runtime/internal/atomic/types.go:194 (0x44035d)
(*Uint32).Load: return Load(&u.value)
/home/federico/.go/go1.22.1/src/runtime/asm_amd64.s:1695 (0x473501)
goexit: BYTE $0x90 // NOP
Expected behavior Clean exit with a clear error message (like "Error: must be run inside the main folder of a git repository")
Version info:
$ lazygit --version
commit=, build date=, build source=unknown, version=unversioned, os=linux, arch=amd64, git version=2.34.1
$ git --version
git version 2.34.1
Would also like to add that this occurs in the subdirectory of a bare work tree root. For example, if I have a directory that contains a .bare directory with a .git file that points git to it (let's call this directory the "work tree root")...
[2024-08-15 09:16:19]» ls -al
total 64
drwxrwxr-x 15 chb chb 4096 Aug 15 09:12 .
drwxrwxr-x 67 chb chb 4096 Aug 9 18:30 ..
drwxrwxr-x 10 chb chb 4096 Aug 15 09:09 .bare
drwxrwxr-x 8 chb chb 4096 Jun 13 15:17 docs-and-diagrams
-rw-rw-r-- 1 chb chb 16 May 9 2023 .git
drwxrwxr-x 8 chb chb 4096 Jun 10 08:45 main
drwxrwxr-x 3 chb adm 4096 Apr 17 14:43 RWEB-8332-redis-logging
and then I change to what was once a worktree directory (in the eg above, RWEB-8332-redis-logging)...
[2024-08-15 09:16:20]» cd RWEB-8332-redis-logging
[2024-08-15 09:19:08]» ls -al
total 12
drwxrwxr-x 3 chb adm 4096 Apr 17 14:43 .
drwxrwxr-x 15 chb chb 4096 Aug 15 09:12 ..
drwxrwxr-x 2 chb adm 4096 Apr 17 14:43 .idea
The directory is now empty of git artifacts. If I run lazygit inside of this directory, I get the following:
[2024-08-15 09:12:36]» lg │
2024/08/15 09:12:37 An error occurred! Please create an issue at: https://github.com/jesseduffield/lazygit/issues │
│
*errors.errorString 'git -C /home/chb/code/work/pbs/terraform-pbsorg/RWEB-8332-redis-logging rev-parse --path-format=absolute --show-toplevel --absolute-git-di│
r --git-common-dir --is-bare-repository --show-superproject-working-tree' failed: fatal: this operation must be run in a work tree │
│
/home/runner/work/lazygit/lazygit/pkg/commands/git_commands/repo_paths.go:147 (0x9bd5f8) │
/home/runner/work/lazygit/lazygit/pkg/commands/git_commands/repo_paths.go:91 (0x9bcdc8) │
/home/runner/work/lazygit/lazygit/pkg/commands/git_commands/repo_paths.go:83 (0x9bcc8c) │
/home/runner/work/lazygit/lazygit/pkg/app/app.go:123 (0xbcaa79) │
/home/runner/work/lazygit/lazygit/pkg/app/app.go:45 (0xbca318) │
/home/runner/work/lazygit/lazygit/pkg/app/entry_point.go:161 (0xbcc597) │
/home/runner/work/lazygit/lazygit/main.go:23 (0xbcdef8) │
/opt/hostedtoolcache/go/1.22.5/x64/src/runtime/internal/atomic/types.go:194 (0x43ff5d) │
/opt/hostedtoolcache/go/1.22.5/x64/src/runtime/asm_amd64.s:1695 (0x477e81) │
[2024-08-15 09:19:10]» lazygit --version
commit=71ad3fac63a3ef3326021837b49e9497d332818b, build date=2024-07-13T10:24:19Z, build source=binaryRelease, version=0.43.1, os=linux, arch=amd64, git version
=2.46.0