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

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

33 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 repo.git.branch("-d", "-r", *refs) 

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

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

64 # manually. 

65 for ref in refs: 

66 try: 

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

68 except OSError: 

69 pass 

70 try: 

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

72 except OSError: 

73 pass 

74 # END for each ref 

75 

76 @classmethod 

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

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

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