Patchwork [2,of,2] grep: fix hash(linestate) to not include linenum

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 9, 2020, 12:20 p.m.
Message ID <d9a83a225c54affd48b5.1599654035@mimosa>
Download mbox | patch
Permalink /patch/47113/
State Accepted
Headers show

Comments

Yuya Nishihara - Sept. 9, 2020, 12:20 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1599619278 -32400
#      Wed Sep 09 11:41:18 2020 +0900
# Node ID d9a83a225c54affd48b570c486bd4f08a6877252
# Parent  cc233fb2375feef75d3f9619ed69cd5d5dea89dd
grep: fix hash(linestate) to not include linenum

linestate.__eq__() just compares the line values whereas __hash__() does
hash both self.line and self.linenum, which violates the rule. __hash__()
was added at fb502719c75c, "python 2.6 compatibility: add __hash__ to
classes that have __eq__" with no additional remarks, so this would probably
be a simple mistake.

The test output changed because difflib.SequenceMatcher() internally uses
a dict. As you can see, the line "export" is unchanged at the revision 2,
so the new output is correct.

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -3400,7 +3400,7 @@  def grep(ui, repo, pattern, *pats, **opt
             self.colend = colend
 
         def __hash__(self):
-            return hash((self.linenum, self.line))
+            return hash(self.line)
 
         def __eq__(self, other):
             return self.line == other.line
diff --git a/tests/test-grep.t b/tests/test-grep.t
--- a/tests/test-grep.t
+++ b/tests/test-grep.t
@@ -321,14 +321,61 @@  simple JSON without matching lines
    }
   ]
 
+diff of each revision for reference
+
+  $ hg log -p -T'== rev: {rev} ==\n'
+  == rev: 4 ==
+  diff -r 95040cfd017d -r 914fa752cdea port
+  --- a/port	Thu Jan 01 00:00:03 1970 +0000
+  +++ b/port	Thu Jan 01 00:00:04 1970 +0000
+  @@ -1,4 +1,3 @@
+   export
+   vaportight
+   import/export
+  -import/export
+  
+  == rev: 3 ==
+  diff -r 3b325e3481a1 -r 95040cfd017d port
+  --- a/port	Thu Jan 01 00:00:02 1970 +0000
+  +++ b/port	Thu Jan 01 00:00:03 1970 +0000
+  @@ -1,3 +1,4 @@
+   export
+   vaportight
+   import/export
+  +import/export
+  
+  == rev: 2 ==
+  diff -r 8b20f75c1585 -r 3b325e3481a1 port
+  --- a/port	Thu Jan 01 00:00:01 1970 +0000
+  +++ b/port	Thu Jan 01 00:00:02 1970 +0000
+  @@ -1,2 +1,3 @@
+  -import
+   export
+  +vaportight
+  +import/export
+  
+  == rev: 1 ==
+  diff -r f31323c92170 -r 8b20f75c1585 port
+  --- a/port	Thu Jan 01 00:00:00 1970 +0000
+  +++ b/port	Thu Jan 01 00:00:01 1970 +0000
+  @@ -1,1 +1,2 @@
+   import
+  +export
+  
+  == rev: 0 ==
+  diff -r 000000000000 -r f31323c92170 port
+  --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+  +++ b/port	Thu Jan 01 00:00:00 1970 +0000
+  @@ -0,0 +1,1 @@
+  +import
+  
+
 all
 
   $ hg grep --traceback --all -nu port port
   port:4:4:-:spam:import/export
   port:3:4:+:eggs:import/export
   port:2:1:-:spam:import
-  port:2:2:-:spam:export
-  port:2:1:+:spam:export
   port:2:2:+:spam:vaportight
   port:2:3:+:spam:import/export
   port:1:2:+:eggs:export
@@ -369,26 +416,6 @@  all JSON
     "user": "spam"
    },
    {
-    "change": "-",
-    "date": [2, 0],
-    "lineno": 2,
-    "node": "3b325e3481a1f07435d81dfdbfa434d9a0245b47",
-    "path": "port",
-    "rev": 2,
-    "texts": [{"matched": false, "text": "ex"}, {"matched": true, "text": "port"}],
-    "user": "spam"
-   },
-   {
-    "change": "+",
-    "date": [2, 0],
-    "lineno": 1,
-    "node": "3b325e3481a1f07435d81dfdbfa434d9a0245b47",
-    "path": "port",
-    "rev": 2,
-    "texts": [{"matched": false, "text": "ex"}, {"matched": true, "text": "port"}],
-    "user": "spam"
-   },
-   {
     "change": "+",
     "date": [2, 0],
     "lineno": 2,
@@ -460,8 +487,6 @@  follow
   port:4:4:-:spam:import/export
   port:3:4:+:eggs:import/export
   port:2:1:-:spam:import
-  port:2:2:-:spam:export
-  port:2:1:+:spam:export
   port:2:2:+:spam:vaportight
   port:2:3:+:spam:import/export
   port:1:2:+:eggs:export