Use correct path separators for named repo files.
authorDave Borowitz <dborowitz@google.com>
Thu, 13 May 2010 00:47:46 +0000 (17:47 -0700)
committerDave Borowitz <dborowitz@google.com>
Tue, 1 Jun 2010 22:33:53 +0000 (15:33 -0700)
Change-Id: I79175078920ef177006bbd323c3d63232f3060b8

NEWS
dulwich/repo.py
dulwich/tests/test_repository.py
dulwich/web.py

diff --git a/NEWS b/NEWS
index 72cf790553857439b4e9b5fa616edf532a1df734..4681e9f947fac16a6ab131e34931242dcf3008ed 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,8 @@
 
   * Fix memory leak in C implementation of sorted_tree_items. (Dave Borowitz)
 
+  * Use correct path separators for named repo files. (Dave Borowitz)
+
  FEATURES
 
   * Move named file initilization to BaseRepo. (Dave Borowitz)
index ffda6d63094a500667f36bab343e02e0577a9ef6..322d776b9024544c02256a54cdf3c2e16a3c5401 100644 (file)
@@ -758,7 +758,7 @@ class BaseRepo(object):
                                         'filemode = true\n'
                                         'bare = false\n'
                                         'logallrefupdates = true\n'))
-        self._put_named_file('info/exclude', '')
+        self._put_named_file(os.path.join('info', 'exclude'), '')
 
     def get_named_file(self, path):
         """Get a file from the control dir with a specific name.
@@ -1095,6 +1095,7 @@ class Repo(BaseRepo):
         :param path: The path to the file, relative to the control dir.
         :contents: A string to write to the file.
         """
+        path = path.lstrip(os.path.sep)
         f = GitFile(os.path.join(self.controldir(), path), 'wb')
         try:
             f.write(contents)
@@ -1111,8 +1112,11 @@ class Repo(BaseRepo):
         :param path: The path to the file, relative to the control dir.
         :return: An open file object, or None if the file does not exist.
         """
+        # TODO(dborowitz): sanitize filenames, since this is used directly by
+        # the dumb web serving code.
+        path = path.lstrip(os.path.sep)
         try:
-            return open(os.path.join(self.controldir(), path.lstrip('/')), 'rb')
+            return open(os.path.join(self.controldir(), path), 'rb')
         except (IOError, OSError), e:
             if e.errno == errno.ENOENT:
                 return None
index ee7d861754d77af0b37a68911ed523046a678df2..423d3b1a43e6908c8c5757ecc31f6129b724b1ef 100644 (file)
@@ -50,11 +50,28 @@ missing_sha = 'b91fa4d900e17e99b433218e988c4eb4a3e9a097'
 
 class CreateRepositoryTests(unittest.TestCase):
 
-    def test_create(self):
+    def assertFileContentsEqual(self, expected, repo, path):
+        f = repo.get_named_file(path)
+        if not f:
+            self.assertEqual(expected, None)
+        else:
+            try:
+                self.assertEqual(expected, f.read())
+            finally:
+                f.close()
+
+    def _check_repo_contents(self, repo):
+        self.assertTrue(repo.bare)
+        self.assertFileContentsEqual('Unnamed repository', repo, 'description')
+        self.assertFileContentsEqual('', repo, os.path.join('info', 'exclude'))
+        self.assertFileContentsEqual(None, repo, 'nonexistent file')
+
+    def test_create_disk(self):
         tmp_dir = tempfile.mkdtemp()
         try:
             repo = Repo.init_bare(tmp_dir)
             self.assertEquals(tmp_dir, repo._controldir)
+            self._check_repo_contents(repo)
         finally:
             shutil.rmtree(tmp_dir)
 
index 380d5073c4068e1df935f67c9dac97bef3dc53a1..a9776cabdef8e7ad8d881b250c6952870ad4aec4 100644 (file)
@@ -19,6 +19,7 @@
 """HTTP server for dulwich that implements the git smart HTTP protocol."""
 
 from cStringIO import StringIO
+import os
 import re
 import time
 
@@ -98,9 +99,14 @@ def send_file(req, f, content_type):
         raise
 
 
+def _url_to_path(url):
+    return url.replace('/', os.path.sep)
+
+
 def get_text_file(req, backend, mat):
     req.nocache()
-    return send_file(req, get_repo(backend, mat).get_named_file(mat.group()),
+    path = _url_to_path(mat.group())
+    return send_file(req, get_repo(backend, mat).get_named_file(path),
                      'text/plain')
 
 
@@ -121,13 +127,15 @@ def get_loose_object(req, backend, mat):
 
 def get_pack_file(req, backend, mat):
     req.cache_forever()
-    return send_file(req, get_repo(backend, mat).get_named_file(mat.group()),
+    path = _url_to_path(mat.group())
+    return send_file(req, get_repo(backend, mat).get_named_file(path),
                      'application/x-git-packed-objects')
 
 
 def get_idx_file(req, backend, mat):
     req.cache_forever()
-    return send_file(req, get_repo(backend, mat).get_named_file(mat.group()),
+    path = _url_to_path(mat.group())
+    return send_file(req, get_repo(backend, mat).get_named_file(path),
                      'application/x-git-packed-objects-toc')