Search code examples
pythongitgitpython

Number of lines added and deleted in a git repository using python


The following code prints the files which have been modified in current tree vs previous tree (if changed):

for modified in commit.diff('HEAD~1').iter_change_type('M'):                         
    print(modified.a_blob.path)      # prints all files modified

How to get number of lines added and deleted too? (Just like we do using git log --numstat).


Solution

  • You can use git directly to do this from within gitpython:

    the_git = repo.git
    log = the_git.log('--numstat')
    

    More here if you like: http://gitpython.readthedocs.io/en/stable/tutorial.html#using-git-directly