Skip to content

Commit

Permalink
Do not initialize git config on help and version commands (#209)
Browse files Browse the repository at this point in the history
Allow to run `lefthook version` and `lefthook help` commands outside of a git repository.
  • Loading branch information
pwinckles committed Jun 10, 2021
1 parent 679915d commit ad086ac
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const (
configExtensionPattern string = ".*"
defaultFilePermission os.FileMode = 0755
defaultDirPermission os.FileMode = 0666
gitInitMessage string = `This command must be executed within git repository.
Change working directory or initialize new repository with 'git init'.`
)

var (
Expand All @@ -49,14 +51,13 @@ lefthook install`,
return
}

initGitConfig()

if gitInitialized, _ := afero.Exists(appFs, filepath.Join(getRootPath(), ".git")); gitInitialized {
return
}

message := `This command must be executed within git repository.
Change working directory or initialize new repository with 'git init'.`

log.Fatal(au.Brown(message))
log.Fatal(au.Brown(gitInitMessage))
},
}

Expand Down Expand Up @@ -90,9 +91,6 @@ func initAurora() {
func initConfig() {
log.SetFlags(0)

setRootPath()
setGitHooksPath(getHooksPathFromGitConfig())

// store original config before merge
originConfig = viper.New()
originConfig.SetConfigName(configFileName)
Expand Down Expand Up @@ -123,6 +121,11 @@ func initConfig() {
viper.AutomaticEnv()
}

func initGitConfig() {
setRootPath()
setGitHooksPath(getHooksPathFromGitConfig())
}

func getRootPath() string {
return rootPath
}
Expand All @@ -131,7 +134,12 @@ func getRootPath() string {
func setRootPath() {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")

outputBytes, _ := cmd.CombinedOutput()
outputBytes, err := cmd.CombinedOutput()

if err != nil {
log.Fatal(au.Brown(gitInitMessage))
}

rootPath = strings.TrimSpace(string(outputBytes))
}

Expand Down

0 comments on commit ad086ac

Please sign in to comment.