Skip to content

Commit bca646d

Browse files
committed
fix: cfg.json created in code base dir
cause: while submitting, working dir is the code base dir, not the hooks dir. solve: add code to check if there's a hook dir, read and write the cfg in that dir if exists.
1 parent 97390ce commit bca646d

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

config.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import (
77
)
88

99
const (
10-
MERGE_PATTERN = `^Merge `
11-
HEADER_PATTERN = `^((fixup! |squash! )?(\w+)(?:\(([^\)\s]+)\))?: (.+))(?:\n|$)`
12-
CONFIG_FILE = "commit-msg.cfg.json"
10+
MERGE_PATTERN = `^Merge `
11+
HEADER_PATTERN = `^((fixup! |squash! )?(\w+)(?:\(([^\)\s]+)\))?: (.+))(?:\n|$)`
12+
CONFIG_FILE_NAME = "commit-msg.cfg.json"
13+
HOOK_DIR = "./.git/hooks/"
1314
)
1415

1516
type GlobalConfig struct {
@@ -66,8 +67,16 @@ func (state MsgState) Hint() string {
6667
return Lang.HintList[state]
6768
}
6869

69-
func loadConfig() *GlobalConfig {
70-
f, err := os.Open(CONFIG_FILE)
70+
func locateConfig() string {
71+
f, err := os.Stat(HOOK_DIR)
72+
if err != nil || !f.IsDir() {
73+
return CONFIG_FILE_NAME
74+
}
75+
return HOOK_DIR + CONFIG_FILE_NAME
76+
}
77+
78+
func loadConfig(path string) *GlobalConfig {
79+
f, err := os.Open(path)
7180
if err != nil {
7281
return nil
7382
}
@@ -80,9 +89,9 @@ func loadConfig() *GlobalConfig {
8089
return &cfg
8190
}
8291

83-
func initConfig() *GlobalConfig {
92+
func initConfig(path string) *GlobalConfig {
8493
cfg := &GlobalConfig{"en", false, 80}
85-
f, err := os.Create(CONFIG_FILE)
94+
f, err := os.Create(path)
8695
if err != nil {
8796
return cfg
8897
}
@@ -94,9 +103,10 @@ func initConfig() *GlobalConfig {
94103
}
95104

96105
func init() {
97-
Config = loadConfig()
106+
path := locateConfig()
107+
Config = loadConfig(path)
98108
if Config == nil {
99-
Config = initConfig()
109+
Config = initConfig(path)
100110
}
101111

102112
switch Config.Lang {

0 commit comments

Comments
 (0)