def ancestor(self, a, b):
        """calculate the least common ancestor of nodes a and b"""

        # fast path, check if it is a descendant
        a, b = self.rev(a), self.rev(b)
        start, end = sorted((a, b))
        if self.descendant(start, end):
            return self.node(start)

        def parents(rev):
            return [p for p in self.parentrevs(rev) if p != nullrev]

        c = ancestor.ancestor(a, b, parents)
        if c is None:
            return nullid

        return self.node(c)