1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-27 00:51:18 +02:00

support opening lazygit outside a git directory

This commit is contained in:
Jesse Duffield
2020-08-16 22:49:37 +10:00
parent db826b3c87
commit 4f4bb40ea6
3 changed files with 35 additions and 9 deletions

View File

@ -113,7 +113,8 @@ func NewApp(config config.AppConfigurer, filterPath string) (*App, error) {
return app, err return app, err
} }
if err := app.setupRepo(); err != nil { showRecentRepos, err := app.setupRepo()
if err != nil {
return app, err return app, err
} }
@ -121,36 +122,49 @@ func NewApp(config config.AppConfigurer, filterPath string) (*App, error) {
if err != nil { if err != nil {
return app, err 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 { if err != nil {
return app, err return app, err
} }
return app, nil 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 we are not in a git repo, we ask if we want to `git init`
if err := app.OSCommand.RunCommand("git status"); err != nil { if err := app.OSCommand.RunCommand("git status"); err != nil {
cwd, err := os.Getwd() cwd, err := os.Getwd()
if err != nil { if err != nil {
return err return false, err
} }
info, _ := os.Stat(filepath.Join(cwd, ".git")) info, _ := os.Stat(filepath.Join(cwd, ".git"))
if info != nil && info.IsDir() { 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. // Offer to initialize a new repository in current directory.
fmt.Print(app.Tr.SLocalize("CreateRepo")) fmt.Print(app.Tr.SLocalize("CreateRepo"))
response, _ := bufio.NewReader(os.Stdin).ReadString('\n') response, _ := bufio.NewReader(os.Stdin).ReadString('\n')
if strings.Trim(response, " \n") != "y" { 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) os.Exit(1)
} }
if err := app.OSCommand.RunCommand("git init"); err != nil { if err := app.OSCommand.RunCommand("git init"); err != nil {
return err return false, err
} }
} }
return nil return false, nil
} }
func (app *App) Run() error { func (app *App) Run() error {
@ -204,7 +218,7 @@ func (app *App) KnownError(err error) (string, bool) {
mappings := []errorMapping{ 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"), newError: app.Tr.SLocalize("notARepository"),
}, },
} }

View File

@ -93,6 +93,10 @@ type Gui struct {
fileWatcher *fileWatcher fileWatcher *fileWatcher
viewBufferManagerMap map[string]*tasks.ViewBufferManager viewBufferManagerMap map[string]*tasks.ViewBufferManager
stopChan chan struct{} 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 // 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 // for now the split view will always be on
// NewGui builds a new gui handler // 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{ gui := &Gui{
Log: log, Log: log,
GitCommand: gitCommand, GitCommand: gitCommand,
@ -288,6 +292,7 @@ func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *comma
Updater: updater, Updater: updater,
statusManager: &statusManager{}, statusManager: &statusManager{},
viewBufferManagerMap: map[string]*tasks.ViewBufferManager{}, viewBufferManagerMap: map[string]*tasks.ViewBufferManager{},
showRecentRepos: showRecentRepos,
} }
gui.resetState() gui.resetState()

View File

@ -402,6 +402,13 @@ func (gui *Gui) onInitialViewsCreation() error {
gui.getBranchesView().Context = "local-branches" gui.getBranchesView().Context = "local-branches"
gui.getCommitsView().Context = "branch-commits" gui.getCommitsView().Context = "branch-commits"
if gui.showRecentRepos {
if err := gui.handleCreateRecentReposMenu(); err != nil {
return err
}
gui.showRecentRepos = false
}
return gui.loadNewRepo() return gui.loadNewRepo()
} }