Mercurial > kallithea
changeset 2404:a3efdd61a21f beta
Git Hooks are automatically installed in new repos
- fixed issue with initial push
- changed git pre-receive hook template to py file
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Wed, 06 Jun 2012 21:32:19 +0200 |
parents | 6418fdb7d807 |
children | 5019f7798733 |
files | rhodecode/config/pre-receive.tmpl rhodecode/config/pre_receive_tmpl.py rhodecode/lib/hooks.py rhodecode/model/repo.py |
diffstat | 4 files changed, 58 insertions(+), 33 deletions(-) [+] |
line wrap: on
line diff
--- a/rhodecode/config/pre-receive.tmpl Wed Jun 06 19:53:43 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -#!/usr/bin/env python -import os -import sys - -try: - import rhodecode - 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(' ') - # 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() \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rhodecode/config/pre_receive_tmpl.py Wed Jun 06 21:32:19 2012 +0200 @@ -0,0 +1,29 @@ +#!/usr/bin/env python +import os +import sys + +try: + import rhodecode + 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(' ') + # 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() \ No newline at end of file
--- a/rhodecode/lib/hooks.py Wed Jun 06 19:53:43 2012 +0200 +++ b/rhodecode/lib/hooks.py Wed Jun 06 21:32:19 2012 +0200 @@ -33,6 +33,7 @@ from rhodecode import EXTENSIONS from rhodecode.lib import helpers as h from rhodecode.lib.utils import action_logger +from rhodecode.lib.vcs.backends.base import EmptyChangeset def _get_scm_size(alias, root_path): @@ -242,9 +243,14 @@ baseui.setconfig('rhodecode_extras', k, v) repo = repo.scm_instance repo.ui = baseui - old_rev, new_rev = revs[0:-1] - - cmd = 'log ' + old_rev + '..' + new_rev + ' --reverse --pretty=format:"%H"' + 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, '') + 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() log_push_action(baseui, repo, _git_revs=git_revs)
--- a/rhodecode/model/repo.py Wed Jun 06 19:53:43 2012 +0200 +++ b/rhodecode/model/repo.py Wed Jun 06 21:32:19 2012 +0200 @@ -22,10 +22,13 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +from __future__ import with_statement import os import shutil import logging import traceback +import pkg_resources +from os.path import dirname as dn, join as jn from datetime import datetime from rhodecode.lib.vcs.backends import get_backend @@ -461,7 +464,23 @@ if alias == 'hg': backend(repo_path, create=True, src_url=clone_uri) elif alias == 'git': - backend(repo_path, create=True, src_url=clone_uri, bare=True) + r = backend(repo_path, create=True, src_url=clone_uri, bare=True) + # add rhodecode hook into this repo + + loc = jn(r.path, 'hooks') + if not r.bare: + loc = jn(r.path, '.git', 'hooks') + if not os.path.isdir(loc): + os.makedirs(loc) + + tmpl = pkg_resources.resource_string( + 'rhodecode', jn('config', 'pre_receive_tmpl.py') + ) + _hook_file = jn(loc, 'pre-receive') + with open(_hook_file, 'wb') as f: + f.write(tmpl) + os.chmod(_hook_file, 0555) + else: raise Exception('Undefined alias %s' % alias)