diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2015-01-06 17:56:21 +0000 |
---|---|---|
committer | Adam Coldrick <adam.coldrick@codethink.co.uk> | 2015-02-18 10:31:43 +0000 |
commit | 0c9a8f3bcd13428bdde3fd7bf7201818aec554f0 (patch) | |
tree | 3ddbdfd7e1b4550d1179a8302ca37b343bfb5d97 /morphlib/cachedrepo.py | |
parent | a9dd06285bcd3ed40cb333d8fc7f65e6f273ad88 (diff) | |
download | morph-0c9a8f3bcd13428bdde3fd7bf7201818aec554f0.tar.gz |
Add a mechanism for extracting all files from a given commit to a dir
This is nice because it's fast. We don't have to copy all the Git
history along with it like we do with a clone. And it doesn't touch
any files in the cached repo.
Diffstat (limited to 'morphlib/cachedrepo.py')
-rw-r--r-- | morphlib/cachedrepo.py | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/morphlib/cachedrepo.py b/morphlib/cachedrepo.py index aa2b5af1..8b38c5c9 100644 --- a/morphlib/cachedrepo.py +++ b/morphlib/cachedrepo.py @@ -1,4 +1,4 @@ -# Copyright (C) 2012-2014 Codethink Limited +# Copyright (C) 2012-2015 Codethink Limited # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -15,7 +15,9 @@ import cliapp + import os +import tempfile import morphlib @@ -169,6 +171,28 @@ class CachedRepo(object): self._checkout_ref_in_clone(ref, target_dir) + def extract_commit(self, ref, target_dir): + '''Extract files from a given commit into target_dir. + + This is different to a 'checkout': a checkout assumes a working tree + associated with a repository. Here, the repository is immutable (it's + in the cache) and we just want to look at the files in a quick way + (quicker than going 'git cat-file everything'). + + This seems marginally quicker than doing a shallow clone. Running + `morph list-artifacts` 10 times gave an average time of 1.334s + using `git clone --depth 1` and an average time of 1.261s using + this code. + + ''' + if not os.path.exists(target_dir): + os.makedirs(target_dir) + + with tempfile.NamedTemporaryFile() as index_file: + index = self._gitdir.get_index(index_file=index_file.name) + index.set_to_tree(ref) + index.checkout(working_tree=target_dir) + def requires_update_for_ref(self, ref): '''Returns False if there's no need to update this cached repo. |