Skip to content

Commit 99ddc24

Browse files
avargitster
authored andcommitted
hook API: don't segfault on strbuf_addf() to NULL "out"
Fix a logic error in a082345 (hook API: fix v2.36.0 regression: hooks should be connected to a TTY, 2022-06-07). When it started using the "ungroup" API added in fd3aaf5 (run-command: add an "ungroup" option to run_process_parallel(), 2022-06-07) it should have made the same sort of change that fd3aaf5 itself made in "t/helper/test-run-command.c". The correct way to emit this "Couldn't start" output with "ungroup" would be: fprintf(stderr, _("Couldn't start hook '%s'\n"), hook_path); But we should instead remove the emitting of this output. As the added test shows we already emit output when we can't run the child. The "cannot run" output here is emitted by run-command.c's child_err_spew(). So the addition of the "Couldn't start hook" output here in 96e7225 (hook: add 'run' subcommand, 2021-12-22) was always redundant. For the pre-commit hook we'll now emit exactly the same output as we did before f443246 (commit: convert {pre-commit,prepare-commit-msg} hook to hook.h, 2021-12-22) (and likewise for others). We could at this point add this to the pick_next_hook() callbacks in hook.c: assert(!out); assert(!*pp_task_cb); And this to notify_start_failure() and notify_hook_finished() (in the latter case the parameter is called "pp_task_cp"): assert(!out); assert(!pp_task_cb); But let's leave any such instrumentation for some eventual cleanup of the "ungroup" API. Reported-by: Ilya K <[email protected]> Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Reviewed-by: Emily Shaffer <[email protected]> Reviewed-by: Đoàn Trần Công Danh <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a082345 commit 99ddc24

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

hook.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ static int pick_next_hook(struct child_process *cp,
6262
strvec_push(&cp->args, hook_path);
6363
strvec_pushv(&cp->args, hook_cb->options->args.v);
6464

65-
/* Provide context for errors if necessary */
66-
*pp_task_cb = (char *)hook_path;
67-
6865
/*
6966
* This pick_next_hook() will be called again, we're only
7067
* running one hook, so indicate that no more work will be
@@ -80,13 +77,9 @@ static int notify_start_failure(struct strbuf *out,
8077
void *pp_task_cp)
8178
{
8279
struct hook_cb_data *hook_cb = pp_cb;
83-
const char *hook_path = pp_task_cp;
8480

8581
hook_cb->rc |= 1;
8682

87-
strbuf_addf(out, _("Couldn't start hook '%s'\n"),
88-
hook_path);
89-
9083
return 1;
9184
}
9285

t/t1800-hook.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,30 @@ test_expect_success TTY 'git commit: stdout and stderr are connected to a TTY' '
151151
test_hook_tty commit -m"B.new"
152152
'
153153

154+
test_expect_success 'git hook run a hook with a bad shebang' '
155+
test_when_finished "rm -rf bad-hooks" &&
156+
mkdir bad-hooks &&
157+
write_script bad-hooks/test-hook "/bad/path/no/spaces" </dev/null &&
158+
159+
# TODO: We should emit the same (or at least a more similar)
160+
# error on Windows and !Windows. See the OS-specific code in
161+
# start_command()
162+
if test_have_prereq !WINDOWS
163+
then
164+
cat >expect <<-\EOF
165+
fatal: cannot run bad-hooks/test-hook: ...
166+
EOF
167+
else
168+
cat >expect <<-\EOF
169+
error: cannot spawn bad-hooks/test-hook: ...
170+
EOF
171+
fi &&
172+
test_expect_code 1 git \
173+
-c core.hooksPath=bad-hooks \
174+
hook run test-hook >out 2>err &&
175+
test_must_be_empty out &&
176+
sed -e "s/test-hook: .*/test-hook: .../" <err >actual &&
177+
test_cmp expect actual
178+
'
179+
154180
test_done

0 commit comments

Comments
 (0)