I try to make git commits programmatically right in Python/Django. The problem I encounter is the syntax of the command. The message of a commit is a variable, that may contain several words. So, I tried to do it this way:
command('git commit -m "%s"'%msg) # command is a custom function that calls
# system Popen function
If I then make a push command to a remote repository at github, then first what I see, is that commit messages are in double quotes like "Test" and another problem is that if a message contains two or more words like "Test message", then it is not even executed. In other words, in a situation like
msg = "Test message"
command('git commit -m "%s"'%msg)
Nothing happens.
-
What are you writing a new wrapper for? There's already gitpython.simonzack– simonzack2014年11月09日 08:11:27 +00:00Commented Nov 9, 2014 at 8:11
-
First of all, I do not need a behemot, I just need several simplest commands in my app. And besides, when I tested gittle and gitpython I encountered a number of problems like getting the list of modified files etc.Jacobian– Jacobian2014年11月09日 08:13:21 +00:00Commented Nov 9, 2014 at 8:13
-
That's your issue, not gitpython's issue.simonzack– simonzack2014年11月09日 08:14:55 +00:00Commented Nov 9, 2014 at 8:14
-
For example, in gittle instead of a list of modified files I get some garbage with a command repo.modified_files (probably, I did something wrong, but still I have this issue), while in gitpython I do not even see a command to list modified files. I know something about diffs, but IMHO that looks just terrible.Jacobian– Jacobian2014年11月09日 08:19:52 +00:00Commented Nov 9, 2014 at 8:19
1 Answer 1
I used git from command line, too. This is how I do it here.
import subprocess
def git_commit(message):
return subprocess.check_output(['git', 'commit', '-m', message])
If nothing happens it might be that you need to add the changed files first.
def git_add(file_path):
return subprocess.check_output(['git', 'add', file_path])
Or you add and commit all.
def git_commit_all(message):
return subprocess.check_output(['git', 'commit', '-am', message])