|
| 1 | +from symbolic import SymbolicReference |
| 2 | +from reference import Reference |
| 3 | +from gitdb.config import SectionConstraint |
| 4 | +from gitdb.util import join_path |
| 5 | + |
| 6 | +__all__ = ["HEAD", "Head"] |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +class HEAD(SymbolicReference): |
| 11 | + """Special case of a Symbolic Reference as it represents the repository's |
| 12 | + HEAD reference.""" |
| 13 | + _HEAD_NAME = 'HEAD' |
| 14 | + _ORIG_HEAD_NAME = 'ORIG_HEAD' |
| 15 | + __slots__ = tuple() |
| 16 | + |
| 17 | + def __init__(self, repo, path=_HEAD_NAME): |
| 18 | + if path != self._HEAD_NAME: |
| 19 | + raise ValueError("HEAD instance must point to %r, got %r" % (self._HEAD_NAME, path)) |
| 20 | + super(HEAD, self).__init__(repo, path) |
| 21 | + |
| 22 | + def orig_head(self): |
| 23 | + """ |
| 24 | + :return: SymbolicReference pointing at the ORIG_HEAD, which is maintained |
| 25 | + to contain the previous value of HEAD""" |
| 26 | + return SymbolicReference(self.repo, self._ORIG_HEAD_NAME) |
| 27 | + |
| 28 | + |
| 29 | +class Head(Reference): |
| 30 | + """A Head is a named reference to a Commit""" |
| 31 | + _common_path_default = "refs/heads" |
| 32 | + k_config_remote = "remote" |
| 33 | + k_config_remote_ref = "merge" # branch to merge from remote |
| 34 | + |
| 35 | + # will be set by init method ! |
| 36 | + RemoteReferenceCls = None |
| 37 | + |
| 38 | + #{ Configuration |
| 39 | + |
| 40 | + def set_tracking_branch(self, remote_reference): |
| 41 | + """ |
| 42 | + Configure this branch to track the given remote reference. This will alter |
| 43 | + this branch's configuration accordingly. |
| 44 | + |
| 45 | + :param remote_reference: The remote reference to track or None to untrack |
| 46 | + any references |
| 47 | + :return: self""" |
| 48 | + if remote_reference is not None and not isinstance(remote_reference, self.RemoteReferenceCls): |
| 49 | + raise ValueError("Incorrect parameter type: %r" % remote_reference) |
| 50 | + # END handle type |
| 51 | + |
| 52 | + writer = self.config_writer() |
| 53 | + if remote_reference is None: |
| 54 | + writer.remove_option(self.k_config_remote) |
| 55 | + writer.remove_option(self.k_config_remote_ref) |
| 56 | + if len(writer.options()) == 0: |
| 57 | + writer.remove_section() |
| 58 | + # END handle remove section |
| 59 | + else: |
| 60 | + writer.set_value(self.k_config_remote, remote_reference.remote_name) |
| 61 | + writer.set_value(self.k_config_remote_ref, Head.to_full_path(remote_reference.remote_head)) |
| 62 | + # END handle ref value |
| 63 | + |
| 64 | + return self |
| 65 | + |
| 66 | + def tracking_branch(self): |
| 67 | + """ |
| 68 | + :return: The remote_reference we are tracking, or None if we are |
| 69 | + not a tracking branch""" |
| 70 | + reader = self.config_reader() |
| 71 | + if reader.has_option(self.k_config_remote) and reader.has_option(self.k_config_remote_ref): |
| 72 | + ref = Head(self.repo, Head.to_full_path(reader.get_value(self.k_config_remote_ref))) |
| 73 | + remote_refpath = self.RemoteReferenceCls.to_full_path(join_path(reader.get_value(self.k_config_remote), ref.name)) |
| 74 | + return self.RemoteReferenceCls(self.repo, remote_refpath) |
| 75 | + # END handle have tracking branch |
| 76 | + |
| 77 | + # we are not a tracking branch |
| 78 | + return None |
| 79 | + |
| 80 | + |
| 81 | + #{ Configruation |
| 82 | + |
| 83 | + def _config_parser(self, read_only): |
| 84 | + if read_only: |
| 85 | + parser = self.repo.config_reader() |
| 86 | + else: |
| 87 | + parser = self.repo.config_writer() |
| 88 | + # END handle parser instance |
| 89 | + |
| 90 | + return SectionConstraint(parser, 'branch "%s"' % self.name) |
| 91 | + |
| 92 | + def config_reader(self): |
| 93 | + """ |
| 94 | + :return: A configuration parser instance constrained to only read |
| 95 | + this instance's values""" |
| 96 | + return self._config_parser(read_only=True) |
| 97 | + |
| 98 | + def config_writer(self): |
| 99 | + """ |
| 100 | + :return: A configuration writer instance with read-and write acccess |
| 101 | + to options of this head""" |
| 102 | + return self._config_parser(read_only=False) |
| 103 | + |
| 104 | + #} END configuration |
| 105 | + |
| 106 | + |
0 commit comments