Changeset - 96e26544d037
[Not reviewed]
default
0 3 0
Mads Kiilerich - 7 years ago 2019-01-08 13:04:19
mads@kiilerich.com
middleware: introduce BaseVCSController scm_alias - prepare for sharing shared code
3 files changed with 14 insertions and 8 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/base.py
Show inline comments
 
@@ -189,12 +189,14 @@ class BasicAuth(paste.auth.basic.AuthBas
 

	
 
class BaseVCSController(object):
 
    """Base controller for handling Mercurial/Git protocol requests
 
    (coming from a VCS client, and not a browser).
 
    """
 

	
 
    scm_alias = None # 'hg' / 'git'
 

	
 
    def __init__(self, application, config):
 
        self.application = application
 
        self.config = config
 
        # base path of repo locations
 
        self.basepath = self.config['base_path']
 
        # authenticate this VCS request using the authentication modules
kallithea/lib/middleware/simplegit.py
Show inline comments
 
@@ -54,12 +54,14 @@ cmd_mapping = {
 
    'git-upload-pack': 'pull',
 
}
 

	
 

	
 
class SimpleGit(BaseVCSController):
 

	
 
    scm_alias = 'git'
 

	
 
    @classmethod
 
    def parse_request(cls, environ):
 
        path_info = environ.get('PATH_INFO', '')
 
        m = GIT_PROTO_PAT.match(path_info)
 
        if m is None:
 
            return None
 
@@ -82,13 +84,13 @@ class SimpleGit(BaseVCSController):
 
    def _handle_request(self, parsed_request, environ, start_response):
 
        ip_addr = self._get_ip_addr(environ)
 
        # skip passing error to error controller
 
        environ['pylons.status_code_redirect'] = True
 

	
 
        # quick check if repo exists...
 
        if not is_valid_repo(parsed_request.repo_name, self.basepath, 'git'):
 
        if not is_valid_repo(parsed_request.repo_name, self.basepath, self.scm_alias):
 
            raise HTTPNotFound()
 

	
 
        if parsed_request.action is None:
 
            # Note: the client doesn't get the helpful error message
 
            raise HTTPBadRequest('Unable to detect pull/push action for %r! Are you using a nonstandard command or client?' % parsed_request.repo_name)
 

	
 
@@ -105,13 +107,13 @@ class SimpleGit(BaseVCSController):
 
        server_url = get_server_url(environ)
 
        extras = {
 
            'ip': ip_addr,
 
            'username': user.username,
 
            'action': parsed_request.action,
 
            'repository': parsed_request.repo_name,
 
            'scm': 'git',
 
            'scm': self.scm_alias,
 
            'config': CONFIG['__file__'],
 
            'server_url': server_url,
 
        }
 

	
 
        #===================================================================
 
        # GIT REQUEST HANDLING
 
@@ -119,14 +121,14 @@ class SimpleGit(BaseVCSController):
 
        log.debug('HOOKS extras is %s', extras)
 
        baseui = make_ui()
 
        _set_extras(extras or {})
 

	
 
        try:
 
            self._handle_githooks(parsed_request.repo_name, parsed_request.action, baseui, environ)
 
            log.info('%s action on Git repo "%s" by "%s" from %s',
 
                     parsed_request.action, parsed_request.repo_name, safe_str(user.username), ip_addr)
 
            log.info('%s action on %s repo "%s" by "%s" from %s',
 
                     parsed_request.action, self.scm_alias, parsed_request.repo_name, safe_str(user.username), ip_addr)
 
            app = self.__make_app(parsed_request.repo_name)
 
            return app(environ, start_response)
 
        except Exception:
 
            log.error(traceback.format_exc())
 
            raise HTTPInternalServerError()
 

	
kallithea/lib/middleware/simplehg.py
Show inline comments
 
@@ -93,12 +93,14 @@ cmd_mapping = {
 
    'unbundle': 'push',
 
    }
 

	
 

	
 
class SimpleHg(BaseVCSController):
 

	
 
    scm_alias = 'hg'
 

	
 
    @classmethod
 
    def parse_request(cls, environ):
 
        http_accept = environ.get('HTTP_ACCEPT', '')
 
        if not http_accept.startswith('application/mercurial'):
 
            return None
 
        path_info = environ.get('PATH_INFO', '')
 
@@ -137,13 +139,13 @@ class SimpleHg(BaseVCSController):
 
    def _handle_request(self, parsed_request, environ, start_response):
 
        ip_addr = self._get_ip_addr(environ)
 
        # skip passing error to error controller
 
        environ['pylons.status_code_redirect'] = True
 

	
 
        # quick check if repo exists...
 
        if not is_valid_repo(parsed_request.repo_name, self.basepath, 'hg'):
 
        if not is_valid_repo(parsed_request.repo_name, self.basepath, self.scm_alias):
 
            raise HTTPNotFound()
 

	
 
        if parsed_request.action is None:
 
            # Note: the client doesn't get the helpful error message
 
            raise HTTPBadRequest('Unable to detect pull/push action for %r! Are you using a nonstandard command or client?' % parsed_request.repo_name)
 

	
 
@@ -160,13 +162,13 @@ class SimpleHg(BaseVCSController):
 
        server_url = get_server_url(environ)
 
        extras = {
 
            'ip': ip_addr,
 
            'username': user.username,
 
            'action': parsed_request.action,
 
            'repository': parsed_request.repo_name,
 
            'scm': 'hg',
 
            'scm': self.scm_alias,
 
            'config': CONFIG['__file__'],
 
            'server_url': server_url,
 
        }
 
        #======================================================================
 
        # MERCURIAL REQUEST HANDLING
 
        #======================================================================
 
@@ -176,14 +178,14 @@ class SimpleHg(BaseVCSController):
 

	
 
        log.debug('HOOKS extras is %s', extras)
 
        baseui = make_ui(repo_path=repo_path)
 
        _set_extras(extras or {})
 

	
 
        try:
 
            log.info('%s action on Mercurial repo "%s" by "%s" from %s',
 
                     parsed_request.action, parsed_request.repo_name, safe_str(user.username), ip_addr)
 
            log.info('%s action on %s repo "%s" by "%s" from %s',
 
                     parsed_request.action, self.scm_alias, parsed_request.repo_name, safe_str(user.username), ip_addr)
 
            environ['REPO_NAME'] = str_repo_name # used by hgweb_mod.hgweb
 
            app = self.__make_app(repo_path, baseui)
 
            return app(environ, start_response)
 
        except Exception:
 
            log.error(traceback.format_exc())
 
            raise HTTPInternalServerError()
0 comments (0 inline, 0 general)