From 503c78d27943943f0bdf195fc0993bd619d6bd1a Mon Sep 17 00:00:00 2001 From: Dmitry Tsepelev Date: Mon, 26 Apr 2021 18:10:36 +0300 Subject: [PATCH] Create `.git/hooks` directory when it does not exist (#169) --- CHANGELOG.md | 7 ++++--- cmd/root.go | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) 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..bf989ce6 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.Name() == "help" || cmd.Name() == "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)) }