Skip to content

Commit 83e21a6

Browse files
feat: add --hookspath flag to init, hook create command
1 parent a74f2ae commit 83e21a6

File tree

4 files changed

+49
-20
lines changed

4 files changed

+49
-20
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,17 @@ commitlint init
6666
commitlint init --global
6767
```
6868

69+
- to customize hooks destination pass `--hookspath` with desired location
70+
71+
```bash
72+
commitlint init --hookspath /path/to/hooks
73+
commitlint init --global --hookspath /path/to/hooks
74+
```
75+
6976
### Manual
7077

7178
- run `commitlint hook create` to create `.commitlint/hooks` containing git hooks
79+
- pass `--hookspath` to customize the hooks output path
7280
- To enable in single repo
7381
- run `git config core.hooksPath /path/to/.commitlint/hooks`
7482
- To enable globally

internal/cmd/cli.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func newLintCmd() *cli.Command {
5757
func newInitCmd() *cli.Command {
5858
confFlag := newConfFlag()
5959
replaceFlag := newReplaceFlag()
60+
hooksFlag := newHooksPathFlag()
6061

6162
globalFlag := &cli.BoolFlag{
6263
Name: "global",
@@ -67,13 +68,14 @@ func newInitCmd() *cli.Command {
6768
return &cli.Command{
6869
Name: "init",
6970
Usage: "Setup commitlint for git repos",
70-
Flags: []cli.Flag{globalFlag, confFlag, replaceFlag},
71+
Flags: []cli.Flag{globalFlag, confFlag, replaceFlag, hooksFlag},
7172
Action: func(ctx *cli.Context) error {
7273
confPath := ctx.String("config")
7374
isGlobal := ctx.Bool("global")
7475
isReplace := ctx.Bool("replace")
76+
hooksPath := ctx.String("hookspath")
7577

76-
err := initLint(confPath, isGlobal, isReplace)
78+
err := initLint(confPath, hooksPath, isGlobal, isReplace)
7779
if err != nil {
7880
if isHookExists(err) {
7981
fmt.Println("commitlint init failed")
@@ -158,14 +160,16 @@ func newConfigCmd() *cli.Command {
158160

159161
func newHookCmd() *cli.Command {
160162
replaceFlag := newReplaceFlag()
163+
hooksFlag := newHooksPathFlag()
161164

162165
createCmd := &cli.Command{
163166
Name: "create",
164167
Usage: "Creates git hook files in current directory",
165-
Flags: []cli.Flag{replaceFlag},
168+
Flags: []cli.Flag{replaceFlag, hooksFlag},
166169
Action: func(ctx *cli.Context) error {
167170
isReplace := ctx.Bool("replace")
168-
err := hookCreate(isReplace)
171+
hooksPath := ctx.String("hookspath")
172+
err := hookCreate(hooksPath, isReplace)
169173
if err != nil {
170174
if isHookExists(err) {
171175
fmt.Println("create failed. hook files already exists")
@@ -205,6 +209,14 @@ func newConfFlag() *cli.StringFlag {
205209
}
206210
}
207211

212+
func newHooksPathFlag() *cli.StringFlag {
213+
return &cli.StringFlag{
214+
Name: "hookspath",
215+
Value: "",
216+
Usage: "Optional hookspath to install git hooks",
217+
}
218+
}
219+
208220
func newReplaceFlag() *cli.BoolFlag {
209221
return &cli.BoolFlag{
210222
Name: "replace",

internal/cmd/hook.go

+22-13
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,25 @@ import (
1111
)
1212

1313
const (
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

8291
func getRepoRootDir() (string, error) {

internal/cmd/init.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import (
66
)
77

88
// initLint is the callback function for init command
9-
func initLint(confPath string, isGlobal, isReplace bool) error {
10-
hookDir, err := initHooks(confPath, isGlobal, isReplace)
9+
func initLint(confPath, hooksPath string, isGlobal, isReplace bool) error {
10+
hookDir, err := initHooks(confPath, hooksPath, isGlobal, isReplace)
1111
if err != nil {
1212
return err
1313
}
1414
return setGitConf(hookDir, isGlobal)
1515
}
1616

1717
func setGitConf(hookDir string, isGlobal bool) error {
18-
var args = []string{"config"}
18+
args := []string{"config"}
1919
if isGlobal {
2020
args = append(args, "--global")
2121
}

0 commit comments

Comments
 (0)