Make gitosis-serve create repositories on demand when pushing.

This commit is contained in:
Tommi Virtanen 2007-09-02 13:28:41 -07:00
parent 3339783581
commit 3dd96139a1
2 changed files with 38 additions and 2 deletions

View file

@ -10,6 +10,7 @@ import sys, os, optparse, re
from ConfigParser import RawConfigParser
from gitosis import access
from gitosis import repository
def die(msg):
print >>sys.stderr, '%s: %s' % (sys.argv[0], msg)
@ -106,6 +107,13 @@ def serve(
# didn't have write access and tried to write
raise WriteAccessDenied()
if (not os.path.exists(newpath)
and verb in COMMANDS_WRITE):
# it doesn't exist on the filesystem, but the configuration
# refers to it, we're serving a write request, and the user is
# authorized to do that: create the repository on the fly
repository.init(path=newpath)
# put the verb back together with the new path
newcmd = "%(verb)s '%(newpath)s'" % dict(
verb=verb,

View file

@ -1,9 +1,11 @@
from nose.tools import eq_ as eq
from gitosis.test.util import assert_raises
import os
from ConfigParser import RawConfigParser
from gitosis import serve
from gitosis import repository
from gitosis.test import util
@ -88,7 +90,11 @@ def test_bad_forbiddenCommand_write_readAccess():
assert isinstance(e, serve.ServingError)
def test_simple_read():
tmp = util.maketemp()
repository.init(os.path.join(tmp, 'foo.git'))
cfg = RawConfigParser()
cfg.add_section('gitosis')
cfg.set('gitosis', 'repositories', tmp)
cfg.add_section('group foo')
cfg.set('group foo', 'members', 'jdoe')
cfg.set('group foo', 'readonly', 'foo')
@ -97,10 +103,14 @@ def test_simple_read():
user='jdoe',
command="git-upload-pack 'foo'",
)
eq(got, "git-upload-pack 'repositories/foo'")
eq(got, "git-upload-pack '%s/foo'" % tmp)
def test_simple_write():
tmp = util.maketemp()
repository.init(os.path.join(tmp, 'foo.git'))
cfg = RawConfigParser()
cfg.add_section('gitosis')
cfg.set('gitosis', 'repositories', tmp)
cfg.add_section('group foo')
cfg.set('group foo', 'members', 'jdoe')
cfg.set('group foo', 'writable', 'foo')
@ -109,4 +119,22 @@ def test_simple_write():
user='jdoe',
command="git-receive-pack 'foo'",
)
eq(got, "git-receive-pack 'repositories/foo'")
eq(got, "git-receive-pack '%s/foo'" % tmp)
def test_push_inits_if_needed():
# a push to a non-existent repository (but where config authorizes
# you to do that) will create the repository on the fly
tmp = util.maketemp()
cfg = RawConfigParser()
cfg.add_section('gitosis')
cfg.set('gitosis', 'repositories', tmp)
cfg.add_section('group foo')
cfg.set('group foo', 'members', 'jdoe')
cfg.set('group foo', 'writable', 'foo')
got = serve.serve(
cfg=cfg,
user='jdoe',
command="git-receive-pack 'foo'",
)
eq(os.listdir(tmp), ['foo'])
assert os.path.isfile(os.path.join(tmp, 'foo', 'HEAD'))