Search code examples
pythongitgitpython

GitPython - cannot get index for repo?


How on earth does one get the index for a repos using GitPython ?

import git

repo = git.Repo.init('/path/to/repos/')
... add some files ...
... commit ...
index = repo.index()

Throw error: 'Repo' object has no attribute 'index'

WTH? My eyes are bloody from reading the Trier tutorial over and over getting nowhere fast. Clues anyone ?


Solution

  • There are two issues here. First, index isn't method, it's an IndexFile object. Secondly, I wonder if you're not using the current version of GitPython.

    If I start with:

    $ virtualenv stacktest
    $ ./stacktext/bin/pip install GitPython
    $ ./stackext/bin/python
    Python 2.7.5 (default, Nov 12 2013, 16:18:42) 
    [GCC 4.8.2 20131017 (Red Hat 4.8.2-1)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import git
    >>> repo = git.Repo.init('/home/username/projects/myproject')
    >>> repo.index
    <git.index.base.IndexFile object at 0x1213a48>
    >>> repo.index.entries
    {('README.md', 0): (100644, 515cbd1e78aa13ec91941eaa63ecec89d5e4b947, 0, README.md), ('setup.py', 0): (100644, 7497e295447af70a6865b7313bfb2f86ba6577d6, 0, setup.py)}
    

    Another possibility is that something in your code is masking the attribute. If you can post an actual runnable code sampe that someone else can run, that will help narrow down the problem.