Browse Source

Always create head, even if not checking out.

Jelmer Vernooij 7 years ago
parent
commit
356e0dd20a
2 changed files with 18 additions and 14 deletions
  1. 11 11
      dulwich/porcelain.py
  2. 7 3
      dulwich/tests/test_porcelain.py

+ 11 - 11
dulwich/porcelain.py

@@ -325,17 +325,17 @@ def clone(source, target=None, bare=False, checkout=None,
             (b'remote', origin), b'fetch',
             b'+refs/heads/*:refs/remotes/' + origin + b'/*')
         target_config.write_to_path()
-        if checkout and not bare:
-            # TODO(jelmer): Support symref capability,
-            # https://github.com/jelmer/dulwich/issues/485
-            try:
-                head = r[remote_refs[b"HEAD"]]
-            except KeyError:
-                pass
-            else:
-                r[b'HEAD'] = head.id
-                errstream.write(b'Checking out ' + head.id + b'\n')
-                r.reset_index(head.tree)
+        # TODO(jelmer): Support symref capability,
+        # https://github.com/jelmer/dulwich/issues/485
+        try:
+            head = r[remote_refs[b"HEAD"]]
+        except KeyError:
+            head = None
+        else:
+            r[b'HEAD'] = head.id
+        if checkout and not bare and head is not None:
+            errstream.write(b'Checking out ' + head.id + b'\n')
+            r.reset_index(head.tree)
     except BaseException:
         r.close()
         raise

+ 7 - 3
dulwich/tests/test_porcelain.py

@@ -39,7 +39,10 @@ from dulwich.objects import (
     Tree,
     ZERO_SHA,
     )
-from dulwich.repo import Repo
+from dulwich.repo import (
+    NoIndexPresent,
+    Repo,
+    )
 from dulwich.tests import (
     TestCase,
     )
@@ -122,7 +125,7 @@ class CloneTests(PorcelainTestCase):
                             checkout=False, errstream=errstream)
         self.assertEqual(r.path, target_path)
         target_repo = Repo(target_path)
-        self.assertRaises(KeyError, target_repo.head)
+        self.assertEqual(0, len(target_repo.open_index()))
         self.assertEqual(c3.id, target_repo.refs[b'refs/tags/foo'])
         self.assertTrue(b'f1' not in os.listdir(target_path))
         self.assertTrue(b'f2' not in os.listdir(target_path))
@@ -175,7 +178,8 @@ class CloneTests(PorcelainTestCase):
                 errstream=errstream) as r:
             self.assertEqual(r.path, target_path)
         with Repo(target_path) as r:
-            self.assertRaises(KeyError, r.head)
+            r.head()
+            self.assertRaises(NoIndexPresent, r.open_index)
         self.assertFalse(b'f1' in os.listdir(target_path))
         self.assertFalse(b'f2' in os.listdir(target_path))