Add helper to create (bare) repositories with custom templates.
This commit is contained in:
parent
f86b354744
commit
5655b7f517
4 changed files with 105 additions and 0 deletions
39
gitosis/repository.py
Normal file
39
gitosis/repository.py
Normal file
|
@ -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)),
|
||||
)
|
2
gitosis/test/mocktemplates/hooks/post-update
Executable file
2
gitosis/test/mocktemplates/hooks/post-update
Executable file
|
@ -0,0 +1,2 @@
|
|||
#!/bin/sh
|
||||
# i can override standard templates
|
1
gitosis/test/mocktemplates/no-confusion
Normal file
1
gitosis/test/mocktemplates/no-confusion
Normal file
|
@ -0,0 +1 @@
|
|||
i should show up
|
63
gitosis/test/test_repository.py
Normal file
63
gitosis/test/test_repository.py
Normal file
|
@ -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'))
|
Loading…
Reference in a new issue