summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Maw <richard.maw@codethink.co.uk>2014-06-20 14:37:30 +0000
committerRichard Maw <richard.maw@codethink.co.uk>2014-06-20 14:41:12 +0000
commitec7076974dd667d1b85711ade9a7a07986714d62 (patch)
treec513eeb7e75480328ad677f8eeb90bdcda00821a
parent7b721a81d6fe695f1bae86d43d65d04c971707e7 (diff)
downloadmorph-ec7076974dd667d1b85711ade9a7a07986714d62.tar.gz
Don't attempt to load symlinks as morphologies
This was noticed because our definitions.git had a dangling symlink, so it failed to construct the temporary build branch. We shouldn't process symlinks as morphologies either, since either we make symlinked morphologies a first-class behaviour, and validate that the link points inside the repository, which is a lot of work for something we don't and probably won't need; or we can just assume that we deal with the morphology this is linked to correctly anyway.
-rw-r--r--morphlib/gitdir.py10
-rw-r--r--morphlib/gitdir_tests.py37
-rw-r--r--morphlib/sysbranchdir.py2
3 files changed, 48 insertions, 1 deletions
diff --git a/morphlib/gitdir.py b/morphlib/gitdir.py
index 8f6d69d7..5b0693cb 100644
--- a/morphlib/gitdir.py
+++ b/morphlib/gitdir.py
@@ -515,6 +515,16 @@ class GitDirectory(object):
tree = self.resolve_ref_to_tree(ref)
return self.get_file_from_ref(tree, filename)
+ def is_symlink(self, filename, ref=None):
+ if ref is None and self.is_bare():
+ raise NoWorkingTreeError(self)
+ if ref is None:
+ filepath = os.path.join(self.dirname, filename.lstrip('/'))
+ return os.path.islink(filepath)
+ tree_entry = self._runcmd(['git', 'ls-tree', ref, filename])
+ file_mode = tree_entry.split(' ', 1)[0]
+ return file_mode == '120000'
+
@property
def HEAD(self):
output = self._runcmd(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
diff --git a/morphlib/gitdir_tests.py b/morphlib/gitdir_tests.py
index 14b2a57a..b3b4a8ab 100644
--- a/morphlib/gitdir_tests.py
+++ b/morphlib/gitdir_tests.py
@@ -216,6 +216,43 @@ class GitDirectoryContentsTests(unittest.TestCase):
self.assertEqual(gd.describe(), 'example')
+class GitDirectoryFileTypeTests(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)
+ with open(os.path.join(self.dirname, 'file'), "w") as f:
+ f.write('dummy morphology text')
+ os.symlink('file', os.path.join(self.dirname, 'link'))
+ os.symlink('no file', os.path.join(self.dirname, 'broken'))
+ gd._runcmd(['git', 'add', '.'])
+ gd._runcmd(['git', 'commit', '-m', 'Initial commit'])
+ 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_working_tree_symlinks(self):
+ gd = morphlib.gitdir.GitDirectory(self.dirname)
+ self.assertTrue(gd.is_symlink('link'))
+ self.assertTrue(gd.is_symlink('broken'))
+ self.assertFalse(gd.is_symlink('file'))
+
+ def test_bare_symlinks(self):
+ gd = morphlib.gitdir.GitDirectory(self.mirror)
+ self.assertTrue(gd.is_symlink('link', 'HEAD'))
+ self.assertTrue(gd.is_symlink('broken', 'HEAD'))
+ self.assertFalse(gd.is_symlink('file', 'HEAD'))
+
+ def test_is_symlink_raises_no_ref_no_work_tree(self):
+ gd = morphlib.gitdir.GitDirectory(self.mirror)
+ self.assertRaises(morphlib.gitdir.NoWorkingTreeError,
+ gd.is_symlink, 'file')
+
+
class GitDirectoryRefTwiddlingTests(unittest.TestCase):
def setUp(self):
diff --git a/morphlib/sysbranchdir.py b/morphlib/sysbranchdir.py
index 8546d726..d82390c7 100644
--- a/morphlib/sysbranchdir.py
+++ b/morphlib/sysbranchdir.py
@@ -175,7 +175,7 @@ class SystemBranchDirectory(object):
def load_all_morphologies(self, loader): # pragma: no cover
gd_name = self.get_git_directory_name(self.root_repository_url)
gd = morphlib.gitdir.GitDirectory(gd_name)
- for filename in gd.list_files():
+ for filename in (f for f in gd.list_files() if not gd.is_symlink(f)):
text = gd.read_file(filename)
try:
m = loader.load_from_string(text, filename=filename)