changeset 7327:9937ae52f167

hg: set encoding to utf-8 by default to always show unicode characters correctly Unicode characters would be shown as '?' if Kallithea was launched in a LANG=C environment (or similar). The problem could be solved by setting HGENCODING before launching Kallithea or before importing Mercurial. These are often not good solutions. Instead, introduce a hgencoding config setting that triggers monkey patching of Mercurial.
author Mads Kiilerich <mads@kiilerich.com>
date Sat, 09 Jun 2018 16:28:33 +0200
parents abaf8e1033a6
children e2519d2e74c2
files development.ini docs/setup.rst kallithea/config/app_cfg.py kallithea/lib/paster_commands/template.ini.mako
diffstat 4 files changed, 15 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/development.ini	Sat May 19 22:00:41 2018 +0200
+++ b/development.ini	Sat Jun 09 16:28:33 2018 +0200
@@ -160,6 +160,9 @@
 ## can be also a comma separated list of encoding in case of mixed encodings
 default_encoding = utf8
 
+## Set Mercurial encoding, similar to setting HGENCODING before launching Kallithea
+hgencoding = utf-8
+
 ## issue tracker for Kallithea (leave blank to disable, absent for default)
 #bugtracker = https://bitbucket.org/conservancy/kallithea/issues
 
--- a/docs/setup.rst	Sat May 19 22:00:41 2018 +0200
+++ b/docs/setup.rst	Sat Jun 09 16:28:33 2018 +0200
@@ -633,6 +633,9 @@
 library is installed. If ``chardet`` is detected Kallithea will fallback to it
 when there are encode/decode errors.
 
+The Mercurial encoding is configurable as ``hgencoding``. It is similar to
+setting the ``HGENCODING`` environment variable, but will override it.
+
 
 Celery configuration
 --------------------
@@ -894,7 +897,6 @@
 .. code-block:: python
 
     import os
-    os.environ["HGENCODING"] = "UTF-8"
     os.environ['PYTHON_EGG_CACHE'] = '/srv/kallithea/.egg-cache'
 
     # sometimes it's needed to set the current dir
--- a/kallithea/config/app_cfg.py	Sat May 19 22:00:41 2018 +0200
+++ b/kallithea/config/app_cfg.py	Sat Jun 09 16:28:33 2018 +0200
@@ -28,6 +28,7 @@
 from alembic.script.base import ScriptDirectory
 from alembic.migration import MigrationContext
 from sqlalchemy import create_engine
+import mercurial
 
 from kallithea.lib.middleware.https_fixup import HttpsFixup
 from kallithea.lib.middleware.simplegit import SimpleGit
@@ -119,6 +120,11 @@
 def setup_configuration(app):
     config = app.config
 
+    # Mercurial sets encoding at module import time, so we have to monkey patch it
+    hgencoding = config.get('hgencoding')
+    if hgencoding:
+        mercurial.encoding.encoding = hgencoding
+
     if config.get('ignore_alembic_revision', False):
         log.warn('database alembic revision checking is disabled')
     else:
--- a/kallithea/lib/paster_commands/template.ini.mako	Sat May 19 22:00:41 2018 +0200
+++ b/kallithea/lib/paster_commands/template.ini.mako	Sat Jun 09 16:28:33 2018 +0200
@@ -254,6 +254,9 @@
 <%text>## can be also a comma separated list of encoding in case of mixed encodings</%text>
 default_encoding = utf8
 
+<%text>## Set Mercurial encoding, similar to setting HGENCODING before launching Kallithea</%text>
+hgencoding = utf-8
+
 <%text>## issue tracker for Kallithea (leave blank to disable, absent for default)</%text>
 #bugtracker = https://bitbucket.org/conservancy/kallithea/issues