hirakiucの日記

いろいろ

git pre-pushで ユーザの入力を受け付ける方法

https://raw.github.com/git/git/master/Documentation/RelNotes/1.8.2.txt

git 1.8.2からpre-push hookが追加されたので、早速使ってみた。

このとき、ユーザからの入力を受け付けるところで少し工夫が必要だったのでメモ。

ユーザからの入力が拾えない?

ユーザに yes/no ? と確認してから動作するようなスクリプトを書いたのだけど、単純に $stdin.gets とすると入力待ちしてくれない様子。

また、pre-pushは $stdinからはpushされる対象の情報が渡ってくるとのこと。

# Information about the commits which are being pushed is supplied as lines to
# the standard input in the form:
#
#   <local ref> <local sha1> <remote ref> <remote sha1>
#
# This sample shows how to prevent push of commits where the log message starts
# with "WIP" (work in progress).
解決方法

https://gist.github.com/ebihara/5948590#file-pre-push-py-L10

こちらの10行目が解決方法になってた。

まずスクリプトの先頭で $stdinから拾えるだけ入力を拾ってきておく。

その後は、$stdinを open("/dev/tty") で置き換えて利用。

これで解決できた。