From 41b54d742f6d5aacfb06174ea527ad33ddcaccec Mon Sep 17 00:00:00 2001 From: nullishamy Date: Fri, 29 Jul 2022 23:55:34 +0100 Subject: [PATCH 1/8] Check for bare repositories --- pkg/app/app.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pkg/app/app.go b/pkg/app/app.go index 9a7b1ffcc..265b5caaf 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -151,6 +151,17 @@ func isDirectoryAGitRepository(dir string) (bool, error) { return info != nil && info.IsDir(), err } +func isBareRepo(osCommand *oscommands.OSCommand) (bool, error) { + res, err := osCommand.Cmd.New("git rev-parse --is-bare-repository").DontLog().RunWithOutput() + + if err != nil { + return false, err + } + + // The command returns output with a newline, so we need to strip + return strconv.ParseBool(strings.TrimSpace(res)) +} + func (app *App) setupRepo() (bool, error) { if err := app.validateGitVersion(); err != nil { return false, err @@ -167,6 +178,7 @@ func (app *App) setupRepo() (bool, error) { if err != nil { return false, err } + if isRepo, err := isDirectoryAGitRepository(cwd); isRepo { return false, err } @@ -210,6 +222,17 @@ func (app *App) setupRepo() (bool, error) { } } + // Run this afterward so that the previous repo creation steps can run without this interfering + if isBare, err := isBareRepo(app.OSCommand); isBare { + if err != nil { + return false, err + } + + if isBare { + log.Fatalln("bare repositories are not supported by lazygit, please make this a working repository.") + } + } + return false, nil } From 2866827ca8cb94bf979221429f7993b4bfbbdb50 Mon Sep 17 00:00:00 2001 From: nullishamy Date: Mon, 1 Aug 2022 17:05:16 +0100 Subject: [PATCH 2/8] Apply suggestions from code review --- pkg/app/app.go | 17 ++++++++++++++++- pkg/i18n/english.go | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pkg/app/app.go b/pkg/app/app.go index 8f2ac1ea0..8f05679cc 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -229,7 +229,22 @@ func (app *App) setupRepo() (bool, error) { } if isBare { - log.Fatalln("bare repositories are not supported by lazygit, please make this a working repository.") + log.Println(app.Tr.BareRepo) + response, _ := bufio.NewReader(os.Stdin).ReadString('\n') + shouldOpenRecent := strings.Trim(response, " \r\n") == "y" + + if shouldOpenRecent { + for _, repoDir := range app.Config.GetAppState().RecentRepos { + if isRepo, _ := isDirectoryAGitRepository(repoDir); isRepo { + if err := os.Chdir(repoDir); err == nil { + return true, nil + } + } + } + + fmt.Println(app.Tr.NoRecentRepositories) + os.Exit(1) + } } } diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index b57c1aad8..83de83f7a 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -259,6 +259,7 @@ type TranslationSet struct { DiscardFileChangesPrompt string DisabledForGPG string CreateRepo string + BareRepo string InitialBranch string NoRecentRepositories string AutoStashTitle string @@ -896,6 +897,7 @@ func EnglishTranslationSet() TranslationSet { DiscardFileChangesPrompt: "Are you sure you want to discard this commit's changes to this file? If this file was created in this commit, it will be deleted", DisabledForGPG: "Feature not available for users using GPG", CreateRepo: "Not in a git repository. Create a new git repository? (y/n): ", + BareRepo: "You've attempted to open Lazygit in a bare repo but Lazygit does not yet support bare repos. Open most recent repo? (y/n)" , InitialBranch: "Branch name? (leave empty for git's default): ", NoRecentRepositories: "Must open lazygit in a git repository. No valid recent repositories. Exiting.", AutoStashTitle: "Autostash?", From b9b2f58bc85edc88c6673e26e8cd88190e2fb8cf Mon Sep 17 00:00:00 2001 From: nullishamy Date: Mon, 1 Aug 2022 17:41:20 +0100 Subject: [PATCH 3/8] Format, bug fixes --- pkg/app/app.go | 4 +++- pkg/i18n/english.go | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/app/app.go b/pkg/app/app.go index 8f05679cc..46d1831b0 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -229,7 +229,7 @@ func (app *App) setupRepo() (bool, error) { } if isBare { - log.Println(app.Tr.BareRepo) + fmt.Print(app.Tr.BareRepo) response, _ := bufio.NewReader(os.Stdin).ReadString('\n') shouldOpenRecent := strings.Trim(response, " \r\n") == "y" @@ -245,6 +245,8 @@ func (app *App) setupRepo() (bool, error) { fmt.Println(app.Tr.NoRecentRepositories) os.Exit(1) } + + os.Exit(0) } } diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 83de83f7a..aafd89de7 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -259,7 +259,7 @@ type TranslationSet struct { DiscardFileChangesPrompt string DisabledForGPG string CreateRepo string - BareRepo string + BareRepo string InitialBranch string NoRecentRepositories string AutoStashTitle string @@ -897,7 +897,7 @@ func EnglishTranslationSet() TranslationSet { DiscardFileChangesPrompt: "Are you sure you want to discard this commit's changes to this file? If this file was created in this commit, it will be deleted", DisabledForGPG: "Feature not available for users using GPG", CreateRepo: "Not in a git repository. Create a new git repository? (y/n): ", - BareRepo: "You've attempted to open Lazygit in a bare repo but Lazygit does not yet support bare repos. Open most recent repo? (y/n)" , + BareRepo: "You've attempted to open Lazygit in a bare repo but Lazygit does not yet support bare repos. Open most recent repo? (y/n) ", InitialBranch: "Branch name? (leave empty for git's default): ", NoRecentRepositories: "Must open lazygit in a git repository. No valid recent repositories. Exiting.", AutoStashTitle: "Autostash?", From 69718fb5577d8a6a542c76ee69211c74cd14431e Mon Sep 17 00:00:00 2001 From: nullishamy Date: Mon, 1 Aug 2022 19:39:39 +0100 Subject: [PATCH 4/8] Factor out redundant statement --- pkg/app/app.go | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/pkg/app/app.go b/pkg/app/app.go index 46d1831b0..bf9b7b73e 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -228,26 +228,25 @@ func (app *App) setupRepo() (bool, error) { return false, err } - if isBare { - fmt.Print(app.Tr.BareRepo) - response, _ := bufio.NewReader(os.Stdin).ReadString('\n') - shouldOpenRecent := strings.Trim(response, " \r\n") == "y" + fmt.Print(app.Tr.BareRepo) - if shouldOpenRecent { - for _, repoDir := range app.Config.GetAppState().RecentRepos { - if isRepo, _ := isDirectoryAGitRepository(repoDir); isRepo { - if err := os.Chdir(repoDir); err == nil { - return true, nil - } + response, _ := bufio.NewReader(os.Stdin).ReadString('\n') + shouldOpenRecent := strings.Trim(response, " \r\n") == "y" + + if shouldOpenRecent { + for _, repoDir := range app.Config.GetAppState().RecentRepos { + if isRepo, _ := isDirectoryAGitRepository(repoDir); isRepo { + if err := os.Chdir(repoDir); err == nil { + return true, nil } } - - fmt.Println(app.Tr.NoRecentRepositories) - os.Exit(1) } - os.Exit(0) + fmt.Println(app.Tr.NoRecentRepositories) + os.Exit(1) } + + os.Exit(0) } return false, nil From a658cd4076922fcc5746dd01ad3091477ee4e52a Mon Sep 17 00:00:00 2001 From: nullishamy Date: Mon, 1 Aug 2022 20:05:35 +0100 Subject: [PATCH 5/8] Factor out opening of recent repos --- pkg/app/app.go | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/pkg/app/app.go b/pkg/app/app.go index bf9b7b73e..019ac80d6 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -162,6 +162,18 @@ func isBareRepo(osCommand *oscommands.OSCommand) (bool, error) { return strconv.ParseBool(strings.TrimSpace(res)) } +func openRecentRepo(app *App) bool { + for _, repoDir := range app.Config.GetAppState().RecentRepos { + if isRepo, _ := isDirectoryAGitRepository(repoDir); isRepo { + if err := os.Chdir(repoDir); err == nil { + return true + } + } + } + + return false +} + func (app *App) setupRepo() (bool, error) { if err := app.validateGitVersion(); err != nil { return false, err @@ -205,17 +217,13 @@ func (app *App) setupRepo() (bool, error) { } if !shouldInitRepo { - // check if we have a recent repo we can open - for _, repoDir := range app.Config.GetAppState().RecentRepos { - if isRepo, _ := isDirectoryAGitRepository(repoDir); isRepo { - if err := os.Chdir(repoDir); err == nil { - return true, nil - } - } + // Attempt to open a recent repo, exit if no repo could be opened + if didOpenRepo := openRecentRepo(app); !didOpenRepo { + fmt.Println(app.Tr.NoRecentRepositories) + os.Exit(1) } - fmt.Println(app.Tr.NoRecentRepositories) - os.Exit(1) + return true, nil } if err := app.OSCommand.Cmd.New("git init " + initialBranch).Run(); err != nil { return false, err @@ -234,19 +242,16 @@ func (app *App) setupRepo() (bool, error) { shouldOpenRecent := strings.Trim(response, " \r\n") == "y" if shouldOpenRecent { - for _, repoDir := range app.Config.GetAppState().RecentRepos { - if isRepo, _ := isDirectoryAGitRepository(repoDir); isRepo { - if err := os.Chdir(repoDir); err == nil { - return true, nil - } - } + if didOpenRepo := openRecentRepo(app); !didOpenRepo { + fmt.Println(app.Tr.NoRecentRepositories) + os.Exit(1) } - - fmt.Println(app.Tr.NoRecentRepositories) - os.Exit(1) + + // We managed to open a recent repo, continue as usual + return true, nil + } else { + os.Exit(0) } - - os.Exit(0) } return false, nil From 0b4f9f8c76f15efbd6103c1e806f4ebd4bd4ae26 Mon Sep 17 00:00:00 2001 From: nullishamy Date: Mon, 1 Aug 2022 21:54:54 +0100 Subject: [PATCH 6/8] Refactor branching logic --- pkg/app/app.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/pkg/app/app.go b/pkg/app/app.go index 019ac80d6..c5fe30d2e 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -239,19 +239,17 @@ func (app *App) setupRepo() (bool, error) { fmt.Print(app.Tr.BareRepo) response, _ := bufio.NewReader(os.Stdin).ReadString('\n') - shouldOpenRecent := strings.Trim(response, " \r\n") == "y" - - if shouldOpenRecent { - if didOpenRepo := openRecentRepo(app); !didOpenRepo { - fmt.Println(app.Tr.NoRecentRepositories) - os.Exit(1) - } - // We managed to open a recent repo, continue as usual - return true, nil - } else { + if shouldOpenRecent := strings.Trim(response, " \r\n") == "y"; !shouldOpenRecent { os.Exit(0) } + + if didOpenRepo := openRecentRepo(app); didOpenRepo { + return true, nil + } + + fmt.Println(app.Tr.NoRecentRepositories) + os.Exit(1) } return false, nil From 154bd975a6361cd84e870d8128a2352505353aac Mon Sep 17 00:00:00 2001 From: nullishamy Date: Mon, 15 Aug 2022 13:59:34 +0100 Subject: [PATCH 7/8] Apply refactoring suggestions --- pkg/app/app.go | 13 ++----------- pkg/commands/git_commands/status.go | 24 +++++++++++++++++------- pkg/gui/recent_repos_panel.go | 7 ++++++- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/pkg/app/app.go b/pkg/app/app.go index c5fe30d2e..d4be7a8e6 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -15,6 +15,7 @@ import ( "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/commands" + "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/git_config" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/common" @@ -151,16 +152,6 @@ func isDirectoryAGitRepository(dir string) (bool, error) { return info != nil, err } -func isBareRepo(osCommand *oscommands.OSCommand) (bool, error) { - res, err := osCommand.Cmd.New("git rev-parse --is-bare-repository").DontLog().RunWithOutput() - - if err != nil { - return false, err - } - - // The command returns output with a newline, so we need to strip - return strconv.ParseBool(strings.TrimSpace(res)) -} func openRecentRepo(app *App) bool { for _, repoDir := range app.Config.GetAppState().RecentRepos { @@ -231,7 +222,7 @@ func (app *App) setupRepo() (bool, error) { } // Run this afterward so that the previous repo creation steps can run without this interfering - if isBare, err := isBareRepo(app.OSCommand); isBare { + if isBare, err := git_commands.IsBareRepo(app.OSCommand); isBare { if err != nil { return false, err } diff --git a/pkg/commands/git_commands/status.go b/pkg/commands/git_commands/status.go index 50b1fab57..d660dd8b0 100644 --- a/pkg/commands/git_commands/status.go +++ b/pkg/commands/git_commands/status.go @@ -2,8 +2,10 @@ package git_commands import ( "path/filepath" + "strconv" + "strings" - gogit "github.com/jesseduffield/go-git/v5" + "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/commands/types/enums" ) @@ -49,13 +51,21 @@ func (self *StatusCommands) WorkingTreeState() enums.RebaseMode { return enums.REBASE_MODE_NONE } +func (self *StatusCommands) IsBareRepo() (bool, error) { + return IsBareRepo(self.os) +} + +func IsBareRepo(osCommand *oscommands.OSCommand) (bool, error) { + res, err := osCommand.Cmd.New("git rev-parse --is-bare-repository").DontLog().RunWithOutput() + if err != nil { + return false, err + } + + // The command returns output with a newline, so we need to strip + return strconv.ParseBool(strings.TrimSpace(res)) +} + // IsInMergeState states whether we are still mid-merge func (self *StatusCommands) IsInMergeState() (bool, error) { return self.os.FileExists(filepath.Join(self.dotGitDir, "MERGE_HEAD")) } - -func (self *StatusCommands) IsBareRepo() bool { - // note: could use `git rev-parse --is-bare-repository` if we wanna drop go-git - _, err := self.repo.Worktree() - return err == gogit.ErrIsBareRepository -} diff --git a/pkg/gui/recent_repos_panel.go b/pkg/gui/recent_repos_panel.go index 73d6e54c5..0225f591a 100644 --- a/pkg/gui/recent_repos_panel.go +++ b/pkg/gui/recent_repos_panel.go @@ -158,7 +158,12 @@ func (gui *Gui) dispatchSwitchToRepo(path string, reuse bool) error { // updateRecentRepoList registers the fact that we opened lazygit in this repo, // so that we can open the same repo via the 'recent repos' menu func (gui *Gui) updateRecentRepoList() error { - if gui.git.Status.IsBareRepo() { + isBareRepo, err := gui.git.Status.IsBareRepo() + if err != nil { + return err + } + + if isBareRepo { // we could totally do this but it would require storing both the git-dir and the // worktree in our recent repos list, which is a change that would need to be // backwards compatible From 956372cf8a126e62ae64d515368d96c4bb510138 Mon Sep 17 00:00:00 2001 From: nullishamy Date: Thu, 18 Aug 2022 18:26:34 +0100 Subject: [PATCH 8/8] Run gofumpt --- pkg/app/app.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/app/app.go b/pkg/app/app.go index d8031b7d5..899325142 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -156,7 +156,6 @@ func isDirectoryAGitRepository(dir string) (bool, error) { return info != nil, err } - func openRecentRepo(app *App) bool { for _, repoDir := range app.Config.GetAppState().RecentRepos { if isRepo, _ := isDirectoryAGitRepository(repoDir); isRepo { @@ -218,7 +217,6 @@ func (app *App) setupRepo() (bool, error) { os.Exit(1) } - if shouldInitRepo { if err := app.OSCommand.Cmd.New("git init " + initialBranchArg).Run(); err != nil { return false, err @@ -248,7 +246,7 @@ func (app *App) setupRepo() (bool, error) { fmt.Print(app.Tr.BareRepo) response, _ := bufio.NewReader(os.Stdin).ReadString('\n') - + if shouldOpenRecent := strings.Trim(response, " \r\n") == "y"; !shouldOpenRecent { os.Exit(0) }