Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Unified Diff: infra/libs/git2/repo.py

Issue 413983003: Refactor infra git libs and testing. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Address comments Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: infra/libs/git2/repo.py
diff --git a/infra/services/gnumbd/support/git.py b/infra/libs/git2/repo.py
similarity index 51%
rename from infra/services/gnumbd/support/git.py
rename to infra/libs/git2/repo.py
index b337ac20c4a65e682839ac350a1f56d5783eb255..84c54cc230e265d243467b3c04dc8c342fcb21ff 100644
--- a/infra/services/gnumbd/support/git.py
+++ b/infra/libs/git2/repo.py
@@ -1,6 +1,7 @@
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
+
import collections
import fnmatch
import logging
@@ -10,30 +11,13 @@ import sys
import tempfile
import urlparse
-from infra.services.gnumbd.support.util import (
- cached_property, CalledProcessError)
-
-from infra.services.gnumbd.support.data import CommitData
+from infra.libs.git2 import CalledProcessError
+from infra.libs.git2 import Commit
+from infra.libs.git2 import Ref
LOGGER = logging.getLogger(__name__)
-class _Invalid(object):
- def __call__(self, *_args, **_kwargs):
- return self
-
- def __getattr__(self, _key):
- return self
-
- def __eq__(self, _other):
- return False
-
- def __ne__(self, _other): # pylint: disable=R0201
- return True
-
-INVALID = _Invalid()
-
-
class Repo(object):
"""Represents a remote git repo.
@@ -50,6 +34,10 @@ class Repo(object):
self._commit_cache = collections.OrderedDict()
self._log = LOGGER.getChild('Repo')
+ def __getitem__(self, ref):
+ """Get a Ref attached to this Repo."""
+ return Ref(self, ref)
+
def reify(self):
"""Ensures the local mirror of this Repo exists."""
assert self.repos_dir is not None
@@ -146,125 +134,3 @@ class Repo(object):
def intern(self, data, typ='blob'):
return self.run(
'hash-object', '-w', '-t', typ, '--stdin', indata=str(data)).strip()
-
-
-class Commit(object):
- """Represents the identity of a commit in a git repo."""
-
- def __init__(self, repo, hsh):
- """
- @type repo: Repo
- """
- assert CommitData.HASH_RE.match(hsh)
- self._repo = repo
- self._hsh = hsh
-
- # Comparison & Representation
- def __eq__(self, other):
- return (self is other) or (
- isinstance(other, Commit) and (
- self.hsh == other.hsh
- )
- )
-
- def __ne__(self, other):
- return not (self == other)
-
- def __repr__(self):
- return 'Commit({_repo!r}, {_hsh!r})'.format(**self.__dict__)
-
- # Accessors
- # pylint: disable=W0212
- repo = property(lambda self: self._repo)
- hsh = property(lambda self: self._hsh)
-
- # Properties
- @cached_property
- def data(self):
- """Get a structured data representation of this commit."""
- try:
- raw_data = self.repo.run('cat-file', 'commit', self.hsh)
- except CalledProcessError:
- return INVALID
- return CommitData.from_raw(raw_data)
-
- @cached_property
- def parent(self):
- """Get the corresponding parent Commit() for this Commit(), or None.
-
- If self has more than one parent, this raises an Exception.
- """
- parents = self.data.parents
- if len(parents) > 1:
- LOGGER.error('Commit %r has more than one parent!', self.hsh)
- return INVALID
- return self.repo.get_commit(parents[0]) if parents else None
-
- # Methods
- def alter(self, **kwargs):
- """Get a new Commit which is the same as this one, except for alterations
- specified by kwargs.
-
- This will intern the new Commit object into the Repo.
- """
- return self.repo.get_commit(
- self.repo.intern(self.data.alter(**kwargs), 'commit'))
-
-
-class Ref(object):
- """Represents a single simple ref in a git Repo."""
- def __init__(self, repo, ref_str):
- """
- @type repo: Repo
- @type ref_str: str
- """
- self._repo = repo
- self._ref = ref_str
-
- # Comparison & Representation
- def __eq__(self, other):
- return (self is other) or (
- isinstance(other, Ref) and (
- self.ref == other.ref and
- self.repo is other.repo
- )
- )
-
- def __ne__(self, other):
- return not (self == other)
-
- def __repr__(self):
- return 'Ref({_repo!r}, {_ref!r})'.format(**self.__dict__)
-
- # Accessors
- # pylint: disable=W0212
- repo = property(lambda self: self._repo)
- ref = property(lambda self: self._ref)
-
- # Properties
- @property
- def commit(self):
- """Get the Commit at the tip of this Ref."""
- try:
- val = self._repo.run('show-ref', '--verify', self._ref)
- except CalledProcessError:
- return INVALID
- return self._repo.get_commit(val.split()[0])
-
- # Methods
- def to(self, other):
- """Generate Commit()'s which occur from `self..other`."""
- assert self.commit is not INVALID
- arg = '%s..%s' % (self.ref, other.ref)
- for hsh in self.repo.run('rev-list', '--reverse', arg).splitlines():
- yield self.repo.get_commit(hsh)
-
- def fast_forward_push(self, commit):
- """Push |commit| to this ref on the remote, and update the local copy of the
- ref to |commit|."""
- self.repo.run('push', 'origin', '%s:%s' % (commit.hsh, self.ref))
- self.update_to(commit)
-
- def update_to(self, commit):
- """Update the local copy of the ref to |commit|."""
- self.repo.run('update-ref', self.ref, commit.hsh)

Powered by Google App Engine
This is Rietveld 408576698