1

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.

4
  • What are you writing a new wrapper for? There's already gitpython. Commented 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. Commented Nov 9, 2014 at 8:13
  • That's your issue, not gitpython's issue. Commented 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. Commented Nov 9, 2014 at 8:19

1 Answer 1

4

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])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.