commit 3e18f57eae19b6e9597dabd2123aac040667c705 from: Jelmer Vernooij date: Thu Jun 30 19:18:08 2022 UTC Move apply_instead_of to dulwich.config. commit - 89da2e07b8c63f4beddc5c20a2874bfaf50741d6 commit + 3e18f57eae19b6e9597dabd2123aac040667c705 blob - 19f540e69893a800afb90122765f0e02d003faf1 blob + 0e90b0f88e3fa00beb9222893fd3f596d8193767 --- dulwich/client.py +++ dulwich/client.py @@ -59,7 +59,7 @@ from urllib.parse import ( import dulwich -from dulwich.config import get_xdg_config_home_path, Config +from dulwich.config import get_xdg_config_home_path, Config, apply_instead_of from dulwich.errors import ( GitProtocolError, NotGitRepository, @@ -2266,42 +2266,8 @@ def _win32_url_to_path(parsed) -> str: if url2pathname is None: from urllib.request import url2pathname # type: ignore return url2pathname(netloc + path) # type: ignore - - -def iter_instead_of(config: Config, push: bool = False) -> Iterable[Tuple[str, str]]: - """Iterate over insteadOf / pushInsteadOf values. - """ - for section in config.sections(): - if section[0] != b'url': - continue - replacement = section[1] - try: - needles = list(config.get_multivar(section, "insteadOf")) - except KeyError: - needles = [] - if push: - try: - needles += list(config.get_multivar(section, "pushInsteadOf")) - except KeyError: - pass - for needle in needles: - yield needle.decode('utf-8'), replacement.decode('utf-8') -def apply_instead_of(config: Config, orig_url: str, push: bool = False) -> str: - """Apply insteadOf / pushInsteadOf to a URL. - """ - longest_needle = "" - updated_url = orig_url - for needle, replacement in iter_instead_of(config, push): - if not orig_url.startswith(needle): - continue - if len(longest_needle) < len(needle): - longest_needle = needle - updated_url = replacement + orig_url[len(needle):] - return updated_url - - def get_transport_and_path_from_url( url: str, config: Optional[Config] = None, operation: Optional[str] = None, **kwargs) -> Tuple[GitClient, str]: blob - e52d468e3bc66f6fa295218061a2656e207ac800 blob + 4142b6d861fa2b1b7a880b8718e6f482ec7a9ed5 --- dulwich/config.py +++ dulwich/config.py @@ -772,3 +772,38 @@ def parse_submodules(config: ConfigFile) -> Iterator[T sm_path = config.get(section, b"path") sm_url = config.get(section, b"url") yield (sm_path, sm_url, section_name) + + +def iter_instead_of(config: Config, push: bool = False) -> Iterable[Tuple[str, str]]: + """Iterate over insteadOf / pushInsteadOf values. + """ + for section in config.sections(): + if section[0] != b'url': + continue + replacement = section[1] + try: + needles = list(config.get_multivar(section, "insteadOf")) + except KeyError: + needles = [] + if push: + try: + needles += list(config.get_multivar(section, "pushInsteadOf")) + except KeyError: + pass + for needle in needles: + assert isinstance(needle, bytes) + yield needle.decode('utf-8'), replacement.decode('utf-8') + + +def apply_instead_of(config: Config, orig_url: str, push: bool = False) -> str: + """Apply insteadOf / pushInsteadOf to a URL. + """ + longest_needle = "" + updated_url = orig_url + for needle, replacement in iter_instead_of(config, push): + if not orig_url.startswith(needle): + continue + if len(longest_needle) < len(needle): + longest_needle = needle + updated_url = replacement + orig_url[len(needle):] + return updated_url blob - d8c00b557f47b03af641d3e2624c4142616b8c12 blob + 767fe94a49c684c5c481bf515a8b19c4892dfe1c --- dulwich/tests/test_client.py +++ dulwich/tests/test_client.py @@ -52,7 +52,6 @@ from dulwich.client import ( PLinkSSHVendor, HangupException, GitProtocolError, - apply_instead_of, check_wants, default_urllib3_manager, get_credentials_from_store, @@ -1616,31 +1615,3 @@ And this line is just random noise, too. ] ), ) - - -class ApplyInsteadOfTests(TestCase): - def test_none(self): - config = ConfigDict() - self.assertEqual( - 'https://example.com/', apply_instead_of(config, 'https://example.com/')) - - def test_apply(self): - config = ConfigDict() - config.set( - ('url', 'https://samba.org/'), 'insteadOf', 'https://example.com/') - self.assertEqual( - 'https://samba.org/', - apply_instead_of(config, 'https://example.com/')) - - def test_apply_multiple(self): - config = ConfigDict() - config.set( - ('url', 'https://samba.org/'), 'insteadOf', 'https://blah.com/') - config.set( - ('url', 'https://samba.org/'), 'insteadOf', 'https://example.com/') - self.assertEqual( - [b'https://blah.com/', b'https://example.com/'], - list(config.get_multivar(('url', 'https://samba.org/'), 'insteadOf'))) - self.assertEqual( - 'https://samba.org/', - apply_instead_of(config, 'https://example.com/')) blob - 39f58c87262e96c0e28783e3f312a4fb7c29a5b9 blob + e5d0dbb4a9f21db19fa8eb1494ccf6bc0f4358b9 --- dulwich/tests/test_config.py +++ dulwich/tests/test_config.py @@ -36,6 +36,7 @@ from dulwich.config import ( _escape_value, _parse_string, parse_submodules, + apply_instead_of, ) from dulwich.tests import ( TestCase, @@ -428,3 +429,31 @@ class SubmodulesTests(TestCase): ], got, ) + + +class ApplyInsteadOfTests(TestCase): + def test_none(self): + config = ConfigDict() + self.assertEqual( + 'https://example.com/', apply_instead_of(config, 'https://example.com/')) + + def test_apply(self): + config = ConfigDict() + config.set( + ('url', 'https://samba.org/'), 'insteadOf', 'https://example.com/') + self.assertEqual( + 'https://samba.org/', + apply_instead_of(config, 'https://example.com/')) + + def test_apply_multiple(self): + config = ConfigDict() + config.set( + ('url', 'https://samba.org/'), 'insteadOf', 'https://blah.com/') + config.set( + ('url', 'https://samba.org/'), 'insteadOf', 'https://example.com/') + self.assertEqual( + [b'https://blah.com/', b'https://example.com/'], + list(config.get_multivar(('url', 'https://samba.org/'), 'insteadOf'))) + self.assertEqual( + 'https://samba.org/', + apply_instead_of(config, 'https://example.com/'))