changeset 2203:d9972f76322e beta

added emulation of pull hook for git-backend, and dummy git-push hook
author Marcin Kuzminski <marcin@python-works.com>
date Thu, 19 Apr 2012 01:27:37 +0200
parents 48d9a62c9b75
children 8ce4c8e43eb7
files rhodecode/lib/helpers.py rhodecode/lib/hooks.py rhodecode/lib/middleware/simplegit.py rhodecode/lib/middleware/simplehg.py
diffstat 4 files changed, 66 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/lib/helpers.py	Wed Apr 18 02:07:38 2012 +0200
+++ b/rhodecode/lib/helpers.py	Thu Apr 19 01:27:37 2012 +0200
@@ -87,7 +87,7 @@
     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 @@
                         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(
--- a/rhodecode/lib/hooks.py	Wed Apr 18 02:07:38 2012 +0200
+++ b/rhodecode/lib/hooks.py	Thu Apr 19 01:27:37 2012 +0200
@@ -92,6 +92,7 @@
     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 @@
     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)
 
--- a/rhodecode/lib/middleware/simplegit.py	Wed Apr 18 02:07:38 2012 +0200
+++ b/rhodecode/lib/middleware/simplegit.py	Thu Apr 19 01:27:37 2012 +0200
@@ -74,7 +74,7 @@
 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 @@
 
         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 @@
                     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 @@
         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 @@
             # 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)
+
--- a/rhodecode/lib/middleware/simplehg.py	Wed Apr 18 02:07:38 2012 +0200
+++ b/rhodecode/lib/middleware/simplehg.py	Thu Apr 19 01:27:37 2012 +0200
@@ -153,7 +153,8 @@
             'ip': ipaddr,
             'username': username,
             'action': action,
-            'repository': repo_name
+            'repository': repo_name,
+            'scm': 'hg',
         }
 
         #======================================================================