Просмотр исходного кода

Merge fix for cloning repositories without HEAD set.

Jelmer Vernooij 12 лет назад
Родитель
Сommit
25250c1694
3 измененных файлов с 29 добавлено и 8 удалено
  1. 3 0
      NEWS
  2. 9 8
      dulwich/repo.py
  3. 17 0
      dulwich/tests/test_repository.py

+ 3 - 0
NEWS

@@ -30,6 +30,9 @@
 
   * Handle None elements in lists of TreeChange objects. (Alex Holmes)
 
+  * Support cloning repositories without HEAD set.
+    (D-Key, Jelmer Vernooij, issue #69)
+
 0.8.5	2012-03-29
 
  BUG FIXES

+ 9 - 8
dulwich/repo.py

@@ -1383,14 +1383,15 @@ class Repo(BaseRepo):
 
         # Update target head
         head, head_sha = self.refs._follow('HEAD')
-        target.refs.set_symbolic_ref('HEAD', head)
-        target['HEAD'] = head_sha
-
-        if not bare:
-            # Checkout HEAD to target dir
-            from dulwich.index import build_index_from_tree
-            build_index_from_tree(target.path, target.index_path(),
-                    target.object_store, target['HEAD'].tree)
+        if head is not None and head_sha is not None:
+            target.refs.set_symbolic_ref('HEAD', head)
+            target['HEAD'] = head_sha
+
+            if not bare:
+                # Checkout HEAD to target dir
+                from dulwich.index import build_index_from_tree
+                build_index_from_tree(target.path, target.index_path(),
+                        target.object_store, target['HEAD'].tree)
 
         return target
 

+ 17 - 0
dulwich/tests/test_repository.py

@@ -286,6 +286,23 @@ class RepositoryTests(TestCase):
         self.assertEqual(shas, [t.head(),
                          '2a72d929692c41d8554c07f6301757ba18a65d91'])
 
+    def test_clone_no_head(self):
+        temp_dir = tempfile.mkdtemp()
+        self.addCleanup(shutil.rmtree, temp_dir)
+        repo_dir = os.path.join(os.path.dirname(__file__), 'data', 'repos')
+        dest_dir = os.path.join(temp_dir, 'a.git')
+        shutil.copytree(os.path.join(repo_dir, 'a.git'),
+                        dest_dir, symlinks=True)
+        r = Repo(dest_dir)
+        del r.refs["refs/heads/master"]
+        del r.refs["HEAD"]
+        t = r.clone(os.path.join(temp_dir, 'b.git'), mkdir=True)
+        self.assertEqual({
+            'refs/tags/mytag': '28237f4dc30d0d462658d6b937b08a0f0b6ef55a',
+            'refs/tags/mytag-packed':
+                'b0931cadc54336e78a1d980420e3268903b57a50',
+            }, t.refs.as_dict())
+
     def test_merge_history(self):
         r = self._repo = open_repo('simple_merge.git')
         shas = [e.commit.id for e in r.get_walker()]