remote.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import os
  2. from git.util import join_path
  3. from .head import Head
  4. __all__ = ["RemoteReference"]
  5. # typing ------------------------------------------------------------------
  6. from typing import Any, Iterator, NoReturn, Union, TYPE_CHECKING
  7. from git.types import PathLike
  8. if TYPE_CHECKING:
  9. from git.repo import Repo
  10. from git import Remote
  11. # ------------------------------------------------------------------------------
  12. class RemoteReference(Head):
  13. """Represents a reference pointing to a remote head."""
  14. _common_path_default = Head._remote_common_path_default
  15. @classmethod
  16. def iter_items(
  17. cls,
  18. repo: "Repo",
  19. common_path: Union[PathLike, None] = None,
  20. remote: Union["Remote", None] = None,
  21. *args: Any,
  22. **kwargs: Any,
  23. ) -> Iterator["RemoteReference"]:
  24. """Iterate remote references, and if given, constrain them to the given remote"""
  25. common_path = common_path or cls._common_path_default
  26. if remote is not None:
  27. common_path = join_path(common_path, str(remote))
  28. # END handle remote constraint
  29. # super is Reference
  30. return super(RemoteReference, cls).iter_items(repo, common_path)
  31. # The Head implementation of delete also accepts strs, but this
  32. # implementation does not. mypy doesn't have a way of representing
  33. # tightening the types of arguments in subclasses and recommends Any or
  34. # "type: ignore". (See https://github.com/python/typing/issues/241)
  35. @classmethod
  36. def delete(cls, repo: "Repo", *refs: "RemoteReference", **kwargs: Any) -> None: # type: ignore
  37. """Delete the given remote references
  38. :note:
  39. kwargs are given for comparability with the base class method as we
  40. should not narrow the signature."""
  41. repo.git.branch("-d", "-r", *refs)
  42. # the official deletion method will ignore remote symbolic refs - these
  43. # are generally ignored in the refs/ folder. We don't though
  44. # and delete remainders manually
  45. for ref in refs:
  46. try:
  47. os.remove(os.path.join(repo.common_dir, ref.path))
  48. except OSError:
  49. pass
  50. try:
  51. os.remove(os.path.join(repo.git_dir, ref.path))
  52. except OSError:
  53. pass
  54. # END for each ref
  55. @classmethod
  56. def create(cls, *args: Any, **kwargs: Any) -> NoReturn:
  57. """Used to disable this method"""
  58. raise TypeError("Cannot explicitly create remote references")