1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-31 14:24:25 +03:00

when panicking due to malformed gitconfig, show a more useful error

This commit is contained in:
Jesse Duffield
2018-08-28 18:01:53 +10:00
parent db1d5328f2
commit 320ccdb22a
3 changed files with 27 additions and 11 deletions

View File

@ -30,11 +30,6 @@ type App struct {
func newProductionLogger(config config.AppConfigurer) *logrus.Logger { func newProductionLogger(config config.AppConfigurer) *logrus.Logger {
log := logrus.New() log := logrus.New()
log.Out = ioutil.Discard log.Out = ioutil.Discard
if config.GetUserConfig().GetString("reporting") == "on" {
// this isn't really a secret token: it only has permission to push new rollbar items
hook := rollrus.NewHook("23432119147a4367abf7c0de2aa99a2d", "production")
log.Hooks.Add(hook)
}
return log return log
} }
@ -50,11 +45,18 @@ func newDevelopmentLogger() *logrus.Logger {
func newLogger(config config.AppConfigurer) *logrus.Entry { func newLogger(config config.AppConfigurer) *logrus.Entry {
var log *logrus.Logger var log *logrus.Logger
environment := "production"
if config.GetDebug() { if config.GetDebug() {
environment = "development"
log = newDevelopmentLogger() log = newDevelopmentLogger()
} else { } else {
log = newProductionLogger(config) log = newProductionLogger(config)
} }
if config.GetUserConfig().GetString("reporting") == "on" {
// this isn't really a secret token: it only has permission to push new rollbar items
hook := rollrus.NewHook("23432119147a4367abf7c0de2aa99a2d", environment)
log.Hooks.Add(hook)
}
return log.WithFields(logrus.Fields{ return log.WithFields(logrus.Fields{
"debug": config.GetDebug(), "debug": config.GetDebug(),
"version": config.GetVersion(), "version": config.GetVersion(),
@ -75,7 +77,7 @@ func NewApp(config config.AppConfigurer) (*App, error) {
app.Tr = i18n.NewLocalizer(app.Log) app.Tr = i18n.NewLocalizer(app.Log)
app.GitCommand, err = commands.NewGitCommand(app.Log, app.OSCommand) app.GitCommand, err = commands.NewGitCommand(app.Log, app.OSCommand, app.Tr)
if err != nil { if err != nil {
return app, err return app, err
} }

View File

@ -8,6 +8,7 @@ import (
"strings" "strings"
"github.com/jesseduffield/gocui" "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
gitconfig "github.com/tcnksm/go-gitconfig" gitconfig "github.com/tcnksm/go-gitconfig"
@ -20,13 +21,15 @@ type GitCommand struct {
OSCommand *OSCommand OSCommand *OSCommand
Worktree *gogit.Worktree Worktree *gogit.Worktree
Repo *gogit.Repository Repo *gogit.Repository
Tr *i18n.Localizer
} }
// NewGitCommand it runs git commands // NewGitCommand it runs git commands
func NewGitCommand(log *logrus.Entry, osCommand *OSCommand) (*GitCommand, error) { func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer) (*GitCommand, error) {
gitCommand := &GitCommand{ gitCommand := &GitCommand{
Log: log, Log: log,
OSCommand: osCommand, OSCommand: osCommand,
Tr: tr,
} }
return gitCommand, nil return gitCommand, nil
} }
@ -35,7 +38,10 @@ func NewGitCommand(log *logrus.Entry, osCommand *OSCommand) (*GitCommand, error)
func (c *GitCommand) SetupGit() { func (c *GitCommand) SetupGit() {
c.verifyInGitRepo() c.verifyInGitRepo()
c.navigateToRepoRootDirectory() c.navigateToRepoRootDirectory()
c.setupWorktree() if err := c.setupWorktree(); err != nil {
c.Log.Error(err)
panic(err)
}
} }
// GetStashEntries stash entryies // GetStashEntries stash entryies
@ -159,18 +165,23 @@ func (c *GitCommand) navigateToRepoRootDirectory() {
} }
} }
func (c *GitCommand) setupWorktree() { func (c *GitCommand) setupWorktree() error {
r, err := gogit.PlainOpen(".") r, err := gogit.PlainOpen(".")
if err != nil { if err != nil {
panic(err) if strings.Contains(err.Error(), `unquoted '\' must be followed by new line`) {
errorMessage := c.Tr.SLocalize("GitconfigParseErr")
return errors.New(errorMessage)
}
return err
} }
c.Repo = r c.Repo = r
w, err := r.Worktree() w, err := r.Worktree()
if err != nil { if err != nil {
panic(err) return err
} }
c.Worktree = w c.Worktree = w
return nil
} }
// ResetHard does the equivalent of `git reset --hard HEAD` // ResetHard does the equivalent of `git reset --hard HEAD`

View File

@ -336,6 +336,9 @@ func addEnglish(i18nObject *i18n.Bundle) error {
}, &i18n.Message{ }, &i18n.Message{
ID: "AnonymousReportingPrompt", ID: "AnonymousReportingPrompt",
Other: "Would you like to enable anonymous reporting data to help improve lazygit? (enter/esc)", Other: "Would you like to enable anonymous reporting data to help improve lazygit? (enter/esc)",
}, &i18n.Message{
ID: "GitconfigParseErr",
Other: `Gogit failed to parse your gitconfig file due to the presence of unquoted '\' characters. Removing these should fix the issue.`,
}, },
) )
} }