git log --follow, the gitpython way

瘦欲@ 提交于 2021-02-05 18:05:14

问题


I am trying to access the commit history of a single file as in:

git log --follow -- <filename>

I have to use gitpython, so what I am doing now is:

import git 
g = git.Git('repo_dir') 
hexshas = g.log('--pretty=%H','--follow','--',filename).split('\n') 

then I build commit objects:

repo = git.Repo('repo_dir')
commits = [repo.rev_parse(c) for c in r]

Is there a way to do it in a more gitpython-ic way? I tried both commit.iter_parents() and commit.iter_items(), but they both rely on git-rev-list, so they don't have a --follow option.


回答1:


For example,

With range time:

g = git.Git("C:/path/to/your/repo") 
loginfo = g.log('--since=2013-09-01','--author=KIM BASINGER','--pretty=tformat:','--numstat')
print loginfo

Output:

3       2       path/in/your/solutions/some_file.cs

You can see the added lines, removed lines and the file with these changes.




回答2:


I'd suggest you to use PyDriller instead (it uses GitPython internally). Much easier to use:

for commit in RepositoryMining("path_to_repo", filepath="here_the_file").traverse_commits():
    # here you have the commit object
    print(commit.hash)



回答3:


pydriller is awesome! thanks for recommending.



来源:https://stackoverflow.com/questions/10073154/git-log-follow-the-gitpython-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!