[PATCH] git: move non-kallithea hooks and execute other hooks

Tim Ooms tim.ooms at aeronomie.be
Tue Oct 20 15:19:51 UTC 2020


# HG changeset patch
# User Tim Ooms <tatankat at users.noreply.github.com>
# Date 1603201903 -7200
#      Tue Oct 20 15:51:43 2020 +0200
# Node ID 672e57b165d0c1774b692b5706a174bf98f42e4c
# Parent  b9b53e25a08d3714c54d82641b419e6d01820e12
git: move non-kallithea hooks and execute other hooks

diff -r b9b53e25a08d -r 672e57b165d0 kallithea/model/scm.py
--- a/kallithea/model/scm.py	Mon Oct 19 12:18:28 2020 +0200
+++ b/kallithea/model/scm.py	Tue Oct 20 15:51:43 2020 +0200
@@ -25,10 +25,12 @@
 :license: GPLv3, see LICENSE.md for more details.
 """
 
+import datetime
 import logging
 import os
 import posixpath
 import re
+import stat
 import sys
 import traceback
 
@@ -694,7 +696,7 @@
         Creates a kallithea hook inside a git repository
 
         :param repo: Instance of VCS repo
-        :param force: Overwrite existing non-Kallithea hooks
+        :param force: Move existing non-Kallithea hooks
         """
 
         hooks_path = os.path.join(repo.path, 'hooks')
@@ -730,14 +732,46 @@
                         other_hook = True
 
             if other_hook and not force:
-                log.warning('skipping overwriting hook file %s', hook_file)
+                log.warning('skip moving non-Kallithea hook file %s',
+                            hook_file)
             else:
+                # if we want to write the hook,
+                #  we move the other hook out of the way
+                if other_hook:
+                    # existing non-kallithea hook script will be renamed
+                    # additional scripts can be named like:
+                    #  "%s-receive.kallithea-extern.YYYYmmdd.manual
+                    # all "%s-receive.kallithea-extern.*.*" hook scripts
+                    #  will be executed in order
+                    moved_hook_file = '%s.kallithea-extern.%s.%s' % (
+                            hook_file,
+                            datetime.datetime.now().strftime("%Y%m%d%H%M%S%f"),
+                            kallithea.__version__)
+                    log.warning('moving hook file %s to %s',
+                                hook_file, moved_hook_file)
+                    # least chances to break an existing script by renaming
+                    # moving to a subdir may break scripts due to changed paths
+                    os.rename(hook_file, moved_hook_file)
+                # now we can write our hook
                 log.debug('writing %s hook file !', h_type)
                 try:
                     with open(hook_file, 'wb') as f:
                         tmpl = tmpl.replace(b'_TMPL_', safe_bytes(kallithea.__version__))
                         f.write(tmpl)
-                    os.chmod(hook_file, 0o755)
+                    try:
+                        if not os.path.islink(hook_file):
+                            # add permissions we (may) need
+                            # other bits are up to umask/inherited permissions
+                            os.chmod(
+                                hook_file, os.stat(hook_file).st_mode
+                                | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
+                                | stat.S_IRGRP | stat.S_IXGRP
+                                | stat.S_IROTH | stat.S_IXOTH)
+                        # else this will throw a permission error
+                        # but someone choose to create a symlink
+                    except IOError as e:
+                        log.error('error changing permissions on hoook %s: %s',
+                                  hook_file, e)
                 except IOError as e:
                     log.error('error writing hook %s: %s', hook_file, e)
 
diff -r b9b53e25a08d -r 672e57b165d0 kallithea/templates/py/git_post_receive_hook.py
--- a/kallithea/templates/py/git_post_receive_hook.py	Mon Oct 19 12:18:28 2020 +0200
+++ b/kallithea/templates/py/git_post_receive_hook.py	Tue Oct 20 15:51:43 2020 +0200
@@ -9,6 +9,8 @@
 """
 
 import os
+import pathlib
+import subprocess
 import sys
 
 import kallithea.lib.hooks
@@ -30,6 +32,11 @@
 def main():
     repo_path = os.path.abspath('.')
     git_stdin_lines = sys.stdin.readlines()
+    full_stdin = ''.join(git_stdin_lines)
+
+    for file in sorted(pathlib.Path('hooks').glob('post-receive.kallithea-extern.*.*')):
+        subprocess.run([file], input=full_stdin, universal_newlines=True)
+
     sys.exit(kallithea.lib.hooks.handle_git_post_receive(repo_path, git_stdin_lines))
 
 
diff -r b9b53e25a08d -r 672e57b165d0 kallithea/templates/py/git_pre_receive_hook.py
--- a/kallithea/templates/py/git_pre_receive_hook.py	Mon Oct 19 12:18:28 2020 +0200
+++ b/kallithea/templates/py/git_pre_receive_hook.py	Tue Oct 20 15:51:43 2020 +0200
@@ -9,6 +9,8 @@
 """
 
 import os
+import pathlib
+import subprocess
 import sys
 
 import kallithea.lib.hooks
@@ -30,6 +32,11 @@
 def main():
     repo_path = os.path.abspath('.')
     git_stdin_lines = sys.stdin.readlines()
+    full_stdin = ''.join(git_stdin_lines)
+
+    for file in sorted(pathlib.Path('hooks').glob('pre-receive.kallithea-extern.*.*')):
+        subprocess.run([file], input=full_stdin, universal_newlines=True)
+
     sys.exit(kallithea.lib.hooks.handle_git_pre_receive(repo_path, git_stdin_lines))
 
 



More information about the kallithea-general mailing list