Add init support to porcelain.
authorJelmer Vernooij <jelmer@samba.org>
Sun, 29 Sep 2013 01:09:05 +0000 (02:09 +0100)
committerJelmer Vernooij <jelmer@samba.org>
Sun, 29 Sep 2013 01:09:05 +0000 (02:09 +0100)
bin/dulwich
dulwich/porcelain.py
dulwich/tests/test_porcelain.py

index e3d509b79ba66ec8abd52a8fdd0589bff12bc6cf..953315752909cc9d00b5dd0ade91866e306ae8cf 100755 (executable)
@@ -36,6 +36,7 @@ from dulwich.index import Index
 from dulwich.porcelain import (
     archive,
     commit,
+    init,
     update_server_info,
     )
 from dulwich.pack import Pack, sha_to_hex
@@ -164,13 +165,7 @@ def cmd_init(args):
     else:
         path = args[0]
 
-    if not os.path.exists(path):
-        os.mkdir(path)
-
-    if "--bare" in opts:
-        Repo.init_bare(path)
-    else:
-        Repo.init(path)
+    init(path, bare=("--bare" in opts))
 
 
 def cmd_clone(args):
index 5ab1b0cef88ffd901747f4d40c8117ac2923a85a..c9b2a9e11ad0d6c8a737e1fe440b57a3931cafff 100644 (file)
@@ -16,6 +16,7 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 # MA  02110-1301, USA.
 
+import os
 import sys
 
 from dulwich.client import get_transport_and_path
@@ -67,3 +68,18 @@ def commit(path=".", message=None):
     # FIXME: Support --signoff argument
     r = Repo(path)
     r.do_commit(message=message)
+
+
+def init(path=".", bare=False):
+    """Create a new git repository.
+
+    :param path: Path to repository.
+    :param bare: Whether to create a bare repository.
+    """
+    if not os.path.exists(path):
+        os.mkdir(path)
+
+    if bare:
+        Repo.init_bare(path)
+    else:
+        Repo.init(path)
index f8ade98ae74159817d58ea92c489b3877a8ea583..e7852f48c16820a847b6d07fabc4b861b3f1fb27 100644 (file)
@@ -27,6 +27,7 @@ import tempfile
 from dulwich.porcelain import (
     archive,
     commit,
+    init,
     update_server_info,
     )
 from dulwich.repo import Repo
@@ -81,3 +82,16 @@ class CommitTests(PorcelainTestCase):
         c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
         self.repo.refs["refs/heads/foo"] = c3.id
         commit(self.repo.path, message="Some message")
+
+
+class CommitTests(TestCase):
+
+    def test_non_bare(self):
+        repo_dir = tempfile.mkdtemp()
+        self.addCleanup(shutil.rmtree, repo_dir)
+        init(repo_dir)
+
+    def test_bare(self):
+        repo_dir = tempfile.mkdtemp()
+        self.addCleanup(shutil.rmtree, repo_dir)
+        init(repo_dir, bare=True)