7

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!

Roberto
  • 1,944
  • 1
  • 30
  • 42

3 Answers3

4

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.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • Yes, actually I found I'd be better off keeping some variables on the .git/config file for these purposes, and having the server do these related tasks. It was more a problem of my idea of a workflow than git itself, it turns out. Thanks! – Roberto Jul 02 '09 at 16:56
3

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 ;)

nickel715
  • 2,505
  • 1
  • 23
  • 28
1

Git 2.40 (Q1 2023) does extend the run-hooks API to allow feeding data from the standard input when running the hook script(s).

That can facilitate reading from STDIN on a Git pre-commit Hook, something which is not natively supported, and start being implemented with Git 2.40 (but only for git hook run, not yet any all hook automatic execution).

See commit 0414b38, commit 96af564, commit 917e080, commit 5402673 (08 Feb 2023) by Emily Shaffer (nasamuffin).
See commit 5123e6e (08 Feb 2023) by Ævar Arnfjörð Bjarmason (avar).
(Merged by Junio C Hamano -- gitster -- in commit 5048df6, 22 Feb 2023)

hook: support a --to-stdin=<path> option

Signed-off-by: Emily Shaffer
Signed-off-by: Ævar Arnfjörð Bjarmason

Expose the "path_to_stdin" API added in the preceding commit in the "git hook run"(man) command.

For now we won't be using this command interface outside of the tests, but exposing this functionality makes it easier to test the hook API.
The plan is to use this to extend the "sendemail-validate" hook (thread, thread).

git hook now includes in its man page:

'git hook' run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]

git hook now includes in its man page:

--to-stdin

For "run"; Specify a file which will be streamed into the hook's stdin. The hook will receive the entire file from beginning to EOF.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250