refactor(config): replace is_empty_config with contains_commitizen_section, improve multi config resolution algorithm by bearomorphism · Pull Request #1842 · commitizen-tools/commitizen · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions commitizen/config/__init__.py
8 changes: 7 additions & 1 deletion commitizen/config/base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@

class BaseConfig:
def __init__(self) -> None:
self.is_empty_config = False
self._settings: Settings = DEFAULT_SETTINGS.copy()
self._path: Path | None = None

def contains_commitizen_section(self) -> bool:
"""Check if the config file contains a commitizen section.

The implementation is different for each config file type.
"""
raise NotImplementedError()
Comment thread
bearomorphism marked this conversation as resolved.

@property
def settings(self) -> Settings:
return self._settings
Expand Down
9 changes: 7 additions & 2 deletions commitizen/config/json_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ def __init__(self, *, data: bytes | str, path: Path) -> None:
self.path = path
self._parse_setting(data)

def contains_commitizen_section(self) -> bool:
with self.path.open("rb") as json_file:
config_doc = json.load(json_file)
return config_doc.get("commitizen") is not None

def init_empty_config_content(self) -> None:
with smart_open(
self.path, "a", encoding=self._settings["encoding"]
) as json_file:
json.dump({"commitizen": {}}, json_file)

def set_key(self, key: str, value: object) -> Self:
with open(self.path, "rb") as f:
with self.path.open("rb") as f:
config_doc = json.load(f)

config_doc["commitizen"][key] = value
Expand All @@ -59,4 +64,4 @@ def _parse_setting(self, data: bytes | str) -> None:
try:
self.settings.update(doc["commitizen"])
except KeyError:
self.is_empty_config = True
pass
7 changes: 6 additions & 1 deletion commitizen/config/toml_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def __init__(self, *, data: bytes | str, path: Path) -> None:
self.path = path
self._parse_setting(data)

def contains_commitizen_section(self) -> bool:
with self.path.open("rb") as f:
config_doc = parse(f.read())
return config_doc.get("tool", {}).get("commitizen") is not None

def init_empty_config_content(self) -> None:
config_doc = TOMLDocument()
if os.path.isfile(self.path):
Expand Down Expand Up @@ -67,4 +72,4 @@ def _parse_setting(self, data: bytes | str) -> None:
try:
self.settings.update(doc["tool"]["commitizen"]) # type: ignore[index,typeddict-item] # TODO: fix this
except exceptions.NonExistentKey:
self.is_empty_config = True
pass
9 changes: 6 additions & 3 deletions commitizen/config/yaml_config.py