Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/git/refs/remote.py: 44%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

36 statements  

1# This module is part of GitPython and is released under the 

2# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/ 

3 

4"""Module implementing a remote object allowing easy access to git remotes.""" 

5 

6__all__ = ["RemoteReference"] 

7 

8import os 

9 

10from git.util import join_path 

11 

12from .head import Head 

13 

14# typing ------------------------------------------------------------------ 

15 

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

17 

18from git.types import PathLike 

19 

20if TYPE_CHECKING: 

21 from git.remote import Remote 

22 from git.repo import Repo 

23 

24# ------------------------------------------------------------------------------ 

25 

26 

27class RemoteReference(Head): 

28 """A reference pointing to a remote head.""" 

29 

30 _common_path_default = Head._remote_common_path_default 

31 

32 @classmethod 

33 def iter_items( 

34 cls, 

35 repo: "Repo", 

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

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

38 *args: Any, 

39 **kwargs: Any, 

40 ) -> Iterator["RemoteReference"]: 

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

42 common_path = common_path or cls._common_path_default 

43 if remote is not None: 

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

45 # END handle remote constraint 

46 # super is Reference 

47 return super().iter_items(repo, common_path) 

48 

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

50 # not. mypy doesn't have a way of representing tightening the types of arguments in 

51 # subclasses and recommends Any or "type: ignore". 

52 # (See: https://github.com/python/typing/issues/241) 

53 @classmethod 

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

55 """Delete the given remote references. 

56 

57 :note: 

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

59 should not narrow the signature. 

60 """ 

61 for ref in refs: 

62 cls._check_ref_name_valid(ref.path) 

63 

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

65 # The official deletion method will ignore remote symbolic refs - these are 

66 # generally ignored in the refs/ folder. We don't though and delete remainders 

67 # manually. 

68 for ref in refs: 

69 try: 

70 os.remove(cls._get_validated_path(repo.common_dir, ref.path)) 

71 except OSError: 

72 pass 

73 try: 

74 os.remove(cls._get_validated_path(repo.git_dir, ref.path)) 

75 except OSError: 

76 pass 

77 # END for each ref 

78 

79 @classmethod 

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

81 """Raise :exc:`TypeError`. Defined so the ``create`` method is disabled.""" 

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