Esempio n. 1
0
def serve(cfg, user, command):
    if "\n" in command:
        raise CommandMayNotContainNewlineError()

    try:
        verb, args = command.split(None, 1)
    except ValueError:
        # all known "git-foo" commands take one argument; improve
        # if/when needed
        raise UnknownCommandError()

    if verb == "git":
        try:
            subverb, args = args.split(None, 1)
        except ValueError:
            # all known "git foo" commands take one argument; improve
            # if/when needed
            raise UnknownCommandError()
        verb = "%s %s" % (verb, subverb)

    if verb not in COMMANDS_WRITE and verb not in COMMANDS_READONLY:
        raise UnknownCommandError()

    match = ALLOW_RE.match(args)
    if match is None:
        raise UnsafeArgumentsError()

    path = match.group("path")

    # write access is always sufficient
    newpath = access.haveAccess(config=cfg, user=user, mode="writable", path=path)

    if newpath is None:
        # didn't have write access; try once more with the popular
        # misspelling
        newpath = access.haveAccess(config=cfg, user=user, mode="writeable", path=path)
        if newpath is not None:
            log.warning('Repository %r config has typo "writeable", ' + 'should be "writable"', path)

    if newpath is None:
        # didn't have write access

        newpath = access.haveAccess(config=cfg, user=user, mode="readonly", path=path)

        if newpath is None:
            raise ReadAccessDenied()

        if verb in COMMANDS_WRITE:
            # didn't have write access and tried to write
            raise WriteAccessDenied()

    (topdir, relpath) = newpath
    assert not relpath.endswith(".git"), "git extension should have been stripped: %r" % relpath
    repopath = "%s.git" % relpath
    fullpath = os.path.join(topdir, repopath)
    if not os.path.exists(fullpath) 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

        # create leading directories
        p = topdir
        for segment in repopath.split(os.sep)[:-1]:
            p = os.path.join(p, segment)
            util.mkdir(p, 0750)

        repository.init(path=fullpath)

        gitweb.set_descriptions(config=cfg)
        generated = util.getGeneratedFilesDir(config=cfg)
        gitweb.generate_project_list(config=cfg, path=os.path.join(generated, "projects.list"))
        gitdaemon.set_export_ok(config=cfg)
        # Run buildhook.py here to notify buildserver of this repo
        buildhook.notify(relpath)

    elif os.path.exists(fullpath) and verb in COMMANDS_WRITE:
        # it exists on the filesystem

        # Run buildhook.py here to notify buildserver of this repo
        buildhook.notify(relpath)

    # put the verb back together with the new path
    newcmd = "%(verb)s '%(path)s'" % dict(verb=verb, path=fullpath)
    return newcmd
Esempio n. 2
0
def test_buildhook():
    buildhook.notify("asdf")