Patchwork D7324: index: add a `rev` method (API)

login
register
mail settings
Submitter phabricator
Date Nov. 8, 2019, 1:34 p.m.
Message ID <differential-rev-PHID-DREV-qli5gycjzxztovun2rx2-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/42928/
State Superseded
Headers show

Comments

phabricator - Nov. 8, 2019, 1:34 p.m.
marmoute created this revision.
Herald added a reviewer: indygreg.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  The new `index.rev(node)` is to be preferred over using `node
  index.nodemap[node]`.
  
  This get us closer to be able to remove the `nodemap` attribute of the index.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D7324

AFFECTED FILES
  mercurial/cext/revlog.c
  mercurial/pure/parsers.py
  mercurial/revlog.py

CHANGE DETAILS




To: marmoute, indygreg, #hg-reviewers
Cc: mercurial-devel
phabricator - Nov. 8, 2019, 8:06 p.m.
This revision now requires changes to proceed.
indygreg added a comment.
indygreg requested changes to this revision.


  The code is fine. But since I've already left a number of comments on this series, I thought I'd ask about raising a better exception than `RevlogError`. I thought we had dedicated exceptions for revision not existing?

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7324/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7324

To: marmoute, indygreg, #hg-reviewers
Cc: mercurial-devel
phabricator - Nov. 9, 2019, 12:36 a.m.
marmoute added a comment.
marmoute requested review of this revision.


  In D7324#107591 <https://phab.mercurial-scm.org/D7324#107591>, @indygreg wrote:
  
  > The code is fine. But since I've already left a number of comments on this series, I thought I'd ask about raising a better exception than `RevlogError`. I thought we had dedicated exceptions for revision not existing?
  
  This is the exception currently raised by nodemap access and recognised by higher level code (that turn it into a better exception). Maybe this should change ? but this is out of scope from this series in my opinion.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7324/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7324

To: marmoute, indygreg, #hg-reviewers
Cc: mercurial-devel
phabricator - Nov. 9, 2019, 8:35 a.m.
martinvonz added a comment.


  Perhaps it's best to bump the version number in this patch and the `get_rev` patch as well? I don't feel strongly, but I'll queue all the `has_node` patches to start with.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7324/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7324

To: marmoute, indygreg, #hg-reviewers
Cc: martinvonz, mercurial-devel
phabricator - Nov. 9, 2019, 3:45 p.m.
This revision now requires changes to proceed.
indygreg added a comment.
indygreg requested changes to this revision.


  I'm fine ignoring the `RevlogError` quirk, since it already exists.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7324/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7324

To: marmoute, indygreg, #hg-reviewers
Cc: martinvonz, mercurial-devel

Patch

diff --git a/mercurial/revlog.py b/mercurial/revlog.py
--- a/mercurial/revlog.py
+++ b/mercurial/revlog.py
@@ -217,6 +217,12 @@ 
         """return True if the node exist in the index"""
         return node in self.nodemap
 
+    def rev(self, node):
+        """return a revision for a node
+
+        If the node is unknown, raise a RevlogError"""
+        return self.nodemap[node]
+
     def append(self, tup):
         self.nodemap[tup[7]] = len(self)
         super(revlogoldindex, self).append(tup)
diff --git a/mercurial/pure/parsers.py b/mercurial/pure/parsers.py
--- a/mercurial/pure/parsers.py
+++ b/mercurial/pure/parsers.py
@@ -59,6 +59,12 @@ 
         """return True if the node exist in the index"""
         return node in self.nodemap
 
+    def rev(self, node):
+        """return a revision for a node
+
+        If the node is unknown, raise a RevlogError"""
+        return self.nodemap[node]
+
     def _stripnodes(self, start):
         if 'nodemap' in vars(self):
             for r in range(start, len(self)):
diff --git a/mercurial/cext/revlog.c b/mercurial/cext/revlog.c
--- a/mercurial/cext/revlog.c
+++ b/mercurial/cext/revlog.c
@@ -2071,6 +2071,21 @@ 
 	return PyBool_FromLong((long)ret);
 }
 
+static PyObject *index_m_rev(indexObject *self, PyObject *val)
+{
+	char *node;
+	int rev;
+
+	if (node_check(val, &node) == -1)
+		return NULL;
+	rev = index_find_node(self, node, 20);
+	if (rev >= -1)
+		return PyInt_FromLong(rev);
+	if (rev == -2)
+		raise_revlog_error();
+	return NULL;
+}
+
 typedef uint64_t bitmask;
 
 /*
@@ -2731,6 +2746,8 @@ 
     {"get", (PyCFunction)index_m_get, METH_VARARGS, "get an index entry"},
     {"has_node", (PyCFunction)index_m_has_node, METH_O,
      "return True if the node exist in the index"},
+    {"rev", (PyCFunction)index_m_rev, METH_O,
+     "return `rev` associated with a node or raise RevlogError"},
     {"computephasesmapsets", (PyCFunction)compute_phases_map_sets, METH_VARARGS,
      "compute phases"},
     {"reachableroots2", (PyCFunction)reachableroots2, METH_VARARGS,