changeset 2618:e1370dcb9908 beta

Created install_git_hook more verbose version of previos code. - allows autoupdating hooks in later releases
author Marcin Kuzminski <marcin@python-works.com>
date Mon, 16 Jul 2012 02:28:02 +0200
parents c0ec29b20eb6
children f1dfd3a2a193
files rhodecode/model/repo.py rhodecode/model/scm.py
diffstat 2 files changed, 56 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/model/repo.py	Mon Jul 16 02:26:15 2012 +0200
+++ b/rhodecode/model/repo.py	Mon Jul 16 02:28:02 2012 +0200
@@ -27,8 +27,6 @@
 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 @@
                                    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 @@
         :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 @@
         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)
 
--- a/rhodecode/model/scm.py	Mon Jul 16 02:26:15 2012 +0200
+++ b/rhodecode/model/scm.py	Mon Jul 16 02:28:02 2012 +0200
@@ -23,14 +23,18 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 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 @@
         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