comparison mercurial/pure/parsers.py @ 38851:781b2720d2ac

index: don't include nullid in len() I suspect the reason the nullid is in the index in the last position is that it lets index[i] for regular revision number, even when index was just a regular Python list. An alternative solution would have been to reserve revision number 0 for the null revision. I don't know why that wasn't done. Now that we have classes backing the index, we can easily make index[-1] get the nullid without having to put it last in the list and including it in the len(). This patch just hides the nullid -- it will still be accessible at index[len(index)]. I realize that this will be annoying when checking out across this commit for debugging (including bisection). Differential Revision: https://phab.mercurial-scm.org/D4022
author Martin von Zweigbergk <martinvonz@google.com>
date Fri, 20 Jul 2018 08:10:32 -0700
parents 6104b203bec8
children a1f934573c0b
comparison
equal deleted inserted replaced
38850:6104b203bec8 38851:781b2720d2ac
37 def offset_type(offset, type): 37 def offset_type(offset, type):
38 return int(int(offset) << 16 | type) 38 return int(int(offset) << 16 | type)
39 39
40 class BaseIndexObject(object): 40 class BaseIndexObject(object):
41 def __len__(self): 41 def __len__(self):
42 return self._lgt + len(self._extra) + 1 42 return self._lgt + len(self._extra)
43 43
44 def append(self, tup): 44 def append(self, tup):
45 self._extra.append(tup) 45 self._extra.append(tup)
46 46
47 def _fix_index(self, i): 47 def _fix_index(self, i):
48 if not isinstance(i, int): 48 if not isinstance(i, int):
49 raise TypeError("expecting int indexes") 49 raise TypeError("expecting int indexes")
50 if i < 0 or i >= len(self): 50 if i < 0 or i >= len(self) + 1:
51 raise IndexError 51 raise IndexError
52 return i 52 return i
53 53
54 def __getitem__(self, i): 54 def __getitem__(self, i):
55 if i == -1 or i == len(self) - 1: 55 if i == -1 or i == len(self):
56 return (0, 0, 0, -1, -1, -1, -1, nullid) 56 return (0, 0, 0, -1, -1, -1, -1, nullid)
57 i = self._fix_index(i) 57 i = self._fix_index(i)
58 if i >= self._lgt: 58 if i >= self._lgt:
59 return self._extra[i - self._lgt] 59 return self._extra[i - self._lgt]
60 index = self._calculate_index(i) 60 index = self._calculate_index(i)