33from pathlib import Path
44
55from commitizen import defaults , git
6- from commitizen .exceptions import ConfigFileNotFound
6+ from commitizen .exceptions import ConfigFileNotFound , ConfigFileIsEmpty
77
88from .base_config import BaseConfig
99from .json_config import JsonConfig
1414def read_cfg (filepath : str | None = None ) -> BaseConfig :
1515 conf = BaseConfig ()
1616
17- git_project_root = git .find_git_project_root ()
18-
1917 if filepath is not None :
20- given_cfg_path = Path (filepath )
21-
22- if not given_cfg_path .exists ():
18+ if not Path (filepath ).exists ():
2319 raise ConfigFileNotFound ()
2420
25- with open (given_cfg_path , "rb" ) as f :
26- given_cfg_data : bytes = f .read ()
27-
28- given_cfg : TomlConfig | JsonConfig | YAMLConfig
29-
30- if "toml" in given_cfg_path .suffix :
31- given_cfg = TomlConfig (data = given_cfg_data , path = given_cfg_path )
32- elif "json" in given_cfg_path .suffix :
33- given_cfg = JsonConfig (data = given_cfg_data , path = given_cfg_path )
34- elif "yaml" in given_cfg_path .suffix :
35- given_cfg = YAMLConfig (data = given_cfg_data , path = given_cfg_path )
36-
37- return given_cfg
21+ cfg_paths = (path for path in (Path (filepath ),))
22+ else :
23+ git_project_root = git .find_git_project_root ()
24+ cfg_search_paths = [Path ("." )]
25+ if git_project_root :
26+ cfg_search_paths .append (git_project_root )
3827
39- cfg_search_paths = [Path ("." )]
40- if git_project_root :
41- cfg_search_paths .append (git_project_root )
28+ cfg_paths = (
29+ path / Path (filename )
30+ for path in cfg_search_paths
31+ for filename in defaults .config_files
32+ )
4233
43- cfg_paths = (
44- path / Path (filename )
45- for path in cfg_search_paths
46- for filename in defaults .config_files
47- )
4834 for filename in cfg_paths :
4935 if not filename .exists ():
5036 continue
@@ -61,7 +47,9 @@ def read_cfg(filepath: str | None = None) -> BaseConfig:
6147 elif "yaml" in filename .suffix :
6248 _conf = YAMLConfig (data = data , path = filename )
6349
64- if _conf .is_empty_config :
50+ if filepath is not None and _conf .is_empty_config :
51+ raise ConfigFileIsEmpty ()
52+ elif _conf .is_empty_config :
6553 continue
6654 else :
6755 conf = _conf
0 commit comments