Reputation: 527
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?
Upvotes: 8
Views: 10217
Reputation: 1
This solution was tested with gitPython 3.1.26
import git
foo = git.Git("~/git/GitPython")
cmd1 = "git add -A"
bar = "git commit -m 'message'"
foo.execute(f"{cmd1}; {bar}", shell=True, istream=PIPE, with_stdout=True)
As it can be seing gitPython
just not only can execute commands in string form, but also can execute more than one command in a single call.
This post about executing multiple commands gave me the idea. Popen
is the base of git.Git.execute
.
Unfortunately I searched the official gitPython
documentation and other sites and couldn't find a similar example using git.Git.execute
.
Upvotes: 0
Reputation:
In general, ~ expansion is done by your shell and is not a feature of the file system, so you shouldn't expect it to work.
os.path.expanduser can apply the expansion for you, but in general you're better off writing out the full path (since then the script works whoever runs it, providing they have access to your files).
I suspect you want:
'/Users/bacon/git/GitPython'
Upvotes: 0
Reputation: 864
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:
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
Upvotes: 20
Reputation: 13814
In the tutorial it says ...
The first step is to create a ``Repo`` object to represent your repository.
>>> from git import *
>>> repo = Repo("/Users/mtrier/Development/git-python")
I don't see your Repo.
I am looking at the file named tutorial.rst in the doc directory of GitPython.
Upvotes: 0