diff options
author | Richard Maw <richard.maw@codethink.co.uk> | 2013-09-03 14:15:08 +0000 |
---|---|---|
committer | Richard Maw <richard.maw@codethink.co.uk> | 2013-09-05 14:03:41 +0000 |
commit | cb842f99895da98a9696cfa770d42e8324c9fdb4 (patch) | |
tree | c2de4c84e92857ae5e46a9cca515b093994a46d2 /morphlib | |
parent | b16e99a71980e9f10ff719d749ad8e453704ea83 (diff) | |
download | morph-cb842f99895da98a9696cfa770d42e8324c9fdb4.tar.gz |
gitdir: Add methods to inspect contents
This adds methods to list and read files. The difference between doing
this to a commit and the currrent working tree is abstracted over by
whether the passed ref is None or omitted.
Diffstat (limited to 'morphlib')
-rw-r--r-- | morphlib/gitdir.py | 69 | ||||
-rw-r--r-- | morphlib/gitdir_tests.py | 73 |
2 files changed, 142 insertions, 0 deletions
diff --git a/morphlib/gitdir.py b/morphlib/gitdir.py index f40190ff..cb247303 100644 --- a/morphlib/gitdir.py +++ b/morphlib/gitdir.py @@ -17,10 +17,27 @@ import cliapp +import glob +import os import morphlib +class NoWorkingTreeError(cliapp.AppException): + + def __init__(self, repo): + cliapp.AppException.__init__( + self, 'Git directory %s has no working tree ' + '(is bare).' % repo.dirname) + + +class InvalidRefError(cliapp.AppException): + def __init__(self, repo, ref): + cliapp.AppException.__init__( + self, 'Git directory %s has no commit ' + 'at ref %s.' %(repo.dirname, ref)) + + class GitDirectory(object): '''Represent a git working tree + .git directory. @@ -126,6 +143,58 @@ class GitDirectory(object): '''Run "git remote update --prune".''' self._runcmd(['git', 'remote', 'update', '--prune']) + def is_bare(self): + '''Determine whether the repository has no work tree (is bare)''' + return self.get_config('core.bare') == 'true' + + def list_files(self, ref=None): + '''Return an iterable of the files in the repository. + + If `ref` is specified, list files at that ref, otherwise + use the working tree. + + If this is a bare repository and no ref is specified, raises + an exception. + + ''' + if ref is None and self.is_bare(): + raise NoWorkingTreeError(self) + if ref is None: + return self._list_files_in_work_tree() + else: + return self._list_files_in_ref(ref) + + def _rev_parse_tree(self, ref): + try: + return self._runcmd(['git', 'rev-parse', '--verify', + '%s^{tree}' % ref]).strip() + except cliapp.AppException as e: + raise InvalidRefError(self, ref) + + def _list_files_in_work_tree(self): + for dirpath, subdirs, filenames in os.walk(self.dirname): + if dirpath == self.dirname and '.git' in subdirs: + subdirs.remove('.git') + for filename in filenames: + yield os.path.join(dirpath, filename)[len(self.dirname)+1:] + + def _list_files_in_ref(self, ref): + tree = self._rev_parse_tree(ref) + output = self._runcmd(['git', 'ls-tree', '--name-only', '-rz', tree]) + # ls-tree appends \0 instead of interspersing, so we need to + # strip the trailing \0 before splitting + paths = output.strip('\0').split('\0') + return paths + + def read_file(self, filename, ref=None): + if ref is None and self.is_bare(): + raise NoWorkingTreeError(self) + if ref is None: + with open(os.path.join(self.dirname, filename)) as f: + return f.read() + tree = self._rev_parse_tree(ref) + return self.cat_file('blob', tree, filename) + def init(dirname): '''Initialise a new git repository.''' diff --git a/morphlib/gitdir_tests.py b/morphlib/gitdir_tests.py index 2494981a..175b8ee7 100644 --- a/morphlib/gitdir_tests.py +++ b/morphlib/gitdir_tests.py @@ -64,3 +64,76 @@ class GitDirectoryTests(unittest.TestCase): gitdir.set_remote_fetch_url('origin', url) self.assertEqual(gitdir.get_remote_fetch_url('origin'), url) +class GitDirectoryContentsTests(unittest.TestCase): + + def setUp(self): + self.tempdir = tempfile.mkdtemp() + self.dirname = os.path.join(self.tempdir, 'foo') + os.mkdir(self.dirname) + gd = morphlib.gitdir.init(self.dirname) + for fn in ('foo', 'bar.morph', 'baz.morph', 'quux'): + with open(os.path.join(self.dirname, fn), "w") as f: + f.write('dummy morphology text') + gd._runcmd(['git', 'add', '.']) + gd._runcmd(['git', 'commit', '-m', 'Initial commit']) + os.rename(os.path.join(self.dirname, 'foo'), + os.path.join(self.dirname, 'foo.morph')) + self.mirror = os.path.join(self.tempdir, 'mirror') + gd._runcmd(['git', 'clone', '--mirror', self.dirname, self.mirror]) + + def tearDown(self): + shutil.rmtree(self.tempdir) + + def test_lists_files_in_work_tree(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + self.assertEqual(sorted(gd.list_files()), + ['bar.morph', 'baz.morph', 'foo.morph', 'quux']) + + def test_read_file_in_work_tree(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + self.assertEqual(gd.read_file('bar.morph'), + 'dummy morphology text') + + def test_list_raises_no_ref_no_work_tree(self): + gd = morphlib.gitdir.GitDirectory(self.mirror) + self.assertRaises(morphlib.gitdir.NoWorkingTreeError, + gd.list_files) + + def test_read_raises_no_ref_no_work_tree(self): + gd = morphlib.gitdir.GitDirectory(self.mirror) + self.assertRaises(morphlib.gitdir.NoWorkingTreeError, + gd.read_file, 'bar.morph') + + def test_lists_files_in_HEAD(self): + for gitdir in (self.dirname, self.mirror): + gd = morphlib.gitdir.GitDirectory(gitdir) + self.assertEqual(sorted(gd.list_files('HEAD')), + ['bar.morph', 'baz.morph', 'foo', 'quux']) + + def test_read_files_in_HEAD(self): + for gitdir in (self.dirname, self.mirror): + gd = morphlib.gitdir.GitDirectory(gitdir) + self.assertEqual(gd.read_file('bar.morph', 'HEAD'), + 'dummy morphology text') + + def test_lists_files_in_named_ref(self): + for gitdir in (self.dirname, self.mirror): + gd = morphlib.gitdir.GitDirectory(gitdir) + self.assertEqual(sorted(gd.list_files('master')), + ['bar.morph', 'baz.morph', 'foo', 'quux']) + + def test_read_file_in_named_ref(self): + for gitdir in (self.dirname, self.mirror): + gd = morphlib.gitdir.GitDirectory(gitdir) + self.assertEqual(gd.read_file('bar.morph', 'master'), + 'dummy morphology text') + + def test_list_raises_invalid_ref(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + self.assertRaises(morphlib.gitdir.InvalidRefError, + gd.list_files, 'no-such-ref') + + def test_read_raises_invalid_ref(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + self.assertRaises(morphlib.gitdir.InvalidRefError, + gd.read_file, 'bar', 'no-such-ref') |