Changeset - c0ec29b20eb6
[Not reviewed]
beta
0 2 0
Marcin Kuzminski - 13 years ago 2012-07-16 02:26:15
marcin@python-works.com
Fixed githooks for fetching multiple tags and branches.
- Updated git hook template with version and fixed issue with parsing refs
2 files changed with 30 insertions and 9 deletions:
0 comments (0 inline, 0 general)
rhodecode/config/post_receive_tmpl.py
Show inline comments
 
#!/usr/bin/env python
 
import os
 
import sys
 

	
 
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
 

	
 

	
 
def main():
 
    if rhodecode is None:
 
        # exit with success if we cannot import rhodecode !!
 
        # this allows simply push to this repo even without
 
        # rhodecode
 
        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
 
    # like IP or username from basic-auth
 
    handle_git_post_receive(repo_path, push_data, os.environ)
 
    sys.exit(0)
 

	
 
if __name__ == '__main__':
 
    main()
rhodecode/lib/hooks.py
Show inline comments
 
@@ -216,44 +216,63 @@ def handle_git_post_receive(repo_path, r
 
    from rhodecode.model.db import RhodeCodeUi
 
    from rhodecode.lib.utils import make_ui
 
    from rhodecode.model.db import Repository
 

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

	
 
    engine = engine_from_config(conf, 'sqlalchemy.db1.')
 
    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'],
 
         'repository': repo.repo_name,
 
         'scm': 'git',
 
         'action': 'push',
 
         'ip': env['RHODECODE_CONFIG_IP'],
 
        }
 
        for k, v in extras.items():
 
            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:
 

	
 
        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(ref, '')
 
                    heads = heads.replace(push_ref['ref'], '')
 
            heads = ' '.join(map(lambda c: c.strip('\n').strip(),
 
                                 heads.splitlines()))
 
            cmd = ('log ' + new_rev +
 
                    cmd = (('log %(new_rev)s' % push_ref) +
 
                   ' --reverse --pretty=format:"%H" --not ' + heads)
 
        else:
 
            cmd = ('log ' + old_rev + '..' + new_rev +
 
                    cmd = (('log %(old_rev)s..%(new_rev)s' % push_ref) +
 
                   ' --reverse --pretty=format:"%H"')
 
        git_revs = repo.run_git_command(cmd)[0].splitlines()
 
                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)
0 comments (0 inline, 0 general)