1import os
2
3from git.util import join_path
4
5import os.path as osp
6
7from .head import Head
8
9
10__all__ = ["RemoteReference"]
11
12
13class RemoteReference(Head):
14
15    """Represents a reference pointing to a remote head."""
16    _common_path_default = Head._remote_common_path_default
17
18    @classmethod
19    def iter_items(cls, repo, common_path=None, remote=None):
20        """Iterate remote references, and if given, constrain them to the given remote"""
21        common_path = common_path or cls._common_path_default
22        if remote is not None:
23            common_path = join_path(common_path, str(remote))
24        # END handle remote constraint
25        return super(RemoteReference, cls).iter_items(repo, common_path)
26
27    @classmethod
28    def delete(cls, repo, *refs, **kwargs):
29        """Delete the given remote references
30
31        :note:
32            kwargs are given for comparability with the base class method as we
33            should not narrow the signature."""
34        repo.git.branch("-d", "-r", *refs)
35        # the official deletion method will ignore remote symbolic refs - these
36        # are generally ignored in the refs/ folder. We don't though
37        # and delete remainders manually
38        for ref in refs:
39            try:
40                os.remove(osp.join(repo.common_dir, ref.path))
41            except OSError:
42                pass
43            try:
44                os.remove(osp.join(repo.git_dir, ref.path))
45            except OSError:
46                pass
47        # END for each ref
48
49    @classmethod
50    def create(cls, *args, **kwargs):
51        """Used to disable this method"""
52        raise TypeError("Cannot explicitly create remote references")
53