I'm writing some scripts that call git commands, but having trouble when git tries to output using a pager, which blocks on user input. For example the following drops me in less
(afaik) and I have to q
to continue:
subprocess.Popen(['git', 'diff', '--stat', '--cached', 'origin/master']).wait()
In this specific case I can disable that by telling git not to use a pager:
subprocess.Popen(['git', '--no-pager', 'diff', '--stat', '--cached', 'origin/master']).wait()
Is there some general solution (Edit: i.e. that works for everything, not just git
), where I can capture the output and not block on the pager, or is there some mechanism that could be informing the sub-process (git in this case) not to use a pager automatically?
I've tried closing subprocess's stdin and experimenting with the shell=True/False
argument, but neither helped.
Update:
For example,
git diff --stat --cached origin/master
will land you in a pager if the output is longer than a page. git diff --stat --cached origin/master | cat
will not. How does git know? How can I apply this same effect using subprocess?