changeset 2617:c0ec29b20eb6 beta

Fixed githooks for fetching multiple tags and branches. - Updated git hook template with version and fixed issue with parsing refs
author Marcin Kuzminski <marcin@python-works.com>
date Mon, 16 Jul 2012 02:26:15 +0200
parents bab7eaa2cd7d
children e1370dcb9908
files rhodecode/config/post_receive_tmpl.py rhodecode/lib/hooks.py
diffstat 2 files changed, 37 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/config/post_receive_tmpl.py	Mon Jul 16 02:24:10 2012 +0200
+++ b/rhodecode/config/post_receive_tmpl.py	Mon Jul 16 02:26:15 2012 +0200
@@ -4,6 +4,8 @@
 
 try:
     import rhodecode
+    RC_HOOK_VER = '_TMPL_'
+    os.environ['RC_HOOK_VER'] = RC_HOOK_VER
     from rhodecode.lib.hooks import handle_git_post_receive
 except ImportError:
     rhodecode = None
@@ -17,7 +19,7 @@
         sys.exit(0)
 
     repo_path = os.path.abspath('.')
-    push_data = sys.stdin.read().strip().split(' ')
+    push_data = sys.stdin.readlines()
     # os.environ is modified here by a subprocess call that
     # runs git and later git executes this hook.
     # Environ get's some additional info from rhodecode system
--- a/rhodecode/lib/hooks.py	Mon Jul 16 02:24:10 2012 +0200
+++ b/rhodecode/lib/hooks.py	Mon Jul 16 02:26:15 2012 +0200
@@ -225,11 +225,13 @@
     init_model(engine)
 
     baseui = make_ui('db')
+    # fix if it's not a bare repo
+    if repo_path.endswith('.git'):
+        repo_path = repo_path[:-4]
     repo = Repository.get_by_full_path(repo_path)
-
     _hooks = dict(baseui.configitems('hooks')) or {}
     # if push hook is enabled via web interface
-    if _hooks.get(RhodeCodeUi.HOOK_PUSH):
+    if repo and _hooks.get(RhodeCodeUi.HOOK_PUSH):
 
         extras = {
          'username': env['RHODECODE_USER'],
@@ -242,18 +244,35 @@
             baseui.setconfig('rhodecode_extras', k, v)
         repo = repo.scm_instance
         repo.ui = baseui
-        old_rev, new_rev, ref = revs
-        if old_rev == EmptyChangeset().raw_id:
-            cmd = "for-each-ref --format='%(refname)' 'refs/heads/*'"
-            heads = repo.run_git_command(cmd)[0]
-            heads = heads.replace(ref, '')
-            heads = ' '.join(map(lambda c: c.strip('\n').strip(),
-                                 heads.splitlines()))
-            cmd = ('log ' + new_rev +
-                   ' --reverse --pretty=format:"%H" --not ' + heads)
-        else:
-            cmd = ('log ' + old_rev + '..' + new_rev +
-                   ' --reverse --pretty=format:"%H"')
-        git_revs = repo.run_git_command(cmd)[0].splitlines()
+
+        rev_data = []
+        for l in revs:
+            old_rev, new_rev, ref = l.split(' ')
+            _ref_data = ref.split('/')
+            if _ref_data[1] in ['tags', 'heads']:
+                rev_data.append({'old_rev': old_rev,
+                                 'new_rev': new_rev,
+                                 'ref': ref,
+                                 'type': _ref_data[1],
+                                 'name': _ref_data[2].strip()})
+
+        git_revs = []
+        for push_ref  in rev_data:
+            _type = push_ref['type']
+            if _type == 'heads':
+                if push_ref['old_rev'] == EmptyChangeset().raw_id:
+                    cmd = "for-each-ref --format='%(refname)' 'refs/heads/*'"
+                    heads = repo.run_git_command(cmd)[0]
+                    heads = heads.replace(push_ref['ref'], '')
+                    heads = ' '.join(map(lambda c: c.strip('\n').strip(),
+                                         heads.splitlines()))
+                    cmd = (('log %(new_rev)s' % push_ref) +
+                           ' --reverse --pretty=format:"%H" --not ' + heads)
+                else:
+                    cmd = (('log %(old_rev)s..%(new_rev)s' % push_ref) +
+                           ' --reverse --pretty=format:"%H"')
+                git_revs += repo.run_git_command(cmd)[0].splitlines()
+            elif _type == 'tags':
+                git_revs += [push_ref['name']]
 
         log_push_action(baseui, repo, _git_revs=git_revs)