Skip to content

Commit 88d14d3

Browse files
jlanzarottaJeff Lanzarotta
andauthored
Added XDG Base Directory support (#103)
* feat(config.go): xdg base directory support Added support for XDG Base Directory. If the XDG_CONFIG_HOME environment variable is set, look in the XDG_CONFIG_HOME/commitizen directory for the .czrc configuration file. The order of operation is, look in the current repo directory, then user's home directory, and the XDG_CONFIG_HOME/commitizen directory. If no configuration file is found, look in ~/.config/commitizen. * Updated README.md to reflect previous changes. --------- Co-authored-by: Jeff Lanzarotta <[email protected]>
1 parent 4712cfa commit 88d14d3

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ $ make && ./_output/$(GOOS)/$(GOARCH)/bin/commitizen init
9898

9999
## Configuration
100100

101-
You can set configuration file that `.czrc` at repository root or home directory. The configuration file that located in repository root have a priority over the one in home directory. The format is the same as the following:
101+
You can set configuration file that `.czrc` at repository root, home directory, or the $XDG_CONFIG_HOME/commitizen directory. The configuration file that located in repository root have a priority over the one in home directory. The format is the same as the following:
102102

103103
```yaml
104104
name: default

internal/config/config.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,26 @@ func New() *Config {
3535

3636
func (c *Config) initialize() error {
3737
var fpath string
38+
// Check if the configuration file is local to the repo.
3839
if fsutil.IsExists(RCFilename) {
3940
fpath = RCFilename
4041
} else {
42+
// Check if the configuration file is in the user's home directory.
4143
home := sysutil.HomeDir()
4244
p := filepath.Join(home, RCFilename)
4345
if fsutil.IsExists(p) {
4446
fpath = p
47+
} else {
48+
// Check if the configuration file is in the configured
49+
// XDG_CONFIG_HOME directory.
50+
xdgConfigHome, found := os.LookupEnv("XDG_CONFIG_HOME")
51+
if !found {
52+
xdgConfigHome = sysutil.HomeDir()
53+
}
54+
p := filepath.Join(xdgConfigHome, "commitizen", RCFilename)
55+
if fsutil.IsExists(p) {
56+
fpath = p
57+
}
4558
}
4659
}
4760

0 commit comments

Comments
 (0)