1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-01 13:17:53 +02:00

472 - Don't panic if not in a repository

Display a friendly message and exit with an error if not
in a Git repository. Using the same approach used in this PR:
https://github.com/jesseduffield/lazydocker/pull/14/files
This commit is contained in:
Giorgio Previtera 2019-07-07 19:15:11 +01:00 committed by Jesse Duffield
parent 25c93c678d
commit 9fb9962ce7
5 changed files with 38 additions and 1 deletions

View File

@ -52,6 +52,9 @@ func main() {
} }
if err != nil { if err != nil {
if errorMessage, known := app.KnownError(err); known {
log.Fatal(errorMessage)
}
newErr := errors.Wrap(err, 0) newErr := errors.Wrap(err, 0)
stackTrace := newErr.ErrorStack() stackTrace := newErr.ErrorStack()
app.Log.Error(stackTrace) app.Log.Error(stackTrace)

View File

@ -33,6 +33,11 @@ type App struct {
ClientContext string ClientContext string
} }
type errorMapping struct {
originalError string
newError string
}
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
@ -158,7 +163,8 @@ func (app *App) Run() error {
os.Exit(0) os.Exit(0)
} }
return app.Gui.RunWithSubprocesses() err := app.Gui.RunWithSubprocesses()
return err
} }
// Rebase contains logic for when we've been run in demon mode, meaning we've // Rebase contains logic for when we've been run in demon mode, meaning we've
@ -192,3 +198,22 @@ func (app *App) Close() error {
} }
return nil return nil
} }
// KnownError takes an error and tells us whether it's an error that we know about where we can print a nicely formatted version of it rather than panicking with a stack trace
func (app *App) KnownError(err error) (string, bool) {
errorMessage := err.Error()
mappings := []errorMapping{
{
originalError: "fatal: not a git repository (or any of the parent directories): .git",
newError: app.Tr.SLocalize("notARepository"),
},
}
for _, mapping := range mappings {
if strings.Contains(errorMessage, mapping.originalError) {
return mapping.newError, true
}
}
return "", false
}

View File

@ -748,6 +748,9 @@ func addDutch(i18nObject *i18n.Bundle) error {
}, &i18n.Message{ }, &i18n.Message{
ID: "stashOptions", ID: "stashOptions",
Other: "Stash options", Other: "Stash options",
}, &i18n.Message{
ID: "notARepository",
Other: "Error: must be run inside a repository",
}, },
) )
} }

View File

@ -771,6 +771,9 @@ func addEnglish(i18nObject *i18n.Bundle) error {
}, &i18n.Message{ }, &i18n.Message{
ID: "stashOptions", ID: "stashOptions",
Other: "Stash options", Other: "Stash options",
}, &i18n.Message{
ID: "notARepository",
Other: "Error: must be run inside a repository",
}, },
) )
} }

View File

@ -731,6 +731,9 @@ func addPolish(i18nObject *i18n.Bundle) error {
}, &i18n.Message{ }, &i18n.Message{
ID: "stashOptions", ID: "stashOptions",
Other: "Stash options", Other: "Stash options",
}, &i18n.Message{
ID: "notARepository",
Other: "Error: must be run inside a repository",
}, },
) )
} }