Make create_repo a @classmethod
authorGuido Günther <agx@sigxcpu.org>
Fri, 21 Oct 2011 19:05:05 +0000 (21:05 +0200)
committerGuido Günther <agx@sigxcpu.org>
Sat, 22 Oct 2011 13:33:40 +0000 (15:33 +0200)
and GitInit superfluous

Git-Dch: Ignore

gbp/command_wrappers.py
gbp/git.py
git-import-dsc
tests/03_test_gbp_branch.py
tests/04_test_gbp_submodules.py

index dd7bc4c..3488575 100644 (file)
@@ -231,14 +231,6 @@ class GitCommand(Command):
 
 
 # FIXME: move to gbp.git.__init__
-class GitInit(GitCommand):
-    """Wrap git init"""
-    def __init__(self):
-        GitCommand.__init__(self, 'init')
-        self.run_error = "Couldn't init git repository"
-
-
-# FIXME: move to gbp.git.__init__
 class GitClone(GitCommand):
     """Wrap git clone"""
     def __init__(self):
index 19f63bc..6499033 100644 (file)
@@ -19,7 +19,7 @@
 import re
 import subprocess
 import os.path
-from command_wrappers import (GitCommand, GitInit, GitAdd, GitBranch, copy_from)
+from command_wrappers import (GitCommand, GitAdd, GitBranch, copy_from)
 from errors import GbpError
 import log
 import dateutil.parser
@@ -583,6 +583,23 @@ class GitRepository(object):
                                                       recursive=recursive)
         return submodules
 
+    @classmethod
+    def create(klass, path, description=None):
+        """create a repository at path"""
+        abspath = os.path.abspath(path)
+        try:
+            if not os.path.exists(abspath):
+                os.makedirs(abspath)
+            GitCommand("init", cwd=abspath)()
+            if description:
+                with file(os.path.join(abspath, ".git", "description"), 'w') as f:
+                    description += '\n' if description[-1] != '\n' else ''
+                    f.write(description)
+            return klass(abspath)
+        except OSError, err:
+            raise GitRepositoryError, "Cannot create Git repository at %s: %s " % (abspath, err[1])
+        return None
+
 
 class FastImport(object):
     """Invoke git-fast-import"""
@@ -649,22 +666,6 @@ from refs/heads/%(branch)s^0
         self.close()
 
 
-def create_repo(path):
-    """create a repository at path"""
-    abspath = os.path.abspath(path)
-    pwd = os.path.abspath(os.curdir)
-    try:
-        os.makedirs(abspath)
-        os.chdir(abspath)
-        GitInit()()
-        return GitRepository(abspath)
-    except OSError, err:
-        raise GitRepositoryError, "Cannot create Git repository at %s: %s " % (path, err[1])
-    finally:
-        os.chdir(pwd)
-    return None
-
-
 def build_tag(format, version):
     """Generate a tag from a given format and a version
 
index 16a9771..0332e42 100755 (executable)
@@ -29,7 +29,7 @@ from email.Utils import parseaddr
 import gbp.command_wrappers as gbpc
 from gbp.deb import (debian_version_chars, parse_changelog,
                      parse_dsc, DscFile, UpstreamSource)
-from gbp.git import (build_tag, create_repo, GitRepository,
+from gbp.git import (build_tag, GitRepository,
                      GitRepositoryError, rfc822_date_to_git)
 from gbp.config import GbpOptionParser, GbpOptionGroup, no_upstream_branch_msg
 from gbp.errors import GbpError
@@ -246,7 +246,7 @@ def main(argv):
 
             if needs_repo:
                 gbp.log.info("No git repository found, creating one.")
-                repo = create_repo(src.pkg)
+                repo = GitRepository.create(src.pkg)
                 os.chdir(repo.path)
 
             dirs['tmp'] = os.path.abspath(tempfile.mkdtemp(dir='..'))
index 9c3d936..a305697 100644 (file)
@@ -16,7 +16,7 @@ def setup():
 
     top = os.path.abspath(os.curdir)
     repo_dir = os.path.join(top, 'gbp_%s_test_repo' % __name__)
-    repo = gbp.git.create_repo(repo_dir)
+    repo = gbp.git.GitRepository.create(repo_dir)
     os.chdir(repo_dir)
 
 
index 7a4dfa9..af44c07 100644 (file)
@@ -23,7 +23,7 @@ class Submodule(object):
     def __init__(self, name, tmpdir):
         self.name = name
         self.dir = os.path.join(tmpdir, name)
-        self.repo = gbp.git.create_repo(self.dir)
+        self.repo = gbp.git.GitRepository.create(self.dir)
 
 
 def setup():
@@ -34,7 +34,7 @@ def setup():
     os.mkdir(tmpdir)
 
     repodir = os.path.join(tmpdir, 'test_repo')
-    repo = gbp.git.create_repo(repodir)
+    repo = gbp.git.GitRepository.create(repodir)
 
     for name in submodule_names:
         submodules.append(Submodule(name, tmpdir))