changeset 8838:761efb9624fa

hooks: use internal Mercurial module for update after push Don't rely on having a working 'hg' command in $PATH.
author Mads Kiilerich <mads@kiilerich.com>
date Sun, 10 Jan 2021 23:53:50 +0100
parents fff835606650
children 25c51511c8eb
files kallithea/bin/vcs_hooks.py kallithea/lib/db_manage.py kallithea/lib/utils.py kallithea/tests/functional/test_admin_settings.py
diffstat 4 files changed, 24 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/bin/vcs_hooks.py	Mon Jan 11 13:29:23 2021 +0100
+++ b/kallithea/bin/vcs_hooks.py	Sun Jan 10 23:53:50 2021 +0100
@@ -25,9 +25,11 @@
 :license: GPLv3, see LICENSE.md for more details.
 """
 
+import logging
 import os
 import sys
 
+import mercurial.hg
 import mercurial.scmutil
 import paste.deploy
 
@@ -40,6 +42,9 @@
 from kallithea.model import db
 
 
+log = logging.getLogger(__name__)
+
+
 def repo_size(ui, repo, hooktype=None, **kwargs):
     """Show size of Mercurial repository.
 
@@ -60,6 +65,21 @@
     ui.status(safe_bytes(msg))
 
 
+def update(ui, repo, hooktype=None, **kwargs):
+    """Update repo after push. The equivalent to 'hg update' but using the same
+    Mercurial as everything else.
+
+    Called as Mercurial hook changegroup.update after push.
+    """
+    try:
+        ui.pushbuffer(error=True, subproc=True)
+        rev = brev = None
+        mercurial.hg.updatetotally(ui, repo, rev, brev)
+    finally:
+        s = ui.popbuffer()  # usually just "x files updated, x files merged, x files removed, x files unresolved"
+        log.info('%s update hook output: %s', safe_str(repo.root), safe_str(s).rstrip())
+
+
 def pull_action(ui, repo, **kwargs):
     """Logs user pull action
 
--- a/kallithea/lib/db_manage.py	Mon Jan 11 13:29:23 2021 +0100
+++ b/kallithea/lib/db_manage.py	Sun Jan 10 23:53:50 2021 +0100
@@ -244,8 +244,8 @@
         ui_config = [
             ('paths', '/', repo_root_path, True),
             #('phases', 'publish', 'false', False)
-            ('hooks', db.Ui.HOOK_UPDATE, 'hg update >&2', False),
-            ('hooks', db.Ui.HOOK_REPO_SIZE, 'python:', True),  # the actual value doesn't matter
+            ('hooks', db.Ui.HOOK_UPDATE, 'python:', False),  # the actual value in db doesn't matter
+            ('hooks', db.Ui.HOOK_REPO_SIZE, 'python:', True),  # the actual value in db doesn't matter
             ('extensions', 'largefiles', '', True),
             ('largefiles', 'usercache', os.path.join(repo_root_path, '.cache', 'largefiles'), True),
             ('extensions', 'hggit', '', False),
--- a/kallithea/lib/utils.py	Mon Jan 11 13:29:23 2021 +0100
+++ b/kallithea/lib/utils.py	Sun Jan 10 23:53:50 2021 +0100
@@ -283,6 +283,8 @@
     baseui.setconfig(b'hooks', b'outgoing.kallithea_pull_action', b'python:kallithea.bin.vcs_hooks.pull_action')
     if baseui.config(b'hooks', ascii_bytes(db.Ui.HOOK_REPO_SIZE)):  # ignore actual value
         baseui.setconfig(b'hooks', ascii_bytes(db.Ui.HOOK_REPO_SIZE), b'python:kallithea.bin.vcs_hooks.repo_size')
+    if baseui.config(b'hooks', ascii_bytes(db.Ui.HOOK_UPDATE)):  # ignore actual value
+        baseui.setconfig(b'hooks', ascii_bytes(db.Ui.HOOK_UPDATE), b'python:kallithea.bin.vcs_hooks.update')
 
     if repo_path is not None:
         # Note: MercurialRepository / mercurial.localrepo.instance will do this too, so it will always be possible to override db settings or what is hardcoded above
--- a/kallithea/tests/functional/test_admin_settings.py	Mon Jan 11 13:29:23 2021 +0100
+++ b/kallithea/tests/functional/test_admin_settings.py	Sun Jan 10 23:53:50 2021 +0100
@@ -98,7 +98,6 @@
         self.checkSessionFlash(response, 'Builtin hooks are read-only')
         response = response.follow()
         response.mustcontain('changegroup.update')
-        response.mustcontain('hg update &gt;&amp;2')
 
     def test_index_search(self):
         self.log_user()