# HG changeset patch # User Marcin Kuzminski # Date 2012-04-18 23:27:37 # Node ID d9972f76322eb0ad80d6b8da20c0e0761c93da85 # Parent 48d9a62c9b75d2e6ae44b431bda0f3c9266cdb58 added emulation of pull hook for git-backend, and dummy git-push hook diff --git a/rhodecode/lib/helpers.py b/rhodecode/lib/helpers.py --- a/rhodecode/lib/helpers.py +++ b/rhodecode/lib/helpers.py @@ -87,7 +87,7 @@ def get_token(): if not token_key in session: try: token = hashlib.sha1(str(random.getrandbits(128))).hexdigest() - except AttributeError: # Python < 2.4 + except AttributeError: # Python < 2.4 token = hashlib.sha1(str(random.randrange(2 ** 128))).hexdigest() session[token_key] = token if hasattr(session, 'save'): @@ -454,11 +454,14 @@ def action_parser(user_log, feed=False): revision=rev.raw_id), title=tooltip(message(rev)), class_='tooltip') ) - # get only max revs_top_limit of changeset for performance/ui reasons - revs = [ - x for x in repo.get_changesets(revs_ids[0], - revs_ids[:revs_top_limit][-1]) - ] + + revs = [] + if len(filter(lambda v: v != '', revs_ids)) > 0: + # get only max revs_top_limit of changeset for performance/ui reasons + revs = [ + x for x in repo.get_changesets(revs_ids[0], + revs_ids[:revs_top_limit][-1]) + ] cs_links = [] cs_links.append(" " + ', '.join( diff --git a/rhodecode/lib/hooks.py b/rhodecode/lib/hooks.py --- a/rhodecode/lib/hooks.py +++ b/rhodecode/lib/hooks.py @@ -92,6 +92,7 @@ def log_pull_action(ui, repo, **kwargs): extras = dict(repo.ui.configitems('rhodecode_extras')) username = extras['username'] repository = extras['repository'] + scm = extras['scm'] action = 'pull' action_logger(username, action, repository, extras['ip'], commit=True) @@ -117,21 +118,26 @@ def log_push_action(ui, repo, **kwargs): username = extras['username'] repository = extras['repository'] action = extras['action'] + ':%s' - node = kwargs['node'] + scm = extras['scm'] - def get_revs(repo, rev_opt): - if rev_opt: - revs = revrange(repo, rev_opt) + if scm == 'hg': + node = kwargs['node'] + + def get_revs(repo, rev_opt): + if rev_opt: + revs = revrange(repo, rev_opt) - if len(revs) == 0: - return (nullrev, nullrev) - return (max(revs), min(revs)) - else: - return (len(repo) - 1, 0) + if len(revs) == 0: + return (nullrev, nullrev) + return (max(revs), min(revs)) + else: + return (len(repo) - 1, 0) - stop, start = get_revs(repo, [node + ':']) + stop, start = get_revs(repo, [node + ':']) - revs = (str(repo[r]) for r in xrange(start, stop + 1)) + revs = (str(repo[r]) for r in xrange(start, stop + 1)) + elif scm == 'git': + revs = [] action = action % ','.join(revs) diff --git a/rhodecode/lib/middleware/simplegit.py b/rhodecode/lib/middleware/simplegit.py --- a/rhodecode/lib/middleware/simplegit.py +++ b/rhodecode/lib/middleware/simplegit.py @@ -74,7 +74,7 @@ from paste.httpheaders import REMOTE_USE from rhodecode.lib.utils2 import safe_str from rhodecode.lib.base import BaseVCSController from rhodecode.lib.auth import get_container_username -from rhodecode.lib.utils import is_valid_repo +from rhodecode.lib.utils import is_valid_repo, make_ui from rhodecode.model.db import User from webob.exc import HTTPNotFound, HTTPForbidden, HTTPInternalServerError @@ -103,6 +103,7 @@ class SimpleGit(BaseVCSController): ipaddr = self._get_ip_addr(environ) username = None + self._git_first_op = False # skip passing error to error controller environ['pylons.status_code_redirect'] = True @@ -178,6 +179,13 @@ class SimpleGit(BaseVCSController): perm = self._check_permission(action, user, repo_name) if perm is not True: return HTTPForbidden()(environ, start_response) + extras = { + 'ip': ipaddr, + 'username': username, + 'action': action, + 'repository': repo_name, + 'scm': 'git', + } #=================================================================== # GIT REQUEST HANDLING @@ -185,10 +193,16 @@ class SimpleGit(BaseVCSController): repo_path = os.path.join(safe_str(self.basepath), safe_str(repo_name)) log.debug('Repository path is %s' % repo_path) + baseui = make_ui('db') + for k, v in extras.items(): + baseui.setconfig('rhodecode_extras', k, v) + try: - #invalidate cache on push + # invalidate cache on push if action == 'push': self._invalidate_cache(repo_name) + self._handle_githooks(action, baseui, environ) + log.info('%s action on GIT repo "%s"' % (action, repo_name)) app = self.__make_app(repo_name, repo_path) return app(environ, start_response) @@ -249,3 +263,25 @@ class SimpleGit(BaseVCSController): # operation is pull/push op = getattr(self, '_git_stored_op', 'pull') return op + + def _handle_githooks(self, action, baseui, environ): + + from rhodecode.lib.hooks import log_pull_action, log_push_action + service = environ['QUERY_STRING'].split('=') + if len(service) < 2: + return + + class cont(object): + pass + + repo = cont() + setattr(repo, 'ui', baseui) + + push_hook = 'pretxnchangegroup.push_logger' + pull_hook = 'preoutgoing.pull_logger' + _hooks = dict(baseui.configitems('hooks')) or {} + if action == 'push' and _hooks.get(push_hook): + log_push_action(ui=baseui, repo=repo) + elif action == 'pull' and _hooks.get(pull_hook): + log_pull_action(ui=baseui, repo=repo) + diff --git a/rhodecode/lib/middleware/simplehg.py b/rhodecode/lib/middleware/simplehg.py --- a/rhodecode/lib/middleware/simplehg.py +++ b/rhodecode/lib/middleware/simplehg.py @@ -153,7 +153,8 @@ class SimpleHg(BaseVCSController): 'ip': ipaddr, 'username': username, 'action': action, - 'repository': repo_name + 'repository': repo_name, + 'scm': 'hg', } #======================================================================