Search code examples
pythongitpython

Git Python seems not work


I'm begin in python and trying to use GitPython and I desperately tries to make working this module.

I've seen on many website that documentation is poor and the example I follow doesn't seems to work.

I've try this on Windows (2012/ Python 3.5):

# -*-coding:Latin-1 -*

from git import *

path = ('C:\\Users\\me\\Documents\\Repos\\integration')
repo = Repo(path)
assert repo.bare == False

repo.commits()

os.system("pause")

And this on Linux (Debian/Python 2.7) :

from git import Repo

repo = Repo('/home/git/repos/target_repos')
assert repo.bare == False

repo.commits ()

But anyway, I've no result... And finish with this error :

Traceback (most recent call last):
  File "gitrepo.py", line 6, in <module>
    repo.commits ()
AttributeError: 'Repo' object has no attribute 'commits'

in the two case.

My question is the following :

  1. Is there a way to make this module work ? All the link I found are old...
  2. If yes, please help me or giving me an example.
  3. If not, is there another module ? I've trying to install Dulwich but no succes (on Windows only)
  4. I've seen there is a way by using fab ? Is it possible to manipulate git with that ?

The goal is to manage git with python in the future, as well as other things for integration.

Thank you for your answer.


Solution

  • The module works fine, you're not using it properly. If you want to access the commits in a git repository, use Repo.iter_commits(). The method commits() you're trying to use doesn't exist in GitPython.Repo. You can check the official module documentation here for all supported operations and attributes.

    See below for a working example

    from git import Repo, Commit
    
    path = "test_repository"
    
    repo = Repo(path)
    
    for commit in repo.iter_commits():
        print "Author: ", commit.author
        print "Summary: ", commit.summary
    

    This shows the author and summary for every commit in the repository. Check the Commit object's documentation for operations and accessing additional information they carry (e.g. the commit hash, date, etc.)