2

I am using pygithub for analyzing repositories and iterating on all commits of a repository. But how can I extract the corresponding branch for each commit?

commits = repo.get_commits()
for commit in commits:
     # print(commit.branch) ... something in this way

Many thanks for your help!

Sebastian

  • did you try the get_branch method ? You have also an url param in the repo.get_commits() method....did you try to apply your complete url wit the branch like so : `github.com/YOUR_ACCOUNT/YOUR_REPO/tree/BRANCH_NAME` – jossefaz Apr 25 '20 at 18:46
  • Hi yAzou, after some investigation I found that: + `get_branches` provides a list of all branches but those provide only a reference on the HEAD + `html_url` of a commit does not contain any branch information but only the SHA. Any other ideas? – Poseidonius Apr 25 '20 at 19:10
  • And did you try with the url parameters in repo.get_commits() ? https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html?highlight=commits#github.Repository.Repository.get_commits – jossefaz Apr 25 '20 at 19:28
  • Sorry, I do not understand. The signature of `get_commits()` looks in the following way: `get_commits(sha=NotSet, path=NotSet, since=NotSet, until=NotSet, author=NotSet)`. How to integrate a url? – Poseidonius Apr 25 '20 at 19:38
  • I mean the path params.... like so : get_commits(path="tree/YOUR_DESIRED_BRANCH_NAME") – jossefaz Apr 25 '20 at 19:39
  • Many thanks for your patience! I evaluated the following: ``` branches = repo.get_branches() for branch in branches: print(branch.name) commits = repo.get_commits(path="tree/"+branch.name) for commit in commits: print(commit) ``` But I received only on commit per branch? What's wrong? – Poseidonius Apr 25 '20 at 19:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/212499/discussion-between-poseidonius-and-yazou). – Poseidonius Apr 25 '20 at 19:59
  • Could you share you answer please I have the same problem. – SebNik Jun 27 '20 at 07:47
  • Hi SebNik, at the end I identified all occurring branches and iterate them step by step. At the end every commit referenced the branches in which it appears. – Poseidonius Jun 28 '20 at 12:41
  • @Poseidonius after you managed to get the paginated list of commits, are you able to iterate over it? i've got the paginated list but i can't seem to iterate – intrigued_66 Jan 10 '22 at 12:13

0 Answers0