Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitPython and sending commands to the Git object

Tags:

git

python

GitPython is a way of interacting with git from python. I'm trying to access the basic git commands (e.g. git commit -m "message") from this module, which according to this should be accessed through the Git module. Here's what I've tried so far to get these commands working:

>>> import git
>>> foo = git.Git("~/git/GitPython")
>>> bar = "git commit -m 'message'"
>>> beef = git.Git.execute(foo,bar)

This shows up an error saying that there is no such file or directory. I've also tried the following as paths to my git directory:

~/git/GitPython/.git
/Users/bacon/git/gitclient/

The only other option is that the command is wrong, so I tried: commit -m "message" as well, and still get "no such file or directory".

What do I need to do to get these git commands working properly?

like image 258
Tom Crayford Avatar asked Jun 09 '26 22:06

Tom Crayford


1 Answers

I havn't tried it to verify yet but it seems git.Git.execute expects a list of commandline arguments (if you give it a string it'll look for an executable exactly matching the string, spaces and everything - which naturally wouldn't be found), so something like this I think would work:

import git
import os, os.path
g = git.Git(os.path.expanduser("~/git/GitPython"))
result = g.execute(["git", "commit", "-m", "'message'"])

other changes:

  • I expect using a path with ~ in it wouldn't work so I used os.path.expanduser to expand ~ to your home directory
  • using instance.method(*args) instead of Class.method(instance, *args) is generally preferred so I changed that, though it'd still work with the other way

There might be saner ways than manually running the commit command though (I just didn't notice any quickly looking through the source) so I suggest making sure there isn't a higher-level way before doing it that way

like image 96
TFKyle Avatar answered Jun 11 '26 13:06

TFKyle