瀏覽代碼

Move named file initilization to BaseRepo.

BaseRepo now supports both getting (abstractly) putting the contents of named
files.

Change-Id: I657ddc122eca5f363b101182c34e4a222b6e072c
Dave Borowitz 15 年之前
父節點
當前提交
20f3afcdb8
共有 2 個文件被更改,包括 27 次插入9 次删除
  1. 4 0
      NEWS
  2. 23 9
      dulwich/repo.py

+ 4 - 0
NEWS

@@ -4,6 +4,10 @@
 
   * Fix memory leak in C implementation of sorted_tree_items. (Dave Borowitz)
 
+ FEATURES
+
+  * Move named file initilization to BaseRepo. (Dave Borowitz)
+
  TESTS
 
   * Add tests for sorted_tree_items and C implementation. (Dave Borowitz)

+ 23 - 9
dulwich/repo.py

@@ -750,6 +750,16 @@ class BaseRepo(object):
         self.object_store = object_store
         self.refs = refs
 
+    def _init_files(self):
+        """Initialize a default set of named files."""
+        self._put_named_file('description', "Unnamed repository")
+        self._put_named_file('config', ('[core]\n'
+                                        'repositoryformatversion = 0\n'
+                                        'filemode = true\n'
+                                        'bare = false\n'
+                                        'logallrefupdates = true\n'))
+        self._put_named_file('info/exclude', '')
+
     def get_named_file(self, path):
         """Get a file from the control dir with a specific name.
 
@@ -762,6 +772,14 @@ class BaseRepo(object):
         """
         raise NotImplementedError(self.get_named_file)
 
+    def _put_named_file(self, path, contents):
+        """Write a file to the control dir with the given name and contents.
+
+        :param path: The path to the file, relative to the control dir.
+        :contents: A string to write to the file.
+        """
+        raise NotImplementedError(self._put_named_file)
+
     def open_index(self):
         """Open the index for this repository.
 
@@ -1072,7 +1090,10 @@ class Repo(BaseRepo):
         return self._controldir
 
     def _put_named_file(self, path, contents):
-        """Write a file from the control dir with a specific name and contents.
+        """Write a file to the control dir with the given name and contents.
+
+        :param path: The path to the file, relative to the control dir.
+        :contents: A string to write to the file.
         """
         f = GitFile(os.path.join(self.controldir(), path), 'wb')
         try:
@@ -1162,14 +1183,7 @@ class Repo(BaseRepo):
         DiskObjectStore.init(os.path.join(path, OBJECTDIR))
         ret = cls(path)
         ret.refs.set_symbolic_ref("HEAD", "refs/heads/master")
-        ret._put_named_file('description', "Unnamed repository")
-        ret._put_named_file('config', """[core]
-    repositoryformatversion = 0
-    filemode = true
-    bare = false
-    logallrefupdates = true
-""")
-        ret._put_named_file(os.path.join('info', 'exclude'), '')
+        ret._init_files()
         return ret
 
     create = init_bare