Am trying to use Python to extract the list of all commit messages from a branch after a specific start commit. i couldn't find any useful function from GITPython api or maybe i just missed it. Could anyone please guide me with it?
Asked
Active
Viewed 1,212 times
0
-
1Assuming that by *after*, you mean *commits that are descendants of hash ID X* for some X, the problem is that this information is not available. What *is* available is *commits that are ancestors of hash ID Y* for some Y. You can add the constraint "*and* are descendants of X" or "*and* are *not* ancestors of X" (these two are different constraints!) in Git; how to do that in GitPython, I don't know off hand. – torek Mar 01 '19 at 22:47
-
You can do it in straight git easily enough, `git rev-list --ancestry-path base..tip`. – jthill Mar 01 '19 at 23:33
-
@torek yes thats exactly what i wanted.. But i would prefer to get that with api or atleast something which is automated. Necause my idea is to do other things post extracting the commit msgs.. – sbkhbk Mar 02 '19 at 00:00
-
@jthill but i wanted it through api which would be autoextracted bcos its just a part of my idea.. – sbkhbk Mar 02 '19 at 00:01
-
The key is that you must start not with X but with Y. What is the *end point?* Git starts at the end and works backwards. Having generated this full list of every commit reachable from the end point, you can now bring in X to subtract some commits. You haven't mentioned the end point Y, only the start point X. – torek Mar 02 '19 at 00:08
-
@torek i mentioned only start point because i want to get the commit msg starting from the start of the branch until the last commit. I thought by giving start commit i could iterate until the latest commit in branch. Sometimes it could be dynamic but starting commit would be constant.. – sbkhbk Mar 02 '19 at 00:12
-
*I thought by giving start commit i could iterate until the latest commit in branch.* That's the point, you *can't*. Git only works *backwards*, from ends to starts. It never works forwards, from starts to ends. (Moreover, the forward path depends on *where you want to end up!)* – torek Mar 02 '19 at 00:13
-
@torek ah okay got your point. Sorry for misunderstanding it :) so that being said. Any suggestions to do that through any api usinf python ? – sbkhbk Mar 02 '19 at 00:14
-
Unfortunately, I'm not that familiar with GitPython. The one that I did use once, some years ago, would let you run any arbitrary Git command and collect its output, in which case, you can use the Git command in jthill's comment. Note that this produces the "ancestors of Y, descendants of X" list, not the "ancestors of Y but not ancestors of X" list. – torek Mar 02 '19 at 00:15
2 Answers
1
You may want to try PyDriller, it's easier:
for commit in RepositoryMining("path_to_repo", from_commit="STARTING COMMIT").traverse_commits():
print(commit.msg)
If you want commits of a specific branch, add the parameter only_in_branch="BRANCH_NAME"
. Docs: http://pydriller.readthedocs.io/en/latest/

Davide Spadini
- 271
- 2
- 7
-
Interesting, i installed pydriller using pip, but am not sure what is the import statement to be used for this. From the git example, i used "from pydriller import RepositoryMining", but it says "no module named 'pydriller'".. However pip installation was successful.. – sbkhbk Mar 06 '19 at 14:33
-
i did a silly mistake while installing the package :).. it's resolved & it works as expected..Thanks much for sharing this library.. – sbkhbk Mar 06 '19 at 15:48
0
I also recommend to try PyDriller which is developed on top of git and lizard. You can extract all a lot information about the commit not only the commit message.
for commit in RepositoryMining("path_to_repo").traverse_commits():
print(commit.msg)
print(commit.hash)
print(commit.author)
print(commit.project_name)
and many more options. If you want to use the Python packages- git and lizard. Some of the useful links are - https://medium.com/@deepakr6242/using-python-to-extract-files-from-git-hub-repo-through-commits-id-2bdf76b2e0fd https://www.pylabz.com/2019/08/using-python-to-pull-files-of-git-hub.html

Guru Bhandari
- 109
- 1
- 7