index: handle index[-1] as nullid more explicitly
authorMartin von Zweigbergk <martinvonz@google.com>
Fri, 20 Jul 2018 09:53:54 -0700
changeset 38847 f3d394ea17db
parent 38846 f738c502e43b
child 38848 c0d411ea6639
index: handle index[-1] as nullid more explicitly I find it more intuitive to check if "pos == -1" than to first add the index length (which includes one extra item for the nullid) and compare that to "length - 1". However, because test-parseindex2.py compares the whole index (up to len(index)-1), we need to also preserve that other check for a little while more. I'll remove it soon. Differential Revision: https://phab.mercurial-scm.org/D4018
mercurial/cext/revlog.c
mercurial/pure/parsers.py
--- a/mercurial/cext/revlog.c	Fri Jul 20 22:26:28 2018 -0700
+++ b/mercurial/cext/revlog.c	Fri Jul 20 09:53:54 2018 -0700
@@ -158,6 +158,11 @@
 	Py_ssize_t length = index_length(self);
 	PyObject *entry;
 
+	if (pos == -1 || pos == length - 1) {
+		Py_INCREF(nullentry);
+		return nullentry;
+	}
+
 	if (pos < 0)
 		pos += length;
 
@@ -166,11 +171,6 @@
 		return NULL;
 	}
 
-	if (pos == length - 1) {
-		Py_INCREF(nullentry);
-		return nullentry;
-	}
-
 	if (pos >= self->length - 1) {
 		PyObject *obj;
 		obj = PyList_GET_ITEM(self->added, pos - self->length + 1);
--- a/mercurial/pure/parsers.py	Fri Jul 20 22:26:28 2018 -0700
+++ b/mercurial/pure/parsers.py	Fri Jul 20 09:53:54 2018 -0700
@@ -55,9 +55,9 @@
         return i
 
     def __getitem__(self, i):
+        if i == -1 or i == len(self) - 1:
+            return (0, 0, 0, -1, -1, -1, -1, nullid)
         i = self._fix_index(i)
-        if i == len(self) - 1:
-            return (0, 0, 0, -1, -1, -1, -1, nullid)
         if i >= self._lgt:
             return self._extra[i - self._lgt]
         index = self._calculate_index(i)