Git configuration management.
| 79 | |
| 80 | |
| 81 | class Config: |
| 82 | """Git configuration management.""" |
| 83 | |
| 84 | _repo: 'BaseRepository' |
| 85 | _config: 'GitConfigC' |
| 86 | |
| 87 | def __init__(self, path: str | None = None) -> None: |
| 88 | cconfig = ffi.new('git_config **') |
| 89 | |
| 90 | if not path: |
| 91 | err = C.git_config_new(cconfig) |
| 92 | else: |
| 93 | path_bytes = str_to_bytes(path, 'path') |
| 94 | err = C.git_config_open_ondisk(cconfig, path_bytes) |
| 95 | |
| 96 | check_error(err, io=True) |
| 97 | self._config = cconfig[0] |
| 98 | |
| 99 | @classmethod |
| 100 | def from_c(cls, repo: 'BaseRepository', ptr: 'GitConfigC') -> 'Config': |
| 101 | config = cls.__new__(cls) |
| 102 | config._repo = repo |
| 103 | config._config = ptr |
| 104 | |
| 105 | return config |
| 106 | |
| 107 | def __del__(self) -> None: |
| 108 | try: |
| 109 | C.git_config_free(self._config) |
| 110 | except AttributeError: |
| 111 | pass |
| 112 | |
| 113 | def _get(self, key: str | bytes) -> tuple[int, 'ConfigEntry']: |
| 114 | key = str_to_bytes(key, 'key') |
| 115 | |
| 116 | entry = ffi.new('git_config_entry **') |
| 117 | err = C.git_config_get_entry(entry, self._config, key) |
| 118 | |
| 119 | return err, ConfigEntry._from_c(entry[0]) |
| 120 | |
| 121 | def _get_entry(self, key: str | bytes) -> 'ConfigEntry': |
| 122 | err, entry = self._get(key) |
| 123 | |
| 124 | if err == C.GIT_ENOTFOUND: |
| 125 | raise KeyError(key) |
| 126 | |
| 127 | check_error(err) |
| 128 | return entry |
| 129 | |
| 130 | def __contains__(self, key: str | bytes) -> bool: |
| 131 | err, cstr = self._get(key) |
| 132 | |
| 133 | if err == C.GIT_ENOTFOUND: |
| 134 | return False |
| 135 | |
| 136 | check_error(err) |
| 137 | |
| 138 | return True |
no outgoing calls
searching dependent graphs…