Changeset - 20e850093f1c
[Not reviewed]
default
0 4 0
Mads Kiilerich (mads) - 5 years ago 2021-01-11 23:28:09
mads@kiilerich.com
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.
4 files changed with 18 insertions and 3 deletions:
0 comments (0 inline, 0 general)
docs/setup.rst
Show inline comments
 
@@ -339,26 +339,28 @@ Hook management
 
---------------
 

	
 
Custom Mercurial hooks can be managed in a similar way to that used in ``.hgrc`` files.
 
To manage hooks, choose *Admin > Settings > Hooks*.
 

	
 
To add another custom hook simply fill in the first textbox with
 
``<name>.<hook_type>`` and the second with the hook path. Example hooks
 
can be found in ``kallithea.lib.hooks``.
 

	
 
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
 
-------------------------
 

	
 
By default, Kallithea uses UTF-8 encoding.
 
This is configurable as ``default_encoding`` in the .ini file.
 
This affects many parts in Kallithea including user names, filenames, and
 
encoding of commit messages. In addition Kallithea can detect if the ``chardet``
 
library is installed. If ``chardet`` is detected Kallithea will fallback to it
 
when there are encode/decode errors.
 

	
docs/upgrade.rst
Show inline comments
 
@@ -227,21 +227,24 @@ clear out your log file so that new erro
 

	
 
10. Reinstall internal Git repository hooks
 
-------------------------------------------
 

	
 
It is possible that an upgrade involves changes to the Git hooks installed by
 
Kallithea. As these hooks are created inside the repositories on the server
 
filesystem, they are not updated automatically when upgrading Kallithea itself.
 

	
 
To update the hooks of your Git repositories, run::
 

	
 
    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*
 
* Select the checkbox *Install Git hooks*
 
* Click the button *Rescan repositories*
 

	
 
.. note::
 
    Kallithea does not use hooks on Mercurial repositories. This step is thus
 
    not necessary if you only have Mercurial repositories.
docs/usage/troubleshooting.rst
Show inline comments
 
@@ -43,24 +43,25 @@ Troubleshooting
 
|
 

	
 
:Q: **How can I use hooks in Kallithea?**
 
:A: If using Mercurial, use *Admin > Settings > Hooks* to install
 
    global hooks. Inside the hooks, you can use the current working directory to
 
    control different behaviour for different repositories.
 

	
 
    If using Git, install the hooks manually in each repository, for example by
 
    creating a file ``gitrepo/hooks/pre-receive``.
 
    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.
 

	
 
|
 

	
 
:Q: **Kallithea is slow for me, how can I make it faster?**
 
:A: See the :ref:`performance` section.
 

	
 
|
 

	
 
:Q: **UnicodeDecodeError on Apache mod_wsgi**
kallithea/templates/py/git_post_receive_hook.py
Show inline comments
 
"""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 subprocess
 
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.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__':
 
    main()
0 comments (0 inline, 0 general)