Skip to content

Commit 0eb09ba

Browse files
committed
Implement most code review feedback, some still TODO
- In-memory backend now returns last entry in a multivar instead of first entry to be consistent with Git CLI and stock libgit2 backends. - Nested entry into config context manager is now explicitly disallowed (in code and in documentation). This was never meant to be supported. - Added missing acquisition of write and read locks in `_config_memory_backend_set` and `_config_memory_backend_get`. - Fixed mismatched backtick in `_set_write_order` docstring. - Config entry name case is now preserved even while it is matched case-insensitively. - Fixed type of first argument for FFI stub of `GitConfigIteratorC.next`.
1 parent 7f1538e commit 0eb09ba

2 files changed

Lines changed: 124 additions & 27 deletions

File tree

‎pygit2/config.py‎

Lines changed: 66 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,8 @@ def __init__(
817817
self._backend_added = False
818818
self._backend = _InMemoryBackend(config=self)
819819

820+
self._backend_active = False
821+
820822
@abc.abstractmethod
821823
def _add_backend_to_config(self, backend: _InMemoryBackend) -> None:
822824
"""Adds the backend to the config object."""
@@ -833,15 +835,26 @@ def __enter__(self) -> Self:
833835
the context endures, Git operations will use the sum total configuration that
834836
includes the in-memory configuration.
835837
838+
The resulting context is non-reentrant. It must be exited before it can be
839+
entered again, or else a ``RuntimeError`` is raised.
840+
836841
:returns: ``self``
837842
838843
:raises TypeError: If this is a read-only snapshot of the configuration.
844+
:raises RuntimeError: If the config context manager has already been entered but
845+
not exited.
839846
"""
840847
if self._is_snapshot:
841848
raise TypeError(
842849
'A read-only config snapshot cannot be used as a context manager, '
843850
'because its backend data cannot be changed.'
844851
)
852+
if self._backend_active:
853+
raise RuntimeError(
854+
'The config context manager is non-reentrant. You must exit the active '
855+
'context before attempting to re-enter it.'
856+
)
857+
self._backend_active = True
845858
if not self._backend_added:
846859
self._add_backend_to_config(self._backend)
847860
self._backend_added = True
@@ -865,6 +878,7 @@ def __exit__(
865878
if self._backend_added:
866879
self._set_write_order(self._default_write_order())
867880
self._backend.clear()
881+
self._backend_active = False
868882
return False
869883

870884
def _set_write_order(self, levels: Sequence[ConfigLevel]) -> None:
@@ -873,7 +887,7 @@ def _set_write_order(self, levels: Sequence[ConfigLevel]) -> None:
873887
By default, when libgit2 creates a ``git_config`` object for a repository, it sets
874888
the write order to ``{ GIT_CONFIG_LEVEL_LOCAL }``. This means that writes go
875889
only to the local config in ``.git/config`` and nowhere else. We need to change
876-
this to ``{ GIT_CONFIG_LEVEL_APP }` when entering the context and then back to
890+
this to ``{ GIT_CONFIG_LEVEL_APP }`` when entering the context and then back to
877891
``{ GIT_CONFIG_LEVEL_LOCAL }`` when exiting the context. For the non-repo "default"
878892
config, the default we want to restore is ``{ GIT_CONFIG_LEVEL_GLOBAL,
879893
GIT_CONFIG_LEVEL_XDG, GIT_CONFIG_LEVEL_SYSTEM, GIT_CONFIG_LEVEL_PROGRAMDATA }``.
@@ -909,8 +923,9 @@ class DefaultConfig(_InMemoryAppBackendConfig):
909923
usual order. When the context manager exits, the in-memory backend's contents are
910924
erased, undoing any changes made to it.
911925
912-
The context manager can be re-entered and then re-exited repeatedly; it is not a
913-
one-use-only operation.
926+
The context manager can be entered, exited, then re-entered and re-exited repeatedly;
927+
it is not a one-use-only operation. However, an active context is non-reentrant. You
928+
must exit the context before re-entering, or else a ``RuntimeError`` is raised.
914929
915930
Only writeable ``DefaultConfig`` objects can be used as a context manager.
916931
Read-only snapshot ``DefaultConfig`` objects cannot.
@@ -1019,8 +1034,9 @@ class RepositoryConfig(_InMemoryAppBackendConfig):
10191034
in-memory backend's contents are erased, undoing any changes made to it and
10201035
allowing write operations to resume affecting the local configuration.
10211036
1022-
The context manager can be re-entered and then re-exited repeatedly; it is not a
1023-
one-use-only operation.
1037+
The context manager can be entered, exited, then re-entered and re-exited repeatedly;
1038+
it is not a one-use-only operation. However, an active context is non-reentrant. You
1039+
must exit the context before re-entering, or else a ``RuntimeError`` is raised.
10241040
10251041
Only writeable ``RepositoryConfig`` objects can be used as a context manager.
10261042
Read-only snapshot ``RepositoryConfig`` objects cannot.
@@ -1321,7 +1337,7 @@ def read_lock(self) -> Generator[None, None, None]:
13211337
transaction).
13221338
"""
13231339
if self._is_snapshot:
1324-
# If it's a snapshot, it's read-only.
1340+
# If it's a snapshot, it's read-only, so lock nothing.
13251341
yield
13261342
return
13271343

@@ -1483,8 +1499,16 @@ def __init__(
14831499
self._c_handle = ffi.new_handle(self)
14841500
self._c_iterator = c_iterator
14851501
self._c_entries: dict[int, 'PyGitConfigIteratorEntryC'] = {}
1502+
self._closed = False
1503+
1504+
def close(self) -> None:
1505+
if not self._closed:
1506+
self._closed = True
1507+
self._generator.close()
14861508

14871509
def __next__(self) -> tuple[str, _InMemoryBackend.Entry]:
1510+
if self._closed:
1511+
raise StopIteration()
14881512
return next(self._generator)
14891513

14901514
def __enter__(self) -> Self:
@@ -1497,9 +1521,13 @@ def __exit__(
14971521
exc_val: BaseException | None,
14981522
exc_tb: TracebackType | None,
14991523
) -> Literal[False]:
1524+
self.close()
15001525
self._backend._iterators.pop(id(self), None)
15011526
return False
15021527

1528+
def __del__(self) -> None:
1529+
self.close()
1530+
15031531

15041532
@ffi.def_extern()
15051533
def _config_memory_backend_open(
@@ -1570,11 +1598,13 @@ def _config_memory_backend_get(
15701598
backend_wrapper = ffi.cast('_pygit_in_memory_backend *', backend)
15711599
self = cast(_InMemoryBackend, ffi.from_handle(backend_wrapper.self))
15721600
try:
1573-
key = ffi.string(name).decode('utf-8').lower()
1574-
if key not in self._read_data or not self._read_data[key]:
1575-
return C.GIT_ENOTFOUND
1601+
key = ffi.string(name).decode('utf-8')
1602+
key_lowered = key.casefold()
1603+
with self.read_lock():
1604+
if key_lowered not in self._read_data or not self._read_data[key_lowered]:
1605+
return C.GIT_ENOTFOUND
1606+
value = self._read_data[key_lowered][-1]
15761607

1577-
value = self._read_data[key][0]
15781608
entry = ffi.new('_pygit_in_memory_backend_entry *')
15791609
ptr = int(ffi.cast('uintptr_t', entry))
15801610
entry.owner = backend_wrapper
@@ -1617,9 +1647,11 @@ def _config_memory_backend_set(
16171647
try:
16181648
if self._is_snapshot:
16191649
return C.GIT_EREADONLY
1620-
key = ffi.string(name).decode('utf-8').lower()
1650+
key = ffi.string(name).decode('utf-8')
1651+
key_lowered = key.casefold()
16211652
decoded_value = ffi.string(value).decode('utf-8')
1622-
self._write_data[key] = [_InMemoryBackend.Entry(key, decoded_value)]
1653+
with self.write_lock():
1654+
self._write_data[key_lowered] = [_InMemoryBackend.Entry(key, decoded_value)]
16231655
except BaseException as e:
16241656
self.store_exception(e)
16251657
return C.GIT_EUSER
@@ -1657,16 +1689,19 @@ def _config_memory_backend_set_multivar(
16571689
try:
16581690
if self._is_snapshot:
16591691
return C.GIT_EREADONLY
1660-
key = ffi.string(name).decode('utf-8').lower()
1692+
key = ffi.string(name).decode('utf-8')
1693+
key_lowered = key.casefold()
16611694
with self.write_lock():
1662-
if key in self._write_data and regexp != ffi.NULL:
1695+
if key_lowered in self._write_data and regexp != ffi.NULL:
16631696
expression = re.compile(ffi.string(regexp).decode('utf-8'))
1664-
self._write_data[key] = [
1665-
v for v in self._write_data[key] if not expression.search(v.value)
1697+
self._write_data[key_lowered] = [
1698+
v
1699+
for v in self._write_data[key_lowered]
1700+
if not expression.search(v.value)
16661701
]
1667-
elif key not in self._write_data:
1668-
self._write_data[key] = []
1669-
self._write_data[key].append(
1702+
elif key_lowered not in self._write_data:
1703+
self._write_data[key_lowered] = []
1704+
self._write_data[key_lowered].append(
16701705
_InMemoryBackend.Entry(key, ffi.string(value).decode('utf-8')),
16711706
)
16721707
except BaseException as e:
@@ -1698,11 +1733,12 @@ def _config_memory_backend_del(
16981733
try:
16991734
if self._is_snapshot:
17001735
return C.GIT_EREADONLY
1701-
key = ffi.string(name).decode('utf-8').lower()
1736+
key = ffi.string(name).decode('utf-8')
1737+
key_lowered = key.casefold()
17021738
with self.write_lock():
1703-
if key not in self._write_data:
1739+
if key_lowered not in self._write_data:
17041740
return C.GIT_ENOTFOUND
1705-
del self._write_data[key]
1741+
del self._write_data[key_lowered]
17061742
except BaseException as e:
17071743
self.store_exception(e)
17081744
return C.GIT_EUSER
@@ -1734,16 +1770,19 @@ def _config_memory_backend_del_multivar(
17341770
try:
17351771
if self._is_snapshot:
17361772
return C.GIT_EREADONLY
1737-
key = ffi.string(name).decode('utf-8').lower()
1773+
key = ffi.string(name).decode('utf-8')
1774+
key_lowered = key.casefold()
17381775
with self.write_lock():
1739-
if key not in self._write_data:
1776+
if key_lowered not in self._write_data:
17401777
return C.GIT_ENOTFOUND
17411778
if regexp == ffi.NULL:
1742-
del self._write_data[key]
1779+
del self._write_data[key_lowered]
17431780
else:
17441781
expression = re.compile(ffi.string(regexp).decode('utf-8'))
1745-
self._write_data[key] = [
1746-
v for v in self._write_data[key] if not expression.search(v.value)
1782+
self._write_data[key_lowered] = [
1783+
v
1784+
for v in self._write_data[key_lowered]
1785+
if not expression.search(v.value)
17471786
]
17481787
except BaseException as e:
17491788
self.store_exception(e)

‎test/test_config.py‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,24 @@ def test_repository_config_in_memory_overrides(config: RepositoryConfig) -> None
493493
config.set_multivar('core.override4', '^$', 'bar')
494494
assert 'core.override4' in config
495495
assert list(config.get_multivar('core.override4')) == ['bar']
496+
assert config['core.override4'] == 'bar'
496497
config.set_multivar('core.override4', '^$', 'baz')
497498
assert list(config.get_multivar('core.override4')) == ['bar', 'baz']
499+
assert config['core.override4'] == 'baz'
498500
config.set_multivar('core.override4', '^ba', 'qux')
499501
assert list(config.get_multivar('core.override4')) == ['qux']
502+
assert config['core.override4'] == 'qux'
503+
504+
with pytest.raises(RuntimeError) as exc_info:
505+
with config:
506+
pass
507+
assert exc_info.value.args == (
508+
'The config context manager is non-reentrant. You must exit the active '
509+
'context before attempting to re-enter it.',
510+
)
511+
assert 'core.override3' in config
512+
assert config['core.override3'] == 'foo'
513+
assert config['core.override4'] == 'qux'
500514

501515
# try deleting some stuff
502516
assert 'core.override5' not in config
@@ -572,6 +586,21 @@ def test_repository_config_in_memory_overrides(config: RepositoryConfig) -> None
572586
assert not config.get_bool('core.override1')
573587
assert not config.get_bool('core.oVeRrIdE1')
574588

589+
# and make sure case is preserved even though it's matched insensitively
590+
folded_case_found = False
591+
for entry in config:
592+
if entry.name.lower() == 'core.override1':
593+
folded_case_found = True
594+
assert entry.name == 'core.oVeRrIdE1', (
595+
f"Expected the preserved name for config key 'core.override1' "
596+
f"to be 'core.oVeRrIdE1', but instead it was '{entry.name}'."
597+
)
598+
break
599+
assert folded_case_found, (
600+
f"Expected to find 'core.oVeRrIdE1' when iterating the config, but did "
601+
f'not. All keys: {[entry.name for entry in config]!r}'
602+
)
603+
575604
assert 'core.override2' not in config
576605
config['core.override2'] = 81
577606
assert 'core.override2' in config
@@ -665,10 +694,24 @@ def test_default_config_in_memory_overrides() -> None:
665694
config.set_multivar('core.override4', '^$', 'bar')
666695
assert 'core.override4' in config
667696
assert list(config.get_multivar('core.override4')) == ['bar']
697+
assert config['core.override4'] == 'bar'
668698
config.set_multivar('core.override4', '^$', 'baz')
669699
assert list(config.get_multivar('core.override4')) == ['bar', 'baz']
700+
assert config['core.override4'] == 'baz'
670701
config.set_multivar('core.override4', '^ba', 'qux')
671702
assert list(config.get_multivar('core.override4')) == ['qux']
703+
assert config['core.override4'] == 'qux'
704+
705+
with pytest.raises(RuntimeError) as exc_info:
706+
with config:
707+
pass
708+
assert exc_info.value.args == (
709+
'The config context manager is non-reentrant. You must exit the active '
710+
'context before attempting to re-enter it.',
711+
)
712+
assert 'core.override3' in config
713+
assert config['core.override3'] == 'foo'
714+
assert config['core.override4'] == 'qux'
672715

673716
# try deleting some stuff
674717
assert 'core.override5' not in config
@@ -711,6 +754,21 @@ def test_default_config_in_memory_overrides() -> None:
711754
assert not config.get_bool('core.override1')
712755
assert not config.get_bool('core.oVeRrIdE1')
713756

757+
# and make sure case is preserved even though it's matched insensitively
758+
folded_case_found = False
759+
for entry in config:
760+
if entry.name.lower() == 'core.override1':
761+
folded_case_found = True
762+
assert entry.name == 'core.oVeRrIdE1', (
763+
f"Expected the preserved name for config key 'core.override1' "
764+
f"to be 'core.oVeRrIdE1', but instead it was '{entry.name}'."
765+
)
766+
break
767+
assert folded_case_found, (
768+
f"Expected to find 'core.oVeRrIdE1' when iterating the config, but did "
769+
f'not. All keys: {[entry.name for entry in config]!r}'
770+
)
771+
714772
assert 'core.override2' not in config
715773
config['core.override2'] = 81
716774
assert 'core.override2' in config

0 commit comments

Comments
 (0)