I recently made a git command using python that executes git command using subproces.Popen so I am debating whether to take advantage of gitPython module or not ? Does it make any difference if gitpython module is used ?
Using the subprocess module is easier to get working. It seems to take more time to get gitPython to work. Depending on your project, it might just be easier to use subprocess. If you're thinking of going the subprocess route, sh is also worth checking out. http://amoffat.github.io/sh/ sh allows you to call a program as if it were a function. Also, have you looked into https://github.com/FriendCode/gittle - it might be nice if you're just looking to automate Git commands.
ex:
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()
more info: Python Git Module experiences?
For me, GitPython has many advantages. Most important is in my opinion that the results of commands are Python objects, so you execute and receive an object with typed properties and methods. This gives you better results in face of the unexpected, like - there is no remote, the branch name is different than you think, etc.
repo = git.Repo('/home/user/my_project')
print(repo.branches)
And from the above follows for example that you can iterate over the results of commands like remotes, branches.
for branch in repo.branches:
print(branch)
Such things get really cumbersome really quickly with raw command-line.
And, I think that GitPython has more complete support of git features than gittle, e.g. submodules support.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With