2

I'd need to walk on entire repo's branches commits. I have tried this but with no success. :

for branch_name in list(repo.branches.remote):
   try:
        branch = repo.lookup_branch(branch_name)
        ref = repo.lookup_reference(branch.name)
        repo.checkout(ref)

        for commit in repo.walk(branch.target, pygit2.GIT_SORT_TIME):
            print(commit.id.hex)

Any help would be appreciated, thanks.

user2326167
  • 107
  • 1
  • 6

1 Answers1

1

This is what I have:

def iterate_repository(dir: str) -> None:
    repo = pygit2.Repository(dir)
    for branch_name in list(repo.branches.remote):
        branch = repo.branches.get(branch_name)
        latest_commit_id = branch.target
        latest_commit = repo.revparse_single(latest_commit_id.hex)

        for commit in repo.walk(latest_commit.id, pygit2.GIT_SORT_TIME):
            print(commit.id.hex)

Expanding from that should be relatively easy. What I do is gather statistics from files included in a commit.

Jari Turkia
  • 1,184
  • 1
  • 21
  • 37
  • This code sometimes raises `AttributeError: 'str' object has no attribute 'hex'`. `branch.target` sometimes has type `str`. – wchistow May 29 '23 at 08:55