changeset 8846:20e850093f1c

hooks: on Git, invoke hooks/post-receive-custom from hooks/post-receive Make it possible for admins to install all kinds of hooks. Based on a patch by Tim Ooms. A more generic solution would be nice, but for now we aim for this minimal solution.
author Mads Kiilerich <mads@kiilerich.com>
date Mon, 11 Jan 2021 23:28:09 +0100
parents d8e65780dbe9
children 73f3ed6a91d9
files docs/setup.rst docs/upgrade.rst docs/usage/troubleshooting.rst kallithea/templates/py/git_post_receive_hook.py
diffstat 4 files changed, 18 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/docs/setup.rst	Mon Jan 11 14:25:52 2021 +0100
+++ b/docs/setup.rst	Mon Jan 11 23:28:09 2021 +0100
@@ -348,8 +348,10 @@
 Kallithea will also use some hooks internally. They cannot be modified, but
 some of them can be enabled or disabled in the *VCS* section.
 
-Kallithea has no support for custom Git hooks. Kallithea will install and use
-Git hooks internally, and they might collide with manually installed hooks.
+Kallithea does not actively support custom Git hooks, but hooks can be installed
+manually in the file system. Kallithea will install and use the
+``post-receive`` Git hook internally, but it will then invoke
+``post-receive-custom`` if present.
 
 
 Changing default encoding
--- a/docs/upgrade.rst	Mon Jan 11 14:25:52 2021 +0100
+++ b/docs/upgrade.rst	Mon Jan 11 23:28:09 2021 +0100
@@ -236,6 +236,9 @@
 
     kallithea-cli repo-scan -c my.ini --install-git-hooks
 
+Watch out for warnings like ``skipping overwriting hook file X``, then fix it
+and rerun, or consider using ``--overwrite-git-hooks`` instead.
+
 Or:
 
 * Go to *Admin > Settings > Remap and Rescan*
--- a/docs/usage/troubleshooting.rst	Mon Jan 11 14:25:52 2021 +0100
+++ b/docs/usage/troubleshooting.rst	Mon Jan 11 23:28:09 2021 +0100
@@ -52,6 +52,7 @@
     Note that Kallithea uses the ``post-receive`` hook internally.
     Kallithea will not work properly if another post-receive hook is installed instead.
     You might also accidentally overwrite your own post-receive hook with the Kallithea hook.
+    Instead, put your post-receive hook in ``post-receive-custom``, and the Kallithea hook will invoke it.
 
     You can also use Kallithea-extensions to connect to callback hooks,
     for both Git and Mercurial.
--- a/kallithea/templates/py/git_post_receive_hook.py	Mon Jan 11 14:25:52 2021 +0100
+++ b/kallithea/templates/py/git_post_receive_hook.py	Mon Jan 11 23:28:09 2021 +0100
@@ -9,6 +9,7 @@
 """
 
 import os
+import subprocess
 import sys
 
 import kallithea.bin.vcs_hooks
@@ -30,7 +31,15 @@
 def main():
     repo_path = os.path.abspath('.')
     git_stdin_lines = sys.stdin.readlines()
-    sys.exit(kallithea.bin.vcs_hooks.post_receive(repo_path, git_stdin_lines))
+    status = kallithea.bin.vcs_hooks.post_receive(repo_path, git_stdin_lines)
+
+    custom_hook = os.path.join(repo_path, 'hooks', 'post-receive-custom')
+    custom_status = None
+    if os.access(custom_hook, os.X_OK):
+        result = subprocess.run([custom_hook], input=''.join(git_stdin_lines), universal_newlines=True)
+        custom_status = result.returncode
+
+    sys.exit(status or custom_status)
 
 
 if __name__ == '__main__':