[PATCH 2 of 4 STABLE] i18n: use "encoding.lower()" to normalize specified keywords for log searching

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Sat Dec 24 08:31:12 CST 2011


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1324735129 -32400
# Branch stable
# Node ID 4e856321cb9978bd2d00685b489276d127e0df4c
# Parent  01fe3735501eeca6004a36d1f961ec50414ef163
i18n: use "encoding.lower()" to normalize specified keywords for log searching

some problematic encoding (e.g.: cp932) uses ASCII alphabet characters
in byte sequence of multi byte characters.

"str.lower()" on such byte sequence may treat distinct characters as
same one, and cause unexpected log matching.

this patch uses "encoding.lower()" instead of "str.lower()" to
normalize strings for compare.

diff -r 01fe3735501e -r 4e856321cb99 mercurial/commands.py
--- a/mercurial/commands.py	Sat Dec 24 22:58:49 2011 +0900
+++ b/mercurial/commands.py	Sat Dec 24 22:58:49 2011 +0900
@@ -3865,14 +3865,21 @@
             return
         if df and not df(ctx.date()[0]):
             return
-        if opts['user'] and not [k for k in opts['user']
-                                 if k.lower() in ctx.user().lower()]:
-            return
+
+        lower = encoding.lower
+        if opts.get('user'):
+            luser = lower(ctx.user())
+            for k in [lower(x) for x in opts['user']]:
+                if (k in luser):
+                    break
+            else:
+                return
         if opts.get('keyword'):
-            for k in [kw.lower() for kw in opts['keyword']]:
-                if (k in ctx.user().lower() or
-                    k in ctx.description().lower() or
-                    k in " ".join(ctx.files()).lower()):
+            luser = lower(ctx.user())
+            ldesc = lower(ctx.description())
+            lfiles = lower(" ".join(ctx.files()))
+            for k in [lower(x) for x in opts['keyword']]:
+                if (k in luser or k in ldesc or k in lfiles):
                     break
             else:
                 return
diff -r 01fe3735501e -r 4e856321cb99 tests/test-log-i18n.t
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-log-i18n.t	Sat Dec 24 22:58:49 2011 +0900
@@ -0,0 +1,41 @@
+  $ hg init repo1
+  $ cd repo1
+
+  $ HGENCODING=utf-8
+  $ export HGENCODING
+
+# unicode: cp932:
+# u30A2    0x83 0x41(= 'A')
+# u30C2    0x83 0x61(= 'a')
+
+create rev #0
+  $ echo a > text
+  $ hg add text
+  $ python -c "print u'hg commit -u \'\u30A2\' -m none'.encode('utf-8')" | sh
+
+create rev #1
+  $ echo b > text
+  $ python -c "print u'hg commit -u \'\u30C2\' -m none'.encode('utf-8')" | sh
+
+create rev #2
+  $ echo c > text
+  $ python -c "print u'hg commit -u none -m \'\u30A2\''.encode('utf-8')" | sh
+
+create rev #3
+  $ echo d > text
+  $ python -c "print u'hg commit -u none -m \'\u30C2\''.encode('utf-8')" | sh
+
+change encoding to cp932
+  $ HGENCODING=cp932
+
+  $ python -c "print u'hg log -u \'\u30A2\' --template \'{rev}\\n\''.encode('cp932')" | sh
+  0
+  $ python -c "print u'hg log -u \'\u30C2\' --template \'{rev}\\n\''.encode('cp932')" | sh
+  1
+
+  $ python -c "print u'hg log -k \'\u30A2\' --template \'{rev}\\n\''.encode('cp932')" | sh
+  2
+  0
+  $ python -c "print u'hg log -k \'\u30C2\' --template \'{rev}\\n\''.encode('cp932')" | sh
+  3
+  1


More information about the Mercurial-devel mailing list