forked from datalad/datalad-core
This new implementation represents a substantial departure from the relatively thin wrapper around `git-config` that is used in legacy DataLad. It is based on the `Settings` class from `datasalad`, and extends it with additional features, for example a context manager to temporarily amend or override configuration items (see https://github.com/datalad/datalad/issues/5155). Unlike legacy DataLad, no global config manager instance is created on import. Instead, a `get_manager()` function is provided that can be called by any code that requires access to a global manager instance. A number of additional implementations of setting sources are provided, most notably individual sources for all of Git's configuration scopes. This now also includes a `git-worktree` scope, not supported by legacy DataLad. All sources use a dedicated `ConfigItem` class for individual settings. At present, this is identical to a `datasalad` `Setting`. However, this item class will be extended to support UI-based query for configuration values in the future. This is not implemented yet. A dedicated `ImplementationDefaults` source (singleton) is included in any `ConfigManager` instance. It can be populated with application specific defaults (on import). This commit also introduced type-coercion for few standard Git configuration items using this feature.
117 lines
3.9 KiB
Python
117 lines
3.9 KiB
Python
from __future__ import annotations
|
|
|
|
from os import environ
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Mapping
|
|
|
|
|
|
def get_gitconfig_items_from_env() -> dict[str, str | tuple[str, ...]]:
|
|
"""Parse git-config ENV (``GIT_CONFIG_COUNT|KEY|VALUE``) and return as dict
|
|
|
|
This implementation does not use ``git-config`` directly, but aims to
|
|
mimic its behavior with respect to parsing the environment as much
|
|
as possible.
|
|
|
|
Raises
|
|
------
|
|
ValueError
|
|
Whenever ``git-config`` would also error out, and includes an
|
|
message in the respective exception that resembles ``git-config``'s
|
|
for that specific case.
|
|
|
|
Returns
|
|
-------
|
|
dict
|
|
Configuration key-value mappings. When a key is declared multiple
|
|
times, the respective values are aggregated in reported as a tuple
|
|
for that specific key.
|
|
"""
|
|
items: dict[str, str | tuple[str, ...]] = {}
|
|
for k, v in (
|
|
(_get_gitconfig_var_from_env(i, 'key'), _get_gitconfig_var_from_env(i, 'value'))
|
|
for i in range(_get_gitconfig_itemcount())
|
|
):
|
|
val = items.get(k)
|
|
if val is None:
|
|
items[k] = v
|
|
elif isinstance(val, tuple):
|
|
items[k] = (*val, v)
|
|
else:
|
|
items[k] = (val, v)
|
|
return items
|
|
|
|
|
|
def _get_gitconfig_itemcount() -> int:
|
|
try:
|
|
return int(environ.get('GIT_CONFIG_COUNT', '0'))
|
|
except (TypeError, ValueError) as e:
|
|
msg = 'bogus count in GIT_CONFIG_COUNT'
|
|
raise ValueError(msg) from e
|
|
|
|
|
|
def _get_gitconfig_var_from_env(nid: int, kind: str) -> str:
|
|
envname = f'GIT_CONFIG_{kind.upper()}_{nid}'
|
|
var = environ.get(envname)
|
|
if var is None:
|
|
msg = f'missing config {kind} {envname}'
|
|
raise ValueError(msg)
|
|
if kind != 'key':
|
|
return var
|
|
if not var:
|
|
msg = f'empty config key {envname}'
|
|
raise ValueError(msg)
|
|
if '.' not in var:
|
|
msg = f'key {envname} does not contain a section: {var}'
|
|
raise ValueError(msg)
|
|
return var
|
|
|
|
|
|
def set_gitconfig_items_in_env(items: Mapping[str, str | tuple[str, ...]]):
|
|
"""Set git-config ENV (``GIT_CONFIG_COUNT|KEY|VALUE``) from a mapping
|
|
|
|
Any existing declaration of configuration items in the environment is
|
|
replaced. Any ENV variable of a *valid* existing declaration is removed,
|
|
before the set configuration items are posted in the ENV.
|
|
|
|
Multi-value configuration keys are supported (values provided as a tuple).
|
|
|
|
Any item with a value of ``None`` will be posted into the ENV with an
|
|
empty string as value, i.e. the corresponding ``GIT_CONFIG_VALUE_{count}``
|
|
variable will be an empty string. ``None`` item values indicate that the
|
|
configuration key was unset on the command line, via the global option
|
|
``-c``.
|
|
|
|
No verification (e.g., of syntax compliance) is performed.
|
|
"""
|
|
_clean_env_from_gitconfig_items()
|
|
|
|
count = 0
|
|
for key, value in items.items():
|
|
# homogeneous processing of multiple value items, and single values
|
|
values = value if isinstance(value, tuple) else (value,)
|
|
for v in values:
|
|
environ[f'GIT_CONFIG_KEY_{count}'] = key
|
|
# we support None even though not an allowed input type, because
|
|
# of https://github.com/datalad/datalad/issues/7589
|
|
# this can be removed, when that issue is resolved.
|
|
environ[f'GIT_CONFIG_VALUE_{count}'] = '' if v is None else str(v)
|
|
count += 1
|
|
if count:
|
|
environ['GIT_CONFIG_COUNT'] = str(count)
|
|
|
|
|
|
def _clean_env_from_gitconfig_items():
|
|
# we only care about intact specifications here, if there was cruft
|
|
# to start with, we have no responsibilities
|
|
try:
|
|
count = _get_gitconfig_itemcount()
|
|
except ValueError:
|
|
return
|
|
|
|
for i in range(count):
|
|
environ.pop(f'GIT_CONFIG_KEY_{i}', None)
|
|
environ.pop(f'GIT_CONFIG_VALUE_{i}', None)
|
|
|
|
environ.pop('GIT_CONFIG_COUNT', None)
|