@@ -11,20 +11,25 @@ import (
1111)
1212
1313const (
14- // hookBaseDir represent default hook directory
15- hookBaseDir = ".commitlint/hooks"
14+ defaultHooksPath = ".commitlint/hooks"
1615)
1716
18- var errHooksExist = errors .New ("hooks already exists" )
19- var errConfigExist = errors .New ("config file already exists" )
17+ var (
18+ errHooksExist = errors .New ("hooks already exists" )
19+ errConfigExist = errors .New ("config file already exists" )
20+ )
2021
2122// hookCreate is the callback function for create hook command
22- func hookCreate (isReplace bool ) error {
23- return createHooks (isReplace )
23+ func hookCreate (hooksPath string , isReplace bool ) error {
24+ if hooksPath == "" {
25+ hooksPath = filepath .Join ("." , defaultHooksPath )
26+ }
27+ hooksPath = filepath .Clean (hooksPath )
28+ return createHooks (hooksPath , isReplace )
2429}
2530
26- func initHooks (confPath string , isGlobal , isReplace bool ) (string , error ) {
27- hookDir , err := getHookDir (hookBaseDir , isGlobal )
31+ func initHooks (confPath , hookFlag string , isGlobal , isReplace bool ) (string , error ) {
32+ hookDir , err := getHookDir (hookFlag , isGlobal )
2833 if err != nil {
2934 return "" , err
3035 }
@@ -36,7 +41,7 @@ func initHooks(confPath string, isGlobal, isReplace bool) (string, error) {
3641 return hookDir , nil
3742}
3843
39- func createHooks (isReplace bool ) error {
44+ func createHooks (hookBaseDir string , isReplace bool ) error {
4045 return writeHooks (hookBaseDir , isReplace )
4146}
4247
@@ -57,8 +62,12 @@ func writeHooks(hookDir string, isReplace bool) error {
5762 return hook .WriteHooks (hookDir )
5863}
5964
60- func getHookDir (baseDir string , isGlobal bool ) (string , error ) {
61- baseDir = filepath .Clean (baseDir )
65+ func getHookDir (hookFlag string , isGlobal bool ) (string , error ) {
66+ if hookFlag != "" {
67+ return filepath .Abs (hookFlag )
68+ }
69+
70+ hookFlag = defaultHooksPath
6271
6372 if isGlobal {
6473 // get user home dir
@@ -68,15 +77,15 @@ func getHookDir(baseDir string, isGlobal bool) (string, error) {
6877 }
6978
7079 // create hooks dir
71- hookDir := filepath .Join (homeDir , baseDir )
80+ hookDir := filepath .Join (homeDir , hookFlag )
7281 return hookDir , nil
7382 }
7483
7584 gitDir , err := getRepoRootDir ()
7685 if err != nil {
7786 return "" , err
7887 }
79- return filepath .Join (gitDir , baseDir ), nil
88+ return filepath .Join (gitDir , hookFlag ), nil
8089}
8190
8291func getRepoRootDir () (string , error ) {
0 commit comments