changeset 8844:3b5657fba7f3

hooks: drop using Git pre-receive hooks - we don't need them
author Mads Kiilerich <mads@kiilerich.com>
date Mon, 11 Jan 2021 14:25:52 +0100
parents 8c7bbe5bd032
children d8e65780dbe9
files kallithea/bin/vcs_hooks.py kallithea/model/scm.py kallithea/templates/py/git_pre_receive_hook.py kallithea/tests/vcs/test_git.py
diffstat 4 files changed, 18 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/bin/vcs_hooks.py	Mon Jan 11 01:14:07 2021 +0100
+++ b/kallithea/bin/vcs_hooks.py	Mon Jan 11 14:25:52 2021 +0100
@@ -129,14 +129,6 @@
     return repo
 
 
-def pre_receive(repo_path, git_stdin_lines):
-    """Called from Git pre-receive hook.
-    The returned value is used as hook exit code and must be 0.
-    """
-    # Currently unused. TODO: remove?
-    return 0
-
-
 def post_receive(repo_path, git_stdin_lines):
     """Called from Git post-receive hook.
     The returned value is used as hook exit code and must be 0.
--- a/kallithea/model/scm.py	Mon Jan 11 01:14:07 2021 +0100
+++ b/kallithea/model/scm.py	Mon Jan 11 14:25:52 2021 +0100
@@ -674,12 +674,8 @@
         tmpl_post += pkg_resources.resource_string(
             'kallithea', os.path.join('templates', 'py', 'git_post_receive_hook.py')
         )
-        tmpl_pre = b"#!%s\n" % safe_bytes(self._get_git_hook_interpreter())
-        tmpl_pre += pkg_resources.resource_string(
-            'kallithea', os.path.join('templates', 'py', 'git_pre_receive_hook.py')
-        )
 
-        for h_type, tmpl in [('pre-receive', tmpl_pre), ('post-receive', tmpl_post)]:
+        for h_type, tmpl in [('pre-receive', None), ('post-receive', tmpl_post)]:
             hook_file = os.path.join(hooks_path, h_type)
             other_hook = False
             log.debug('Installing git hook %s in repo %s', h_type, repo.path)
@@ -702,7 +698,7 @@
                 other_hook = True
             if other_hook and not force:
                 log.warning('skipping overwriting hook file %s', hook_file)
-            else:
+            elif h_type == 'post-receive':
                 log.debug('writing hook file %s', hook_file)
                 if other_hook:
                     backup_file = hook_file + '.bak'
@@ -716,6 +712,9 @@
                     os.rename(fn, hook_file)
                 except (OSError, IOError) as e:
                     log.error('error writing hook %s: %s', hook_file, e)
+            elif h_type == 'pre-receive':  # no longer used, so just remove any existing Kallithea hook
+                if os.path.lexists(hook_file) and not other_hook:
+                    os.remove(hook_file)
 
 
 def AvailableRepoGroupChoices(repo_group_perm_level, extras=()):
--- a/kallithea/templates/py/git_pre_receive_hook.py	Mon Jan 11 01:14:07 2021 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,37 +0,0 @@
-"""Kallithea Git hook
-
-This hook is installed and maintained by Kallithea. It will be overwritten
-by Kallithea - don't customize it manually!
-
-When Kallithea invokes Git, the KALLITHEA_EXTRAS environment variable will
-contain additional info like the Kallithea instance and user info that this
-hook will use.
-"""
-
-import os
-import sys
-
-import kallithea.bin.vcs_hooks
-
-
-# Set output mode on windows to binary for stderr.
-# This prevents python (or the windows console) from replacing \n with \r\n.
-# Git doesn't display remote output lines that contain \r,
-# and therefore without this modification git would display empty lines
-# instead of the exception output.
-if sys.platform == "win32":
-    import msvcrt
-    msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
-
-KALLITHEA_HOOK_VER = '_TMPL_'
-os.environ['KALLITHEA_HOOK_VER'] = KALLITHEA_HOOK_VER
-
-
-def main():
-    repo_path = os.path.abspath('.')
-    git_stdin_lines = sys.stdin.readlines()
-    sys.exit(kallithea.bin.vcs_hooks.pre_receive(repo_path, git_stdin_lines))
-
-
-if __name__ == '__main__':
-    main()
--- a/kallithea/tests/vcs/test_git.py	Mon Jan 11 01:14:07 2021 +0100
+++ b/kallithea/tests/vcs/test_git.py	Mon Jan 11 14:25:52 2021 +0100
@@ -780,9 +780,11 @@
 
         # Create a dictionary where keys are hook names, and values are paths to
         # them in the non-bare repo. Deduplicates code in tests a bit.
+        self.pre_receive = os.path.join(self.repo.path, '.git', 'hooks', "pre-receive")
+        self.post_receive = os.path.join(self.repo.path, '.git', 'hooks', "post-receive")
         self.kallithea_hooks = {
-            "pre-receive": os.path.join(self.repo.path, '.git', 'hooks', "pre-receive"),
-            "post-receive": os.path.join(self.repo.path, '.git', 'hooks', "post-receive"),
+            "pre-receive": self.pre_receive,
+            "post-receive": self.post_receive,
         }
 
     def test_hooks_created_if_missing(self):
@@ -796,8 +798,8 @@
 
         ScmModel().install_git_hooks(self.repo)
 
-        for hook, hook_path in self.kallithea_hooks.items():
-            assert os.path.exists(hook_path)
+        assert not os.path.exists(self.pre_receive)
+        assert os.path.exists(self.post_receive)
 
     def test_kallithea_hooks_updated(self):
         """
@@ -810,9 +812,9 @@
 
         ScmModel().install_git_hooks(self.repo)
 
-        for hook, hook_path in self.kallithea_hooks.items():
-            with open(hook_path) as f:
-                assert "JUST_BOGUS" not in f.read()
+        assert not os.path.exists(self.pre_receive)
+        with open(self.post_receive) as f:
+            assert "JUST_BOGUS" not in f.read()
 
     def test_custom_hooks_untouched(self):
         """
@@ -840,6 +842,7 @@
 
         ScmModel().install_git_hooks(self.repo, force=True)
 
-        for hook, hook_path in self.kallithea_hooks.items():
-            with open(hook_path) as f:
-                assert "KALLITHEA_HOOK_VER" in f.read()
+        with open(self.pre_receive) as f:
+            assert "KALLITHEA_HOOK_VER" not in f.read()
+        with open(self.post_receive) as f:
+            assert "KALLITHEA_HOOK_VER" in f.read()