diff --git a/kallithea/lib/utils.py b/kallithea/lib/utils.py --- a/kallithea/lib/utils.py +++ b/kallithea/lib/utils.py @@ -500,7 +500,7 @@ def repo2db_mapper(initial_repo_dict, re 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 --git a/kallithea/model/repo.py b/kallithea/model/repo.py --- a/kallithea/model/repo.py +++ b/kallithea/model/repo.py @@ -666,7 +666,7 @@ class RepoModel(object): 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 --git a/kallithea/model/scm.py b/kallithea/model/scm.py --- a/kallithea/model/scm.py +++ b/kallithea/model/scm.py @@ -691,12 +691,12 @@ class ScmModel(object): 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 @@ class ScmModel(object): 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 @@ class ScmModel(object): 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 @@ class ScmModel(object): 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 --git a/kallithea/tests/vcs/test_git.py b/kallithea/tests/vcs/test_git.py --- a/kallithea/tests/vcs/test_git.py +++ b/kallithea/tests/vcs/test_git.py @@ -794,7 +794,7 @@ class TestGitHooks(object): 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 @@ class TestGitHooks(object): 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 @@ class TestGitHooks(object): 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 @@ class TestGitHooks(object): 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: