Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/git/refs/remote.py: 45%

32 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1import os 

2 

3from git.util import join_path 

4 

5from .head import Head 

6 

7 

8__all__ = ["RemoteReference"] 

9 

10# typing ------------------------------------------------------------------ 

11 

12from typing import Any, Iterator, NoReturn, Union, TYPE_CHECKING 

13from git.types import PathLike 

14 

15 

16if TYPE_CHECKING: 16 ↛ 17line 16 didn't jump to line 17, because the condition on line 16 was never true

17 from git.repo import Repo 

18 from git import Remote 

19 

20# ------------------------------------------------------------------------------ 

21 

22 

23class RemoteReference(Head): 

24 

25 """Represents a reference pointing to a remote head.""" 

26 

27 _common_path_default = Head._remote_common_path_default 

28 

29 @classmethod 

30 def iter_items( 

31 cls, 

32 repo: "Repo", 

33 common_path: Union[PathLike, None] = None, 

34 remote: Union["Remote", None] = None, 

35 *args: Any, 

36 **kwargs: Any, 

37 ) -> Iterator["RemoteReference"]: 

38 """Iterate remote references, and if given, constrain them to the given remote""" 

39 common_path = common_path or cls._common_path_default 

40 if remote is not None: 

41 common_path = join_path(common_path, str(remote)) 

42 # END handle remote constraint 

43 # super is Reference 

44 return super(RemoteReference, cls).iter_items(repo, common_path) 

45 

46 # The Head implementation of delete also accepts strs, but this 

47 # implementation does not. mypy doesn't have a way of representing 

48 # tightening the types of arguments in subclasses and recommends Any or 

49 # "type: ignore". (See https://github.com/python/typing/issues/241) 

50 @classmethod 

51 def delete(cls, repo: "Repo", *refs: "RemoteReference", **kwargs: Any) -> None: # type: ignore 

52 """Delete the given remote references 

53 

54 :note: 

55 kwargs are given for comparability with the base class method as we 

56 should not narrow the signature.""" 

57 repo.git.branch("-d", "-r", *refs) 

58 # the official deletion method will ignore remote symbolic refs - these 

59 # are generally ignored in the refs/ folder. We don't though 

60 # and delete remainders manually 

61 for ref in refs: 

62 try: 

63 os.remove(os.path.join(repo.common_dir, ref.path)) 

64 except OSError: 

65 pass 

66 try: 

67 os.remove(os.path.join(repo.git_dir, ref.path)) 

68 except OSError: 

69 pass 

70 # END for each ref 

71 

72 @classmethod 

73 def create(cls, *args: Any, **kwargs: Any) -> NoReturn: 

74 """Used to disable this method""" 

75 raise TypeError("Cannot explicitly create remote references")