From 5611d9a3ef442bfa162ee604b94af3639db73a09 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Thu, 27 Aug 2020 22:19:03 +1000 Subject: [PATCH] gracefully fail due to git version less than 2.0 --- pkg/app/app.go | 36 ++++++++++++++++++++++++++++++++++++ pkg/i18n/english.go | 3 +++ 2 files changed, 39 insertions(+) diff --git a/pkg/app/app.go b/pkg/app/app.go index f2289ecd9..617e2dd0e 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -2,11 +2,13 @@ package app import ( "bufio" + "errors" "fmt" "io" "io/ioutil" "os" "path/filepath" + "strconv" "strings" "github.com/jesseduffield/lazygit/pkg/commands" @@ -129,7 +131,33 @@ func NewApp(config config.AppConfigurer, filterPath string) (*App, error) { return app, nil } +func (app *App) validateGitVersion() error { + output, err := app.OSCommand.RunCommandWithOutput("git --version") + // if we get an error anywhere here we'll show the same status + minVersionError := errors.New(app.Tr.SLocalize("minGitVersionError")) + if err != nil { + return minVersionError + } + // output should be something like: 'git version 2.23.0' + // first number in the string should be greater than 0 + split := strings.Split(output, " ") + gitVersion := split[len(split)-1] + majorVersion, err := strconv.Atoi(gitVersion[0:1]) + if err != nil { + return minVersionError + } + if majorVersion < 2 { + return minVersionError + } + + return nil +} + func (app *App) setupRepo() (bool, error) { + if err := app.validateGitVersion(); err != nil { + return false, err + } + // 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() @@ -216,6 +244,14 @@ func (app *App) Close() error { func (app *App) KnownError(err error) (string, bool) { errorMessage := err.Error() + knownErrorMessages := []string{app.Tr.SLocalize("minGitVersionError")} + + for _, message := range knownErrorMessages { + if errorMessage == message { + return message, true + } + } + mappings := []errorMapping{ { originalError: "fatal: not a git repository", diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 7f52790f8..d3e170d2d 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -1176,6 +1176,9 @@ func addEnglish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "viewCommits", Other: "view commits", + }, &i18n.Message{ + ID: "minGitVersionError", + Other: "Git version must be at least 2.0 (i.e. from 2014 onwards). Please upgrade your git version. Alternatively raise an issue at https://github.com/jesseduffield/lazygit/issues for lazygit to be more backwards compatible.", }, ) }