# HG changeset patch # User Mads Kiilerich # Date 1595267190 -7200 # Node ID eb486c0c3114e74bf7f912ef5ff7cca1612859e7 # Parent ed92c3b846c7eeb835fbfecb559ad0d50a89dfe9 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. diff -r ed92c3b846c7 -r eb486c0c3114 kallithea/lib/utils.py --- 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 diff -r ed92c3b846c7 -r eb486c0c3114 kallithea/model/repo.py --- 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) diff -r ed92c3b846c7 -r eb486c0c3114 kallithea/model/scm.py --- 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=()): diff -r ed92c3b846c7 -r eb486c0c3114 kallithea/tests/vcs/test_git.py --- 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: