◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
17 changes: 16 additions & 1 deletion git/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
See: https://git-scm.com/docs/git-config#_conditional_includes
"""


class MetaParserBuilder(abc.ABCMeta): # noqa: B024
"""Utility class wrapping base-class methods into decorators that assure read-only
@@ -778,6 +781,7 @@ def _assure_writable(self, method_name: str) -> None:

def add_section(self, section: "cp._SectionName") -> None:
"""Assures added options will stay in order."""
return super().add_section(section)

@property
Expand Down Expand Up @@ -884,10 +888,14 @@ def _value_to_string(self, value: Union[str, bytes, int, float, bool]) -> str:

def _value_to_string_safe(self, value: Union[str, bytes, int, float, bool]) -> str:
value_str = self._value_to_string(value)
if re.search(r"[\r\n\x00]", value_str):
raise ValueError("Git config values must not contain CR, LF, or NUL")
return value_str

@needs_values
@set_dirty_and_flush_changes
def set(
Expand All @@ -896,6 +904,8 @@ def set(
option: str,
value: Union[str, bytes, int, float, bool, None] = None,
) -> None:
if value is not None:
value = self._value_to_string_safe(value)
return super().set(section, option, value)
Expand All @@ -920,6 +930,8 @@ def set_value(self, section: str, option: str, value: Union[str, bytes, int, flo
:return:
This instance
"""
value_str = self._value_to_string_safe(value)
if not self.has_section(section):
self.add_section(section)
Expand Down @@ -948,6 +960,8 @@ def add_value(self, section: str, option: str, value: Union[str, bytes, int, flo
:return:
This instance
"""
value_str = self._value_to_string_safe(value)
if not self.has_section(section):
self.add_section(section)
Expand All @@ -968,6 +982,7 @@ def rename_section(self, section: str, new_name: str) -> "GitConfigParser":
"""
if not self.has_section(section):
raise ValueError("Source section '%s' doesn't exist" % section)
if self.has_section(new_name):
raise ValueError("Destination section '%s' already exists" % new_name)

Loading
Toggle all file notes Toggle all file annotations