changeset 8598:eb486c0c3114

scm: refactor install_git_hooks Rename, simplify, and negate some logic to make the flow more readable to me and give better logging. For example, "force_create" were more about "force overwrite". Calling it "force" is more precise.
author Mads Kiilerich <mads@kiilerich.com>
date Mon, 20 Jul 2020 19:46:30 +0200
parents ed92c3b846c7
children 68eee0e7f4f5
files kallithea/lib/utils.py kallithea/model/repo.py kallithea/model/scm.py kallithea/tests/vcs/test_git.py
diffstat 4 files changed, 18 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/utils.py	Sat Jul 25 21:20:46 2020 +0200
+++ b/kallithea/lib/utils.py	Mon Jul 20 19:46:30 2020 +0200
@@ -500,7 +500,7 @@
             new_repo.update_changeset_cache()
         elif install_git_hooks:
             if db_repo.repo_type == 'git':
-                ScmModel().install_git_hooks(db_repo.scm_instance, force_create=overwrite_git_hooks)
+                ScmModel().install_git_hooks(db_repo.scm_instance, force=overwrite_git_hooks)
 
     removed = []
     # remove from database those repositories that are not in the filesystem
--- a/kallithea/model/repo.py	Sat Jul 25 21:20:46 2020 +0200
+++ b/kallithea/model/repo.py	Mon Jul 20 19:46:30 2020 +0200
@@ -666,7 +666,7 @@
         elif repo_type == 'git':
             repo = backend(repo_path, create=True, src_url=clone_uri, bare=True)
             # add kallithea hook into this repo
-            ScmModel().install_git_hooks(repo=repo)
+            ScmModel().install_git_hooks(repo)
         else:
             raise Exception('Not supported repo_type %s expected hg/git' % repo_type)
 
--- a/kallithea/model/scm.py	Sat Jul 25 21:20:46 2020 +0200
+++ b/kallithea/model/scm.py	Mon Jul 20 19:46:30 2020 +0200
@@ -691,12 +691,12 @@
                 or sys.executable
                 or '/usr/bin/env python3')
 
-    def install_git_hooks(self, repo, force_create=False):
+    def install_git_hooks(self, repo, force=False):
         """
         Creates a kallithea hook inside a git repository
 
         :param repo: Instance of VCS repo
-        :param force_create: Create even if same name hook exists
+        :param force: Overwrite existing non-Kallithea hooks
         """
 
         hooks_path = os.path.join(repo.path, 'hooks')
@@ -716,7 +716,7 @@
 
         for h_type, tmpl in [('pre', tmpl_pre), ('post', tmpl_post)]:
             hook_file = os.path.join(hooks_path, '%s-receive' % h_type)
-            has_hook = False
+            other_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 kallithea ?
@@ -725,17 +725,15 @@
                     data = f.read()
                     matches = re.search(br'^KALLITHEA_HOOK_VER\s*=\s*(.*)$', data, flags=re.MULTILINE)
                     if matches:
-                        try:
-                            ver = matches.groups()[0]
-                            log.debug('Found Kallithea hook - it has KALLITHEA_HOOK_VER %r', ver)
-                            has_hook = True
-                        except Exception:
-                            log.error(traceback.format_exc())
+                        ver = matches.groups()[0]
+                        log.debug('Found Kallithea hook - it has KALLITHEA_HOOK_VER %r', ver)
+                    else:
+                        log.debug('Found non-Kallithea hook at %s', hook_file)
+                        other_hook = True
+
+            if other_hook and not force:
+                log.warning('skipping overwriting hook file %s', hook_file)
             else:
-                # there is no hook in this dir, so we want to create one
-                has_hook = True
-
-            if has_hook or force_create:
                 log.debug('writing %s hook file !', h_type)
                 try:
                     with open(hook_file, 'wb') as f:
@@ -743,9 +741,7 @@
                         f.write(tmpl)
                     os.chmod(hook_file, 0o755)
                 except IOError as e:
-                    log.error('error writing %s: %s', hook_file, e)
-            else:
-                log.debug('skipping writing hook file')
+                    log.error('error writing hook %s: %s', hook_file, e)
 
 
 def AvailableRepoGroupChoices(repo_group_perm_level, extras=()):
--- a/kallithea/tests/vcs/test_git.py	Sat Jul 25 21:20:46 2020 +0200
+++ b/kallithea/tests/vcs/test_git.py	Mon Jul 20 19:46:30 2020 +0200
@@ -794,7 +794,7 @@
             if os.path.exists(hook_path):
                 os.remove(hook_path)
 
-        ScmModel().install_git_hooks(repo=self.repo)
+        ScmModel().install_git_hooks(self.repo)
 
         for hook, hook_path in self.kallithea_hooks.items():
             assert os.path.exists(hook_path)
@@ -808,7 +808,7 @@
             with open(hook_path, "w") as f:
                 f.write("KALLITHEA_HOOK_VER=0.0.0\nJUST_BOGUS")
 
-        ScmModel().install_git_hooks(repo=self.repo)
+        ScmModel().install_git_hooks(self.repo)
 
         for hook, hook_path in self.kallithea_hooks.items():
             with open(hook_path) as f:
@@ -823,7 +823,7 @@
             with open(hook_path, "w") as f:
                 f.write("#!/bin/bash\n#CUSTOM_HOOK")
 
-        ScmModel().install_git_hooks(repo=self.repo)
+        ScmModel().install_git_hooks(self.repo)
 
         for hook, hook_path in self.kallithea_hooks.items():
             with open(hook_path) as f:
@@ -838,7 +838,7 @@
             with open(hook_path, "w") as f:
                 f.write("#!/bin/bash\n#CUSTOM_HOOK")
 
-        ScmModel().install_git_hooks(repo=self.repo, force_create=True)
+        ScmModel().install_git_hooks(self.repo, force=True)
 
         for hook, hook_path in self.kallithea_hooks.items():
             with open(hook_path) as f: