Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read from STDIN on a Git pre-commit Hook (with PHP)

Tags:

git

php

stdin

hook

I'm looking for a way to have git-commit wait for standard input. I'm coding this in PHP, as my bash skills are non-existant, so I thougth doing a regular

<?php
$input = trim(fgets(STDIN));
fscanf(STDIN, "%d\n", $line);
?>

would do the trick, and wait until I write stuff in to continue, but it just goes ahead and continues executing my PHP script anyways.

The idea behind this is that after I tag a release, git will push HEAD to the testing webserver, send a couple of tweets, and let me write in some details about the release in the CHANGELOG.

While I can achieve the writing to a file (using exec('mate -w')), I'd like it to hang on until I do a quick test on the server. This would enable me to rollback if I notice any errors (lazy, I know).

Thanks for any help!

like image 973
Roberto Avatar asked Jul 01 '09 08:07

Roberto


2 Answers

Most git hooks either have something special fed to there stdin, or have stdin detached from the terminal. They are all designed to be run non-interactively, so I don't believe that a hook is suitable for what you want to do. You can, of course, manually talk to /dev/tty but I don't think that it's a very good idea.

I also don't believe that the 'pre-commit' hook is suitable to your task, surely not every commit that you make will be a release of some sort? A 'post-receive' hook on the testing webserver machine sounds more appropriate.

like image 120
CB Bailey Avatar answered Nov 05 '22 05:11

CB Bailey


I need user input in my post-merge hook (written in PHP).

I solved it with this piece of code: trim(exec('exec < /dev/tty && read input && echo $input'))

Don't ask, it works ;)

like image 44
nickel715 Avatar answered Nov 05 '22 03:11

nickel715