From 4f4bb40ea689925c24b2663c3b642393c7c62fe8 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 16 Aug 2020 22:49:37 +1000 Subject: support opening lazygit outside a git directory --- pkg/app/app.go | 30 ++++++++++++++++++++++-------- pkg/gui/gui.go | 7 ++++++- pkg/gui/layout.go | 7 +++++++ 3 files changed, 35 insertions(+), 9 deletions(-) (limited to 'pkg') diff --git a/pkg/app/app.go b/pkg/app/app.go index ef821aad6..f2289ecd9 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -113,7 +113,8 @@ func NewApp(config config.AppConfigurer, filterPath string) (*App, error) { return app, err } - if err := app.setupRepo(); err != nil { + showRecentRepos, err := app.setupRepo() + if err != nil { return app, err } @@ -121,36 +122,49 @@ func NewApp(config config.AppConfigurer, filterPath string) (*App, error) { if err != nil { return app, err } - app.Gui, err = gui.NewGui(app.Log, app.GitCommand, app.OSCommand, app.Tr, config, app.Updater, filterPath) + app.Gui, err = gui.NewGui(app.Log, app.GitCommand, app.OSCommand, app.Tr, config, app.Updater, filterPath, showRecentRepos) if err != nil { return app, err } return app, nil } -func (app *App) setupRepo() error { +func (app *App) setupRepo() (bool, error) { // if we are not in a git repo, we ask if we want to `git init` if err := app.OSCommand.RunCommand("git status"); err != nil { cwd, err := os.Getwd() if err != nil { - return err + return false, err } info, _ := os.Stat(filepath.Join(cwd, ".git")) if info != nil && info.IsDir() { - return err // Current directory appears to be a git repository. + return false, err // Current directory appears to be a git repository. } // Offer to initialize a new repository in current directory. fmt.Print(app.Tr.SLocalize("CreateRepo")) response, _ := bufio.NewReader(os.Stdin).ReadString('\n') if strings.Trim(response, " \n") != "y" { + // check if we have a recent repo we can open + recentRepos := app.Config.GetAppState().RecentRepos + if len(recentRepos) > 0 { + var err error + // try opening each repo in turn, in case any have been deleted + for _, repoDir := range recentRepos { + if err = os.Chdir(repoDir); err == nil { + return true, nil + } + } + return false, err + } + os.Exit(1) } if err := app.OSCommand.RunCommand("git init"); err != nil { - return err + return false, err } } - return nil + return false, nil } func (app *App) Run() error { @@ -204,7 +218,7 @@ func (app *App) KnownError(err error) (string, bool) { mappings := []errorMapping{ { - originalError: "fatal: not a git repository (or any of the parent directories): .git", + originalError: "fatal: not a git repository", newError: app.Tr.SLocalize("notARepository"), }, } diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index a4c99e1c0..16c829025 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -93,6 +93,10 @@ type Gui struct { fileWatcher *fileWatcher viewBufferManagerMap map[string]*tasks.ViewBufferManager stopChan chan struct{} + + // when lazygit is opened outside a git directory we want to open to the most + // recent repo with the recent repos popup showing + showRecentRepos bool } // for now the staging panel state, unlike the other panel states, is going to be @@ -278,7 +282,7 @@ func (gui *Gui) resetState() { // for now the split view will always be on // NewGui builds a new gui handler -func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *commands.OSCommand, tr *i18n.Localizer, config config.AppConfigurer, updater *updates.Updater, filterPath string) (*Gui, error) { +func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *commands.OSCommand, tr *i18n.Localizer, config config.AppConfigurer, updater *updates.Updater, filterPath string, showRecentRepos bool) (*Gui, error) { gui := &Gui{ Log: log, GitCommand: gitCommand, @@ -288,6 +292,7 @@ func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *comma Updater: updater, statusManager: &statusManager{}, viewBufferManagerMap: map[string]*tasks.ViewBufferManager{}, + showRecentRepos: showRecentRepos, } gui.resetState() diff --git a/pkg/gui/layout.go b/pkg/gui/layout.go index b09a6a400..a9720f249 100644 --- a/pkg/gui/layout.go +++ b/pkg/gui/layout.go @@ -402,6 +402,13 @@ func (gui *Gui) onInitialViewsCreation() error { gui.getBranchesView().Context = "local-branches" gui.getCommitsView().Context = "branch-commits" + if gui.showRecentRepos { + if err := gui.handleCreateRecentReposMenu(); err != nil { + return err + } + gui.showRecentRepos = false + } + return gui.loadNewRepo() } -- cgit v1.2.3