Skip to content

Commit 8b371ad

Browse files
committed
feat(config): add notARepository: quit
1 parent 70a4602 commit 8b371ad

File tree

3 files changed

+39
-23
lines changed

3 files changed

+39
-23
lines changed

docs/Config.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ confirmOnQuit: false
101101
# determines whether hitting 'esc' will quit the application when there is nothing to cancel/close
102102
quitOnTopLevelReturn: false
103103
disableStartupPopups: false
104-
notARepository: 'prompt' # one of: 'prompt' | 'create' | 'skip'
104+
notARepository: 'prompt' # one of: 'prompt' | 'create' | 'skip' | 'quit'
105105
promptToReturnFromSubprocess: true # display confirmation when subprocess terminates
106106
keybinding:
107107
universal:
@@ -531,3 +531,8 @@ notARepository: 'create'
531531
# to skip without creating a new repo
532532
notARepository: 'skip'
533533
```
534+
535+
```yaml
536+
# to exit immediately if run outside of the Git repository
537+
notARepository: 'quit'
538+
```

pkg/app/app.go

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -171,43 +171,52 @@ func (app *App) setupRepo() (bool, error) {
171171
return false, err
172172
}
173173

174-
shouldInitRepo := true
175-
notARepository := app.UserConfig.NotARepository
176-
initialBranch := ""
177-
if notARepository == "prompt" {
174+
var shouldInitRepo bool
175+
initialBranchArg := ""
176+
switch app.UserConfig.NotARepository {
177+
case "prompt":
178178
// Offer to initialize a new repository in current directory.
179179
fmt.Print(app.Tr.CreateRepo)
180180
response, _ := bufio.NewReader(os.Stdin).ReadString('\n')
181-
if strings.Trim(response, " \r\n") != "y" {
182-
shouldInitRepo = false
183-
} else {
181+
shouldInitRepo = (strings.Trim(response, " \r\n") == "y")
182+
if shouldInitRepo {
184183
// Ask for the initial branch name
185184
fmt.Print(app.Tr.InitialBranch)
186185
response, _ := bufio.NewReader(os.Stdin).ReadString('\n')
187186
if trimmedResponse := strings.Trim(response, " \r\n"); len(trimmedResponse) > 0 {
188-
initialBranch += "--initial-branch=" + trimmedResponse
187+
initialBranchArg += "--initial-branch=" + app.OSCommand.Quote(trimmedResponse)
189188
}
190189
}
191-
} else if notARepository == "skip" {
190+
case "create":
191+
shouldInitRepo = true
192+
case "skip":
192193
shouldInitRepo = false
194+
case "quit":
195+
fmt.Fprintln(os.Stderr, app.Tr.NotARepository)
196+
os.Exit(1)
197+
default:
198+
fmt.Fprintln(os.Stderr, app.Tr.IncorrectNotARepository)
199+
os.Exit(1)
193200
}
194201

195-
if !shouldInitRepo {
196-
// check if we have a recent repo we can open
197-
for _, repoDir := range app.Config.GetAppState().RecentRepos {
198-
if isRepo, _ := isDirectoryAGitRepository(repoDir); isRepo {
199-
if err := os.Chdir(repoDir); err == nil {
200-
return true, nil
201-
}
202-
}
202+
if shouldInitRepo {
203+
if err := app.OSCommand.Cmd.New("git init " + initialBranchArg).Run(); err != nil {
204+
return false, err
203205
}
204-
205-
fmt.Println(app.Tr.NoRecentRepositories)
206-
os.Exit(1)
206+
return false, nil
207207
}
208-
if err := app.OSCommand.Cmd.New("git init " + initialBranch).Run(); err != nil {
209-
return false, err
208+
209+
// check if we have a recent repo we can open
210+
for _, repoDir := range app.Config.GetAppState().RecentRepos {
211+
if isRepo, _ := isDirectoryAGitRepository(repoDir); isRepo {
212+
if err := os.Chdir(repoDir); err == nil {
213+
return true, nil
214+
}
215+
}
210216
}
217+
218+
fmt.Fprintln(os.Stderr, app.Tr.NoRecentRepositories)
219+
os.Exit(1)
211220
}
212221

213222
return false, nil

pkg/i18n/english.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ type TranslationSet struct {
261261
CreateRepo string
262262
InitialBranch string
263263
NoRecentRepositories string
264+
IncorrectNotARepository string
264265
AutoStashTitle string
265266
AutoStashPrompt string
266267
StashPrefix string
@@ -900,6 +901,7 @@ func EnglishTranslationSet() TranslationSet {
900901
CreateRepo: "Not in a git repository. Create a new git repository? (y/n): ",
901902
InitialBranch: "Branch name? (leave empty for git's default): ",
902903
NoRecentRepositories: "Must open lazygit in a git repository. No valid recent repositories. Exiting.",
904+
IncorrectNotARepository: "The value of 'notARepository' is incorrect. It should be one of 'prompt', 'create', 'skip', or 'quit'.",
903905
AutoStashTitle: "Autostash?",
904906
AutoStashPrompt: "You must stash and pop your changes to bring them across. Do this automatically? (enter/esc)",
905907
StashPrefix: "Auto-stashing changes for ",

0 commit comments

Comments
 (0)