I am writing a pre-commit hook for Git that runs pyflakes and checks for tabs and trailing spaces in the modified files (code on Github). I would like to make it possible to override the hook by asking for user confirmation as follows:
answer = raw_input('Commit anyway? [N/y] ')
if answer.strip()[0].lower() == 'y':
print >> sys.stderr, 'Committing anyway.'
sys.exit(0)
else:
print >> sys.stderr, 'Commit aborted.'
sys.exit(1)
This code produces an error:
Commit anyway? [N/y] Traceback (most recent call last):
File ".git/hooks/pre-commit", line 59, in ?
main()
File ".git/hooks/pre-commit", line 20, in main
answer = raw_input('Commit anyway? [N/y] ')
EOFError: EOF when reading a line
Is it even possible to use raw_input() or a similar function in Git hooks and if yes, what am I doing wrong?
You could use:
git commitcallspython .git/hooks/pre-commit:Looking inside
/proc/21802/fd(on this linux box) shows the state of the file descriptors for the process with PID 21802 (thepre-commitprocess):Thus,
pre-commitwas spawned withsys.stdinpointing at/dev/null.sys.stdin = open('/dev/tty')redirectssys.stdinto an open filehandle from whichraw_inputcan read.