diff --git a/pkg/app/app.go b/pkg/app/app.go index 9e0679755..899325142 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -16,6 +16,7 @@ import ( "github.com/jesseduffield/generics/slices" appTypes "github.com/jesseduffield/lazygit/pkg/app/types" "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" @@ -155,6 +156,18 @@ 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 { + 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 @@ -171,6 +184,7 @@ func (app *App) setupRepo() (bool, error) { if err != nil { return false, err } + if isRepo, err := isDirectoryAGitRepository(cwd); isRepo { return false, err } @@ -223,6 +237,28 @@ func (app *App) setupRepo() (bool, error) { os.Exit(1) } + // Run this afterward so that the previous repo creation steps can run without this interfering + if isBare, err := git_commands.IsBareRepo(app.OSCommand); isBare { + if err != nil { + return false, err + } + + fmt.Print(app.Tr.BareRepo) + + response, _ := bufio.NewReader(os.Stdin).ReadString('\n') + + 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 } 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 705461726..df7656841 100644 --- a/pkg/gui/recent_repos_panel.go +++ b/pkg/gui/recent_repos_panel.go @@ -159,7 +159,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 diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 6d3e0b111..608ad2f45 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 IncorrectNotARepository string @@ -898,6 +899,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.", IncorrectNotARepository: "The value of 'notARepository' is incorrect. It should be one of 'prompt', 'create', 'skip', or 'quit'.",