from pathlib import Path  # 3.x


def group_parts_by_existence(docroot, path):
    """Return continguous subpaths grouped by existence

    Even-numbered groups, starting from zero are real. Query strings are
    dropped entirely. Two example outcomes where the docroot is
    ``/docroot``::

        # uri = "/gitroot/repo.git/info/refs"
        # fs = "/docroot/gitroot/repo.git/info"
        ["gitroot", "repo.git", "info"], ["refs"]

        # uri = "/gitroot/my_ns/repo.git?service=foo"
        # fs  = "/docroot/gitroot/repo.git"
        ["gitroot"], ["my_ns"], ["repo.git"]

        # uri = "/my_ns/repo.git?service=foo"
        # fs  = "/docroot/repo.git"
        [], ["my_ns"], ["repo.git"]

    """

    path, *_ = path.split("?")
    trunk = Path(docroot).resolve(True)
    parts = iter(Path(path.strip("/")).parts)
    out = []
    current = []

    for p in parts:
        is_real = (trunk / p).exists()
        is_fake = not is_real
        is_odd = bool(len(out) % 2)
        is_even = not is_odd

        if is_real:
            trunk /= p

        if (is_odd and is_real) or (is_even and is_fake):
            out.append(tuple(current))
            current = [p]
            continue
        current.append(p)

    out.append(tuple(current))
    return tuple(out)