OLD | NEW |
(Empty) | |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 from infra.libs.git2.util import CalledProcessError |
| 6 from infra.libs.git2.util import INVALID |
| 7 |
| 8 class Ref(object): |
| 9 """Represents a single simple ref in a git Repo.""" |
| 10 def __init__(self, repo, ref_str): |
| 11 """ |
| 12 @type repo: Repo |
| 13 @type ref_str: str |
| 14 """ |
| 15 self._repo = repo |
| 16 self._ref = ref_str |
| 17 |
| 18 # Comparison & Representation |
| 19 def __eq__(self, other): |
| 20 return (self is other) or ( |
| 21 isinstance(other, Ref) and ( |
| 22 self.ref == other.ref and |
| 23 self.repo is other.repo |
| 24 ) |
| 25 ) |
| 26 |
| 27 def __ne__(self, other): |
| 28 return not (self == other) |
| 29 |
| 30 def __repr__(self): |
| 31 return 'Ref({_repo!r}, {_ref!r})'.format(**self.__dict__) |
| 32 |
| 33 # Accessors |
| 34 # pylint: disable=W0212 |
| 35 repo = property(lambda self: self._repo) |
| 36 ref = property(lambda self: self._ref) |
| 37 |
| 38 # Properties |
| 39 @property |
| 40 def commit(self): |
| 41 """Get the Commit at the tip of this Ref.""" |
| 42 try: |
| 43 val = self._repo.run('show-ref', '--verify', self._ref) |
| 44 except CalledProcessError: |
| 45 return INVALID |
| 46 return self._repo.get_commit(val.split()[0]) |
| 47 |
| 48 # Methods |
| 49 def to(self, other): |
| 50 """Generate Commit()'s which occur from `self..other`.""" |
| 51 assert self.commit is not INVALID |
| 52 arg = '%s..%s' % (self.ref, other.ref) |
| 53 for hsh in self.repo.run('rev-list', '--reverse', arg).splitlines(): |
| 54 yield self.repo.get_commit(hsh) |
| 55 |
| 56 def fast_forward_push(self, commit): |
| 57 """Push |commit| to this ref on the remote, and update the local copy of the |
| 58 ref to |commit|.""" |
| 59 self.repo.run('push', 'origin', '%s:%s' % (commit.hsh, self.ref)) |
| 60 self.update_to(commit) |
| 61 |
| 62 def update_to(self, commit): |
| 63 """Update the local copy of the ref to |commit|.""" |
| 64 self.repo.run('update-ref', self.ref, commit.hsh) |
OLD | NEW |