Skip to content

Fastest way to get the latest commit? #240

Closed
@ercpe

Description

@ercpe

I'm trying to get the latest commit for each item in a list of Tree or Blob objects (sth. like a repo browser). Currently i'm doing something like

repo = git.Repo("test.git")
tree = repo.tree()
for obj in tree:
    print obj, obj.path, repo.iter_commits(paths=obj.path, max_count=1).next()

but this is incredible slow. Another solution is to use repo.iter_commits() to fill a dict of path <-> commit:

for commit in repo.iter_commits(paths=paths):
    for f in commit.stats.files.keys():
        p = f[:f.index('/')] if '/' in f else f
        if p in latest_commits:
            continue

        latest_commits[p] = commit

However, in the worst case this one iterates over all commits in the entire repository (which is obviously a very bad idea).

Is there a faster solution?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions