summaryrefslogtreecommitdiff
path: root/morphlib/cachedrepo.py
diff options
context:
space:
mode:
authorDaniel Silverstone <daniel.silverstone@codethink.co.uk>2012-09-14 09:32:45 +0100
committerDaniel Silverstone <daniel.silverstone@codethink.co.uk>2012-09-14 17:14:16 +0100
commitdd4f52f311467240ea2749c773d46b751478381d (patch)
treef6939600c12ad695bf375e452657c8e13f2c9bf2 /morphlib/cachedrepo.py
parent8d98f5692fb974d081dfb74a64e2f4b861b27461 (diff)
downloadmorph-dd4f52f311467240ea2749c773d46b751478381d.tar.gz
Rework git caches to be bare mirrors of the repos.
This reworks the code for managing and using the git caches in morph to treat the caches as bare repositories which are mirrors of where we clone from. In addition we correctly prune the branches during updates, so that we don't end up accumulating pointless branches over and over. This is even more important with branch-and-merge generating temporary build refs for things.
Diffstat (limited to 'morphlib/cachedrepo.py')
-rw-r--r--morphlib/cachedrepo.py50
1 files changed, 43 insertions, 7 deletions
diff --git a/morphlib/cachedrepo.py b/morphlib/cachedrepo.py
index ad17785a..61526cd7 100644
--- a/morphlib/cachedrepo.py
+++ b/morphlib/cachedrepo.py
@@ -49,6 +49,14 @@ class CloneError(cliapp.AppException):
def __init__(self, repo, target_dir):
cliapp.AppException.__init__(
self,
+ 'Failed to clone %s into %s' % (repo.original_name, target_dir))
+
+
+class CopyError(cliapp.AppException):
+
+ def __init__(self, repo, target_dir):
+ cliapp.AppException.__init__(
+ self,
'Failed to copy %s into %s' % (repo.original_name, target_dir))
@@ -75,6 +83,9 @@ class CachedRepo(object):
remote Git repository. This remote repository is set up as the
'origin' remote.
+ Cached repositories are bare mirrors of the upstream. Locally created
+ branches will be lost the next time the repository updates.
+
CachedRepo objects can resolve Git refs into SHA1s. Given a SHA1
ref, they can also be asked to return the contents of a file via the
cat() method. They can furthermore check out the repository into
@@ -109,8 +120,7 @@ class CachedRepo(object):
if not self.is_valid_sha1(ref):
try:
refs = self._show_ref(ref).split('\n')
- # split each ref line into an array, drop non-origin branches
- refs = [x.split() for x in refs if 'origin' in x]
+ refs = [x.split() for x in refs]
absref = refs[0][0]
except cliapp.AppException:
raise InvalidReferenceError(self, ref)
@@ -146,8 +156,8 @@ class CachedRepo(object):
raise IOError('File %s does not exist in ref %s of repo %s' %
(filename, ref, self))
- def checkout(self, ref, target_dir):
- '''Unpacks the repository in a directory and checks out a commit ref.
+ def clone_checkout(self, ref, target_dir):
+ '''Clone from the cache into the target path and check out a given ref.
Raises a CheckoutDirectoryExistsError if the target
directory already exists. Raises an InvalidReferenceError if the
@@ -160,9 +170,28 @@ class CachedRepo(object):
if os.path.exists(target_dir):
raise CheckoutDirectoryExistsError(self, target_dir)
- os.mkdir(target_dir)
+ self.resolve_ref(ref)
+
+ self._clone_into(target_dir, ref)
+
+ def checkout(self, ref, target_dir):
+ '''Unpacks the repository in a directory and checks out a commit ref.
+
+ Raises an InvalidReferenceError if the ref is not found in the
+ repository. Raises a CopyError if something goes wrong with the copy
+ of the repository. Raises a CheckoutError if something else goes wrong
+ while copying the repository or checking out the SHA1 ref.
+
+ '''
+
+ if not os.path.exists(target_dir):
+ os.mkdir(target_dir)
+ # Note, we copy instead of cloning because it's much faster in the case
+ # that the target is on a different filesystem from the cache. We then
+ # take care to turn the copy into something as good as a real clone.
self._copy_repository(self.path, target_dir)
+
self._checkout_ref(ref, target_dir)
def ls_tree(self, ref):
@@ -219,11 +248,18 @@ class CachedRepo(object):
return self._runcmd(['git', 'cat-file', 'blob',
'%s:%s' % (ref, filename)])
+ def _clone_into(self, target_dir, ref): #pragma: no cover
+ '''Actually perform the clone'''
+ try:
+ morphlib.git.clone_into(self._runcmd, self.path, target_dir, ref)
+ except cliapp.AppException:
+ raise CloneError(self, target_dir)
+
def _copy_repository(self, source_dir, target_dir): # pragma: no cover
try:
morphlib.git.copy_repository(self._runcmd, source_dir, target_dir)
except cliapp.AppException:
- raise CloneError(self, target_dir)
+ raise CopyError(self, target_dir)
def _checkout_ref(self, ref, target_dir): # pragma: no cover
try:
@@ -232,7 +268,7 @@ class CachedRepo(object):
raise CheckoutError(self, ref, target_dir)
def _update(self): # pragma: no cover
- self._runcmd(['git', 'remote', 'update', 'origin'])
+ self._runcmd(['git', 'remote', 'update', 'origin', '--prune'])
def __str__(self): # pragma: no cover
return self.url