This is a simple git repository. I have tagged the commits with numbers for easy referencing. The repo has the following branches:
- master: 13 commits (1,2,3,4,5,6,7,8,9,10,11,12,13)
- new_branch: 8 commits (1,2,3,4,5,6,14,15)
- test_branch: 3 commits (1,2,3)
- yet_another_branch: 14 commits (1,2,3,4,5,6,7,8,9,10,11,12,16,17)
Commit 5,6 belong to a pull request thus the blue portion with 5,6 is not a branch.
Please note that commit 1,2 are considered as part of all branches, but I want to consider all black colored commits as part of the master. Similarly, for 'test-branch', I want to consider only commit 3 as part of the branch.
from git import Repo
git_url = "https://github.com/unimamun/test-repo.git"
repo_dir = "/mnt/hdd/aam/J2_Repos/test-repo/test-repo"
repo = Repo.clone_from(git_url, repo_dir)
# get all commits by branches
def get_commits(repo, ref_name):
commits = []
for commit in repo.iter_commits(rev=ref_name):
commits.append(commit)
return commits
print('\nCommits in Branches:')
for ref in repo.references:
print(ref.name,': ', str(len(get_commits(repo, ref.name))))
print('\nCommits in master:')
commits = list(repo.iter_commits('master'))
commits.reverse()
i = 0
for commit in commits:
i += 1
print(i,': ', commit.hexsha)
# to see parents of the commit
#print('Parents: ',commit.parents)
From the above code, I have the following output:
Commits in Branches:
master : 13
origin/HEAD : 13
origin/master : 13
origin/new_branch : 8
origin/test-branch : 3
origin/yet_another_branch : 14
Commits in master:
1 : 694df9fee2f9c03a33979725e76a484bce1738a0
2 : c0fe1b76131b7fcb103f171fd93d85cda17b756c
3 : 0199ad335f65d52a2895a678a19e209e1e16a1a7
4 : dd0903259b0aadbf2d8fb00e566eee014264f7c0
5 : 7ed55c51e2527f47bc6344cd960ff5beb90cc65d
6 : d10f19c85fbc1c27b7719a2dc64989255697181d
7 : c41bdfaeae1f801776420ce161ca2555dffc5aad
8 : 56b5d6e1831a477c79e0fd336acc96ca266d5dea
9 : 6305a72d4e257ebe74b10ca538906f1eceb091bf
10 : 4c5d1ebe5f2f8168ee8bf4a969855821d04caf09
11 : 362bc52be00af3fb917196cf27a8ddc0bb8fd4ba
12 : 5a70a46394eb08b4b48f9eb05798048ca7269a9d
13 : f4a8bdd318b2678191d06616a55df26416a28363
I want the following output. So that 'master' is printed for every black dots in the figure and other branch names for non-black color commits (in this case for green colored commit 3, test-branch should be printed)
Commits in master:
1 : 694df9fee2f9c03a33979725e76a484bce1738a0 master
2 : c0fe1b76131b7fcb103f171fd93d85cda17b756c master
3 : 0199ad335f65d52a2895a678a19e209e1e16a1a7 test-branch
4 : dd0903259b0aadbf2d8fb00e566eee014264f7c0 master
5 : 7ed55c51e2527f47bc6344cd960ff5beb90cc65d master
6 : d10f19c85fbc1c27b7719a2dc64989255697181d master
7 : c41bdfaeae1f801776420ce161ca2555dffc5aad master
8 : 56b5d6e1831a477c79e0fd336acc96ca266d5dea master
9 : 6305a72d4e257ebe74b10ca538906f1eceb091bf master
10 : 4c5d1ebe5f2f8168ee8bf4a969855821d04caf09 master
11 : 362bc52be00af3fb917196cf27a8ddc0bb8fd4ba master
12 : 5a70a46394eb08b4b48f9eb05798048ca7269a9d master
13 : f4a8bdd318b2678191d06616a55df26416a28363 master
I need to iterate from commit 1 to 13 and along the way I need to determine which commit belong to which branch. Thanks a lot.