aboutsummaryrefslogtreecommitdiff
path: root/gitosis
diff options
context:
space:
mode:
authorTommi Virtanen <tv@eagain.net>2007-08-30 20:00:15 -0700
committerTommi Virtanen <tv@eagain.net>2007-08-30 20:03:17 -0700
commit5655b7f517a46fc1177a3e9007ab56b36a6d8e0c (patch)
tree07305ea591f10edd80f4ccc9f5c85e1e9d22d24e /gitosis
parentRefactor unit test writeFile/readFile helpers. (diff)
downloadgitosis-dakkar-5655b7f517a46fc1177a3e9007ab56b36a6d8e0c.tar.gz
gitosis-dakkar-5655b7f517a46fc1177a3e9007ab56b36a6d8e0c.tar.bz2
gitosis-dakkar-5655b7f517a46fc1177a3e9007ab56b36a6d8e0c.zip
Add helper to create (bare) repositories with custom templates.
Diffstat (limited to 'gitosis')
-rw-r--r--gitosis/repository.py39
-rwxr-xr-xgitosis/test/mocktemplates/hooks/post-update2
-rw-r--r--gitosis/test/mocktemplates/no-confusion1
-rw-r--r--gitosis/test/test_repository.py63
4 files changed, 105 insertions, 0 deletions
diff --git a/gitosis/repository.py b/gitosis/repository.py
new file mode 100644
index 0000000..ea8db54
--- /dev/null
+++ b/gitosis/repository.py
@@ -0,0 +1,39 @@
+import errno
+import os
+import subprocess
+
+def _mkdir(*a, **kw):
+ try:
+ os.mkdir(*a, **kw)
+ except OSError, e:
+ if e.errno == errno.EEXIST:
+ pass
+ else:
+ raise
+
+def init(
+ path,
+ template=None,
+ _git=None,
+ ):
+ if _git is None:
+ _git = 'git'
+
+ _mkdir(path, 0750)
+ args = [_git, 'init']
+ if template is not None:
+ args.append('--template=%s' % template)
+ env = {}
+ env.update(os.environ)
+ env['GIT_DIR'] = '.'
+ returncode = subprocess.call(
+ args=args,
+ cwd=path,
+ close_fds=True,
+ env=env,
+ )
+ if returncode != 0:
+ raise RuntimeError(
+ ("Command '%r' returned non-zero exit status %d"
+ % (args, returncode)),
+ )
diff --git a/gitosis/test/mocktemplates/hooks/post-update b/gitosis/test/mocktemplates/hooks/post-update
new file mode 100755
index 0000000..7e81207
--- /dev/null
+++ b/gitosis/test/mocktemplates/hooks/post-update
@@ -0,0 +1,2 @@
+#!/bin/sh
+# i can override standard templates
diff --git a/gitosis/test/mocktemplates/no-confusion b/gitosis/test/mocktemplates/no-confusion
new file mode 100644
index 0000000..4d1bc79
--- /dev/null
+++ b/gitosis/test/mocktemplates/no-confusion
@@ -0,0 +1 @@
+i should show up
diff --git a/gitosis/test/test_repository.py b/gitosis/test/test_repository.py
new file mode 100644
index 0000000..9b8b70c
--- /dev/null
+++ b/gitosis/test/test_repository.py
@@ -0,0 +1,63 @@
+from nose.tools import eq_ as eq
+
+import os
+import stat
+import shutil
+
+from gitosis import repository
+
+from gitosis.test.util import mkdir, maketemp, writeFile, readFile
+
+def check_bare(path):
+ # we want it to be a bare repository
+ assert not os.path.exists(os.path.join(path, '.git'))
+
+def test_init_simple():
+ tmp = maketemp()
+ path = os.path.join(tmp, 'repo.git')
+ repository.init(path)
+ st = os.stat(path)
+ assert stat.S_ISDIR(st.st_mode)
+ eq(stat.S_IMODE(st.st_mode), 0750)
+ check_bare(path)
+
+def test_init_exist_dir():
+ tmp = maketemp()
+ path = os.path.join(tmp, 'repo.git')
+ mkdir(path)
+ repository.init(path)
+ st = os.stat(path)
+ assert stat.S_ISDIR(st.st_mode)
+ eq(stat.S_IMODE(st.st_mode), 0750)
+ check_bare(path)
+
+def test_init_exist_git():
+ tmp = maketemp()
+ path = os.path.join(tmp, 'repo.git')
+ repository.init(path)
+ repository.init(path)
+ st = os.stat(path)
+ assert stat.S_ISDIR(st.st_mode)
+ eq(stat.S_IMODE(st.st_mode), 0750)
+ check_bare(path)
+
+def test_init_templates():
+ tmp = maketemp()
+ path = os.path.join(tmp, 'repo.git')
+ if os.path.exists(path):
+ shutil.rmtree(path)
+ templatedir = os.path.join(
+ os.path.dirname(__file__),
+ 'mocktemplates',
+ )
+ repository.init(path, template=templatedir)
+ repository.init(path)
+ got = readFile(os.path.join(path, 'no-confusion'))
+ eq(got, 'i should show up\n')
+ st = os.stat(os.path.join(path, 'hooks', 'post-update'))
+ assert stat.S_ISREG(st.st_mode)
+ eq('%04o'%stat.S_IMODE(st.st_mode), '%04o'%0755)
+ got = readFile(os.path.join(path, 'hooks', 'post-update'))
+ eq(got, '#!/bin/sh\n# i can override standard templates\n')
+ # standard templates are there, too
+ assert os.path.isfile(os.path.join(path, 'hooks', 'pre-rebase'))