pygit2 icon indicating copy to clipboard operation
pygit2 copied to clipboard

Add RemoteCollection.names() iterator

Open agausmann opened this issue 3 years ago • 2 comments

Motivation

This names() iterator provides a big benefit in performance in my use case, where I want to test whether or not a remote exists for a large number of potential remote names (hundreds)

The first code I used looked something like:

try:
    repo.remotes[remote_name]
except KeyError:
    repo.remotes.create(remote_name, remote_url)

However, this performs a lookup for all remote names, which was prohibitively expensive. So I decided to try collecting all the names into a set beforehand:

existing_remotes = set(remote.name for remote in repo.remotes)

for remote_name, remote_url in wanted_remotes:
    if remote_name not in existing_remotes:
        repo.remotes.create(remote_name, remote_url)

However, this still performs a lookup for each remote to create the Remote objects yielded by the iterator.

With RemoteCollection.names(), I can construct the set much more efficiently:

existing_remotes = set(repo.remotes.names())

agausmann avatar Sep 25 '22 20:09 agausmann

Thanks, please add a unit test.

jdavid avatar Sep 26 '22 08:09 jdavid

For reference I think iteration should yield the names, like references/branches do, but that breaks compatibility. Also I'm unsure which is best names() or keys(), names is more like git, keys is more like python.

jdavid avatar Sep 26 '22 08:09 jdavid