Skip to content

Commit bafa87f

Browse files
committed
refactor(config): remove _check_and_warn_multiple_configs function; refactor read_cfg function
1 parent 2e7dbf9 commit bafa87f

File tree

1 file changed

+11
-27
lines changed

1 file changed

+11
-27
lines changed

commitizen/config/__init__.py

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,22 @@ def _resolve_config_paths(filepath: str | None = None) -> Generator[Path, None,
3434
yield out_path
3535

3636

37-
def _check_and_warn_multiple_configs(filepath: str | None = None) -> None:
38-
"""Check if multiple config files exist and warn the user."""
39-
if filepath is not None:
40-
# If user explicitly specified a config file, no need to warn
41-
return
42-
43-
cfg_search_paths = [Path(".")]
44-
if (git_project_root := git.find_git_project_root()):
45-
cfg_search_paths.append(git_project_root)
37+
def read_cfg(filepath: str | None = None) -> BaseConfig:
38+
config_candidates = list(_resolve_config_paths(filepath))
4639

47-
for path in cfg_search_paths:
48-
# Find all existing config files (excluding pyproject.toml for clearer warning)
49-
existing_files = [
50-
filename
51-
for filename in defaults.CONFIG_FILES
52-
if filename != "pyproject.toml" and (path / filename).exists()
40+
# Check for multiple config files and warn the user
41+
if filepath is None:
42+
config_candidates_exclude_pyproject = [
43+
path for path in config_candidates if path.name != "pyproject.toml"
5344
]
54-
55-
# If more than one config file exists, warn the user
56-
if len(existing_files) > 1:
45+
if len(config_candidates_exclude_pyproject) > 1:
46+
filenames = [path.name for path in config_candidates_exclude_pyproject]
5747
out.warn(
58-
f"Multiple config files detected: {', '.join(existing_files)}. "
59-
f"Using config file: '{existing_files[0]}'."
48+
f"Multiple config files detected: {', '.join(filenames)}. "
49+
f"Using config file: '{filenames[0]}'."
6050
)
61-
break
62-
63-
64-
def read_cfg(filepath: str | None = None) -> BaseConfig:
65-
if filepath is None:
66-
_check_and_warn_multiple_configs()
6751

68-
for filename in _resolve_config_paths(filepath):
52+
for filename in config_candidates:
6953
with open(filename, "rb") as f:
7054
data: bytes = f.read()
7155

0 commit comments

Comments
 (0)