Changeset - 475eaccc844b
[Not reviewed]
default
0 1 0
Mads Kiilerich - 7 years ago 2018-08-08 02:23:11
mads@kiilerich.com
hooks: extract a separate _hook_environment from handle_git_receive

We want to split handle_git_receive up, and this self-contained chunk is what
will be shared.

Just moving code - not changing comments or anything.
1 file changed with 22 insertions and 11 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/hooks.py
Show inline comments
 
@@ -31,14 +31,15 @@ import binascii
 

	
 
from kallithea.lib.vcs.utils.hgcompat import nullrev, revrange
 
from kallithea.lib import helpers as h
 
from kallithea.lib.utils import action_logger
 
from kallithea.lib.vcs.backends.base import EmptyChangeset
 
from kallithea.lib.exceptions import HTTPLockedRC, UserCreationError
 
from kallithea.lib.utils import make_ui, setup_cache_regions
 
from kallithea.lib.utils2 import safe_str, safe_unicode, _extract_extras
 
from kallithea.model.db import Repository, User
 
from kallithea.model.db import Repository, User, Ui
 

	
 

	
 
def _get_scm_size(alias, root_path):
 
    if not alias.startswith('.'):
 
        alias += '.'
 

	
 
@@ -363,29 +364,25 @@ def handle_git_pre_receive(repo_path, gi
 

	
 
def handle_git_post_receive(repo_path, git_stdin_lines, env):
 
    """Called from Git post-receive hook"""
 
    return handle_git_receive(repo_path, git_stdin_lines, env, hook_type='post')
 

	
 

	
 
def handle_git_receive(repo_path, git_stdin_lines, env, hook_type):
 
def _hook_environment(repo_path, env):
 
    """
 
    A really hacky method that is run by git post-receive hook and logs
 
    a push action together with pushed revisions. It's executed by subprocess
 
    thus needs all info to be able to create an on the fly app environment,
 
    connect to database and run the logging code. Hacky as sh*t but works.
 
    Create a light-weight environment for stand-alone scripts and return an UI and the
 
    db repository.
 

	
 
    :param repo_path:
 
    :param revs:
 
    :param env:
 
    Git hooks are executed as subprocess of Git while Kallithea is waiting, and
 
    they thus need enough info to be able to create an app environment and
 
    connect to the database.
 
    """
 
    from paste.deploy import appconfig
 
    from sqlalchemy import engine_from_config
 
    from kallithea.config.environment import load_environment
 
    from kallithea.model.base import init_model
 
    from kallithea.model.db import Ui
 
    from kallithea.lib.utils import make_ui, setup_cache_regions
 

	
 
    extras = _extract_extras(env)
 
    path, ini_name = os.path.split(extras['config'])
 
    conf = appconfig('config:%s' % ini_name, relative_to=path)
 
    conf = load_environment(conf.global_conf, conf.local_conf)
 

	
 
@@ -402,12 +399,26 @@ def handle_git_receive(repo_path, git_st
 
    repo = Repository.get_by_full_path(repo_path)
 
    if not repo:
 
        raise OSError('Repository %s not found in database'
 
                      % (safe_str(repo_path)))
 

	
 
    baseui = make_ui('db')
 
    return baseui, repo
 

	
 
def handle_git_receive(repo_path, git_stdin_lines, env, hook_type):
 
    """
 
    A really hacky method that is run by git post-receive hook and logs
 
    a push action together with pushed revisions. It's executed by subprocess
 
    thus needs all info to be able to create an on the fly app environment,
 
    connect to database and run the logging code. Hacky as sh*t but works.
 

	
 
    :param repo_path:
 
    :param revs:
 
    :param env:
 
    """
 
    baseui, repo = _hook_environment(repo_path, env)
 

	
 
    if hook_type == 'pre':
 
        scm_repo = repo.scm_instance
 
    else:
 
        # post push should never use the cached instance
 
        scm_repo = repo.scm_instance_no_cache()
0 comments (0 inline, 0 general)