Skip to content

Commit 8a8226c

Browse files
committed
feat(config): support custom configuration
before: defaultConfig used in commit/config.go after: supports custome config with .git-czrc in git root dir or userHome path
1 parent bc6df9e commit 8a8226c

File tree

4 files changed

+75
-13
lines changed

4 files changed

+75
-13
lines changed

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @Author: robert zhang <robertzhangwenjie@gmail.com>
33
* @Date: 2022-08-07 11:20:22
4-
* @LastEditTime: 2022-08-22 10:22:03
4+
* @LastEditTime: 2022-08-23 16:01:05
55
* @LastEditors: robert zhang
66
* @Description:
77
*/

commit/config.go

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,72 @@
11
/*
22
* @Author: robert zhang <robertzhangwenjie@gmail.com>
33
* @Date: 2022-08-04 17:12:31
4-
* @LastEditTime: 2022-08-10 16:55:52
4+
* @LastEditTime: 2022-08-29 11:51:15
55
* @LastEditors: robert zhang
66
* @Description:
77
*/
88
package commit
99

10+
import (
11+
"encoding/json"
12+
"fmt"
13+
"os"
14+
"path/filepath"
15+
16+
"github.com/robertzhangwenjie/commitizen/git"
17+
)
18+
19+
var configName = ".git-czrc"
20+
21+
type messageConfig struct {
22+
Template string
23+
Items []*Form
24+
}
25+
26+
func loadConfig() (*messageConfig, error) {
27+
var msgConfig = new(messageConfig)
28+
29+
// 如果当前git仓库根目录下拥有configFile,则优先使用它作为配置文件
30+
// loadConfig
31+
gitRoot, _ := git.GetCurrentRepositoryRoot()
32+
path, config, err := getConfigIn(gitRoot)
33+
if err == nil {
34+
err := json.Unmarshal(config, &msgConfig)
35+
if err != nil {
36+
return msgConfig, fmt.Errorf("config %s is not valid: %v", path, err)
37+
}
38+
return msgConfig, nil
39+
}
40+
41+
// 如果git根目录下没有,家目录下有配置文件,则使用家目录下的配置文件
42+
homePath, err := os.UserHomeDir()
43+
if err == nil {
44+
path, config, err := getConfigIn(homePath)
45+
if err == nil {
46+
err := json.Unmarshal(config, &msgConfig)
47+
if err != nil {
48+
return msgConfig, fmt.Errorf("config %s is not valid: %v", path, err)
49+
}
50+
return msgConfig, nil
51+
}
52+
}
53+
54+
// 缺省配置
55+
err = json.Unmarshal([]byte(defaultConfig), &msgConfig)
56+
return msgConfig, err
57+
}
58+
59+
// getConfigIn return the config contenct if config exists
60+
func getConfigIn(path string) (string, []byte, error) {
61+
configPath := filepath.Join(path, configName)
62+
config, err := os.ReadFile(configPath)
63+
// 如果家目录下有配置文件,且git仓库根目录下没有,则使用家目录下的配置文件
64+
if err != nil {
65+
return configPath, nil, err
66+
}
67+
return configPath, config, nil
68+
}
69+
1070
var selectQuestionTemplate = `
1171
{{- define "option"}}
1272
{{- if eq .SelectedIndex .CurrentIndex }}{{color .Config.Icons.SelectFocus.Format }}{{ .Config.Icons.SelectFocus.Text }} {{else}}{{color "default"}} {{end}}

commit/form.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/*
22
* @Author: robert zhang <robertzhangwenjie@gmail.com>
33
* @Date: 2022-08-05 11:52:41
4-
* @LastEditTime: 2022-08-10 16:56:47
4+
* @LastEditTime: 2022-08-29 11:30:41
55
* @LastEditors: robert zhang
66
* @Description:
77
*/
88
package commit
99

1010
import (
1111
"bytes"
12-
"encoding/json"
12+
"fmt"
1313
"log"
1414
"strings"
1515
"text/template"
@@ -30,11 +30,6 @@ type SelectOption struct {
3030
Desc string
3131
}
3232

33-
type messageConfig struct {
34-
Template string
35-
Items []*Form
36-
}
37-
3833
// fill out the form
3934
func fillOutForm() ([]byte, error) {
4035
qs, tmpl, err := loadForm()
@@ -71,9 +66,9 @@ func fillOutForm() ([]byte, error) {
7166

7267
// loadForm load the from with messgaeConfig
7368
func loadForm() (qs []*survey.Question, template string, err error) {
74-
var msgConfig = new(messageConfig)
75-
if err = json.Unmarshal([]byte(defaultConfig), &msgConfig); err != nil {
76-
return nil, "", err
69+
msgConfig, err := loadConfig()
70+
if err != nil {
71+
return nil, "", fmt.Errorf("load config failed: %v", err)
7772
}
7873

7974
// customize template for showing multiline's answer in new line

git/util.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @Author: robert zhang <robertzhangwenjie@gmail.com>
33
* @Date: 2022-08-07 18:14:41
4-
* @LastEditTime: 2022-08-10 15:06:26
4+
* @LastEditTime: 2022-08-29 11:43:34
55
* @LastEditors: robert zhang
66
* @Description:
77
*/
@@ -30,6 +30,13 @@ func IsGitRepository(dir string) bool {
3030
return err == nil
3131
}
3232

33+
// GetRepositoryRoot get the git root path to which the dir belongs
34+
func GetCurrentRepositoryRoot() (path string, err error) {
35+
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
36+
output, err := cmd.Output()
37+
return strings.TrimSpace(string((output))), err
38+
}
39+
3340
// HasStagedFiles determine whether has changes to be committed
3441
func HasStagedFiles(dir string) bool {
3542
cmd := exec.Command("git", "diff", "--quiet", "--cached")

0 commit comments

Comments
 (0)