D5295: branchmap: define a hasbranch() to find whether a branch exists or not

Yuya Nishihara yuya at tcha.org
Sat Nov 24 23:03:40 EST 2018


I don't think it's good idea to re-scan the cache file per hasbranch() call.
Instead, we'll probably need a lazy parser backed by a in-memory cache data.
The current cache file format is text-based, which wouldn't be easily bisected
without loading (or memmap) the whole content.

> +def hasbranch(repo, branchname):
> +    """check whether a branchname exists in the repo or not by reading the
> +    branchmap cache"""
> +
> +    if not repo.cachevfs.exists(_filename(repo)):
> +        # branchmap file is not present, let's go repo.branchmap() route which
> +        # will create that file
> +        return branchname in repo.branchmap()
> +
> +    # TODO: implement binary-search here for faster search
> +    with repo.cachevfs(_filename(repo)) as f:
> +        f = repo.cachevfs(_filename(repo))
> +        lineiter = iter(f)
> +        next(lineiter).rstrip('\n').split(" ", 2)

Need to check if the cache file is valid.

> +        for l in lineiter:
> +            l = l.rstrip('\n')
> +            if not l:
> +                continue
> +            label = l.split(" ", 2)[2]
> +            label = encoding.tolocal(label.strip())
> +            if label == branchname:

And maybe need to check if the node exists.

> +                return True
> +    return False


More information about the Mercurial-devel mailing list