[ACCEPTED]-GitPython and sending commands to the Git object-git

Accepted answer
Score: 19

I havn't tried it to verify yet but it seems 11 git.Git.execute expects a list of commandline 10 arguments (if you give it a string it'll 9 look for an executable exactly matching 8 the string, spaces and everything - which 7 naturally wouldn't be found), so something 6 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 5 might be saner ways than manually running 4 the commit command though (I just didn't 3 notice any quickly looking through the source) so 2 I suggest making sure there isn't a higher-level 1 way before doing it that way

Score: 0

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 2 Repo.

I am looking at the file named tutorial.rst 1 in the doc directory of GitPython.

Score: 0

In general, ~ expansion is done by your 7 shell and is not a feature of the file system, so 6 you shouldn't expect it to work.

os.path.expanduser 5 can apply the expansion for you, but in 4 general you're better off writing out the 3 full path (since then the script works whoever 2 runs it, providing they have access to your 1 files).

I suspect you want:

Score: 0

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 7 be seing gitPython just not only can execute commands 6 in string form, but also can execute more 5 than one command in a single call.

This post about executing multiple commands gave 4 me the idea. Popen is the base of git.Git.execute.

Unfortunately 3 I searched the official gitPython documentation and 2 other sites and couldn't find a similar 1 example using git.Git.execute.

More Related questions