changeset 6612:94bf2f307b75 stable

topic: check that topic namespace names are human-readable like topics
author Anton Shestakov <av6@dwimlabs.net>
date Mon, 11 Dec 2023 16:51:27 -0300
parents 1dc008b6ad21
children 5199463aef07
files hgext3rd/topic/__init__.py tests/test-namespaces.t
diffstat 2 files changed, 22 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/hgext3rd/topic/__init__.py	Mon Dec 11 16:27:09 2023 -0300
+++ b/hgext3rd/topic/__init__.py	Mon Dec 11 16:51:27 2023 -0300
@@ -1841,6 +1841,18 @@
         if b'/' in tns:
             raise error.Abort(_(b"topic namespace cannot contain '/' character"))
         scmutil.checknewlabel(repo, tns, b'topic namespace')
+
+        helptxt = _(b"topic namespace names can only consist of alphanumeric, "
+                    b"'-', '_' and '.' characters")
+        try:
+            utns = encoding.unifromlocal(tns)
+        except error.Abort:
+            # Maybe we should allow these topic names as well, as long as they
+            # don't break any other rules
+            utns = ''
+        rmatch = re.match(r'[-_.\w]+', utns, re.UNICODE)
+        if not utns or not rmatch or rmatch.group(0) != utns:
+            raise compat.InputError(_(b"invalid topic namespace name: '%s'") % tns, hint=helptxt)
     ctns = repo.currenttns
     _changecurrenttns(repo, tns)
     if ctns == b'none' and tns != b'none':
--- a/tests/test-namespaces.t	Mon Dec 11 16:27:09 2023 -0300
+++ b/tests/test-namespaces.t	Mon Dec 11 16:51:27 2023 -0300
@@ -43,6 +43,16 @@
   $ hg --encoding latin1 debug-topic-namespaces
   \xe6 (esc)
 
+  $ hg --encoding utf-8 debug-topic-namespace ©
+  abort: invalid topic namespace name: '\xc2\xa9' (esc)
+  (topic namespace names can only consist of alphanumeric, '-', '_' and '.' characters)
+  [10]
+
+  $ hg --encoding latin1 debug-topic-namespace æ
+  abort: invalid topic namespace name: '\xc3\xa6' (esc)
+  (topic namespace names can only consist of alphanumeric, '-', '_' and '.' characters)
+  [10]
+
   $ hg branches
 
   $ hg debug-topic-namespace --clear