From 5655b7f517a46fc1177a3e9007ab56b36a6d8e0c Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Thu, 30 Aug 2007 20:00:15 -0700 Subject: [PATCH] Add helper to create (bare) repositories with custom templates. --- gitosis/repository.py | 39 ++++++++++++ gitosis/test/mocktemplates/hooks/post-update | 2 + gitosis/test/mocktemplates/no-confusion | 1 + gitosis/test/test_repository.py | 63 ++++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 gitosis/repository.py create mode 100755 gitosis/test/mocktemplates/hooks/post-update create mode 100644 gitosis/test/mocktemplates/no-confusion create mode 100644 gitosis/test/test_repository.py 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'))