# HG changeset patch # User Marcin Kuzminski # Date 2012-07-16 00:28:02 # Node ID e1370dcb9908be7ac49dde9d7788559853584b16 # Parent c0ec29b20eb62c2e9638c60a6259014b0877670b Created install_git_hook more verbose version of previos code. - allows autoupdating hooks in later releases diff --git a/rhodecode/model/repo.py b/rhodecode/model/repo.py --- a/rhodecode/model/repo.py +++ b/rhodecode/model/repo.py @@ -27,8 +27,6 @@ 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 @@ -284,13 +282,15 @@ class RepoModel(BaseModel): clone_uri) log_create_repository(new_repo.get_dict(), created_by=owner.username) - + else: + # install the githook if it's a git repo + if repo_type == 'git': + ScmModel().install_git_hook(repo=new_repo.scm_instance) # now automatically start following this repository as owner ScmModel(self.sa).toggle_following_repo(new_repo.repo_id, owner.user_id) return new_repo except: - print traceback.format_exc() log.error(traceback.format_exc()) raise @@ -448,6 +448,7 @@ class RepoModel(BaseModel): :param clone_uri: """ from rhodecode.lib.utils import is_valid_repo, is_valid_repos_group + from rhodecode.model.scm import ScmModel if parent: new_parent_path = os.sep.join(parent.full_path_splitted) @@ -476,21 +477,7 @@ class RepoModel(BaseModel): elif alias == 'git': 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', 'post_receive_tmpl.py') - ) - _hook_file = jn(loc, 'post-receive') - with open(_hook_file, 'wb') as f: - f.write(tmpl) - os.chmod(_hook_file, 0755) - + ScmModel().install_git_hook(repo=r) else: raise Exception('Undefined alias %s' % alias) diff --git a/rhodecode/model/scm.py b/rhodecode/model/scm.py --- a/rhodecode/model/scm.py +++ b/rhodecode/model/scm.py @@ -23,14 +23,18 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . import os +import re import time import traceback import logging import cStringIO +import pkg_resources +from os.path import dirname as dn, join as jn from sqlalchemy import func from pylons.i18n.translation import _ +import rhodecode from rhodecode.lib.vcs import get_backend from rhodecode.lib.vcs.exceptions import RepositoryError from rhodecode.lib.vcs.utils.lazy import LazyProperty @@ -545,3 +549,49 @@ class ScmModel(BaseModel): choices.extend([x[0] for x in tags_group[0]]) return choices, hist_l + + def install_git_hook(self, repo, force_create=False): + """ + Creates a rhodecode hook inside a git repository + + :param repo: Instance of VCS repo + :param force_create: Create even if same name hook exists + """ + + loc = jn(repo.path, 'hooks') + if not repo.bare: + loc = jn(repo.path, '.git', 'hooks') + if not os.path.isdir(loc): + os.makedirs(loc) + + tmpl = pkg_resources.resource_string( + 'rhodecode', jn('config', 'post_receive_tmpl.py') + ) + + _hook_file = jn(loc, 'post-receive') + _rhodecode_hook = False + log.debug('Installing git hook in repo %s' % repo) + if os.path.exists(_hook_file): + # let's take a look at this hook, maybe it's rhodecode ? + log.debug('hook exists, checking if it is from rhodecode') + _HOOK_VER_PAT = re.compile(r'^RC_HOOK_VER') + with open(_hook_file, 'rb') as f: + data = f.read() + matches = re.compile(r'(?:%s)\s*=\s*(.*)' + % 'RC_HOOK_VER').search(data) + if matches: + try: + ver = matches.groups()[0] + log.debug('got %s it is rhodecode' % (ver)) + _rhodecode_hook = True + except: + log.error(traceback.format_exc()) + + if _rhodecode_hook or force_create: + log.debug('writing hook file !') + with open(_hook_file, 'wb') as f: + tmpl = tmpl.replace('_TMPL_', rhodecode.__version__) + f.write(tmpl) + os.chmod(_hook_file, 0755) + else: + log.debug('skipping writing hook file') \ No newline at end of file