import pathlib


def _match_path(p1, p2):
    """Compare two paths from right to left and return True if they could refer
    to the same file.

    As a special case, if the second argument is None, or empty, it is always
    considered a match. This simplifies query logic when the target does not
    have a path component.

    If p2 starts with "./" then the paths must math entirely. This to allow
    addressing in the case where a path is a prefix of another.
    """
    if not p2:
        return True

    part1 = pathlib.Path(p1).parts
    part2 = pathlib.Path(p2).parts

    if p2.startswith(".") and part2 and not part2[0].startswith("."):
        minlen = 0
    else:
        minlen = min(len(part1), len(part2))

    return part1[-minlen:] == part2[-minlen:]