Git script is running differently when I run it through subprocess.check_output?

Question:

When I run this Bash script

#!/bin/bash

cd /srv/http
unset GIT_DIR

git commit -am "design changes"
git push -u origin master
git checkout dev
git merge master
git commit -am "design changes"
git checkout master

through the command line, I get this output

[master 914422f] design changes
 1 file changed, 17 deletions(-)
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 334 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To https://[email protected]/blah/blah.git
   562207f..914422f  master -> master
Branch master set up to track remote branch master from origin.
Switched to branch 'dev'
Updating 562207f..914422f
Fast-forward
 save-design | 17 -----------------
 1 file changed, 17 deletions(-)
On branch dev
nothing to commit, working directory clean
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

but when I run it through Python using

subprocess.check_output('./save-design', shell=True)

I get this output

M   save-design
Already up-to-date.
M   save-design
Your branch is up-to-date with 'origin/master'.

Why does this happen?

Asked By: user2058002

||

Answers:

I still don’t know why it’s happening, but I fixed it by running it by sshing to localhost:

subprocess.check_output('ssh localhost "/srv/http/save-design"', shell=True)
Answered By: user2058002

could it be that you aren’t setting the working directory for subprocess.check_output and so git is picking up whatever random working directory gets set?

subprocess.check_output('./save-design', cwd='/my/working/dir/', shell=True)
Answered By: Eric Zinda
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.