diff --git a/CHANGELOG.md b/CHANGELOG.md index 424f3136..376bc950 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,12 +2,13 @@ ## master (unreleased) --[PR](https://github.com/evilmartians/lefthook/pull/169) Create .git/hooks directory when it does not exist @DmitryTsepelev +- [PR](https://github.com/evilmartians/lefthook/pull/171) Improve check for installed git @DmitryTsepelev +- [PR](https://github.com/evilmartians/lefthook/pull/169) Create .git/hooks directory when it does not exist @DmitryTsepelev # 0.7.3 (2021-04-23) --[PR](https://github.com/evilmartians/lefthook/pull/168) Package versions for all architectures (x86_64, ARM64, x86) into Ruby gem and NPM package @Envek --[PR](https://github.com/evilmartians/lefthook/pull/167) Fix golang 15+ build @skryukov +- [PR](https://github.com/evilmartians/lefthook/pull/168) Package versions for all architectures (x86_64, ARM64, x86) into Ruby gem and NPM package @Envek +- [PR](https://github.com/evilmartians/lefthook/pull/167) Fix golang 15+ build @skryukov # 0.7.2 (2020-02-02) diff --git a/cmd/root.go b/cmd/root.go index c5cea526..7c534d00 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/logrusorgru/aurora" + "github.com/spf13/afero" "github.com/mattn/go-isatty" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -43,6 +44,21 @@ var rootCmd = &cobra.Command{ Long: `After installation go to your project directory and execute the following command: lefthook install`, + PersistentPreRun: func(cmd *cobra.Command, args []string) { + if cmd.Use == "help [command]" || cmd.Use == "version" { + return + } + + var fs = afero.NewOsFs() + if gitInitialized, _ := afero.DirExists(fs, 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)) + }, } func Execute() { @@ -115,8 +131,7 @@ func setRootPath(path string) { // get absolute path to .git dir (project root) cmd := exec.Command("git", "rev-parse", "--show-toplevel") - outputBytes, err := cmd.CombinedOutput() - check(err) + outputBytes, _ := cmd.CombinedOutput() rootPath = strings.TrimSpace(string(outputBytes)) }