Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call 'git pull' from within Python?

Using the github webhooks, I would like to be able to pull any changes to a remote development server. At the moment, when in the appropriate directory, git pull gets any changes that need to be made. However, I can't figure out how to call that function from within Python. I have tried the following:

import subprocess process = subprocess.Popen("git pull", stdout=subprocess.PIPE) output = process.communicate()[0] 

But this results in the following error

Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "/usr/lib/python2.7/subprocess.py", line 679, in __init__     errread, errwrite)   File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child     raise child_exception OSError: [Errno 2] No such file or directory 

Is there a way that I can call this bash command from within Python?

like image 236
djq Avatar asked Mar 09 '13 20:03

djq


People also ask

How do I git pull from a branch?

In case you are using the Tower Git client, pulling from a remote is very easy: simply drag the remote branch and drop it onto your current HEAD in the sidebar - or click the "Pull" button in the toolbar.


1 Answers

Have you considered using GitPython? It's designed to handle all this nonsense for you.

import git   g = git.cmd.Git(git_dir) g.pull() 

https://github.com/gitpython-developers/GitPython

like image 71
jleahy Avatar answered Nov 05 '22 03:11

jleahy