Python Git Module experiences?
While this question was asked a while ago and I dont know the state of the libraries at that point, it is worth mentioning for searchers that GitPython does a good job of abstracting the command line tools so that you dont need to use subprocess. There are some useful built in abstractions that you can use, but for everything else you can do things like:
import git repo = git.Repo( /home/me/repodir ) print repo.git.status() # checkout and track a remote branch print repo.git.checkout( origin/somebranch, b=somebranch ) # add a file print repo.git.add( somefile ) # commit print repo.git.commit( m=my commit message ) # now we are one commit ahead print repo.git.status()
Everything else in GitPython just makes it easier to navigate. Im fairly well satisfied with this library and appreciate that it is a wrapper on the underlying git tools.
UPDATE: Ive switched to using the sh module for not just git but most commandline utilities I need in python. To replicate the above I would do this instead:
import sh git = sh.git.bake(_cwd=/home/me/repodir) print git.status() # checkout and track a remote branch print git.checkout(-b, somebranch) # add a file print git.add(somefile) # commit print git.commit(m=my commit message) # now we are one commit ahead print git.status()
I thought I would answer my own question, since Im taking a different path than suggested in the answers. Nonetheless, thanks to those who answered.
First, a brief synopsis of my experiences with GitPython, PyGit, and Dulwich:
- GitPython: After downloading, I got this imported and the appropriate object initialized. However, trying to do what was suggested in the tutorial led to errors. Lacking more documentation, I turned elsewhere.
- PyGit: This would not even import, and I could find no documentation.
- Dulwich: Seems to be the most promising (at least for what I wanted and saw). I made some progress with it, more than with GitPython, since its egg comes with Python source. However, after a while, I decided it may just be easier to try what I did.
Also, StGit looks interesting, but I would need the functionality extracted into a separate module and do not want wait for that to happen right now.
In (much) less time than I spent trying to get the three modules above working, I managed to get git commands working via the subprocess module, e.g.
def gitAdd(fileName, repoDir): cmd = [git, add, fileName] p = subprocess.Popen(cmd, cwd=repoDir) p.wait() gitAdd(exampleFile.txt, /usr/local/example_git_repo_dir)
This isnt fully incorporated into my program yet, but Im not anticipating a problem, except maybe speed (since Ill be processing hundreds or even thousands of files at times).
Maybe I just didnt have the patience to get things going with Dulwich or GitPython. That said, Im hopeful the modules will get more development and be more useful soon.