Explorar o código

Add checkout. checkout master on clone

Note: Only supports checking out master on a fresh clone
milki %!s(int64=13) %!d(string=hai) anos
pai
achega
48d91cec0b
Modificáronse 1 ficheiros con 45 adicións e 0 borrados
  1. 45 0
      dulwich/repo.py

+ 45 - 0
dulwich/repo.py

@@ -1355,8 +1355,53 @@ class Repo(BaseRepo):
                 self.refs['refs/heads/master'])
         except KeyError:
             pass
+
+        target.checkout()
         return target
 
+    def checkout(self, branch='refs/heads/master'):
+        """Checkout a branch.
+
+        This current only supports checking out master on fresh clones.
+
+        :param branch: Branch to checkout
+        """
+
+        if self.bare:
+            raise # Cannot checkout in a bare repo
+
+        from dulwich.index import (
+                Index,
+                index_entry_from_stat
+                )
+        import stat
+
+        try:
+            index = self.open_index()
+            index.clear() # FIXME: When switching branches, merge working dir
+        except NoIndexPresent:
+            index = Index(self.index_path())
+
+        tree_id = self[branch].tree
+
+        # FIXME: When switching branches, merge working dir
+        for entry in self.object_store.iter_tree_contents(tree_id):
+            full_path = os.path.join(self.path, entry.path)
+
+            if not os.path.exists( os.path.dirname(full_path) ):
+                os.makedirs(os.path.dirname(full_path))
+
+            with open(full_path, 'wb') as file:
+                # Write out file
+                file.write(self.get_object(entry.sha).as_raw_string()) 
+                os.chmod(full_path, entry.mode)
+
+                # Add file to index
+                st = os.stat(full_path)
+                index[entry.path] = index_entry_from_stat(st, entry.sha, entry.mode)
+
+        index.write()
+
     def __repr__(self):
         return "<Repo at %r>" % self.path