Skip to contentSkip to conversation

hooks: allow input from stdin

PATCH Superseded Orgad Shaneh via GitGitGadget 5 participants 22 messages Nov 17, 2020 – Dec 9, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hooks: allow input from stdin

From: Orgad Shaneh <[email protected]>

Let hooks receive user input if applicable.

Closing stdin originates in f5bbc3225 (Port git commit to C, 2007). Looks like the original shell implementation did have stdin open. Not clear why the author chose to close it on the C port (maybe copy&paste).

The only hook that passes internal information to the hook via stdin is pre-push, which has its own logic.

Some references of users requesting this feature. Some of them use acrobatics to gain access to stdin:
[1] https://stackoverflow.com/q/1067874/764870
[2] https://stackoverflow.com/q/47477766/764870
[3] https://stackoverflow.com/q/3417896/764870
[4] https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/3165
[5] https://github.com/typicode/husky/issues/442

---
hooks: allow input from stdin

Let hooks receive user input if applicable.

Closing stdin originates in f5bbc3225 (Port git commit to C, 2007). Looks like the original shell implementation did have stdin open. Not clear why the author chose to close it on the C port (maybe copy&paste).

The only hook that passes internal information to the hook via stdin is pre-push, which has its own logic.

Some references of users requesting this feature. Some of them use acrobatics to gain access to stdin: [1]
https://stackoverflow.com/q/1067874/764870[2]
https://stackoverflow.com/q/47477766/764870[3]
https://stackoverflow.com/q/3417896/764870[4]
https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/3165[5]
https://github.com/typicode/husky/issues/442

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-790%2Forgads%2Fhooks-stdin-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-790/orgads/hooks-stdin-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/790

 run-command.c | 1 -
 1 file changed, 1 deletion(-)
run-command.c +0 -1
index 2ee59acdc8..a17b613216 100644
@@ -1356,7 +1356,6 @@ int run_hook_ve(const char *const *env, const char *name, va_list args)
 	while ((p = va_arg(args, const char *)))
 		strvec_push(&hook.args, p);
 	hook.env = env;
-	hook.no_stdin = 1;
 	hook.stdout_to_stderr = 1;
 	hook.trace2_hook_name = name;
 

base-commit: e31aba42fb12bdeb0f850829e008e1e3f43af500

Signature · 1 line
gitgitgadget
#1 lore raw
Quoted 8 lines · Orgad Shaneh via GitGitGadget

From: Orgad Shaneh <[email protected]>

Let hooks receive user input if applicable.

Closing stdin originates in f5bbc3225 (Port git commit to C, 2007). Looks like the original shell implementation did have stdin open. Not clear why the author chose to close it on the C port (maybe copy&paste).

For "git commit" leaving the standard input open and let hook interact with the end-user sitting at the terminal might be OK, but doing this for any and all hooks probably breaks callers of hooks that deal with transport protocols where their standard input is *not* supposed to be molested (think: the main Git process is about to read a packstream over the network and spawns a hook using the standard run_hook*() interface---if the hook reads standard input, the main process would lose the initial part of the pack stream).

Quoted 2 lines

The only hook that passes internal information to the hook via stdin is pre-push, which has its own logic.

Hmph, doesn't "pre-receive" hook gets information from its standard input? In any case, it is natural that such hooks can and have to be able to read from their standard input---they are spawned with their input connected to Git process that feeds them input that are necessary for them to make decision, so comparing them with random other hooks that do not use information from Git to do their thing is comparing apples and oranges.

So I think the patch we see as-is is probably not a good idea, primarily because it lets run_hook_ve() to pretend that it still is a generic interface to run any hooks by sitting in run_command.c, but now its behaviour has been tweaked to fit only needs by "git commit" and the like. You probably need to add a new "options" parameter to run_hook_ve() that allows the callers to say "this hook is allowed to read from the standard input" etc., if we want to keep it in run_command.c and let it pretend to be generally useful interface.

Another possibility is to remove run_hook_ve(), and open code its body in commit.c::run_commit_hook(), but that is less than ideal.

Quoted 7 lines

Some references of users requesting this feature. Some of them use acrobatics to gain access to stdin:
[1] https://stackoverflow.com/q/1067874/764870
[2] https://stackoverflow.com/q/47477766/764870
[3] https://stackoverflow.com/q/3417896/764870
[4] https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/3165
[5] https://github.com/typicode/husky/issues/442

Instead of wasting 7 lines here, it would be a much better approach to just add a test or two that demonstrate sample usages and that protect this new feature from accidentally getting broken at the same time.

Quoted 33 lines

The only hook that passes internal information to the hook via stdin is pre-push, which has its own logic.

Some references of users requesting this feature. Some of them use acrobatics to gain access to stdin: [1]
https://stackoverflow.com/q/1067874/764870[2]
https://stackoverflow.com/q/47477766/764870[3]
https://stackoverflow.com/q/3417896/764870[4]
https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/3165[5]
https://github.com/typicode/husky/issues/442

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-790%2Forgads%2Fhooks-stdin-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-790/orgads/hooks-stdin-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/790

 run-command.c | 1 -
 1 file changed, 1 deletion(-)
run-command.c +0 -1
index 2ee59acdc8..a17b613216 100644
@@ -1356,7 +1356,6 @@ int run_hook_ve(const char *const *env, const char *name, va_list args)
 	while ((p = va_arg(args, const char *)))
 		strvec_push(&hook.args, p);
 	hook.env = env;
-	hook.no_stdin = 1;
 	hook.stdout_to_stderr = 1;
 	hook.trace2_hook_name = name;
 

base-commit: e31aba42fb12bdeb0f850829e008e1e3f43af500

#2 lore raw

PATCH v2 1/1 hooks: allow input from stdin

In reply to Orgad Shaneh via GitGitGadget

From: Orgad Shaneh <[email protected]>

Let hooks receive user input if applicable.

Closing stdin originates in f5bbc3225 (Port git commit to C, 2007). Looks like the original shell implementation did have stdin open. Not clear why the author chose to close it on the C port (maybe copy&paste).

Allow stdin only for commit-related hooks. Some of the other hooks pass their own input to the hook, so don't change them.

---
hooks: allow input from stdin

Let hooks receive user input if applicable.

Closing stdin originates in f5bbc3225 (Port git commit to C, 2007). Looks like the original shell implementation did have stdin open. Not clear why the author chose to close it on the C port (maybe copy&paste).

The only hook that passes internal information to the hook via stdin is pre-push, which has its own logic.

Some references of users requesting this feature. Some of them use acrobatics to gain access to stdin: [1]
https://stackoverflow.com/q/1067874/764870[2]
https://stackoverflow.com/q/47477766/764870[3]
https://stackoverflow.com/q/3417896/764870[4]
https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/3165[5]
https://github.com/typicode/husky/issues/442

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-790%2Forgads%2Fhooks-stdin-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-790/orgads/hooks-stdin-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/790

Range-diff vs v1:

 1:  4aa6b4c507 < -:  ---------- hooks: allow input from stdin
 -:  ---------- > 1:  6a9bcf9d8b hooks: allow input from stdin
 builtin/am.c                                  |  6 +--
 builtin/checkout.c                            |  2 +-
 builtin/clone.c                               |  2 +-
 builtin/gc.c                                  |  2 +-
 builtin/merge.c                               |  2 +-
 builtin/rebase.c                              |  2 +-
 builtin/receive-pack.c                        |  2 +-
 commit.c                                      |  2 +-
 read-cache.c                                  |  2 +-
 reset.c                                       |  2 +-
 run-command.c                                 |  9 +++--
 run-command.h                                 | 15 +++++---
 ...3-pre-commit-and-pre-merge-commit-hooks.sh | 37 ++++++++++++++++++-
 t/t7504-commit-msg-hook.sh                    | 15 ++++++++
 t/t7505-prepare-commit-msg-hook.sh            | 14 +++++++
 15 files changed, 92 insertions(+), 22 deletions(-)
builtin/am.c +3 -3
index f22c73a05b..1946569d5b 100644
@@ -428,7 +428,7 @@ static int run_applypatch_msg_hook(struct am_state *state)
 	int ret;
 
 	assert(state->msg);
-	ret = run_hook_le(NULL, "applypatch-msg", am_path(state, "final-commit"), NULL);
+	ret = run_hook_le(NULL, 0, "applypatch-msg", am_path(state, "final-commit"), NULL);
 
 	if (!ret) {
 		FREE_AND_NULL(state->msg);
@@ -1559,7 +1559,7 @@ static void do_commit(const struct am_state *state)
 	const char *reflog_msg, *author, *committer = NULL;
 	struct strbuf sb = STRBUF_INIT;
 
-	if (run_hook_le(NULL, "pre-applypatch", NULL))
+	if (run_hook_le(NULL, 0, "pre-applypatch", NULL))
 		exit(1);
 
 	if (write_cache_as_tree(&tree, 0, NULL))
@@ -1611,7 +1611,7 @@ static void do_commit(const struct am_state *state)
 		fclose(fp);
 	}
 
-	run_hook_le(NULL, "post-applypatch", NULL);
+	run_hook_le(NULL, 0, "post-applypatch", NULL);
 
 	strbuf_release(&sb);
 }
builtin/checkout.c +1 -1
index 9b82119129..293e8ebd76 100644
@@ -104,7 +104,7 @@ struct branch_info {
 static int post_checkout_hook(struct commit *old_commit, struct commit *new_commit,
 			      int changed)
 {
-	return run_hook_le(NULL, "post-checkout",
+	return run_hook_le(NULL, 0, "post-checkout",
 			   oid_to_hex(old_commit ? &old_commit->object.oid : &null_oid),
 			   oid_to_hex(new_commit ? &new_commit->object.oid : &null_oid),
 			   changed ? "1" : "0", NULL);
builtin/clone.c +1 -1
index a0841923cf..6cfd2f23be 100644
@@ -816,7 +816,7 @@ static int checkout(int submodule_progress)
 	if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
 		die(_("unable to write new index file"));
 
-	err |= run_hook_le(NULL, "post-checkout", oid_to_hex(&null_oid),
+	err |= run_hook_le(NULL, 0, "post-checkout", oid_to_hex(&null_oid),
 			   oid_to_hex(&oid), "1", NULL);
 
 	if (!err && (option_recurse_submodules.nr > 0)) {
builtin/gc.c +1 -1
index 5cd2a43f9f..c790a362df 100644
@@ -385,7 +385,7 @@ static int need_to_gc(void)
 	else
 		return 0;
 
-	if (run_hook_le(NULL, "pre-auto-gc", NULL))
+	if (run_hook_le(NULL, 0, "pre-auto-gc", NULL))
 		return 0;
 	return 1;
 }
builtin/merge.c +1 -1
index 4c133402a6..1f1d234879 100644
@@ -474,7 +474,7 @@ static void finish(struct commit *head_commit,
 	}
 
 	/* Run a post-merge hook */
-	run_hook_le(NULL, "post-merge", squash ? "1" : "0", NULL);
+	run_hook_le(NULL, 0, "post-merge", squash ? "1" : "0", NULL);
 
 	apply_autostash(git_path_merge_autostash(the_repository));
 	strbuf_release(&reflog_message);
builtin/rebase.c +1 -1
index 7b65525301..c17f7a628b 100644
@@ -2014,7 +2014,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
 
 	/* If a hook exists, give it a chance to interrupt*/
 	if (!ok_to_skip_pre_rebase &&
-	    run_hook_le(NULL, "pre-rebase", options.upstream_arg,
+	    run_hook_le(NULL, 0, "pre-rebase", options.upstream_arg,
 			argc ? argv[0] : NULL, NULL))
 		die(_("The pre-rebase hook refused to rebase."));
 
builtin/receive-pack.c +1 -1
index bb9909c52e..c1398af755 100644
@@ -1384,7 +1384,7 @@ static const char *push_to_checkout(unsigned char *hash,
 				    const char *work_tree)
 {
 	strvec_pushf(env, "GIT_WORK_TREE=%s", absolute_path(work_tree));
-	if (run_hook_le(env->v, push_to_checkout_hook,
+	if (run_hook_le(env->v, 0, push_to_checkout_hook,
 			hash_to_hex(hash), NULL))
 		return "push-to-checkout hook declined";
 	else
commit.c +1 -1
index fe1fa3dc41..775019ec9d 100644
@@ -1646,7 +1646,7 @@ int run_commit_hook(int editor_is_used, const char *index_file,
 		strvec_push(&hook_env, "GIT_EDITOR=:");
 
 	va_start(args, name);
-	ret = run_hook_ve(hook_env.v, name, args);
+	ret = run_hook_ve(hook_env.v, RUN_HOOK_ALLOW_STDIN, name, args);
 	va_end(args);
 	strvec_clear(&hook_env);
 
read-cache.c +1 -1
index ecf6f68994..a83beac63e 100644
@@ -3070,7 +3070,7 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l
 	else
 		ret = close_lock_file_gently(lock);
 
-	run_hook_le(NULL, "post-index-change",
+	run_hook_le(NULL, 0, "post-index-change",
 			istate->updated_workdir ? "1" : "0",
 			istate->updated_skipworktree ? "1" : "0", NULL);
 	istate->updated_workdir = 0;
reset.c +1 -1
index 2f4fbd07c5..33687b0b5b 100644
@@ -127,7 +127,7 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action,
 					    reflog_head);
 	}
 	if (run_hook)
-		run_hook_le(NULL, "post-checkout",
+		run_hook_le(NULL, 0, "post-checkout",
 			    oid_to_hex(orig ? orig : &null_oid),
 			    oid_to_hex(oid), "1", NULL);
 
run-command.c +5 -4
index 2ee59acdc8..21b1f0a5e9 100644
@@ -1343,7 +1343,7 @@ const char *find_hook(const char *name)
 	return path.buf;
 }
 
-int run_hook_ve(const char *const *env, const char *name, va_list args)
+int run_hook_ve(const char *const *env, int opt, const char *name, va_list args)
 {
 	struct child_process hook = CHILD_PROCESS_INIT;
 	const char *p;
@@ -1356,20 +1356,21 @@ int run_hook_ve(const char *const *env, const char *name, va_list args)
 	while ((p = va_arg(args, const char *)))
 		strvec_push(&hook.args, p);
 	hook.env = env;
-	hook.no_stdin = 1;
+	if (!(opt & RUN_HOOK_ALLOW_STDIN))
+		hook.no_stdin = 1;
 	hook.stdout_to_stderr = 1;
 	hook.trace2_hook_name = name;
 
 	return run_command(&hook);
 }
 
-int run_hook_le(const char *const *env, const char *name, ...)
+int run_hook_le(const char *const *env, int opt, const char *name, ...)
 {
 	va_list args;
 	int ret;
 
 	va_start(args, name);
-	ret = run_hook_ve(env, name, args);
+	ret = run_hook_ve(env, opt, name, args);
 	va_end(args);
 
 	return ret;
run-command.h +10 -5
index 6472b38bde..e6a850c6fe 100644
@@ -201,11 +201,16 @@ int run_command(struct child_process *);
  */
 const char *find_hook(const char *name);
 
+#define RUN_HOOK_ALLOW_STDIN 1
+
 /**
  * Run a hook.
- * The first argument is a pathname to an index file, or NULL
- * if the hook uses the default index file or no index is needed.
- * The second argument is the name of the hook.
+ * The first argument is an array of environment variables, or NULL
+ * if the hook uses the default environment and doesn't require
+ * additional variables.
+ * The second argument is zero or RUN_HOOK_ALLOW_STDIN, which enables
+ * stdin for the child process (the default is no_stdin).
+ * The third argument is the name of the hook.
  * The further arguments correspond to the hook arguments.
  * The last argument has to be NULL to terminate the arguments list.
  * If the hook does not exist or is not executable, the return
@@ -215,8 +220,8 @@ const char *find_hook(const char *name);
  * On execution, .stdout_to_stderr and .no_stdin will be set.
  */
 LAST_ARG_MUST_BE_NULL
-int run_hook_le(const char *const *env, const char *name, ...);
-int run_hook_ve(const char *const *env, const char *name, va_list args);
+int run_hook_le(const char *const *env, int opt, const char *name, ...);
+int run_hook_ve(const char *const *env, int opt, const char *name, va_list args);
 
 /*
  * Trigger an auto-gc
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +36 -1
index b3485450a2..e915ffe546 100755
@@ -7,6 +7,7 @@ test_description='pre-commit and pre-merge-commit hooks'
 HOOKDIR="$(git rev-parse --git-dir)/hooks"
 PRECOMMIT="$HOOKDIR/pre-commit"
 PREMERGE="$HOOKDIR/pre-merge-commit"
+POSTCOMMIT="$HOOKDIR/post-commit"
 
 # Prepare sample scripts that write their $0 to actual_hooks
 test_expect_success 'sample script setup' '
@@ -28,11 +29,15 @@ test_expect_success 'sample script setup' '
 	echo $0 >>actual_hooks
 	test $GIT_PREFIX = "success/"
 	EOF
-	write_script "$HOOKDIR/check-author.sample" <<-\EOF
+	write_script "$HOOKDIR/check-author.sample" <<-\EOF &&
 	echo $0 >>actual_hooks
 	test "$GIT_AUTHOR_NAME" = "New Author" &&
 	test "$GIT_AUTHOR_EMAIL" = "[email protected]"
 	EOF
+	write_script "$HOOKDIR/user-input.sample" <<-\EOF
+	! read -r line || echo "$line" > hook_input
+	exit 0
+	EOF
 '
 
 test_expect_success 'root commit' '
@@ -278,4 +283,34 @@ test_expect_success 'check the author in hook' '
 	test_cmp expected_hooks actual_hooks
 '
 
+test_expect_success 'with user input' '
+	test_when_finished "rm -f \"$PRECOMMIT\" user_input hook_input" &&
+	cp "$HOOKDIR/user-input.sample" "$PRECOMMIT" &&
+	echo "user input" > user_input &&
+	echo "more" >>file &&
+	git add file &&
+	git commit -m "more" < user_input &&
+	test_cmp user_input hook_input
+'
+
+test_expect_success 'post-commit with user input' '
+	test_when_finished "rm -f \"$POSTCOMMIT\" user_input hook_input" &&
+	cp "$HOOKDIR/user-input.sample" "$POSTCOMMIT" &&
+	echo "user input" > user_input &&
+	echo "more" >>file &&
+	git add file &&
+	git commit -m "more" < user_input &&
+	test_cmp user_input hook_input
+'
+
+test_expect_success 'with user input (merge)' '
+	test_when_finished "rm -f \"$PREMERGE\" user_input hook_input" &&
+	cp "$HOOKDIR/user-input.sample" "$PREMERGE" &&
+	echo "user input" > user_input &&
+	git checkout side &&
+	git merge -m "merge master" master < user_input &&
+	git checkout master &&
+	test_cmp user_input hook_input
+'
+
 test_done
t/t7504-commit-msg-hook.sh +15 -0
index 31b9c6a2c1..ad467aad86 100755
@@ -294,5 +294,20 @@ test_expect_success 'hook is called for reword during `rebase -i`' '
 
 '
 
+# now a hook that accepts input and writes it as the commit message
+cat > "$HOOK" <<'EOF'
+#!/bin/sh
+! read -r line || echo "$line" > "$1"
+EOF
+chmod +x "$HOOK"
+
+test_expect_success 'hook with user input' '
+
+	echo "additional" >> file &&
+	git add file &&
+	echo "user input" | git commit -m "additional" &&
+	commit_msg_is "user input"
+
+'
 
 test_done
t/t7505-prepare-commit-msg-hook.sh +14 -0
index 94f85cdf83..16a161f129 100755
@@ -91,6 +91,11 @@ else
 fi
 test "$GIT_EDITOR" = : && source="$source (no editor)"
 
+if read -r line
+then
+	source="$source $line"
+fi
+
 if test $rebasing = 1
 then
 	echo "$source $(get_last_cmd)" >"$1"
@@ -113,6 +118,15 @@ test_expect_success 'with hook (-m)' '
 
 '
 
+test_expect_success 'with hook (-m and input)' '
+
+	echo "more" >> file &&
+	git add file &&
+	echo "user input" | git commit -m "more" &&
+	test "$(git log -1 --pretty=format:%s)" = "message (no editor) user input"
+
+'
+
 test_expect_success 'with hook (-m editor)' '
 
 	echo "more" >> file &&

base-commit: e31aba42fb12bdeb0f850829e008e1e3f43af500

Signature · 1 line
gitgitgadget
#3 lore raw

PATCH v3 1/1 hooks: allow input from stdin for commit-related hooks

From: Orgad Shaneh <[email protected]>

Let hooks receive user input if applicable.

Closing stdin originates in f5bbc3225 (Port git commit to C, 2007). Looks like the original shell implementation did have stdin open. Not clear why the author chose to close it on the C port (maybe copy&paste).

Allow stdin only for commit-related hooks. Some of the other hooks pass their own input to the hook, so don't change them.

---
hooks: allow input from stdin for commit-related hooks

Let hooks receive user input if applicable.

Closing stdin originates in f5bbc3225 (Port git commit to C, 2007). Looks like the original shell implementation did have stdin open. Not clear why the author chose to close it on the C port (maybe copy&paste).

The only hook that passes internal information to the hook via stdin is pre-push, which has its own logic.

Some references of users requesting this feature. Some of them use acrobatics to gain access to stdin: [1]
https://stackoverflow.com/q/1067874/764870[2]
https://stackoverflow.com/q/47477766/764870[3]
https://stackoverflow.com/q/3417896/764870[4]
https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/3165[5]
https://github.com/typicode/husky/issues/442

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-790%2Forgads%2Fhooks-stdin-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-790/orgads/hooks-stdin-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/790

Range-diff vs v2:

1: 6a9bcf9d8b ! 1: 2f7a45c828 hooks: allow input from stdin
@@ Metadata
Author: Orgad Shaneh <[email protected]>

## Commit message ##
- hooks: allow input from stdin
+ hooks: allow input from stdin for commit-related hooks

Let hooks receive user input if applicable.

 builtin/am.c                                  |  6 +--
 builtin/checkout.c                            |  2 +-
 builtin/clone.c                               |  2 +-
 builtin/gc.c                                  |  2 +-
 builtin/merge.c                               |  2 +-
 builtin/rebase.c                              |  2 +-
 builtin/receive-pack.c                        |  2 +-
 commit.c                                      |  2 +-
 read-cache.c                                  |  2 +-
 reset.c                                       |  2 +-
 run-command.c                                 |  9 +++--
 run-command.h                                 | 15 +++++---
 ...3-pre-commit-and-pre-merge-commit-hooks.sh | 37 ++++++++++++++++++-
 t/t7504-commit-msg-hook.sh                    | 15 ++++++++
 t/t7505-prepare-commit-msg-hook.sh            | 14 +++++++
 15 files changed, 92 insertions(+), 22 deletions(-)
builtin/am.c +3 -3
index f22c73a05b..1946569d5b 100644
@@ -428,7 +428,7 @@ static int run_applypatch_msg_hook(struct am_state *state)
 	int ret;
 
 	assert(state->msg);
-	ret = run_hook_le(NULL, "applypatch-msg", am_path(state, "final-commit"), NULL);
+	ret = run_hook_le(NULL, 0, "applypatch-msg", am_path(state, "final-commit"), NULL);
 
 	if (!ret) {
 		FREE_AND_NULL(state->msg);
@@ -1559,7 +1559,7 @@ static void do_commit(const struct am_state *state)
 	const char *reflog_msg, *author, *committer = NULL;
 	struct strbuf sb = STRBUF_INIT;
 
-	if (run_hook_le(NULL, "pre-applypatch", NULL))
+	if (run_hook_le(NULL, 0, "pre-applypatch", NULL))
 		exit(1);
 
 	if (write_cache_as_tree(&tree, 0, NULL))
@@ -1611,7 +1611,7 @@ static void do_commit(const struct am_state *state)
 		fclose(fp);
 	}
 
-	run_hook_le(NULL, "post-applypatch", NULL);
+	run_hook_le(NULL, 0, "post-applypatch", NULL);
 
 	strbuf_release(&sb);
 }
builtin/checkout.c +1 -1
index 9b82119129..293e8ebd76 100644
@@ -104,7 +104,7 @@ struct branch_info {
 static int post_checkout_hook(struct commit *old_commit, struct commit *new_commit,
 			      int changed)
 {
-	return run_hook_le(NULL, "post-checkout",
+	return run_hook_le(NULL, 0, "post-checkout",
 			   oid_to_hex(old_commit ? &old_commit->object.oid : &null_oid),
 			   oid_to_hex(new_commit ? &new_commit->object.oid : &null_oid),
 			   changed ? "1" : "0", NULL);
builtin/clone.c +1 -1
index a0841923cf..6cfd2f23be 100644
@@ -816,7 +816,7 @@ static int checkout(int submodule_progress)
 	if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
 		die(_("unable to write new index file"));
 
-	err |= run_hook_le(NULL, "post-checkout", oid_to_hex(&null_oid),
+	err |= run_hook_le(NULL, 0, "post-checkout", oid_to_hex(&null_oid),
 			   oid_to_hex(&oid), "1", NULL);
 
 	if (!err && (option_recurse_submodules.nr > 0)) {
builtin/gc.c +1 -1
index 5cd2a43f9f..c790a362df 100644
@@ -385,7 +385,7 @@ static int need_to_gc(void)
 	else
 		return 0;
 
-	if (run_hook_le(NULL, "pre-auto-gc", NULL))
+	if (run_hook_le(NULL, 0, "pre-auto-gc", NULL))
 		return 0;
 	return 1;
 }
builtin/merge.c +1 -1
index 4c133402a6..1f1d234879 100644
@@ -474,7 +474,7 @@ static void finish(struct commit *head_commit,
 	}
 
 	/* Run a post-merge hook */
-	run_hook_le(NULL, "post-merge", squash ? "1" : "0", NULL);
+	run_hook_le(NULL, 0, "post-merge", squash ? "1" : "0", NULL);
 
 	apply_autostash(git_path_merge_autostash(the_repository));
 	strbuf_release(&reflog_message);
builtin/rebase.c +1 -1
index 7b65525301..c17f7a628b 100644
@@ -2014,7 +2014,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
 
 	/* If a hook exists, give it a chance to interrupt*/
 	if (!ok_to_skip_pre_rebase &&
-	    run_hook_le(NULL, "pre-rebase", options.upstream_arg,
+	    run_hook_le(NULL, 0, "pre-rebase", options.upstream_arg,
 			argc ? argv[0] : NULL, NULL))
 		die(_("The pre-rebase hook refused to rebase."));
 
builtin/receive-pack.c +1 -1
index bb9909c52e..c1398af755 100644
@@ -1384,7 +1384,7 @@ static const char *push_to_checkout(unsigned char *hash,
 				    const char *work_tree)
 {
 	strvec_pushf(env, "GIT_WORK_TREE=%s", absolute_path(work_tree));
-	if (run_hook_le(env->v, push_to_checkout_hook,
+	if (run_hook_le(env->v, 0, push_to_checkout_hook,
 			hash_to_hex(hash), NULL))
 		return "push-to-checkout hook declined";
 	else
commit.c +1 -1
index fe1fa3dc41..775019ec9d 100644
@@ -1646,7 +1646,7 @@ int run_commit_hook(int editor_is_used, const char *index_file,
 		strvec_push(&hook_env, "GIT_EDITOR=:");
 
 	va_start(args, name);
-	ret = run_hook_ve(hook_env.v, name, args);
+	ret = run_hook_ve(hook_env.v, RUN_HOOK_ALLOW_STDIN, name, args);
 	va_end(args);
 	strvec_clear(&hook_env);
 
read-cache.c +1 -1
index ecf6f68994..a83beac63e 100644
@@ -3070,7 +3070,7 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l
 	else
 		ret = close_lock_file_gently(lock);
 
-	run_hook_le(NULL, "post-index-change",
+	run_hook_le(NULL, 0, "post-index-change",
 			istate->updated_workdir ? "1" : "0",
 			istate->updated_skipworktree ? "1" : "0", NULL);
 	istate->updated_workdir = 0;
reset.c +1 -1
index 2f4fbd07c5..33687b0b5b 100644
@@ -127,7 +127,7 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action,
 					    reflog_head);
 	}
 	if (run_hook)
-		run_hook_le(NULL, "post-checkout",
+		run_hook_le(NULL, 0, "post-checkout",
 			    oid_to_hex(orig ? orig : &null_oid),
 			    oid_to_hex(oid), "1", NULL);
 
run-command.c +5 -4
index 2ee59acdc8..21b1f0a5e9 100644
@@ -1343,7 +1343,7 @@ const char *find_hook(const char *name)
 	return path.buf;
 }
 
-int run_hook_ve(const char *const *env, const char *name, va_list args)
+int run_hook_ve(const char *const *env, int opt, const char *name, va_list args)
 {
 	struct child_process hook = CHILD_PROCESS_INIT;
 	const char *p;
@@ -1356,20 +1356,21 @@ int run_hook_ve(const char *const *env, const char *name, va_list args)
 	while ((p = va_arg(args, const char *)))
 		strvec_push(&hook.args, p);
 	hook.env = env;
-	hook.no_stdin = 1;
+	if (!(opt & RUN_HOOK_ALLOW_STDIN))
+		hook.no_stdin = 1;
 	hook.stdout_to_stderr = 1;
 	hook.trace2_hook_name = name;
 
 	return run_command(&hook);
 }
 
-int run_hook_le(const char *const *env, const char *name, ...)
+int run_hook_le(const char *const *env, int opt, const char *name, ...)
 {
 	va_list args;
 	int ret;
 
 	va_start(args, name);
-	ret = run_hook_ve(env, name, args);
+	ret = run_hook_ve(env, opt, name, args);
 	va_end(args);
 
 	return ret;
run-command.h +10 -5
index 6472b38bde..e6a850c6fe 100644
@@ -201,11 +201,16 @@ int run_command(struct child_process *);
  */
 const char *find_hook(const char *name);
 
+#define RUN_HOOK_ALLOW_STDIN 1
+
 /**
  * Run a hook.
- * The first argument is a pathname to an index file, or NULL
- * if the hook uses the default index file or no index is needed.
- * The second argument is the name of the hook.
+ * The first argument is an array of environment variables, or NULL
+ * if the hook uses the default environment and doesn't require
+ * additional variables.
+ * The second argument is zero or RUN_HOOK_ALLOW_STDIN, which enables
+ * stdin for the child process (the default is no_stdin).
+ * The third argument is the name of the hook.
  * The further arguments correspond to the hook arguments.
  * The last argument has to be NULL to terminate the arguments list.
  * If the hook does not exist or is not executable, the return
@@ -215,8 +220,8 @@ const char *find_hook(const char *name);
  * On execution, .stdout_to_stderr and .no_stdin will be set.
  */
 LAST_ARG_MUST_BE_NULL
-int run_hook_le(const char *const *env, const char *name, ...);
-int run_hook_ve(const char *const *env, const char *name, va_list args);
+int run_hook_le(const char *const *env, int opt, const char *name, ...);
+int run_hook_ve(const char *const *env, int opt, const char *name, va_list args);
 
 /*
  * Trigger an auto-gc
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +36 -1
index b3485450a2..e915ffe546 100755
@@ -7,6 +7,7 @@ test_description='pre-commit and pre-merge-commit hooks'
 HOOKDIR="$(git rev-parse --git-dir)/hooks"
 PRECOMMIT="$HOOKDIR/pre-commit"
 PREMERGE="$HOOKDIR/pre-merge-commit"
+POSTCOMMIT="$HOOKDIR/post-commit"
 
 # Prepare sample scripts that write their $0 to actual_hooks
 test_expect_success 'sample script setup' '
@@ -28,11 +29,15 @@ test_expect_success 'sample script setup' '
 	echo $0 >>actual_hooks
 	test $GIT_PREFIX = "success/"
 	EOF
-	write_script "$HOOKDIR/check-author.sample" <<-\EOF
+	write_script "$HOOKDIR/check-author.sample" <<-\EOF &&
 	echo $0 >>actual_hooks
 	test "$GIT_AUTHOR_NAME" = "New Author" &&
 	test "$GIT_AUTHOR_EMAIL" = "[email protected]"
 	EOF
+	write_script "$HOOKDIR/user-input.sample" <<-\EOF
+	! read -r line || echo "$line" > hook_input
+	exit 0
+	EOF
 '
 
 test_expect_success 'root commit' '
@@ -278,4 +283,34 @@ test_expect_success 'check the author in hook' '
 	test_cmp expected_hooks actual_hooks
 '
 
+test_expect_success 'with user input' '
+	test_when_finished "rm -f \"$PRECOMMIT\" user_input hook_input" &&
+	cp "$HOOKDIR/user-input.sample" "$PRECOMMIT" &&
+	echo "user input" > user_input &&
+	echo "more" >>file &&
+	git add file &&
+	git commit -m "more" < user_input &&
+	test_cmp user_input hook_input
+'
+
+test_expect_success 'post-commit with user input' '
+	test_when_finished "rm -f \"$POSTCOMMIT\" user_input hook_input" &&
+	cp "$HOOKDIR/user-input.sample" "$POSTCOMMIT" &&
+	echo "user input" > user_input &&
+	echo "more" >>file &&
+	git add file &&
+	git commit -m "more" < user_input &&
+	test_cmp user_input hook_input
+'
+
+test_expect_success 'with user input (merge)' '
+	test_when_finished "rm -f \"$PREMERGE\" user_input hook_input" &&
+	cp "$HOOKDIR/user-input.sample" "$PREMERGE" &&
+	echo "user input" > user_input &&
+	git checkout side &&
+	git merge -m "merge master" master < user_input &&
+	git checkout master &&
+	test_cmp user_input hook_input
+'
+
 test_done
t/t7504-commit-msg-hook.sh +15 -0
index 31b9c6a2c1..ad467aad86 100755
@@ -294,5 +294,20 @@ test_expect_success 'hook is called for reword during `rebase -i`' '
 
 '
 
+# now a hook that accepts input and writes it as the commit message
+cat > "$HOOK" <<'EOF'
+#!/bin/sh
+! read -r line || echo "$line" > "$1"
+EOF
+chmod +x "$HOOK"
+
+test_expect_success 'hook with user input' '
+
+	echo "additional" >> file &&
+	git add file &&
+	echo "user input" | git commit -m "additional" &&
+	commit_msg_is "user input"
+
+'
 
 test_done
t/t7505-prepare-commit-msg-hook.sh +14 -0
index 94f85cdf83..16a161f129 100755
@@ -91,6 +91,11 @@ else
 fi
 test "$GIT_EDITOR" = : && source="$source (no editor)"
 
+if read -r line
+then
+	source="$source $line"
+fi
+
 if test $rebasing = 1
 then
 	echo "$source $(get_last_cmd)" >"$1"
@@ -113,6 +118,15 @@ test_expect_success 'with hook (-m)' '
 
 '
 
+test_expect_success 'with hook (-m and input)' '
+
+	echo "more" >> file &&
+	git add file &&
+	echo "user input" | git commit -m "more" &&
+	test "$(git log -1 --pretty=format:%s)" = "message (no editor) user input"
+
+'
+
 test_expect_success 'with hook (-m editor)' '
 
 	echo "more" >> file &&

base-commit: e31aba42fb12bdeb0f850829e008e1e3f43af500

Signature · 1 line
gitgitgadget
#4 lore raw

Re: [PATCH v3] hooks: allow input from stdin for commit-related hooks

Quoted 8 lines · Orgad Shaneh via GitGitGadget

From: Orgad Shaneh <[email protected]>

Let hooks receive user input if applicable.

Closing stdin originates in f5bbc3225 (Port git commit to C, 2007). Looks like the original shell implementation did have stdin open. Not clear why the author chose to close it on the C port (maybe copy&paste).

Please drop unsubstanciated guess in the parentheses. If anything, we've learned during the discussion thread that it is a bad idea to leave the standard input open when spawning hooks in general, and to me it looks like a lot more plausible reason why we tightened the interface when it was made from "only to run hooks for 'git commit'" to an interface that more widely usable to run any hook. If we are not going to record that finding in the log message to help people to find out what we knew at the time when the commit was created, then we shouldn't mislead them with "maybe copy&paste" that is not backed by anything other than a hunch.

Quoted 2 lines

Allow stdin only for commit-related hooks. Some of the other hooks pass their own input to the hook, so don't change them.

Before this paragraph that gives orders to the code to "be like so", the log message needs to explain why it is a good idea to make such a change. Which hook benefits by being able to read the standard input? Describe what becomes possible in terms of end-user visible effects (i.e. "now reading standard input becomes possible for pre-commit hook" is *not* an answer. What new things a pre-commit hook that now can read from the standard input do for the end user?) to justify why such a change is a good thing to have, before this paragraph to justify why leaving the standard input open for hooks run by "git commit" is a good idea and is a safe thing to do.

Note that even "git commit" may compete for its standard input with hooks. "git commit -F - <message" currently may read the message to EOF before doing anything interesting like spawning a hook, but it is not implausible that the reading of the message may want to happen much later in a future codebase, at which point the hook may end up stealing the beginning of the message by reading from the standard input. So ideally, if we can find a way to selectively close the standard input for the hooks if "git commit" itself uses the standard input, that would be better than unconditionally leaving it open.

Let's reorder the patch hunks to see the bottom layer first, as the callers are mostly the same.

Quoted 25 lines
run-command.h +8 -3
index 6472b38bde..e6a850c6fe 100644
@@ -201,11 +201,16 @@ int run_command(struct child_process *);
  */
 const char *find_hook(const char *name);
 
+#define RUN_HOOK_ALLOW_STDIN 1
+
 /**
  * Run a hook.
- * The first argument is a pathname to an index file, or NULL
- * if the hook uses the default index file or no index is needed.
- * The second argument is the name of the hook.
+ * The first argument is an array of environment variables, or NULL
+ * if the hook uses the default environment and doesn't require
+ * additional variables.
+ * The second argument is zero or RUN_HOOK_ALLOW_STDIN, which enables
+ * stdin for the child process (the default is no_stdin).
+ * The third argument is the name of the hook.
  * The further arguments correspond to the hook arguments.
  * The last argument has to be NULL to terminate the arguments list.
  * If the hook does not exist or is not executable, the return

run-command.h const char *find_hook(const char *name); · line 224

Quoted 7 lines
run-command.h +2 -2
@@ -215,8 +220,8 @@ const char *find_hook(const char *name);
  * On execution, .stdout_to_stderr and .no_stdin will be set.
  */
 LAST_ARG_MUST_BE_NULL
-int run_hook_le(const char *const *env, const char *name, ...);
-int run_hook_ve(const char *const *env, const char *name, va_list args);
+int run_hook_le(const char *const *env, int opt, const char *name, ...);
+int run_hook_ve(const char *const *env, int opt, const char *name, va_list args);

Is this new parameter meant to be used as an enum? When the run_hook interface gets extended the next time and we want a new option, is the option expected to be mutually incompatible with allow-stdin?

I suspect that it would make this a more useful API if this new parameter is not used as an enum but as a collection of flag bits.

If so, a few things must change in the above:

- The description of the second parameter in the comment shouldn't say "zero or RUN_HOOK_ALLOW_STDIN"; it should rather say "an OR'ed collection of feature bits like RUN_HOOK_ALLOW_STDIN defined above"

- The second parameter should be 'unsigned flags', not 'int opt'.

It is my understanding that "git commit" only needs run_hook_ve() to drive its hook scripts. Isn't it premature to touch run_hook_le(), in which nobody wants to leave the standard input open while running hooks? It _might_ be a better idea to allow users of _le() to do the same eventually, but then perhaps it is a good idea to do so in a separate step at the end, as "only to be complete" patch. That is, the structure of the topic ought to be something like:

- [PATCH 1/2] add the "unsigned flags" word to _ve(), assign the RUN_HOOK_ALLOW_STDIN bit, and update commit.c::run_commit_hook() to pass RUN_HOOK_ALLOW_STDIN to it.

- [PATCH 2/2] after surveying the options "git commit" takes, find out the condition where "git commit" itself would want to consume the standard input (e.g. "commit -F -", there may be others), and tell run_commit_hook() *not* to pass RUN_HOOK_ALLOW_STDIN when we use the standard input ourselves (i.e. forbid hooks to read from it).

- [PATCH 3/2] add the same "unsigned flags" word to _le(), and teach all callers to pass 0, as a "just for completeness" step.

Personally, I think we should stop at [2/2], and do not do [3/2], as there is no real demonstrated use of the standard input for hooks. Especially because users of the _le() interface includes programs like receive-pack whose standard input should not be molested, I'd feel safer not to see [3/2] done at all (for that matter, I'm not happy with [1/2] unless it comes with [2/2], either).

Quoted 14 lines
run-command.c +1 -1
index 2ee59acdc8..21b1f0a5e9 100644
@@ -1343,7 +1343,7 @@ const char *find_hook(const char *name)
 	return path.buf;
 }
 
-int run_hook_ve(const char *const *env, const char *name, va_list args)
+int run_hook_ve(const char *const *env, int opt, const char *name, va_list args)
 {
 	struct child_process hook = CHILD_PROCESS_INIT;
 	const char *p;

run-command.c int run_hook_ve(const char *const *env, const char *name, va_list args) · line 1360

Quoted 6 lines
run-command.c +2 -1
@@ -1356,20 +1356,21 @@ int run_hook_ve(const char *const *env, const char *name, va_list args)
 	while ((p = va_arg(args, const char *)))
 		strvec_push(&hook.args, p);
 	hook.env = env;
-	hook.no_stdin = 1;
+	if (!(opt & RUN_HOOK_ALLOW_STDIN))
+		hook.no_stdin = 1;

OK, so you are using the parameter as a flag word after all. Then "int opt" should definitely be "unsigned flags". And these two lines would be more readable, when written like so:

	hook.no_stdin = !(flags & RUN_HOOK_ALLOW_STDIN);

I would think.

Quoted 5 lines
run-command.c +0 -0
 	hook.stdout_to_stderr = 1;
 	hook.trace2_hook_name = name;
 
 	return run_command(&hook);
 }

run-command.c int run_hook_ve(const char *const *env, const char *name, va_list args) · line 1373

Quoted 10 lines
run-command.c +2 -2
 
-int run_hook_le(const char *const *env, const char *name, ...)
+int run_hook_le(const char *const *env, int opt, const char *name, ...)
 {
 	va_list args;
 	int ret;
 
 	va_start(args, name);
-	ret = run_hook_ve(env, name, args);
+	ret = run_hook_ve(env, opt, name, args);

I'd rather not to see the function signature of _le() changed in patches [1/2] and [2/2]; instead we can just pass hardcoded 0 from here to the underlying _ve().

Now, what is left is individual commands that use the run_hook interface.

commit.c int run_commit_hook(int editor_is_used, const char *index_file, · line 1652

Quoted 13 lines
commit.c +1 -1
index fe1fa3dc41..775019ec9d 100644
@@ -1646,7 +1646,7 @@ int run_commit_hook(int editor_is_used, const char *index_file,
 		strvec_push(&hook_env, "GIT_EDITOR=:");
 
 	va_start(args, name);
-	ret = run_hook_ve(hook_env.v, name, args);
+	ret = run_hook_ve(hook_env.v, RUN_HOOK_ALLOW_STDIN, name, args);
 	va_end(args);
 	strvec_clear(&hook_env);
 

This is good for [1/2]. We should avoid "git commit" from competing with hooks for its standard input by conditionally passing ALLOW_STDIN from here---only when the program itself does not use the standard input in [2/2].

builtin/am.c

Quoted 1 line
builtin/am.c +0 -0

"git am <mbox" reads from the mailbox. "git am -i" interacts with the end user via its stdin/stdout. There may be other situations where the hooks should not touch the standard input.

builtin/checkout.c

Quoted 1 line
builtin/checkout.c +0 -0

I do not offhand think of a reason why "git checkout" would compete with its hooks for the standard input. If we were to allow hooks to read from the standard input, that should come as an independent patch for each program after patch [3/2], I think. The ones I don't mention below should never leave the standard input open for hooks.

reset.c

Quoted 5 lines
builtin/clone.c +0 -0
builtin/gc.c +0 -0
builtin/merge.c +0 -0
builtin/rebase.c +0 -0
reset.c +0 -0

Ditto.

Quoted 15 lines
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +1 -0
index b3485450a2..e915ffe546 100755
@@ -7,6 +7,7 @@ test_description='pre-commit and pre-merge-commit hooks'
 HOOKDIR="$(git rev-parse --git-dir)/hooks"
 PRECOMMIT="$HOOKDIR/pre-commit"
 PREMERGE="$HOOKDIR/pre-merge-commit"
+POSTCOMMIT="$HOOKDIR/post-commit"
 
 # Prepare sample scripts that write their $0 to actual_hooks
 test_expect_success 'sample script setup' '
@@ -28,11 +29,15 @@ test_expect_success 'sample script setup' '
 	echo $0 >>actual_hooks
 	test $GIT_PREFIX = "success/"

t/t7503-pre-commit-and-pre-merge-commit-hooks.sh test_expect_success 'sample script setup' ' · line 39

Quoted 10 lines
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +4 -1
 	EOF
-	write_script "$HOOKDIR/check-author.sample" <<-\EOF
+	write_script "$HOOKDIR/check-author.sample" <<-\EOF &&
 	echo $0 >>actual_hooks
 	test "$GIT_AUTHOR_NAME" = "New Author" &&
 	test "$GIT_AUTHOR_EMAIL" = "[email protected]"
 	EOF
+	write_script "$HOOKDIR/user-input.sample" <<-\EOF
+	! read -r line || echo "$line" > hook_input
+	exit 0

Style (Documentation/CodingGuidelines)

- Redirection operators should be written with space before, but no space after them. In other words, write 'echo test >"$file"' instead of 'echo test> $file' or 'echo test > $file'.

So, when our "read" immediately hits EOF (or I/O error, but let's not worry about that case for now), we leave hook_input file alone, but otherwise we write the single line we read to hook_input, and regardless of an error, we report success to the invoking "git commit". Which makes sense.

We report success even when we had trouble writing into hook_input file, though. Perhaps you should lose the "exit 0" at the end?

t/t7503-pre-commit-and-pre-merge-commit-hooks.sh test_expect_success 'check the author in hook' ' · line 289

Quoted 12 lines
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +5 -0
+	EOF
 '
 
 test_expect_success 'root commit' '
@@ -278,4 +283,34 @@ test_expect_success 'check the author in hook' '
 	test_cmp expected_hooks actual_hooks
 '
 
+test_expect_success 'with user input' '
+	test_when_finished "rm -f \"$PRECOMMIT\" user_input hook_input" &&
+	cp "$HOOKDIR/user-input.sample" "$PRECOMMIT" &&
+	echo "user input" > user_input &&

Style (I won't repeat from here on).

t/t7503-pre-commit-and-pre-merge-commit-hooks.sh test_expect_success 'check the author in hook' ' · line 294

Quoted 5 lines
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +5 -0
+	echo "more" >>file &&
+	git add file &&
+	git commit -m "more" < user_input &&
+	test_cmp user_input hook_input
+'

This is probably a good place to also test

git commit -F - <user_input

and see what happens.

Thanks.

#5 lore raw

Re: [PATCH v3] hooks: allow input from stdin for commit-related hooks

Quoted 12 lines · Junio C Hamano
Quoted 8 lines · Orgad Shaneh via GitGitGadget

From: Orgad Shaneh <[email protected]>

Let hooks receive user input if applicable.

Closing stdin originates in f5bbc3225 (Port git commit to C, 2007). Looks like the original shell implementation did have stdin open. Not clear why the author chose to close it on the C port (maybe copy&paste).

Quoted 10 lines

Please drop unsubstanciated guess in the parentheses. If anything, we've learned during the discussion thread that it is a bad idea to leave the standard input open when spawning hooks in general, and to me it looks like a lot more plausible reason why we tightened the interface when it was made from "only to run hooks for 'git commit'" to an interface that more widely usable to run any hook. If we are not going to record that finding in the log message to help people to find out what we knew at the time when the commit was created, then we shouldn't mislead them with "maybe copy&paste" that is not backed by anything other than a hunch.

Done

Quoted 13 lines
Quoted 2 lines

Allow stdin only for commit-related hooks. Some of the other hooks pass their own input to the hook, so don't change them.

Before this paragraph that gives orders to the code to "be like so", the log message needs to explain why it is a good idea to make such a change. Which hook benefits by being able to read the standard input? Describe what becomes possible in terms of end-user visible effects (i.e. "now reading standard input becomes possible for pre-commit hook" is *not* an answer. What new things a pre-commit hook that now can read from the standard input do for the end user?) to justify why such a change is a good thing to have, before this paragraph to justify why leaving the standard input open for hooks run by "git commit" is a good idea and is a safe thing to do.

Added justification.

Quoted 10 lines

Note that even "git commit" may compete for its standard input with hooks. "git commit -F - <message" currently may read the message to EOF before doing anything interesting like spawning a hook, but it is not implausible that the reading of the message may want to happen much later in a future codebase, at which point the hook may end up stealing the beginning of the message by reading from the standard input. So ideally, if we can find a way to selectively close the standard input for the hooks if "git commit" itself uses the standard input, that would be better than unconditionally leaving it open.

Good catch! I found that pre-commit is called before reading stdin, so -F - is broken if pre-commit reads the input. Not sure what's the best way to solve this. Should I pass the flag to run_commit_hook everywhere? Or maybe add a new opt-out flag for run_commit_hook, and pass 0 on most calls?

Quoted 32 lines

Let's reorder the patch hunks to see the bottom layer first, as the callers are mostly the same.

Quoted 29 lines
run-command.h +8 -4
index 6472b38bde..e6a850c6fe 100644
@@ -201,11 +201,16 @@ int run_command(struct child_process *);
  */
 const char *find_hook(const char *name);
 
+#define RUN_HOOK_ALLOW_STDIN 1
+
 /**
  * Run a hook.
- * The first argument is a pathname to an index file, or NULL
- * if the hook uses the default index file or no index is needed.
- * The second argument is the name of the hook.
+ * The first argument is an array of environment variables, or NULL
+ * if the hook uses the default environment and doesn't require
+ * additional variables.
+ * The second argument is zero or RUN_HOOK_ALLOW_STDIN, which enables
+ * stdin for the child process (the default is no_stdin).
+ * The third argument is the name of the hook.
  * The further arguments correspond to the hook arguments.
  * The last argument has to be NULL to terminate the arguments list.
  * If the hook does not exist or is not executable, the return
@@ -215,8 +220,8 @@ const char *find_hook(const char *name);
  * On execution, .stdout_to_stderr and .no_stdin will be set.
  */
 LAST_ARG_MUST_BE_NULL
-int run_hook_le(const char *const *env, const char *name, ...);
Quoted 8 lines

run-command.h const char *find_hook(const char *name); · line 224

Quoted 3 lines
run-command.h +2 -1
-int run_hook_ve(const char *const *env, const char *name, va_list args);
+int run_hook_le(const char *const *env, int opt, const char *name, ...);
+int run_hook_ve(const char *const *env, int opt, const char *name, va_list args);

Is this new parameter meant to be used as an enum? When the run_hook interface gets extended the next time and we want a new option, is the option expected to be mutually incompatible with allow-stdin?

You found it :)

Quoted 9 lines

I suspect that it would make this a more useful API if this new parameter is not used as an enum but as a collection of flag bits.

If so, a few things must change in the above:

- The description of the second parameter in the comment shouldn't say "zero or RUN_HOOK_ALLOW_STDIN"; it should rather say "an OR'ed collection of feature bits like RUN_HOOK_ALLOW_STDIN defined above"

Done, thanks.

Quoted 1 line

- The second parameter should be 'unsigned flags', not 'int opt'.

I copied from run_command_v_opt*, which have int opt for flags. Changed anyway.

Quoted 12 lines

It is my understanding that "git commit" only needs run_hook_ve() to drive its hook scripts. Isn't it premature to touch run_hook_le(), in which nobody wants to leave the standard input open while running hooks? It _might_ be a better idea to allow users of _le() to do the same eventually, but then perhaps it is a good idea to do so in a separate step at the end, as "only to be complete" patch. That is, the structure of the topic ought to be something like:

- [PATCH 1/2] add the "unsigned flags" word to _ve(), assign the RUN_HOOK_ALLOW_STDIN bit, and update commit.c::run_commit_hook() to pass RUN_HOOK_ALLOW_STDIN to it.

Quoted 6 lines

- [PATCH 2/2] after surveying the options "git commit" takes, find out the condition where "git commit" itself would want to consume the standard input (e.g. "commit -F -", there may be others), and tell run_commit_hook() *not* to pass RUN_HOOK_ALLOW_STDIN when we use the standard input ourselves (i.e. forbid hooks to read from it).

Accepted. Waiting for your feedback to implement this part.

Quoted 9 lines

- [PATCH 3/2] add the same "unsigned flags" word to _le(), and teach all callers to pass 0, as a "just for completeness" step.

Personally, I think we should stop at [2/2], and do not do [3/2], as there is no real demonstrated use of the standard input for hooks. Especially because users of the _le() interface includes programs like receive-pack whose standard input should not be molested, I'd feel safer not to see [3/2] done at all (for that matter, I'm not happy with [1/2] unless it comes with [2/2], either).

Agreed.

Quoted 10 lines

run-command.h const char *find_hook(const char *name); · line 226

Quoted 2 lines
run-command.h +2 -0
+     if (!(opt & RUN_HOOK_ALLOW_STDIN))
+             hook.no_stdin = 1;

OK, so you are using the parameter as a flag word after all. Then "int opt" should definitely be "unsigned flags". And these two lines would be more readable, when written like so:

        hook.no_stdin = !(flags & RUN_HOOK_ALLOW_STDIN);

I would think.

Done.

Quoted 84 lines
Quoted 5 lines
run-command.h +0 -0
      hook.stdout_to_stderr = 1;
      hook.trace2_hook_name = name;
 
      return run_command(&hook);
 }

run-command.h const char *find_hook(const char *name);

Quoted 10 lines
run-command.h +2 -2
 
-int run_hook_le(const char *const *env, const char *name, ...)
+int run_hook_le(const char *const *env, int opt, const char *name, ...)
 {
      va_list args;
      int ret;
 
      va_start(args, name);
-     ret = run_hook_ve(env, name, args);
+     ret = run_hook_ve(env, opt, name, args);

I'd rather not to see the function signature of _le() changed in patches [1/2] and [2/2]; instead we can just pass hardcoded 0 from here to the underlying _ve().

Now, what is left is individual commands that use the run_hook interface.

commit.c int run_commit_hook(int editor_is_used, const char *index_file, · line 1652

Quoted 13 lines
commit.c +1 -1
index fe1fa3dc41..775019ec9d 100644
@@ -1646,7 +1646,7 @@ int run_commit_hook(int editor_is_used, const char *index_file,
              strvec_push(&hook_env, "GIT_EDITOR=:");
 
      va_start(args, name);
-     ret = run_hook_ve(hook_env.v, name, args);
+     ret = run_hook_ve(hook_env.v, RUN_HOOK_ALLOW_STDIN, name, args);
      va_end(args);
      strvec_clear(&hook_env);
 

This is good for [1/2]. We should avoid "git commit" from competing with hooks for its standard input by conditionally passing ALLOW_STDIN from here---only when the program itself does not use the standard input in [2/2].

builtin/am.c

Quoted 1 line
builtin/am.c +0 -0

"git am <mbox" reads from the mailbox. "git am -i" interacts with the end user via its stdin/stdout. There may be other situations where the hooks should not touch the standard input.

builtin/checkout.c

Quoted 1 line
builtin/checkout.c +0 -0

I do not offhand think of a reason why "git checkout" would compete with its hooks for the standard input. If we were to allow hooks to read from the standard input, that should come as an independent patch for each program after patch [3/2], I think. The ones I don't mention below should never leave the standard input open for hooks.

reset.c

Quoted 5 lines
builtin/clone.c +0 -0
builtin/gc.c +0 -0
builtin/merge.c +0 -0
builtin/rebase.c +0 -0
reset.c +0 -0

Ditto.

Quoted 19 lines
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +2 -1
index b3485450a2..e915ffe546 100755
@@ -7,6 +7,7 @@ test_description='pre-commit and pre-merge-commit hooks'
 HOOKDIR="$(git rev-parse --git-dir)/hooks"
 PRECOMMIT="$HOOKDIR/pre-commit"
 PREMERGE="$HOOKDIR/pre-merge-commit"
+POSTCOMMIT="$HOOKDIR/post-commit"
 
 # Prepare sample scripts that write their $0 to actual_hooks
 test_expect_success 'sample script setup' '
@@ -28,11 +29,15 @@ test_expect_success 'sample script setup' '
      echo $0 >>actual_hooks
      test $GIT_PREFIX = "success/"
      EOF
-     write_script "$HOOKDIR/check-author.sample" <<-\EOF
+     write_script "$HOOKDIR/check-author.sample" <<-\EOF &&
      echo $0 >>actual_hooks
Quoted 8 lines

t/t7503-pre-commit-and-pre-merge-commit-hooks.sh test_expect_success 'sample script setup' ' · line 39

Quoted 6 lines
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +3 -0
      test "$GIT_AUTHOR_NAME" = "New Author" &&
      test "$GIT_AUTHOR_EMAIL" = "[email protected]"
      EOF
+     write_script "$HOOKDIR/user-input.sample" <<-\EOF
+     ! read -r line || echo "$line" > hook_input
+     exit 0

Style (Documentation/CodingGuidelines)

Fixed.

Quoted 4 lines

- Redirection operators should be written with space before, but no space after them. In other words, write 'echo test >"$file"' instead of 'echo test> $file' or 'echo test > $file'.

Quoted 8 lines

So, when our "read" immediately hits EOF (or I/O error, but let's not worry about that case for now), we leave hook_input file alone, but otherwise we write the single line we read to hook_input, and regardless of an error, we report success to the invoking "git commit". Which makes sense.

We report success even when we had trouble writing into hook_input file, though. Perhaps you should lose the "exit 0" at the end?

Removed.

Quoted 8 lines
Quoted 8 lines
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +1 -0
+     EOF
 '
 
 test_expect_success 'root commit' '
@@ -278,4 +283,34 @@ test_expect_success 'check the author in hook' '
      test_cmp expected_hooks actual_hooks
 '
 
Quoted 6 lines

t/t7503-pre-commit-and-pre-merge-commit-hooks.sh test_expect_success 'check the author in hook' ' · line 289

Quoted 4 lines
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +4 -0
+test_expect_success 'with user input' '
+     test_when_finished "rm -f \"$PRECOMMIT\" user_input hook_input" &&
+     cp "$HOOKDIR/user-input.sample" "$PRECOMMIT" &&
+     echo "user input" > user_input &&

Style (I won't repeat from here on).

Fixed.

Quoted 11 lines

t/t7503-pre-commit-and-pre-merge-commit-hooks.sh test_expect_success 'check the author in hook' ' · line 294

Quoted 5 lines
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +5 -0
+     echo "more" >>file &&
+     git add file &&
+     git commit -m "more" < user_input &&
+     test_cmp user_input hook_input
+'

This is probably a good place to also test

git commit -F - <user_input

and see what happens.

Added a test (currently failing).

Quoted 1 line

Thanks.

Thank you! Your feedback is thorough and helpful.

- Orgad

#6 lore raw

v4 cover hooks: allow input from stdin for commit-related hooks

In reply to Orgad Shaneh via GitGitGadget

Let hooks receive user input if applicable.

Closing stdin originates in f5bbc3225 (Port git commit to C, 2007). Looks like the original shell implementation did have stdin open. Not clear why the author chose to close it on the C port (maybe copy&paste).

The only hook that passes internal information to the hook via stdin is pre-push, which has its own logic.

Some references of users requesting this feature. Some of them use acrobatics to gain access to stdin: [1]
https://stackoverflow.com/q/1067874/764870[2]
https://stackoverflow.com/q/47477766/764870[3]
https://stackoverflow.com/q/3417896/764870[4]
https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/3165[5]
https://github.com/typicode/husky/issues/442

Orgad Shaneh (2):
hooks: allow input from stdin for commit-related hooks
commit: fix stdin conflict between message and hook

 builtin/commit.c                              | 14 ++++--
 builtin/merge.c                               | 12 +++--
 commit.c                                      |  4 +-
 commit.h                                      |  3 +-
 run-command.c                                 |  6 +--
 run-command.h                                 | 17 +++++--
 sequencer.c                                   |  6 +--
 ...3-pre-commit-and-pre-merge-commit-hooks.sh | 46 ++++++++++++++++++-
 t/t7504-commit-msg-hook.sh                    | 15 ++++++
 t/t7505-prepare-commit-msg-hook.sh            | 14 ++++++
 10 files changed, 113 insertions(+), 24 deletions(-)

base-commit: e31aba42fb12bdeb0f850829e008e1e3f43af500
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-790%2Forgads%2Fhooks-stdin-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-790/orgads/hooks-stdin-v4
Pull-Request: https://github.com/gitgitgadget/git/pull/790

Range-diff vs v3:

1: 2f7a45c828 ! 1: 3bd6024a23 hooks: allow input from stdin for commit-related hooks
@@ Commit message

Closing stdin originates in f5bbc3225 (Port git commit to C, 2007). Looks like the original shell implementation did have
- stdin open. Not clear why the author chose to close it on
- the C port (maybe copy&paste).
+ stdin open.

- Allow stdin only for commit-related hooks. Some of the other
- hooks pass their own input to the hook, so don't change them.
+ This allows for example prompting the user to choose an issue
+ in prepare-commit-msg, and add "Fixes #123" to the commit message.

- Signed-off-by: Orgad Shaneh <[email protected]>
+ Another possible use-case is running sanity test on pre-commit,
+ and having a prompt like "This and that issue were found in your
+ changes. Are you sure you want to commit? [Y/N]".

- ## builtin/am.c ##
-@@ builtin/am.c: static int run_applypatch_msg_hook(struct am_state *state)
- int ret;
-
- assert(state->msg);
-- ret = run_hook_le(NULL, "applypatch-msg", am_path(state, "final-commit"), NULL);
-+ ret = run_hook_le(NULL, 0, "applypatch-msg", am_path(state, "final-commit"), NULL);
-
- if (!ret) {
- FREE_AND_NULL(state->msg);
-@@ builtin/am.c: static void do_commit(const struct am_state *state)
- const char *reflog_msg, *author, *committer = NULL;
- struct strbuf sb = STRBUF_INIT;
-
-- if (run_hook_le(NULL, "pre-applypatch", NULL))
-+ if (run_hook_le(NULL, 0, "pre-applypatch", NULL))
- exit(1);
-
- if (write_cache_as_tree(&tree, 0, NULL))
-@@ builtin/am.c: static void do_commit(const struct am_state *state)
- fclose(fp);
- }
-
-- run_hook_le(NULL, "post-applypatch", NULL);
-+ run_hook_le(NULL, 0, "post-applypatch", NULL);
-
- strbuf_release(&sb);
- }
-
- ## builtin/checkout.c ##
-@@ builtin/checkout.c: struct branch_info {
- static int post_checkout_hook(struct commit *old_commit, struct commit *new_commit,
- int changed)
- {
-- return run_hook_le(NULL, "post-checkout",
-+ return run_hook_le(NULL, 0, "post-checkout",
- oid_to_hex(old_commit ? &old_commit->object.oid : &null_oid),
- oid_to_hex(new_commit ? &new_commit->object.oid : &null_oid),
- changed ? "1" : "0", NULL);
-
- ## builtin/clone.c ##
-@@ builtin/clone.c: static int checkout(int submodule_progress)
- if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
- die(_("unable to write new index file"));
-
-- err |= run_hook_le(NULL, "post-checkout", oid_to_hex(&null_oid),
-+ err |= run_hook_le(NULL, 0, "post-checkout", oid_to_hex(&null_oid),
- oid_to_hex(&oid), "1", NULL);
-
- if (!err && (option_recurse_submodules.nr > 0)) {
-
- ## builtin/gc.c ##
-@@ builtin/gc.c: static int need_to_gc(void)
- else
- return 0;
-
-- if (run_hook_le(NULL, "pre-auto-gc", NULL))
-+ if (run_hook_le(NULL, 0, "pre-auto-gc", NULL))
- return 0;
- return 1;
- }
-
- ## builtin/merge.c ##
-@@ builtin/merge.c: static void finish(struct commit *head_commit,
- }
-
- /* Run a post-merge hook */
-- run_hook_le(NULL, "post-merge", squash ? "1" : "0", NULL);
-+ run_hook_le(NULL, 0, "post-merge", squash ? "1" : "0", NULL);
-
- apply_autostash(git_path_merge_autostash(the_repository));
- strbuf_release(&reflog_message);
+ Allow stdin only for commit-related hooks. Some of the other
+ hooks pass their own input to the hook, so don't change them.

- ## builtin/rebase.c ##
-@@ builtin/rebase.c: int cmd_rebase(int argc, const char **argv, const char *prefix) -
- /* If a hook exists, give it a chance to interrupt*/
- if (!ok_to_skip_pre_rebase &&
-- run_hook_le(NULL, "pre-rebase", options.upstream_arg,
-+ run_hook_le(NULL, 0, "pre-rebase", options.upstream_arg,
- argc ? argv[0] : NULL, NULL))
- die(_("The pre-rebase hook refused to rebase."));
-
+ Note: If pre-commit reads from stdin, and git commit is executed
+ with -F - (read message from stdin), the message is not read
+ correctly. This is fixed in the follow-up commit.

- ## builtin/receive-pack.c ##
-@@ builtin/receive-pack.c: static const char *push_to_checkout(unsigned char *hash,
- const char *work_tree)
- {
- strvec_pushf(env, "GIT_WORK_TREE=%s", absolute_path(work_tree));
-- if (run_hook_le(env->v, push_to_checkout_hook,
-+ if (run_hook_le(env->v, 0, push_to_checkout_hook,
- hash_to_hex(hash), NULL))
- return "push-to-checkout hook declined";
- else
+ Signed-off-by: Orgad Shaneh <[email protected]>

## commit.c ##
@@ commit.c: int run_commit_hook(int editor_is_used, const char *index_file,
@@ commit.c: int run_commit_hook(int editor_is_used, const char *index_file,
strvec_clear(&hook_env);

- ## read-cache.c ##
-@@ read-cache.c: static int do_write_locked_index(struct index_state *istate, struct lock_file *l
- else
- ret = close_lock_file_gently(lock);
-
-- run_hook_le(NULL, "post-index-change",
-+ run_hook_le(NULL, 0, "post-index-change",
- istate->updated_workdir ? "1" : "0",
- istate->updated_skipworktree ? "1" : "0", NULL);
- istate->updated_workdir = 0;
-
- ## reset.c ##
-@@ reset.c: int reset_head(struct repository *r, struct object_id *oid, const char *action,
- reflog_head);
- }
- if (run_hook)
-- run_hook_le(NULL, "post-checkout",
-+ run_hook_le(NULL, 0, "post-checkout",
- oid_to_hex(orig ? orig : &null_oid),
- oid_to_hex(oid), "1", NULL);
-
-
## run-command.c ##
@@ run-command.c: const char *find_hook(const char *name)
return path.buf;
}

-int run_hook_ve(const char *const *env, const char *name, va_list args)
-+int run_hook_ve(const char *const *env, int opt, const char *name, va_list args) ++int run_hook_ve(const char *const *env, unsigned flags, const char *name, va_list args)
{
struct child_process hook = CHILD_PROCESS_INIT;
const char *p;
@@ run-command.c: int run_hook_ve(const char *const *env, const char *name, va_list
strvec_push(&hook.args, p);
hook.env = env;
- hook.no_stdin = 1;
-+ if (!(opt & RUN_HOOK_ALLOW_STDIN))
-+ hook.no_stdin = 1;
++ hook.no_stdin = !(flags & RUN_HOOK_ALLOW_STDIN);
hook.stdout_to_stderr = 1;
hook.trace2_hook_name = name;

- return run_command(&hook);
- }
-
--int run_hook_le(const char *const *env, const char *name, ...) -+int run_hook_le(const char *const *env, int opt, const char *name, ...)
- {
- va_list args;
+@@ run-command.c: int run_hook_le(const char *const *env, const char *name, ...)
int ret;

va_start(args, name);
- ret = run_hook_ve(env, name, args);
-+ ret = run_hook_ve(env, opt, name, args);
++ ret = run_hook_ve(env, 0, name, args);
va_end(args);

return ret;
@@ run-command.h: int run_command(struct child_process *);
- * The first argument is a pathname to an index file, or NULL
- * if the hook uses the default index file or no index is needed.
- * The second argument is the name of the hook.
-+ * The first argument is an array of environment variables, or NULL
++ * The env argument is an array of environment variables, or NULL
+ * if the hook uses the default environment and doesn't require
+ * additional variables.
-+ * The second argument is zero or RUN_HOOK_ALLOW_STDIN, which enables
++ * The flags argument is an OR'ed collection of feature bits like
++ * RUN_HOOK_ALLOW_STDIN defined above, which enables
+ * stdin for the child process (the default is no_stdin).
-+ * The third argument is the name of the hook.
++ * The name argument is the name of the hook.
* The further arguments correspond to the hook arguments.
* The last argument has to be NULL to terminate the arguments list.
* If the hook does not exist or is not executable, the return
-@@ run-command.h: const char *find_hook(const char *name);
- * On execution, .stdout_to_stderr and .no_stdin will be set.
+ * value will be zero.
+ * If it is executable, the hook will be executed and the exit
+ * status of the hook is returned.
+- * On execution, .stdout_to_stderr and .no_stdin will be set.
++ * On execution, .stdout_to_stderr will be set, and .no_stdin will be
++ * set unless RUN_HOOK_ALLOW_STDIN flag is requested.
*/
LAST_ARG_MUST_BE_NULL
--int run_hook_le(const char *const *env, const char *name, ...);
+ int run_hook_le(const char *const *env, const char *name, ...);
-int run_hook_ve(const char *const *env, const char *name, va_list args);
-+int run_hook_le(const char *const *env, int opt, const char *name, ...); -+int run_hook_ve(const char *const *env, int opt, const char *name, va_list args); ++int run_hook_ve(const char *const *env, unsigned flags, const char *name, va_list args);

/*
* Trigger an auto-gc
@@ t/t7503-pre-commit-and-pre-merge-commit-hooks.sh: test_expect_success 'sample sc
test "$GIT_AUTHOR_EMAIL" = "[email protected]"
EOF
+ write_script "$HOOKDIR/user-input.sample" <<-\EOF
-+ ! read -r line || echo "$line" > hook_input
-+ exit 0
++ ! read -r line || echo "$line" >hook_input
+ EOF
'

@@ t/t7503-pre-commit-and-pre-merge-commit-hooks.sh: test_expect_success 'check the
+test_expect_success 'with user input' '
+ test_when_finished "rm -f \"$PRECOMMIT\" user_input hook_input" &&
+ cp "$HOOKDIR/user-input.sample" "$PRECOMMIT" &&
-+ echo "user input" > user_input &&
++ echo "user input" >user_input &&
+ echo "more" >>file &&
+ git add file &&
-+ git commit -m "more" < user_input &&
++ git commit -m "more" <user_input &&
+ test_cmp user_input hook_input
+'
+
++test_expect_failure 'with user input combined with -F -' '
++ test_when_finished "rm -f \"$PRECOMMIT\" user_input hook_input" &&
++ cp "$HOOKDIR/user-input.sample" "$PRECOMMIT" &&
++ echo "user input" >user_input &&
++ echo "more" >>file &&
++ git add file &&
++ git commit -F - <user_input &&
++ ! test_path_is_file hook_input
++'
++
+test_expect_success 'post-commit with user input' '
+ test_when_finished "rm -f \"$POSTCOMMIT\" user_input hook_input" &&
+ cp "$HOOKDIR/user-input.sample" "$POSTCOMMIT" &&
-+ echo "user input" > user_input &&
++ echo "user input" >user_input &&
+ echo "more" >>file &&
+ git add file &&
-+ git commit -m "more" < user_input &&
++ git commit -m "more" <user_input &&
+ test_cmp user_input hook_input
+'
+
+test_expect_success 'with user input (merge)' '
+ test_when_finished "rm -f \"$PREMERGE\" user_input hook_input" &&
+ cp "$HOOKDIR/user-input.sample" "$PREMERGE" &&
-+ echo "user input" > user_input &&
++ echo "user input" >user_input &&
+ git checkout side &&
-+ git merge -m "merge master" master < user_input &&
++ git merge -m "merge master" master <user_input &&
+ git checkout master &&
+ test_cmp user_input hook_input
+'
@@ t/t7504-commit-msg-hook.sh: test_expect_success 'hook is called for reword durin
'

+# now a hook that accepts input and writes it as the commit message
-+cat > "$HOOK" <<'EOF'
++cat >"$HOOK" <<'EOF'
+#!/bin/sh
-+! read -r line || echo "$line" > "$1"
++! read -r line || echo "$line" >"$1"
+EOF
+chmod +x "$HOOK"
+
+test_expect_success 'hook with user input' '
+
-+ echo "additional" >> file &&
++ echo "additional" >>file &&
+ git add file &&
+ echo "user input" | git commit -m "additional" &&
+ commit_msg_is "user input"
@@ t/t7505-prepare-commit-msg-hook.sh: test_expect_success 'with hook (-m)' '

+test_expect_success 'with hook (-m and input)' '
+
-+ echo "more" >> file &&
++ echo "more" >>file &&
+ git add file &&
+ echo "user input" | git commit -m "more" &&
+ test "$(git log -1 --pretty=format:%s)" = "message (no editor) user input"
-: ---------- > 2: e048a9db62 commit: fix stdin conflict between message and hook

Signature · 1 line
gitgitgadget
#7 lore raw

PATCH v4 1/2 hooks: allow input from stdin for commit-related hooks

From: Orgad Shaneh <[email protected]>

Let hooks receive user input if applicable.

Closing stdin originates in f5bbc3225 (Port git commit to C, 2007). Looks like the original shell implementation did have stdin open.

This allows for example prompting the user to choose an issue in prepare-commit-msg, and add "Fixes #123" to the commit message.

Another possible use-case is running sanity test on pre-commit, and having a prompt like "This and that issue were found in your changes. Are you sure you want to commit? [Y/N]".

Allow stdin only for commit-related hooks. Some of the other hooks pass their own input to the hook, so don't change them.

Note: If pre-commit reads from stdin, and git commit is executed with -F - (read message from stdin), the message is not read correctly. This is fixed in the follow-up commit.

---
 commit.c                                      |  2 +-
 run-command.c                                 |  6 +--
 run-command.h                                 | 17 +++++--
 ...3-pre-commit-and-pre-merge-commit-hooks.sh | 46 ++++++++++++++++++-
 t/t7504-commit-msg-hook.sh                    | 15 ++++++
 t/t7505-prepare-commit-msg-hook.sh            | 14 ++++++
 6 files changed, 90 insertions(+), 10 deletions(-)
commit.c +1 -1
index fe1fa3dc41..775019ec9d 100644
@@ -1646,7 +1646,7 @@ int run_commit_hook(int editor_is_used, const char *index_file,
 		strvec_push(&hook_env, "GIT_EDITOR=:");
 
 	va_start(args, name);
-	ret = run_hook_ve(hook_env.v, name, args);
+	ret = run_hook_ve(hook_env.v, RUN_HOOK_ALLOW_STDIN, name, args);
 	va_end(args);
 	strvec_clear(&hook_env);
 
run-command.c +3 -3
index 2ee59acdc8..38ce53bee5 100644
@@ -1343,7 +1343,7 @@ const char *find_hook(const char *name)
 	return path.buf;
 }
 
-int run_hook_ve(const char *const *env, const char *name, va_list args)
+int run_hook_ve(const char *const *env, unsigned flags, const char *name, va_list args)
 {
 	struct child_process hook = CHILD_PROCESS_INIT;
 	const char *p;
@@ -1356,7 +1356,7 @@ int run_hook_ve(const char *const *env, const char *name, va_list args)
 	while ((p = va_arg(args, const char *)))
 		strvec_push(&hook.args, p);
 	hook.env = env;
-	hook.no_stdin = 1;
+	hook.no_stdin = !(flags & RUN_HOOK_ALLOW_STDIN);
 	hook.stdout_to_stderr = 1;
 	hook.trace2_hook_name = name;
 
@@ -1369,7 +1369,7 @@ int run_hook_le(const char *const *env, const char *name, ...)
 	int ret;
 
 	va_start(args, name);
-	ret = run_hook_ve(env, name, args);
+	ret = run_hook_ve(env, 0, name, args);
 	va_end(args);
 
 	return ret;
run-command.h +12 -5
index 6472b38bde..e613e5e3f9 100644
@@ -201,22 +201,29 @@ int run_command(struct child_process *);
  */
 const char *find_hook(const char *name);
 
+#define RUN_HOOK_ALLOW_STDIN 1
+
 /**
  * Run a hook.
- * The first argument is a pathname to an index file, or NULL
- * if the hook uses the default index file or no index is needed.
- * The second argument is the name of the hook.
+ * The env argument is an array of environment variables, or NULL
+ * if the hook uses the default environment and doesn't require
+ * additional variables.
+ * The flags argument is an OR'ed collection of feature bits like
+ * RUN_HOOK_ALLOW_STDIN defined above, which enables
+ * stdin for the child process (the default is no_stdin).
+ * The name argument is the name of the hook.
  * The further arguments correspond to the hook arguments.
  * The last argument has to be NULL to terminate the arguments list.
  * If the hook does not exist or is not executable, the return
  * value will be zero.
  * If it is executable, the hook will be executed and the exit
  * status of the hook is returned.
- * On execution, .stdout_to_stderr and .no_stdin will be set.
+ * On execution, .stdout_to_stderr will be set, and .no_stdin will be
+ * set unless RUN_HOOK_ALLOW_STDIN flag is requested.
  */
 LAST_ARG_MUST_BE_NULL
 int run_hook_le(const char *const *env, const char *name, ...);
-int run_hook_ve(const char *const *env, const char *name, va_list args);
+int run_hook_ve(const char *const *env, unsigned flags, const char *name, va_list args);
 
 /*
  * Trigger an auto-gc
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +45 -1

66 lines not on this page · raw message

t/t7504-commit-msg-hook.sh +15 -0
index 31b9c6a2c1..aa76eb7e1f 100755
@@ -294,5 +294,20 @@ test_expect_success 'hook is called for reword during `rebase -i`' '
 
 '
 
+# now a hook that accepts input and writes it as the commit message
+cat >"$HOOK" <<'EOF'
+#!/bin/sh
+! read -r line || echo "$line" >"$1"
+EOF
+chmod +x "$HOOK"
+
+test_expect_success 'hook with user input' '
+
+	echo "additional" >>file &&
+	git add file &&
+	echo "user input" | git commit -m "additional" &&
+	commit_msg_is "user input"
+
+'
 
 test_done
t/t7505-prepare-commit-msg-hook.sh +14 -0
index 94f85cdf83..aa9c9375e6 100755
@@ -91,6 +91,11 @@ else
 fi
 test "$GIT_EDITOR" = : && source="$source (no editor)"
 
+if read -r line
+then
+	source="$source $line"
+fi
+
 if test $rebasing = 1
 then
 	echo "$source $(get_last_cmd)" >"$1"
@@ -113,6 +118,15 @@ test_expect_success 'with hook (-m)' '
 
 '
 
+test_expect_success 'with hook (-m and input)' '
+
+	echo "more" >>file &&
+	git add file &&
+	echo "user input" | git commit -m "more" &&
+	test "$(git log -1 --pretty=format:%s)" = "message (no editor) user input"
+
+'
+
 test_expect_success 'with hook (-m editor)' '
 
 	echo "more" >> file &&
Signature · 1 line
gitgitgadget
#8 lore raw

Re: [PATCH v4 1/2] hooks: allow input from stdin for commit-related hooks

Quoted 8 lines · Orgad Shaneh via GitGitGadget

Let hooks receive user input if applicable.
[...]
This allows for example prompting the user to choose an issue in prepare-commit-msg, and add "Fixes #123" to the commit message.

Another possible use-case is running sanity test on pre-commit, and having a prompt like "This and that issue were found in your changes. Are you sure you want to commit? [Y/N]".

These use-cases really help readers understand the motivation for this change. Good.

Quoted 6 lines

Allow stdin only for commit-related hooks. Some of the other hooks pass their own input to the hook, so don't change them.

Note: If pre-commit reads from stdin, and git commit is executed with -F - (read message from stdin), the message is not read correctly. This is fixed in the follow-up commit.

Rather than making such a fundamental change and having to deal with the fallout by introducing complexity to handle various special-cases which pop up now and in the future, I wonder if it makes more sense to instead just update documentation to tell hook authors to read explicitly from the console rather than expecting stdin to be available (since stdin may already be consumed for other purposes when dealing with hooks or commands which invoke the hooks).

#9 lore raw

Re: [PATCH v4 1/2] hooks: allow input from stdin for commit-related hooks

Quoted 7 lines · Eric Sunshine

Rather than making such a fundamental change and having to deal with the fallout by introducing complexity to handle various special-cases which pop up now and in the future, I wonder if it makes more sense to instead just update documentation to tell hook authors to read explicitly from the console rather than expecting stdin to be available (since stdin may already be consumed for other purposes when dealing with hooks or commands which invoke the hooks).

;-)

Thanks for saying this.

#10 lore raw

Re: [PATCH v4 1/2] hooks: allow input from stdin for commit-related hooks

In reply to Eric Sunshine
Quoted 22 lines · Eric Sunshine
Quoted 8 lines · Orgad Shaneh via GitGitGadget

Let hooks receive user input if applicable.
[...]
This allows for example prompting the user to choose an issue in prepare-commit-msg, and add "Fixes #123" to the commit message.

Another possible use-case is running sanity test on pre-commit, and having a prompt like "This and that issue were found in your changes. Are you sure you want to commit? [Y/N]".

These use-cases really help readers understand the motivation for this change. Good.

Quoted 6 lines

Allow stdin only for commit-related hooks. Some of the other hooks pass their own input to the hook, so don't change them.

Note: If pre-commit reads from stdin, and git commit is executed with -F - (read message from stdin), the message is not read correctly. This is fixed in the follow-up commit.

Quoted 7 lines

Rather than making such a fundamental change and having to deal with the fallout by introducing complexity to handle various special-cases which pop up now and in the future, I wonder if it makes more sense to instead just update documentation to tell hook authors to read explicitly from the console rather than expecting stdin to be available (since stdin may already be consumed for other purposes when dealing with hooks or commands which invoke the hooks).

On the first revision I had several links in the commit message to users who solved it this way. This solution however is not optimal. I have a prepare-commit-msg hook that requires user interaction for choosing an issue. This hook must work from the terminal and also from GUI applications like IDE.

Currently the hook always pops a GUI window, but when using it from the terminal this is inconvenient (and when running over remote SSH without X forwarding it can't work), so I'd like it to be usable also from the terminal.

To achieve that, I created 2 classes - one for terminal and one for GUI, and trying to choose the correct class by checking if stdin is a tty. The condition looks like this (Ruby):
client = STDIN.tty? ? Terminal.new : GUI.new

At this point I was surprised to discover that Git closes stdin, so the condition is never satisfied, and I always end up with GUI.

As I mentioned, I need it to work also when executed from GUI applications, so just reading from the console will not work in my case. I tried other ways to detect "running from terminal" without the tty condition, but couldn't. The environment variables are identical when running in a GUI terminal and in the IDE.

Can you suggest an alternative way to determine if I can accept user input from the console or not?

- Orgad

#11 lore raw

Re: [PATCH v4 1/2] hooks: allow input from stdin for commit-related hooks

Quoted 16 lines · Orgad Shaneh
Quoted 4 lines · Eric Sunshine

[...] I wonder if it makes more sense to
instead just update documentation to tell hook authors to read explicitly from the console rather than expecting stdin to be available [...]

I have a prepare-commit-msg hook that requires user interaction for choosing an issue. This hook must work from the terminal and also from GUI applications like IDE.
[...]
As I mentioned, I need it to work also when executed from GUI applications, so just reading from the console will not work in my case. I tried other ways to detect "running from terminal" without the tty condition, but couldn't. The environment variables are identical when running in a GUI terminal and in the IDE.

Quoted 2 lines

Can you suggest an alternative way to determine if I can accept user input from the console or not?

Not at present, and I expect that the answer is that any such mechanism for determining this would be IDE-dependent. (That is, although your IDE doesn't distinguish itself in any way which your hook can detect, other IDE's might, but that doesn't help in the general case.)

What I can say, though, is that the additional information you supplied in your response should be part of the commit message to help reviewers and future readers better understand why this change is wanted. The use-cases presented in the v4 commit message, although helpful, didn't provide sufficient explanation considering that the first question which popped into this reviewer's mind was "why not have the hook read from the console explicitly?". (It is an unfortunate fact that reviewer time is a limited resource, so many reviewers on this list don't bother chasing down links like those you included in the commit message of v1 -- which would have helped justify the change -- but instead base their reviews only on the information presented in the commit message itself. In my case, I was only lightly skimming this series, thus didn't even bother chasing down those links -- but have done so now for this reply.)

I do find it quite concerning that the way this series handles the stdin conflict between the hook and -F - can break the hook silently and mysteriously. How confusing for a user to write a hook which works with git commit -m msg and git commit -F file but breaks silently with git commit -F -. What is worse is that this breakage may be outside the user's control. For instance, it is easy to imagine some IDE passing the commit message to git-commit via stdin (using -F -) rather than via a file (using -F file).

At the very least, this change deserves a documentation update, both to explain that the prepare-commit-msg hook has a valid stdin, and (importantly) that it won't be able to rely upon stdin in conjunction with -F -. (This also makes me wonder if it would be possible to signal to the hook whether or not stdin is available. Perhaps this could be done by passing an additional argument to the hook.)

Finally, I realize that you followed Junio's suggestion for organizing the series, however, it feels undesirable for patch [1/2] to leave the command in a somewhat broken state, by which I'm referring to the indeterminate outcome of the hook and -F - competing for stdin; a situation which is only resolved in [2/2]. To me, a cleaner organization would be for [1/2] to introduce the underlying mechanism and support by adding flags to run_hook_ve() (and perhaps to run_commit_hook()) but not to turn on RUN_HOOK_ALLOW_STDIN, and then have patch [2/2] actually enable RUN_HOOK_ALLOW_STDIN where appropriate _and_ deal with the -F - conflict all at the same time. (And the commit message should mention the conflict and how it is handled.)

#12 lore raw

Re: [PATCH v4 1/2] hooks: allow input from stdin for commit-related hooks

Quoted 9 lines · Eric Sunshine

I do find it quite concerning that the way this series handles the stdin conflict between the hook and -F - can break the hook silently and mysteriously. How confusing for a user to write a hook which works with git commit -m msg and git commit -F file but breaks silently with git commit -F -. What is worse is that this breakage may be outside the user's control. For instance, it is easy to imagine some IDE passing the commit message to git-commit via stdin (using -F -) rather than via a file (using -F file).

Quoted 4 lines

At the very least, this change deserves a documentation update, both to explain that the prepare-commit-msg hook has a valid stdin, and (importantly) that it won't be able to rely upon stdin in conjunction with -F -.

What I forgot to say here was that this patch series doesn't help users at all if their IDE passes the commit message to git-commit via stdin using -F -. In such a case, their hook will _never_ see a valid stdin coming from Git, no matter what their script does. So, the change made by this patch series may help some users but not others, and this is a limitation that should be stated in the commit message (and perhaps mentioned in the documentation, though that may be difficult to do in a general way).

#13 lore raw

Re: [PATCH v4 1/2] hooks: allow input from stdin for commit-related hooks

Quoted 16 lines · Eric Sunshine
Quoted 9 lines · Eric Sunshine

I do find it quite concerning that the way this series handles the stdin conflict between the hook and -F - can break the hook silently and mysteriously. How confusing for a user to write a hook which works with git commit -m msg and git commit -F file but breaks silently with git commit -F -. What is worse is that this breakage may be outside the user's control. For instance, it is easy to imagine some IDE passing the commit message to git-commit via stdin (using -F -) rather than via a file (using -F file).

Quoted 4 lines

At the very least, this change deserves a documentation update, both to explain that the prepare-commit-msg hook has a valid stdin, and (importantly) that it won't be able to rely upon stdin in conjunction with -F -.

Quoted 8 lines

What I forgot to say here was that this patch series doesn't help users at all if their IDE passes the commit message to git-commit via stdin using -F -. In such a case, their hook will _never_ see a valid stdin coming from Git, no matter what their script does. So, the change made by this patch series may help some users but not others, and this is a limitation that should be stated in the commit message (and perhaps mentioned in the documentation, though that may be difficult to do in a general way).

At least in my case, I never expect stdin to be available when running in the IDE, so my hook is expected to use GUI anyway. I only need stdin when the user is running git from the terminal. So all I need from the IDE is that it doesn't pretend to be a tty while running Git.

And regarding the hook itself - the hook author should be aware that stdin is not always a tty, and sometimes can be closed or pipe, and write the hook in a way that handles special cases.

- Orgad

#14 lore raw

Re: [PATCH v4 1/2] hooks: allow input from stdin for commit-related hooks

In reply to Eric Sunshine
Quoted 10 lines · Eric Sunshine

At the very least, this change deserves a documentation update, both to explain that the prepare-commit-msg hook has a valid stdin, and (importantly) that it won't be able to rely upon stdin in conjunction with -F -. (This also makes me wonder if it would be possible to signal to the hook whether or not stdin is available. Perhaps this could be done by passing an additional argument to the hook.)

Finally, I realize that you followed Junio's suggestion for organizing the series, however, it feels undesirable for patch [1/2] to leave the command in a somewhat broken state, ...

True. The split you suggest sounds saner, if we were to still move forward with this change. I originally threw "commit -F -" in the "don't do it if it hurts" category, but I agree with you that it is quite plausible that IDE would want to use the feature to feed the log message to the command (that way they do not need to worry about a temporary file at all), so it can become a real issue.

Thanks.

#15 lore raw

Re: [PATCH v4 1/2] hooks: allow input from stdin for commit-related hooks

In reply to Orgad Shaneh
Quoted 50 lines · Orgad Shaneh
Quoted 22 lines · Eric Sunshine
Quoted 8 lines · Orgad Shaneh via GitGitGadget

Let hooks receive user input if applicable.
[...]
This allows for example prompting the user to choose an issue in prepare-commit-msg, and add "Fixes #123" to the commit message.

Another possible use-case is running sanity test on pre-commit, and having a prompt like "This and that issue were found in your changes. Are you sure you want to commit? [Y/N]".

These use-cases really help readers understand the motivation for this change. Good.

Quoted 6 lines

Allow stdin only for commit-related hooks. Some of the other hooks pass their own input to the hook, so don't change them.

Note: If pre-commit reads from stdin, and git commit is executed with -F - (read message from stdin), the message is not read correctly. This is fixed in the follow-up commit.

Quoted 7 lines

Rather than making such a fundamental change and having to deal with the fallout by introducing complexity to handle various special-cases which pop up now and in the future, I wonder if it makes more sense to instead just update documentation to tell hook authors to read explicitly from the console rather than expecting stdin to be available (since stdin may already be consumed for other purposes when dealing with hooks or commands which invoke the hooks).

On the first revision I had several links in the commit message to users who solved it this way. This solution however is not optimal. I have a prepare-commit-msg hook that requires user interaction for choosing an issue. This hook must work from the terminal and also from GUI applications like IDE.

Currently the hook always pops a GUI window, but when using it from the terminal this is inconvenient (and when running over remote SSH without X forwarding it can't work), so I'd like it to be usable also from the terminal.

To achieve that, I created 2 classes - one for terminal and one for GUI, and trying to choose the correct class by checking if stdin is a tty. The condition looks like this (Ruby):
client = STDIN.tty? ? Terminal.new : GUI.new

At this point I was surprised to discover that Git closes stdin, so the condition is never satisfied, and I always end up with GUI.

Quoted 8 lines

As I mentioned, I need it to work also when executed from GUI applications, so just reading from the console will not work in my case. I tried other ways to detect "running from terminal" without the tty condition, but couldn't. The environment variables are identical when running in a GUI terminal and in the IDE.

Can you suggest an alternative way to determine if I can accept user input from the console or not?

Like Eric noted in his reply I can't think of a way to do that particular thing reliably either, and agree with his comments that if such a way is found / some aspect of this change is kept having this explanation in the patch/commit message is really helpful.

I think what you're trying to do here isn't a good fit for most git workflows. Instead of trying to interactively compose a commit message why not change the commit template to start with e.g.:

# You must replace XXX with an issue number here!:
Issue #XXX:

That gives the user the same thing to fill out, but in their editor instead of via some terminal/GUI prompt. They need to write the rest of the commit message anyway in the editor, so even if you could why open up two UIs?

Projects that have these conventions also typically settle on just not trying to solve this problem on the client-side, but e.g. having a pre-receive hook that does the validation, or do it via CI / before a merge to master happens etc.

#16 lore raw

Re: [PATCH v4 1/2] hooks: allow input from stdin for commit-related hooks

Quoted 11 lines · Ævar Arnfjörð Bjarmason
Quoted 2 lines · Orgad Shaneh

Can you suggest an alternative way to determine if I can accept user input from the console or not?

Like Eric noted in his reply I can't think of a way to do that particular thing reliably either, and agree with his comments that if such a way is found / some aspect of this change is kept having this explanation in the patch/commit message is really helpful.

I'll reword.

Quoted 11 lines

I think what you're trying to do here isn't a good fit for most git workflows. Instead of trying to interactively compose a commit message why not change the commit template to start with e.g.:

# You must replace XXX with an issue number here!:
Issue #XXX:

That gives the user the same thing to fill out, but in their editor instead of via some terminal/GUI prompt. They need to write the rest of the commit message anyway in the editor, so even if you could why open up two UIs?

We do have a template. The hook pops a listbox with all the open issues assigned to the user, which he/she can easily pick from, instead of searching for them in the browser and copying the issue id. This is only done if the user doesn't write an issue in the commit message.

Quoted 4 lines

Projects that have these conventions also typically settle on just not trying to solve this problem on the client-side, but e.g. having a pre-receive hook that does the validation, or do it via CI / before a merge to master happens etc.

We have validation on the server too. The hook is there for convenience.

- Orgad

#17 lore raw

PATCH v4 2/2 commit: fix stdin conflict between message and hook

In reply to Orgad Shaneh via GitGitGadget

From: Orgad Shaneh <[email protected]>

If git commit is executed with -F - (meaning read the commit message from stdin), and pre-commit hook is also reading from stdin, the message itself was consumed by the hook before reaching the point where it is read for the commit message.

Fix this by detecting this case, and passing this information to run_commit_hook.

---
 builtin/commit.c                                 | 14 +++++++++-----
 builtin/merge.c                                  | 12 ++++++++----
 commit.c                                         |  4 ++--
 commit.h                                         |  3 ++-
 sequencer.c                                      |  6 +++---
 t/t7503-pre-commit-and-pre-merge-commit-hooks.sh |  2 +-
 6 files changed, 25 insertions(+), 16 deletions(-)
builtin/commit.c +9 -5
index 505fe60956..074a57937f 100644
@@ -695,11 +695,14 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE);
 	int old_display_comment_prefix;
 	int merge_contains_scissors = 0;
+	int message_from_stdin = logfile && !strcmp(logfile, "-");
+	const unsigned hook_flags = message_from_stdin ? 0 : RUN_HOOK_ALLOW_STDIN;
 
 	/* This checks and barfs if author is badly specified */
 	determine_author_info(author_ident);
 
-	if (!no_verify && run_commit_hook(use_editor, index_file, "pre-commit", NULL))
+	if (!no_verify &&
+	    run_commit_hook(use_editor, index_file, hook_flags, "pre-commit", NULL))
 		return 0;
 
 	if (squash_message) {
@@ -724,7 +727,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	if (have_option_m && !fixup_message) {
 		strbuf_addbuf(&sb, &message);
 		hook_arg1 = "message";
-	} else if (logfile && !strcmp(logfile, "-")) {
+	} else if (message_from_stdin) {
 		if (isatty(0))
 			fprintf(stderr, _("(reading log message from standard input)\n"));
 		if (strbuf_read(&sb, 0, 0) < 0)
@@ -998,7 +1001,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 		return 0;
 	}
 
-	if (run_commit_hook(use_editor, index_file, "prepare-commit-msg",
+	if (run_commit_hook(use_editor, index_file, RUN_HOOK_ALLOW_STDIN, "prepare-commit-msg",
 			    git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL))
 		return 0;
 
@@ -1015,7 +1018,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	}
 
 	if (!no_verify &&
-	    run_commit_hook(use_editor, index_file, "commit-msg", git_path_commit_editmsg(), NULL)) {
+	    run_commit_hook(use_editor, index_file, RUN_HOOK_ALLOW_STDIN, "commit-msg",
+			    git_path_commit_editmsg(), NULL)) {
 		return 0;
 	}
 
@@ -1701,7 +1705,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 
 	repo_rerere(the_repository, 0);
 	run_auto_maintenance(quiet);
-	run_commit_hook(use_editor, get_index_file(), "post-commit", NULL);
+	run_commit_hook(use_editor, get_index_file(), RUN_HOOK_ALLOW_STDIN, "post-commit", NULL);
 	if (amend && !no_post_rewrite) {
 		commit_post_rewrite(the_repository, current_head, &oid);
 	}
builtin/merge.c +8 -4
index 4c133402a6..550b38cd20 100644
@@ -822,8 +822,11 @@ static void prepare_to_commit(struct commit_list *remoteheads)
 	struct strbuf msg = STRBUF_INIT;
 	const char *index_file = get_index_file();
 
-	if (!no_verify && run_commit_hook(0 < option_edit, index_file, "pre-merge-commit", NULL))
+	if (!no_verify &&
+	    run_commit_hook(0 < option_edit, index_file, RUN_HOOK_ALLOW_STDIN,
+			    "pre-merge-commit", NULL)) {
 		abort_commit(remoteheads, NULL);
+	}
 	/*
 	 * Re-read the index as pre-merge-commit hook could have updated it,
 	 * and write it out as a tree.  We must do this before we invoke
@@ -850,8 +853,9 @@ static void prepare_to_commit(struct commit_list *remoteheads)
 		append_signoff(&msg, ignore_non_trailer(msg.buf, msg.len), 0);
 	write_merge_heads(remoteheads);
 	write_file_buf(git_path_merge_msg(the_repository), msg.buf, msg.len);
-	if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg",
-			    git_path_merge_msg(the_repository), "merge", NULL))
+	if (run_commit_hook(0 < option_edit, get_index_file(), RUN_HOOK_ALLOW_STDIN,
+			    "prepare-commit-msg", git_path_merge_msg(the_repository),
+			    "merge", NULL))
 		abort_commit(remoteheads, NULL);
 	if (0 < option_edit) {
 		if (launch_editor(git_path_merge_msg(the_repository), NULL, NULL))
@@ -859,7 +863,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
 	}
 
 	if (!no_verify && run_commit_hook(0 < option_edit, get_index_file(),
-					  "commit-msg",
+					  RUN_HOOK_ALLOW_STDIN, "commit-msg",
 					  git_path_merge_msg(the_repository), NULL))
 		abort_commit(remoteheads, NULL);
 
commit.c +2 -2
index 775019ec9d..3f5a50164e 100644
@@ -1631,7 +1631,7 @@ size_t ignore_non_trailer(const char *buf, size_t len)
 }
 
 int run_commit_hook(int editor_is_used, const char *index_file,
-		    const char *name, ...)
+		    unsigned flags, const char *name, ...)
 {
 	struct strvec hook_env = STRVEC_INIT;
 	va_list args;
@@ -1646,7 +1646,7 @@ int run_commit_hook(int editor_is_used, const char *index_file,
 		strvec_push(&hook_env, "GIT_EDITOR=:");
 
 	va_start(args, name);
-	ret = run_hook_ve(hook_env.v, RUN_HOOK_ALLOW_STDIN, name, args);
+	ret = run_hook_ve(hook_env.v, flags, name, args);
 	va_end(args);
 	strvec_clear(&hook_env);
 
commit.h +2 -1
index 5467786c7b..72215d57fb 100644
@@ -352,6 +352,7 @@ int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
 int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void *unused);
 
 LAST_ARG_MUST_BE_NULL
-int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...);
+int run_commit_hook(int editor_is_used, const char *index_file, unsigned flags,
+		    const char *name, ...);
 
 #endif /* COMMIT_H */
sequencer.c +3 -3
index 684ea9d5ce..505101c29c 100644
@@ -1171,8 +1171,8 @@ static int run_prepare_commit_msg_hook(struct repository *r,
 	} else {
 		arg1 = "message";
 	}
-	if (run_commit_hook(0, r->index_file, "prepare-commit-msg", name,
-			    arg1, arg2, NULL))
+	if (run_commit_hook(0, r->index_file, RUN_HOOK_ALLOW_STDIN,
+			    "prepare-commit-msg", name, arg1, arg2, NULL))
 		ret = error(_("'prepare-commit-msg' hook failed"));
 
 	return ret;
@@ -1496,7 +1496,7 @@ static int try_to_commit(struct repository *r,
 		goto out;
 	}
 
-	run_commit_hook(0, r->index_file, "post-commit", NULL);
+	run_commit_hook(0, r->index_file, RUN_HOOK_ALLOW_STDIN, "post-commit", NULL);
 	if (flags & AMEND_MSG)
 		commit_post_rewrite(r, current_head, oid);
 
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +1 -1
index 7bfb7435c6..a243b7efa1 100755
@@ -292,7 +292,7 @@ test_expect_success 'with user input' '
 	test_cmp user_input hook_input
 '
 
-test_expect_failure 'with user input combined with -F -' '
+test_expect_success 'with user input combined with -F -' '
 	test_when_finished "rm -f \"$PRECOMMIT\" user_input hook_input" &&
 	cp "$HOOKDIR/user-input.sample" "$PRECOMMIT" &&
 	echo "user input" >user_input &&
Signature · 1 line
gitgitgadget
#18 lore raw

v5 cover hooks: allow input from stdin for commit-related hooks

In reply to Orgad Shaneh via GitGitGadget

Let hooks receive user input if applicable.

Closing stdin originates in f5bbc3225 (Port git commit to C, 2007). Looks like the original shell implementation did have stdin open. Not clear why the author chose to close it on the C port (maybe copy&paste).

The only hook that passes internal information to the hook via stdin is pre-push, which has its own logic.

Some references of users requesting this feature. Some of them use acrobatics to gain access to stdin: [1]
https://stackoverflow.com/q/1067874/764870 [2]
https://stackoverflow.com/q/47477766/764870 [3]
https://stackoverflow.com/q/3417896/764870 [4]
https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/3165 [5]
https://github.com/typicode/husky/issues/442

Orgad Shaneh (2):
hooks: lay foundations for passing stdin to hooks
hooks: allow input from stdin for commit-related hooks

 builtin/commit.c                              | 14 ++++--
 builtin/merge.c                               | 12 +++--
 commit.c                                      |  4 +-
 commit.h                                      |  3 +-
 run-command.c                                 |  6 +--
 run-command.h                                 | 17 +++++--
 sequencer.c                                   |  6 +--
 ...3-pre-commit-and-pre-merge-commit-hooks.sh | 46 ++++++++++++++++++-
 t/t7504-commit-msg-hook.sh                    | 15 ++++++
 t/t7505-prepare-commit-msg-hook.sh            | 14 ++++++
 10 files changed, 113 insertions(+), 24 deletions(-)

base-commit: 3cf59784d42c4152a0b3de7bb7a75d0071e5f878
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-790%2Forgads%2Fhooks-stdin-v5
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-790/orgads/hooks-stdin-v5
Pull-Request: https://github.com/gitgitgadget/git/pull/790

Range-diff vs v4:

2: e048a9db62c ! 1: 27e66d43c83 commit: fix stdin conflict between message and hook
@@ Metadata
Author: Orgad Shaneh <[email protected]>

## Commit message ##
- commit: fix stdin conflict between message and hook
+ hooks: lay foundations for passing stdin to hooks

- If git commit is executed with -F - (meaning read the commit message
- from stdin), and pre-commit hook is also reading from stdin, the
- message itself was consumed by the hook before reaching the point
- where it is read for the commit message.
-
- Fix this by detecting this case, and passing this information to
- run_commit_hook.
+ Used in the follow-up commit for enabling stdin for commit-related
+ hooks.

## builtin/commit.c ##
@@ builtin/commit.c: static int prepare_to_commit(const char *index_file, const char *prefix,
- int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE);
- int old_display_comment_prefix;
- int merge_contains_scissors = 0;
-+ int message_from_stdin = logfile && !strcmp(logfile, "-");
-+ const unsigned hook_flags = message_from_stdin ? 0 : RUN_HOOK_ALLOW_STDIN;
-
/* This checks and barfs if author is badly specified */ determine_author_info(author_ident);

- if (!no_verify && run_commit_hook(use_editor, index_file, "pre-commit", NULL))
-+ if (!no_verify &&
-+ run_commit_hook(use_editor, index_file, hook_flags, "pre-commit", NULL))
++ if (!no_verify && run_commit_hook(use_editor, index_file, 0, "pre-commit", NULL))
return 0;

if (squash_message) {
-@@ builtin/commit.c: static int prepare_to_commit(const char *index_file, const char *prefix,
- if (have_option_m && !fixup_message) {
- strbuf_addbuf(&sb, &message);
- hook_arg1 = "message";
-- } else if (logfile && !strcmp(logfile, "-")) {
-+ } else if (message_from_stdin) {
- if (isatty(0))
- fprintf(stderr, _("(reading log message from standard input)\n"));
- if (strbuf_read(&sb, 0, 0) < 0)
@@ builtin/commit.c: static int prepare_to_commit(const char *index_file, const char *prefix,
return 0;
}

- if (run_commit_hook(use_editor, index_file, "prepare-commit-msg",
-+ if (run_commit_hook(use_editor, index_file, RUN_HOOK_ALLOW_STDIN, "prepare-commit-msg",
++ if (run_commit_hook(use_editor, index_file, 0, "prepare-commit-msg",
git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL))
return 0;

@@ builtin/commit.c: static int prepare_to_commit(const char *index_file, const cha

if (!no_verify &&
- run_commit_hook(use_editor, index_file, "commit-msg", git_path_commit_editmsg(), NULL)) {
-+ run_commit_hook(use_editor, index_file, RUN_HOOK_ALLOW_STDIN, "commit-msg",
-+ git_path_commit_editmsg(), NULL)) {
++ run_commit_hook(use_editor, index_file, 0, "commit-msg", git_path_commit_editmsg(), NULL)) {
return 0;
}

@@ builtin/commit.c: int cmd_commit(int argc, const char **argv, const char *prefix
repo_rerere(the_repository, 0);
run_auto_maintenance(quiet);
- run_commit_hook(use_editor, get_index_file(), "post-commit", NULL);
-+ run_commit_hook(use_editor, get_index_file(), RUN_HOOK_ALLOW_STDIN, "post-commit", NULL);
++ run_commit_hook(use_editor, get_index_file(), 0, "post-commit", NULL);
if (amend && !no_post_rewrite) {
commit_post_rewrite(the_repository, current_head, &oid);
}
@@ builtin/merge.c: static void prepare_to_commit(struct commit_list *remoteheads)
const char *index_file = get_index_file();

- if (!no_verify && run_commit_hook(0 < option_edit, index_file, "pre-merge-commit", NULL))
-+ if (!no_verify &&
-+ run_commit_hook(0 < option_edit, index_file, RUN_HOOK_ALLOW_STDIN,
-+ "pre-merge-commit", NULL)) {
++ if (!no_verify && run_commit_hook(0 < option_edit, index_file, 0, "pre-merge-commit", NULL))
abort_commit(remoteheads, NULL);
-+ }
/*
* Re-read the index as pre-merge-commit hook could have updated it,
- * and write it out as a tree. We must do this before we invoke
@@ builtin/merge.c: static void prepare_to_commit(struct commit_list *remoteheads)
append_signoff(&msg, ignore_non_trailer(msg.buf, msg.len), 0);
write_merge_heads(remoteheads); write_file_buf(git_path_merge_msg(the_repository), msg.buf, msg.len);
- if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg",
-- git_path_merge_msg(the_repository), "merge", NULL))
-+ if (run_commit_hook(0 < option_edit, get_index_file(), RUN_HOOK_ALLOW_STDIN,
-+ "prepare-commit-msg", git_path_merge_msg(the_repository),
-+ "merge", NULL))
++ if (run_commit_hook(0 < option_edit, get_index_file(), 0, "prepare-commit-msg",
+ git_path_merge_msg(the_repository), "merge", NULL))
abort_commit(remoteheads, NULL);
if (0 < option_edit) {
- if (launch_editor(git_path_merge_msg(the_repository), NULL, NULL))
@@ builtin/merge.c: static void prepare_to_commit(struct commit_list *remoteheads)
}

if (!no_verify && run_commit_hook(0 < option_edit, get_index_file(),
- "commit-msg",
-+ RUN_HOOK_ALLOW_STDIN, "commit-msg",
++ 0, "commit-msg",
git_path_merge_msg(the_repository), NULL))
abort_commit(remoteheads, NULL);

@@ commit.c: int run_commit_hook(int editor_is_used, const char *index_file,
strvec_push(&hook_env, "GIT_EDITOR=:");

va_start(args, name);
-- ret = run_hook_ve(hook_env.v, RUN_HOOK_ALLOW_STDIN, name, args);
+- ret = run_hook_ve(hook_env.v, name, args);
+ ret = run_hook_ve(hook_env.v, flags, name, args);
va_end(args);
strvec_clear(&hook_env);
@@ commit.h: int compare_commits_by_commit_date(const void *a_, const void *b_, voi

#endif /* COMMIT_H */

+ ## run-command.c ##
+@@ run-command.c: const char *find_hook(const char *name)
+ return path.buf;
+ }
+
+-int run_hook_ve(const char *const *env, const char *name, va_list args) ++int run_hook_ve(const char *const *env, unsigned flags, const char *name, va_list args)
+ {
+ struct child_process hook = CHILD_PROCESS_INIT;
+ const char *p;
+@@ run-command.c: int run_hook_ve(const char *const *env, const char *name, va_list args)
+ while ((p = va_arg(args, const char *)))
+ strvec_push(&hook.args, p);
+ hook.env = env;
+- hook.no_stdin = 1;
++ hook.no_stdin = !(flags & RUN_HOOK_ALLOW_STDIN);
+ hook.stdout_to_stderr = 1;
+ hook.trace2_hook_name = name;
+
+@@ run-command.c: int run_hook_le(const char *const *env, const char *name, ...)
+ int ret;
+
+ va_start(args, name);
+- ret = run_hook_ve(env, name, args);
++ ret = run_hook_ve(env, 0, name, args);
+ va_end(args);
+
+ return ret;
+
+ ## run-command.h ##
+@@ run-command.h: int run_command(struct child_process *);
+ */
+ const char *find_hook(const char *name);
+
++#define RUN_HOOK_ALLOW_STDIN 1
++
+ /**
+ * Run a hook.
+- * The first argument is a pathname to an index file, or NULL
+- * if the hook uses the default index file or no index is needed.
+- * The second argument is the name of the hook.
++ * The env argument is an array of environment variables, or NULL
++ * if the hook uses the default environment and doesn't require
++ * additional variables.
++ * The flags argument is an OR'ed collection of feature bits like
++ * RUN_HOOK_ALLOW_STDIN defined above, which enables
++ * stdin for the child process (the default is no_stdin).
++ * The name argument is the name of the hook.
+ * The further arguments correspond to the hook arguments.
+ * The last argument has to be NULL to terminate the arguments list.
+ * If the hook does not exist or is not executable, the return
+ * value will be zero.
+ * If it is executable, the hook will be executed and the exit
+ * status of the hook is returned.
+- * On execution, .stdout_to_stderr and .no_stdin will be set.
++ * On execution, .stdout_to_stderr will be set, and .no_stdin will be
++ * set unless RUN_HOOK_ALLOW_STDIN flag is requested.
+ */
+ LAST_ARG_MUST_BE_NULL
+ int run_hook_le(const char *const *env, const char *name, ...);
+-int run_hook_ve(const char *const *env, const char *name, va_list args); ++int run_hook_ve(const char *const *env, unsigned flags, const char *name, va_list args); +
+ /*
+ * Trigger an auto-gc
+
## sequencer.c ##
@@ sequencer.c: static int run_prepare_commit_msg_hook(struct repository *r,
} else {
arg1 = "message";
}
- if (run_commit_hook(0, r->index_file, "prepare-commit-msg", name,
-- arg1, arg2, NULL))
-+ if (run_commit_hook(0, r->index_file, RUN_HOOK_ALLOW_STDIN,
-+ "prepare-commit-msg", name, arg1, arg2, NULL))
++ if (run_commit_hook(0, r->index_file, 0, "prepare-commit-msg", name,
+ arg1, arg2, NULL))
ret = error(_("'prepare-commit-msg' hook failed"));

- return ret;
@@ sequencer.c: static int try_to_commit(struct repository *r,
goto out;
}

- run_commit_hook(0, r->index_file, "post-commit", NULL);
-+ run_commit_hook(0, r->index_file, RUN_HOOK_ALLOW_STDIN, "post-commit", NULL);
++ run_commit_hook(0, r->index_file, 0, "post-commit", NULL);
if (flags & AMEND_MSG)
commit_post_rewrite(r, current_head, oid);

-
- ## t/t7503-pre-commit-and-pre-merge-commit-hooks.sh ##
-@@ t/t7503-pre-commit-and-pre-merge-commit-hooks.sh: test_expect_success 'with user input' '
- test_cmp user_input hook_input
- '
-
--test_expect_failure 'with user input combined with -F -' ' -+test_expect_success 'with user input combined with -F -' '
- test_when_finished "rm -f \"$PRECOMMIT\" user_input hook_input" &&
- cp "$HOOKDIR/user-input.sample" "$PRECOMMIT" &&
- echo "user input" >user_input &&
1: 3bd6024a236 ! 2: 25db4da3cd5 hooks: allow input from stdin for commit-related hooks
@@ Commit message

Let hooks receive user input if applicable.

- Closing stdin originates in f5bbc3225 (Port git commit to C,
- 2007). Looks like the original shell implementation did have
- stdin open.
+ Closing stdin originates in f5bbc3225 (Port git commit to C, 2007).
+ Looks like the original shell implementation did have stdin open.

- This allows for example prompting the user to choose an issue
- in prepare-commit-msg, and add "Fixes #123" to the commit message.
+ Due to stdin being closed, hooks that require user input have to either
+ read the input directly from the console (which can't work when running
+ from GUI applications), or popup a GUI dialog (which is inconvenient
+ when running from the terminal).

- Another possible use-case is running sanity test on pre-commit,
- and having a prompt like "This and that issue were found in your
- changes. Are you sure you want to commit? [Y/N]".
+ This allows for example prompting the user to choose an issue in
+ prepare-commit-msg, and add "Fixes #123" to the commit message.

- Allow stdin only for commit-related hooks. Some of the other
- hooks pass their own input to the hook, so don't change them.
+ Another possible use-case is running sanity test on pre-commit, and
+ having a prompt like "This and that issue were found in your changes.
+ Are you sure you want to commit? [Y/N]".

- Note: If pre-commit reads from stdin, and git commit is executed
- with -F - (read message from stdin), the message is not read
- correctly. This is fixed in the follow-up commit.
+ It's important to note that the hook author should be aware that stdin
+ is not always applicable. For example, when running from IDE. This can
+ be checked by isatty on stdin. The hooks should handle cases of closed
+ input, and possibly fall-back to GUI input, or have sane defaults with
+ a message to the user on this case.
+
+ Allow stdin only for commit-related hooks. Some of the other hooks pass
+ their own input to the hook, so don't change them.
+
+ Note: If pre-commit reads from stdin, and git commit is executed with
+ -F - (read message from stdin), stdin cannot be passed to the hook,
+ since it will consume it before reaching the point where it is read for
+ the commit message.

- ## commit.c ##
-@@ commit.c: int run_commit_hook(int editor_is_used, const char *index_file,
- strvec_push(&hook_env, "GIT_EDITOR=:");
-
- va_start(args, name);
-- ret = run_hook_ve(hook_env.v, name, args);
-+ ret = run_hook_ve(hook_env.v, RUN_HOOK_ALLOW_STDIN, name, args);
- va_end(args);
- strvec_clear(&hook_env);
-
-
- ## run-command.c ##
-@@ run-command.c: const char *find_hook(const char *name)
- return path.buf;
- }
-
--int run_hook_ve(const char *const *env, const char *name, va_list args) -+int run_hook_ve(const char *const *env, unsigned flags, const char *name, va_list args)
- {
- struct child_process hook = CHILD_PROCESS_INIT;
- const char *p;
-@@ run-command.c: int run_hook_ve(const char *const *env, const char *name, va_list args)
- while ((p = va_arg(args, const char *)))
- strvec_push(&hook.args, p);
- hook.env = env;
-- hook.no_stdin = 1;
-+ hook.no_stdin = !(flags & RUN_HOOK_ALLOW_STDIN);
- hook.stdout_to_stderr = 1;
- hook.trace2_hook_name = name;
-
-@@ run-command.c: int run_hook_le(const char *const *env, const char *name, ...)
- int ret;
-
- va_start(args, name);
-- ret = run_hook_ve(env, name, args);
-+ ret = run_hook_ve(env, 0, name, args);
- va_end(args);
+ ## builtin/commit.c ##
+@@ builtin/commit.c: static int prepare_to_commit(const char *index_file, const char *prefix,
+ int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE);
+ int old_display_comment_prefix;
+ int merge_contains_scissors = 0;
++ int message_from_stdin = logfile && !strcmp(logfile, "-");
++ const unsigned hook_flags = message_from_stdin ? 0 : RUN_HOOK_ALLOW_STDIN;

- return ret;
+ /* This checks and barfs if author is badly specified */
+ determine_author_info(author_ident);
+
+- if (!no_verify && run_commit_hook(use_editor, index_file, 0, "pre-commit", NULL))
++ if (!no_verify &&
++ run_commit_hook(use_editor, index_file, hook_flags, "pre-commit", NULL))
+ return 0;
+
+ if (squash_message) {
+@@ builtin/commit.c: static int prepare_to_commit(const char *index_file, const char *prefix,
+ if (have_option_m && !fixup_message) {
+ strbuf_addbuf(&sb, &message);
+ hook_arg1 = "message";
+- } else if (logfile && !strcmp(logfile, "-")) {
++ } else if (message_from_stdin) {
+ if (isatty(0))
+ fprintf(stderr, _("(reading log message from standard input)\n"));
+ if (strbuf_read(&sb, 0, 0) < 0)
+@@ builtin/commit.c: static int prepare_to_commit(const char *index_file, const char *prefix,
+ return 0;
+ }
+
+- if (run_commit_hook(use_editor, index_file, 0, "prepare-commit-msg",
++ if (run_commit_hook(use_editor, index_file, RUN_HOOK_ALLOW_STDIN, "prepare-commit-msg",
+ git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL))
+ return 0;
+
+@@ builtin/commit.c: static int prepare_to_commit(const char *index_file, const char *prefix,
+ }
+
+ if (!no_verify &&
+- run_commit_hook(use_editor, index_file, 0, "commit-msg", git_path_commit_editmsg(), NULL)) {
++ run_commit_hook(use_editor, index_file, RUN_HOOK_ALLOW_STDIN, "commit-msg",
++ git_path_commit_editmsg(), NULL)) {
+ return 0;
+ }
+
+@@ builtin/commit.c: int cmd_commit(int argc, const char **argv, const char *prefix) +
+ repo_rerere(the_repository, 0);
+ run_auto_maintenance(quiet);
+- run_commit_hook(use_editor, get_index_file(), 0, "post-commit", NULL);
++ run_commit_hook(use_editor, get_index_file(), RUN_HOOK_ALLOW_STDIN, "post-commit", NULL);
+ if (amend && !no_post_rewrite) {
+ commit_post_rewrite(the_repository, current_head, &oid);
+ }

- ## run-command.h ##
-@@ run-command.h: int run_command(struct child_process *);
- */
- const char *find_hook(const char *name);
+ ## builtin/merge.c ##
+@@ builtin/merge.c: static void prepare_to_commit(struct commit_list *remoteheads)
+ struct strbuf msg = STRBUF_INIT;
+ const char *index_file = get_index_file();
+
+- if (!no_verify && run_commit_hook(0 < option_edit, index_file, 0, "pre-merge-commit", NULL))
++ if (!no_verify &&
++ run_commit_hook(0 < option_edit, index_file, RUN_HOOK_ALLOW_STDIN,
++ "pre-merge-commit", NULL)) {
+ abort_commit(remoteheads, NULL);
++ }
+ /*
+ * Re-read the index as pre-merge-commit hook could have updated it,
+ * and write it out as a tree. We must do this before we invoke
+@@ builtin/merge.c: static void prepare_to_commit(struct commit_list *remoteheads)
+ append_signoff(&msg, ignore_non_trailer(msg.buf, msg.len), 0);
+ write_merge_heads(remoteheads);
+ write_file_buf(git_path_merge_msg(the_repository), msg.buf, msg.len);
+- if (run_commit_hook(0 < option_edit, get_index_file(), 0, "prepare-commit-msg",
+- git_path_merge_msg(the_repository), "merge", NULL))
++ if (run_commit_hook(0 < option_edit, get_index_file(), RUN_HOOK_ALLOW_STDIN,
++ "prepare-commit-msg", git_path_merge_msg(the_repository),
++ "merge", NULL))
+ abort_commit(remoteheads, NULL);
+ if (0 < option_edit) {
+ if (launch_editor(git_path_merge_msg(the_repository), NULL, NULL))
+@@ builtin/merge.c: static void prepare_to_commit(struct commit_list *remoteheads)
+ }
+
+ if (!no_verify && run_commit_hook(0 < option_edit, get_index_file(),
+- 0, "commit-msg",
++ RUN_HOOK_ALLOW_STDIN, "commit-msg",
+ git_path_merge_msg(the_repository), NULL))
+ abort_commit(remoteheads, NULL);
+
+
+ ## sequencer.c ##
+@@ sequencer.c: static int run_prepare_commit_msg_hook(struct repository *r,
+ } else {
+ arg1 = "message";
+ }
+- if (run_commit_hook(0, r->index_file, 0, "prepare-commit-msg", name,
+- arg1, arg2, NULL))
++ if (run_commit_hook(0, r->index_file, RUN_HOOK_ALLOW_STDIN,
++ "prepare-commit-msg", name, arg1, arg2, NULL))
+ ret = error(_("'prepare-commit-msg' hook failed"));
+
+ return ret;
+@@ sequencer.c: static int try_to_commit(struct repository *r,
+ goto out;
+ }
+
+- run_commit_hook(0, r->index_file, 0, "post-commit", NULL);
++ run_commit_hook(0, r->index_file, RUN_HOOK_ALLOW_STDIN, "post-commit", NULL);
+ if (flags & AMEND_MSG)
+ commit_post_rewrite(r, current_head, oid);

-+#define RUN_HOOK_ALLOW_STDIN 1
-+
- /**
- * Run a hook.
-- * The first argument is a pathname to an index file, or NULL
-- * if the hook uses the default index file or no index is needed.
-- * The second argument is the name of the hook.
-+ * The env argument is an array of environment variables, or NULL
-+ * if the hook uses the default environment and doesn't require
-+ * additional variables.
-+ * The flags argument is an OR'ed collection of feature bits like
-+ * RUN_HOOK_ALLOW_STDIN defined above, which enables
-+ * stdin for the child process (the default is no_stdin).
-+ * The name argument is the name of the hook.
- * The further arguments correspond to the hook arguments.
- * The last argument has to be NULL to terminate the arguments list.
- * If the hook does not exist or is not executable, the return
- * value will be zero.
- * If it is executable, the hook will be executed and the exit
- * status of the hook is returned.
-- * On execution, .stdout_to_stderr and .no_stdin will be set.
-+ * On execution, .stdout_to_stderr will be set, and .no_stdin will be
-+ * set unless RUN_HOOK_ALLOW_STDIN flag is requested.
- */
- LAST_ARG_MUST_BE_NULL
- int run_hook_le(const char *const *env, const char *name, ...);
--int run_hook_ve(const char *const *env, const char *name, va_list args); -+int run_hook_ve(const char *const *env, unsigned flags, const char *name, va_list args); -
- /*
- * Trigger an auto-gc

## t/t7503-pre-commit-and-pre-merge-commit-hooks.sh ##
@@ t/t7503-pre-commit-and-pre-merge-commit-hooks.sh: test_description='pre-commit and pre-merge-commit hooks'
@@ t/t7503-pre-commit-and-pre-merge-commit-hooks.sh: test_expect_success 'check the
+ test_cmp user_input hook_input
+'
+
-+test_expect_failure 'with user input combined with -F -' '
++test_expect_success 'with user input combined with -F -' '
+ test_when_finished "rm -f \"$PRECOMMIT\" user_input hook_input" &&
+ cp "$HOOKDIR/user-input.sample" "$PRECOMMIT" &&
+ echo "user input" >user_input &&

Signature · 1 line
gitgitgadget
#19 lore raw

PATCH v5 1/2 hooks: lay foundations for passing stdin to hooks

From: Orgad Shaneh <[email protected]>

Used in the follow-up commit for enabling stdin for commit-related hooks.

---
 builtin/commit.c |  8 ++++----
 builtin/merge.c  |  6 +++---
 commit.c         |  4 ++--
 commit.h         |  3 ++-
 run-command.c    |  6 +++---
 run-command.h    | 17 ++++++++++++-----
 sequencer.c      |  4 ++--
 7 files changed, 28 insertions(+), 20 deletions(-)
builtin/commit.c +4 -4
index 505fe60956d..70a7842e224 100644
@@ -699,7 +699,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	/* This checks and barfs if author is badly specified */
 	determine_author_info(author_ident);
 
-	if (!no_verify && run_commit_hook(use_editor, index_file, "pre-commit", NULL))
+	if (!no_verify && run_commit_hook(use_editor, index_file, 0, "pre-commit", NULL))
 		return 0;
 
 	if (squash_message) {
@@ -998,7 +998,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 		return 0;
 	}
 
-	if (run_commit_hook(use_editor, index_file, "prepare-commit-msg",
+	if (run_commit_hook(use_editor, index_file, 0, "prepare-commit-msg",
 			    git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL))
 		return 0;
 
@@ -1015,7 +1015,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	}
 
 	if (!no_verify &&
-	    run_commit_hook(use_editor, index_file, "commit-msg", git_path_commit_editmsg(), NULL)) {
+	    run_commit_hook(use_editor, index_file, 0, "commit-msg", git_path_commit_editmsg(), NULL)) {
 		return 0;
 	}
 
@@ -1701,7 +1701,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 
 	repo_rerere(the_repository, 0);
 	run_auto_maintenance(quiet);
-	run_commit_hook(use_editor, get_index_file(), "post-commit", NULL);
+	run_commit_hook(use_editor, get_index_file(), 0, "post-commit", NULL);
 	if (amend && !no_post_rewrite) {
 		commit_post_rewrite(the_repository, current_head, &oid);
 	}
builtin/merge.c +3 -3
index 1cff7307153..26e6ae15993 100644
@@ -836,7 +836,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
 	struct strbuf msg = STRBUF_INIT;
 	const char *index_file = get_index_file();
 
-	if (!no_verify && run_commit_hook(0 < option_edit, index_file, "pre-merge-commit", NULL))
+	if (!no_verify && run_commit_hook(0 < option_edit, index_file, 0, "pre-merge-commit", NULL))
 		abort_commit(remoteheads, NULL);
 	/*
 	 * Re-read the index as pre-merge-commit hook could have updated it,
@@ -864,7 +864,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
 		append_signoff(&msg, ignore_non_trailer(msg.buf, msg.len), 0);
 	write_merge_heads(remoteheads);
 	write_file_buf(git_path_merge_msg(the_repository), msg.buf, msg.len);
-	if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg",
+	if (run_commit_hook(0 < option_edit, get_index_file(), 0, "prepare-commit-msg",
 			    git_path_merge_msg(the_repository), "merge", NULL))
 		abort_commit(remoteheads, NULL);
 	if (0 < option_edit) {
@@ -873,7 +873,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
 	}
 
 	if (!no_verify && run_commit_hook(0 < option_edit, get_index_file(),
-					  "commit-msg",
+					  0, "commit-msg",
 					  git_path_merge_msg(the_repository), NULL))
 		abort_commit(remoteheads, NULL);
 
commit.c +2 -2
index fe1fa3dc41f..3f5a50164eb 100644
@@ -1631,7 +1631,7 @@ size_t ignore_non_trailer(const char *buf, size_t len)
 }
 
 int run_commit_hook(int editor_is_used, const char *index_file,
-		    const char *name, ...)
+		    unsigned flags, const char *name, ...)
 {
 	struct strvec hook_env = STRVEC_INIT;
 	va_list args;
@@ -1646,7 +1646,7 @@ int run_commit_hook(int editor_is_used, const char *index_file,
 		strvec_push(&hook_env, "GIT_EDITOR=:");
 
 	va_start(args, name);
-	ret = run_hook_ve(hook_env.v, name, args);
+	ret = run_hook_ve(hook_env.v, flags, name, args);
 	va_end(args);
 	strvec_clear(&hook_env);
 
commit.h +2 -1
index 5467786c7be..72215d57fb2 100644
@@ -352,6 +352,7 @@ int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
 int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void *unused);
 
 LAST_ARG_MUST_BE_NULL
-int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...);
+int run_commit_hook(int editor_is_used, const char *index_file, unsigned flags,
+		    const char *name, ...);
 
 #endif /* COMMIT_H */
run-command.c +3 -3
index ea4d0fb4b15..30d69562f43 100644
@@ -1344,7 +1344,7 @@ const char *find_hook(const char *name)
 	return path.buf;
 }
 
-int run_hook_ve(const char *const *env, const char *name, va_list args)
+int run_hook_ve(const char *const *env, unsigned flags, const char *name, va_list args)
 {
 	struct child_process hook = CHILD_PROCESS_INIT;
 	const char *p;
@@ -1357,7 +1357,7 @@ int run_hook_ve(const char *const *env, const char *name, va_list args)
 	while ((p = va_arg(args, const char *)))
 		strvec_push(&hook.args, p);
 	hook.env = env;
-	hook.no_stdin = 1;
+	hook.no_stdin = !(flags & RUN_HOOK_ALLOW_STDIN);
 	hook.stdout_to_stderr = 1;
 	hook.trace2_hook_name = name;
 
@@ -1370,7 +1370,7 @@ int run_hook_le(const char *const *env, const char *name, ...)
 	int ret;
 
 	va_start(args, name);
-	ret = run_hook_ve(env, name, args);
+	ret = run_hook_ve(env, 0, name, args);
 	va_end(args);
 
 	return ret;
run-command.h +12 -5
index 6472b38bde4..e613e5e3f92 100644
@@ -201,22 +201,29 @@ int run_command(struct child_process *);
  */
 const char *find_hook(const char *name);
 
+#define RUN_HOOK_ALLOW_STDIN 1
+
 /**
  * Run a hook.
- * The first argument is a pathname to an index file, or NULL
- * if the hook uses the default index file or no index is needed.
- * The second argument is the name of the hook.
+ * The env argument is an array of environment variables, or NULL
+ * if the hook uses the default environment and doesn't require
+ * additional variables.
+ * The flags argument is an OR'ed collection of feature bits like
+ * RUN_HOOK_ALLOW_STDIN defined above, which enables
+ * stdin for the child process (the default is no_stdin).
+ * The name argument is the name of the hook.
  * The further arguments correspond to the hook arguments.
  * The last argument has to be NULL to terminate the arguments list.
  * If the hook does not exist or is not executable, the return
  * value will be zero.
  * If it is executable, the hook will be executed and the exit
  * status of the hook is returned.
- * On execution, .stdout_to_stderr and .no_stdin will be set.
+ * On execution, .stdout_to_stderr will be set, and .no_stdin will be
+ * set unless RUN_HOOK_ALLOW_STDIN flag is requested.
  */
 LAST_ARG_MUST_BE_NULL
 int run_hook_le(const char *const *env, const char *name, ...);
-int run_hook_ve(const char *const *env, const char *name, va_list args);
+int run_hook_ve(const char *const *env, unsigned flags, const char *name, va_list args);
 
 /*
  * Trigger an auto-gc
sequencer.c +2 -2
index 8909a467700..5f48d32e2fa 100644
@@ -1203,7 +1203,7 @@ static int run_prepare_commit_msg_hook(struct repository *r,
 	} else {
 		arg1 = "message";
 	}
-	if (run_commit_hook(0, r->index_file, "prepare-commit-msg", name,
+	if (run_commit_hook(0, r->index_file, 0, "prepare-commit-msg", name,
 			    arg1, arg2, NULL))
 		ret = error(_("'prepare-commit-msg' hook failed"));
 
@@ -1528,7 +1528,7 @@ static int try_to_commit(struct repository *r,
 		goto out;
 	}
 
-	run_commit_hook(0, r->index_file, "post-commit", NULL);
+	run_commit_hook(0, r->index_file, 0, "post-commit", NULL);
 	if (flags & AMEND_MSG)
 		commit_post_rewrite(r, current_head, oid);
 
Signature · 1 line
gitgitgadget
#20 lore raw

PATCH v5 2/2 hooks: allow input from stdin for commit-related hooks

In reply to Orgad Shaneh via GitGitGadget

From: Orgad Shaneh <[email protected]>

Let hooks receive user input if applicable.

Closing stdin originates in f5bbc3225 (Port git commit to C, 2007). Looks like the original shell implementation did have stdin open.

Due to stdin being closed, hooks that require user input have to either read the input directly from the console (which can't work when running from GUI applications), or popup a GUI dialog (which is inconvenient when running from the terminal).

This allows for example prompting the user to choose an issue in prepare-commit-msg, and add "Fixes #123" to the commit message.

Another possible use-case is running sanity test on pre-commit, and having a prompt like "This and that issue were found in your changes. Are you sure you want to commit? [Y/N]".

It's important to note that the hook author should be aware that stdin is not always applicable. For example, when running from IDE. This can be checked by isatty on stdin. The hooks should handle cases of closed input, and possibly fall-back to GUI input, or have sane defaults with a message to the user on this case.

Allow stdin only for commit-related hooks. Some of the other hooks pass their own input to the hook, so don't change them.

Note: If pre-commit reads from stdin, and git commit is executed with -F - (read message from stdin), stdin cannot be passed to the hook, since it will consume it before reaching the point where it is read for the commit message.

---
 builtin/commit.c                              | 14 ++++--
 builtin/merge.c                               | 12 +++--
 sequencer.c                                   |  6 +--
 ...3-pre-commit-and-pre-merge-commit-hooks.sh | 46 ++++++++++++++++++-
 t/t7504-commit-msg-hook.sh                    | 15 ++++++
 t/t7505-prepare-commit-msg-hook.sh            | 14 ++++++
 6 files changed, 94 insertions(+), 13 deletions(-)
builtin/commit.c +9 -5
index 70a7842e224..074a57937f1 100644
@@ -695,11 +695,14 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE);
 	int old_display_comment_prefix;
 	int merge_contains_scissors = 0;
+	int message_from_stdin = logfile && !strcmp(logfile, "-");
+	const unsigned hook_flags = message_from_stdin ? 0 : RUN_HOOK_ALLOW_STDIN;
 
 	/* This checks and barfs if author is badly specified */
 	determine_author_info(author_ident);
 
-	if (!no_verify && run_commit_hook(use_editor, index_file, 0, "pre-commit", NULL))
+	if (!no_verify &&
+	    run_commit_hook(use_editor, index_file, hook_flags, "pre-commit", NULL))
 		return 0;
 
 	if (squash_message) {
@@ -724,7 +727,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	if (have_option_m && !fixup_message) {
 		strbuf_addbuf(&sb, &message);
 		hook_arg1 = "message";
-	} else if (logfile && !strcmp(logfile, "-")) {
+	} else if (message_from_stdin) {
 		if (isatty(0))
 			fprintf(stderr, _("(reading log message from standard input)\n"));
 		if (strbuf_read(&sb, 0, 0) < 0)
@@ -998,7 +1001,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 		return 0;
 	}
 
-	if (run_commit_hook(use_editor, index_file, 0, "prepare-commit-msg",
+	if (run_commit_hook(use_editor, index_file, RUN_HOOK_ALLOW_STDIN, "prepare-commit-msg",
 			    git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL))
 		return 0;
 
@@ -1015,7 +1018,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	}
 
 	if (!no_verify &&
-	    run_commit_hook(use_editor, index_file, 0, "commit-msg", git_path_commit_editmsg(), NULL)) {
+	    run_commit_hook(use_editor, index_file, RUN_HOOK_ALLOW_STDIN, "commit-msg",
+			    git_path_commit_editmsg(), NULL)) {
 		return 0;
 	}
 
@@ -1701,7 +1705,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 
 	repo_rerere(the_repository, 0);
 	run_auto_maintenance(quiet);
-	run_commit_hook(use_editor, get_index_file(), 0, "post-commit", NULL);
+	run_commit_hook(use_editor, get_index_file(), RUN_HOOK_ALLOW_STDIN, "post-commit", NULL);
 	if (amend && !no_post_rewrite) {
 		commit_post_rewrite(the_repository, current_head, &oid);
 	}
builtin/merge.c +8 -4
index 26e6ae15993..d6faca59258 100644
@@ -836,8 +836,11 @@ static void prepare_to_commit(struct commit_list *remoteheads)
 	struct strbuf msg = STRBUF_INIT;
 	const char *index_file = get_index_file();
 
-	if (!no_verify && run_commit_hook(0 < option_edit, index_file, 0, "pre-merge-commit", NULL))
+	if (!no_verify &&
+	    run_commit_hook(0 < option_edit, index_file, RUN_HOOK_ALLOW_STDIN,
+			    "pre-merge-commit", NULL)) {
 		abort_commit(remoteheads, NULL);
+	}
 	/*
 	 * Re-read the index as pre-merge-commit hook could have updated it,
 	 * and write it out as a tree.  We must do this before we invoke
@@ -864,8 +867,9 @@ static void prepare_to_commit(struct commit_list *remoteheads)
 		append_signoff(&msg, ignore_non_trailer(msg.buf, msg.len), 0);
 	write_merge_heads(remoteheads);
 	write_file_buf(git_path_merge_msg(the_repository), msg.buf, msg.len);
-	if (run_commit_hook(0 < option_edit, get_index_file(), 0, "prepare-commit-msg",
-			    git_path_merge_msg(the_repository), "merge", NULL))
+	if (run_commit_hook(0 < option_edit, get_index_file(), RUN_HOOK_ALLOW_STDIN,
+			    "prepare-commit-msg", git_path_merge_msg(the_repository),
+			    "merge", NULL))
 		abort_commit(remoteheads, NULL);
 	if (0 < option_edit) {
 		if (launch_editor(git_path_merge_msg(the_repository), NULL, NULL))
@@ -873,7 +877,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
 	}
 
 	if (!no_verify && run_commit_hook(0 < option_edit, get_index_file(),
-					  0, "commit-msg",
+					  RUN_HOOK_ALLOW_STDIN, "commit-msg",
 					  git_path_merge_msg(the_repository), NULL))
 		abort_commit(remoteheads, NULL);
 
sequencer.c +3 -3
index 5f48d32e2fa..5190879695a 100644
@@ -1203,8 +1203,8 @@ static int run_prepare_commit_msg_hook(struct repository *r,
 	} else {
 		arg1 = "message";
 	}
-	if (run_commit_hook(0, r->index_file, 0, "prepare-commit-msg", name,
-			    arg1, arg2, NULL))
+	if (run_commit_hook(0, r->index_file, RUN_HOOK_ALLOW_STDIN,
+			    "prepare-commit-msg", name, arg1, arg2, NULL))
 		ret = error(_("'prepare-commit-msg' hook failed"));
 
 	return ret;
@@ -1528,7 +1528,7 @@ static int try_to_commit(struct repository *r,
 		goto out;
 	}
 
-	run_commit_hook(0, r->index_file, 0, "post-commit", NULL);
+	run_commit_hook(0, r->index_file, RUN_HOOK_ALLOW_STDIN, "post-commit", NULL);
 	if (flags & AMEND_MSG)
 		commit_post_rewrite(r, current_head, oid);
 
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +45 -1

66 lines not on this page · raw message

t/t7504-commit-msg-hook.sh +15 -0
index 31b9c6a2c1d..aa76eb7e1f9 100755
@@ -294,5 +294,20 @@ test_expect_success 'hook is called for reword during `rebase -i`' '
 
 '
 
+# now a hook that accepts input and writes it as the commit message
+cat >"$HOOK" <<'EOF'
+#!/bin/sh
+! read -r line || echo "$line" >"$1"
+EOF
+chmod +x "$HOOK"
+
+test_expect_success 'hook with user input' '
+
+	echo "additional" >>file &&
+	git add file &&
+	echo "user input" | git commit -m "additional" &&
+	commit_msg_is "user input"
+
+'
 
 test_done
t/t7505-prepare-commit-msg-hook.sh +14 -0
index 94f85cdf831..aa9c9375e63 100755
@@ -91,6 +91,11 @@ else
 fi
 test "$GIT_EDITOR" = : && source="$source (no editor)"
 
+if read -r line
+then
+	source="$source $line"
+fi
+
 if test $rebasing = 1
 then
 	echo "$source $(get_last_cmd)" >"$1"
@@ -113,6 +118,15 @@ test_expect_success 'with hook (-m)' '
 
 '
 
+test_expect_success 'with hook (-m and input)' '
+
+	echo "more" >>file &&
+	git add file &&
+	echo "user input" | git commit -m "more" &&
+	test "$(git log -1 --pretty=format:%s)" = "message (no editor) user input"
+
+'
+
 test_expect_success 'with hook (-m editor)' '
 
 	echo "more" >> file &&
Signature · 1 line
gitgitgadget
#21 lore raw

Re: [PATCH v5 2/2] hooks: allow input from stdin for commit-related hooks

Quoted 5 lines · Orgad Shaneh via GitGitGadget

It's important to note that the hook author should be aware that stdin is not always applicable. For example, when running from IDE. This can be checked by isatty on stdin. The hooks should handle cases of closed input, and possibly fall-back to GUI input, or have sane defaults with a message to the user on this case.

I think this point was already brought up in the review on previous rounds, but when the hook needs to check the standard input anyway, it probably is a better design to close and have the hook open tty if needed, isn't it? I do not recall I saw a satisfactory answer to that question.

Quoted 7 lines

Allow stdin only for commit-related hooks. Some of the other hooks pass their own input to the hook, so don't change them.

Note: If pre-commit reads from stdin, and git commit is executed with -F - (read message from stdin), stdin cannot be passed to the hook, since it will consume it before reaching the point where it is read for the commit message.

It is unclear what that Note is trying to achieve. Is it describing a known-bug in this implementation (if so, we'd probably need to update the documentation to mention this known regression)? Is it describing a reason why certain part of patch was done in a certain way that is not described in this message (e.g. when -F option is in effect the standard input stream is closed when invoking a hook)?

Thanks.