diff --git a/docs/Config.md b/docs/Config.md index eca5fc4b6..666e465ec 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -101,7 +101,7 @@ confirmOnQuit: false # determines whether hitting 'esc' will quit the application when there is nothing to cancel/close quitOnTopLevelReturn: false disableStartupPopups: false -notARepository: 'prompt' # one of: 'prompt' | 'create' | 'skip' +notARepository: 'prompt' # one of: 'prompt' | 'create' | 'skip' | 'quit' promptToReturnFromSubprocess: true # display confirmation when subprocess terminates keybinding: universal: @@ -531,3 +531,8 @@ notARepository: 'create' # to skip without creating a new repo notARepository: 'skip' ``` + +```yaml +# to exit immediately if run outside of the Git repository +notARepository: 'quit' +``` diff --git a/pkg/app/app.go b/pkg/app/app.go index 3a1c127de..9e0679755 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -175,43 +175,52 @@ func (app *App) setupRepo() (bool, error) { return false, err } - shouldInitRepo := true - notARepository := app.UserConfig.NotARepository - initialBranch := "" - if notARepository == "prompt" { + var shouldInitRepo bool + initialBranchArg := "" + switch app.UserConfig.NotARepository { + case "prompt": // Offer to initialize a new repository in current directory. fmt.Print(app.Tr.CreateRepo) response, _ := bufio.NewReader(os.Stdin).ReadString('\n') - if strings.Trim(response, " \r\n") != "y" { - shouldInitRepo = false - } else { + shouldInitRepo = (strings.Trim(response, " \r\n") == "y") + if shouldInitRepo { // Ask for the initial branch name fmt.Print(app.Tr.InitialBranch) response, _ := bufio.NewReader(os.Stdin).ReadString('\n') if trimmedResponse := strings.Trim(response, " \r\n"); len(trimmedResponse) > 0 { - initialBranch += "--initial-branch=" + trimmedResponse + initialBranchArg += "--initial-branch=" + app.OSCommand.Quote(trimmedResponse) } } - } else if notARepository == "skip" { + case "create": + shouldInitRepo = true + case "skip": shouldInitRepo = false - } - - if !shouldInitRepo { - // check if we have a recent repo we can open - for _, repoDir := range app.Config.GetAppState().RecentRepos { - if isRepo, _ := isDirectoryAGitRepository(repoDir); isRepo { - if err := os.Chdir(repoDir); err == nil { - return true, nil - } - } - } - - fmt.Println(app.Tr.NoRecentRepositories) + case "quit": + fmt.Fprintln(os.Stderr, app.Tr.NotARepository) + os.Exit(1) + default: + fmt.Fprintln(os.Stderr, app.Tr.IncorrectNotARepository) os.Exit(1) } - if err := app.OSCommand.Cmd.New("git init " + initialBranch).Run(); err != nil { - return false, err + + if shouldInitRepo { + if err := app.OSCommand.Cmd.New("git init " + initialBranchArg).Run(); err != nil { + return false, err + } + return false, nil } + + // check if we have a recent repo we can open + for _, repoDir := range app.Config.GetAppState().RecentRepos { + if isRepo, _ := isDirectoryAGitRepository(repoDir); isRepo { + if err := os.Chdir(repoDir); err == nil { + return true, nil + } + } + } + + fmt.Fprintln(os.Stderr, app.Tr.NoRecentRepositories) + os.Exit(1) } return false, nil diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index d316c482e..6d3e0b111 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -261,6 +261,7 @@ type TranslationSet struct { CreateRepo string InitialBranch string NoRecentRepositories string + IncorrectNotARepository string AutoStashTitle string AutoStashPrompt string StashPrefix string @@ -899,6 +900,7 @@ func EnglishTranslationSet() TranslationSet { CreateRepo: "Not in a git repository. Create a new git repository? (y/n): ", InitialBranch: "Branch name? (leave empty for git's default): ", NoRecentRepositories: "Must open lazygit in a git repository. No valid recent repositories. Exiting.", + IncorrectNotARepository: "The value of 'notARepository' is incorrect. It should be one of 'prompt', 'create', 'skip', or 'quit'.", AutoStashTitle: "Autostash?", AutoStashPrompt: "You must stash and pop your changes to bring them across. Do this automatically? (enter/esc)", StashPrefix: "Auto-stashing changes for ",