diff --git a/cmd/root.go b/cmd/root.go index 1a62fd8..9eb9b1c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -6,7 +6,6 @@ import ( "log" "os" - "github.com/integralist/go-findroot/find" "github.com/lintingzhen/commitizen-go/commit" "github.com/lintingzhen/commitizen-go/git" homedir "github.com/mitchellh/go-homedir" @@ -59,14 +58,14 @@ func initConfig() { log.Printf("Get home dir failed, err=%v\n", err) os.Exit(1) } - stat, err := find.Repo() - if err != nil { - log.Printf("Get git repository root failed, err=%v\n", err) + workingTreeRoot, err := git.WorkingTreeRoot() + if err != nil || workingTreeRoot == "" { + log.Printf("current directory is not a git working tree\n") os.Exit(1) } - // Search config in home directory and repository root directory with name ".git-czrc" or ".git-czrc.json". - viper.AddConfigPath(stat.Path) + // Search config in repository working tree root directory and home directory with name ".git-czrc" or ".git-czrc.json". + viper.AddConfigPath(workingTreeRoot) viper.AddConfigPath(home) viper.SetConfigName(".git-czrc") viper.SetConfigType("json") diff --git a/git/git.go b/git/git.go index b21f6eb..1ecdf7c 100644 --- a/git/git.go +++ b/git/git.go @@ -52,6 +52,15 @@ func IsCurrentDirectoryGitRepo() (bool, error) { return true, nil } +// WorkingTreeRoot return path of the top-level directory of the working tree +func WorkingTreeRoot() (path string, err error) { + output, err := exec.Command("git", "rev-parse", "--show-toplevel").Output() + if err != nil { + return "", err + } + return strings.TrimSpace(string(output)), nil +} + func CommitMessage(message []byte, all bool) ([]byte, error) { // save the commit message to temp file var err error diff --git a/go.mod b/go.mod index 8ebf68f..948be5e 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.16 require ( github.com/AlecAivazis/survey/v2 v2.3.2 github.com/fsnotify/fsnotify v1.4.9 // indirect - github.com/integralist/go-findroot v0.0.0-20160518114804-ac90681525dc // indirect github.com/kr/pretty v0.2.1 // indirect github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/mapstructure v1.3.3 diff --git a/go.sum b/go.sum index 7119ab2..c67a1a7 100644 --- a/go.sum +++ b/go.sum @@ -102,8 +102,6 @@ github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174 h1:WlZsjVhE8Af9IcZDG github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174/go.mod h1:DqJ97dSdRW1W22yXSB90986pcOyQ7r45iio1KN2ez1A= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/integralist/go-findroot v0.0.0-20160518114804-ac90681525dc h1:4IZpk3M4m6ypx0IlRoEyEyY1gAdicWLMQ0NcG/gBnnA= -github.com/integralist/go-findroot v0.0.0-20160518114804-ac90681525dc/go.mod h1:UlaC6ndby46IJz9m/03cZPKKkR9ykeIVBBDE3UDBdJk= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=