propose config-based hooks
Subsystems (2)
PATCH v2 26/30 receive-pack: convert 'update' hook to hook.h
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
This makes use of the new sideband API in hook.h added in the preceding commit.
--- builtin/receive-pack.c | 65 ++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 21 deletions(-)
builtin/receive-pack.c +44 -21
@@ -938,33 +938,56 @@ static int run_receive_hook(struct command *commands,return status; } -static int run_update_hook (struct command *cmd) +static void hook_output_to_sideband (struct strbuf *output,void *cb_data) { -const char *argv[5 ]; -struct child_process proc =CHILD_PROCESS_INIT ; -int code; +int keepalive_active =0 ; - argv[0 ] =find_hook ("update" ); -if (!argv[0 ]) -return 0 ; +if (keepalive_in_sec <=0 ) + use_keepalive =KEEPALIVE_NEVER ; +if (use_keepalive ==KEEPALIVE_ALWAYS ) + keepalive_active =1 ; - argv[1 ] = cmd->ref_name; - argv[2 ] =oid_to_hex (&cmd->old_oid); - argv[3 ] =oid_to_hex (&cmd->new_oid); - argv[4 ] =NULL ; +/* send a keepalive if there is no data to write */ +if (keepalive_active && !output->len) { +static const char buf[] ="0005\1" ; +write_or_die (1 , buf,sizeof (buf) -1 ); +return ; + } - proc.no_stdin =1 ; - proc.stdout_to_stderr =1 ; - proc.err = use_sideband ?-1 :0 ; - proc.argv = argv; - proc.trace2_hook_name ="update" ; +if (use_keepalive ==KEEPALIVE_AFTER_NUL && !keepalive_active) { +const char *first_null =memchr (output->buf,'\0' , output->len); +if (first_null) { +/* The null bit is excluded. */ +size_t before_null = first_null - output->buf; +size_t after_null = output->len - (before_null +1 ); + keepalive_active =1 ; +send_sideband (1 ,2 , output->buf, before_null, use_sideband); +send_sideband (1 ,2 , first_null +1 , after_null, use_sideband); + +return ; + } + } + +send_sideband (1 ,2 , output->buf, output->len, use_sideband); +} + +static int run_update_hook (struct command *cmd) +{ +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +int code; + +strvec_pushl (&opt.args, + cmd->ref_name, +oid_to_hex (&cmd->old_oid), +oid_to_hex (&cmd->new_oid), +NULL ); - code =start_command (&proc); -if (code) -return code;if (use_sideband) -copy_to_sideband (proc.err,-1 ,NULL ); -return finish_command (&proc); + opt.consume_sideband = hook_output_to_sideband; + + code =run_hooks ("update" , &opt); +run_hooks_opt_clear (&opt); +return code; } static struct command *find_command_by_refname(struct command *list,
Signature · 1 line
2.32.0.rc3.434.gd8aed1f08a7
PATCH v2 27/30 post-update: use hook.h library
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
--- builtin/receive-pack.c | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-)
builtin/receive-pack.c +7 -19
@@ -1657,33 +1657,21 @@ static const char *update(struct command *cmd, struct shallow_info *si)static void run_update_post_hook (struct command *commands) {struct command *cmd; -struct child_process proc =CHILD_PROCESS_INIT ; -const char *hook; - - hook =find_hook ("post-update" ); -if (!hook) -return ; +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ;for (cmd = commands; cmd; cmd = cmd->next) {if (cmd->error_string || cmd->did_not_exist)continue ; -if (!proc.args.nr) -strvec_push (&proc.args, hook); -strvec_push (&proc.args, cmd->ref_name); +strvec_push (&opt.args, cmd->ref_name); } -if (!proc.args.nr) +if (!opt.args.nr)return ; - proc.no_stdin =1 ; - proc.stdout_to_stderr =1 ; - proc.err = use_sideband ?-1 :0 ; - proc.trace2_hook_name ="post-update" ; +if (use_sideband) + opt.consume_sideband = hook_output_to_sideband; -if (!start_command (&proc)) { -if (use_sideband) -copy_to_sideband (proc.err,-1 ,NULL ); -finish_command (&proc); - } +run_hooks ("post-update" , &opt); +run_hooks_opt_clear (&opt); } static void check_aliased_update_internal(struct command *cmd,
Signature · 1 line
2.32.0.rc3.434.gd8aed1f08a7
PATCH v2 28/30 receive-pack: convert receive hooks to hook.h
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
--- builtin/receive-pack.c | 197 +++++++++++++++++++---------------------- 1 file changed, 90 insertions(+), 107 deletions(-)
builtin/receive-pack.c +90 -107
@@ -748,7 +748,7 @@ static int check_cert_push_options(const struct string_list *push_options)return retval; } -static void prepare_push_cert_sha1 (struct child_process *proc) +static void prepare_push_cert_sha1 (struct run_hooks_opt *opt) {static int already_done;
@@ -772,110 +772,42 @@ static void prepare_push_cert_sha1(struct child_process *proc) nonce_status =check_nonce (push_cert.buf, bogs); }if (!is_null_oid (&push_cert_oid)) { -strvec_pushf (&proc->env_array,"GIT_PUSH_CERT=%s" , +strvec_pushf (&opt->env,"GIT_PUSH_CERT=%s" ,oid_to_hex (&push_cert_oid)); -strvec_pushf (&proc->env_array,"GIT_PUSH_CERT_SIGNER=%s" , +strvec_pushf (&opt->env,"GIT_PUSH_CERT_SIGNER=%s" , sigcheck.signer ? sigcheck.signer :"" ); -strvec_pushf (&proc->env_array,"GIT_PUSH_CERT_KEY=%s" , +strvec_pushf (&opt->env,"GIT_PUSH_CERT_KEY=%s" , sigcheck.key ? sigcheck.key :"" ); -strvec_pushf (&proc->env_array,"GIT_PUSH_CERT_STATUS=%c" , +strvec_pushf (&opt->env,"GIT_PUSH_CERT_STATUS=%c" , sigcheck.result);if (push_cert_nonce) { -strvec_pushf (&proc->env_array, +strvec_pushf (&opt->env,"GIT_PUSH_CERT_NONCE=%s" , push_cert_nonce); -strvec_pushf (&proc->env_array, +strvec_pushf (&opt->env,"GIT_PUSH_CERT_NONCE_STATUS=%s" , nonce_status);if (nonce_status ==NONCE_SLOP ) -strvec_pushf (&proc->env_array, +strvec_pushf (&opt->env,"GIT_PUSH_CERT_NONCE_SLOP=%ld" , nonce_stamp_slop); } } } +struct receive_hook_feed_context { +struct command *cmd; +int skip_broken; +}; +struct receive_hook_feed_state {struct command *cmd;struct ref_push_report *report;int skip_broken;struct strbuf buf; -const struct string_list *push_options; }; -typedef int (*feed_fn )(void *,const char **,size_t *); -static int run_and_feed_hook (const char *hook_name,feed_fn feed, -struct receive_hook_feed_state *feed_state) -{ -struct child_process proc =CHILD_PROCESS_INIT ; -struct async muxer; -const char *argv[2 ]; -int code; - - argv[0 ] =find_hook (hook_name); -if (!argv[0 ]) -return 0 ; - - argv[1 ] =NULL ; - - proc.argv = argv; - proc.in =-1 ; - proc.stdout_to_stderr =1 ; - proc.trace2_hook_name = hook_name; - -if (feed_state->push_options) { -int i; -for (i =0 ; i < feed_state->push_options->nr; i++) -strvec_pushf (&proc.env_array, -"GIT_PUSH_OPTION_%d=%s" , i, - feed_state->push_options->items[i].string); -strvec_pushf (&proc.env_array,"GIT_PUSH_OPTION_COUNT=%d" , - feed_state->push_options->nr); - }else -strvec_pushf (&proc.env_array,"GIT_PUSH_OPTION_COUNT" ); - -if (tmp_objdir) -strvec_pushv (&proc.env_array,tmp_objdir_env (tmp_objdir)); - -if (use_sideband) { -memset (&muxer,0 ,sizeof (muxer)); - muxer.proc = copy_to_sideband; - muxer.in =-1 ; - code =start_async (&muxer); -if (code) -return code; - proc.err = muxer.in; - } - -prepare_push_cert_sha1 (&proc); - - code =start_command (&proc); -if (code) { -if (use_sideband) -finish_async (&muxer); -return code; - } - -sigchain_push (SIGPIPE ,SIG_IGN ); - -while (1 ) { -const char *buf; -size_t n; -if (feed (feed_state, &buf, &n)) -break ; -if (write_in_full (proc.in, buf, n) <0 ) -break ; - } -close (proc.in); -if (use_sideband) -finish_async (&muxer); - -sigchain_pop (SIGPIPE ); - -return finish_command (&proc); -} - -static int feed_receive_hook (void *state_,const char **bufp,size_t *sizep) +static int feed_receive_hook (void *state_) {struct receive_hook_feed_state *state = state_;struct command *cmd = state->cmd;
@@ -884,9 +816,7 @@ static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep) state->skip_broken && (cmd->error_string || cmd->did_not_exist)) cmd = cmd->next;if (!cmd) -return -1 ;/* EOF */ -if (!bufp) -return 0 ;/* OK, can feed something. */ +return 1 ;/* EOF - close the pipe*/ strbuf_reset (&state->buf);if (!state->report) state->report = cmd->report;
@@ -910,32 +840,36 @@ static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep) cmd->ref_name); state->cmd = cmd->next; } -if (bufp) { - *bufp = state->buf.buf; - *sizep = state->buf.len; - }return 0 ; } -static int run_receive_hook (struct command *commands, -const char *hook_name, -int skip_broken, -const struct string_list *push_options) +static int feed_receive_hook_cb (struct strbuf *pipe,void *pp_cb,void *pp_task_cb) { -struct receive_hook_feed_state state; -int status; - -strbuf_init (&state.buf,0 ); - state.cmd = commands; - state.skip_broken = skip_broken; - state.report =NULL ; -if (feed_receive_hook (&state,NULL ,NULL )) -return 0 ; - state.cmd = commands; - state.push_options = push_options; - status =run_and_feed_hook (hook_name, feed_receive_hook, &state); -strbuf_release (&state.buf); -return status; +struct hook *hook = pp_task_cb; +struct receive_hook_feed_state *feed_state = hook->feed_pipe_cb_data; +int rc; + +/* first-time setup */ +if (!feed_state) { +struct hook_cb_data *hook_cb = pp_cb; +struct run_hooks_opt *opt = hook_cb->options; +struct receive_hook_feed_context *ctx = opt->feed_pipe_ctx; +if (!ctx) +BUG ("run_hooks_opt.feed_pipe_ctx required for receive hook" ); + + feed_state =xmalloc (sizeof (struct receive_hook_feed_state )); +strbuf_init (&feed_state->buf,0 ); + feed_state->cmd = ctx->cmd; + feed_state->skip_broken = ctx->skip_broken; + feed_state->report =NULL ; + + hook->feed_pipe_cb_data = feed_state; + } + + rc =feed_receive_hook (feed_state); +if (!rc) +strbuf_addbuf (pipe, &feed_state->buf); +return rc; }static void hook_output_to_sideband (struct strbuf *output,void *cb_data)
@@ -971,6 +905,55 @@ static void hook_output_to_sideband(struct strbuf *output, void *cb_data)send_sideband (1 ,2 , output->buf, output->len, use_sideband); } +static int run_receive_hook (struct command *commands, +const char *hook_name, +int skip_broken, +const struct string_list *push_options) +{ +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct receive_hook_feed_context ctx; +int rc; +struct command *iter = commands; + +/* if there are no valid commands, don't invoke the hook at all. */ +while (iter && skip_broken && (iter->error_string || iter->did_not_exist)) + iter = iter->next; +if (!iter) +return 0 ; + +/* pre-receive hooks should run in series as the hook updates refs */ +if (!strcmp (hook_name,"pre-receive" )) + opt.jobs =1 ; + +if (push_options) { +int i; +for (i =0 ; i < push_options->nr; i++) +strvec_pushf (&opt.env,"GIT_PUSH_OPTION_%d=%s" , i, + push_options->items[i].string); +strvec_pushf (&opt.env,"GIT_PUSH_OPTION_COUNT=%d" , push_options->nr); + }else +strvec_push (&opt.env,"GIT_PUSH_OPTION_COUNT" ); + +if (tmp_objdir) +strvec_pushv (&opt.env,tmp_objdir_env (tmp_objdir)); + +prepare_push_cert_sha1 (&opt); + +/* set up sideband printer */ +if (use_sideband) + opt.consume_sideband = hook_output_to_sideband; + +/* set up stdin callback */ + ctx.cmd = commands; + ctx.skip_broken = skip_broken; + opt.feed_pipe = feed_receive_hook_cb; + opt.feed_pipe_ctx = &ctx; + + rc =run_hooks (hook_name, &opt); +run_hooks_opt_clear (&opt); +return rc; +} +static int run_update_hook (struct command *cmd) {struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ;
Signature · 1 line
2.32.0.rc3.434.gd8aed1f08a7
PATCH v2 29/30 hooks: fix a TOCTOU in "did we run a hook?" heuristic
In reply to Ævar Arnfjörð BjarmasonFix a Time-of-check to time-of-use (TOCTOU) race in code added in 680ee550d72 (commit: skip discarding the index if there is no pre-commit hook, 2017-08-14).
We can fix the race passing around information about whether or not we ran the hook in question, instead of running hook_exists() after the fact to check if the hook in question exists. This problem has been noted on-list when 680ee550d72 was discussed[1], but had not been fixed.
In addition to fixing this for the pre-commit hook as suggested there I'm also fixing this for the pre-merge-commit hook. See 6098817fd7f (git-merge: honor pre-merge-commit hook, 2019-08-07) for the introduction of its previous behavior.
Let's also change this for the push-to-checkout hook. Now instead of checking if the hook exists and either doing a push to checkout or a push to deploy we'll always attempt a push to checkout. If the hook doesn't exist we'll fall back on push to deploy. The same behavior as before, without the TOCTOU race. See 0855331941b (receive-pack:
support push-to-checkout hook, 2014-12-01) for the introduction of the previous behavior.
This leaves uses of hook_exists() in two places that matter. The "reference-transaction" check in refs.c, see 67541597670 (refs:
implement reference transaction hook, 2020-06-19), and the prepare-commit-msg hook, see 66618a50f9c (sequencer: run 'prepare-commit-msg' hook, 2018-01-24).
In both of those cases we're saving ourselves CPU time by not preparing data for the hook that we'll then do nothing with if we don't have the hook. So using this "invoked_hook" pattern doesn't make sense in those cases.
More importantly, in those cases the worst we'll do is miss that we "should" run the hook because a new hook appeared, whereas in the pre-commit and pre-merge-commit cases we'll skip an important discard_cache() on the bases of our faulty guess.
I do think none of these races really matter in practice. It would be some one-off issue as a hook was added or removed. I did think it was stupid that we didn't pass a "did this run?" flag instead of doing this guessing at a distance though, so now we're not guessing anymore.
1. https://lore.kernel.org/git/[email protected]/
--- builtin/commit.c | 18 +++++++++++------- builtin/merge.c | 16 ++++++++++------ builtin/receive-pack.c | 8 +++++--- commit.c | 1 + commit.h | 3 ++- hook.c | 4 ++++ hook.h | 10 ++++++++++ sequencer.c | 4 ++-- 8 files changed, 45 insertions(+), 19 deletions(-)
builtin/commit.c +11 -7
@@ -725,11 +725,13 @@ 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 invoked_hook =0 ;/* 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, &invoked_hook, +"pre-commit" ,NULL ))return 0 ;if (squash_message) {
@@ -1045,10 +1047,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix,return 0 ; } -if (!no_verify &&hook_exists ("pre-commit" )) { +if (!no_verify && invoked_hook) {/* -* Re-read the index as pre-commit hook could have updated it, -* and write it out as a tree. We must do this before we invoke +* Re-read the index as the pre-commit-commit hook was invoked +* and could have updated it. We must do this before we invoke * the editor and after we invoke run_status above. */ discard_cache ();
@@ -1060,7 +1062,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,NULL ,"prepare-commit-msg" ,git_path_commit_editmsg (), hook_arg1, hook_arg2,NULL ))return 0 ;
@@ -1077,7 +1079,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,NULL ,"commit-msg" , +git_path_commit_editmsg (),NULL )) {return 0 ; }
@@ -1830,7 +1833,8 @@ 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 (),NULL ,"post-commit" , +NULL );if (amend && !no_post_rewrite) {commit_post_rewrite (the_repository, current_head, &oid); }
builtin/merge.c +10 -6
@@ -844,15 +844,18 @@ static void prepare_to_commit(struct commit_list *remoteheads) {struct strbuf msg =STRBUF_INIT ;const char *index_file =get_index_file (); +int invoked_hook =0 ; -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, + &invoked_hook,"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 +* Re-read the index as the pre-merge-commit hook was invoked +* and could have updated it. We must do this before we invoke * the editor and after we invoke run_status above. */ -if (hook_exists ("pre-merge-commit" )) +if (invoked_hook)discard_cache ();read_cache_from (index_file);strbuf_addbuf (&msg, &merge_msg);
@@ -873,7 +876,8 @@ 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 (),NULL , +"prepare-commit-msg" ,git_path_merge_msg (the_repository),"merge" ,NULL ))abort_commit (remoteheads,NULL );if (0 < option_edit) {
@@ -882,7 +886,7 @@ static void prepare_to_commit(struct commit_list *remoteheads) }if (!no_verify &&run_commit_hook (0 < option_edit,get_index_file (), -"commit-msg" , +NULL ,"commit-msg" ,git_path_merge_msg (the_repository),NULL ))abort_commit (remoteheads,NULL );
builtin/receive-pack.c +5 -3
@@ -1439,10 +1439,12 @@ static const char *push_to_deploy(unsigned char *sha1,static const char *push_to_checkout_hook ="push-to-checkout" ;static const char *push_to_checkout (unsigned char *hash, +int *invoked_hook,struct strvec *env,const char *work_tree) {struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; + opt.invoked_hook = invoked_hook;strvec_pushf (env,"GIT_WORK_TREE=%s" ,absolute_path (work_tree));strvec_pushv (&opt.env, env->v);
@@ -1460,6 +1462,7 @@ static const char *update_worktree(unsigned char *sha1, const struct worktree *w {const char *retval, *work_tree, *git_dir =NULL ;struct strvec env =STRVEC_INIT ; +int invoked_hook =0 ;if (worktree && worktree->path) work_tree = worktree->path;
@@ -1477,10 +1480,9 @@ static const char *update_worktree(unsigned char *sha1, const struct worktree *wstrvec_pushf (&env,"GIT_DIR=%s" ,absolute_path (git_dir)); -if (!hook_exists (push_to_checkout_hook)) + retval =push_to_checkout (sha1, &invoked_hook, &env, work_tree); +if (!invoked_hook) retval =push_to_deploy (sha1, &env, work_tree); -else - retval =push_to_checkout (sha1, &env, work_tree);strvec_clear (&env);return retval;
commit.c +1 -0
@@ -1697,6 +1697,7 @@ size_t ignore_non_trailer(const char *buf, size_t len) }int run_commit_hook (int editor_is_used,const char *index_file, +int *invoked_hook,const char *name, ...) {struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ;
commit.h +2 -1
@@ -363,7 +363,8 @@ 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, +int *invoked_hook,const char *name, ...);/* Sign a commit or tag buffer, storing the result in a header. */ int sign_with_header (struct strbuf *buf,const char *keyid);
hook.c +4 -0
@@ -138,6 +138,9 @@ static int notify_hook_finished(int result,/* |= rc in cb */ hook_cb->rc |= result; +if (hook_cb->invoked_hook) + *hook_cb->invoked_hook =1 ; +return 1 ; }
@@ -152,6 +155,7 @@ int run_found_hooks(const char *hook_name, const char *hook_path, .rc = 0, .hook_name = hook_name, .options = options, + .invoked_hook = options->invoked_hook, };if (options->absolute_path) {strbuf_add_absolute_path (&abs_path, hook_path);
hook.h +10 -0
@@ -57,6 +57,15 @@ struct run_hooks_opt* for an example. */ consume_sideband_fn consume_sideband; + +/* +* A pointer which if provided will be set to 1 or 0 depending +* on if a hook was invoked (i.e. existed), regardless of +* whether or not that was successful. Used for avoiding +* TOCTOU races in code that would otherwise call hook_exist() +* after a "maybe hook run" to see if a hook was invoked. +*/ +int *invoked_hook; };#define RUN_HOOKS_OPT_INIT { \
@@ -81,6 +90,7 @@ struct hook_cb_data {const char *hook_name;struct hook *run_me;struct run_hooks_opt *options; +int *invoked_hook; };/*
sequencer.c +2 -2
@@ -1204,7 +1204,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,NULL ,"prepare-commit-msg" , name, arg1, arg2,NULL )) ret =error (_ ("'prepare-commit-msg' hook failed" ));
@@ -1534,7 +1534,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,NULL ,"post-commit" ,NULL );if (flags &AMEND_MSG )commit_post_rewrite (r, current_head, oid);
Signature · 1 line
2.32.0.rc3.434.gd8aed1f08a7
PATCH v2 30/30 hook-list.h: add a generated list of hooks, like config-list.h
In reply to Ævar Arnfjörð BjarmasonResolve a long-standing TODO item in bugreport.c of there being no centralized listing of hooks, this fixes a bug with the bugreport listing only knowing about 1/4 of the p4 hooks. It didn't know about the "reference-transaction" hook either.
We can now make sure this is kept up-to-date, as the hook.c library will die if asked to find a hook we don't know about yet. The only (undocumented) exception is the artificial "test-hook" used in our own test suite. Move some of the tests away from the "does-not-exist" pseudo-hook, and test for the new behavior.
--- .gitignore | 1 + Makefile | 14 +++++++++++--- builtin/bugreport.c | 44 ++++++++------------------------------------ generate-hooklist.sh | 24 ++++++++++++++++++++++++ hook.c | 22 ++++++++++++++++++++++ t/t1800-hook.sh | 14 +++++++++++--- 6 files changed, 77 insertions(+), 42 deletions(-) create mode 100755 generate-hooklist.sh
.gitignore +1 -0
@@ -191,6 +191,7 @@ /gitweb/static/gitweb.min.* /config-list.h /command-list.h +/hook-list.h *.tar.gz *.dsc *.deb
Makefile +11 -3
@@ -817,6 +817,7 @@ XDIFF_LIB = xdiff/lib.a GENERATED_H += command-list.h GENERATED_H += config-list.h +GENERATED_H += hook-list.h LIB_H := $(sort $(patsubst ./%,%,$(shell git ls-files '*.h' ':!t/' ':!Documentation/' 2>/dev/null || \ $(FIND) . \
@@ -2207,7 +2208,9 @@ git$X: git.o GIT-LDFLAGS $(BUILTIN_OBJS) $(GITLIBS) help.sp help.s help.o: command-list.h -builtin/help.sp builtin/help.s builtin/help.o: config-list.h GIT-PREFIX +hook.sp hook.s hook.o: hook-list.h + +builtin/help.sp builtin/help.s builtin/help.o: config-list.h hook-list.h GIT-PREFIX builtin/help.sp builtin/help.s builtin/help.o: EXTRA_CPPFLAGS = \ '-DGIT_HTML_PATH="$(htmldir_relative_SQ)"' \ '-DGIT_MAN_PATH="$(mandir_relative_SQ)"' \
@@ -2240,6 +2243,11 @@ command-list.h: $(wildcard Documentation/git*.txt) $(patsubst %,--exclude-program %,$(EXCLUDED_PROGRAMS)) \ command-list.txt >$@+ && mv $@+ $@ +hook-list.h: generate-hooklist.sh +hook-list.h: Documentation/githooks.txt + $(QUIET_GEN)$(SHELL_PATH) ./generate-hooklist.sh \ + >$@+ && mv $@+ $@ + SCRIPT_DEFINES = $(SHELL_PATH_SQ):$(DIFF_SQ):$(GIT_VERSION):\ $(localedir_SQ):$(NO_CURL):$(USE_GETTEXT_SCHEME):$(SANE_TOOL_PATH_SQ):\ $(gitwebdir_SQ):$(PERL_PATH_SQ):$(SANE_TEXT_GREP):$(PAGER_ENV):\
@@ -2890,7 +2898,7 @@ $(SP_OBJ): %.sp: %.c GIT-CFLAGS FORCE .PHONY: sparse $(SP_OBJ) sparse: $(SP_OBJ) -EXCEPT_HDRS := command-list.h config-list.h unicode-width.h compat/% xdiff/% +EXCEPT_HDRS := command-list.h config-list.h hook-list.h unicode-width.h compat/% xdiff/% ifndef GCRYPT_SHA256 EXCEPT_HDRS += sha256/gcrypt.h endif
@@ -2912,7 +2920,7 @@ hdr-check: $(HCO) style: git clang-format --style file --diff --extensions c,h -check: config-list.h command-list.h +check: config-list.h command-list.h hook-list.h @if sparse; \ then \ echo >&2 "Use 'make sparse' instead"; \
builtin/bugreport.c +8 -36
62 lines not on this page · raw message
generate-hooklist.sh new file +24 -0
@@ -0,0 +1,24 @@ +#!/bin/sh + +echo "/* Automatically generated by generate-hooklist.sh */" + +print_hook_list () { + cat <<EOF +static const char *hook_name_list[] = { +EOF + perl -ne ' + chomp; + @l[$.] = $_; + push @h => $l[$. - 1] if /^~~~+$/s; + END { + print qq[\t"$_",\n] for sort @h; + } + ' <Documentation/githooks.txt + cat <<EOF + NULL, +}; +EOF +} + +echo +print_hook_list
hook.c +22 -0
@@ -1,11 +1,33 @@#include "cache.h" #include "hook.h" #include "run-command.h" +#include "hook-list.h" + +static int known_hook (const char *name) +{ +const char **p; +size_t len =strlen (name); +for (p = hook_name_list; *p; p++) { +const char *hook = *p; + +if (!strncmp (name, hook, len) && hook[len] =='\0' ) +return 1 ; + } +if (!strcmp (name,"test-hook" ) || + !strcmp (name,"does-not-exist" )) +return 1 ; + +return 0 ; +}const char *find_hook (const char *name) {static struct strbuf path =STRBUF_INIT ; +if (!known_hook (name)) +die (_ ("the hook '%s' is not known to git, should be in hook-list.h via githooks(5)" ), + name); +strbuf_reset (&path);strbuf_git_path (&path,"hooks/%s" , name);if (access (path.buf,X_OK ) <0 ) {
t/t1800-hook.sh +11 -3
@@ -6,17 +6,25 @@ test_description='git-hook command' test_expect_success 'git hook run -- nonexistent hook' ' cat >stderr.expect <<-\EOF && - error: cannot find a hook named does-not-exist + error: cannot find a hook named test-hook EOF - test_expect_code 1 git hook run does-not-exist 2>stderr.actual && + test_expect_code 1 git hook run test-hook 2>stderr.actual && test_cmp stderr.expect stderr.actual ' test_expect_success 'git hook run -- nonexistent hook with --ignore-missing' ' - git hook run --ignore-missing does-not-exist 2>stderr.actual && + git hook run --ignore-missing test-hook 2>stderr.actual && test_must_be_empty stderr.actual ' +test_expect_success 'git hook run -- nonexistent hook with --ignore-missing' ' + cat >stderr.expect <<-\EOF && + fatal: the hook '"'"'unknown-hook'"'"' is not known to git, should be in hook-list.h via githooks(5) + EOF + test_expect_code 128 git hook run unknown-hook 2>stderr.actual && + test_cmp stderr.expect stderr.actual +' + test_expect_success 'git hook run -- basic' ' write_script .git/hooks/test-hook <<-EOF && echo Test hook
Signature · 1 line
2.32.0.rc3.434.gd8aed1f08a7
Re: [PATCH v2 30/30] hook-list.h: add a generated list of hooks, like config-list.h
Quoted 39 lines · Ævar Arnfjörð Bjarmason
Resolve a long-standing TODO item in
bugreport.cof there being no centralized listing of hooks, this fixes a bug with the bugreport listing only knowing about 1/4 of the p4 hooks. It didn't know about the "reference-transaction" hook either.We can now make sure this is kept up-to-date, as the
hook.clibrary will die if asked to find a hook we don't know about yet. The only (undocumented) exception is the artificial "test-hook" used in our own test suite. Move some of the tests away from the "does-not-exist" pseudo-hook, and test for the new behavior.Signed-off-by Ævar Arnfjörð Bjarmason <[email protected]>--- .gitignore | 1 + Makefile | 14 +++++++++++--- builtin/bugreport.c | 44 ++++++++------------------------------------ generate-hooklist.sh | 24 ++++++++++++++++++++++++ hook.c | 22 ++++++++++++++++++++++ t/t1800-hook.sh | 14 +++++++++++--- 6 files changed, 77 insertions(+), 42 deletions(-) create mode 100755 generate-hooklist.sh.gitignore +1
-0@@ -191,6 +191,7 @@ /gitweb/static/gitweb.min.* /config-list.h /command-list.h +/hook-list.h *.tar.gz *.dsc *.debMakefile +0
-0
Makefile XDIFF_LIB = xdiff/lib.a · line 820
Quoted 4 lines
Makefile +1
-0@@ -817,6 +817,7 @@ XDIFF_LIB = xdiff/lib.a GENERATED_H += command-list.h GENERATED_H += config-list.h +GENERATED_H += hook-list.h
This fails CI on the Windows boxes (or some of them...) because we hard rely on cmake there since semi-recently; See 4c2c38e800f (ci:
modification of main.yml to use cmake for vs-build job, 2020-06-26).
Fixing that seems to be a matter of copying some similar boilerplate around, but I haven't tried.
I was somewhat surprised that cmake support being semi-mandatory seems to have snuck up on us (or at least on me). The docs in compat/vcbuild/README still suggest you need make, so perhaps the CI on Windows could be split up into something that uses "make" and one that just checks for the cmake compatibility.
It's not that I much prefer the Makefile, but I do think the state of affairs of needing to duplicate any work of patching the Makefile would be a step back for me, but maybe the benefit on Windows is so large as to be worth it...
Re: [PATCH v2 00/30] Minimal restart of "config-based-hooks"
In reply to Ævar Arnfjörð BjarmasonQuoted 6 lines · Ævar Arnfjörð Bjarmason
I proposed splitting Emily's "hook config" topic[1] into at least a topic that retains all current behavior of the codebase, and merely refactors existing behavior to new APIs, and then doing behavior changes later.
Quoted 7 lines
This is a re-roll of an attempt to do that. See v1's CL [2] for much more details.
I was hoping to get more feedback from Emily in reply to [3] and related E-Mails on the v1, but as it's been almost 2 weeks with no reply, and both her topic and mine semantically conflict with changes since merged to "master" I thought I'd send this re-roll.
Yes, sorry about this. I'm planning on reviewing your series this week. Thanks for sending the v2.
- Emily
Re: [PATCH v2 00/30] Minimal restart of "config-based-hooks"
Quoted 10 lines · Emily Shaffer
Quoted 9 lines · Ævar Arnfjörð Bjarmason
I proposed splitting Emily's "hook config" topic[1] into at least a topic that retains all current behavior of the codebase, and merely refactors existing behavior to new APIs, and then doing behavior changes later.
This is a re-roll of an attempt to do that. See v1's CL [2] for much more details.
Quoted 7 lines
Quoted 4 lines
I was hoping to get more feedback from Emily in reply to [3] and related E-Mails on the v1, but as it's been almost 2 weeks with no reply, and both her topic and mine semantically conflict with changes since merged to "master" I thought I'd send this re-roll.
Yes, sorry about this. I'm planning on reviewing your series this week. Thanks for sending the v2.
Thanks, both. In the meantime, I will eject es/config-based-hooks out of 'seen' and replace it with these patches, to see if there are unexpected interactions with other topics in flight.
cover Base for "config-based-hooks"
In reply to Ævar Arnfjörð BjarmasonThis v3 of the "Base for 'config-based-hooks'" topic is >95% a slimmed-down versio nof Emily Schaffer's work to introduce a mechanism to drive hooks via config.
This topic doesn't do that, but moves all hook execution (C libary and Perl|Python script) to either the new hook.[ch] library code, or the "git hook run" utility.
See previous iterations for more details:
v0 (Emily's): http://lore.kernel.org/git/[email protected]
v1: https://lore.kernel.org/git/[email protected]/
v2: http://lore.kernel.org/git/[email protected]
This series gained two new dependencies since v2, my just-submitted preparatory topics:
https://lore.kernel.org/git/[email protected]/ https://lore.kernel.org/git/[email protected]/
And hopefully addreses all the feedback on v2, mainly/entirely from Emily at https://lore.kernel.org/git/[email protected]
I'd normally have waited longer for a v3, but as discussed in the small hook.[ch] topic that precedes this one building the new hook-list.h had an error on Windows CI due to a missing CMake change, that's now fixed.
Other changes can be seen in the range-diff, all rather trivial fixes like comment fixes, trimming off "argc/argv" before we pass things to "git hook run", removing a redundant test setup etc.
I also added a GIT_TEST_FAKE_HOOKS=true for use in the test suite to make us support a "test-hook" and a "does-not-exist" hook, we'd previously accept those outside the test environment.
Emily Shaffer (25):
hook: add 'run' subcommand
gc: use hook library for pre-auto-gc hook
rebase: teach pre-rebase to use hook.h
am: convert applypatch hooks to use config
hooks: convert 'post-checkout' hook to hook library
merge: use config-based hooks for post-merge hook
send-email: use 'git hook run' for 'sendemail-validate'
git-p4: use 'git hook' to run hooks
commit: use hook.h to execute hooks
read-cache: convert post-index-change hook to use config
receive-pack: convert push-to-checkout hook to hook.h
run-command: remove old run_hook_{le,ve}() hook API
run-command: allow stdin for run_processes_parallel
hook: support passing stdin to hooks
am: convert 'post-rewrite' hook to hook.h
run-command: add stdin callback for parallelization
hook: provide stdin by string_list or callback
hook: convert 'post-rewrite' hook in sequencer.c to hook.h
transport: convert pre-push hook to use config
reference-transaction: use hook.h to run hooks
run-command: allow capturing of collated output
hooks: allow callers to capture output
receive-pack: convert 'update' hook to hook.h
post-update: use hook.h library
receive-pack: convert receive hooks to hook.h
Ævar Arnfjörð Bjarmason (2):
git hook run: add an --ignore-missing flag
hooks: fix a TOCTOU in "did we run a hook?" heuristic
.gitignore | 1 + Documentation/git-hook.txt | 51 ++++++ Documentation/githooks.txt | 4 + Makefile | 1 + builtin.h | 1 + builtin/am.c | 33 ++-- builtin/checkout.c | 17 +- builtin/clone.c | 7 +- builtin/commit.c | 18 ++- builtin/fetch.c | 1 + builtin/gc.c | 8 +- builtin/hook.c | 90 +++++++++++ builtin/merge.c | 21 ++- builtin/rebase.c | 9 +- builtin/receive-pack.c | 298 ++++++++++++++++++------------------ builtin/submodule--helper.c | 2 +- builtin/worktree.c | 30 ++-- command-list.txt | 1 + commit.c | 17 +- commit.h | 3 +- git-p4.py | 72 +-------- git-send-email.perl | 20 ++- git.c | 1 + hook.c | 170 ++++++++++++++++++++ hook.h | 109 +++++++++++++ read-cache.c | 12 +- refs.c | 42 ++--- reset.c | 15 +- run-command.c | 122 ++++++++++----- run-command.h | 48 ++++-- sequencer.c | 87 +++++------ submodule.c | 1 + t/helper/test-run-command.c | 46 +++++- t/t0061-run-command.sh | 37 +++++ t/t1800-hook.sh | 154 +++++++++++++++++++ t/t9001-send-email.sh | 4 +- transport.c | 57 ++----- 37 files changed, 1136 insertions(+), 474 deletions(-) create mode 100644 Documentation/git-hook.txt create mode 100644 builtin/hook.c create mode 100755 t/t1800-hook.sh
Range-diff:
1: 447d349c73 ! 1: cf4b06bfdf hook: add 'run' subcommand
@@ Documentation/git-hook.txt (new)
+-----------
+
+run::
-+
-+ Run the `<hook-name>` hook. Any positional arguments to the
-+ hook should be passed after an optional "--" (or
-+ "--end-of-options"). See "OPTIONS" below for the arguments
-+ this accepts.
++ Run the `<hook-name>` hook. See linkgit:githooks[5] for
++ the hook names we support.
+++
++Any positional arguments to the hook should be passed after an
++optional `--` (or `--end-of-options`, see linkgit:gitcli[7]). The
++arguments (if any) differ by hook name, see linkgit:githooks[5] for
++what those are.
+
+SEE ALSO
+--------
@@ Documentation/githooks.txt: and "0" meaning they were not.
Part of the linkgit:git[1] suite
## Makefile ##
-@@ Makefile: LIB_OBJS += hash-lookup.o
- LIB_OBJS += hashmap.o
- LIB_OBJS += help.o
- LIB_OBJS += hex.o
-+LIB_OBJS += hook.o
- LIB_OBJS += ident.o
- LIB_OBJS += json-writer.o
- LIB_OBJS += kwset.o
@@ Makefile: BUILTIN_OBJS += builtin/get-tar-commit-id.o
BUILTIN_OBJS += builtin/grep.o
BUILTIN_OBJS += builtin/hash-object.o
@@ builtin/hook.c (new)
+#include "strvec.h"
+
+static const char * const builtin_hook_usage[] = {
++ N_("git hook <command> [...]"),
++ N_("git hook run <hook-name> [-- <hook-args>]"),
++ NULL
++};
++
++static const char * const builtin_hook_run_usage[] = {
+ N_("git hook run <hook-name> [-- <hook-args>]"),
+ NULL
+};
@@ builtin/hook.c (new)
+ };
+
+ argc = parse_options(argc, argv, prefix, run_options,
-+ builtin_hook_usage, PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
++ builtin_hook_run_usage,
++ PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
+
-+ if (argc > 2) {
-+ if (strcmp(argv[2], "--") &&
-+ strcmp(argv[2], "--end-of-options"))
++ if (argc > 1) {
++ if (strcmp(argv[1], "--") &&
++ strcmp(argv[1], "--end-of-options"))
+ /* Having a -- for "run" is mandatory */
+ usage_with_options(builtin_hook_usage, run_options);
+ /* Add our arguments, start after -- */
-+ for (i = 3 ; i < argc; i++)
++ for (i = 2 ; i < argc; i++)
+ strvec_push(&opt.args, argv[i]);
+ }
+
+ /* Need to take into account core.hooksPath */
+ git_config(git_default_config, NULL);
+
-+ hook_name = argv[1];
++ /*
++ * We are not using run_hooks() because we'd like to detect
++ * missing hooks. Let's find it ourselves and call
++ * run_found_hooks() instead.
++ */
++ hook_name = argv[0];
+ hook_path = find_hook(hook_name);
+ if (!hook_path) {
+ error("cannot find a hook named %s", hook_name);
@@ builtin/hook.c (new)
+ struct option builtin_hook_options[] = {
+ OPT_END(),
+ };
++ argc = parse_options(argc, argv, NULL, builtin_hook_options,
++ builtin_hook_usage, PARSE_OPT_STOP_AT_NON_OPTION);
++ if (!argc)
++ usage_with_options(builtin_hook_usage, builtin_hook_options);
+
-+ if (!strcmp(argv[1], "run"))
++ if (!strcmp(argv[0], "run"))
+ return run(argc, argv, prefix);
-+ usage_with_options(builtin_hook_usage, builtin_hook_options);
-+ return 1;
++ else
++ usage_with_options(builtin_hook_usage, builtin_hook_options);
+}
## command-list.txt ##
@@ git.c: static struct cmd_struct commands[] = {
{ "init", cmd_init_db },
{ "init-db", cmd_init_db },
- ## hook.c (new) ##
+ ## hook.c ##
@@
-+#include "cache.h"
-+#include "hook.h"
-+#include "run-command.h"
+ #include "hook.h"
+ #include "run-command.h"
+ #include "hook-list.h"
++#include "config.h"
+
+ static int known_hook(const char *name)
+ {
+ const char **p;
+ size_t len = strlen(name);
++ static int test_hooks_ok = -1;
++
+ for (p = hook_name_list; *p; p++) {
+ const char *hook = *p;
+
+@@ hook.c: static int known_hook(const char *name)
+ return 1;
+ }
+
++ if (test_hooks_ok == -1)
++ test_hooks_ok = git_env_bool("GIT_TEST_FAKE_HOOKS", 0);
++
++ if (test_hooks_ok &&
++ (!strcmp(name, "test-hook") ||
++ !strcmp(name, "does-not-exist")))
++ return 1;
++
+ return 0;
+ }
+
+@@ hook.c: int hook_exists(const char *name)
+ {
+ return !!find_hook(name);
+ }
+
+void run_hooks_opt_clear(struct run_hooks_opt *o)
+{
@@ hook.c (new)
+ struct hook_cb_data *hook_cb = pp_cb;
+ struct hook *run_me = hook_cb->run_me;
+
-+ if (!run_me)
-+ BUG("did we not return 1 in notify_hook_finished?");
-+
+ cp->no_stdin = 1;
+ cp->env = hook_cb->options->env.v;
+ cp->stdout_to_stderr = 1;
@@ hook.c (new)
+ struct hook_cb_data *hook_cb = pp_cb;
+ struct hook *attempted = pp_task_cp;
+
-+ /* |= rc in cb */
+ hook_cb->rc |= 1;
+
+ strbuf_addf(out, _("Couldn't start hook '%s'\n"),
@@ hook.c (new)
+{
+ struct hook_cb_data *hook_cb = pp_cb;
+
-+ /* |= rc in cb */
+ hook_cb->rc |= result;
+
+ return 1;
+}
+
-+
+int run_found_hooks(const char *hook_name, const char *hook_path,
+ struct run_hooks_opt *options)
+{
@@ hook.c (new)
+
+ hook_path = find_hook(hook_name);
+
-+ /* Care about nonexistence? Use run_found_hooks() */
++ /*
++ * If you need to act on a missing hook, use run_found_hooks()
++ * instead
++ */
+ if (!hook_path)
+ return 0;
+
@@ hook.c (new)
+ return ret;
+}
- ## hook.h (new) ##
+ ## hook.h ##
@@
-+#ifndef HOOK_H
-+#define HOOK_H
+ #ifndef HOOK_H
+ #define HOOK_H
+#include "strbuf.h"
+#include "strvec.h"
+#include "run-command.h"
-+
+
+ /*
+ * Returns the path to the hook file, or NULL if the hook is missing
+@@ hook.h: const char *find_hook(const char *name);
+ */
+ int hook_exists(const char *hookname);
+
+struct hook {
+ /* The path to the hook */
+ const char *hook_path;
@@ hook.h (new)
+ /* Args to be passed to each hook */
+ struct strvec args;
+
-+ /* Number of threads to parallelize across */
++ /*
++ * Number of threads to parallelize across, currently a stub,
++ * we use the parallel API for future-proofing, but we always
++ * have one hook of a given name, so this is always an
++ * implicit 1 for now.
++ */
+ int jobs;
+};
+
@@ hook.h (new)
+ .args = STRVEC_INIT, \
+}
+
-+/*
-+ * Callback provided to feed_pipe_fn and consume_sideband_fn.
-+ */
+struct hook_cb_data {
++ /* rc reflects the cumulative failure state */
+ int rc;
+ const char *hook_name;
+ struct hook *run_me;
@@ hook.h (new)
+ */
+int run_found_hooks(const char *hookname, const char *hook_path,
+ struct run_hooks_opt *options);
-+#endif
+ #endif
## t/t1800-hook.sh (new) ##
@@
@@ t/t1800-hook.sh (new)
+
+. ./test-lib.sh
+
-+test_expect_success 'git hook run -- nonexistent hook' '
++test_expect_success 'git hook usage' '
++ test_expect_code 129 git hook &&
++ test_expect_code 129 git hook -h &&
++ test_expect_code 129 git hook run -h
++'
++
++test_expect_success 'setup GIT_TEST_FAKE_HOOKS=true to permit "test-hook" and "does-not-exist" names"' '
++ GIT_TEST_FAKE_HOOKS=true &&
++ export GIT_TEST_FAKE_HOOKS
++'
++
++test_expect_success 'git hook run: nonexistent hook' '
+ cat >stderr.expect <<-\EOF &&
-+ error: cannot find a hook named does-not-exist
++ error: cannot find a hook named test-hook
+ EOF
-+ test_expect_code 1 git hook run does-not-exist 2>stderr.actual &&
++ test_expect_code 1 git hook run test-hook 2>stderr.actual &&
+ test_cmp stderr.expect stderr.actual
+'
+
-+test_expect_success 'git hook run -- basic' '
++test_expect_success 'git hook run: basic' '
+ write_script .git/hooks/test-hook <<-EOF &&
+ echo Test hook
+ EOF
@@ t/t1800-hook.sh (new)
+ test_cmp expect actual
+'
+
-+test_expect_success 'git hook run -- stdout and stderr handling' '
++test_expect_success 'git hook run: stdout and stderr both write to our stderr' '
+ write_script .git/hooks/test-hook <<-EOF &&
+ echo >&1 Will end up on stderr
+ echo >&2 Will end up on stderr
@@ t/t1800-hook.sh (new)
+ test_must_be_empty stdout.actual
+'
+
-+test_expect_success 'git hook run -- exit codes are passed along' '
++test_expect_success 'git hook run: exit codes are passed along' '
+ write_script .git/hooks/test-hook <<-EOF &&
+ exit 1
+ EOF
@@ t/t1800-hook.sh (new)
+
+test_expect_success 'git -c core.hooksPath=<PATH> hook run' '
+ mkdir my-hooks &&
-+ write_script my-hooks/test-hook <<-EOF &&
-+ echo Hook ran >>actual
++ write_script my-hooks/test-hook <<-\EOF &&
++ echo Hook ran $1 >>actual
+ EOF
+
+ cat >expect <<-\EOF &&
+ Test hook
-+ Hook ran
-+ Hook ran
-+ Hook ran
-+ Hook ran
++ Hook ran one
++ Hook ran two
++ Hook ran three
++ Hook ran four
+ EOF
+
+ # Test various ways of specifying the path. See also
+ # t1350-config-hooks-path.sh
+ >actual &&
-+ git hook run test-hook 2>>actual &&
-+ git -c core.hooksPath=my-hooks hook run test-hook 2>>actual &&
-+ git -c core.hooksPath=my-hooks/ hook run test-hook 2>>actual &&
-+ git -c core.hooksPath="$PWD/my-hooks" hook run test-hook 2>>actual &&
-+ git -c core.hooksPath="$PWD/my-hooks/" hook run test-hook 2>>actual &&
++ git hook run test-hook -- ignored 2>>actual &&
++ git -c core.hooksPath=my-hooks hook run test-hook -- one 2>>actual &&
++ git -c core.hooksPath=my-hooks/ hook run test-hook -- two 2>>actual &&
++ git -c core.hooksPath="$PWD/my-hooks" hook run test-hook -- three 2>>actual &&
++ git -c core.hooksPath="$PWD/my-hooks/" hook run test-hook -- four 2>>actual &&
+ test_cmp expect actual
+'
+
-+test_expect_success 'set up a pre-commit hook in core.hooksPath' '
-+ >actual &&
-+ mkdir -p .git/custom-hooks .git/hooks &&
-+ write_script .git/custom-hooks/pre-commit <<-\EOF &&
-+ echo CUSTOM >>actual
-+ EOF
-+ write_script .git/hooks/pre-commit <<-\EOF
-+ echo NORMAL >>actual
-+ EOF
-+'
-+
+test_done
2: 85195a78cf < -: ---------- run-command.h: move find_hook() to hook.h
3: eb5bdd993c < -: ---------- hook.c: add a hook_exists() wrapper and use it in bugreport.c
4: da2763192a = 2: 7209f73f28 gc: use hook library for pre-auto-gc hook
5: 51e6e72f23 = 3: e9a1e7cf61 rebase: teach pre-rebase to use hook.h
6: d2f3b26d46 = 4: 1d08726930 am: convert applypatch hooks to use config
7: d884465aab ! 5: 32eec5dc2f hooks: convert 'post-checkout' hook to hook library @@ hook.c: int run_found_hooks(const char *hook_name, const char *hook_path,
## hook.h ##
@@ hook.h: struct run_hooks_opt
-
- /* Number of threads to parallelize across */
+ * implicit 1 for now.
+ */
int jobs;
+
+ /* Resolve and run the "absolute_path(hook)" instead of
8: ad6e750784 = 6: e9fa3f6759 merge: use config-based hooks for post-merge hook 9: 8d8b2d2645 ! 7: 12347d901b git hook run: add an --ignore-missing flag
@@ Documentation/git-hook.txt: git-hook - run git hooks
DESCRIPTION
-----------
-@@ Documentation/git-hook.txt: run::
- "--end-of-options"). See "OPTIONS" below for the arguments
- this accepts.
+@@ Documentation/git-hook.txt: optional -- (or --end-of-options, see linkgit:gitcli[7]). The
+ arguments (if any) differ by hook name, see linkgit:githooks[5] for
+ what those are.
+OPTIONS
+-------
@@ builtin/hook.c: static int run(int argc, const char **argv, const char *prefix)
};
@@ builtin/hook.c: static int run(int argc, const char **argv, const char *prefix)
- hook_name = argv[1];
+ /*
+ * We are not using run_hooks() because we'd like to detect
+ * missing hooks. Let's find it ourselves and call
+- * run_found_hooks() instead.
++ * run_found_hooks() instead...
+ */
+ hook_name = argv[0];
hook_path = find_hook(hook_name);
if (!hook_path) {
++ /* ... act like run_hooks() under --ignore-missing */
+ if (ignore_missing)
+ return 0;
error("cannot find a hook named %s", hook_name);
@@ builtin/hook.c: static int run(int argc, const char **argv, const char *prefix)
}
## t/t1800-hook.sh ##
-@@ t/t1800-hook.sh: test_expect_success 'git hook run -- nonexistent hook' ' +@@ t/t1800-hook.sh: test_expect_success 'git hook run: nonexistent hook' '
test_cmp stderr.expect stderr.actual
'
-+test_expect_success 'git hook run -- nonexistent hook with --ignore-missing' ' +-test_expect_success 'git hook run: basic' ' ++test_expect_success 'git hook run: nonexistent hook with --ignore-missing' '
+ git hook run --ignore-missing does-not-exist 2>stderr.actual &&
+ test_must_be_empty stderr.actual
+'
+
- test_expect_success 'git hook run -- basic' ' ++test_expect_success 'git hook run -- basic' '
write_script .git/hooks/test-hook <<-EOF &&
echo Test hook
+ EOF
10: 1953326d1d = 8: 71d209b407 send-email: use 'git hook run' for 'sendemail-validate' 11: aa970a8175 ! 9: 246a82b55b git-p4: use 'git hook' to run hooks
@@ Commit message
Python, we can directly call 'git hook run'. We emulate the existence check with the --ignore-missing flag.
+ As this is the last hook execution in git.git to not go through "git
+ hook run" or the hook.[ch] library we can now be absolutely sure that
+ our assertion in hook.c that only hooks known by the generated (from
+ githooks(5)) hook-list.h are permitted.
+
12: 7756f10aac = 10: e3f8482d80 commit: use hook.h to execute hooks
13: 0300607a9b = 11: 6ed61071c5 read-cache: convert post-index-change hook to use config
14: ec4ad437f8 = 12: e4ef3f4548 receive-pack: convert push-to-checkout hook to hook.h
15: 477eb2245c = 13: e3dda367ec run-command: remove old run_hook_{le,ve}() hook API
16: 53a3877a47 = 14: 477d75bf57 run-command: allow stdin for run_processes_parallel
17: c4f60db606 ! 15: b7c0ee9719 hook: support passing stdin to hooks @@ Documentation/git-hook.txt: git-hook - run git hooks
DESCRIPTION
-----------
-@@ Documentation/git-hook.txt: run::
+@@ Documentation/git-hook.txt: what those are.
OPTIONS
-------
@@ Documentation/git-hook.txt: run::
## builtin/hook.c ##
@@
- #include "strvec.h"
static const char * const builtin_hook_usage[] = {
+ N_("git hook <command> [...]"),
- N_("git hook run <hook-name> [-- <hook-args>]"),
++ N_("git hook run [<args>] <hook-name> [-- <hook-args>]"),
+ NULL
+ };
+
+ static const char * const builtin_hook_run_usage[] = {
+ N_("git hook run <hook-name> [-- <hook-args>]"),
+ N_("git hook run [--to-stdin=<path>] <hook-name> [-- <hook-args>]"),
NULL
};
@@ builtin/hook.c: static int run(int argc, const char **argv, const char *prefix)
## hook.c ##
@@ hook.c: static int pick_next_hook(struct child_process *cp,
- if (!run_me)
- BUG("did we not return 1 in notify_hook_finished?");
+ struct hook_cb_data *hook_cb = pp_cb;
+ struct hook *run_me = hook_cb->run_me;
- cp->no_stdin = 1;
++
+ /* reopen the file for stdin; run_command closes it. */
+ if (hook_cb->options->path_to_stdin) {
+ cp->no_stdin = 0;
@@ hook.h: struct run_hooks_opt
#define RUN_HOOKS_OPT_INIT { \
## t/t1800-hook.sh ##
-@@ t/t1800-hook.sh: test_expect_success 'set up a pre-commit hook in core.hooksPath' '
- EOF
+@@ t/t1800-hook.sh: test_expect_success 'git -c core.hooksPath=<PATH> hook run' '
+ test_cmp expect actual
'
+test_expect_success 'stdin to hooks' '
18: febf05ef23 = 16: 4035069a98 am: convert 'post-rewrite' hook to hook.h 19: 7baf2469d5 = 17: c847a19581 run-command: add stdin callback for parallelization 20: 2edf9dea41 ! 18: da46c859c1 hook: provide stdin by string_list or callback
@@ hook.c: static int pick_next_hook(struct child_process *cp,
} else {
cp->no_stdin = 1;
}
-@@ hook.c: static int notify_hook_finished(int result,
- return 1;
- }
-
--
- int run_found_hooks(const char *hook_name, const char *hook_path,
- struct run_hooks_opt *options)
- {
@@ hook.c: int run_found_hooks(const char *hook_name, const char *hook_path,
run_processes_parallel_tr2(options->jobs,
pick_next_hook,
@@ hook.c: int run_hooks(const char *hook_name, struct run_hooks_opt *options)
+
hook_path = find_hook(hook_name);
- /* Care about nonexistence? Use run_found_hooks() */
+ /*
## hook.h ##
-@@
+@@ hook.h: int hook_exists(const char *hookname);
struct hook {
/* The path to the hook */
const char *hook_path;
@@ hook.h: struct run_hooks_opt
+ */
+int pipe_from_string_list(struct strbuf *pipe, void *pp_cb, void *pp_task_cb); +
- /*
- * Callback provided to feed_pipe_fn and consume_sideband_fn.
- */
+ struct hook_cb_data {
+ /* rc reflects the cumulative failure state */
+ int rc;
21: 303b31ee62 = 19: 7343be28ef hook: convert 'post-rewrite' hook in sequencer.c to hook.h
22: 62eecafb3f = 20: 85bf13a083 transport: convert pre-push hook to use config
23: 6049b1cdc7 = 21: 331014bad1 reference-transaction: use hook.h to run hooks
24: 26ebbe4c54 = 22: f7f56d0a3b run-command: allow capturing of collated output
25: 251085b752 = 23: 7f7fcc0688 hooks: allow callers to capture output
26: e11a8f6007 = 24: e74d49e559 receive-pack: convert 'update' hook to hook.h
27: ecaedd13b8 = 25: 0bdc4878ac post-update: use hook.h library
28: bb9d57f809 = 26: db70b59b3b receive-pack: convert receive hooks to hook.h
29: 793f112f7a ! 27: d86fedf041 hooks: fix a TOCTOU in "did we run a hook?" heuristic
@@ commit.h: int compare_commits_by_commit_date(const void *a_, const void *b_, voi
## hook.c ##
@@ hook.c: static int notify_hook_finished(int result,
- /* |= rc in cb */
+
hook_cb->rc |= result;
+ if (hook_cb->invoked_hook)
@@ hook.h: struct hook_cb_data {
+ int *invoked_hook;
};
- /*
+ void run_hooks_opt_clear(struct run_hooks_opt *o);
## sequencer.c ##
@@ sequencer.c: static int run_prepare_commit_msg_hook(struct repository *r,30: bc086454d6 < -: ---------- hook-list.h: add a generated list of hooks, like config-list.h
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 1/27 hook: add 'run' subcommand
From: Emily Shaffer <[email protected]>
In order to enable hooks to be run as an external process, by a standalone Git command, or by tools which wrap Git, provide an external means to run all configured hook commands for a given hook event.
Most of our hooks require more complex functionality than this, but let's start with the bare minimum required to support our simplest hooks.
--- .gitignore | 1 + Documentation/git-hook.txt | 38 +++++++++++ Documentation/githooks.txt | 4 ++ Makefile | 1 + builtin.h | 1 + builtin/hook.c | 81 +++++++++++++++++++++++ command-list.txt | 1 + git.c | 1 + hook.c | 119 +++++++++++++++++++++++++++++++++ hook.h | 53 +++++++++++++++ t/t1800-hook.sh | 131 +++++++++++++++++++++++++++++++++++++ 11 files changed, 431 insertions(+) create mode 100644 Documentation/git-hook.txt create mode 100644 builtin/hook.c create mode 100755 t/t1800-hook.sh
.gitignore +1 -0
@@ -77,6 +77,7 @@ /git-grep /git-hash-object /git-help +/git-hook /git-http-backend /git-http-fetch /git-http-push
Documentation/git-hook.txt new file +38 -0
@@ -0,0 +1,38 @@ +git-hook(1) +=========== + +NAME +---- +git-hook - run git hooks + +SYNOPSIS +-------- +[verse] +'git hook' run <hook-name> [-- <hook-args>] + +DESCRIPTION +----------- + +This command is an interface to git hooks (see linkgit:githooks[5]). +Currently it only provides a convenience wrapper for running hooks for +use by git itself. In the future it might gain other functionality. + +SUBCOMMANDS +----------- + +run:: + Run the `<hook-name>` hook. See linkgit:githooks[5] for + the hook names we support. ++ +Any positional arguments to the hook should be passed after an +optional `--` (or `--end-of-options`, see linkgit:gitcli[7]). The +arguments (if any) differ by hook name, see linkgit:githooks[5] for +what those are. + +SEE ALSO +-------- +linkgit:githooks[5] + +GIT +--- +Part of the linkgit:git[1] suite
Documentation/githooks.txt +4 -0
@@ -698,6 +698,10 @@ and "0" meaning they were not. Only one parameter should be set to "1" when the hook runs. The hook running passing "1", "1" should not be possible. +SEE ALSO +-------- +linkgit:git-hook[1] + GIT --- Part of the linkgit:git[1] suite
Makefile +1 -0
@@ -1106,6 +1106,7 @@ BUILTIN_OBJS += builtin/get-tar-commit-id.o BUILTIN_OBJS += builtin/grep.o BUILTIN_OBJS += builtin/hash-object.o BUILTIN_OBJS += builtin/help.o +BUILTIN_OBJS += builtin/hook.o BUILTIN_OBJS += builtin/index-pack.o BUILTIN_OBJS += builtin/init-db.o BUILTIN_OBJS += builtin/interpret-trailers.o
builtin.h +1 -0
@@ -164,6 +164,7 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix);int cmd_grep (int argc,const char **argv,const char *prefix);int cmd_hash_object (int argc,const char **argv,const char *prefix);int cmd_help (int argc,const char **argv,const char *prefix); +int cmd_hook (int argc,const char **argv,const char *prefix);int cmd_index_pack (int argc,const char **argv,const char *prefix);int cmd_init_db (int argc,const char **argv,const char *prefix);int cmd_interpret_trailers (int argc,const char **argv,const char *prefix);
builtin/hook.c new file +81 -0
81 lines not on this page · raw message
command-list.txt +1 -0
@@ -103,6 +103,7 @@ git-grep mainporcelain info git-gui mainporcelain git-hash-object plumbingmanipulators git-help ancillaryinterrogators complete +git-hook mainporcelain git-http-backend synchingrepositories git-http-fetch synchelpers git-http-push synchelpers
git.c +1 -0
@@ -538,6 +538,7 @@ static struct cmd_struct commands[] = { { "grep", cmd_grep, RUN_SETUP_GENTLY }, { "hash-object", cmd_hash_object }, { "help", cmd_help }, + { "hook", cmd_hook, RUN_SETUP }, { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY | NO_PARSEOPT }, { "init", cmd_init_db }, { "init-db", cmd_init_db },
hook.c +119 -0
139 lines not on this page · raw message
hook.h +53 -0
62 lines not on this page · raw message
t/t1800-hook.sh new file +131 -0
131 lines not on this page · raw message
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 2/27 gc: use hook library for pre-auto-gc hook
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
Using the hook.h library instead of the run-command.h library to run pre-auto-gc means that those hooks can be set up in config files, as well as in the hookdir. pre-auto-gc is called only from builtin/gc.c.
--- builtin/gc.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
builtin/gc.c +7 -1
@@ -32,6 +32,7 @@#include "remote.h" #include "object-store.h" #include "exec-cmd.h" +#include "hook.h" #define FAILED_RUN "failed to run %s"
@@ -348,6 +349,8 @@ static void add_repack_incremental_option(void)static int need_to_gc (void ) { +struct run_hooks_opt hook_opt =RUN_HOOKS_OPT_INIT ; +/* * Setting gc.auto to 0 or negative can disable the * automatic gc.
@@ -394,8 +397,11 @@ static int need_to_gc(void)else return 0 ; -if (run_hook_le (NULL ,"pre-auto-gc" ,NULL )) +if (run_hooks ("pre-auto-gc" , &hook_opt)) { +run_hooks_opt_clear (&hook_opt);return 0 ; + } +run_hooks_opt_clear (&hook_opt);return 1 ; }
Signature · 1 line
2.32.0.576.g59759b6ca7d
Re: [PATCH 02/27] gc: use hook library for pre-auto-gc hook
Quoted 5 lines · Ævar Arnfjörð Bjarmason
Using the
hook.hlibrary instead of therun-command.hlibrary to run pre-auto-gc means that those hooks can be set up in config files, as well as in the hookdir. pre-auto-gc is called only frombuiltin/gc.c.
This one is a good example of an issue Junio pointed out to me in an earlier iteration - since the strvecs in run_hooks_opt need to be cleared, you need to be careful with exiting without running run_hooks_opt_clear(). need_to_gc() can exit before running the hook in some cases.
- Emily
Quoted 44 lines
Signed-off-by Emily Shaffer <[email protected]>Signed-off-by Ævar Arnfjörð Bjarmason <[email protected]>--- builtin/gc.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)builtin/gc.c +7
-1@@ -32,6 +32,7 @@#include "remote.h" #include "object-store.h" #include "exec-cmd.h" +#include "hook.h" #define FAILED_RUN "failed to run %s"@@ -348,6 +349,8 @@ static void add_repack_incremental_option(void)static int need_to_gc (void ) { +struct run_hooks_opt hook_opt =RUN_HOOKS_OPT_INIT ; +/* * Setting gc.auto to 0 or negative can disable the * automatic gc. @@ -394,8 +397,11 @@ static int need_to_gc(void)else return 0 ; -if (run_hook_le (NULL ,"pre-auto-gc" ,NULL )) +if (run_hooks ("pre-auto-gc" , &hook_opt)) { +run_hooks_opt_clear (&hook_opt);return 0 ; + } +run_hooks_opt_clear (&hook_opt);return 1 ; }Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 3/27 rebase: teach pre-rebase to use hook.h
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
Move the pre-rebase hook away from run-command.h to and over to the new hook.h library.
--- builtin/rebase.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
builtin/rebase.c +7 -2
@@ -28,6 +28,7 @@#include "sequencer.h" #include "rebase-interactive.h" #include "reset.h" +#include "hook.h" #define DEFAULT_REFLOG_ACTION "rebase"
@@ -1313,6 +1314,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)char *squash_onto_name =NULL ;int reschedule_failed_exec =-1 ;int allow_preemptive_ff =1 ; +struct run_hooks_opt hook_opt =RUN_HOOKS_OPT_INIT ;struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, N_("revision"),
@@ -2022,10 +2024,13 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) }/* If a hook exists, give it a chance to interrupt*/ +strvec_pushl (&hook_opt.args, options.upstream_arg, argc ? argv[0 ] :NULL ,NULL );if (!ok_to_skip_pre_rebase && -run_hook_le (NULL ,"pre-rebase" , options.upstream_arg, - argc ? argv[0 ] :NULL ,NULL )) +run_hooks ("pre-rebase" , &hook_opt)) { +run_hooks_opt_clear (&hook_opt);die (_ ("The pre-rebase hook refused to rebase." )); + } +run_hooks_opt_clear (&hook_opt);if (options.flags &REBASE_DIFFSTAT ) {struct diff_options opts;
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 4/27 am: convert applypatch hooks to use config
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
Teach pre-applypatch, post-applypatch, and applypatch-msg to use the hook.h library instead of the run-command.h library.
--- builtin/am.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
builtin/am.c +12 -3
@@ -445,9 +445,12 @@ static void am_destroy(const struct am_state *state)static int run_applypatch_msg_hook (struct am_state *state) {int ret; +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ;assert (state->msg); - ret =run_hook_le (NULL ,"applypatch-msg" ,am_path (state,"final-commit" ),NULL ); +strvec_push (&opt.args,am_path (state,"final-commit" )); + ret =run_hooks ("applypatch-msg" , &opt); +run_hooks_opt_clear (&opt);if (!ret) {FREE_AND_NULL (state->msg);
@@ -1607,9 +1610,13 @@ static void do_commit(const struct am_state *state)struct commit_list *parents =NULL ;const char *reflog_msg, *author, *committer =NULL ;struct strbuf sb =STRBUF_INIT ; +struct run_hooks_opt hook_opt_pre =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt hook_opt_post =RUN_HOOKS_OPT_INIT ; -if (run_hook_le (NULL ,"pre-applypatch" ,NULL )) +if (run_hooks ("pre-applypatch" , &hook_opt_pre)) { +run_hooks_opt_clear (&hook_opt_pre);exit (1 ); + }if (write_cache_as_tree (&tree,0 ,NULL ))die (_ ("git write-tree failed to write a tree" ));
@@ -1660,8 +1667,10 @@ static void do_commit(const struct am_state *state)fclose (fp); } -run_hook_le (NULL ,"post-applypatch" ,NULL ); +run_hooks ("post-applypatch" , &hook_opt_post); +run_hooks_opt_clear (&hook_opt_pre); +run_hooks_opt_clear (&hook_opt_post);strbuf_release (&sb); }
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 5/27 hooks: convert 'post-checkout' hook to hook library
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
Move the running of the 'post-checkout' hook away from run-command.h to the new hook.h library. For "worktree" this requires a change to it to run the hooks from a given directory.
We could strictly speaking skip the "absolute_path" flag and just check if "dir" is specified, but let's split them up for clarity, as well as for any future user who'd like to set "dir" but not implicitly change the argument to an absolute path.
--- builtin/checkout.c | 17 ++++++++++++----- builtin/clone.c | 7 +++++-- builtin/worktree.c | 30 ++++++++++++++---------------- hook.c | 8 ++++++++ hook.h | 9 +++++++++ read-cache.c | 1 + reset.c | 15 +++++++++++---- 7 files changed, 60 insertions(+), 27 deletions(-)
builtin/checkout.c +12 -5
@@ -9,6 +9,7 @@#include "config.h" #include "diff.h" #include "dir.h" +#include "hook.h" #include "ll-merge.h" #include "lockfile.h" #include "merge-recursive.h"
@@ -106,13 +107,19 @@ 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" , -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 ); +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +int rc; +/* "new_commit" can be NULL when checking out from the index before a commit exists. */ - +strvec_pushl (&opt.args, +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 ); + rc =run_hooks ("post-checkout" , &opt); +run_hooks_opt_clear (&opt); +return rc; } static int update_some(const struct object_id *oid, struct strbuf *base,
builtin/clone.c +5 -2
@@ -32,6 +32,7 @@#include "connected.h" #include "packfile.h" #include "list-objects-filter-options.h" +#include "hook.h" /* * Overall FIXMEs:
@@ -775,6 +776,7 @@ static int checkout(int submodule_progress)struct tree *tree;struct tree_desc t;int err =0 ; +struct run_hooks_opt hook_opt =RUN_HOOKS_OPT_INIT ;if (option_no_checkout)return 0 ;
@@ -820,8 +822,9 @@ 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 ()), -oid_to_hex (&oid),"1" ,NULL ); +strvec_pushl (&hook_opt.args,oid_to_hex (null_oid ()),oid_to_hex (&oid),"1" ,NULL ); + err |=run_hooks ("post-checkout" , &hook_opt); +run_hooks_opt_clear (&hook_opt);if (!err && (option_recurse_submodules.nr >0 )) {struct strvec args =STRVEC_INIT ;
builtin/worktree.c +14 -16
@@ -382,22 +382,20 @@ static int add_worktree(const char *path, const char *refname,* is_junk is cleared, but do return appropriate code when hook fails. */ if (!ret && opts->checkout) { -const char *hook =find_hook ("post-checkout" ); -if (hook) { -const char *env[] = {"GIT_DIR" ,"GIT_WORK_TREE" ,NULL }; - cp.git_cmd =0 ; - cp.no_stdin =1 ; - cp.stdout_to_stderr =1 ; - cp.dir = path; - cp.env = env; - cp.argv =NULL ; - cp.trace2_hook_name ="post-checkout" ; -strvec_pushl (&cp.args,absolute_path (hook), -oid_to_hex (null_oid ()), -oid_to_hex (&commit->object.oid), -"1" ,NULL ); - ret =run_command (&cp); - } +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; + +strvec_pushl (&opt.env,"GIT_DIR" ,"GIT_WORK_TREE" ,NULL ); +strvec_pushl (&opt.args, +oid_to_hex (null_oid ()), +oid_to_hex (&commit->object.oid), +"1" , +NULL ); + opt.dir = path; + opt.absolute_path =1 ; + + ret =run_hooks ("post-checkout" , &opt); + +run_hooks_opt_clear (&opt); }strvec_clear (&child_env);
hook.c +8 -0
@@ -89,6 +89,7 @@ static int pick_next_hook(struct child_process *cp, cp->env = hook_cb->options->env.v; cp->stdout_to_stderr =1 ; cp->trace2_hook_name = hook_cb->hook_name; + cp->dir = hook_cb->options->dir;/* add command */ strvec_push (&cp->args, run_me->hook_path);
@@ -135,6 +136,7 @@ static int notify_hook_finished(int result,int run_found_hooks (const char *hook_name,const char *hook_path,struct run_hooks_opt *options) { +struct strbuf abs_path =STRBUF_INIT ;struct hook my_hook = { .hook_path = hook_path, };
@@ -143,6 +145,10 @@ int run_found_hooks(const char *hook_name, const char *hook_path, .hook_name = hook_name, .options = options, }; +if (options->absolute_path) { +strbuf_add_absolute_path (&abs_path, hook_path); + my_hook.hook_path = abs_path.buf; + } cb_data.run_me = &my_hook;if (options->jobs !=1 )
@@ -155,6 +161,8 @@ int run_found_hooks(const char *hook_name, const char *hook_path, &cb_data,"hook" , hook_name); +if (options->absolute_path) +strbuf_release (&abs_path);return cb_data.rc; }
hook.h +9 -0
@@ -36,6 +36,15 @@ struct run_hooks_opt* implicit 1 for now. */ int jobs; + +/* Resolve and run the "absolute_path(hook)" instead of +* "hook". Used for "git worktree" hooks +*/ +int absolute_path; + +/* Path to initial working directory for subprocess */ +const char *dir; + };#define RUN_HOOKS_OPT_INIT { \
read-cache.c +1 -0
@@ -27,6 +27,7 @@#include "progress.h" #include "sparse-index.h" #include "csum-file.h" +#include "hook.h" /* Mask for the name length in ce_flags in the on-disk index */
reset.c +11 -4
@@ -7,6 +7,7 @@#include "tree-walk.h" #include "tree.h" #include "unpack-trees.h" +#include "hook.h" int reset_head(struct repository *r, struct object_id *oid, const char *action, const char *switch_to_branch, unsigned flags,
@@ -126,10 +127,16 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action, ret =create_symref ("HEAD" , switch_to_branch, reflog_head); } -if (run_hook) -run_hook_le (NULL ,"post-checkout" , -oid_to_hex (orig ? orig :null_oid ()), -oid_to_hex (oid),"1" ,NULL ); +if (run_hook) { +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +strvec_pushl (&opt.args, +oid_to_hex (orig ? orig :null_oid ()), +oid_to_hex (oid), +"1" , +NULL ); +run_hooks ("post-checkout" , &opt); +run_hooks_opt_clear (&opt); + } leave_reset_head:strbuf_release (&msg);
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 6/27 merge: use config-based hooks for post-merge hook
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
Teach post-merge to use the hook.h library instead of the run-command.h library to run hooks. This means that post-merge hooks can come from the config as well as from the hookdir. post-merge is invoked only from builtin/merge.c.
--- builtin/merge.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
builtin/merge.c +5 -2
@@ -448,6 +448,7 @@ static void finish(struct commit *head_commit, const struct object_id *new_head, const char *msg) { struct strbuf reflog_message = STRBUF_INIT; +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ;const struct object_id *head = &head_commit->object.oid;if (!msg)
@@ -489,7 +490,9 @@ static void finish(struct commit *head_commit, }/* Run a post-merge hook */ -run_hook_le (NULL ,"post-merge" , squash ?"1" :"0" ,NULL ); +strvec_push (&opt.args, squash ?"1" :"0" ); +run_hooks ("post-merge" , &opt); +run_hooks_opt_clear (&opt);apply_autostash (git_path_merge_autostash (the_repository));strbuf_release (&reflog_message);
@@ -849,7 +852,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)* and write it out as a tree. We must do this before we invoke * the editor and after we invoke run_status above. */ -if (find_hook ("pre-merge-commit" )) +if (hook_exists ("pre-merge-commit" ))discard_cache ();read_cache_from (index_file);strbuf_addbuf (&msg, &merge_msg);
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 7/27 git hook run: add an --ignore-missing flag
In reply to Ævar Arnfjörð BjarmasonFor certain one-shot hooks we'd like to optimistically run them, and not complain if they don't exist. This will be used by send-email in a subsequent commit.
--- Documentation/git-hook.txt | 10 +++++++++- builtin/hook.c | 8 +++++++- t/t1800-hook.sh | 7 ++++++- 3 files changed, 22 insertions(+), 3 deletions(-)
Documentation/git-hook.txt +9 -1
@@ -8,7 +8,7 @@ git-hook - run git hooks SYNOPSIS -------- [verse] -'git hook' run <hook-name> [-- <hook-args>] +'git hook' run [--ignore-missing] <hook-name> [-- <hook-args>] DESCRIPTION -----------
@@ -29,6 +29,14 @@ optional `--` (or `--end-of-options`, see linkgit:gitcli[7]). The arguments (if any) differ by hook name, see linkgit:githooks[5] for what those are. +OPTIONS +------- + +--ignore-missing:: + Ignore any missing hook by quietly returning zero. Used for + tools that want to do a blind one-shot run of a hook that may + or may not be present. + SEE ALSO -------- linkgit:githooks[5]
builtin/hook.c +7 -1
@@ -22,10 +22,13 @@ static int run(int argc, const char **argv, const char *prefix)int i;struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ;int rc =0 ; +int ignore_missing =0 ;const char *hook_name;const char *hook_path;struct option run_options[] = { +OPT_BOOL (0 ,"ignore-missing" , &ignore_missing, +N_ ("exit quietly with a zero exit code if the requested hook cannot be found" )),OPT_END (), };
@@ -49,11 +52,14 @@ static int run(int argc, const char **argv, const char *prefix)/* * We are not using run_hooks() because we'd like to detect * missing hooks. Let's find it ourselves and call -* run_found_hooks() instead. +* run_found_hooks() instead... */ hook_name = argv[0 ]; hook_path =find_hook (hook_name);if (!hook_path) { +/* ... act like run_hooks() under --ignore-missing */ +if (ignore_missing) +return 0 ;error ("cannot find a hook named %s" , hook_name);return 1 ; }
t/t1800-hook.sh +6 -1
@@ -23,7 +23,12 @@ test_expect_success 'git hook run: nonexistent hook' ' test_cmp stderr.expect stderr.actual ' -test_expect_success 'git hook run: basic' ' +test_expect_success 'git hook run: nonexistent hook with --ignore-missing' ' + git hook run --ignore-missing does-not-exist 2>stderr.actual && + test_must_be_empty stderr.actual +' + +test_expect_success 'git hook run -- basic' ' write_script .git/hooks/test-hook <<-EOF && echo Test hook EOF
Signature · 1 line
2.32.0.576.g59759b6ca7d
Re: [PATCH 07/27] git hook run: add an --ignore-missing flag
Quoted 64 lines · Ævar Arnfjörð Bjarmason
For certain one-shot hooks we'd like to optimistically run them, and not complain if they don't exist. This will be used by send-email in a subsequent commit.
Signed-off-by Ævar Arnfjörð Bjarmason <[email protected]>--- Documentation/git-hook.txt | 10 +++++++++- builtin/hook.c | 8 +++++++- t/t1800-hook.sh | 7 ++++++- 3 files changed, 22 insertions(+), 3 deletions(-)Documentation/git-hook.txt +9
-1@@ -8,7 +8,7 @@ git-hook - run git hooks SYNOPSIS -------- [verse] -'git hook' run <hook-name> [-- <hook-args>] +'git hook' run [--ignore-missing] <hook-name> [-- <hook-args>] DESCRIPTION -----------@@ -29,6 +29,14 @@ optional `--` (or `--end-of-options`, see linkgit:gitcli[7]). The arguments (if any) differ by hook name, see linkgit:githooks[5] for what those are. +OPTIONS +------- + +--ignore-missing:: + Ignore any missing hook by quietly returning zero. Used for + tools that want to do a blind one-shot run of a hook that may + or may not be present. + SEE ALSO -------- linkgit:githooks[5]builtin/hook.c +4
-1@@ -22,10 +22,13 @@ static int run(int argc, const char **argv, const char *prefix)int i;struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ;int rc =0 ; +int ignore_missing =0 ;const char *hook_name;const char *hook_path;struct option run_options[] = { +OPT_BOOL (0 ,"ignore-missing" , &ignore_missing, +N_ ("exit quietly with a zero exit code if the requested hook cannot be found" )),OPT_END (), };@@ -49,11 +52,14 @@ static int run(int argc, const char **argv, const char *prefix)/* * We are not using run_hooks() because we'd like to detect * missing hooks. Let's find it ourselves and call -* run_found_hooks() instead. +* run_found_hooks() instead...
builtin/hook.c static int run(int argc, const char **argv, const char *prefix) · line 65
Quoted 10 lines
builtin/hook.c +3
-0*/ hook_name = argv[0 ]; hook_path =find_hook (hook_name);if (!hook_path) { +/* ... act like run_hooks() under --ignore-missing */ +if (ignore_missing) +return 0 ;error ("cannot find a hook named %s" , hook_name);return 1 ; }
Have started to rebase the configgy stuff on top of this series, and the final implementation here is kind of confusing to me. The difference between run_hooks() and run_found_hooks() is the behavior when there's no hook to run; but you're checking for missing hooks before you even let them try to run here in builtin/hook.c. So is it really necessary to have the two methods? I think I'm missing something?
Quoted 21 lines
t/t1800-hook.sh +6
-1@@ -23,7 +23,12 @@ test_expect_success 'git hook run: nonexistent hook' ' test_cmp stderr.expect stderr.actual ' -test_expect_success 'git hook run: basic' ' +test_expect_success 'git hook run: nonexistent hook with --ignore-missing' ' + git hook run --ignore-missing does-not-exist 2>stderr.actual && + test_must_be_empty stderr.actual +' + +test_expect_success 'git hook run -- basic' ' write_script .git/hooks/test-hook <<-EOF && echo Test hook EOFSignature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 8/27 send-email: use 'git hook run' for 'sendemail-validate'
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
Change the "sendmail-validate" hook to be run via the "git hook run" wrapper instead of via a direct invocation.
This is the smallest possibly change to get "send-email" using "git hook run". We still check the hook itself with "-x", and set a "GIT_DIR" variable, both of which are asserted by our tests. We'll need to get rid of this special behavior if we start running N hooks, but for now let's be as close to bug-for-bug compatible as possible.
--- git-send-email.perl | 20 ++++++++++++-------- t/t9001-send-email.sh | 4 ++-- 2 files changed, 14 insertions(+), 10 deletions(-)
git-send-email.perl +12 -8
@@ -214,13 +214,13 @@ sub format_2822_time { my $editor; sub system_or_msg { - my ($args, $msg) = @_; + my ($args, $msg, $cmd_name) = @_; system(@$args); my $signalled = $? & 127; my $exit_code = $? >> 8; return unless $signalled or $exit_code; - my @sprintf_args = ($args->[0], $exit_code); + my @sprintf_args = ($cmd_name ? $cmd_name : $args->[0], $exit_code); if (defined $msg) { # Quiet the 'redundant' warning category, except we # need to support down to Perl 5.8, so we can't do a
@@ -1979,9 +1979,9 @@ sub validate_patch { my ($fn, $xfer_encoding) = @_; if ($repo) { + my $hook_name = 'sendemail-validate'; my $hooks_path = $repo->command_oneline('rev-parse', '--git-path', 'hooks'); - my $validate_hook = catfile($hooks_path, - 'sendemail-validate'); + my $validate_hook = catfile($hooks_path, $hook_name); my $hook_error; if (-x $validate_hook) { my $target = abs_path($fn);
@@ -1990,13 +1990,17 @@ sub validate_patch { chdir($repo->wc_path() or $repo->repo_path()) or die("chdir: $!"); local $ENV{"GIT_DIR"} = $repo->repo_path(); - $hook_error = system_or_msg([$validate_hook, $target]); + my @validate_hook = ("git", "hook", "run", "--ignore-missing", $hook_name, "--", $target); + $hook_error = system_or_msg(\@validate_hook, undef, + "git hook run $hook_name -- <patch>"); chdir($cwd_save) or die("chdir: $!"); } if ($hook_error) { - die sprintf(__("fatal: %s: rejected by sendemail-validate hook\n" . - "%s\n" . - "warning: no patches were sent\n"), $fn, $hook_error); + $hook_error = sprintf(__("fatal: %s: rejected by %s hook\n" . + $hook_error . "\n" . + "warning: no patches were sent\n"), + $fn, $hook_name); + die $hook_error; } }
t/t9001-send-email.sh +2 -2
@@ -539,7 +539,7 @@ test_expect_success $PREREQ "--validate respects relative core.hooksPath path" ' test_path_is_file my-hooks.ran && cat >expect <<-EOF && fatal: longline.patch: rejected by sendemail-validate hook - fatal: command '"'"'my-hooks/sendemail-validate'"'"' died with exit code 1 + fatal: command '"'"'git hook run sendemail-validate -- <patch>'"'"' died with exit code 1 warning: no patches were sent EOF test_cmp expect actual
@@ -558,7 +558,7 @@ test_expect_success $PREREQ "--validate respects absolute core.hooksPath path" ' test_path_is_file my-hooks.ran && cat >expect <<-EOF && fatal: longline.patch: rejected by sendemail-validate hook - fatal: command '"'"'$hooks_path/sendemail-validate'"'"' died with exit code 1 + fatal: command '"'"'git hook run sendemail-validate -- <patch>'"'"' died with exit code 1 warning: no patches were sent EOF test_cmp expect actual
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 9/27 git-p4: use 'git hook' to run hooks
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
Instead of duplicating the behavior of run-command.h:run_hook_le() in Python, we can directly call 'git hook run'. We emulate the existence check with the --ignore-missing flag.
As this is the last hook execution in git.git to not go through "git hook run" or the hook.[ch] library we can now be absolutely sure that our assertion in hook.c that only hooks known by the generated (from githooks(5)) hook-list.h are permitted.
--- git-p4.py | 72 ++++++------------------------------------------------- 1 file changed, 7 insertions(+), 65 deletions(-)
git-p4.py +7 -65
@@ -207,71 +207,13 @@ def decode_path(path): return path def run_git_hook(cmd, param=[]): - """Execute a hook if the hook exists.""" - if verbose: - sys.stderr.write("Looking for hook: %s\n" % cmd) - sys.stderr.flush() - - hooks_path = gitConfig("core.hooksPath") - if len(hooks_path) <= 0: - hooks_path = os.path.join(os.environ["GIT_DIR"], "hooks") - - if not isinstance(param, list): - param=[param] - - # resolve hook file name, OS depdenent - hook_file = os.path.join(hooks_path, cmd) - if platform.system() == 'Windows': - if not os.path.isfile(hook_file): - # look for the file with an extension - files = glob.glob(hook_file + ".*") - if not files: - return True - files.sort() - hook_file = files.pop() - while hook_file.upper().endswith(".SAMPLE"): - # The file is a sample hook. We don't want it - if len(files) > 0: - hook_file = files.pop() - else: - return True - - if not os.path.isfile(hook_file) or not os.access(hook_file, os.X_OK): - return True - - return run_hook_command(hook_file, param) == 0 - -def run_hook_command(cmd, param): - """Executes a git hook command - cmd = the command line file to be executed. This can be - a file that is run by OS association. - - param = a list of parameters to pass to the cmd command - - On windows, the extension is checked to see if it should - be run with the Git for Windows Bash shell. If there - is no file extension, the file is deemed a bash shell - and will be handed off to sh.exe. Otherwise, Windows - will be called with the shell to handle the file assocation. - - For non Windows operating systems, the file is called - as an executable. - """ - cli = [cmd] + param - use_shell = False - if platform.system() == 'Windows': - (root,ext) = os.path.splitext(cmd) - if ext == "": - exe_path = os.environ.get("EXEPATH") - if exe_path is None: - exe_path = "" - else: - exe_path = os.path.join(exe_path, "bin") - cli = [os.path.join(exe_path, "SH.EXE")] + cli - else: - use_shell = True - return subprocess.call(cli, shell=use_shell) - + """args are specified with -a <arg> -a <arg> -a <arg>""" + args = ['git', 'hook', 'run', '--ignore-missing', cmd] + if param: + args.append("--") + for p in param: + args.append(p) + return subprocess.call(args) == 0 def write_pipe(c, stdin): if verbose:
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 10/27 commit: use hook.h to execute hooks
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
Teach run_commit_hook() to call hook.h instead of run-command.h. This covers 'pre-commit', 'commit-msg', and 'prepare-commit-msg'.
Additionally, ask the hook library - not run-command - whether any hooks will be run, as it's possible hooks may exist in the config but not the hookdir.
Because all but 'post-commit' hooks are expected to make some state change, force all but 'post-commit' hook to run in series. 'post-commit' "is meant primarily for notification, and cannot affect the outcome of git commit," so it is fine to run in parallel.
--- builtin/commit.c | 2 +- commit.c | 16 ++++++++++------ sequencer.c | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-)
builtin/commit.c +1 -1
@@ -1045,7 +1045,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,return 0 ; } -if (!no_verify &&find_hook ("pre-commit" )) { +if (!no_verify &&hook_exists ("pre-commit" )) {/* * Re-read the index as pre-commit hook could have updated it, * and write it out as a tree. We must do this before we invoke
commit.c +10 -6
@@ -21,6 +21,7 @@#include "commit-reach.h" #include "run-command.h" #include "shallow.h" +#include "hook.h" static struct commit_extra_header *read_commit_extra_header_lines (const char *buf,size_t len,const char **);
@@ -1698,22 +1699,25 @@ 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, ...) { -struct strvec hook_env =STRVEC_INIT ; +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ;va_list args; +const char *arg;int ret; - -strvec_pushf (&hook_env,"GIT_INDEX_FILE=%s" , index_file); +strvec_pushf (&opt.env,"GIT_INDEX_FILE=%s" , index_file);/* * Let the hook know that no editor will be launched. */ if (!editor_is_used) -strvec_push (&hook_env,"GIT_EDITOR=:" ); +strvec_push (&opt.env,"GIT_EDITOR=:" );va_start (args, name); - ret =run_hook_ve (hook_env.v, name, args); + while ((arg = va_arg(args, const char *))) +strvec_push (&opt.args, arg);va_end (args); -strvec_clear (&hook_env); + + ret =run_hooks (name, &opt); +run_hooks_opt_clear (&opt);return ret; }
sequencer.c +1 -1
@@ -1446,7 +1446,7 @@ static int try_to_commit(struct repository *r, } } -if (find_hook ("prepare-commit-msg" )) { +if (hook_exists ("prepare-commit-msg" )) { res =run_prepare_commit_msg_hook (r, msg, hook_commit);if (res) goto out;
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 11/27 read-cache: convert post-index-change hook to use config
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
By using hook.h instead of run-command.h to run, post-index-change hooks can now be specified in the config in addition to the hookdir. post-index-change is not run anywhere besides in read-cache.c.
This removes the last direct user of run_hook_ve(), so we can make the function static now. It'll be removed entirely soon.
--- read-cache.c | 11 ++++++++--- run-command.c | 2 +- run-command.h | 1 - 3 files changed, 9 insertions(+), 5 deletions(-)
read-cache.c +8 -3
@@ -3063,6 +3063,7 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l {int ret;int was_full = !istate->sparse_index; +struct run_hooks_opt hook_opt =RUN_HOOKS_OPT_INIT ; ret =convert_to_sparse (istate);
@@ -3091,9 +3092,13 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *lelse ret =close_lock_file_gently (lock); -run_hook_le (NULL ,"post-index-change" , - istate->updated_workdir ?"1" :"0" , - istate->updated_skipworktree ?"1" :"0" ,NULL ); +strvec_pushl (&hook_opt.args, + istate->updated_workdir ?"1" :"0" , + istate->updated_skipworktree ?"1" :"0" , +NULL ); +run_hooks ("post-index-change" , &hook_opt); +run_hooks_opt_clear (&hook_opt); + istate->updated_workdir =0 ; istate->updated_skipworktree =0 ;
run-command.c +1 -1
@@ -1321,7 +1321,7 @@ int async_with_fork(void)#endif } -int run_hook_ve (const char *const *env,const char *name,va_list args) +static int run_hook_ve (const char *const *env,const char *name,va_list args) {struct child_process hook =CHILD_PROCESS_INIT ;const char *p;
run-command.h +0 -1
@@ -216,7 +216,6 @@ int run_command(struct child_process *);*/ 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);/* * Trigger an auto-gc
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 12/27 receive-pack: convert push-to-checkout hook to hook.h
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
By using hook.h instead of run-command.h to invoke push-to-checkout, hooks can now be specified in the config as well as in the hookdir. push-to-checkout is not called anywhere but in builtin/receive-pack.c.
--- builtin/receive-pack.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
builtin/receive-pack.c +10 -4
@@ -1436,12 +1436,18 @@ static const char *push_to_checkout(unsigned char *hash,struct strvec *env, const char *work_tree) { + struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; +strvec_pushf (env,"GIT_WORK_TREE=%s" ,absolute_path (work_tree)); -if (run_hook_le (env->v, push_to_checkout_hook, -hash_to_hex (hash),NULL )) +strvec_pushv (&opt.env, env->v); +strvec_push (&opt.args,hash_to_hex (hash)); +if (run_hooks (push_to_checkout_hook, &opt)) { +run_hooks_opt_clear (&opt);return "push-to-checkout hook declined" ; -else + }else { +run_hooks_opt_clear (&opt);return NULL ; + } }static const char *update_worktree (unsigned char *sha1,const struct worktree *worktree)
@@ -1465,7 +1471,7 @@ static const char *update_worktree(unsigned char *sha1, const struct worktree *wstrvec_pushf (&env,"GIT_DIR=%s" ,absolute_path (git_dir)); -if (!find_hook (push_to_checkout_hook)) +if (!hook_exists (push_to_checkout_hook)) retval =push_to_deploy (sha1, &env, work_tree);else retval =push_to_checkout (sha1, &env, work_tree);
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 13/27 run-command: remove old run_hook_{le,ve}() hook API
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
The new hook.h library has replaced all run-command.h hook-related functionality. So let's delete this dead code.
--- run-command.c | 32 -------------------------------- run-command.h | 16 ---------------- 2 files changed, 48 deletions(-)
run-command.c +0 -32
@@ -1321,38 +1321,6 @@ int async_with_fork(void)#endif } -static int run_hook_ve (const char *const *env,const char *name,va_list args) -{ -struct child_process hook =CHILD_PROCESS_INIT ; -const char *p; - - p =find_hook (name); -if (!p) -return 0 ; - -strvec_push (&hook.args, p); - 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; - -return run_command (&hook); -} - -int run_hook_le (const char *const *env,const char *name, ...) -{ -va_list args; -int ret; - -va_start (args, name); - ret =run_hook_ve (env, name, args); -va_end (args); - -return ret; -} -struct io_pump {/* initialized by caller */ int fd;
run-command.h +0 -16
@@ -201,22 +201,6 @@ int finish_command_in_signal(struct child_process *);*/ int run_command (struct child_process *); -/** -* 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 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. -*/ -LAST_ARG_MUST_BE_NULL -int run_hook_le(const char *const *env, const char *name, ...); -/* * Trigger an auto-gc */
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 14/27 run-command: allow stdin for run_processes_parallel
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
While it makes sense not to inherit stdin from the parent process to avoid deadlocking, it's not necessary to completely ban stdin to children. An informed user should be able to configure stdin safely. By setting some_child.process.no_stdin=1 before calling get_next_task() we provide a reasonable default behavior but enable users to set up stdin streaming for themselves during the callback.
some_child.process.stdout_to_stderr, however, remains unmodifiable by get_next_task() - the rest of the run_processes_parallel() API depends on child output in stderr.
--- run-command.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
run-command.c +8 -1
@@ -1628,6 +1628,14 @@ static int pp_start_one(struct parallel_processes *pp)if (i == pp->max_processes)BUG ("bookkeeping is hard" ); +/* +* By default, do not inherit stdin from the parent process - otherwise, +* all children would share stdin! Users may overwrite this to provide +* something to the child's stdin by having their 'get_next_task' +* callback assign 0 to .no_stdin and an appropriate integer to .in. +*/ + pp->children[i].process.no_stdin =1 ; + code = pp->get_next_task(&pp->children[i].process, &pp->children[i].err, pp->data,
@@ -1639,7 +1647,6 @@ static int pp_start_one(struct parallel_processes *pp) } pp->children[i].process.err =-1 ; pp->children[i].process.stdout_to_stderr =1 ; - pp->children[i].process.no_stdin =1 ;if (start_command (&pp->children[i].process)) { code = pp->start_failure(&pp->children[i].err,
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 15/27 hook: support passing stdin to hooks
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
Some hooks (such as post-rewrite) need to take input via stdin. Previously, callers provided stdin to hooks by setting run-command.h:child_process.in, which takes a FD. Callers would open the file in question themselves before calling run-command(). However, since we will now need to seek to the front of the file and read it again for every hook which runs, hook.h:run_command() takes a path and handles FD management itself. Since this file is opened for read only, it should not prevent later parallel execution support.
On the frontend, this is supported by asking for a file path, rather than by reading stdin. Reading directly from stdin would involve caching the entire stdin (to memory or to disk) and reading it back from the beginning to each hook. We'd want to support cases like insufficient memory or storage for the file. While this may prove useful later, for now the path of least resistance is to just ask the user to make this interim file themselves.
--- Documentation/git-hook.txt | 7 ++++++- builtin/hook.c | 5 ++++- hook.c | 9 ++++++++- hook.h | 2 ++ t/t1800-hook.sh | 18 ++++++++++++++++++ 5 files changed, 38 insertions(+), 3 deletions(-)
Documentation/git-hook.txt +6 -1
@@ -8,7 +8,7 @@ git-hook - run git hooks SYNOPSIS -------- [verse] -'git hook' run [--ignore-missing] <hook-name> [-- <hook-args>] +'git hook' run [--to-stdin=<path>] [--ignore-missing] <hook-name> [-- <hook-args>] DESCRIPTION -----------
@@ -32,6 +32,11 @@ what those are. OPTIONS ------- +--to-stdin:: + For "run"; Specify a file which will be streamed into the + hook's stdin. The hook will receive the entire file from + beginning to EOF. + --ignore-missing:: Ignore any missing hook by quietly returning zero. Used for tools that want to do a blind one-shot run of a hook that may
builtin/hook.c +4 -1
@@ -8,12 +8,13 @@static const char *const builtin_hook_usage[] = {N_ ("git hook <command> [...]" ), -N_ ("git hook run <hook-name> [-- <hook-args>]" ), +N_ ("git hook run [<args>] <hook-name> [-- <hook-args>]" ),NULL };static const char *const builtin_hook_run_usage[] = {N_ ("git hook run <hook-name> [-- <hook-args>]" ), +N_ ("git hook run [--to-stdin=<path>] <hook-name> [-- <hook-args>]" ),NULL };
@@ -29,6 +30,8 @@ static int run(int argc, const char **argv, const char *prefix)struct option run_options[] = {OPT_BOOL (0 ,"ignore-missing" , &ignore_missing,N_ ("exit quietly with a zero exit code if the requested hook cannot be found" )), +OPT_STRING (0 ,"to-stdin" , &opt.path_to_stdin,N_ ("path" ), +N_ ("file to read into hooks' stdin" )),OPT_END (), };
hook.c +8 -1
@@ -85,7 +85,14 @@ static int pick_next_hook(struct child_process *cp,struct hook_cb_data *hook_cb = pp_cb;struct hook *run_me = hook_cb->run_me; - cp->no_stdin =1 ; + +/* reopen the file for stdin; run_command closes it. */ +if (hook_cb->options->path_to_stdin) { + cp->no_stdin =0 ; + cp->in =xopen (hook_cb->options->path_to_stdin,O_RDONLY ); + }else { + cp->no_stdin =1 ; + } cp->env = hook_cb->options->env.v; cp->stdout_to_stderr =1 ; cp->trace2_hook_name = hook_cb->hook_name;
hook.h +2 -0
@@ -45,6 +45,8 @@ struct run_hooks_opt/* Path to initial working directory for subprocess */ const char *dir; +/* Path to file which should be piped to stdin for each hook */ +const char *path_to_stdin; };#define RUN_HOOKS_OPT_INIT { \
t/t1800-hook.sh +18 -0
@@ -133,4 +133,22 @@ test_expect_success 'git -c core.hooksPath=<PATH> hook run' ' test_cmp expect actual ' +test_expect_success 'stdin to hooks' ' + write_script .git/hooks/test-hook <<-\EOF && + echo BEGIN stdin + cat + echo END stdin + EOF + + cat >expect <<-EOF && + BEGIN stdin + hello + END stdin + EOF + + echo hello >input && + git hook run --to-stdin=input test-hook 2>actual && + test_cmp expect actual +' + test_done
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 16/27 am: convert 'post-rewrite' hook to hook.h
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
--- builtin/am.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-)
builtin/am.c +5 -13
@@ -467,23 +467,15 @@ static int run_applypatch_msg_hook(struct am_state *state)*/ static int run_post_rewrite_hook (const struct am_state *state) { -struct child_process cp =CHILD_PROCESS_INIT ; -const char *hook =find_hook ("post-rewrite" ); +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ;int ret; -if (!hook) -return 0 ; - -strvec_push (&cp.args, hook); -strvec_push (&cp.args,"rebase" ); - - cp.in =xopen (am_path (state,"rewritten" ),O_RDONLY ); - cp.stdout_to_stderr =1 ; - cp.trace2_hook_name ="post-rewrite" ; +strvec_push (&opt.args,"rebase" ); + opt.path_to_stdin =am_path (state,"rewritten" ); - ret =run_command (&cp); + ret =run_hooks ("post-rewrite" , &opt); -close (cp.in); +run_hooks_opt_clear (&opt);return ret; }
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 17/27 run-command: add stdin callback for parallelization
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
If a user of the run_processes_parallel() API wants to pipe a large amount of information to stdin of each parallel command, that information could exceed the buffer of the pipe allocated for that process's stdin. Generally this is solved by repeatedly writing to child_process.in between calls to start_command() and finish_command(); run_processes_parallel() did not provide users an opportunity to access child_process at that time.
Because the data might be extremely large (for example, a list of all refs received during a push from a client) simply taking a string_list or strbuf is not as scalable as using a callback; the rest of the run_processes_parallel() API also uses callbacks, so making this feature match the rest of the API reduces mental load on the user.
--- builtin/fetch.c | 1 + builtin/submodule--helper.c | 2 +- hook.c | 1 + run-command.c | 54 +++++++++++++++++++++++++++++++++++-- run-command.h | 17 +++++++++++- submodule.c | 1 + t/helper/test-run-command.c | 31 ++++++++++++++++++--- t/t0061-run-command.sh | 30 +++++++++++++++++++++ 8 files changed, 129 insertions(+), 8 deletions(-)
builtin/fetch.c +1 -0
@@ -1817,6 +1817,7 @@ static int fetch_multiple(struct string_list *list, int max_children) result =run_processes_parallel_tr2 (max_children, &fetch_next_remote, &fetch_failed_to_start, +NULL , &fetch_finished, &state,"fetch" ,"parallel/fetch" );
builtin/submodule--helper.c +1 -1
@@ -2295,7 +2295,7 @@ static int update_submodules(struct submodule_update_clone *suc)int i;run_processes_parallel_tr2 (suc->max_jobs, update_clone_get_next_task, - update_clone_start_failure, + update_clone_start_failure,NULL , update_clone_task_finished, suc,"submodule" ,"parallel/update" );
hook.c +1 -0
@@ -164,6 +164,7 @@ int run_found_hooks(const char *hook_name, const char *hook_path, run_processes_parallel_tr2(options->jobs, pick_next_hook, notify_start_failure, + NULL, notify_hook_finished, &cb_data, "hook",
run-command.c +52 -2
125 lines not on this page · raw message
run-command.h +16 -1
@@ -419,6 +419,20 @@ typedef int (*start_failure_fn)(struct strbuf *out,void *pp_cb, void *pp_task_cb); +/** +* This callback is called repeatedly on every child process who requests +* start_command() to create a pipe by setting child_process.in < 0. +* +* pp_cb is the callback cookie as passed into run_processes_parallel, and +* pp_task_cb is the callback cookie as passed into get_next_task_fn. +* The contents of 'send' will be read into the pipe and passed to the pipe. +* +* Return nonzero to close the pipe. +*/ +typedef int (*feed_pipe_fn )(struct strbuf *pipe, +void *pp_cb, +void *pp_task_cb); +/** * This callback is called on every child process that finished processing. *
@@ -453,10 +467,11 @@ typedef int (*task_finished_fn)(int result,int run_processes_parallel (int n,get_next_task_fn ,start_failure_fn , +feed_pipe_fn ,task_finished_fn ,void *pp_cb);int run_processes_parallel_tr2 (int n,get_next_task_fn ,start_failure_fn , -task_finished_fn ,void *pp_cb, +feed_pipe_fn ,task_finished_fn ,void *pp_cb,const char *tr2_category,const char *tr2_label);#endif
submodule.c +1 -0
@@ -1645,6 +1645,7 @@ int fetch_populated_submodules(struct repository *r,run_processes_parallel_tr2 (max_parallel_jobs, get_next_submodule, fetch_start_failure, +NULL , fetch_finish, &spf,"submodule" ,"parallel/fetch" );
t/helper/test-run-command.c +27 -4
@@ -32,8 +32,13 @@ static int parallel_next(struct child_process *cp,return 0 ;strvec_pushv (&cp->args, d->argv); + cp->in = d->in; + cp->no_stdin = d->no_stdin;strbuf_addstr (err,"preloaded output of a child\n" ); number_callbacks++; + + *task_cb =xmalloc (sizeof (int )); + *(int *)(*task_cb) =2 ;return 1 ; }
@@ -55,6 +60,17 @@ static int task_finished(int result,return 1 ; } +static int test_stdin (struct strbuf *pipe,void *cb,void *task_cb) +{ +int *lines_remaining = task_cb; + +if (*lines_remaining) +strbuf_addf (pipe,"sample stdin %d\n" , --(*lines_remaining)); + +return !(*lines_remaining); +} + +struct testsuite {struct string_list tests, failed;int next;
@@ -185,7 +201,7 @@ static int testsuite(int argc, const char **argv) suite.tests.nr, max_jobs); ret =run_processes_parallel (max_jobs, next_test, test_failed, - test_finished, &suite); + test_stdin, test_finished, &suite);if (suite.failed.nr >0 ) { ret =1 ;
@@ -413,15 +429,22 @@ int cmd__run_command(int argc, const char **argv)if (!strcmp (argv[1 ],"run-command-parallel" ))exit (run_processes_parallel (jobs, parallel_next, -NULL ,NULL , &proc)); +NULL ,NULL ,NULL , &proc));if (!strcmp (argv[1 ],"run-command-abort" ))exit (run_processes_parallel (jobs, parallel_next, -NULL , task_finished, &proc)); +NULL ,NULL , task_finished, &proc));if (!strcmp (argv[1 ],"run-command-no-jobs" ))exit (run_processes_parallel (jobs, no_job, -NULL , task_finished, &proc)); +NULL ,NULL , task_finished, &proc)); + +if (!strcmp (argv[1 ],"run-command-stdin" )) { + proc.in =-1 ; + proc.no_stdin =0 ; +exit (run_processes_parallel (jobs, parallel_next,NULL , + test_stdin,NULL , &proc)); + }fprintf (stderr,"check usage\n" );return 1 ;
t/t0061-run-command.sh +30 -0
@@ -143,6 +143,36 @@ test_expect_success 'run_command runs in parallel with more tasks than jobs avai test_cmp expect actual ' +cat >expect <<-EOF +preloaded output of a child +listening for stdin: +sample stdin 1 +sample stdin 0 +preloaded output of a child +listening for stdin: +sample stdin 1 +sample stdin 0 +preloaded output of a child +listening for stdin: +sample stdin 1 +sample stdin 0 +preloaded output of a child +listening for stdin: +sample stdin 1 +sample stdin 0 +EOF + +test_expect_success 'run_command listens to stdin' ' + write_script stdin-script <<-\EOF && + echo "listening for stdin:" + while read line; do + echo "$line" + done + EOF + test-tool run-command run-command-stdin 2 ./stdin-script 2>actual && + test_cmp expect actual +' + cat >expect <<-EOF preloaded output of a child asking for a quick stop
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 18/27 hook: provide stdin by string_list or callback
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
In cases where a hook requires only a small amount of information via stdin, it should be simple for users to provide a string_list alone. But in more complicated cases where the stdin is too large to hold in memory, let's instead provide a callback the users can populate line after line.
--- hook.c | 32 +++++++++++++++++++++++++++++++- hook.h | 27 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-)
hook.c +31 -1
@@ -77,6 +77,29 @@ void run_hooks_opt_clear(struct run_hooks_opt *o)strvec_clear (&o->args); } +int pipe_from_string_list (struct strbuf *pipe,void *pp_cb,void *pp_task_cb) +{ +int *item_idx; +struct hook *ctx = pp_task_cb; +struct hook_cb_data *hook_cb = pp_cb; +struct string_list *to_pipe = hook_cb->options->feed_pipe_ctx; + +/* Bootstrap the state manager if necessary. */ +if (!ctx->feed_pipe_cb_data) { + ctx->feed_pipe_cb_data =xmalloc (sizeof (unsigned int )); + *(int *)ctx->feed_pipe_cb_data =0 ; + } + + item_idx = ctx->feed_pipe_cb_data; + +if (*item_idx < to_pipe->nr) { +strbuf_addf (pipe,"%s\n" , to_pipe->items[*item_idx].string); + (*item_idx)++; +return 0 ; + } +return 1 ; +} + static int pick_next_hook(struct child_process *cp, struct strbuf *out, void *pp_cb,
@@ -90,6 +113,10 @@ static int pick_next_hook(struct child_process *cp,if (hook_cb->options->path_to_stdin) { cp->no_stdin =0 ; cp->in =xopen (hook_cb->options->path_to_stdin,O_RDONLY ); + }else if (hook_cb->options->feed_pipe) { +/* ask for start_command() to make a pipe for us */ + cp->in =-1 ; + cp->no_stdin =0 ; }else { cp->no_stdin =1 ; }
@@ -164,7 +191,7 @@ int run_found_hooks(const char *hook_name, const char *hook_path, run_processes_parallel_tr2(options->jobs, pick_next_hook, notify_start_failure, - NULL, + options->feed_pipe, notify_hook_finished, &cb_data, "hook",
@@ -182,6 +209,9 @@ int run_hooks(const char *hook_name, struct run_hooks_opt *options)if (!options)BUG ("a struct run_hooks_opt must be provided to run_hooks" ); +if (options->path_to_stdin && options->feed_pipe) +BUG ("choose only one method to populate stdin" ); + hook_path =find_hook (hook_name);/*
hook.h +27 -0
@@ -19,6 +19,12 @@ int hook_exists(const char *hookname);struct hook {/* The path to the hook */ const char *hook_path; + +/* +* Use this to keep state for your feed_pipe_fn if you are using +* run_hooks_opt.feed_pipe. Otherwise, do not touch it. +*/ +void *feed_pipe_cb_data; };struct run_hooks_opt
@@ -47,6 +53,19 @@ struct run_hooks_opt/* Path to file which should be piped to stdin for each hook */ const char *path_to_stdin; + +/* +* Callback and state pointer to ask for more content to pipe to stdin. +* Will be called repeatedly, for each hook. See +* hook.c:pipe_from_stdin() for an example. Keep per-hook state in +* hook.feed_pipe_cb_data (per process). Keep initialization context in +* feed_pipe_ctx (shared by all processes). +* +* See 'pipe_from_string_list()' for info about how to specify a +* string_list as the stdin input instead of writing your own handler. +*/ +feed_pipe_fn feed_pipe; +void *feed_pipe_ctx; };#define RUN_HOOKS_OPT_INIT { \
@@ -55,6 +74,14 @@ struct run_hooks_opt .args = STRVEC_INIT, \ } +/* +* To specify a 'struct string_list', set 'run_hooks_opt.feed_pipe_ctx' to the +* string_list and set 'run_hooks_opt.feed_pipe' to 'pipe_from_string_list()'. +* This will pipe each string in the list to stdin, separated by newlines. (Do +* not inject your own newlines.) +*/ +int pipe_from_string_list (struct strbuf *pipe,void *pp_cb,void *pp_task_cb); +struct hook_cb_data {/* rc reflects the cumulative failure state */ int rc;
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 19/27 hook: convert 'post-rewrite' hook in sequencer.c to hook.h
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
By using 'hook.h' for 'post-rewrite', we simplify hook invocations by not needing to put together our own 'struct child_process'.
The signal handling that's being removed by this commit now takes place in run-command.h:run_processes_parallel(), so it is OK to remove them here.
--- sequencer.c | 81 ++++++++++++++++++++++------------------------------- 1 file changed, 34 insertions(+), 47 deletions(-)
sequencer.c +34 -47
@@ -35,6 +35,7 @@#include "commit-reach.h" #include "rebase-interactive.h" #include "reset.h" +#include "string-list.h" #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
@@ -1147,33 +1148,28 @@ int update_head_with_reflog(const struct commit *old_head,static int run_rewrite_hook (const struct object_id *oldoid,const struct object_id *newoid) { -struct child_process proc =CHILD_PROCESS_INIT ; -const char *argv[3 ]; +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct strbuf tmp =STRBUF_INIT ; +struct string_list to_stdin =STRING_LIST_INIT_DUP ;int code; -struct strbuf sb =STRBUF_INIT ; - argv[0 ] =find_hook ("post-rewrite" ); -if (!argv[0 ]) -return 0 ; +strvec_push (&opt.args,"amend" ); - argv[1 ] ="amend" ; - argv[2 ] =NULL ; - - proc.argv = argv; - proc.in =-1 ; - proc.stdout_to_stderr =1 ; - proc.trace2_hook_name ="post-rewrite" ; - - code =start_command (&proc); -if (code) -return code; -strbuf_addf (&sb,"%s %s\n" ,oid_to_hex (oldoid),oid_to_hex (newoid)); -sigchain_push (SIGPIPE ,SIG_IGN ); -write_in_full (proc.in, sb.buf, sb.len); -close (proc.in); -strbuf_release (&sb); -sigchain_pop (SIGPIPE ); -return finish_command (&proc); +strbuf_addf (&tmp, +"%s %s" , +oid_to_hex (oldoid), +oid_to_hex (newoid)); +string_list_append (&to_stdin, tmp.buf); + + opt.feed_pipe = pipe_from_string_list; + opt.feed_pipe_ctx = &to_stdin; + + code =run_hooks ("post-rewrite" , &opt); + +run_hooks_opt_clear (&opt); +strbuf_release (&tmp); +string_list_clear (&to_stdin,0 ); +return code; } void commit_post_rewrite(struct repository *r,
@@ -4527,30 +4523,21 @@ static int pick_commits(struct repository *r,flush_rewritten_pending ();if (!stat (rebase_path_rewritten_list (), &st) && st.st_size >0 ) { -struct child_process child =CHILD_PROCESS_INIT ; -const char *post_rewrite_hook = -find_hook ("post-rewrite" ); - - child.in =open (rebase_path_rewritten_list (),O_RDONLY ); - child.git_cmd =1 ; -strvec_push (&child.args,"notes" ); -strvec_push (&child.args,"copy" ); -strvec_push (&child.args,"--for-rewrite=rebase" ); +struct child_process notes_cp =CHILD_PROCESS_INIT ; +struct run_hooks_opt hook_opt =RUN_HOOKS_OPT_INIT ; + + notes_cp.in =open (rebase_path_rewritten_list (),O_RDONLY ); + notes_cp.git_cmd =1 ; +strvec_push (¬es_cp.args,"notes" ); +strvec_push (¬es_cp.args,"copy" ); +strvec_push (¬es_cp.args,"--for-rewrite=rebase" );/* we don't care if this copying failed */ -run_command (&child); - -if (post_rewrite_hook) { -struct child_process hook =CHILD_PROCESS_INIT ; - - hook.in =open (rebase_path_rewritten_list (), -O_RDONLY ); - hook.stdout_to_stderr =1 ; - hook.trace2_hook_name ="post-rewrite" ; -strvec_push (&hook.args, post_rewrite_hook); -strvec_push (&hook.args,"rebase" ); -/* we don't care if this hook failed */ -run_command (&hook); - } +run_command (¬es_cp); + + hook_opt.path_to_stdin =rebase_path_rewritten_list (); +strvec_push (&hook_opt.args,"rebase" ); +run_hooks ("post-rewrite" , &hook_opt); +run_hooks_opt_clear (&hook_opt); }apply_autostash (rebase_path_autostash ());
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 20/27 transport: convert pre-push hook to use config
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
By using the hook.h:run_hooks API, pre-push hooks can be specified in the config as well as in the hookdir.
--- transport.c | 57 ++++++++++++++--------------------------------------- 1 file changed, 15 insertions(+), 42 deletions(-)
transport.c +15 -42
@@ -1199,31 +1199,14 @@ static void die_with_unpushed_submodules(struct string_list *needs_pushing)static int run_pre_push_hook (struct transport *transport,struct ref *remote_refs) { -int ret =0 , x; +int ret =0 ; +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct strbuf tmp =STRBUF_INIT ;struct ref *r; -struct child_process proc =CHILD_PROCESS_INIT ; -struct strbuf buf; -const char *argv[4 ]; - -if (!(argv[0 ] =find_hook ("pre-push" ))) -return 0 ; - - argv[1 ] = transport->remote->name; - argv[2 ] = transport->url; - argv[3 ] =NULL ; - - proc.argv = argv; - proc.in =-1 ; - proc.trace2_hook_name ="pre-push" ; - -if (start_command (&proc)) { -finish_command (&proc); -return -1 ; - } - -sigchain_push (SIGPIPE ,SIG_IGN ); +struct string_list to_stdin =STRING_LIST_INIT_DUP ; -strbuf_init (&buf,256 ); +strvec_push (&opt.args, transport->remote->name); +strvec_push (&opt.args, transport->url);for (r = remote_refs; r; r = r->next) {if (!r->peer_ref)continue ;
@@ -1232,30 +1215,20 @@ static int run_pre_push_hook(struct transport *transport,if (r->status ==REF_STATUS_REJECT_REMOTE_UPDATED )continue ;if (r->status ==REF_STATUS_UPTODATE )continue ; -strbuf_reset (&buf); -strbuf_addf ( &buf,"%s %s %s %s\n" , +strbuf_reset (&tmp); +strbuf_addf (&tmp,"%s %s %s %s" , r->peer_ref->name,oid_to_hex (&r->new_oid), r->name,oid_to_hex (&r->old_oid)); - -if (write_in_full (proc.in, buf.buf, buf.len) <0 ) { -/* We do not mind if a hook does not read all refs. */ -if (errno !=EPIPE ) - ret =-1 ; -break ; - } +string_list_append (&to_stdin, tmp.buf); } -strbuf_release (&buf); - - x =close (proc.in); -if (!ret) - ret = x; - -sigchain_pop (SIGPIPE ); + opt.feed_pipe = pipe_from_string_list; + opt.feed_pipe_ctx = &to_stdin; - x =finish_command (&proc); -if (!ret) - ret = x; + ret =run_hooks ("pre-push" , &opt); +run_hooks_opt_clear (&opt); +strbuf_release (&tmp); +string_list_clear (&to_stdin,0 );return ret; }
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 21/27 reference-transaction: use hook.h to run hooks
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
--- refs.c | 42 +++++++++++++++--------------------------- 1 file changed, 15 insertions(+), 27 deletions(-)
refs.c +15 -27
@@ -2062,47 +2062,35 @@ int ref_update_reject_duplicates(struct string_list *refnames,static int run_transaction_hook (struct ref_transaction *transaction,const char *state) { -struct child_process proc =CHILD_PROCESS_INIT ;struct strbuf buf =STRBUF_INIT ; -const char *hook; +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct string_list to_stdin =STRING_LIST_INIT_DUP ;int ret =0 , i; +char o[GIT_MAX_HEXSZ +1 ], n[GIT_MAX_HEXSZ +1 ]; - hook =find_hook ("reference-transaction" ); -if (!hook) +if (!hook_exists ("reference-transaction" ))return ret; -strvec_pushl (&proc.args, hook, state,NULL ); - proc.in =-1 ; - proc.stdout_to_stderr =1 ; - proc.trace2_hook_name ="reference-transaction" ; - - ret =start_command (&proc); -if (ret) -return ret; - -sigchain_push (SIGPIPE ,SIG_IGN ); +strvec_push (&opt.args, state);for (i =0 ; i < transaction->nr; i++) {struct ref_update *update = transaction->updates[i]; +oid_to_hex_r (o, &update->old_oid); +oid_to_hex_r (n, &update->new_oid);strbuf_reset (&buf); -strbuf_addf (&buf,"%s %s %s\n" , -oid_to_hex (&update->old_oid), -oid_to_hex (&update->new_oid), - update->refname); - -if (write_in_full (proc.in, buf.buf, buf.len) <0 ) { -if (errno !=EPIPE ) - ret =-1 ; -break ; - } +strbuf_addf (&buf,"%s %s %s" , o, n, update->refname); +string_list_append (&to_stdin, buf.buf); } -close (proc.in); -sigchain_pop (SIGPIPE ); + opt.feed_pipe = pipe_from_string_list; + opt.feed_pipe_ctx = &to_stdin; + + ret =run_hooks ("reference-transaction" , &opt); +run_hooks_opt_clear (&opt);strbuf_release (&buf); +string_list_clear (&to_stdin,0 ); - ret |=finish_command (&proc);return ret; }
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 22/27 run-command: allow capturing of collated output
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
Some callers, for example server-side hooks which wish to relay hook output to clients across a transport, want to capture what would normally print to stderr and do something else with it. Allow that via a callback.
By calling the callback regardless of whether there's output available, we allow clients to send e.g. a keepalive if necessary.
Because we expose a strbuf, not a fd or FILE*, there's no need to create a temporary pipe or similar - we can just skip the print to stderr and instead hand it to the caller.
--- builtin/fetch.c | 2 +- builtin/submodule--helper.c | 2 +- hook.c | 1 + run-command.c | 33 +++++++++++++++++++++++++-------- run-command.h | 18 +++++++++++++++++- submodule.c | 2 +- t/helper/test-run-command.c | 25 ++++++++++++++++++++----- t/t0061-run-command.sh | 7 +++++++ 8 files changed, 73 insertions(+), 17 deletions(-)
builtin/fetch.c +1 -1
@@ -1817,7 +1817,7 @@ static int fetch_multiple(struct string_list *list, int max_children) result =run_processes_parallel_tr2 (max_children, &fetch_next_remote, &fetch_failed_to_start, -NULL , +NULL ,NULL , &fetch_finished, &state,"fetch" ,"parallel/fetch" );
builtin/submodule--helper.c +1 -1
@@ -2295,7 +2295,7 @@ static int update_submodules(struct submodule_update_clone *suc)int i;run_processes_parallel_tr2 (suc->max_jobs, update_clone_get_next_task, - update_clone_start_failure,NULL , + update_clone_start_failure,NULL ,NULL , update_clone_task_finished, suc,"submodule" ,"parallel/update" );
hook.c +1 -0
@@ -192,6 +192,7 @@ int run_found_hooks(const char *hook_name, const char *hook_path, pick_next_hook, notify_start_failure, options->feed_pipe, +NULL , notify_hook_finished, &cb_data, "hook",
run-command.c +25 -8
@@ -1494,6 +1494,7 @@ struct parallel_processes {get_next_task_fn get_next_task;start_failure_fn start_failure;feed_pipe_fn feed_pipe; +consume_sideband_fn consume_sideband;task_finished_fn task_finished;struct {
@@ -1559,6 +1560,7 @@ static void pp_init(struct parallel_processes *pp,get_next_task_fn get_next_task, start_failure_fn start_failure, feed_pipe_fn feed_pipe, + consume_sideband_fn consume_sideband, task_finished_fn task_finished, void *data) {
@@ -1579,6 +1581,7 @@ static void pp_init(struct parallel_processes *pp, pp->start_failure = start_failure ? start_failure : default_start_failure; pp->feed_pipe = feed_pipe ? feed_pipe : default_feed_pipe; pp->task_finished = task_finished ? task_finished : default_task_finished; + pp->consume_sideband = consume_sideband; pp->nr_processes =0 ; pp->output_owner =0 ;
@@ -1615,7 +1618,10 @@ static void pp_cleanup(struct parallel_processes *pp)* When get_next_task added messages to the buffer in its last * iteration, the buffered output is non empty. */ -strbuf_write (&pp->buffered_output, stderr); +if (pp->consume_sideband) + pp->consume_sideband (&pp->buffered_output, pp->data); +else +strbuf_write (&pp->buffered_output, stderr);strbuf_release (&pp->buffered_output);sigchain_pop_common ();
@@ -1736,9 +1742,13 @@ static void pp_buffer_stderr(struct parallel_processes *pp, int output_timeout)static void pp_output (struct parallel_processes *pp) {int i = pp->output_owner; +if (pp->children[i].state ==GIT_CP_WORKING && pp->children[i].err.len) { -strbuf_write (&pp->children[i].err, stderr); +if (pp->consume_sideband) + pp->consume_sideband (&pp->children[i].err, pp->data); +else +strbuf_write (&pp->children[i].err, stderr);strbuf_reset (&pp->children[i].err); } }
@@ -1777,11 +1787,15 @@ static int pp_collect_finished(struct parallel_processes *pp)strbuf_addbuf (&pp->buffered_output, &pp->children[i].err);strbuf_reset (&pp->children[i].err); }else { -strbuf_write (&pp->children[i].err, stderr); +/* Output errors, then all other finished child processes */ +if (pp->consume_sideband) { + pp->consume_sideband (&pp->children[i].err, pp->data); + pp->consume_sideband (&pp->buffered_output, pp->data); + }else { +strbuf_write (&pp->children[i].err, stderr); +strbuf_write (&pp->buffered_output, stderr); + }strbuf_reset (&pp->children[i].err); - -/* Output all other finished child processes */ -strbuf_write (&pp->buffered_output, stderr);strbuf_reset (&pp->buffered_output);/*
@@ -1805,6 +1819,7 @@ int run_processes_parallel(int n,get_next_task_fn get_next_task, start_failure_fn start_failure, feed_pipe_fn feed_pipe, + consume_sideband_fn consume_sideband, task_finished_fn task_finished, void *pp_cb) {
@@ -1815,7 +1830,7 @@ int run_processes_parallel(int n,sigchain_push (SIGPIPE ,SIG_IGN ); -pp_init (&pp, n, get_next_task, start_failure, feed_pipe, task_finished, pp_cb); +pp_init (&pp, n, get_next_task, start_failure, feed_pipe, consume_sideband, task_finished, pp_cb);while (1 ) { for (i = 0; i < spawn_cap && !pp.shutdown &&
@@ -1853,6 +1868,7 @@ int run_processes_parallel(int n,int run_processes_parallel_tr2 (int n,get_next_task_fn get_next_task,start_failure_fn start_failure,feed_pipe_fn feed_pipe, +consume_sideband_fn consume_sideband,task_finished_fn task_finished,void *pp_cb,const char *tr2_category,const char *tr2_label) {
@@ -1862,7 +1878,8 @@ int run_processes_parallel_tr2(int n, get_next_task_fn get_next_task, ((n < 1) ? online_cpus() : n)); result =run_processes_parallel (n, get_next_task, start_failure, - feed_pipe, task_finished, pp_cb); + feed_pipe, consume_sideband, + task_finished, pp_cb);trace2_region_leave (tr2_category, tr2_label,NULL );
run-command.h +17 -1
@@ -433,6 +433,20 @@ typedef int (*feed_pipe_fn)(struct strbuf *pipe,void *pp_cb, void *pp_task_cb); +/** +* If this callback is provided, instead of collating process output to stderr, +* they will be collated into a new pipe. consume_sideband_fn will be called +* repeatedly. When output is available on that pipe, it will be contained in +* 'output'. But it will be called with an empty 'output' too, to allow for +* keepalives or similar operations if necessary. +* +* pp_cb is the callback cookie as passed into run_processes_parallel. +* +* Since this callback is provided with the collated output, no task cookie is +* provided. +*/ +typedef void (*consume_sideband_fn )(struct strbuf *output,void *pp_cb); +/** * This callback is called on every child process that finished processing. *
@@ -468,10 +482,12 @@ int run_processes_parallel(int n, get_next_task_fn, start_failure_fn, feed_pipe_fn, + consume_sideband_fn, task_finished_fn, void *pp_cb);int run_processes_parallel_tr2 (int n,get_next_task_fn ,start_failure_fn , -feed_pipe_fn ,task_finished_fn ,void *pp_cb, +feed_pipe_fn ,consume_sideband_fn , +task_finished_fn ,void *pp_cb,const char *tr2_category,const char *tr2_label);#endif
submodule.c +1 -1
@@ -1645,7 +1645,7 @@ int fetch_populated_submodules(struct repository *r,run_processes_parallel_tr2 (max_parallel_jobs, get_next_submodule, fetch_start_failure, -NULL , +NULL ,NULL , fetch_finish, &spf,"submodule" ,"parallel/fetch" );
t/helper/test-run-command.c +20 -5
@@ -51,6 +51,16 @@ static int no_job(struct child_process *cp,return 0 ; } +static void test_consume_sideband (struct strbuf *output,void *cb) +{ +FILE *sideband; + + sideband =fopen ("./sideband" ,"a" ); + +strbuf_write (output, sideband); +fclose (sideband); +} + static int task_finished(int result, struct strbuf *err, void *pp_cb,
@@ -201,7 +211,7 @@ static int testsuite(int argc, const char **argv) suite.tests.nr, max_jobs); ret =run_processes_parallel (max_jobs, next_test, test_failed, - test_stdin, test_finished, &suite); + test_stdin,NULL , test_finished, &suite);if (suite.failed.nr >0 ) { ret =1 ;
@@ -429,23 +439,28 @@ int cmd__run_command(int argc, const char **argv)if (!strcmp (argv[1 ],"run-command-parallel" ))exit (run_processes_parallel (jobs, parallel_next, -NULL ,NULL ,NULL , &proc)); +NULL ,NULL ,NULL ,NULL , &proc));if (!strcmp (argv[1 ],"run-command-abort" ))exit (run_processes_parallel (jobs, parallel_next, -NULL ,NULL , task_finished, &proc)); +NULL ,NULL ,NULL , task_finished, &proc));if (!strcmp (argv[1 ],"run-command-no-jobs" ))exit (run_processes_parallel (jobs, no_job, -NULL ,NULL , task_finished, &proc)); +NULL ,NULL ,NULL , task_finished, &proc));if (!strcmp (argv[1 ],"run-command-stdin" )) { proc.in =-1 ; proc.no_stdin =0 ;exit (run_processes_parallel (jobs, parallel_next,NULL , - test_stdin,NULL , &proc)); + test_stdin,NULL ,NULL , &proc)); } +if (!strcmp (argv[1 ],"run-command-sideband" )) +exit (run_processes_parallel (jobs, parallel_next,NULL ,NULL , + test_consume_sideband,NULL , + &proc)); +fprintf (stderr,"check usage\n" );return 1 ; }
t/t0061-run-command.sh +7 -0
@@ -143,6 +143,13 @@ test_expect_success 'run_command runs in parallel with more tasks than jobs avai test_cmp expect actual ' +test_expect_success 'run_command can divert output' ' + test_when_finished rm sideband && + test-tool run-command run-command-sideband 3 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual && + test_must_be_empty actual && + test_cmp expect sideband +' + cat >expect <<-EOF preloaded output of a child listening for stdin:
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 23/27 hooks: allow callers to capture output
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
Some server-side hooks will require capturing output to send over sideband instead of printing directly to stderr. Expose that capability.
--- hook.c | 2 +- hook.h | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-)
hook.c +1 -1
@@ -192,7 +192,7 @@ int run_found_hooks(const char *hook_name, const char *hook_path, pick_next_hook, notify_start_failure, options->feed_pipe, -NULL , + options->consume_sideband, notify_hook_finished, &cb_data, "hook",
hook.h +8 -0
@@ -66,6 +66,14 @@ struct run_hooks_opt*/ feed_pipe_fn feed_pipe;void *feed_pipe_ctx; + +/* +* Populate this to capture output and prevent it from being printed to +* stderr. This will be passed directly through to +* run_command:run_parallel_processes(). See t/helper/test-run-command.c +* for an example. +*/ +consume_sideband_fn consume_sideband; };#define RUN_HOOKS_OPT_INIT { \
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 24/27 receive-pack: convert 'update' hook to hook.h
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
This makes use of the new sideband API in hook.h added in the preceding commit.
--- builtin/receive-pack.c | 65 ++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 21 deletions(-)
builtin/receive-pack.c +44 -21
@@ -938,33 +938,56 @@ static int run_receive_hook(struct command *commands,return status; } -static int run_update_hook (struct command *cmd) +static void hook_output_to_sideband (struct strbuf *output,void *cb_data) { -const char *argv[5 ]; -struct child_process proc =CHILD_PROCESS_INIT ; -int code; +int keepalive_active =0 ; - argv[0 ] =find_hook ("update" ); -if (!argv[0 ]) -return 0 ; +if (keepalive_in_sec <=0 ) + use_keepalive =KEEPALIVE_NEVER ; +if (use_keepalive ==KEEPALIVE_ALWAYS ) + keepalive_active =1 ; - argv[1 ] = cmd->ref_name; - argv[2 ] =oid_to_hex (&cmd->old_oid); - argv[3 ] =oid_to_hex (&cmd->new_oid); - argv[4 ] =NULL ; +/* send a keepalive if there is no data to write */ +if (keepalive_active && !output->len) { +static const char buf[] ="0005\1" ; +write_or_die (1 , buf,sizeof (buf) -1 ); +return ; + } - proc.no_stdin =1 ; - proc.stdout_to_stderr =1 ; - proc.err = use_sideband ?-1 :0 ; - proc.argv = argv; - proc.trace2_hook_name ="update" ; +if (use_keepalive ==KEEPALIVE_AFTER_NUL && !keepalive_active) { +const char *first_null =memchr (output->buf,'\0' , output->len); +if (first_null) { +/* The null bit is excluded. */ +size_t before_null = first_null - output->buf; +size_t after_null = output->len - (before_null +1 ); + keepalive_active =1 ; +send_sideband (1 ,2 , output->buf, before_null, use_sideband); +send_sideband (1 ,2 , first_null +1 , after_null, use_sideband); + +return ; + } + } + +send_sideband (1 ,2 , output->buf, output->len, use_sideband); +} + +static int run_update_hook (struct command *cmd) +{ +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +int code; + +strvec_pushl (&opt.args, + cmd->ref_name, +oid_to_hex (&cmd->old_oid), +oid_to_hex (&cmd->new_oid), +NULL ); - code =start_command (&proc); -if (code) -return code;if (use_sideband) -copy_to_sideband (proc.err,-1 ,NULL ); -return finish_command (&proc); + opt.consume_sideband = hook_output_to_sideband; + + code =run_hooks ("update" , &opt); +run_hooks_opt_clear (&opt); +return code; } static struct command *find_command_by_refname(struct command *list,
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 25/27 post-update: use hook.h library
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
--- builtin/receive-pack.c | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-)
builtin/receive-pack.c +7 -19
@@ -1657,33 +1657,21 @@ static const char *update(struct command *cmd, struct shallow_info *si)static void run_update_post_hook (struct command *commands) {struct command *cmd; -struct child_process proc =CHILD_PROCESS_INIT ; -const char *hook; - - hook =find_hook ("post-update" ); -if (!hook) -return ; +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ;for (cmd = commands; cmd; cmd = cmd->next) {if (cmd->error_string || cmd->did_not_exist)continue ; -if (!proc.args.nr) -strvec_push (&proc.args, hook); -strvec_push (&proc.args, cmd->ref_name); +strvec_push (&opt.args, cmd->ref_name); } -if (!proc.args.nr) +if (!opt.args.nr)return ; - proc.no_stdin =1 ; - proc.stdout_to_stderr =1 ; - proc.err = use_sideband ?-1 :0 ; - proc.trace2_hook_name ="post-update" ; +if (use_sideband) + opt.consume_sideband = hook_output_to_sideband; -if (!start_command (&proc)) { -if (use_sideband) -copy_to_sideband (proc.err,-1 ,NULL ); -finish_command (&proc); - } +run_hooks ("post-update" , &opt); +run_hooks_opt_clear (&opt); } static void check_aliased_update_internal(struct command *cmd,
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 26/27 receive-pack: convert receive hooks to hook.h
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
--- builtin/receive-pack.c | 197 +++++++++++++++++++---------------------- 1 file changed, 90 insertions(+), 107 deletions(-)
builtin/receive-pack.c +90 -107
@@ -748,7 +748,7 @@ static int check_cert_push_options(const struct string_list *push_options)return retval; } -static void prepare_push_cert_sha1 (struct child_process *proc) +static void prepare_push_cert_sha1 (struct run_hooks_opt *opt) {static int already_done;
@@ -772,110 +772,42 @@ static void prepare_push_cert_sha1(struct child_process *proc) nonce_status =check_nonce (push_cert.buf, bogs); }if (!is_null_oid (&push_cert_oid)) { -strvec_pushf (&proc->env_array,"GIT_PUSH_CERT=%s" , +strvec_pushf (&opt->env,"GIT_PUSH_CERT=%s" ,oid_to_hex (&push_cert_oid)); -strvec_pushf (&proc->env_array,"GIT_PUSH_CERT_SIGNER=%s" , +strvec_pushf (&opt->env,"GIT_PUSH_CERT_SIGNER=%s" , sigcheck.signer ? sigcheck.signer :"" ); -strvec_pushf (&proc->env_array,"GIT_PUSH_CERT_KEY=%s" , +strvec_pushf (&opt->env,"GIT_PUSH_CERT_KEY=%s" , sigcheck.key ? sigcheck.key :"" ); -strvec_pushf (&proc->env_array,"GIT_PUSH_CERT_STATUS=%c" , +strvec_pushf (&opt->env,"GIT_PUSH_CERT_STATUS=%c" , sigcheck.result);if (push_cert_nonce) { -strvec_pushf (&proc->env_array, +strvec_pushf (&opt->env,"GIT_PUSH_CERT_NONCE=%s" , push_cert_nonce); -strvec_pushf (&proc->env_array, +strvec_pushf (&opt->env,"GIT_PUSH_CERT_NONCE_STATUS=%s" , nonce_status);if (nonce_status ==NONCE_SLOP ) -strvec_pushf (&proc->env_array, +strvec_pushf (&opt->env,"GIT_PUSH_CERT_NONCE_SLOP=%ld" , nonce_stamp_slop); } } } +struct receive_hook_feed_context { +struct command *cmd; +int skip_broken; +}; +struct receive_hook_feed_state {struct command *cmd;struct ref_push_report *report;int skip_broken;struct strbuf buf; -const struct string_list *push_options; }; -typedef int (*feed_fn )(void *,const char **,size_t *); -static int run_and_feed_hook (const char *hook_name,feed_fn feed, -struct receive_hook_feed_state *feed_state) -{ -struct child_process proc =CHILD_PROCESS_INIT ; -struct async muxer; -const char *argv[2 ]; -int code; - - argv[0 ] =find_hook (hook_name); -if (!argv[0 ]) -return 0 ; - - argv[1 ] =NULL ; - - proc.argv = argv; - proc.in =-1 ; - proc.stdout_to_stderr =1 ; - proc.trace2_hook_name = hook_name; - -if (feed_state->push_options) { -int i; -for (i =0 ; i < feed_state->push_options->nr; i++) -strvec_pushf (&proc.env_array, -"GIT_PUSH_OPTION_%d=%s" , i, - feed_state->push_options->items[i].string); -strvec_pushf (&proc.env_array,"GIT_PUSH_OPTION_COUNT=%d" , - feed_state->push_options->nr); - }else -strvec_pushf (&proc.env_array,"GIT_PUSH_OPTION_COUNT" ); - -if (tmp_objdir) -strvec_pushv (&proc.env_array,tmp_objdir_env (tmp_objdir)); - -if (use_sideband) { -memset (&muxer,0 ,sizeof (muxer)); - muxer.proc = copy_to_sideband; - muxer.in =-1 ; - code =start_async (&muxer); -if (code) -return code; - proc.err = muxer.in; - } - -prepare_push_cert_sha1 (&proc); - - code =start_command (&proc); -if (code) { -if (use_sideband) -finish_async (&muxer); -return code; - } - -sigchain_push (SIGPIPE ,SIG_IGN ); - -while (1 ) { -const char *buf; -size_t n; -if (feed (feed_state, &buf, &n)) -break ; -if (write_in_full (proc.in, buf, n) <0 ) -break ; - } -close (proc.in); -if (use_sideband) -finish_async (&muxer); - -sigchain_pop (SIGPIPE ); - -return finish_command (&proc); -} - -static int feed_receive_hook (void *state_,const char **bufp,size_t *sizep) +static int feed_receive_hook (void *state_) {struct receive_hook_feed_state *state = state_;struct command *cmd = state->cmd;
@@ -884,9 +816,7 @@ static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep) state->skip_broken && (cmd->error_string || cmd->did_not_exist)) cmd = cmd->next;if (!cmd) -return -1 ;/* EOF */ -if (!bufp) -return 0 ;/* OK, can feed something. */ +return 1 ;/* EOF - close the pipe*/ strbuf_reset (&state->buf);if (!state->report) state->report = cmd->report;
@@ -910,32 +840,36 @@ static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep) cmd->ref_name); state->cmd = cmd->next; } -if (bufp) { - *bufp = state->buf.buf; - *sizep = state->buf.len; - }return 0 ; } -static int run_receive_hook (struct command *commands, -const char *hook_name, -int skip_broken, -const struct string_list *push_options) +static int feed_receive_hook_cb (struct strbuf *pipe,void *pp_cb,void *pp_task_cb) { -struct receive_hook_feed_state state; -int status; - -strbuf_init (&state.buf,0 ); - state.cmd = commands; - state.skip_broken = skip_broken; - state.report =NULL ; -if (feed_receive_hook (&state,NULL ,NULL )) -return 0 ; - state.cmd = commands; - state.push_options = push_options; - status =run_and_feed_hook (hook_name, feed_receive_hook, &state); -strbuf_release (&state.buf); -return status; +struct hook *hook = pp_task_cb; +struct receive_hook_feed_state *feed_state = hook->feed_pipe_cb_data; +int rc; + +/* first-time setup */ +if (!feed_state) { +struct hook_cb_data *hook_cb = pp_cb; +struct run_hooks_opt *opt = hook_cb->options; +struct receive_hook_feed_context *ctx = opt->feed_pipe_ctx; +if (!ctx) +BUG ("run_hooks_opt.feed_pipe_ctx required for receive hook" ); + + feed_state =xmalloc (sizeof (struct receive_hook_feed_state )); +strbuf_init (&feed_state->buf,0 ); + feed_state->cmd = ctx->cmd; + feed_state->skip_broken = ctx->skip_broken; + feed_state->report =NULL ; + + hook->feed_pipe_cb_data = feed_state; + } + + rc =feed_receive_hook (feed_state); +if (!rc) +strbuf_addbuf (pipe, &feed_state->buf); +return rc; }static void hook_output_to_sideband (struct strbuf *output,void *cb_data)
@@ -971,6 +905,55 @@ static void hook_output_to_sideband(struct strbuf *output, void *cb_data)send_sideband (1 ,2 , output->buf, output->len, use_sideband); } +static int run_receive_hook (struct command *commands, +const char *hook_name, +int skip_broken, +const struct string_list *push_options) +{ +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct receive_hook_feed_context ctx; +int rc; +struct command *iter = commands; + +/* if there are no valid commands, don't invoke the hook at all. */ +while (iter && skip_broken && (iter->error_string || iter->did_not_exist)) + iter = iter->next; +if (!iter) +return 0 ; + +/* pre-receive hooks should run in series as the hook updates refs */ +if (!strcmp (hook_name,"pre-receive" )) + opt.jobs =1 ; + +if (push_options) { +int i; +for (i =0 ; i < push_options->nr; i++) +strvec_pushf (&opt.env,"GIT_PUSH_OPTION_%d=%s" , i, + push_options->items[i].string); +strvec_pushf (&opt.env,"GIT_PUSH_OPTION_COUNT=%d" , push_options->nr); + }else +strvec_push (&opt.env,"GIT_PUSH_OPTION_COUNT" ); + +if (tmp_objdir) +strvec_pushv (&opt.env,tmp_objdir_env (tmp_objdir)); + +prepare_push_cert_sha1 (&opt); + +/* set up sideband printer */ +if (use_sideband) + opt.consume_sideband = hook_output_to_sideband; + +/* set up stdin callback */ + ctx.cmd = commands; + ctx.skip_broken = skip_broken; + opt.feed_pipe = feed_receive_hook_cb; + opt.feed_pipe_ctx = &ctx; + + rc =run_hooks (hook_name, &opt); +run_hooks_opt_clear (&opt); +return rc; +} +static int run_update_hook (struct command *cmd) {struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ;
Signature · 1 line
2.32.0.576.g59759b6ca7d
PATCH 27/27 hooks: fix a TOCTOU in "did we run a hook?" heuristic
In reply to Ævar Arnfjörð BjarmasonFix a Time-of-check to time-of-use (TOCTOU) race in code added in 680ee550d72 (commit: skip discarding the index if there is no pre-commit hook, 2017-08-14).
We can fix the race passing around information about whether or not we ran the hook in question, instead of running hook_exists() after the fact to check if the hook in question exists. This problem has been noted on-list when 680ee550d72 was discussed[1], but had not been fixed.
In addition to fixing this for the pre-commit hook as suggested there I'm also fixing this for the pre-merge-commit hook. See 6098817fd7f (git-merge: honor pre-merge-commit hook, 2019-08-07) for the introduction of its previous behavior.
Let's also change this for the push-to-checkout hook. Now instead of checking if the hook exists and either doing a push to checkout or a push to deploy we'll always attempt a push to checkout. If the hook doesn't exist we'll fall back on push to deploy. The same behavior as before, without the TOCTOU race. See 0855331941b (receive-pack:
support push-to-checkout hook, 2014-12-01) for the introduction of the previous behavior.
This leaves uses of hook_exists() in two places that matter. The "reference-transaction" check in refs.c, see 67541597670 (refs:
implement reference transaction hook, 2020-06-19), and the prepare-commit-msg hook, see 66618a50f9c (sequencer: run 'prepare-commit-msg' hook, 2018-01-24).
In both of those cases we're saving ourselves CPU time by not preparing data for the hook that we'll then do nothing with if we don't have the hook. So using this "invoked_hook" pattern doesn't make sense in those cases.
More importantly, in those cases the worst we'll do is miss that we "should" run the hook because a new hook appeared, whereas in the pre-commit and pre-merge-commit cases we'll skip an important discard_cache() on the bases of our faulty guess.
I do think none of these races really matter in practice. It would be some one-off issue as a hook was added or removed. I did think it was stupid that we didn't pass a "did this run?" flag instead of doing this guessing at a distance though, so now we're not guessing anymore.
1. https://lore.kernel.org/git/[email protected]/
--- builtin/commit.c | 18 +++++++++++------- builtin/merge.c | 16 ++++++++++------ builtin/receive-pack.c | 8 +++++--- commit.c | 1 + commit.h | 3 ++- hook.c | 4 ++++ hook.h | 10 ++++++++++ sequencer.c | 4 ++-- 8 files changed, 45 insertions(+), 19 deletions(-)
builtin/commit.c +11 -7
@@ -725,11 +725,13 @@ 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 invoked_hook =0 ;/* 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, &invoked_hook, +"pre-commit" ,NULL ))return 0 ;if (squash_message) {
@@ -1045,10 +1047,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix,return 0 ; } -if (!no_verify &&hook_exists ("pre-commit" )) { +if (!no_verify && invoked_hook) {/* -* Re-read the index as pre-commit hook could have updated it, -* and write it out as a tree. We must do this before we invoke +* Re-read the index as the pre-commit-commit hook was invoked +* and could have updated it. We must do this before we invoke * the editor and after we invoke run_status above. */ discard_cache ();
@@ -1060,7 +1062,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,NULL ,"prepare-commit-msg" ,git_path_commit_editmsg (), hook_arg1, hook_arg2,NULL ))return 0 ;
@@ -1077,7 +1079,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,NULL ,"commit-msg" , +git_path_commit_editmsg (),NULL )) {return 0 ; }
@@ -1830,7 +1833,8 @@ 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 (),NULL ,"post-commit" , +NULL );if (amend && !no_post_rewrite) {commit_post_rewrite (the_repository, current_head, &oid); }
builtin/merge.c +10 -6
@@ -844,15 +844,18 @@ static void prepare_to_commit(struct commit_list *remoteheads) {struct strbuf msg =STRBUF_INIT ;const char *index_file =get_index_file (); +int invoked_hook =0 ; -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, + &invoked_hook,"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 +* Re-read the index as the pre-merge-commit hook was invoked +* and could have updated it. We must do this before we invoke * the editor and after we invoke run_status above. */ -if (hook_exists ("pre-merge-commit" )) +if (invoked_hook)discard_cache ();read_cache_from (index_file);strbuf_addbuf (&msg, &merge_msg);
@@ -873,7 +876,8 @@ 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 (),NULL , +"prepare-commit-msg" ,git_path_merge_msg (the_repository),"merge" ,NULL ))abort_commit (remoteheads,NULL );if (0 < option_edit) {
@@ -882,7 +886,7 @@ static void prepare_to_commit(struct commit_list *remoteheads) }if (!no_verify &&run_commit_hook (0 < option_edit,get_index_file (), -"commit-msg" , +NULL ,"commit-msg" ,git_path_merge_msg (the_repository),NULL ))abort_commit (remoteheads,NULL );
builtin/receive-pack.c +5 -3
@@ -1439,10 +1439,12 @@ static const char *push_to_deploy(unsigned char *sha1,static const char *push_to_checkout_hook ="push-to-checkout" ;static const char *push_to_checkout (unsigned char *hash, +int *invoked_hook,struct strvec *env,const char *work_tree) {struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; + opt.invoked_hook = invoked_hook;strvec_pushf (env,"GIT_WORK_TREE=%s" ,absolute_path (work_tree));strvec_pushv (&opt.env, env->v);
@@ -1460,6 +1462,7 @@ static const char *update_worktree(unsigned char *sha1, const struct worktree *w {const char *retval, *work_tree, *git_dir =NULL ;struct strvec env =STRVEC_INIT ; +int invoked_hook =0 ;if (worktree && worktree->path) work_tree = worktree->path;
@@ -1477,10 +1480,9 @@ static const char *update_worktree(unsigned char *sha1, const struct worktree *wstrvec_pushf (&env,"GIT_DIR=%s" ,absolute_path (git_dir)); -if (!hook_exists (push_to_checkout_hook)) + retval =push_to_checkout (sha1, &invoked_hook, &env, work_tree); +if (!invoked_hook) retval =push_to_deploy (sha1, &env, work_tree); -else - retval =push_to_checkout (sha1, &env, work_tree);strvec_clear (&env);return retval;
commit.c +1 -0
@@ -1697,6 +1697,7 @@ size_t ignore_non_trailer(const char *buf, size_t len) }int run_commit_hook (int editor_is_used,const char *index_file, +int *invoked_hook,const char *name, ...) {struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ;
commit.h +2 -1
@@ -363,7 +363,8 @@ 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, +int *invoked_hook,const char *name, ...);/* Sign a commit or tag buffer, storing the result in a header. */ int sign_with_header (struct strbuf *buf,const char *keyid);
hook.c +4 -0
@@ -164,6 +164,9 @@ static int notify_hook_finished(int result, hook_cb->rc |= result; +if (hook_cb->invoked_hook) + *hook_cb->invoked_hook =1 ; +return 1 ; }
@@ -178,6 +181,7 @@ int run_found_hooks(const char *hook_name, const char *hook_path, .rc = 0, .hook_name = hook_name, .options = options, + .invoked_hook = options->invoked_hook, };if (options->absolute_path) {strbuf_add_absolute_path (&abs_path, hook_path);
hook.h +10 -0
@@ -74,6 +74,15 @@ struct run_hooks_opt* for an example. */ consume_sideband_fn consume_sideband; + +/* +* A pointer which if provided will be set to 1 or 0 depending +* on if a hook was invoked (i.e. existed), regardless of +* whether or not that was successful. Used for avoiding +* TOCTOU races in code that would otherwise call hook_exist() +* after a "maybe hook run" to see if a hook was invoked. +*/ +int *invoked_hook; };#define RUN_HOOKS_OPT_INIT { \
@@ -96,6 +105,7 @@ struct hook_cb_data {const char *hook_name;struct hook *run_me;struct run_hooks_opt *options; +int *invoked_hook; };void run_hooks_opt_clear (struct run_hooks_opt *o);
sequencer.c +2 -2
@@ -1204,7 +1204,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,NULL ,"prepare-commit-msg" , name, arg1, arg2,NULL )) ret =error (_ ("'prepare-commit-msg' hook failed" ));
@@ -1534,7 +1534,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,NULL ,"post-commit" ,NULL );if (flags &AMEND_MSG )commit_post_rewrite (r, current_head, oid);
Signature · 1 line
2.32.0.576.g59759b6ca7d
Re: [PATCH 27/27] hooks: fix a TOCTOU in "did we run a hook?" heuristic
Quoted 40 lines · Ævar Arnfjörð Bjarmason
Fix a Time-of-check to time-of-use (TOCTOU) race in code added in
680ee550d72(commit: skip discarding the index if there is no pre-commit hook, 2017-08-14).We can fix the race passing around information about whether or not we ran the hook in question, instead of running
hook_exists()after the fact to check if the hook in question exists. This problem has been noted on-list when680ee550d72was discussed[1], but had not been fixed.In addition to fixing this for the pre-commit hook as suggested there I'm also fixing this for the pre-merge-commit hook. See
6098817fd7f(git-merge: honor pre-merge-commit hook, 2019-08-07) for the introduction of its previous behavior.Let's also change this for the push-to-checkout hook. Now instead of checking if the hook exists and either doing a push to checkout or a push to deploy we'll always attempt a push to checkout. If the hook doesn't exist we'll fall back on push to deploy. The same behavior as before, without the TOCTOU race. See
0855331941b(receive-pack:
support push-to-checkout hook, 2014-12-01) for the introduction of the previous behavior.This leaves uses of
hook_exists()in two places that matter. The "reference-transaction" check inrefs.c, see 67541597670 (refs:
implement reference transaction hook, 2020-06-19), and the prepare-commit-msg hook, see66618a50f9c(sequencer: run 'prepare-commit-msg' hook, 2018-01-24).In both of those cases we're saving ourselves CPU time by not preparing data for the hook that we'll then do nothing with if we don't have the hook. So using this "
invoked_hook" pattern doesn't make sense in those cases.More importantly, in those cases the worst we'll do is miss that we "should" run the hook because a new hook appeared, whereas in the pre-commit and pre-merge-commit cases we'll skip an important
discard_cache()on the bases of our faulty guess.
Quoted 4 lines
I do think none of these races really matter in practice. It would be some one-off issue as a hook was added or removed. I did think it was stupid that we didn't pass a "did this run?" flag instead of doing this guessing at a distance though, so now we're not guessing anymore.
Yeah, I think your solution is very neat. I like this patch.
- Emily
cover config-based hooks restarted
In reply to Ævar Arnfjörð BjarmasonThe work formerly found at
https://lore.kernel.org/git/[email protected]
has been rebased on top of
https://lore.kernel.org/git/[email protected]
(ab/config-based-hooks-base).
Some features have been dropped from v9 of the larger config-based-hooks series, and many patches have been replaced by Ævar's new base.
This series no longer introduces the 'hook.runHooksDir' config - I think it is OK to introduce that later and quibble about it in isolation from the rest of this work.
I also would encourage folks interested in reviewing this to not spend too much time looking through it without first leaving a review on Ævar's series which this is based on; this series won't go anywhere without that one. :) (And yes, I owe a review on the more recent version of that series, too.)
Note: I am worried that somewhere around patch 6 a flaky test may have been added - I saw flaky failures on the osx-clang and linux-musl tests when I ran CI on GitHub. I *think* this was due to missing a field when initializing 'struct hook' for addition to the hook linked list, and I *think* I have fixed that, but if something jumps out to anyone in the code or in the test, I'd really appreciate the hint. A passing CI run for the tip of this series is at
https://github.com/nasamuffin/git/actions/runs/1035559898.
- Emily
Emily Shaffer (9):
hook: run a list of hooks instead
hook: allow parallel hook execution
hook: introduce "git hook list"
hook: treat hookdir hook specially
hook: allow running non-native hooks
hook: include hooks from the config
hook: allow out-of-repo 'git hook' invocations
hook: teach 'hookcmd' config to alias hook scripts
hook: implement hookcmd.<name>.skip
Documentation/config/hook.txt | 22 +++ Documentation/git-hook.txt | 97 ++++++++++++- builtin/am.c | 12 +- builtin/checkout.c | 3 +- builtin/clone.c | 3 +- builtin/gc.c | 3 +- builtin/hook.c | 58 +++++++- builtin/merge.c | 3 +- builtin/rebase.c | 3 +- builtin/receive-pack.c | 14 +- builtin/worktree.c | 3 +- commit.c | 4 +- git.c | 2 +- hook.c | 251 +++++++++++++++++++++++++++++++--- hook.h | 44 ++++-- read-cache.c | 3 +- refs.c | 3 +- reset.c | 4 +- sequencer.c | 7 +- t/t1360-config-based-hooks.sh | 227 ++++++++++++++++++++++++++++++ transport.c | 4 +- 21 files changed, 711 insertions(+), 59 deletions(-) create mode 100644 Documentation/config/hook.txt create mode 100755 t/t1360-config-based-hooks.sh
Signature · 1 line
2.32.0.402.g57bb445576-goog
PATCH v4 1/9 hook: run a list of hooks instead
To prepare for multihook support, teach hook.[hc] to take a list of hooks at run_hooks and run_found_hooks. Right now the list is always one entry, but in the future we will allow users to supply more than one executable for a single hook event.
--- builtin/hook.c | 9 +++--- hook.c | 85 ++++++++++++++++++++++++++++++++++++++++---------- hook.h | 15 ++++++++- 3 files changed, 87 insertions(+), 22 deletions(-)
builtin/hook.c +5 -4
@@ -25,7 +25,7 @@ static int run(int argc, const char **argv, const char *prefix)int rc =0 ;int ignore_missing =0 ;const char *hook_name; -const char *hook_path; +struct list_head *hooks;struct option run_options[] = { OPT_BOOL(0, "ignore-missing", &ignore_missing,
@@ -58,15 +58,16 @@ static int run(int argc, const char **argv, const char *prefix)* run_found_hooks() instead... */ hook_name = argv[0 ]; - hook_path =find_hook (hook_name); -if (!hook_path) { + hooks =hook_list (hook_name); +if (list_empty (hooks)) {/* ... act like run_hooks() under --ignore-missing */ if (ignore_missing)return 0 ;error ("cannot find a hook named %s" , hook_name);return 1 ; } - rc =run_found_hooks (hook_name, hook_path, &opt); + + rc =run_found_hooks (hook_name, hooks, &opt);run_hooks_opt_clear (&opt);
hook.c +68 -17
153 lines not on this page · raw message
hook.h +14 -1
@@ -3,6 +3,7 @@#include "strbuf.h" #include "strvec.h" #include "run-command.h" +#include "list.h" /* * Returns the path to the hook file, or NULL if the hook is missing
@@ -17,6 +18,7 @@ const char *find_hook(const char *name);int hook_exists (const char *hookname);struct hook { +struct list_head list;/* The path to the hook */ const char *hook_path;
@@ -27,6 +29,12 @@ struct hook {void *feed_pipe_cb_data; }; +/* +* Provides a linked list of 'struct hook' detailing commands which should run +* in response to the 'hookname' event, in execution order. +*/ +struct list_head *hook_list (const char *hookname); +struct run_hooks_opt {/* Environment vars to be set for each hook */
@@ -103,6 +111,7 @@ struct hook_cb_data {/* rc reflects the cumulative failure state */ int rc;const char *hook_name; +struct list_head *head;struct hook *run_me;struct run_hooks_opt *options;int *invoked_hook;
@@ -120,6 +129,10 @@ int run_hooks(const char *hook_name, struct run_hooks_opt *options);* Takes an already resolved hook and runs it. Internally the simpler * run_hooks() will call this. */ -int run_found_hooks (const char *hookname,const char *hook_path, +int run_found_hooks (const char *hookname,struct list_head *hooks,struct run_hooks_opt *options); + +/* Empties the list at 'head', calling 'free_hook()' on each entry */ +void clear_hook_list (struct list_head *head); +#endif
Signature · 1 line
2.32.0.402.g57bb445576-goog
PATCH v4 2/9 hook: allow parallel hook execution
In reply to Emily ShafferIn many cases, there's no reason not to allow hooks to execute in parallel. run_processes_parallel() is well-suited - it's a task queue that runs its housekeeping in series, which means users don't need to worry about thread safety on their callback data. True multithreaded execution with the async_* functions isn't necessary here. Synchronous hook execution can be achieved by only allowing 1 job to run at a time.
Teach run_hooks() to use that function for simple hooks which don't require stdin or capture of stderr.
--- Documentation/config/hook.txt | 4 ++++ Documentation/git-hook.txt | 17 ++++++++++++++++- builtin/am.c | 12 ++++++++---- builtin/checkout.c | 3 ++- builtin/clone.c | 3 ++- builtin/gc.c | 3 ++- builtin/hook.c | 6 +++++- builtin/merge.c | 3 ++- builtin/rebase.c | 3 ++- builtin/receive-pack.c | 14 ++++++++++---- builtin/worktree.c | 3 ++- commit.c | 4 +++- hook.c | 32 +++++++++++++++++++++++++++++--- hook.h | 11 +++++------ read-cache.c | 3 ++- refs.c | 3 ++- reset.c | 4 +++- sequencer.c | 7 +++++-- transport.c | 4 +++- 19 files changed, 107 insertions(+), 32 deletions(-) create mode 100644 Documentation/config/hook.txt
Documentation/config/hook.txt new file +4 -0
@@ -0,0 +1,4 @@ +hook.jobs:: + Specifies how many hooks can be run simultaneously during parallelized + hook execution. If unspecified, defaults to the number of processors on + the current system.
Documentation/git-hook.txt +16 -1
@@ -8,7 +8,8 @@ git-hook - run git hooks SYNOPSIS -------- [verse] -'git hook' run [--to-stdin=<path>] [--ignore-missing] <hook-name> [-- <hook-args>] +'git hook' run [--to-stdin=<path>] [--ignore-missing] [(-j|--jobs) <n>] + <hook-name> [-- <hook-args>] DESCRIPTION -----------
@@ -42,6 +43,20 @@ OPTIONS tools that want to do a blind one-shot run of a hook that may or may not be present. +-j:: +--jobs:: + Only valid for `run`. ++ +Specify how many hooks to run simultaneously. If this flag is not specified, use +the value of the `hook.jobs` config. If the config is not specified, use the +number of CPUs on the current system. Some hooks may be ineligible for +parallelization: for example, 'commit-msg' intends hooks modify the commit +message body and cannot be parallelized. + +CONFIGURATION +------------- +include::config/hook.txt[] + SEE ALSO -------- linkgit:githooks[5]
builtin/am.c +8 -4
@@ -445,8 +445,9 @@ static void am_destroy(const struct am_state *state)static int run_applypatch_msg_hook (struct am_state *state) {int ret; -struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt opt; +run_hooks_opt_init_sync (&opt);assert (state->msg);strvec_push (&opt.args,am_path (state,"final-commit" )); ret =run_hooks ("applypatch-msg" , &opt);
@@ -467,9 +468,10 @@ static int run_applypatch_msg_hook(struct am_state *state)*/ static int run_post_rewrite_hook (const struct am_state *state) { -struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt opt;int ret; +run_hooks_opt_init_async (&opt);strvec_push (&opt.args,"rebase" ); opt.path_to_stdin =am_path (state,"rewritten" );
@@ -1602,9 +1604,10 @@ static void do_commit(const struct am_state *state)struct commit_list *parents =NULL ;const char *reflog_msg, *author, *committer =NULL ;struct strbuf sb =STRBUF_INIT ; -struct run_hooks_opt hook_opt_pre =RUN_HOOKS_OPT_INIT ; -struct run_hooks_opt hook_opt_post =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt hook_opt_pre; +struct run_hooks_opt hook_opt_post; +run_hooks_opt_init_async (&hook_opt_pre);if (run_hooks ("pre-applypatch" , &hook_opt_pre)) {run_hooks_opt_clear (&hook_opt_pre);exit (1 );
@@ -1659,6 +1662,7 @@ static void do_commit(const struct am_state *state)fclose (fp); } +run_hooks_opt_init_async (&hook_opt_post);run_hooks ("post-applypatch" , &hook_opt_post);run_hooks_opt_clear (&hook_opt_pre);
builtin/checkout.c +2 -1
@@ -107,9 +107,10 @@ struct branch_info {static int post_checkout_hook (struct commit *old_commit,struct commit *new_commit,int changed) { -struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt opt;int rc; +run_hooks_opt_init_sync (&opt);/* "new_commit" can be NULL when checking out from the index before a commit exists. */ strvec_pushl(&opt.args,
builtin/clone.c +2 -1
@@ -776,7 +776,7 @@ static int checkout(int submodule_progress)struct tree *tree;struct tree_desc t;int err =0 ; -struct run_hooks_opt hook_opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt hook_opt;if (option_no_checkout)return 0 ;
@@ -822,6 +822,7 @@ static int checkout(int submodule_progress)if (write_locked_index (&the_index, &lock_file,COMMIT_LOCK ))die (_ ("unable to write new index file" )); +run_hooks_opt_init_sync (&hook_opt);strvec_pushl (&hook_opt.args,oid_to_hex (null_oid ()),oid_to_hex (&oid),"1" ,NULL ); err |=run_hooks ("post-checkout" , &hook_opt);run_hooks_opt_clear (&hook_opt);
builtin/gc.c +2 -1
@@ -349,7 +349,7 @@ static void add_repack_incremental_option(void)static int need_to_gc (void ) { -struct run_hooks_opt hook_opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt hook_opt;/* * Setting gc.auto to 0 or negative can disable the
@@ -397,6 +397,7 @@ static int need_to_gc(void)else return 0 ; +run_hooks_opt_init_async (&hook_opt);if (run_hooks ("pre-auto-gc" , &hook_opt)) {run_hooks_opt_clear (&hook_opt);return 0 ;
builtin/hook.c +5 -1
@@ -21,7 +21,7 @@ static const char * const builtin_hook_run_usage[] = {static int run (int argc,const char **argv,const char *prefix) {int i; -struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt opt;int rc =0 ;int ignore_missing =0 ;const char *hook_name;
@@ -32,9 +32,13 @@ static int run(int argc, const char **argv, const char *prefix) N_("exit quietly with a zero exit code if the requested hook cannot be found")),OPT_STRING (0 ,"to-stdin" , &opt.path_to_stdin,N_ ("path" ),N_ ("file to read into hooks' stdin" )), +OPT_INTEGER ('j' ,"jobs" , &opt.jobs, +N_ ("run up to <n> hooks simultaneously" )),OPT_END (), }; +run_hooks_opt_init_async (&opt); + argc =parse_options (argc, argv, prefix, run_options, builtin_hook_run_usage,PARSE_OPT_KEEP_UNKNOWN |PARSE_OPT_KEEP_DASHDASH );
builtin/merge.c +2 -1
@@ -448,7 +448,7 @@ static void finish(struct commit *head_commit, const struct object_id *new_head, const char *msg) { struct strbuf reflog_message = STRBUF_INIT; -struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt opt;const struct object_id *head = &head_commit->object.oid;if (!msg)
@@ -490,6 +490,7 @@ static void finish(struct commit *head_commit, }/* Run a post-merge hook */ +run_hooks_opt_init_async (&opt);strvec_push (&opt.args, squash ?"1" :"0" );run_hooks ("post-merge" , &opt);run_hooks_opt_clear (&opt);
builtin/rebase.c +2 -1
@@ -1314,7 +1314,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)char *squash_onto_name =NULL ;int reschedule_failed_exec =-1 ;int allow_preemptive_ff =1 ; -struct run_hooks_opt hook_opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt hook_opt;struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, N_("revision"),
@@ -2024,6 +2024,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) }/* If a hook exists, give it a chance to interrupt*/ +run_hooks_opt_init_async (&hook_opt);strvec_pushl (&hook_opt.args, options.upstream_arg, argc ? argv[0 ] :NULL ,NULL );if (!ok_to_skip_pre_rebase &&run_hooks ("pre-rebase" , &hook_opt)) {
builtin/receive-pack.c +10 -4
@@ -910,7 +910,7 @@ static int run_receive_hook(struct command *commands,int skip_broken, const struct string_list *push_options) { - struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; + struct run_hooks_opt opt;struct receive_hook_feed_context ctx;int rc;struct command *iter = commands;
@@ -922,6 +922,7 @@ static int run_receive_hook(struct command *commands,return 0 ;/* pre-receive hooks should run in series as the hook updates refs */ +run_hooks_opt_init_async (&opt);if (!strcmp (hook_name,"pre-receive" )) opt.jobs =1 ;
@@ -956,9 +957,10 @@ static int run_receive_hook(struct command *commands,static int run_update_hook (struct command *cmd) { -struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt opt;int code; +run_hooks_opt_init_async (&opt); strvec_pushl(&opt.args, cmd->ref_name, oid_to_hex(&cmd->old_oid),
@@ -1443,7 +1445,10 @@ static const char *push_to_checkout(unsigned char *hash,struct strvec *env, const char *work_tree) { - struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; + struct run_hooks_opt opt; + + +run_hooks_opt_init_sync (&opt); opt.invoked_hook = invoked_hook;strvec_pushf (env,"GIT_WORK_TREE=%s" ,absolute_path (work_tree));
@@ -1642,8 +1647,9 @@ static const char *update(struct command *cmd, struct shallow_info *si)static void run_update_post_hook (struct command *commands) {struct command *cmd; -struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt opt; +run_hooks_opt_init_async (&opt);for (cmd = commands; cmd; cmd = cmd->next) {if (cmd->error_string || cmd->did_not_exist)continue ;
builtin/worktree.c +2 -1
@@ -382,8 +382,9 @@ static int add_worktree(const char *path, const char *refname,* is_junk is cleared, but do return appropriate code when hook fails. */ if (!ret && opts->checkout) { -struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt opt; +run_hooks_opt_init_sync (&opt);strvec_pushl (&opt.env,"GIT_DIR" ,"GIT_WORK_TREE" ,NULL ); strvec_pushl(&opt.args, oid_to_hex(null_oid()),
commit.c +3 -1
@@ -1700,10 +1700,12 @@ int run_commit_hook(int editor_is_used, const char *index_file,int *invoked_hook, const char *name, ...) { - struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; + struct run_hooks_opt opt;va_list args;const char *arg;int ret; + +run_hooks_opt_init_sync (&opt);strvec_pushf (&opt.env,"GIT_INDEX_FILE=%s" , index_file);/*
hook.c +29 -3
@@ -88,6 +88,14 @@ const char *find_hook(const char *name)return path.buf; } +int configured_hook_jobs (void ) +{ +int n =online_cpus (); +git_config_get_int ("hook.jobs" , &n); + +return n; +} +int hook_exists (const char *name) {return !!find_hook (name);
@@ -117,6 +125,26 @@ struct list_head* hook_list(const char* hookname)return hook_head; } +void run_hooks_opt_init_sync (struct run_hooks_opt *o) +{ +strvec_init (&o->env); +strvec_init (&o->args); + o->path_to_stdin =NULL ; + o->jobs =1 ; + o->dir =NULL ; + o->feed_pipe =NULL ; + o->feed_pipe_ctx =NULL ; + o->consume_sideband =NULL ; + o->invoked_hook =NULL ; + o->absolute_path =0 ; +} + +void run_hooks_opt_init_async (struct run_hooks_opt *o) +{ +run_hooks_opt_init_sync (o); + o->jobs =configured_hook_jobs (); +} +void run_hooks_opt_clear (struct run_hooks_opt *o) {strvec_clear (&o->env);
@@ -238,11 +266,9 @@ int run_found_hooks(const char *hook_name, struct list_head *hooks, .invoked_hook = options->invoked_hook, }; + cb_data.head = hooks; cb_data.run_me = list_first_entry(hooks, struct hook, list); -if (options->jobs !=1 ) -BUG ("we do not handle %d or any other != 1 job number yet" , options->jobs); - run_processes_parallel_tr2(options->jobs, pick_next_hook, notify_start_failure,
hook.h +5 -6
@@ -35,6 +35,9 @@ struct hook {*/ struct list_head *hook_list (const char *hookname); +/* Provides the number of threads to use for parallel hook execution. */ +int configured_hook_jobs (void ); +struct run_hooks_opt {/* Environment vars to be set for each hook */
@@ -93,12 +96,6 @@ struct run_hooks_optint *invoked_hook; }; -#define RUN_HOOKS_OPT_INIT { \ - .jobs = 1, \ - .env = STRVEC_INIT, \ - .args = STRVEC_INIT, \ -} -/* * To specify a 'struct string_list', set 'run_hooks_opt.feed_pipe_ctx' to the * string_list and set 'run_hooks_opt.feed_pipe' to 'pipe_from_string_list()'.
@@ -117,6 +114,8 @@ struct hook_cb_data {int *invoked_hook; }; +void run_hooks_opt_init_sync (struct run_hooks_opt *o); +void run_hooks_opt_init_async (struct run_hooks_opt *o);void run_hooks_opt_clear (struct run_hooks_opt *o);/*
read-cache.c +2 -1
@@ -3063,7 +3063,7 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l {int ret;int was_full = !istate->sparse_index; -struct run_hooks_opt hook_opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt hook_opt; ret =convert_to_sparse (istate);
@@ -3092,6 +3092,7 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *lelse ret =close_lock_file_gently (lock); +run_hooks_opt_init_async (&hook_opt); strvec_pushl(&hook_opt.args, istate->updated_workdir ? "1" : "0", istate->updated_skipworktree ? "1" : "0",
refs.c +2 -1
@@ -2063,7 +2063,7 @@ static int run_transaction_hook(struct ref_transaction *transaction, const char *state) {struct strbuf buf =STRBUF_INIT ; -struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt opt;struct string_list to_stdin =STRING_LIST_INIT_DUP ;int ret =0 , i;char o[GIT_MAX_HEXSZ +1 ], n[GIT_MAX_HEXSZ +1 ];
@@ -2071,6 +2071,7 @@ static int run_transaction_hook(struct ref_transaction *transaction,if (!hook_exists ("reference-transaction" ))return ret; +run_hooks_opt_init_async (&opt);strvec_push (&opt.args, state);for (i =0 ; i < transaction->nr; i++) {
reset.c +3 -1
@@ -128,7 +128,9 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action, reflog_head); }if (run_hook) { -struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt opt; + +run_hooks_opt_init_sync (&opt); strvec_pushl(&opt.args, oid_to_hex(orig ? orig : null_oid()), oid_to_hex(oid),
sequencer.c +5 -2
@@ -1148,11 +1148,13 @@ int update_head_with_reflog(const struct commit *old_head,static int run_rewrite_hook (const struct object_id *oldoid,const struct object_id *newoid) { -struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt opt;struct strbuf tmp =STRBUF_INIT ;struct string_list to_stdin =STRING_LIST_INIT_DUP ;int code; +run_hooks_opt_init_async (&opt); +strvec_push (&opt.args,"amend" ); strbuf_addf(&tmp,
@@ -4524,7 +4526,7 @@ static int pick_commits(struct repository *r,if (!stat (rebase_path_rewritten_list (), &st) && st.st_size >0 ) {struct child_process notes_cp =CHILD_PROCESS_INIT ; -struct run_hooks_opt hook_opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt hook_opt; notes_cp.in =open (rebase_path_rewritten_list (),O_RDONLY ); notes_cp.git_cmd =1 ;
@@ -4534,6 +4536,7 @@ static int pick_commits(struct repository *r,/* we don't care if this copying failed */ run_command (¬es_cp); +run_hooks_opt_init_async (&hook_opt); hook_opt.path_to_stdin =rebase_path_rewritten_list ();strvec_push (&hook_opt.args,"rebase" );run_hooks ("post-rewrite" , &hook_opt);
transport.c +3 -1
@@ -1200,11 +1200,13 @@ static int run_pre_push_hook(struct transport *transport, struct ref *remote_refs) {int ret =0 ; -struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt opt;struct strbuf tmp =STRBUF_INIT ;struct ref *r;struct string_list to_stdin =STRING_LIST_INIT_DUP ; +run_hooks_opt_init_async (&opt); +strvec_push (&opt.args, transport->remote->name);strvec_push (&opt.args, transport->url);
Signature · 1 line
2.32.0.402.g57bb445576-goog
Re: [PATCH 2/9] hook: allow parallel hook execution
Quoted 10 lines · Emily Shaffer
In many cases, there's no reason not to allow hooks to execute in parallel.
run_processes_parallel()is well-suited - it's a task queue that runs its housekeeping in series, which means users don't need to worry about thread safety on their callback data. True multithreaded execution with the async_* functions isn't necessary here. Synchronous hook execution can be achieved by only allowing 1 job to run at a time.Teach
run_hooks()to use that function for simple hooks which don't require stdin or capture of stderr.
This doesn't mention...
builtin/am.c static void am_destroy(const struct am_state *state) · line 450
Quoted 5 lines
builtin/am.c +2
-1int ret; -struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt opt; +run_hooks_opt_init_sync (&opt);
...why we need to bring the s/macro/func/ init pattern, back, but looking ahead...
Quoted 24 lines
hook.c +17
-0+int configured_hook_jobs (void )a +{ +int n =online_cpus (); +git_config_get_int ("hook.jobs" , &n); + +return n; +} +int hook_exists (const char *name) {return !!find_hook (name);@@ -117,6 +125,26 @@ struct list_head* hook_list(const char* hookname)return hook_head; } +void run_hooks_opt_init_sync (struct run_hooks_opt *o) +{ +strvec_init (&o->env); +strvec_init (&o->args); + o->path_to_stdin =NULL ; + o->jobs =1 ; + o->dir =NULL ; + o->feed_pipe =NULL ; + o->feed_pipe_ctx =NULL ;
hook.c struct list_head* hook_list(const char* hookname) · line 146
Quoted 10 lines
hook.c +10
-0+ o->consume_sideband =NULL ; + o->invoked_hook =NULL ; + o->absolute_path =0 ; +} + +void run_hooks_opt_init_async (struct run_hooks_opt *o) +{ +run_hooks_opt_init_sync (o); + o->jobs =configured_hook_jobs (); +}
...okey, so it's because you brought back the "call jobs function" in one of the init functions.
I had a comment in a previous round, I found
https://lore.kernel.org/git/[email protected]/, but I think there was a later one where I commented on the "jobs" field specifically.
Anyway, it seems much easier to me to just keep the simpler macro init and then:
hook.c int run_found_hooks(const char *hook_name, struct list_head *hooks, · line 274
Quoted 6 lines
hook.c +0
-3-if (options->jobs !=1 ) -BUG ("we do not handle %d or any other != 1 job number yet" , options->jobs); - run_processes_parallel_tr2(options->jobs, pick_next_hook, notify_start_failure,
There's this one place where we use the "jobs" parameter, just do something like this there:
int configured_hook_jobs(void)
{
static int jobs;
if (!jobs)
return jobs;
if (git_config_get_int("hook.jobs", &jobs))
jobs = online_cpus();
return jobs;
}I.e. you also needlessly call online_cpus() when we're about to override it in the config. The git_config_get_int()'s return value indicates whether you need to do that. Then just:
int jobs = options->jobs ? options->jobs : configured_hook_jobs();
run_processes_parallel_tr2(jobs, [...]);Or some such, i.e. we can defer getting the job number away from startup to when we actually need to start those jobs, and your whole use of a function init pattern came down to doing that really early.
As an aside if you /do/ need to do init-via-function my 5726a6b4012 (*.c *_init(): define in terms of corresponding *_INIT macro, 2021-07-01) in "next" shows a much nicer way to do that. I.e. you'd just do:
void run_hooks_opt_init_sync(struct run_hooks_opt *o)
{
struct run_hooks_opt blank = RUN_HOOKS_OPT_INIT;
memcpy(o, &blank, sizeof(*o));
}
void run_hooks_opt_init_async(struct run_hooks_opt *o)
{
run_hooks_opt_init_sync(o);
o->jobs = configured_hook_jobs();
}In some cases we do actually need to do init via functions, but can init a large option via the macro, which IMO is nicer to read, but here I think we don't need the functions at all per the above.
Re: [PATCH 2/9] hook: allow parallel hook execution
Quoted 92 lines · Ævar Arnfjörð Bjarmason
Quoted 10 lines · Emily Shaffer
In many cases, there's no reason not to allow hooks to execute in parallel.
run_processes_parallel()is well-suited - it's a task queue that runs its housekeeping in series, which means users don't need to worry about thread safety on their callback data. True multithreaded execution with the async_* functions isn't necessary here. Synchronous hook execution can be achieved by only allowing 1 job to run at a time.Teach
run_hooks()to use that function for simple hooks which don't require stdin or capture of stderr.This doesn't mention...
builtin/am.c static void am_destroy(const struct am_state *state) · line 450
Quoted 5 lines
builtin/am.c +2
-1int ret; -struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt opt; +run_hooks_opt_init_sync (&opt);...why we need to bring the s/macro/func/ init pattern, back, but looking ahead...
Quoted 24 lines
hook.c +17
-0+int configured_hook_jobs (void )a +{ +int n =online_cpus (); +git_config_get_int ("hook.jobs" , &n); + +return n; +} +int hook_exists (const char *name) {return !!find_hook (name);@@ -117,6 +125,26 @@ struct list_head* hook_list(const char* hookname)return hook_head; } +void run_hooks_opt_init_sync (struct run_hooks_opt *o) +{ +strvec_init (&o->env); +strvec_init (&o->args); + o->path_to_stdin =NULL ; + o->jobs =1 ; + o->dir =NULL ; + o->feed_pipe =NULL ; + o->feed_pipe_ctx =NULL ;hook.c struct list_head* hook_list(const char* hookname) · line 146
Quoted 10 lines
hook.c +10
-0+ o->consume_sideband =NULL ; + o->invoked_hook =NULL ; + o->absolute_path =0 ; +} + +void run_hooks_opt_init_async (struct run_hooks_opt *o) +{ +run_hooks_opt_init_sync (o); + o->jobs =configured_hook_jobs (); +}...okey, so it's because you brought back the "call jobs function" in one of the init functions.
I had a comment in a previous round, I found
https://lore.kernel.org/git/[email protected]/, but I think there was a later one where I commented on the "jobs" field specifically.Anyway, it seems much easier to me to just keep the simpler macro init and then:
hook.c int run_found_hooks(const char *hook_name, struct list_head *hooks, · line 274
Quoted 6 lines
hook.c +0
-3-if (options->jobs !=1 ) -BUG ("we do not handle %d or any other != 1 job number yet" , options->jobs); - run_processes_parallel_tr2(options->jobs, pick_next_hook, notify_start_failure,There's this one place where we use the "jobs" parameter, just do something like this there:
int configured_hook_jobs(void) { static int jobs; if (!jobs) return jobs; if (git_config_get_int("hook.jobs", &jobs)) jobs = online_cpus(); return jobs; }
Quoted 6 lines
I.e. you also needlessly call
online_cpus()when we're about to override it in the config. The git_config_get_int()'s return value indicates whether you need to do that. Then just:int jobs = options->jobs ? options->jobs : configured_hook_jobs(); run_processes_parallel_tr2(jobs, [...]);
Ahh, and then let RUN_HOOKS_OPT_INIT_ASYNC set jobs to 0 ("go look it up"). Yeah, that makes sense.
Shout if somehow you meant to leave just one initializer macro; otherwise, I'll do it this way - with RUN_HOOKS_OPT_INIT_ASYNC and RUN_HOOKS_OPT_INIT_SYNC. I think it's valuable for hook callers to make it very plain at the callsite whether they're parallelizable or not, and I think
struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
opt.jobs = 0;
doesn't make that as obvious.
Quoted 24 lines
Or some such, i.e. we can defer getting the job number away from startup to when we actually need to start those jobs, and your whole use of a function init pattern came down to doing that really early.
As an aside if you /do/ need to do init-via-function my
5726a6b4012(*.c*_init(): define in terms of corresponding*_INITmacro, 2021-07-01) in "next" shows a much nicer way to do that. I.e. you'd just do:void run_hooks_opt_init_sync(struct run_hooks_opt *o) { struct run_hooks_opt blank = RUN_HOOKS_OPT_INIT; memcpy(o, &blank, sizeof(*o)); } void run_hooks_opt_init_async(struct run_hooks_opt *o) { run_hooks_opt_init_sync(o); o->jobs = configured_hook_jobs(); }In some cases we do actually need to do init via functions, but can init a large option via the macro, which IMO is nicer to read, but here I think we don't need the functions at all per the above.
Re: [PATCH 2/9] hook: allow parallel hook execution
Quoted 109 lines · Emily Shaffer
Quoted 92 lines · Ævar Arnfjörð Bjarmason
Quoted 10 lines · Emily Shaffer
In many cases, there's no reason not to allow hooks to execute in parallel.
run_processes_parallel()is well-suited - it's a task queue that runs its housekeeping in series, which means users don't need to worry about thread safety on their callback data. True multithreaded execution with the async_* functions isn't necessary here. Synchronous hook execution can be achieved by only allowing 1 job to run at a time.Teach
run_hooks()to use that function for simple hooks which don't require stdin or capture of stderr.This doesn't mention...
builtin/am.c static void am_destroy(const struct am_state *state) · line 450
Quoted 5 lines
builtin/am.c +2
-1int ret; -struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT ; +struct run_hooks_opt opt; +run_hooks_opt_init_sync (&opt);...why we need to bring the s/macro/func/ init pattern, back, but looking ahead...
Quoted 24 lines
hook.c +17
-0+int configured_hook_jobs (void )a +{ +int n =online_cpus (); +git_config_get_int ("hook.jobs" , &n); + +return n; +} +int hook_exists (const char *name) {return !!find_hook (name);@@ -117,6 +125,26 @@ struct list_head* hook_list(const char* hookname)return hook_head; } +void run_hooks_opt_init_sync (struct run_hooks_opt *o) +{ +strvec_init (&o->env); +strvec_init (&o->args); + o->path_to_stdin =NULL ; + o->jobs =1 ; + o->dir =NULL ; + o->feed_pipe =NULL ; + o->feed_pipe_ctx =NULL ;hook.c struct list_head* hook_list(const char* hookname) · line 146
Quoted 10 lines
hook.c +10
-0+ o->consume_sideband =NULL ; + o->invoked_hook =NULL ; + o->absolute_path =0 ; +} + +void run_hooks_opt_init_async (struct run_hooks_opt *o) +{ +run_hooks_opt_init_sync (o); + o->jobs =configured_hook_jobs (); +}...okey, so it's because you brought back the "call jobs function" in one of the init functions.
I had a comment in a previous round, I found
https://lore.kernel.org/git/[email protected]/, but I think there was a later one where I commented on the "jobs" field specifically.Anyway, it seems much easier to me to just keep the simpler macro init and then:
hook.c int run_found_hooks(const char *hook_name, struct list_head *hooks, · line 274
Quoted 6 lines
hook.c +0
-3-if (options->jobs !=1 ) -BUG ("we do not handle %d or any other != 1 job number yet" , options->jobs); - run_processes_parallel_tr2(options->jobs, pick_next_hook, notify_start_failure,There's this one place where we use the "jobs" parameter, just do something like this there:
int configured_hook_jobs(void) { static int jobs; if (!jobs) return jobs; if (git_config_get_int("hook.jobs", &jobs)) jobs = online_cpus(); return jobs; }Quoted 6 lines
I.e. you also needlessly call
online_cpus()when we're about to override it in the config. The git_config_get_int()'s return value indicates whether you need to do that. Then just:int jobs = options->jobs ? options->jobs : configured_hook_jobs(); run_processes_parallel_tr2(jobs, [...]);Ahh, and then let
RUN_HOOKS_OPT_INIT_ASYNCset jobs to 0 ("go look it up"). Yeah, that makes sense.Shout if somehow you meant to leave just one initializer macro; otherwise, I'll do it this way - with
RUN_HOOKS_OPT_INIT_ASYNCandRUN_HOOKS_OPT_INIT_SYNC. I think it's valuable for hook callers to make it very plain at the callsite whether they're parallelizable or not, and I think
Quoted 4 lines
struct
run_hooks_optopt =RUN_HOOKS_OPT_INIT;
opt.jobs = 0;doesn't make that as obvious.
Yes agreed, sorry about the ambiguity, I meant we should have two init macros, just like e.g. STRING_LIST_INIT_NODUP and STRING_LIST_INIT_DUP.
PATCH v4 3/9 hook: introduce "git hook list"
In reply to Emily ShafferIf more than one hook will be run, it may be useful to see a list of which hooks should be run. At very least, it will be useful for us to test the semantics of multihooks ourselves.
For now, only list the hooks which will run in the order they will run in; later, it might be useful to include more information like where the hooks were configured and whether or not they will run.
--- builtin/hook.c | 43 +++++++++++++++++++++++++++++++++++++++++++ hook.c | 18 ++++++++---------- 2 files changed, 51 insertions(+), 10 deletions(-)
builtin/hook.c +43 -0
@@ -8,6 +8,7 @@static const char *const builtin_hook_usage[] = {N_ ("git hook <command> [...]" ), +N_ ("git hook list <hookname>" ),N_ ("git hook run [<args>] <hook-name> [-- <hook-args>]" ),NULL };
@@ -18,6 +19,46 @@ static const char * const builtin_hook_run_usage[] = {NULL }; +static int list (int argc,const char **argv,const char *prefix) +{ +struct list_head *head, *pos; +const char *hookname =NULL ; +struct strbuf hookdir_annotation =STRBUF_INIT ; + +struct option list_options[] = { +OPT_END (), + }; + + argc =parse_options (argc, argv, prefix, list_options, + builtin_hook_usage,0 ); + +if (argc <1 ) { +usage_msg_opt (_ ("You must specify a hook event name to list." ), + builtin_hook_usage, list_options); + } + + hookname = argv[0 ]; + + head =hook_list (hookname); + +if (list_empty (head)) { +printf (_ ("no commands configured for hook '%s'\n" ), + hookname); +return 0 ; + } + +list_for_each (pos, head) { + struct hook *item = list_entry(pos, struct hook, list); + item = list_entry(pos, struct hook, list); +if (item) +printf ("%s\n" , item->hook_path); + } + +clear_hook_list (head); +strbuf_release (&hookdir_annotation); + +return 0 ; +}static int run (int argc,const char **argv,const char *prefix) {int i;
@@ -88,6 +129,8 @@ int cmd_hook(int argc, const char **argv, const char *prefix)if (!argc)usage_with_options (builtin_hook_usage, builtin_hook_options); +if (!strcmp (argv[0 ],"list" )) +return list (argc, argv, prefix);if (!strcmp (argv[0 ],"run" ))return run (argc, argv, prefix);else
hook.c +8 -10
@@ -104,22 +104,20 @@ int hook_exists(const char *name)struct list_head *hook_list (const char * hookname) {struct list_head *hook_head =xmalloc (sizeof (struct list_head )); +const char *hook_path =find_hook (hookname); +INIT_LIST_HEAD (hook_head);if (!hookname)return NULL ; -if (have_git_dir ()) { -const char *hook_path =find_hook (hookname); - -/* Add the hook from the hookdir */ -if (hook_path) { -struct hook *to_add =xmalloc (sizeof (*to_add)); - to_add->hook_path = hook_path; - to_add->feed_pipe_cb_data =NULL ; -list_add_tail (&to_add->list, hook_head); - } +/* Add the hook from the hookdir */ +if (hook_path) { +struct hook *to_add =xmalloc (sizeof (*to_add)); + to_add->hook_path = hook_path; + to_add->feed_pipe_cb_data =NULL ; +list_add_tail (&to_add->list, hook_head); }return hook_head;
Signature · 1 line
2.32.0.402.g57bb445576-goog
Re: [PATCH 3/9] hook: introduce "git hook list"
builtin/hook.c line 13
Quoted 5 lines · Emily Shaffer
static const char * const builtin_hook_usage[] = {
builtin/hook.c +1
-0N_ ("git hook <command> [...]" ), +N_ ("git hook list <hookname>" ),N_ ("git hook run [<args>] <hook-name> [-- <hook-args>]" ),NULL
Uses <hook-name> already, let's use that too. I can't remember if it's something I changed myself, or if your original version used both and I picked one for consistency, or...
Anyway, I can re-roll the base topic or whatever, but let's have the end result use one or the other.
builtin/hook.c static const char * const builtin_hook_run_usage[] = { · line 38
Quoted 4 lines
builtin/hook.c +4
-0+if (argc <1 ) { +usage_msg_opt (_ ("You must specify a hook event name to list." ), + builtin_hook_usage, list_options); + }
{} braces not needed.
builtin/hook.c int cmd_hook(int argc, const char **argv, const char *prefix) · line 134
Quoted 3 lines
builtin/hook.c +2
-0+if (!strcmp (argv[0 ],"list" )) +return list (argc, argv, prefix);if (!strcmp (argv[0 ],"run" ))
This should be "else if" now.
(Doesn't matter for code execution, just IMO readability, but I'll leave that to you ... :)
Quoted 24 lines
builtin/hook.c +0
-0return run (argc, argv, prefix); elsehook.c +2
-6@@ -104,22 +104,20 @@ int hook_exists(const char *name)struct list_head *hook_list (const char * hookname) {struct list_head *hook_head =xmalloc (sizeof (struct list_head )); +const char *hook_path =find_hook (hookname); +INIT_LIST_HEAD (hook_head);if (!hookname)return NULL ; -if (have_git_dir ()) { -const char *hook_path =find_hook (hookname); - -/* Add the hook from the hookdir */ -if (hook_path) { -struct hook *to_add =xmalloc (sizeof (*to_add));
hook.c int hook_exists(const char *name) · line 120
Quoted 10 lines
hook.c +6
-4- to_add->hook_path = hook_path; - to_add->feed_pipe_cb_data =NULL ; -list_add_tail (&to_add->list, hook_head); - } +/* Add the hook from the hookdir */ +if (hook_path) { +struct hook *to_add =xmalloc (sizeof (*to_add)); + to_add->hook_path = hook_path; + to_add->feed_pipe_cb_data =NULL ; +list_add_tail (&to_add->list, hook_head);
Maybe we should have a INIT for "struct hook" too? This also needlessly leaves behind an un-free'd hook struct in a way that it wouldn't if we just had this on the stack, no?
Re: [PATCH 3/9] hook: introduce "git hook list"
Quoted 14 lines · Ævar Arnfjörð Bjarmason
builtin/hook.c line 13
Quoted 5 lines · Emily Shaffer
static const char * const builtin_hook_usage[] = {
builtin/hook.c +1
-0N_ ("git hook <command> [...]" ), +N_ ("git hook list <hookname>" ),N_ ("git hook run [<args>] <hook-name> [-- <hook-args>]" ),NULL Uses <hook-name> already, let's use that too. I can't remember if it's something I changed myself, or if your original version used both and I picked one for consistency, or...
Quoted 2 lines
Anyway, I can re-roll the base topic or whatever, but let's have the end result use one or the other.
'hook-name' is fine, I'll use that. Thanks for pointing it out.
Quoted 7 lines
builtin/hook.c static const char * const builtin_hook_run_usage[] = { · line 38
Quoted 5 lines
builtin/hook.c +4
-0+if (argc <1 ) { +usage_msg_opt (_ ("You must specify a hook event name to list." ), + builtin_hook_usage, list_options); + }{} braces not needed.
ACK
Quoted 10 lines
builtin/hook.c int cmd_hook(int argc, const char **argv, const char *prefix) · line 134
Quoted 5 lines
builtin/hook.c +2
-0+if (!strcmp (argv[0 ],"list" )) +return list (argc, argv, prefix);if (!strcmp (argv[0 ],"run" ))This should be "else if" now.
(Doesn't matter for code execution, just IMO readability, but I'll leave that to you ... :)
Eh, I think it's easier to read in the strcmps line up, so I will leave it this way ;)
Quoted 31 lines
Quoted 31 lines
builtin/hook.c +0
-0return run (argc, argv, prefix); elsehook.c +4
-10@@ -104,22 +104,20 @@ int hook_exists(const char *name)struct list_head *hook_list (const char * hookname) {struct list_head *hook_head =xmalloc (sizeof (struct list_head )); +const char *hook_path =find_hook (hookname); +INIT_LIST_HEAD (hook_head);if (!hookname)return NULL ; -if (have_git_dir ()) { -const char *hook_path =find_hook (hookname); - -/* Add the hook from the hookdir */ -if (hook_path) { -struct hook *to_add =xmalloc (sizeof (*to_add)); - to_add->hook_path = hook_path; - to_add->feed_pipe_cb_data =NULL ; -list_add_tail (&to_add->list, hook_head); - } +/* Add the hook from the hookdir */ +if (hook_path) {
Quoted 8 lines
hook.c int hook_exists(const char *name) · line 120
Quoted 4 lines
hook.c +4
-0+struct hook *to_add =xmalloc (sizeof (*to_add)); + to_add->hook_path = hook_path; + to_add->feed_pipe_cb_data =NULL ; +list_add_tail (&to_add->list, hook_head);Maybe we should have a INIT for "struct hook" too? This also needlessly leaves behind an un-free'd hook struct in a way that it wouldn't if we just had this on the stack, no?
I can clean it up here, but I don't think we need an initializer for struct hook. This code chunk gets moved into one of the list manipulators (append_or_move() or something) after the config is introduced.
I don't think it leaves an unfreed hook laying around, does it? list_add_tail() uses the malloc'd pointer, and we free() in clear_hook_list(). What am I missing?
- Emily
Re: [PATCH 3/9] hook: introduce "git hook list"
Quoted 36 lines · Emily Shaffer
Quoted 14 lines · Ævar Arnfjörð Bjarmason
builtin/hook.c line 13
Quoted 5 lines · Emily Shaffer
static const char * const builtin_hook_usage[] = {
builtin/hook.c +1
-0N_ ("git hook <command> [...]" ), +N_ ("git hook list <hookname>" ),N_ ("git hook run [<args>] <hook-name> [-- <hook-args>]" ),NULL Uses <hook-name> already, let's use that too. I can't remember if it's something I changed myself, or if your original version used both and I picked one for consistency, or...
Quoted 2 lines
Anyway, I can re-roll the base topic or whatever, but let's have the end result use one or the other.
'hook-name' is fine, I'll use that. Thanks for pointing it out.
Quoted 7 lines
builtin/hook.c static const char * const builtin_hook_run_usage[] = { · line 38
Quoted 5 lines
builtin/hook.c +4
-0+if (argc <1 ) { +usage_msg_opt (_ ("You must specify a hook event name to list." ), + builtin_hook_usage, list_options); + }{} braces not needed.
ACK
Quoted 8 lines
builtin/hook.c int cmd_hook(int argc, const char **argv, const char *prefix) · line 134
Quoted 5 lines
builtin/hook.c +2
-0+if (!strcmp (argv[0 ],"list" )) +return list (argc, argv, prefix);if (!strcmp (argv[0 ],"run" ))This should be "else if" now.
Quoted 5 lines
Quoted 2 lines
(Doesn't matter for code execution, just IMO readability, but I'll leave that to you ... :)
Eh, I think it's easier to read in the strcmps line up, so I will leave it this way ;)
*nod*
Quoted 40 lines
Quoted 31 lines
Quoted 31 lines
builtin/hook.c +0
-0return run (argc, argv, prefix); elsehook.c +4
-10@@ -104,22 +104,20 @@ int hook_exists(const char *name)struct list_head *hook_list (const char * hookname) {struct list_head *hook_head =xmalloc (sizeof (struct list_head )); +const char *hook_path =find_hook (hookname); +INIT_LIST_HEAD (hook_head);if (!hookname)return NULL ; -if (have_git_dir ()) { -const char *hook_path =find_hook (hookname); - -/* Add the hook from the hookdir */ -if (hook_path) { -struct hook *to_add =xmalloc (sizeof (*to_add)); - to_add->hook_path = hook_path; - to_add->feed_pipe_cb_data =NULL ; -list_add_tail (&to_add->list, hook_head); - } +/* Add the hook from the hookdir */ +if (hook_path) {Quoted 8 lines
hook.c int hook_exists(const char *name) · line 120
Quoted 4 lines
hook.c +4
-0+struct hook *to_add =xmalloc (sizeof (*to_add)); + to_add->hook_path = hook_path; + to_add->feed_pipe_cb_data =NULL ; +list_add_tail (&to_add->list, hook_head);Maybe we should have a INIT for "struct hook" too? This also needlessly leaves behind an un-free'd hook struct in a way that it wouldn't if we just had this on the stack, no?
Quoted 8 lines
I can clean it up here, but I don't think we need an initializer for struct hook. This code chunk gets moved into one of the list manipulators (
append_or_move()or something) after the config is introduced.I don't think it leaves an unfreed hook laying around, does it?
list_add_tail()uses the malloc'd pointer, and wefree()inclear_hook_list(). What am I missing?
I don't think you're missing anything. I replied on that "struct hook" in another E-Mail, i.e. I think I just misread parts of this. Thanks.
PATCH v4 4/9 hook: treat hookdir hook specially
In reply to Emily ShafferSoon, we will allow users to specify hooks using the config. These config-specified hooks may require different child_process options than hook executables in the gitdir. So, let's differentiate between hooks coming from the gitdir and hooks coming from the config.
--- hook.c | 3 ++- hook.h | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-)
hook.c +2 -1
@@ -117,6 +117,7 @@ struct list_head* hook_list(const char* hookname)struct hook *to_add =xmalloc (sizeof (*to_add)); to_add->hook_path = hook_path; to_add->feed_pipe_cb_data =NULL ; + to_add->from_hookdir =1 ;list_add_tail (&to_add->list, hook_head); }
@@ -200,7 +201,7 @@ static int pick_next_hook(struct child_process *cp, cp->dir = hook_cb->options->dir;/* add command */ -if (hook_cb->options->absolute_path) +if (run_me->from_hookdir && hook_cb->options->absolute_path)strvec_push (&cp->args,absolute_path (run_me->hook_path));else strvec_push (&cp->args, run_me->hook_path);
hook.h +2 -0
@@ -22,6 +22,8 @@ struct hook {/* The path to the hook */ const char *hook_path; + unsigned from_hookdir : 1; +/* * Use this to keep state for your feed_pipe_fn if you are using * run_hooks_opt.feed_pipe. Otherwise, do not touch it.
Signature · 1 line
2.32.0.402.g57bb445576-goog
Re: [PATCH 4/9] hook: treat hookdir hook specially
Quoted 37 lines · Emily Shaffer
Soon, we will allow users to specify hooks using the config. These config-specified hooks may require different
child_processoptions than hook executables in the gitdir. So, let's differentiate between hooks coming from the gitdir and hooks coming from the config.Signed-off-by Emily Shaffer <[email protected]>--- hook.c | 3 ++- hook.h | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-)hook.c +2
-1@@ -117,6 +117,7 @@ struct list_head* hook_list(const char* hookname)struct hook *to_add =xmalloc (sizeof (*to_add)); to_add->hook_path = hook_path; to_add->feed_pipe_cb_data =NULL ; + to_add->from_hookdir =1 ;list_add_tail (&to_add->list, hook_head); }@@ -200,7 +201,7 @@ static int pick_next_hook(struct child_process *cp, cp->dir = hook_cb->options->dir;/* add command */ -if (hook_cb->options->absolute_path) +if (run_me->from_hookdir && hook_cb->options->absolute_path)strvec_push (&cp->args,absolute_path (run_me->hook_path));else strvec_push (&cp->args, run_me->hook_path);hook.h +0
-0
hook.h struct hook { · line 29
Quoted 8 lines
hook.h +2
-0@@ -22,6 +22,8 @@ struct hook {/* The path to the hook */ const char *hook_path; + unsigned from_hookdir : 1; +/* * Use this to keep state for your feed_pipe_fn if you are using * run_hooks_opt.feed_pipe. Otherwise, do not touch it.
The "from_hookdir" looks like it isn't used until 6/9, and maybe the absolute_path change too? In any case this seems like a carried-forward rebase of
https://lore.kernel.org/git/[email protected]/
or some version thereof.
At this point I tihnk it would be way better to squash this and other such changes that basically add a field to a struct that isn't used yet into whatever commit use/need them.
Re: [PATCH 4/9] hook: treat hookdir hook specially
Quoted 56 lines · Ævar Arnfjörð Bjarmason
Quoted 37 lines · Emily Shaffer
Soon, we will allow users to specify hooks using the config. These config-specified hooks may require different
child_processoptions than hook executables in the gitdir. So, let's differentiate between hooks coming from the gitdir and hooks coming from the config.Signed-off-by Emily Shaffer <[email protected]>--- hook.c | 3 ++- hook.h | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-)hook.c +2
-1@@ -117,6 +117,7 @@ struct list_head* hook_list(const char* hookname)struct hook *to_add =xmalloc (sizeof (*to_add)); to_add->hook_path = hook_path; to_add->feed_pipe_cb_data =NULL ; + to_add->from_hookdir =1 ;list_add_tail (&to_add->list, hook_head); }@@ -200,7 +201,7 @@ static int pick_next_hook(struct child_process *cp, cp->dir = hook_cb->options->dir;/* add command */ -if (hook_cb->options->absolute_path) +if (run_me->from_hookdir && hook_cb->options->absolute_path)strvec_push (&cp->args,absolute_path (run_me->hook_path));else strvec_push (&cp->args, run_me->hook_path);hook.h +0
-0hook.h struct hook { · line 29
Quoted 8 lines
hook.h +2
-0@@ -22,6 +22,8 @@ struct hook {/* The path to the hook */ const char *hook_path; + unsigned from_hookdir : 1; +/* * Use this to keep state for your feed_pipe_fn if you are using * run_hooks_opt.feed_pipe. Otherwise, do not touch it. The "
from_hookdir" looks like it isn't used until 6/9, and maybe theabsolute_pathchange too? In any case this seems like a carried-forward rebase of
https://lore.kernel.org/git/[email protected]/
or some version thereof.
Quoted 3 lines
At this point I tihnk it would be way better to squash this and other such changes that basically add a field to a struct that isn't used yet into whatever commit use/need them.
I think at this point we run into you and me having different patch-storytelling styles - which probably is what led to the big topic restart in the first place ;)
I'd prefer to see the "start using config too" patch stay as small as it can; that's why I ended up with a handful of minor setup commits and then one "and now here's config" commit.
Even if it's different from how you would tell it - is it wrong? (And if it is, that's fine, and I'll change it, but I don't think it is - that's why I structured the series this way.)
- Emily
Re: [PATCH 4/9] hook: treat hookdir hook specially
Quoted 65 lines · Emily Shaffer
Quoted 56 lines · Ævar Arnfjörð Bjarmason
Quoted 37 lines · Emily Shaffer
Soon, we will allow users to specify hooks using the config. These config-specified hooks may require different
child_processoptions than hook executables in the gitdir. So, let's differentiate between hooks coming from the gitdir and hooks coming from the config.Signed-off-by Emily Shaffer <[email protected]>--- hook.c | 3 ++- hook.h | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-)hook.c +2
-1@@ -117,6 +117,7 @@ struct list_head* hook_list(const char* hookname)struct hook *to_add =xmalloc (sizeof (*to_add)); to_add->hook_path = hook_path; to_add->feed_pipe_cb_data =NULL ; + to_add->from_hookdir =1 ;list_add_tail (&to_add->list, hook_head); }@@ -200,7 +201,7 @@ static int pick_next_hook(struct child_process *cp, cp->dir = hook_cb->options->dir;/* add command */ -if (hook_cb->options->absolute_path) +if (run_me->from_hookdir && hook_cb->options->absolute_path)strvec_push (&cp->args,absolute_path (run_me->hook_path));else strvec_push (&cp->args, run_me->hook_path);hook.h +0
-0hook.h struct hook { · line 29
Quoted 8 lines
hook.h +2
-0@@ -22,6 +22,8 @@ struct hook {/* The path to the hook */ const char *hook_path; + unsigned from_hookdir : 1; +/* * Use this to keep state for your feed_pipe_fn if you are using * run_hooks_opt.feed_pipe. Otherwise, do not touch it. The "
from_hookdir" looks like it isn't used until 6/9, and maybe theabsolute_pathchange too? In any case this seems like a carried-forward rebase of
https://lore.kernel.org/git/[email protected]/
or some version thereof.Quoted 3 lines
At this point I tihnk it would be way better to squash this and other such changes that basically add a field to a struct that isn't used yet into whatever commit use/need them.
I think at this point we run into you and me having different patch-storytelling styles - which probably is what led to the big topic restart in the first place ;)
Quoted 7 lines
I'd prefer to see the "start using config too" patch stay as small as it can; that's why I ended up with a handful of minor setup commits and then one "and now here's config" commit.
Even if it's different from how you would tell it - is it wrong? (And if it is, that's fine, and I'll change it, but I don't think it is - that's why I structured the series this way.)
It's not wrong, if you'd like it that way sure.
The only reason I nudged you about it was that I assumed you'd perhaps mostly rebased these one-at-a-time on top of the base topic, before the base topic the patches were much larger.
There is some arbitrary cut-off-limit where it makes sense to split the addition of code to be used later with the actual use, and that limit is a matter of taste.
I thought perhaps given the base topic some of these patches should be squshed given their new size, but if you've looked at them and think it's fine as-is let's leave it at that.
Re: [PATCH 4/9] hook: treat hookdir hook specially
In reply to Emily ShafferHello Emily,
Quoted 55 lines · Emily Shaffer
Quoted 54 lines · Ævar Arnfjörð Bjarmason
Quoted 37 lines · Emily Shaffer
Soon, we will allow users to specify hooks using the config. These config-specified hooks may require different
child_processoptions than hook executables in the gitdir. So, let's differentiate between hooks coming from the gitdir and hooks coming from the config.Signed-off-by Emily Shaffer <[email protected]>--- hook.c | 3 ++- hook.h | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-)hook.c +2
-1@@ -117,6 +117,7 @@ struct list_head* hook_list(const char* hookname)struct hook *to_add =xmalloc (sizeof (*to_add)); to_add->hook_path = hook_path; to_add->feed_pipe_cb_data =NULL ; + to_add->from_hookdir =1 ;list_add_tail (&to_add->list, hook_head); }@@ -200,7 +201,7 @@ static int pick_next_hook(struct child_process *cp, cp->dir = hook_cb->options->dir;/* add command */ -if (hook_cb->options->absolute_path) +if (run_me->from_hookdir && hook_cb->options->absolute_path)strvec_push (&cp->args,absolute_path (run_me->hook_path));else strvec_push (&cp->args, run_me->hook_path);hook.h +0
-0hook.h struct hook { · line 29
Quoted 8 lines
hook.h +2
-0@@ -22,6 +22,8 @@ struct hook {/* The path to the hook */ const char *hook_path; + unsigned from_hookdir : 1; +/* * Use this to keep state for your feed_pipe_fn if you are using * run_hooks_opt.feed_pipe. Otherwise, do not touch it. The "
from_hookdir" looks like it isn't used until 6/9, and maybe theabsolute_pathchange too? In any case this seems like a carried-forward rebase of
https://lore.kernel.org/git/[email protected]/
or some version thereof.
Quoted 7 lines
Quoted 3 lines
At this point I tihnk it would be way better to squash this and other such changes that basically add a field to a struct that isn't used yet into whatever commit use/need them.
I think at this point we run into you and me having different patch-storytelling styles - which probably is what led to the big topic restart in the first place ;)
Yes, but as a reader of the story I prefer not to have to read the entire thing in order to understand it. I prefer each page to tell a small story.
Quoted 3 lines
I'd prefer to see the "start using config too" patch stay as small as it can; that's why I ended up with a handful of minor setup commits and then one "and now here's config" commit.
I'm in favor of small patches too, but not to the point where the patch is not useful by itself.
This is where where patch storytelling and actual storytelling differ:
we don't need Chekhov's guns.
Quoted 3 lines
Even if it's different from how you would tell it - is it wrong? (And if it is, that's fine, and I'll change it, but I don't think it is - that's why I structured the series this way.)
It's not wrong.
But one of the strongest advantages of open source is that you can have many more reviewers than in closed source, and following Linus's law:
"given enough eyeballs, all bugs are shallow", that's a great thing.
But in order to fully take advantage of that you need to write patches in a way that armchair reviewers can simply take a cursory look at the patch and figure out that's obviously correct.
Putting my armchair reviewer hat I cannot do that for this particular patch, I would need to do more work to make sense of it, and while I'm writing this message to explains that, others will simply skip it, and that's a lost opportunity.
Cheers.
Signature · 1 line
Felipe Contreras
Re: [PATCH 4/9] hook: treat hookdir hook specially
Quoted 10 lines · Felipe Contreras
Quoted 8 lines · Emily Shaffer
Quoted 3 lines · Ævar Arnfjörð Bjarmason
At this point I tihnk it would be way better to squash this and other such changes that basically add a field to a struct that isn't used yet into whatever commit use/need them.
I think at this point we run into you and me having different patch-storytelling styles - which probably is what led to the big topic restart in the first place ;)
Quoted 8 lines
Yes, but as a reader of the story I prefer not to have to read the entire thing in order to understand it. I prefer each page to tell a small story.
Putting my armchair reviewer hat I cannot do that for this particular patch, I would need to do more work to make sense of it, and while I'm writing this message to explains that, others will simply skip it, and that's a lost opportunity.
Implicit in what Felipe and Ævar are saying is that a well-structured patch series asks the reviewer to keep only one or two details in mind after reading a patch in order to understand the next patch in the series, and that the reviewer shouldn't be expected to keep a large set of details in mind over several patches. Unlike the author of the patches who can keep all the details in mind at once and understands the series in its entirety, reviewers (usually) don't have such luxury[1]. So, it's important to hand-hold reviewers as much as possible by not asking them to remember a lot of details between patches and by ensuring that the details which they must remember only need to be remembered for a very short time. This is why it is helpful, for instance, to bundle documentation and test updates in the patch with changes to code, so the reviewer can see at a glance that the changes to documentation and tests match the changes to the code, rather than delaying documentation and test updates until later in the series.
[1]: If you've ever read a novel in which the author has multiple story lines running and switches between story lines infrequently, such that when the author switches back to story-line "A", which you last saw 100 pages ago, and you can't remember what was going in "A" or even who the minor characters are anymore, so that you have to go back and reread 10 or 20 pages from the previous time you saw "A", then that's representative of the difficulty reviewers can experience when reading a patch series, except with a patch series, the cognitive load is already quite high. (Very nice run-on sentence I just wrote.)
Re: [PATCH 4/9] hook: treat hookdir hook specially
Quoted 21 lines · Eric Sunshine
Quoted 10 lines · Felipe Contreras
Quoted 8 lines · Emily Shaffer
Quoted 3 lines · Ævar Arnfjörð Bjarmason
At this point I tihnk it would be way better to squash this and other such changes that basically add a field to a struct that isn't used yet into whatever commit use/need them.
I think at this point we run into you and me having different patch-storytelling styles - which probably is what led to the big topic restart in the first place ;)
Quoted 8 lines
Yes, but as a reader of the story I prefer not to have to read the entire thing in order to understand it. I prefer each page to tell a small story.
Putting my armchair reviewer hat I cannot do that for this particular patch, I would need to do more work to make sense of it, and while I'm writing this message to explains that, others will simply skip it, and that's a lost opportunity.
Quoted 16 lines
Implicit in what Felipe and Ævar are saying is that a well-structured patch series asks the reviewer to keep only one or two details in mind after reading a patch in order to understand the next patch in the series, and that the reviewer shouldn't be expected to keep a large set of details in mind over several patches. Unlike the author of the patches who can keep all the details in mind at once and understands the series in its entirety, reviewers (usually) don't have such luxury[1]. So, it's important to hand-hold reviewers as much as possible by not asking them to remember a lot of details between patches and by ensuring that the details which they must remember only need to be remembered for a very short time. This is why it is helpful, for instance, to bundle documentation and test updates in the patch with changes to code, so the reviewer can see at a glance that the changes to documentation and tests match the changes to the code, rather than delaying documentation and test updates until later in the series.
Another important point is that while as a patch author it's natural to push back against possibly unnecessary changes because it would require considerably more work, there's a reason why writers sometimes chose to rewrite entire chapters, and it's because every effort to improve the text as an author would be translated into less effort for the potentially millions of readers.
It's a multiplicative effect.
Sure, programmers don't have millions of readers, but I think it makes sense to do 2x the effort as a patch author to receive 4x the review (at least).
Cheers.
Signature · 1 line
Felipe Contreras
PATCH v4 5/9 hook: allow running non-native hooks
In reply to Emily ShafferAs the hook architecture and 'git hook run' become more featureful, we may find wrappers wanting to use the hook architecture to run their own hooks, thereby getting nice things like parallelism and idiomatic Git configuration for free. Enable this by letting 'git hook run' bypass the known_hooks() check.
We do still want to keep known_hooks() around, though - by die()ing when an internal Git call asks for run_hooks("my-new-hook"), we can remind Git developers to update Documentation/githooks.txt with their new hook, which in turn helps Git users discover this new hook.
--- builtin/hook.c | 4 ++-- hook.c | 32 ++++++++++++++++++++++++++++---- hook.h | 16 +++++++++++++++- 3 files changed, 45 insertions(+), 7 deletions(-)
builtin/hook.c +2 -2
@@ -39,7 +39,7 @@ static int list(int argc, const char **argv, const char *prefix) hookname = argv[0 ]; - head =hook_list (hookname); + head =hook_list (hookname,1 );if (list_empty (head)) { printf(_("no commands configured for hook '%s'\n"),
@@ -103,7 +103,7 @@ static int run(int argc, const char **argv, const char *prefix)* run_found_hooks() instead... */ hook_name = argv[0 ]; - hooks =hook_list (hook_name); + hooks =hook_list (hook_name,1 );if (list_empty (hooks)) {/* ... act like run_hooks() under --ignore-missing */ if (ignore_missing)
hook.c +28 -4
@@ -52,12 +52,21 @@ static int known_hook(const char *name)const char *find_hook (const char *name) { -static struct strbuf path =STRBUF_INIT ; +const char *hook_path;if (!known_hook (name))die (_ ("the hook '%s' is not known to git, should be in hook-list.h via githooks(5)" ), name); + hook_path =find_hook_gently (name); + +return hook_path; +} + +const char *find_hook_gently (const char *name) +{ +static struct strbuf path =STRBUF_INIT ; +strbuf_reset (&path);strbuf_git_path (&path,"hooks/%s" , name);if (access (path.buf,X_OK ) <0 ) {
@@ -101,10 +110,16 @@ int hook_exists(const char *name)return !!find_hook (name); } -struct list_head *hook_list (const char * hookname) +struct hook_config_cb +{ +struct strbuf *hook_key; +struct list_head *list; +}; + +struct list_head *hook_list (const char * hookname,int allow_unknown) {struct list_head *hook_head =xmalloc (sizeof (struct list_head )); -const char *hook_path =find_hook (hookname); +const char *hook_path;INIT_LIST_HEAD (hook_head);
@@ -112,6 +127,11 @@ struct list_head* hook_list(const char* hookname)if (!hookname)return NULL ; +if (allow_unknown) + hook_path =find_hook_gently (hookname); +else + hook_path =find_hook (hookname); +/* Add the hook from the hookdir */ if (hook_path) {struct hook *to_add =xmalloc (sizeof (*to_add));
@@ -291,7 +311,11 @@ int run_hooks(const char *hook_name, struct run_hooks_opt *options)if (options->path_to_stdin && options->feed_pipe)BUG ("choose only one method to populate stdin" ); - hooks =hook_list (hook_name); +/* +* 'git hooks run <hookname>' uses run_found_hooks, so we don't need to +* allow unknown hooknames here. +*/ + hooks =hook_list (hook_name,0 );/* * If you need to act on a missing hook, use run_found_hooks()
hook.h +15 -1
@@ -9,8 +9,16 @@* Returns the path to the hook file, or NULL if the hook is missing * or disabled. Note that this points to static storage that will be * overwritten by further calls to find_hook and run_hook_*. +* +* If the hook is not a native hook (e.g. present in Documentation/githooks.txt) +* find_hook() will die(). find_hook_gently() does not consult the native hook +* list and will check for any hook name in the hooks directory regardless of +* whether it is known. find_hook() should be used by internal calls to hooks, +* and find_hook_gently() should only be used when the hookname was provided by +* the user, such as by 'git hook run my-wrapper-hook'. */ const char *find_hook (const char *name); +const char *find_hook_gently (const char *name);/* * A boolean version of find_hook()
@@ -34,8 +42,14 @@ struct hook {/* * Provides a linked list of 'struct hook' detailing commands which should run * in response to the 'hookname' event, in execution order. +* +* If allow_unknown is unset, hooks will be checked against the hook list +* generated from Documentation/githooks.txt. Otherwise, any hook name will be +* allowed. allow_unknown should only be set when the hook name is provided by +* the user; internal calls to hook_list should make sure the hook they are +* invoking is present in Documentation/githooks.txt. */ -struct list_head *hook_list (const char *hookname); +struct list_head *hook_list (const char *hookname,int allow_unknown);/* Provides the number of threads to use for parallel hook execution. */ int configured_hook_jobs (void );
Signature · 1 line
2.32.0.402.g57bb445576-goog
PATCH v4 6/9 hook: include hooks from the config
In reply to Emily ShafferTeach the hook.[hc] library to parse configs to populare the list of hooks to run for a given event.
Multiple commands can be specified for a given hook by providing multiple "hook.<hookname>.command = <path-to-hook>" lines. Hooks will be run in config order.
For example:
$ git config --list | grep ^hook hook.pre-commit.command=~/bar.sh
$ git hook run # Runs ~/bar.sh # Runs .git/hooks/pre-commit
--- Documentation/config/hook.txt | 5 ++ Documentation/git-hook.txt | 16 +++- builtin/hook.c | 2 +- hook.c | 109 ++++++++++++++++++++--- hook.h | 2 +- t/t1360-config-based-hooks.sh | 159 ++++++++++++++++++++++++++++++++++ 6 files changed, 278 insertions(+), 15 deletions(-) create mode 100755 t/t1360-config-based-hooks.sh
Documentation/config/hook.txt +5 -0
@@ -1,3 +1,8 @@ +hook.<command>.command:: + A command to execute during the <command> hook event. This can be an + executable on your device, a oneliner for your shell, or the name of a + hookcmd. See linkgit:git-hook[1]. + hook.jobs:: Specifies how many hooks can be run simultaneously during parallelized hook execution. If unspecified, defaults to the number of processors on
Documentation/git-hook.txt +14 -2
@@ -18,12 +18,24 @@ This command is an interface to git hooks (see linkgit:githooks[5]). Currently it only provides a convenience wrapper for running hooks for use by git itself. In the future it might gain other functionality. +This command parses the default configuration files for sections like `hook +"<hook name>"`. `hook` is used to describe the commands which will be run during +a particular hook event; commands are run in the order Git encounters them +during the configuration parse (see linkgit:git-config[1]). + +In general, when instructions suggest adding a script to +`.git/hooks/<something>`, you can specify it in the config instead by running +`git config --add hook.<something>.command <path-to-script>` - this way you can +share the script between multiple repos. That is, `cp ~/my-script.sh +~/project/.git/hooks/pre-commit` would become `git config --add +hook.pre-commit.command ~/my-script.sh`. + SUBCOMMANDS ----------- run:: - Run the `<hook-name>` hook. See linkgit:githooks[5] for - the hook names we support. + Runs hooks configured for `<hook-name>`, in the order they are + discovered during the config parse. + Any positional arguments to the hook should be passed after an optional `--` (or `--end-of-options`, see linkgit:gitcli[7]). The
builtin/hook.c +1 -1
@@ -51,7 +51,7 @@ static int list(int argc, const char **argv, const char *prefix) struct hook *item = list_entry(pos, struct hook, list); item = list_entry(pos, struct hook, list);if (item) -printf ("%s\n" , item->hook_path); +printf ("%s\n" , item->command); }clear_hook_list (head);
hook.c +98 -11
152 lines not on this page · raw message
hook.h +1 -1
@@ -28,7 +28,7 @@ int hook_exists(const char *hookname);struct hook {struct list_head list;/* The path to the hook */ -const char *hook_path; +const char *command;unsigned from_hookdir :1 ;
t/t1360-config-based-hooks.sh new file +159 -0
159 lines not on this page · raw message
Signature · 1 line
2.32.0.402.g57bb445576-goog
Re: [PATCH 6/9] hook: include hooks from the config
Quoted 1 line · Emily Shaffer
+static struct hook *
find_hook_by_command(struct list_head *head, const char *command)
nit: "*find[...]" not "* find[...]", also let's wrap the long line.
hook.c static void free_hook(struct hook *ptr) · line 29
Quoted 10 lines
hook.c +10
-0+{ +struct list_head *pos =NULL , *tmp =NULL ; +struct hook *found =NULL ; + +list_for_each_safe (pos, tmp, head) { + struct hook *it = list_entry(pos, struct hook, list); +if (!strcmp (it->command, command)) { +list_del (pos); + found = it; +break ;
Indented with spaces.
Also is there some subtlety in the list macro here or can we just "s/break/return it/" and skip the break/return pattern?
Quoted 1 line
+static struct hook *
append_or_move_hook(struct list_head *head, const char *command)
Same whitespace nits.
hook.c static void free_hook(struct hook *ptr) · line 52
Quoted 7 lines
hook.c +7
-0+if (!to_add) { +/* adding a new hook, not moving an old one */ + to_add =xmalloc (sizeof (*to_add)); + to_add->command = command; + to_add->feed_pipe_cb_data =NULL ; +/* This gets overwritten in hook_list() for hookdir hooks. */ + to_add->from_hookdir =0 ;
I commented on init verbosity elsewhere, i.e. we could do some things via macros, but in this case just having an "init" helper make sense, but we have at least two places copying the same init of all fields, should just be hook_init_hook() or whatever it'll be called. Maybe with a second "from hookdir" param?
hook.c struct hook_config_cb · line 179
Quoted 5 lines
hook.c +5
-0+if (!strcmp (key, hook_key)) { +const char *command = value; +struct strbuf hookcmd_name =STRBUF_INIT ; + +
Nit: 3x\n, not 2x\n
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (!command) { + strbuf_release(&hookcmd_name);
You don't need to strbuf_release() things that you haven't done anything except init'd, but also...
hook.c struct hook_config_cb · line 182
Quoted 1 line
hook.c +1
-0+BUG ("git_config_get_value overwrote a string it shouldn't have" );
...even if that were the case and it called malloc() memory diligence when we're calling BUG() is probably going overboard, and I say that as someone who'll tend to go overboard with it :)
hook.c struct hook_config_cb · line 193
Quoted 11 lines
hook.c +11
-0+ } + +/* TODO: implement skipping hooks */ + +/* TODO: immplement hook aliases */ + +/* +* TODO: implement an option-getting callback, e.g. +* get configs by pattern hookcmd.$value.* +* for each key+value, do_callback(key, value, cb_data) +*/
I think we should drop the TODO and just let the commit message / comments speak to what we actually implement, and subsequent patches can add more features.
hook.c struct hook_config_cb · line 207
Quoted 3 lines
hook.c +2
-1- +struct strbuf hook_key =STRBUF_INIT ; +struct hook_config_cb cb_data = { &hook_key, hook_head };
Let's use designated initializers.
hook.c struct hook_config_cb · line 218
Quoted 11 lines
hook.c +5
-0INIT_LIST_HEAD (hook_head);if (!hookname)return NULL ; +/* Add the hooks from the config, e.g. hook.pre-commit.command */ +strbuf_addf (&hook_key,"hook.%s.command" , hookname); +git_config (hook_config_lookup, &cb_data); + +
Another 3x\n
hook.c static int pick_next_hook(struct child_process *cp, · line 308
Quoted 2 lines
hook.c +2
-0+/* to enable oneliners, let config-specified hooks run in shell */ + cp->use_shell = !run_me->from_hookdir;
I've lost track at this point, but doesn't that mean we're going to use a shell when we run our own do-not-need-a-shell hooks ourselves?
Isn't isatty() more appropriate here, or actually even interactively why is the shell needed (maybe this is answered elswhere...).
Re: [PATCH 6/9] hook: include hooks from the config
Quoted 7 lines · Ævar Arnfjörð Bjarmason
Quoted 1 line · Emily Shaffer
+static struct hook *
find_hook_by_command(struct list_head *head, const char *command)nit: "*find[...]" not "* find[...]", also let's wrap the long line.
ACK
Quoted 13 lines
hook.c static void free_hook(struct hook *ptr) · line 29
Quoted 11 lines
hook.c +10
-0+{ +struct list_head *pos =NULL , *tmp =NULL ; +struct hook *found =NULL ; + +list_for_each_safe (pos, tmp, head) { + struct hook *it = list_entry(pos, struct hook, list); +if (!strcmp (it->command, command)) { +list_del (pos); + found = it; +break ;Indented with spaces.
I don't know how I even did this. *facepalm*
Quoted 3 lines
Also is there some subtlety in the list macro here or can we just "s/break/return it/" and skip the break/return pattern?
I guess it's probably fine, but we'd need the final return anyway ("otherwise returns NULL"). IMO one return is more readable than two returns, so I'd rather leave this.
Quoted 4 lines
Quoted 2 lines
+static struct hook *
append_or_move_hook(struct list_head *head, const char *command)Same whitespace nits.
ACK
Quoted 14 lines
hook.c static void free_hook(struct hook *ptr) · line 52
Quoted 8 lines
hook.c +7
-0+if (!to_add) { +/* adding a new hook, not moving an old one */ + to_add =xmalloc (sizeof (*to_add)); + to_add->command = command; + to_add->feed_pipe_cb_data =NULL ; +/* This gets overwritten in hook_list() for hookdir hooks. */ + to_add->from_hookdir =0 ;I commented on init verbosity elsewhere, i.e. we could do some things via macros, but in this case just having an "init" helper make sense, but we have at least two places copying the same init of all fields, should just be
hook_init_hook()or whatever it'll be called. Maybe with a second "from hookdir" param?
Hm, where is the second place where we init everything? I think with this commit we remove anywhere we're putting together a 'struct hook' manually except during this helper? Hooks from hookdir are initted by 'append_or_move_hook()'ing them to the end of the list and modifying the from_hookdir field, and builtin/hook.c just calls hook_list() (and some list.h helpers to find an entry).
Quoted 15 lines
hook.c struct hook_config_cb · line 179
Quoted 6 lines
hook.c +5
-0+if (!strcmp (key, hook_key)) { +const char *command = value; +struct strbuf hookcmd_name =STRBUF_INIT ; + +Nit: 3x\n, not 2x\n
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (!command) { + strbuf_release(&hookcmd_name);You don't need to
strbuf_release()things that you haven't done anything except init'd, but also...
Quoted 5 lines
hook.c struct hook_config_cb · line 182
Quoted 1 line
hook.c +1
-0+BUG ("git_config_get_value overwrote a string it shouldn't have" );...even if that were the case and it called
malloc()memory diligence when we're callingBUG()is probably going overboard, and I say that as someone who'll tend to go overboard with it :)
:) ok.
Quoted 16 lines
hook.c struct hook_config_cb · line 193
Quoted 12 lines
hook.c +11
-0+ } + +/* TODO: implement skipping hooks */ + +/* TODO: immplement hook aliases */ + +/* +* TODO: implement an option-getting callback, e.g. +* get configs by pattern hookcmd.$value.* +* for each key+value, do_callback(key, value, cb_data) +*/ I think we should drop the TODO and just let the commit message / comments speak to what we actually implement, and subsequent patches can add more features.
Ok, sure.
Quoted 6 lines
hook.c struct hook_config_cb · line 207
Quoted 4 lines
hook.c +2
-1- +struct strbuf hook_key =STRBUF_INIT ; +struct hook_config_cb cb_data = { &hook_key, hook_head };Let's use designated initializers.
ACK
Quoted 7 lines
Quoted 7 lines
hook.c +0
-0INIT_LIST_HEAD (hook_head);if (!hookname)return NULL ;
Quoted 7 lines
hook.c struct hook_config_cb · line 218
Quoted 5 lines
hook.c +5
-0+/* Add the hooks from the config, e.g. hook.pre-commit.command */ +strbuf_addf (&hook_key,"hook.%s.command" , hookname); +git_config (hook_config_lookup, &cb_data); + +Another 3x\n
ACK
Quoted 9 lines
hook.c static int pick_next_hook(struct child_process *cp, · line 308
Quoted 3 lines
hook.c +2
-0+/* to enable oneliners, let config-specified hooks run in shell */ + cp->use_shell = !run_me->from_hookdir;I've lost track at this point, but doesn't that mean we're going to use a shell when we run our own do-not-need-a-shell hooks ourselves?
Isn't
isatty()more appropriate here, or actually even interactively why is the shell needed (maybe this is answered elswhere...).
use_shell means "conditionally guess whether I need to wrap this thing in sh -c" - it doesn't have anything to do with TTY or not. So we need this for something like hook.post-commit.command = echo "made a commit". In this case the entire argv[0] will be the oneliner, which you will need use_shell set for. If we *do* just do something simple, like hook.post-commit.command = /bin/mail, even though use_shell is marked, the child_process runner will notice that there's no reason to wrap in 'sh -c' and so will just run the /bin/mail executable directly.
- Emily
Re: [PATCH 6/9] hook: include hooks from the config
Quoted 26 lines · Emily Shaffer
Quoted 7 lines · Ævar Arnfjörð Bjarmason
Quoted 1 line · Emily Shaffer
+static struct hook *
find_hook_by_command(struct list_head *head, const char *command)nit: "*find[...]" not "* find[...]", also let's wrap the long line.
ACK
Quoted 13 lines
hook.c static void free_hook(struct hook *ptr) · line 29
Quoted 11 lines
hook.c +10
-0+{ +struct list_head *pos =NULL , *tmp =NULL ; +struct hook *found =NULL ; + +list_for_each_safe (pos, tmp, head) { + struct hook *it = list_entry(pos, struct hook, list); +if (!strcmp (it->command, command)) { +list_del (pos); + found = it; +break ;Indented with spaces.
I don't know how I even did this. *facepalm*
Quoted 1 line
Quoted 6 lines
Quoted 2 lines
Also is there some subtlety in the list macro here or can we just "s/break/return it/" and skip the break/return pattern?
I guess it's probably fine, but we'd need the final return anyway ("otherwise returns NULL"). IMO one return is more readable than two returns, so I'd rather leave this.
Sure makes sense. I'd tend to go for two returns, but let's not split hairs on personal style.
Quoted 20 lines
Quoted 4 lines
Quoted 2 lines
+static struct hook *
append_or_move_hook(struct list_head *head, const char *command)Same whitespace nits.
ACK
Quoted 14 lines
hook.c static void free_hook(struct hook *ptr) · line 52
Quoted 8 lines
hook.c +7
-0+if (!to_add) { +/* adding a new hook, not moving an old one */ + to_add =xmalloc (sizeof (*to_add)); + to_add->command = command; + to_add->feed_pipe_cb_data =NULL ; +/* This gets overwritten in hook_list() for hookdir hooks. */ + to_add->from_hookdir =0 ;I commented on init verbosity elsewhere, i.e. we could do some things via macros, but in this case just having an "init" helper make sense, but we have at least two places copying the same init of all fields, should just be
hook_init_hook()or whatever it'll be called. Maybe with a second "from hookdir" param?
Quoted 6 lines
Hm, where is the second place where we init everything? I think with this commit we remove anywhere we're putting together a 'struct hook' manually except during this helper? Hooks from hookdir are initted by 'append_or_move_hook()'ing them to the end of the list and modifying the
from_hookdirfield, andbuiltin/hook.cjust callshook_list()(and somelist.hhelpers to find an entry).
Looking again I think I just misread this then, thanks.
Quoted 9 lines
Quoted 8 lines
hook.c static int pick_next_hook(struct child_process *cp, · line 308
Quoted 2 lines
hook.c +2
-0+/* to enable oneliners, let config-specified hooks run in shell */ + cp->use_shell = !run_me->from_hookdir;I've lost track at this point, but doesn't that mean we're going to use a shell when we run our own do-not-need-a-shell hooks ourselves?
Isn't
isatty()more appropriate here, or actually even interactively why is the shell needed (maybe this is answered elswhere...).
Quoted 9 lines
use_shellmeans "conditionally guess whether I need to wrap this thing insh -c" - it doesn't have anything to do with TTY or not. So we need this for something likehook.post-commit.command = echo "made a commit". In this case the entire argv[0] will be the oneliner, which you will needuse_shellset for. If we *do* just do something simple, likehook.post-commit.command = /bin/mail, even thoughuse_shellis marked, thechild_processrunner will notice that there's no reason to wrap in 'sh -c' and so will just run the/bin/mailexecutable directly.
Ah, I missed that. Makes sense.
PATCH v4 7/9 hook: allow out-of-repo 'git hook' invocations
In reply to Emily ShafferSince hooks can now be supplied via the config, and a config can be present without a gitdir via the global and system configs, we can start to allow 'git hook run' to occur without a gitdir. This enables us to do things like run sendemail-validate hooks when running 'git send-email' from a nongit directory.
It still doesn't make sense to look for hooks in the hookdir in nongit repos, though, as there is no hookdir.
---
Notes:
For hookdir hooks, do we want to run them in nongit dir when core.hooksPath is set? For example, if someone set core.hooksPath in their global config and then ran 'git hook run sendemail-validate' in a nongit dir?
git.c | 2 +- hook.c | 18 ++++++++++-------- t/t1360-config-based-hooks.sh | 13 +++++++++++++ 3 files changed, 24 insertions(+), 9 deletions(-)
git.c +1 -1
@@ -538,7 +538,7 @@ static struct cmd_struct commands[] = { { "grep", cmd_grep, RUN_SETUP_GENTLY }, { "hash-object", cmd_hash_object }, { "help", cmd_help }, - { "hook", cmd_hook, RUN_SETUP }, + { "hook", cmd_hook, RUN_SETUP_GENTLY }, { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY | NO_PARSEOPT }, { "init", cmd_init_db }, { "init-db", cmd_init_db },
hook.c +10 -8
@@ -202,7 +202,6 @@ static int hook_config_lookup(const char *key, const char *value, void *cb_data)struct list_head *hook_list (const char * hookname,int allow_unknown) {struct list_head *hook_head =xmalloc (sizeof (struct list_head )); -const char *hook_path;struct strbuf hook_key =STRBUF_INIT ;struct hook_config_cb cb_data = { &hook_key, hook_head };
@@ -216,14 +215,17 @@ struct list_head* hook_list(const char* hookname, int allow_unknown)git_config (hook_config_lookup, &cb_data); -if (allow_unknown) - hook_path =find_hook_gently (hookname); -else - hook_path =find_hook (hookname); +if (have_git_dir ()) { +const char *hook_path; +if (allow_unknown) + hook_path =find_hook_gently (hookname); +else + hook_path =find_hook (hookname); -/* Add the hook from the hookdir */ -if (hook_path) -append_or_move_hook (hook_head, hook_path)->from_hookdir =1 ; +/* Add the hook from the hookdir */ +if (hook_path) +append_or_move_hook (hook_head, hook_path)->from_hookdir =1 ; + }return hook_head; }
t/t1360-config-based-hooks.sh +13 -0
@@ -34,6 +34,19 @@ test_expect_success 'git hook rejects commands without a hookname' ' test_must_fail git hook list ' +test_expect_success 'git hook runs outside of a repo' ' + setup_hooks && + + cat >expected <<-EOF && + $ROOT/path/def + EOF + + nongit git config --list --global && + + nongit git hook list pre-commit >actual && + test_cmp expected actual +' + test_expect_success 'git hook list orders by config order' ' setup_hooks &&
Signature · 1 line
2.32.0.402.g57bb445576-goog
Re: [PATCH 7/9] hook: allow out-of-repo 'git hook' invocations
Quoted 8 lines · Emily Shaffer
Since hooks can now be supplied via the config, and a config can be present without a gitdir via the global and system configs, we can start to allow 'git hook run' to occur without a gitdir. This enables us to do things like run sendemail-validate hooks when running 'git send-email' from a nongit directory.
It still doesn't make sense to look for hooks in the hookdir in nongit repos, though, as there is no hookdir.
Hrm, I haven't tested but re the discussion we had about RUN_SETUP_GENTLY on my re-rolled base topic is this really just a regression in my changes there?
I.e. I assumed we could do RUN_SETUP for the bug-for-bug compatibility step, but send-email runs out of repo hooks and we just didn't have tests for it, or am I missing something?
Re: [PATCH 7/9] hook: allow out-of-repo 'git hook' invocations
Quoted 13 lines · Ævar Arnfjörð Bjarmason
Quoted 8 lines · Emily Shaffer
Since hooks can now be supplied via the config, and a config can be present without a gitdir via the global and system configs, we can start to allow 'git hook run' to occur without a gitdir. This enables us to do things like run sendemail-validate hooks when running 'git send-email' from a nongit directory.
It still doesn't make sense to look for hooks in the hookdir in nongit repos, though, as there is no hookdir.
Quoted 7 lines
Hrm, I haven't tested but re the discussion we had about
RUN_SETUP_GENTLYon my re-rolled base topic is this really just a regression in my changes there?I.e. I assumed we could do
RUN_SETUPfor the bug-for-bug compatibility step, but send-email runs out of repo hooks and we just didn't have tests for it, or am I missing something?
I'm not sure. I could see a case for you including RUN_SETUP_GENTLY on your series and adding a test for sendemail-validate + core.hooksPath in global config. I think I also don't have support for that case here, actually....
Anyway, it looks like right now git-send-email.perl:validate_patch() doesn't bother if it's out-of-repo, so this wouldn't have worked before (and still won't work even after this change). So either I can add a patch to my series to allow that, or you can modify your patch converting sendemail-validate to 'git hook run' to drop the 'if $repo' line and teach your series to behave correctly with nongit+hooksPath. It looks like in my earlier attempt at the series, I did drop that check and run 'git hook run' no matter what kind of directory we're in.
- Emily
Re: [PATCH 7/9] hook: allow out-of-repo 'git hook' invocations
Quoted 18 lines · Emily Shaffer
Quoted 17 lines · Ævar Arnfjörð Bjarmason
Quoted 8 lines · Emily Shaffer
Since hooks can now be supplied via the config, and a config can be present without a gitdir via the global and system configs, we can start to allow 'git hook run' to occur without a gitdir. This enables us to do things like run sendemail-validate hooks when running 'git send-email' from a nongit directory.
It still doesn't make sense to look for hooks in the hookdir in nongit repos, though, as there is no hookdir.
Hrm, I haven't tested but re the discussion we had about
RUN_SETUP_GENTLYon my re-rolled base topic is this really just a regression in my changes there?
Quoted 8 lines
Quoted 3 lines
I.e. I assumed we could do
RUN_SETUPfor the bug-for-bug compatibility step, but send-email runs out of repo hooks and we just didn't have tests for it, or am I missing something?I'm not sure. I could see a case for you including
RUN_SETUP_GENTLYon your series and adding a test for sendemail-validate + core.hooksPath in global config. I think I also don't have support for that case here, actually....
The narrow goal of the base topic is to be a bug-for-bug compatible version of what we have now on "master", just via a dispatch command/API.
So yeah, that git-send-email.perl behavior looks bizarre, but let's fix it in a separate set of behavior changing patches on top, no?
Quoted 8 lines
Anyway, it looks like right now git-send-email.perl:validate_patch() doesn't bother if it's out-of-repo, so this wouldn't have worked before (and still won't work even after this change). So either I can add a patch to my series to allow that, or you can modify your patch converting sendemail-validate to 'git hook run' to drop the 'if $repo' line and teach your series to behave correctly with nongit+hooksPath. It looks like in my earlier attempt at the series, I did drop that check and run 'git hook run' no matter what kind of directory we're in.
I think this was one of the things that either needed a test change in your series, or I saw tests for changes to existing but untested behavior, I think this was the latter.
I completely agree that the behavior is weird, probably undesired and should be changed. I'm just saying that we should split up refactorings & behavior changes.
PATCH v4 8/9 hook: teach 'hookcmd' config to alias hook scripts
In reply to Emily ShafferTo enable fine-grained options which apply to a single hook executable, and to make it easier for a single executable to be run on multiple hook events, teach "hookcmd.<alias>.config". These can be configured as follows:
[hookcmd.my-linter]
command = ~/my-linter.sh
[hook.pre-commit]
command = my-linter
During the config parse, we can attempt to dereference the 'hook.pre-commit.command' string 'my-linter' and check if it matches any hookcmd names; if so, we can run the command associated with that hookcmd alias instead.
--- Documentation/config/hook.txt | 5 +++++ Documentation/git-hook.txt | 42 +++++++++++++++++++++++++++++++---- hook.c | 9 +++++++- t/t1360-config-based-hooks.sh | 19 ++++++++++++++++ 4 files changed, 70 insertions(+), 5 deletions(-)
Documentation/config/hook.txt +5 -0
@@ -3,6 +3,11 @@ hook.<command>.command:: executable on your device, a oneliner for your shell, or the name of a hookcmd. See linkgit:git-hook[1]. +hookcmd.<name>.command:: + A command to execute during a hook for which <name> has been specified + as a command. This can be an executable on your device or a oneliner for + your shell. See linkgit:git-hook[1]. + hook.jobs:: Specifies how many hooks can be run simultaneously during parallelized hook execution. If unspecified, defaults to the number of processors on
Documentation/git-hook.txt +38 -4
48 lines not on this page · raw message
hook.c +8 -1
@@ -184,7 +184,14 @@ static int hook_config_lookup(const char *key, const char *value, void *cb_data)/* TODO: implement skipping hooks */ -/* TODO: immplement hook aliases */ +/* +* Check if a hookcmd with that name exists. If it doesn't, +* 'git_config_get_value()' is documented not to touch &command, +* so we don't need to do anything. +*/ +strbuf_reset (&hookcmd_name); +strbuf_addf (&hookcmd_name,"hookcmd.%s.command" , command); +git_config_get_value (hookcmd_name.buf, &command);/* * TODO: implement an option-getting callback, e.g.
t/t1360-config-based-hooks.sh +19 -0
@@ -18,6 +18,11 @@ setup_hooks () { test_config_global hook.pre-commit.command "/path/def" --add } +setup_hookcmd () { + test_config hook.pre-commit.command "abc" --add + test_config_global hookcmd.abc.command "/path/abc" --add +} + setup_hookdir () { mkdir .git/hooks write_script .git/hooks/pre-commit <<-EOF
@@ -59,6 +64,20 @@ test_expect_success 'git hook list orders by config order' ' test_cmp expected actual ' +test_expect_success 'git hook list dereferences a hookcmd' ' + setup_hooks && + setup_hookcmd && + + cat >expected <<-EOF && + $ROOT/path/def + $ROOT/path/ghi + $ROOT/path/abc + EOF + + git hook list pre-commit >actual && + test_cmp expected actual +' + test_expect_success 'git hook list reorders on duplicate commands' ' setup_hooks &&
Signature · 1 line
2.32.0.402.g57bb445576-goog
Re: [PATCH 8/9] hook: teach 'hookcmd' config to alias hook scripts
Quoted 39 lines · Emily Shaffer
To enable fine-grained options which apply to a single hook executable, and to make it easier for a single executable to be run on multiple hook events, teach "hookcmd.<alias>.config". These can be configured as follows:
[...]Documentation/config/hook.txt +24
-0@@ -3,6 +3,11 @@ hook.<command>.command:: executable on your device, a oneliner for your shell, or the name of a hookcmd. See linkgit:git-hook[1]. +hookcmd.<name>.command:: + A command to execute during a hook for which <name> has been specified + as a command. This can be an executable on your device or a oneliner for + your shell. See linkgit:git-hook[1]. + +Global config +---- + [hook "post-commit"] + command = "linter" + command = "~/typocheck.sh" + + [hookcmd "linter"] + command = "/bin/linter --c" +---- + +Local config +---- + [hook "prepare-commit-msg"] + command = "linter" + [hook "post-commit"] + command = "python ~/run-test-suite.py" +---- + +With these configs, you'd then run post-commit hooks in this order:
Documentation/config/hook.txt hook.<command>.command::
Quoted 10 lines
Documentation/config/hook.txt +10
-0+ + /bin/linter --c + ~/typocheck.sh + python ~/run-test-suite.py + .git/hooks/post-commit (if present) + +and prepare-commit-msg hooks in this order: + + /bin/linter --c + .git/hooks/prepare-commit-msg (if present)
I still have outstanding feedback on the fundamental design here. I.e. why is this not:
hook.<name>.event = post-commit
hook.<name>.command = <path>
See:
https://lore.kernel.org/git/[email protected]/
As noted there I don't see why not, and more complexity results from the design choice of doing it the way you're proposing. I.e. we can't discover hooks based on config prefixes, and we end up sticking full FS paths in config keys.
Re: [PATCH 8/9] hook: teach 'hookcmd' config to alias hook scripts
Quoted 60 lines · Ævar Arnfjörð Bjarmason
Quoted 39 lines · Emily Shaffer
To enable fine-grained options which apply to a single hook executable, and to make it easier for a single executable to be run on multiple hook events, teach "hookcmd.<alias>.config". These can be configured as follows:
[...]Documentation/config/hook.txt +24
-0@@ -3,6 +3,11 @@ hook.<command>.command:: executable on your device, a oneliner for your shell, or the name of a hookcmd. See linkgit:git-hook[1]. +hookcmd.<name>.command:: + A command to execute during a hook for which <name> has been specified + as a command. This can be an executable on your device or a oneliner for + your shell. See linkgit:git-hook[1]. + +Global config +---- + [hook "post-commit"] + command = "linter" + command = "~/typocheck.sh" + + [hookcmd "linter"] + command = "/bin/linter --c" +---- + +Local config +---- + [hook "prepare-commit-msg"] + command = "linter" + [hook "post-commit"] + command = "python ~/run-test-suite.py" +---- + +With these configs, you'd then run post-commit hooks in this order:Documentation/config/hook.txt hook.<command>.command::
Quoted 10 lines
Documentation/config/hook.txt +10
-0+ + /bin/linter --c + ~/typocheck.sh + python ~/run-test-suite.py + .git/hooks/post-commit (if present) + +and prepare-commit-msg hooks in this order: + + /bin/linter --c + .git/hooks/prepare-commit-msg (if present)I still have outstanding feedback on the fundamental design here. I.e. why is this not:
hook.<name>.event = post-commit
hook.<name>.command = <path>
Quoted 8 lines
See:
https://lore.kernel.org/git/[email protected]/
As noted there I don't see why not, and more complexity results from the design choice of doing it the way you're proposing. I.e. we can't discover hooks based on config prefixes, and we end up sticking full FS paths in config keys.
Interesting. My gut says that it would make it harder to neatly write a config with the same hook running at the very beginning of one event and the very end of another, but I'm not sure whether that's a case to optimize for.
I would also need twice as many lines to run a script I wrote as a hook
- that is, the base case which is probably all most people will need. So with your proposal I *must* name every hook I want to add. Instead of "hook.pre-commit.command = ~/check-for-debug-strings" I need to configure "hook.check-debug-strings.event = pre-commit" and "hook.check-debug-strings.command = ~/check-for-debug-strings". That's a little frustrating, and if I never want to skip that check or move it later or something, it will be extra overhead for no gain for me.
I do agree that your approach bans the regrettably awkward "hookcmd.~/check-for-debug-strings.skip = true", but I'm not sure whether or not it's worth it.
To help us visualize the change, here's some common and complicated tasks and how they look with either schema (let mine be A and yours be B):
#1 - Add a oneliner (not executing a script)
A:
[hook "post-commit"]
command = echo post commit
B:
[hook "oneliner"]
event = post-commit
command = echo post commit
#2 - Execute a script
A:
[hook "post-commit"]
command = ~/my-post-commit-hook
B:
[hook "script"]
event = post-commit
command = ~/my-post-commit-hook
#3 - Run a handful of scripts in a specific order on one event A:
[hook "post-commit"]
command = ~/my-post-commit-1
command = ~/my-post-commit-2
command = ~/my-post-commit-3
B:
[hook "script 1"]
event = post-commit
command = ~/my-post-commit-1
[hook "script 2"]
event = post-commit
command = ~/my-post-commit-2
[hook "script 3"]
event = post-commit
command = ~/my-post-commit-3
#4 - Skip a script configured more globally
A:
(original config)
[hook "post-commit"]
command = /corp/stuff/corp-post-commit
(local config)
[hookcmd "/corp/stuff/corp-post-commit"]
skip = true
OR,
(original config)
[hookcmd "corp-pc"]
command = /corp/stuff/corp-post-commit
[hook "post-commit"]
command = corp-pc
(local config)
[hookcmd "corp-pc"]
skip = true
B:
(original config)
[hook "corp-pc"]
event = post-commit
command = /corp/stuff/corp-post-commit
(local config)
[hook "corp-pc"]
skip = true
#5 - Execute one script for multiple events
A:
[hookcmd "reusable-hook"]
command = /long/path/reusable-hook
[hook "post-commit"]
command = reusable-hook
[hook "pre-commit"]
command = reusable-hook
[hook "prepare-commit-msg"]
command = reusable-hook
B:
[hook "reusable-hook"]
command = /long/path/reusable-hook
event = post-commit
event = pre-commit
event = prepare-commit-msg
#6 - Execute the same script early for one event and late for another event
A:
(global)
[hookcmd "reusable-hook"]
command = /long/path/reusable-hook
[hook "pre-commit"]
command = reusable-hook
(local)
[hook "post-commit"]
command = other
command = hooks
command = reusable-hook
B:
(global)
[hook "reusable-hook"]
command = /long/path/reusable-hook
event = pre-commit
(local)
[hook "other"]
event = post-commit
command = other
[hook "hooks"]
event = post-commit
command = hooks
[hook "reusable-hook"]
event = reusable-hook
I'm not going to comment on which one I like more yet - I think I will study this for a while and let others weigh in on their preferences too. I tried to order the cases from most common to less common. Please feel free to chime in with more use cases that you think would be handy which I forgot :)
- Emily
Re: [PATCH 8/9] hook: teach 'hookcmd' config to alias hook scripts
Quoted 75 lines · Emily Shaffer
Quoted 60 lines · Ævar Arnfjörð Bjarmason
Quoted 39 lines · Emily Shaffer
To enable fine-grained options which apply to a single hook executable, and to make it easier for a single executable to be run on multiple hook events, teach "hookcmd.<alias>.config". These can be configured as follows:
[...]Documentation/config/hook.txt +24
-0@@ -3,6 +3,11 @@ hook.<command>.command:: executable on your device, a oneliner for your shell, or the name of a hookcmd. See linkgit:git-hook[1]. +hookcmd.<name>.command:: + A command to execute during a hook for which <name> has been specified + as a command. This can be an executable on your device or a oneliner for + your shell. See linkgit:git-hook[1]. + +Global config +---- + [hook "post-commit"] + command = "linter" + command = "~/typocheck.sh" + + [hookcmd "linter"] + command = "/bin/linter --c" +---- + +Local config +---- + [hook "prepare-commit-msg"] + command = "linter" + [hook "post-commit"] + command = "python ~/run-test-suite.py" +---- + +With these configs, you'd then run post-commit hooks in this order:Documentation/config/hook.txt hook.<command>.command::
Quoted 10 lines
Documentation/config/hook.txt +10
-0+ + /bin/linter --c + ~/typocheck.sh + python ~/run-test-suite.py + .git/hooks/post-commit (if present) + +and prepare-commit-msg hooks in this order: + + /bin/linter --c + .git/hooks/prepare-commit-msg (if present)I still have outstanding feedback on the fundamental design here. I.e. why is this not:
hook.<name>.event = post-commit
hook.<name>.command = <path>Quoted 8 lines
See:
https://lore.kernel.org/git/[email protected]/
As noted there I don't see why not, and more complexity results from the design choice of doing it the way you're proposing. I.e. we can't discover hooks based on config prefixes, and we end up sticking full FS paths in config keys.
Interesting. My gut says that it would make it harder to neatly write a config with the same hook running at the very beginning of one event and the very end of another, but I'm not sure whether that's a case to optimize for.
Quoted 8 lines
I would also need twice as many lines to run a script I wrote as a hook
- that is, the base case which is probably all most people will need. So with your proposal I *must* name every hook I want to add. Instead of "hook.pre-commit.command = ~/check-for-debug-strings" I need to configure "hook.check-debug-strings.event = pre-commit" and "hook.check-debug-strings.command = ~/check-for-debug-strings". That's a little frustrating, and if I never want to skip that check or move it later or something, it will be extra overhead for no gain for me.
The gain is that "git hook list" becomes a trivial "git config -show-origin --show-scope --get-regexp" wrapper.
So the series either doesn't need "git hook list" or such a thing becomes much less complex, especially given the proposed addition of other features in the area like "git hook edit", i.e. (quoting the linked E-Mail):
As just one example; surely "git config edit <name>" would need to run around and find config files to edit, then open them in a loop for you, no?
Which we'd eventually want for "git config" in general with an --edit-regexp option or whatever, which brings us (well, at least me) back to "then let's just add it to git-config?".
Quoted 3 lines
I do agree that your approach bans the regrettably awkward "hookcmd.~/check-for-debug-strings.skip = true", but I'm not sure whether or not it's worth it.
That design choice also means that you can't expand the path using "git config --get --type=path.
We do have that with the "includeIf" construct, but if we can avoid it we should, it makes it play nicer with other assumptions and features of the config system.
As noted in the follow-up reply while we don't case normalize the LeVeL" part of "ThReE.LeVeL.KeY" that's tolower(), which we know isn't a 1=1 mapping on some
FS's. https://lore.kernel.org/git/[email protected]/
Quoted 3 lines
To help us visualize the change, here's some common and complicated tasks and how they look with either schema (let mine be A and yours be B):
Before diving into that, I'll just say I don't care about the trivial specifics of how this is done, i.e. the bikeshedding of what the config keys etc. are named.
Just in (as noted above) design choices here forcing avoidable complexity in other areas.
Quoted 16 lines
#1 - Add a oneliner (not executing a script)
A:
[hook "post-commit"]
command = echo post commit
B:
[hook "oneliner"]
event = post-commit
command = echo post commit
#2 - Execute a script
A:
[hook "post-commit"]
command = ~/my-post-commit-hook
B:
[hook "script"]
event = post-commit
command = ~/my-post-commit-hook
...
Quoted 8 lines
#3 - Run a handful of scripts in a specific order on one event A:
[hook "post-commit"]
command = ~/my-post-commit-1
command = ~/my-post-commit-2
command = ~/my-post-commit-3
B:
[hook "script 1"]
Quoted 8 lines
event = post-commit
command = ~/my-post-commit-1
[hook "script 2"]
event = post-commit
command = ~/my-post-commit-2
[hook "script 3"]
event = post-commit
command = ~/my-post-commit-3
To reply to all the above, yes, your suggestion comes out ahead in being less verbose.
But isn't the real difference not in the differing prefixes, i.e. hook.* and hookcmd.* (A) v.s. always hook.* (B, which is what I'm mainly focusing on, i.e. what requires the added complexity.
But that in that in your proposed way of doing it it's:
hook.<event-or-name>.*
V.s. my suggestion of:
hook.<name>.*
And thus whenever you have a <event-or-name> that just happens to be a built-in hook listed in githooks(5) we in (A) implicitly expand config like:
hook.post-commit.command = echo foo
To:
hook.post-commit.command = echo hi
hook.post-commit.type = post-commit
But not knowing about "foo" we don't expand:
hook.foo.command = echo foo
To:
hook.foo.command = echo hi
hook.foo.type = foo # This would be an error, or ignored.
But rather leave "dangling" for the user to later supply the "*.event" themselves, i.e.:
hook.foo.command = echo hi
hook.foo.event = post-commit
And means that you presumably need to detect this case and error about it, but my proposed model does not:
hook.post-commit.command = echo hi
# User error: "*.type" and <event-or-name>" does not match for # "hook.*.command"
hook.post-commit.type = pre-commit
And furthermore, that if someone in your model were to do:
hook.verify-commit.command = echo hi
It's "dangling" today, but if a later version of git learns about a "verify-commit" hook we'll start running it unexpectedly.
Because your design conflates the slot for known hook type names and user-supplied names.
On balance I think it's better just to always supply two lines per-hook, but whether we have this proposed shorthand or not is mostly orthogonal to everything I mentioned in
https://lore.kernel.org/git/[email protected]/
I.e. my proposed version could also have it, but thinking about it I think it's not worth it, we should always use <name>, not <event-or-name> for the reasons noted if you'll read ahead...
Quoted 17 lines
#4 - Skip a script configured more globally
A:
(original config)
[hook "post-commit"]
command = /corp/stuff/corp-post-commit
(local config)
[hookcmd "/corp/stuff/corp-post-commit"]
skip = true
OR,
(original config)
[hookcmd "corp-pc"]
command = /corp/stuff/corp-post-commit
[hook "post-commit"]
command = corp-pc
(local config)
[hookcmd "corp-pc"]
skip = true
Quoted 8 lines
B:
(original config)
[hook "corp-pc"]
event = post-commit
command = /corp/stuff/corp-post-commit
(local config)
[hook "corp-pc"]
skip = true
...which are among other things that no, my proposed version doesn't have "skip" at all, see point #3 at
https://lore.kernel.org/git/[email protected]/
I.e. I think the "skip" is a thing that falls out of a complexity of your design that I'm proposing to do away with.
That complexity being that you use <event-or-name> and I use <name>, and you want to in turn support N number of "*.command" for any "hook.<event-or-name>".
I'm suggesting we use the typical "last key wins" semantics, if you want multiple commands for a given hook type you'll just supply multiple "hook.<name>" sections with the same "hook.*.event = type" in all.
The way to skip hooks in my proposal is:
hook.<name>.command = true
Or whatever noop command you want on the RHS. In practice we wouldn't invoke "true", just like we don't invoke "cat" for "PAGER=cat".
But unlike "*.skip" that doesn't require complexity in implementation or user understanding, it just falls naturally out of the rest of the model.
Quoted 8 lines
#5 - Execute one script for multiple events
A:
[hookcmd "reusable-hook"]
command = /long/path/reusable-hook
[hook "post-commit"]
command = reusable-hook
[hook "pre-commit"]
command = reusable-hook
Quoted 8 lines
[hook "prepare-commit-msg"]
command = reusable-hook
B:
[hook "reusable-hook"]
command = /long/path/reusable-hook
event = post-commit
event = pre-commit
event = prepare-commit-msg
It's been so long since I wrote the original E-Mail that I'm having to skim my own summary there & come up with things as I go along, so maybe this conflicts with something I said earlier.
But no, I don't think I meant that *.event should be multi-value, or that that magic is worthwhile. I.e. I think we just want:
[hook "not-a-reusable-section-1"]
command = /long/path/reusable-hook
event = post-commit
[hook "not-a-reusable-section-2"]
command = /long/path/reusable-hook
event = pre-commit
[hook "not-a-reusable-section-3"]
command = /long/path/reusable-hook
event = prepare-commit-msg
I.e. is wanting to use the same command for N different hooks really that common a use-case to avoid a little verbosity, and quite a lot of complexity?
How would such a hook even be implemented?
We don't have anything in your hook execution model (and I don't remember you adding this) which passes down a "you are this type of hook" to hooks.
It's now implicit in that the hook invoked at .git/hooks/post-commit or .git/hooks/pre-commit can check its own basename, but it won't be with configurable hooks.
We could start passing down a GIT_HOOK_TYPE in the environment or whatever, but I think the simpler case of just having the user do it is better.
I'm assuming that mainly because people have wanted a "/long/path/reusable-hook" router script because we have not supported executing N hooks, or supported concurrency. Once we do that the complexity of such routing scripts (mostly, not everyone's needs will be met) will be replaced by a little bit of config.
I don't see why it's worth it to micro-optimize for lines in that config at the cost of increased config complexity.
For example, how do you "skip" an "event" type? You have it for "*.command"? So let's say in your model (A) I have system config like:
[hook "my-reusable"]
command = /some/commit-msg-hook
event = prepare-commit-msg
Now I want to skip that, easy enough, in my local config:
[hook "my-reusable"]
skip = true
Or if I wanted to also invoke it for commit-msg events, in my local config (which piggy-backs on the system one):
# Now runs for both prepare-commit-msg and commit-message [hook "my-reusable"]
event = commit-msg
Or, if I want to skip the command name and substitute my own:
[hook "my-reusable"]
skip = true
command = /my/commit-msg-hook
I'm just assuming you mean that to work, as discussed above I think all this is leading us down the road of too much complexity.
But anyway, now I want to instead not run it as a prepare-commit-msg hook, but a commit-msg hook.
As far as I can tell you haven't specified that, but I think something consistent with your model would be
[hook "my-reusable"]
event = skip
event = commit-msg
Or rather, because that conflates "skip" with a hook event type name:
[hook "my-reusable"]
skipEvent = true
event = commit-msg
I.e. the "skip" you have now really means "skipCommand", so we'll need a skipSomeName to skip other "hook.*.someName".
As noted above I think all of this is too complex, let's just do the system config like this (exact same as the above):
[hook "my-reusable"]
command = /some/commit-msg-hook
event = prepare-commit-msg
To skip it, in my local config:
[hook "my-reusable"]
command = true
And to re-use /some/commit-msg-hook without having to re-include it in your local config the answer is that we don't support that level of cleverness. Just do this:
# First skip the system hook
[hook "my-reusable"]
command = true
# Set up another hook, just copy/pasting the /some/commit-msg-hook command [hook "my-local"]
command = /some/commit-msg-hook
event = commit-msg
I.e. I think these cases of someone having say a /etc/gitconfig hook on their system not under their control that they want to not run, *but* not as the "event" there, but as another event type is too specific a use-case for us to worry about.
Quoted 5 lines
#6 - Execute the same script early for one event and late for another event
A:
(global)
[hookcmd "reusable-hook"]
Quoted 8 lines
command = /long/path/reusable-hook
[hook "pre-commit"]
command = reusable-hook
(local)
[hook "post-commit"]
command = other
command = hooks
command = reusable-hook
Even with I think it's fair to say deep knowledge of your proposal at this point I still needed to read this a few times to see if that:
command = reusable-hook
Is referring to:
[hookcmd "reusable-hook"]
I.e. is it going to run:
command = /long/path/reusable-hook
Or is it just re-specifying /long/path/reusable-hook but relying on a $PATH lookup?
Having reasoned through that I think the answer is the former. But that also means that in your model:
[hookcmd "rm -rf /"]
command = echo this will not actually destroy your data [hook "pre-commit"]
command = rm -rf /
Is going to run that friendly "echo" command, since "command = rm -rf /" just refers to the "rm -rf /" <name>, not <command>, right the "hookcmd" line is removed, at which point we'll stop treating it as a <name> and run it as a <command>?
In practice I think users are unlikely to use actively harmful names like that.
I'm just making the point that I should not need to know about previous config to see if a "hook.pre-commit.command = rm -rf /" is harmless or not, or need to carefully squint to see if the "reusable-hook" is referring to a section name or command name.
Or am I just confused at this point?
Quoted 6 lines
B:
(global)
[hook "reusable-hook"]
command = /long/path/reusable-hook
event = pre-commit
(local)
Quoted 8 lines
[hook "other"]
event = post-commit
command = other
[hook "hooks"]
event = post-commit
command = hooks
[hook "reusable-hook"]
event = reusable-hook
No, that's not what I'm proposing. I.e. "*.event" is reserved for built-in events listed in githooks(5) that git itself knows about.
If you say "event = reusable-hook" that's going to be one of ignored, warned or errored about.
(Probably "ignored" is the best thing to support multi-version setups, but that's not hook-specific, just how we treat unknown config in general)
My version of this would be the same as noted with "we don't support that level of cleverness[...]" above.
I.e. you'd not re-use that "/long/path/reusable-hook", you can skip it through "command = true", then just copy the relevnt part into your new config. So:
(global)
[hook "reusable-hook"]
command = /long/path/reusable-hook event = pre-commit
(local)
[hook "reusable-hook"]
command = true # skip it
# The "hooks" name is arbitrary, "my-hooks" or whatever would be # clearer, but just going with your example...
[hook "hooks"]
event = post-commit command = hooks
# Not very reusable then...
[hook "reusable-hook"]
command = /long/path/reusable-hook event = pre-commit
Quoted 2 lines
[...]Please feel free to chime in with more use cases that you think would be handy which I forgot :)
I couldn't find this at a quick glance but I think we also had a disussion at some point about hooks controlling parallelism. AFAICT your current implementation just has global:
hook.jobs=N
And we then blacklist certain hooks to always have hook.jobs=1, e.g. the commit-msg hook that needs an implicit "lock" on that file (or rather, we think that's the most common use-case).
I think my version of always having hook.<name>.{event,command} be one value is also better in that case, i.e. we'd then:
[hook "myhook"]
command = some-command
event = pre-receive
parallel = true # the default
[hook "myhook2"]
command = some-command2
event = pre-receive
parallel = true # the default
[hook "myhook3"]
command = some-unconcurrent-command event = pre-receive
parallel = false # I'm not OK with concurrency
To go along with something like:
hook
jobs = 8
Or:
[hookEvent "pre-receive"]
jobs = 4
But if you have N numer of "command" in a section it gets murky, does "parallel = false" then apply to the whole section, but sections can have one or more values. So we'd need both a "hookEvent.pre-receive.jobs=N" and per-section config to control/suppress parallelism?
I haven't thought about it deeply, but have a hunch that having sections be a 1=1 mapping with a specific command instead of either 1=1 or 1=many is going to make that easier to implement and understand.
Re: [PATCH 8/9] hook: teach 'hookcmd' config to alias hook scripts
Quoted 88 lines · Ævar Arnfjörð Bjarmason
Quoted 75 lines · Emily Shaffer
Quoted 60 lines · Ævar Arnfjörð Bjarmason
Quoted 39 lines · Emily Shaffer
To enable fine-grained options which apply to a single hook executable, and to make it easier for a single executable to be run on multiple hook events, teach "hookcmd.<alias>.config". These can be configured as follows:
[...]Documentation/config/hook.txt +24
-0@@ -3,6 +3,11 @@ hook.<command>.command:: executable on your device, a oneliner for your shell, or the name of a hookcmd. See linkgit:git-hook[1]. +hookcmd.<name>.command:: + A command to execute during a hook for which <name> has been specified + as a command. This can be an executable on your device or a oneliner for + your shell. See linkgit:git-hook[1]. + +Global config +---- + [hook "post-commit"] + command = "linter" + command = "~/typocheck.sh" + + [hookcmd "linter"] + command = "/bin/linter --c" +---- + +Local config +---- + [hook "prepare-commit-msg"] + command = "linter" + [hook "post-commit"] + command = "python ~/run-test-suite.py" +---- + +With these configs, you'd then run post-commit hooks in this order:Documentation/config/hook.txt hook.<command>.command::
Quoted 10 lines
Documentation/config/hook.txt +10
-0+ + /bin/linter --c + ~/typocheck.sh + python ~/run-test-suite.py + .git/hooks/post-commit (if present) + +and prepare-commit-msg hooks in this order: + + /bin/linter --c + .git/hooks/prepare-commit-msg (if present)I still have outstanding feedback on the fundamental design here. I.e. why is this not:
hook.<name>.event = post-commit
hook.<name>.command = <path>Quoted 8 lines
See:
https://lore.kernel.org/git/[email protected]/
As noted there I don't see why not, and more complexity results from the design choice of doing it the way you're proposing. I.e. we can't discover hooks based on config prefixes, and we end up sticking full FS paths in config keys.
Interesting. My gut says that it would make it harder to neatly write a config with the same hook running at the very beginning of one event and the very end of another, but I'm not sure whether that's a case to optimize for.
Quoted 8 lines
I would also need twice as many lines to run a script I wrote as a hook
- that is, the base case which is probably all most people will need. So with your proposal I *must* name every hook I want to add. Instead of "hook.pre-commit.command = ~/check-for-debug-strings" I need to configure "hook.check-debug-strings.event = pre-commit" and "hook.check-debug-strings.command = ~/check-for-debug-strings". That's a little frustrating, and if I never want to skip that check or move it later or something, it will be extra overhead for no gain for me.
Quoted 2 lines
The gain is that "git hook list" becomes a trivial "git config -show-origin --show-scope --get-regexp" wrapper.
This isn't a very compelling reason to me, if it makes the user experience worse. (I'm not convinced that it does - just saying I disagree with this reasoning.)
Quoted 5 lines
So the series either doesn't need "git hook list" or such a thing becomes much less complex, especially given the proposed addition of other features in the area like "git hook edit", i.e. (quoting the linked E-Mail):
I still think "git hook list" is useful to have for end-users, compared to remembering the appropriate "git config" invocation. It futureproofs us if we do want to change the execution ordering to something besides config order, it's easier to remember/more discoverable, etc.
Quoted 8 lines
As just one example; surely "git config edit <name>" would need to run around and find config files to edit, then open them in a loop for you, no?
Which we'd eventually want for "git config" in general with an --edit-regexp option or whatever, which brings us (well, at least me) back to "then let's just add it to git-config?".
Quoted 6 lines
Quoted 3 lines
I do agree that your approach bans the regrettably awkward "hookcmd.~/check-for-debug-strings.skip = true", but I'm not sure whether or not it's worth it.
That design choice also means that you can't expand the path using "git config --get --type=path.
Yeah, this is a pretty handy point.
Quoted 9 lines
We do have that with the "includeIf" construct, but if we can avoid it we should, it makes it play nicer with other assumptions and features of the config system.
As noted in the follow-up reply while we don't case normalize the LeVeL" part of "ThReE.LeVeL.KeY" that's
tolower(), which we know isn't a 1=1 mapping on some
FS's. https://lore.kernel.org/git/[email protected]/
This, too.
Quoted 55 lines
Quoted 4 lines
To help us visualize the change, here's some common and complicated tasks and how they look with either schema (let mine be A and yours be B):
Before diving into that, I'll just say I don't care about the trivial specifics of how this is done, i.e. the bikeshedding of what the config keys etc. are named.
Just in (as noted above) design choices here forcing avoidable complexity in other areas.
Quoted 16 lines
#1 - Add a oneliner (not executing a script)
A:
[hook "post-commit"]
command = echo post commit
B:
[hook "oneliner"]
event = post-commit
command = echo post commit
#2 - Execute a script
A:
[hook "post-commit"]
command = ~/my-post-commit-hook
B:
[hook "script"]
event = post-commit
command = ~/my-post-commit-hook...
Quoted 8 lines
#3 - Run a handful of scripts in a specific order on one event A:
[hook "post-commit"]
command = ~/my-post-commit-1
command = ~/my-post-commit-2
command = ~/my-post-commit-3
B:
[hook "script 1"]Quoted 8 lines
event = post-commit
command = ~/my-post-commit-1
[hook "script 2"]
event = post-commit
command = ~/my-post-commit-2
[hook "script 3"]
event = post-commit
command = ~/my-post-commit-3To reply to all the above, yes, your suggestion comes out ahead in being less verbose.
But isn't the real difference not in the differing prefixes, i.e. hook.* and hookcmd.* (A) v.s. always hook.* (B, which is what I'm mainly focusing on, i.e. what requires the added complexity.
Quoted 8 lines
But that in that in your proposed way of doing it it's:
hook.<event-or-name>.*V.s. my suggestion of:
hook.<name>.*
No, I think that's a misunderstanding of proposal A. My proposal A is always hook.<event>.*, and hookcmd.<name>.*, and the way that I read your proposal B is that it's always hook.<name>.*.
Quoted 9 lines
And thus whenever you have a <event-or-name> that just happens to be a built-in hook listed in githooks(5) we in (A) implicitly expand config like:
hook.post-commit.command = echo foo
hook.post-commit.command = echo hi
hook.post-commit.type = post-commit
This is where I'm becoming confused. I *think* you're saying that with your proposal B, if the subsection happens to be the name of a built-in hook, we can skip the .event field. But it also reads like you're saying this is how my proposal A works, which isn't the case, because the subsection is always assumed to be the name of the hook event we're trying to run.
Quoted 6 lines
But not knowing about "foo" we don't expand:
hook.foo.command = echo foo
Quoted 8 lines
hook.foo.command = echo hi
hook.foo.type = foo # This would be an error, or ignored.But rather leave "dangling" for the user to later supply the "*.event" themselves, i.e.:
hook.foo.command = echo hi
hook.foo.event = post-commit
If you're talking about proposal B, then no, this isn't how it works. This config would imply that someone else (like a wrapper) can run "echo hi" by asking for "git hook run foo". But I'm not sure exactly that that's what you mean...
Quoted 9 lines
And means that you presumably need to detect this case and error about it, but my proposed model does not:
hook.post-commit.command = echo hi
# User error: "*.type" and <event-or-name>" does not match for # "hook.*.command"
hook.post-commit.type = pre-commit
Quoted 6 lines
And furthermore, that if someone in your model were to do:
hook.verify-commit.command = echo hi
It's "dangling" today, but if a later version of git learns about a "verify-commit" hook we'll start running it unexpectedly.
No, that's in fact as designed, with my model B. The user configured "echo hi" to run on "verify-commit" events; if those events are initially used by some wrapper, but later we decide they're a great idea and absorb the verify-commit event into native Git, then this is working as intended. I think your argument is based on a misunderstanding of the design.
Could it be that the way I provided the examples (my schema after A: and your schema after B:) made it hard to parse? Sorry about that if so.
Quoted 50 lines
Because your design conflates the slot for known hook type names and user-supplied names.
On balance I think it's better just to always supply two lines per-hook, but whether we have this proposed shorthand or not is mostly orthogonal to everything I mentioned in
https://lore.kernel.org/git/[email protected]/I.e. my proposed version could also have it, but thinking about it I think it's not worth it, we should always use <name>, not <event-or-name> for the reasons noted if you'll read ahead...
Quoted 17 lines
#4 - Skip a script configured more globally
A:
(original config)
[hook "post-commit"]
command = /corp/stuff/corp-post-commit
(local config)
[hookcmd "/corp/stuff/corp-post-commit"]
skip = true
OR,
(original config)
[hookcmd "corp-pc"]
command = /corp/stuff/corp-post-commit
[hook "post-commit"]
command = corp-pc
(local config)
[hookcmd "corp-pc"]
skip = trueQuoted 8 lines
B:
(original config)
[hook "corp-pc"]
event = post-commit
command = /corp/stuff/corp-post-commit
(local config)
[hook "corp-pc"]
skip = true...which are among other things that no, my proposed version doesn't have "skip" at all, see point #3 at
https://lore.kernel.org/git/[email protected]/I.e. I think the "skip" is a thing that falls out of a complexity of your design that I'm proposing to do away with.
That complexity being that you use <event-or-name> and I use <name>, and you want to in turn support N number of "*.command" for any "hook.<event-or-name>".
Quoted 7 lines
I'm suggesting we use the typical "last key wins" semantics, if you want multiple commands for a given hook type you'll just supply multiple "hook.<name>" sections with the same "hook.*.event = type" in all.
The way to skip hooks in my proposal is:
hook.<name>.command = true
I see - instead of fiddly reorganizing the list, you're just changing what the specific hook name is redirecting to.
Then, instead of storing the resolved command in the 'struct hook', we can store the name, and always dereference the name right before execution, using last-one-wins semantics.
The only thing I don't like about this is that it's a little bit confusing to the user. I personally would rather have more complicated code and less complicated user experience, so I like 'skip' more because it's readable and because you don't need to guess "oh, it'll just execute /bin/true which is a noop". That's easy to understand for someone familiar with Unix scripting (and therefore for we Git contributors), but confusing for everybody else.
Maybe we can take the essential spirit of this implementation but make it a little bit simpler. In pseudocode:
populate_list(hookname):
# look for everybody who says "hook.*.event = $hookname"
hooks = get_configs_matching(hook.*.event, hookname)
# the "*" bit is the bit we dereference later
for hook in hooks:
hook_list.add({.name = hook.key.subsection})run_hooks(hook_list):
for hook in hook_list:
# should we even run?
if (get_last_config("hook." + hook.name + ".skip"))
continue # dereference the hook name and find out the command
cmd = get_last_config("hook." + hook.name + ".command")
run(cmd)The price for this improved readability is an extra config lookup in each case. But the code is still readable, definitely moreso than rewalking the config list over and over as the implementation stands now. I personally think it's worth it.
(One could also imagine a more readable placeholder than /bin/true in the "hook.<name>.command" field, but then we get to decide just how hard we want to avoid colliding with legitimate user hook names. Is .command = skip enough? is .command = RESERVED_NAME_SKIP_THIS_HOOK enough? Is something unrunnable like .command = "skip me" enough, although it could conceivably collide with a determined user?)
Quoted 46 lines
Or whatever noop command you want on the RHS. In practice we wouldn't invoke "true", just like we don't invoke "cat" for "PAGER=cat".
But unlike "*.skip" that doesn't require complexity in implementation or user understanding, it just falls naturally out of the rest of the model.
Quoted 8 lines
#5 - Execute one script for multiple events
A:
[hookcmd "reusable-hook"]
command = /long/path/reusable-hook
[hook "post-commit"]
command = reusable-hook
[hook "pre-commit"]
command = reusable-hookQuoted 8 lines
[hook "prepare-commit-msg"]
command = reusable-hook
B:
[hook "reusable-hook"]
command = /long/path/reusable-hook
event = post-commit
event = pre-commit
event = prepare-commit-msgIt's been so long since I wrote the original E-Mail that I'm having to skim my own summary there & come up with things as I go along, so maybe this conflicts with something I said earlier.
But no, I don't think I meant that *.event should be multi-value, or that that magic is worthwhile. I.e. I think we just want:
[hook "not-a-reusable-section-1"]
command = /long/path/reusable-hook
event = post-commit
[hook "not-a-reusable-section-2"]
command = /long/path/reusable-hook
event = pre-commit
[hook "not-a-reusable-section-3"]
command = /long/path/reusable-hook
event = prepare-commit-msgI.e. is wanting to use the same command for N different hooks really that common a use-case to avoid a little verbosity, and quite a lot of complexity?
Quoted 5 lines
How would such a hook even be implemented?
We don't have anything in your hook execution model (and I don't remember you adding this) which passes down a "you are this type of hook" to hooks.
No, but it's something I'm interested in passing as an environment variable. I didn't add it to this series because it seemed to me to distract from the core feature. We would like to add it to simplify our invocations of https://github.com/awslabs/git-secrets, so it's on my radar.
And even without this, just today I wanted to configure a hook to really make sure I had config.mak set up in all my Git worktrees (since you pointed out I pushed code that fails -Wall.... ;) ) and set it up to run on both pre-commit and post-checkout. So yes, it is feasible now, for very stupid hooks.
Quoted 9 lines
It's now implicit in that the hook invoked at .git/hooks/post-commit or .git/hooks/pre-commit can check its own basename, but it won't be with configurable hooks.
We could start passing down a
GIT_HOOK_TYPEin the environment or whatever, but I think the simpler case of just having the user do it is better.
Quoted 5 lines
I'm assuming that mainly because people have wanted a "/long/path/reusable-hook" router script because we have not supported executing N hooks, or supported concurrency. Once we do that the complexity of such routing scripts (mostly, not everyone's needs will be met) will be replaced by a little bit of config.
The above linked git-secrets is an example; the config is irritating and doesn't really need to be there, when we could just teach the script itself how to check GIT_HOOK_TYPE envvar.
Quoted 6 lines
I don't see why it's worth it to micro-optimize for lines in that config at the cost of increased config complexity.
For example, how do you "skip" an "event" type? You have it for "*.command"? So let's say in your model (A) I have system config like:
(The following isn't how my model A works, because it refers always to "hook.<event>", as I mentioned above. But I also covered this same topic in detail earlier in this email, so I'm going to snip this section.)
Quoted 46 lines
Quoted 5 lines
#6 - Execute the same script early for one event and late for another event
A:
(global)
[hookcmd "reusable-hook"]Quoted 8 lines
command = /long/path/reusable-hook
[hook "pre-commit"]
command = reusable-hook
(local)
[hook "post-commit"]
command = other
command = hooks
command = reusable-hookEven with I think it's fair to say deep knowledge of your proposal at this point I still needed to read this a few times to see if that:
command = reusable-hook
Is referring to:
[hookcmd "reusable-hook"]
I.e. is it going to run:
command = /long/path/reusable-hook
Or is it just re-specifying /long/path/reusable-hook but relying on a $PATH lookup?
Having reasoned through that I think the answer is the former. But that also means that in your model:
[hookcmd "rm -rf /"]
command = echo this will not actually destroy your data [hook "pre-commit"]
command = rm -rf /Is going to run that friendly "echo" command, since "command = rm -rf /" just refers to the "rm -rf /" <name>, not <command>, right the "hookcmd" line is removed, at which point we'll stop treating it as a <name> and run it as a <command>?
In practice I think users are unlikely to use actively harmful names like that.
Quoted 6 lines
I'm just making the point that I should not need to know about previous config to see if a "hook.pre-commit.command = rm -rf /" is harmless or not, or need to carefully squint to see if the "reusable-hook" is referring to a section name or command name.
Or am I just confused at this point?
Nah, that's an accurate understanding of how it would work with the current implementation. Sure, it's a good point.
Quoted 15 lines
Quoted 15 lines
B:
(global)
[hook "reusable-hook"]
command = /long/path/reusable-hook
event = pre-commit
(local)
[hook "other"]
event = post-commit
command = other
[hook "hooks"]
event = post-commit
command = hooks
[hook "reusable-hook"]
event = reusable-hook
Hm, I think I mistyped when I wrote "event = reusable-hook" here, and meant to write "event = post-commit" to indicate that we would move "reusable-hook" to the bottom of the execution list for "post-commit".
I'll snip most of the rebuttal about my typo.
Quoted 48 lines
(global)
[hook "reusable-hook"]
command = /long/path/reusable-hook event = pre-commit(local)
[hook "reusable-hook"]
command = true # skip it# The "hooks" name is arbitrary, "my-hooks" or whatever would be # clearer, but just going with your example...
[hook "hooks"]
event = post-commit command = hooks
# Not very reusable then...
[hook "reusable-hook"]
command = /long/path/reusable-hook event = pre-commitQuoted 2 lines
[...]Please feel free to chime in with more use cases that you think would be handy which I forgot :)
I couldn't find this at a quick glance but I think we also had a disussion at some point about hooks controlling parallelism. AFAICT your current implementation just has global:
hook.jobs=N
And we then blacklist certain hooks to always have hook.jobs=1, e.g. the commit-msg hook that needs an implicit "lock" on that file (or rather, we think that's the most common use-case).
I think my version of always having hook.<name>.{event,command} be one value is also better in that case, i.e. we'd then:
[hook "myhook"]
command = some-command
event = pre-receive
parallel = true # the default[hook "myhook2"]
command = some-command2
event = pre-receive
parallel = true # the default
Quoted 4 lines
[hook "myhook3"]
command = some-unconcurrent-command event = pre-receive
parallel = false # I'm not OK with concurrency
I am not sure what it means for a single executable to write "parallel = true" - it is a single executable.
Ok, that is me being facetious - I think you are saying we can AND together all of the 'hook.<thing-with-event-we-care-about>.parallel' to decide whether or not to run in parallel.
I would rather not discuss this now, for this series, because regardless of which config schema we use today, we can figure out "parallel unless we really don't want it" later on. It is too complex to discuss in the context of "hey, we should also configure hooks somewhere else". Let's leave it for future work.
Quoted 3 lines
I haven't thought about it deeply, but have a hunch that having sections be a 1=1 mapping with a specific command instead of either 1=1 or 1=many is going to make that easier to implement and understand.
Sure, probably. But it can be added later to either schema... ;)
Anyway, I did a quick strawpoll with my colleagues (Jonathan Nieder, Jonathan Tan, and Josh Steadmon) and they all like your syntax more. I am ambivalent between the two, personally.
So what I will do, hopefully today, maybe not, is the following:
- no more hookcmd. (and there was much rejoicing)
- Hooks are specified with "[hook "name-of-hook"]"
- I do see value in having an explicit .skip field rather than mapping .command to a noop, so "hook.name-of-hook.skip" as described above. Of course the method you described will work regardless, since its mechanism is based on the inherent result of executing /bin/true.
- I do see value in allowing "hook.name-of-hook.event" to be defined repeatably, as described above, so I will include that.
And left for later work:
- teaching HOOK_EVENT_NAME=post-commit (etc)
- figuring out what the heck we want to do with allowing hooks to describe whether they allow parallelism
Unfortunately I've dropped the design doc from this series, since nobody seemed interested in having it checked in, but I'll try to rework the help doc to make the schema more clear.
Thanks, I think it was useful to hash this out.
- Emily
Re: [PATCH 8/9] hook: teach 'hookcmd' config to alias hook scripts
Quoted 89 lines · Emily Shaffer
Quoted 88 lines · Ævar Arnfjörð Bjarmason
Quoted 83 lines · Emily Shaffer
Quoted 60 lines · Ævar Arnfjörð Bjarmason
Quoted 39 lines · Emily Shaffer
To enable fine-grained options which apply to a single hook executable, and to make it easier for a single executable to be run on multiple hook events, teach "hookcmd.<alias>.config". These can be configured as follows:
[...]Documentation/config/hook.txt +24
-0@@ -3,6 +3,11 @@ hook.<command>.command:: executable on your device, a oneliner for your shell, or the name of a hookcmd. See linkgit:git-hook[1]. +hookcmd.<name>.command:: + A command to execute during a hook for which <name> has been specified + as a command. This can be an executable on your device or a oneliner for + your shell. See linkgit:git-hook[1]. + +Global config +---- + [hook "post-commit"] + command = "linter" + command = "~/typocheck.sh" + + [hookcmd "linter"] + command = "/bin/linter --c" +---- + +Local config +---- + [hook "prepare-commit-msg"] + command = "linter" + [hook "post-commit"] + command = "python ~/run-test-suite.py" +---- + +With these configs, you'd then run post-commit hooks in this order:Documentation/config/hook.txt hook.<command>.command::
Quoted 10 lines
Documentation/config/hook.txt +10
-0+ + /bin/linter --c + ~/typocheck.sh + python ~/run-test-suite.py + .git/hooks/post-commit (if present) + +and prepare-commit-msg hooks in this order: + + /bin/linter --c + .git/hooks/prepare-commit-msg (if present)I still have outstanding feedback on the fundamental design here. I.e. why is this not:
hook.<name>.event = post-commit
hook.<name>.command = <path>Quoted 8 lines
See:
https://lore.kernel.org/git/[email protected]/
As noted there I don't see why not, and more complexity results from the design choice of doing it the way you're proposing. I.e. we can't discover hooks based on config prefixes, and we end up sticking full FS paths in config keys.
Interesting. My gut says that it would make it harder to neatly write a config with the same hook running at the very beginning of one event and the very end of another, but I'm not sure whether that's a case to optimize for.
I would also need twice as many lines to run a script I wrote as a hook
- that is, the base case which is probably all most people will need. So with your proposal I *must* name every hook I want to add. Instead of "hook.pre-commit.command = ~/check-for-debug-strings" I need to configure "hook.check-debug-strings.event = pre-commit" and "hook.check-debug-strings.command = ~/check-for-debug-strings". That's a little frustrating, and if I never want to skip that check or move it later or something, it will be extra overhead for no gain for me.
Quoted 6 lines
Quoted 2 lines
The gain is that "git hook list" becomes a trivial "git config -show-origin --show-scope --get-regexp" wrapper.
This isn't a very compelling reason to me, if it makes the user experience worse. (I'm not convinced that it does - just saying I disagree with this reasoning.)
...(see below)...
Quoted 10 lines
Quoted 5 lines
So the series either doesn't need "git hook list" or such a thing becomes much less complex, especially given the proposed addition of other features in the area like "git hook edit", i.e. (quoting the linked E-Mail):
I still think "git hook list" is useful to have for end-users, compared to remembering the appropriate "git config" invocation. It futureproofs us if we do want to change the execution ordering to something besides config order, it's easier to remember/more discoverable, etc.
To clarify: I'm not against there being a "git hook list", I think we should have it.
It's useful UI. I.e. we have "run", it makes sense to have the same mechanism spew out its idea of what hooks are where, especially since that's not entirely contained in the config, but a union of config and GIT_DIR/hooks (and then there's core.hooksPath...).
So I'm very much for it being there.
I've just been commenting on the relative complexity of the config schema, which in one way surfaces as not being able to do the config part of the "git hook list" as a simple shell-out (or equivalent library invocation) of "git config" under the hood, but also means that users need to mentally track a more complex format when e.g. manually editing config...
Quoted 9 lines
Quoted 9 lines
[...]
But isn't the real difference not in the differing prefixes, i.e. hook.* and hookcmd.* (A) v.s. always hook.* (B, which is what I'm mainly focusing on, i.e. what requires the added complexity.But that in that in your proposed way of doing it it's:
hook.<event-or-name>.*
Quoted 8 lines
Quoted 4 lines
V.s. my suggestion of:
hook.<name>.*No, I think that's a misunderstanding of proposal A. My proposal A is always hook.<event>.*, and hookcmd.<name>.*, and the way that I read your proposal B is that it's always hook.<name>.*.
Ah, thanks.
Quoted 10 lines
Quoted 9 lines
And thus whenever you have a <event-or-name> that just happens to be a built-in hook listed in githooks(5) we in (A) implicitly expand config like:
hook.post-commit.command = echo foo
hook.post-commit.command = echo hi
hook.post-commit.type = post-commit
Quoted 6 lines
This is where I'm becoming confused. I *think* you're saying that with your proposal B, if the subsection happens to be the name of a built-in hook, we can skip the .event field. But it also reads like you're saying this is how my proposal A works, which isn't the case, because the subsection is always assumed to be the name of the hook event we're trying to run.
I *think* this is best addressed below, moving on...
Quoted 12 lines
Quoted 12 lines
But not knowing about "foo" we don't expand:
hook.foo.command = echo foo
hook.foo.command = echo hi
hook.foo.type = foo # This would be an error, or ignored.But rather leave "dangling" for the user to later supply the "*.event" themselves, i.e.:
Quoted 7 lines
Quoted 2 lines
hook.foo.command = echo hi
hook.foo.event = post-commitIf you're talking about proposal B, then no, this isn't how it works. This config would imply that someone else (like a wrapper) can run "echo hi" by asking for "git hook run foo". But I'm not sure exactly that that's what you mean...
...and on (maybe)....
Quoted 23 lines
Quoted 9 lines
And means that you presumably need to detect this case and error about it, but my proposed model does not:
hook.post-commit.command = echo hi
# User error: "*.type" and <event-or-name>" does not match for # "hook.*.command"
hook.post-commit.type = pre-commitQuoted 6 lines
And furthermore, that if someone in your model were to do:
hook.verify-commit.command = echo hi
It's "dangling" today, but if a later version of git learns about a "verify-commit" hook we'll start running it unexpectedly.
No, that's in fact as designed, with my model B. The user configured "echo hi" to run on "verify-commit" events; if those events are initially used by some wrapper, but later we decide they're a great idea and absorb the verify-commit event into native Git, then this is working as intended. I think your argument is based on a misunderstanding of the design.
Quoted 2 lines
Could it be that the way I provided the examples (my schema after A: and your schema after B:) made it hard to parse? Sorry about that if so.
Aren't you assuming that users who specify a verify-commit will be happy because git's usurping of that will 1=1 match what they were using "verify-commit" for.
I'm pointing out that we can't know that, and since you want to make "git hook run" a general thing that runs any <name> of script you've configured, and not just what's in githooks(5) that it becomes very likely that if we add a new hook with some obvious name that we'll either break things for users, or subtly change behavior.
Which isn't just theoretical, e.g. I tend to run something like a "git log --check @{u}.." before I run git-send-email, with this configurable hook mechanism having a "git hook run sendemail-check" would be a way I might expose that in my own ~/.gitconfig.
But if git-send-email learns a "sendemail-check" and the behavior doesn't exactly match mine; E.g. maybe it similar to pre-auto-gc expects me to return a status code to ask me if I want to abort on a failed --check, but mine expects a revision range to run "log --check".
In practice that's a non-issue with the current hook mechanism, i.e. nobody's sticking a script into .git/hooks/my-custom-name and expecting it to do anything useful (and if they are, they have only themselves to blame).
Whereas we'd now be actively inviting users to squat on the same namespace we ourselves will need for future hooks.
Quoted 89 lines
Quoted 50 lines
Because your design conflates the slot for known hook type names and user-supplied names.
On balance I think it's better just to always supply two lines per-hook, but whether we have this proposed shorthand or not is mostly orthogonal to everything I mentioned in
https://lore.kernel.org/git/[email protected]/I.e. my proposed version could also have it, but thinking about it I think it's not worth it, we should always use <name>, not <event-or-name> for the reasons noted if you'll read ahead...
Quoted 17 lines
#4 - Skip a script configured more globally
A:
(original config)
[hook "post-commit"]
command = /corp/stuff/corp-post-commit
(local config)
[hookcmd "/corp/stuff/corp-post-commit"]
skip = true
OR,
(original config)
[hookcmd "corp-pc"]
command = /corp/stuff/corp-post-commit
[hook "post-commit"]
command = corp-pc
(local config)
[hookcmd "corp-pc"]
skip = trueQuoted 8 lines
B:
(original config)
[hook "corp-pc"]
event = post-commit
command = /corp/stuff/corp-post-commit
(local config)
[hook "corp-pc"]
skip = true...which are among other things that no, my proposed version doesn't have "skip" at all, see point #3 at
https://lore.kernel.org/git/[email protected]/I.e. I think the "skip" is a thing that falls out of a complexity of your design that I'm proposing to do away with.
That complexity being that you use <event-or-name> and I use <name>, and you want to in turn support N number of "*.command" for any "hook.<event-or-name>".
Quoted 7 lines
I'm suggesting we use the typical "last key wins" semantics, if you want multiple commands for a given hook type you'll just supply multiple "hook.<name>" sections with the same "hook.*.event = type" in all.
The way to skip hooks in my proposal is:
hook.<name>.command = true
I see - instead of fiddly reorganizing the list, you're just changing what the specific hook name is redirecting to.
Then, instead of storing the resolved command in the 'struct hook', we can store the name, and always dereference the name right before execution, using last-one-wins semantics.
The only thing I don't like about this is that it's a little bit confusing to the user. I personally would rather have more complicated code and less complicated user experience, so I like 'skip' more because it's readable and because you don't need to guess "oh, it'll just execute
/bin/truewhich is a noop". That's easy to understand for someone familiar with Unix scripting (and therefore for we Git contributors), but confusing for everybody else.Maybe we can take the essential spirit of this implementation but make it a little bit simpler. In pseudocode:
populate_list(hookname):# look for everybody who says "hook.*.event = $hookname" hooks = get_configs_matching(hook.*.event, hookname) # the "*" bit is the bit we dereference later for hook in hooks: hook_list.add({.name = hook.key.subsection})
run_hooks(hook_list):
for hook inhook_list:# should we even run? if (get_last_config("hook." + hook.name + ".skip")) continue
Quoted 8 lines
# dereference the hook name and find out the command cmd = get_last_config("hook." + hook.name + ".command") run(cmd)The price for this improved readability is an extra config lookup in each case. But the code is still readable, definitely moreso than rewalking the config list over and over as the implementation stands now. I personally think it's worth it.
I think the less complicated user experience is for our config system to work as consistently as possible with other config. If we define special rules and what's effectively special syntax with "skip" etc. we're asking users to read how that works, and keep that all in their head just to deal with that one area of our config.
Quoted 6 lines
(One could also imagine a more readable placeholder than
/bin/truein the "hook.<name>.command" field, but then we get to decide just how hard we want to avoid colliding with legitimate user hook names. Is .command = skip enough? is .command =RESERVED_NAME_SKIP_THIS_HOOKenough? Is something unrunnable like .command = "skip me" enough, although it could conceivably collide with a determined user?)
All of that's something you'll need to explain in detail to users, which all seems way more complex than a simple:
To skip a previously defined hook insert a noop-command, any will do, but setting it to "true" (usually /bin/true) is a handy convention for doing nothing.
I.e. by keeping the config field as doing one thing only you avoid any such collisions etc.
Quoted 52 lines
Quoted 46 lines
Or whatever noop command you want on the RHS. In practice we wouldn't invoke "true", just like we don't invoke "cat" for "PAGER=cat".
But unlike "*.skip" that doesn't require complexity in implementation or user understanding, it just falls naturally out of the rest of the model.
Quoted 8 lines
#5 - Execute one script for multiple events
A:
[hookcmd "reusable-hook"]
command = /long/path/reusable-hook
[hook "post-commit"]
command = reusable-hook
[hook "pre-commit"]
command = reusable-hookQuoted 8 lines
[hook "prepare-commit-msg"]
command = reusable-hook
B:
[hook "reusable-hook"]
command = /long/path/reusable-hook
event = post-commit
event = pre-commit
event = prepare-commit-msgIt's been so long since I wrote the original E-Mail that I'm having to skim my own summary there & come up with things as I go along, so maybe this conflicts with something I said earlier.
But no, I don't think I meant that *.event should be multi-value, or that that magic is worthwhile. I.e. I think we just want:
[hook "not-a-reusable-section-1"]
command = /long/path/reusable-hook
event = post-commit
[hook "not-a-reusable-section-2"]
command = /long/path/reusable-hook
event = pre-commit
[hook "not-a-reusable-section-3"]
command = /long/path/reusable-hook
event = prepare-commit-msgI.e. is wanting to use the same command for N different hooks really that common a use-case to avoid a little verbosity, and quite a lot of complexity?
Quoted 5 lines
How would such a hook even be implemented?
We don't have anything in your hook execution model (and I don't remember you adding this) which passes down a "you are this type of hook" to hooks.
Quoted 5 lines
No, but it's something I'm interested in passing as an environment variable. I didn't add it to this series because it seemed to me to distract from the core feature. We would like to add it to simplify our invocations of https://github.com/awslabs/git-secrets, so it's on my radar.
Having such an env var as part of the initial series seems like a sensible thing to have.
Quoted 5 lines
And even without this, just today I wanted to configure a hook to really make sure I had config.mak set up in all my Git worktrees (since you pointed out I pushed code that fails -Wall.... ;) ) and set it up to run on both pre-commit and post-checkout. So yes, it is feasible now, for very stupid hooks.
Aside: It's nice to use DEVELOPER=1 in config.mak (or as make param) when hacking on git, it'll set -Wall -Werror and other nice warning flags.
Quoted 32 lines
[...]
Quoted 26 lines
Quoted 2 lines
[...]Please feel free to chime in with more use cases that you think would be handy which I forgot :)
I couldn't find this at a quick glance but I think we also had a disussion at some point about hooks controlling parallelism. AFAICT your current implementation just has global:
hook.jobs=N
And we then blacklist certain hooks to always have hook.jobs=1, e.g. the commit-msg hook that needs an implicit "lock" on that file (or rather, we think that's the most common use-case).
I think my version of always having hook.<name>.{event,command} be one value is also better in that case, i.e. we'd then:
[hook "myhook"]
command = some-command
event = pre-receive
parallel = true # the default[hook "myhook2"]
command = some-command2
event = pre-receive
parallel = true # the defaultQuoted 4 lines
[hook "myhook3"]
command = some-unconcurrent-command event = pre-receive
parallel = false # I'm not OK with concurrency
Quoted 6 lines
I am not sure what it means for a single executable to write "parallel = true" - it is a single executable.
Ok, that is me being facetious - I think you are saying we can AND together all of the 'hook.<thing-with-event-we-care-about>.parallel' to decide whether or not to run in parallel.
Right, the case (whatever the config mechanism) wanting to use several off-the-shelf hooks and accomplish through git some version of this:
parallel -j8 pre-receive-parallel-*.sh &&
parallel -j1 pre-receive-non-parallel-*.sh
I.e. since we have N scripts for the "pre-receive" type, and we're expecting to say whether on not parallelism is OK or not, it seems like a natural thing we'll want to declare that differently for some of those than for others.
Quoted 5 lines
I would rather not discuss this now, for this series, because regardless of which config schema we use today, we can figure out "parallel unless we really don't want it" later on. It is too complex to discuss in the context of "hey, we should also configure hooks somewhere else". Let's leave it for future work.
The point is that no, we really can't figure it out as easily later on regardless of the config schema.
Because with 1=many you can't have 1=many.someAttribute=XYZ without that *.someAttribute=XYZ declaring something for all of 1=many, whereas if it's 1=1 then 1=1.someAttribute.XYZ obviously applies only to that 1=1.
Quoted 7 lines
Quoted 3 lines
I haven't thought about it deeply, but have a hunch that having sections be a 1=1 mapping with a specific command instead of either 1=1 or 1=many is going to make that easier to implement and understand.
Sure, probably. But it can be added later to either schema... ;)
Quoted 8 lines
Anyway, I did a quick strawpoll with my colleagues (Jonathan Nieder, Jonathan Tan, and Josh Steadmon) and they all like your syntax more. I am ambivalent between the two, personally.
So what I will do, hopefully today, maybe not, is the following:
- no more hookcmd. (and there was much rejoicing)
- Hooks are specified with "[hook "name-of-hook"]"
Thanks, I look forward to checking that out. As should be obvious from my misunderstanding of some of your config proposal (I snippet out some of those from the reply) I'm still not entirely clear on even what the current proposed behavior is, hopefully something simpler will be easier to grok ... :)
Quoted 4 lines
- I do see value in having an explicit .skip field rather than mapping .command to a noop, so "hook.name-of-hook.skip" as described above. Of course the method you described will work regardless, since its mechanism is based on the inherent result of executing
/bin/true.
I think we've mainly focused on the theoretical aspect of this, but FWIW I'm still entirely unclear on what this feature is even aimed for.
All of the rest of our config does not have an explicit "skip" for anything, just last-set-wins. In terms of a real-world use-case wouldn't a user just edit or comment out the config earlier in ~/.gitconfig, and not "skip" it at the end with "git config [...] --add"?
I suspect that the use-case is some Googly centrally managed /etc/gitconfig, but that's just speculation...
Quoted 5 lines
- I do see value in allowing "hook.name-of-hook.event" to be defined repeatably, as described above, so I will include that.
[...]
- figuring out what the heck we want to do with allowing hooks to describe whether they allow parallelism
Which just to check if we're on the same page, needs to be figured out because of the complexity of that "defined repeatably", no?
Quoted 3 lines
Unfortunately I've dropped the design doc from this series, since nobody seemed interested in having it checked in, but I'll try to rework the help doc to make the schema more clear.
FWIW I wasn't opposed to it per-se, but remembered commenting on it in earlier rounds, and it being some combination of docs that should really be in manpages (i.e. describing current actual behavior), more general rational and "why is it like this" (which correctly belongs in design docs), and musings about hypothetical future features (e.g. "git hook edit" and the like)'.
Re: [PATCH 8/9] hook: teach 'hookcmd' config to alias hook scripts
Quoted 28 lines · Ævar Arnfjörð Bjarmason
Quoted 6 lines
Quoted 2 lines
The gain is that "git hook list" becomes a trivial "git config -show-origin --show-scope --get-regexp" wrapper.
This isn't a very compelling reason to me, if it makes the user experience worse. (I'm not convinced that it does - just saying I disagree with this reasoning.)
...(see below)...
Quoted 10 lines
Quoted 5 lines
So the series either doesn't need "git hook list" or such a thing becomes much less complex, especially given the proposed addition of other features in the area like "git hook edit", i.e. (quoting the linked E-Mail):
I still think "git hook list" is useful to have for end-users, compared to remembering the appropriate "git config" invocation. It futureproofs us if we do want to change the execution ordering to something besides config order, it's easier to remember/more discoverable, etc.
To clarify: I'm not against there being a "git hook list", I think we should have it.
It's useful UI. I.e. we have "run", it makes sense to have the same mechanism spew out its idea of what hooks are where, especially since that's not entirely contained in the config, but a union of config and GIT_DIR/hooks (and then there's core.hooksPath...).
Quoted 8 lines
So I'm very much for it being there.
I've just been commenting on the relative complexity of the config schema, which in one way surfaces as not being able to do the config part of the "git hook list" as a simple shell-out (or equivalent library invocation) of "git config" under the hood, but also means that users need to mentally track a more complex format when e.g. manually editing config...
Sure, makes sense.
Quoted 46 lines
Quoted 23 lines
Quoted 9 lines
And means that you presumably need to detect this case and error about it, but my proposed model does not:
hook.post-commit.command = echo hi
# User error: "*.type" and <event-or-name>" does not match for # "hook.*.command"
hook.post-commit.type = pre-commitQuoted 6 lines
And furthermore, that if someone in your model were to do:
hook.verify-commit.command = echo hi
It's "dangling" today, but if a later version of git learns about a "verify-commit" hook we'll start running it unexpectedly.
No, that's in fact as designed, with my model B. The user configured "echo hi" to run on "verify-commit" events; if those events are initially used by some wrapper, but later we decide they're a great idea and absorb the verify-commit event into native Git, then this is working as intended. I think your argument is based on a misunderstanding of the design.
Quoted 2 lines
Could it be that the way I provided the examples (my schema after A: and your schema after B:) made it hard to parse? Sorry about that if so.
Aren't you assuming that users who specify a verify-commit will be happy because git's usurping of that will 1=1 match what they were using "verify-commit" for.
I'm pointing out that we can't know that, and since you want to make "git hook run" a general thing that runs any <name> of script you've configured, and not just what's in githooks(5) that it becomes very likely that if we add a new hook with some obvious name that we'll either break things for users, or subtly change behavior.
Which isn't just theoretical, e.g. I tend to run something like a "git log --check @{u}.." before I run git-send-email, with this configurable hook mechanism having a "git hook run sendemail-check" would be a way I might expose that in my own ~/.gitconfig.
But if git-send-email learns a "sendemail-check" and the behavior doesn't exactly match mine; E.g. maybe it similar to pre-auto-gc expects me to return a status code to ask me if I want to abort on a failed --check, but mine expects a revision range to run "log --check".
Quoted 7 lines
In practice that's a non-issue with the current hook mechanism, i.e. nobody's sticking a script into .git/hooks/my-custom-name and expecting it to do anything useful (and if they are, they have only themselves to blame).
Whereas we'd now be actively inviting users to squat on the same namespace we ourselves will need for future hooks.
Yeah, this is a good point. Seems worth a note in the 'git hook run' doc, making a point that "you can use this for your wrapper to run specific hooks, but be careful about namespace collisions". We're a lot less likely to add a hook named "repotool-verify-commit" than we are to add a hook named "verify-commit", for example.
I think it's enough to warn about future namespace collisions and make an "at your own risk" note.
Quoted 41 lines
Quoted 16 lines
Maybe we can take the essential spirit of this implementation but make it a little bit simpler. In pseudocode:
populate_list(hookname):# look for everybody who says "hook.*.event = $hookname" hooks = get_configs_matching(hook.*.event, hookname) # the "*" bit is the bit we dereference later for hook in hooks: hook_list.add({.name = hook.key.subsection})
run_hooks(hook_list):
for hook inhook_list:# should we even run? if (get_last_config("hook." + hook.name + ".skip")) continueQuoted 8 lines
# dereference the hook name and find out the command cmd = get_last_config("hook." + hook.name + ".command") run(cmd)The price for this improved readability is an extra config lookup in each case. But the code is still readable, definitely moreso than rewalking the config list over and over as the implementation stands now. I personally think it's worth it.
I think the less complicated user experience is for our config system to work as consistently as possible with other config. If we define special rules and what's effectively special syntax with "skip" etc. we're asking users to read how that works, and keep that all in their head just to deal with that one area of our config.
Quoted 6 lines
(One could also imagine a more readable placeholder than
/bin/truein the "hook.<name>.command" field, but then we get to decide just how hard we want to avoid colliding with legitimate user hook names. Is .command = skip enough? is .command =RESERVED_NAME_SKIP_THIS_HOOKenough? Is something unrunnable like .command = "skip me" enough, although it could conceivably collide with a determined user?)All of that's something you'll need to explain in detail to users, which all seems way more complex than a simple:
Quoted 6 lines
To skip a previously defined hook insert a noop-command, any will do, but setting it to "true" (usually
/bin/true) is a handy convention for doing nothing.I.e. by keeping the config field as doing one thing only you avoid any such collisions etc.
I disagree fundamentally that "find and run a noop command like /bin/true" is simpler to average users than "skip it by setting a config". Like I said below, by including "skip" both approaches will work.
Quoted 8 lines
Quoted 5 lines
No, but it's something I'm interested in passing as an environment variable. I didn't add it to this series because it seemed to me to distract from the core feature. We would like to add it to simplify our invocations of https://github.com/awslabs/git-secrets, so it's on my radar.
Having such an env var as part of the initial series seems like a sensible thing to have.
Eh. To me, it feels like feature creep. It also is something we could add today to the existing hook mechanism (even if it's a little pointless since you can basename, like you say), so it feels orthogonal. I would prefer not to add it in this series.
Quoted 10 lines
Quoted 6 lines
And even without this, just today I wanted to configure a hook to really make sure I had config.mak set up in all my Git worktrees (since you pointed out I pushed code that fails -Wall.... ;) ) and set it up to run on both pre-commit and post-checkout. So yes, it is feasible now, for very stupid hooks.
Aside: It's nice to use DEVELOPER=1 in config.mak (or as make param) when hacking on git, it'll set -Wall -Werror and other nice warning flags.
This is literally what I was saying I wrote the hook to do, and is also a tip I included when I wrote MyFirstContribution.txt.
Quoted 24 lines
Quoted 6 lines
I am not sure what it means for a single executable to write "parallel = true" - it is a single executable.
Ok, that is me being facetious - I think you are saying we can AND together all of the 'hook.<thing-with-event-we-care-about>.parallel' to decide whether or not to run in parallel.
Right, the case (whatever the config mechanism) wanting to use several off-the-shelf hooks and accomplish through git some version of this:
parallel -j8 pre-receive-parallel-*.sh &&
parallel -j1 pre-receive-non-parallel-*.shI.e. since we have N scripts for the "pre-receive" type, and we're expecting to say whether on not parallelism is OK or not, it seems like a natural thing we'll want to declare that differently for some of those than for others.
Quoted 5 lines
I would rather not discuss this now, for this series, because regardless of which config schema we use today, we can figure out "parallel unless we really don't want it" later on. It is too complex to discuss in the context of "hey, we should also configure hooks somewhere else". Let's leave it for future work.
Quoted 6 lines
The point is that no, we really can't figure it out as easily later on regardless of the config schema.
Because with 1=many you can't have 1=many.someAttribute=XYZ without that *.someAttribute=XYZ declaring something for all of 1=many, whereas if it's 1=1 then 1=1.someAttribute.XYZ obviously applies only to that 1=1.
I think this is moot, since we are moving to "all config hooks have a name", but my plan previously was to let this be set on a hookcmd. Essentially, your suggestion is to make every hook a hookcmd. My point was that it's easy to extend [object which represents an executable] in the config to include "always run me in series" or "run me in series for this specific event" regardless. That is, one could imagine, discarding entirely the hookcmd junk and going with the schema I sketched in my last email (which lands somewhere between yours and mine):
[hook "linter"]
command = ~/linter.sh
event = pre-commit
parallel = false
or...
[hook "linter"]
command = ~/linter.sh
event = pre-commit
event = commit-msg
[hook "linter.commit-msg"]
parallel = false
Or even...
[hook "linter"]
command = ~/linter.sh
event = pre-commit
event = commit-msg
parallel = commit-msg
The possibilities go on, as far as configuration goes.
To me, the harder part of this problem is actually implementing the execution. We had some discussions at length early on in the config-based hook series about ways to do this kind of complex "some stuff needs synchronous execution and some stuff doesn't, in the same event" and decided that it mostly resolved to "you ain't gonna need it" principle. So I would prefer to discuss this when we find out we do actually need it.
Quoted 14 lines
Quoted 14 lines
Quoted 4 lines
I haven't thought about it deeply, but have a hunch that having sections be a 1=1 mapping with a specific command instead of either 1=1 or 1=many is going to make that easier to implement and understand.
Sure, probably. But it can be added later to either schema... ;)
Anyway, I did a quick strawpoll with my colleagues (Jonathan Nieder, Jonathan Tan, and Josh Steadmon) and they all like your syntax more. I am ambivalent between the two, personally.
So what I will do, hopefully today, maybe not, is the following:
Quoted 8 lines
Quoted 2 lines
- no more hookcmd. (and there was much rejoicing)
- Hooks are specified with "[hook "name-of-hook"]"Thanks, I look forward to checking that out. As should be obvious from my misunderstanding of some of your config proposal (I snippet out some of those from the reply) I'm still not entirely clear on even what the current proposed behavior is, hopefully something simpler will be easier to grok ... :)
Fingers crossed ;)
Quoted 9 lines
Quoted 5 lines
- I do see value in having an explicit .skip field rather than mapping .command to a noop, so "hook.name-of-hook.skip" as described above. Of course the method you described will work regardless, since its mechanism is based on the inherent result of executing
/bin/true.I think we've mainly focused on the theoretical aspect of this, but FWIW I'm still entirely unclear on what this feature is even aimed for.
Quoted 7 lines
All of the rest of our config does not have an explicit "skip" for anything, just last-set-wins. In terms of a real-world use-case wouldn't a user just edit or comment out the config earlier in ~/.gitconfig, and not "skip" it at the end with "git config [...] --add"?
I suspect that the use-case is some Googly centrally managed
/etc/gitconfig, but that's just speculation...
Yep, this is exactly why. We've talked often on-list about how we ship and configure Git for Googlers, but the upshot is "we pack up 'next' and also ship an '/etc/gitconfig'".
But I can also think of one really basic scenario when I'd want to skip a hook in one repo without just commenting out my ~/.gitconfig: the Gerrit Change-Id hook.
Gerrit requires all commit messages to contain this Change-Id: abc123 footer. It adds the footer by way of a commit-msg hook. That hook works the same regardless of what your Gerrit remote is, so you can run the same script on any project that uses Gerrit for code review. If, as I have in the past, the vast majority of my projects use Gerrit, but I have one project which does not, then I would love to configure the Gerrit Change-Id hook globally and un-configure it for my one non-Gerrit project.
(At that time, I maintained a subsystem in a project based on Yocto, so I needed to regularly contribute to 5-10 projects, all but one of which used Gerrit. The one non-Gerrit one used a mailing list. I also had a hobby project and my dotfiles, neither of which used Gerrit. This is not an uncommon use case.)
Quoted 9 lines
Quoted 6 lines
- I do see value in allowing "hook.name-of-hook.event" to be defined repeatably, as described above, so I will include that.
[...]
- figuring out what the heck we want to do with allowing hooks to describe whether they allow parallelismWhich just to check if we're on the same page, needs to be figured out because of the complexity of that "defined repeatably", no?
As I wrote above, I think this falls under YAGNI, to be honest, and I don't think we've painted ourselves into a corner if we do end up needing it. I think the semantical complexity is a harder problem than the syntactical complexity, and I'd rather just punt it ;)
Quoted 11 lines
Quoted 4 lines
Unfortunately I've dropped the design doc from this series, since nobody seemed interested in having it checked in, but I'll try to rework the help doc to make the schema more clear.
FWIW I wasn't opposed to it per-se, but remembered commenting on it in earlier rounds, and it being some combination of docs that should really be in manpages (i.e. describing current actual behavior), more general rational and "why is it like this" (which correctly belongs in design docs), and musings about hypothetical future features (e.g. "git hook edit" and the like)'.
Yeah, that's pretty typical of design docs at places I've worked now and in the past. I don't have a big problem with not checking it in - those kinds of docs are most valuable during the early design stage, and so I think that doc has now served its purpose.
- Emily
Re: [PATCH 8/9] hook: teach 'hookcmd' config to alias hook scripts
Quoted 40 lines · Emily Shaffer
Quoted 31 lines · Ævar Arnfjörð Bjarmason
[...]
Quoted 9 lines
No, that's in fact as designed, with my model B. The user configured "echo hi" to run on "verify-commit" events; if those events are initially used by some wrapper, but later we decide they're a great idea and absorb the verify-commit event into native Git, then this is working as intended. I think your argument is based on a misunderstanding of the design.
Could it be that the way I provided the examples (my schema after A: and your schema after B:) made it hard to parse? Sorry about that if so.
Aren't you assuming that users who specify a verify-commit will be happy because git's usurping of that will 1=1 match what they were using "verify-commit" for.
I'm pointing out that we can't know that, and since you want to make "git hook run" a general thing that runs any <name> of script you've configured, and not just what's in githooks(5) that it becomes very likely that if we add a new hook with some obvious name that we'll either break things for users, or subtly change behavior.
Which isn't just theoretical, e.g. I tend to run something like a "git log --check @{u}.." before I run git-send-email, with this configurable hook mechanism having a "git hook run sendemail-check" would be a way I might expose that in my own ~/.gitconfig.
But if git-send-email learns a "sendemail-check" and the behavior doesn't exactly match mine; E.g. maybe it similar to pre-auto-gc expects me to return a status code to ask me if I want to abort on a failed --check, but mine expects a revision range to run "log --check".
Quoted 7 lines
In practice that's a non-issue with the current hook mechanism, i.e. nobody's sticking a script into .git/hooks/my-custom-name and expecting it to do anything useful (and if they are, they have only themselves to blame).
Whereas we'd now be actively inviting users to squat on the same namespace we ourselves will need for future hooks.
Quoted 8 lines
Yeah, this is a good point. Seems worth a note in the 'git hook run' doc, making a point that "you can use this for your wrapper to run specific hooks, but be careful about namespace collisions". We're a lot less likely to add a hook named "repotool-verify-commit" than we are to add a hook named "verify-commit", for example.
I think it's enough to warn about future namespace collisions and make an "at your own risk" note.
I might have lost track at this point, but later examples in this E-Mail you show don't seem to require such a note.
I.e. it's only an issue if we conflate a semantically meaningful slot like "pre-commit" in the config with one that can also have the meaning of simply defining an arbitrary user-decided name.
There's no such collision if the config uses
e.g. hook.mycheck.event=pre-commit & hook.mycheck.command=mycmd, as opposed to hook.pre-commit.command=mycmd.
On the specifics of that example: I don't really care about the bikeshedding of the config key naming specifics, just the semantics of not putting user defined names and hook type names in the same slot if we can avoid it.
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
No, but it's something I'm interested in passing as an environment variable. I didn't add it to this series because it seemed to me to distract from the core feature. We would like to add it to simplify our invocations of https://github.com/awslabs/git-secrets, so it's on my radar.
Quoted 7 lines
Quoted 2 lines
Having such an env var as part of the initial series seems like a sensible thing to have.
Eh. To me, it feels like feature creep. It also is something we could add today to the existing hook mechanism (even if it's a little pointless since you can basename, like you say), so it feels orthogonal. I would prefer not to add it in this series.
Sure, I guess you can add two hook sections to replace e.g. your {pre,post}-receive hook (which are commonly routed to the same script with file-based hooks). Having a single setenv() seems easy enough, and I'd bet a way more common use-case than wanting to skip earlier defined hooks...
Quoted 31 lines
Quoted 24 lines
Quoted 6 lines
I am not sure what it means for a single executable to write "parallel = true" - it is a single executable.
Ok, that is me being facetious - I think you are saying we can AND together all of the 'hook.<thing-with-event-we-care-about>.parallel' to decide whether or not to run in parallel.
Right, the case (whatever the config mechanism) wanting to use several off-the-shelf hooks and accomplish through git some version of this:
parallel -j8 pre-receive-parallel-*.sh &&
parallel -j1 pre-receive-non-parallel-*.shI.e. since we have N scripts for the "pre-receive" type, and we're expecting to say whether on not parallelism is OK or not, it seems like a natural thing we'll want to declare that differently for some of those than for others.
Quoted 5 lines
I would rather not discuss this now, for this series, because regardless of which config schema we use today, we can figure out "parallel unless we really don't want it" later on. It is too complex to discuss in the context of "hey, we should also configure hooks somewhere else". Let's leave it for future work.
Quoted 6 lines
The point is that no, we really can't figure it out as easily later on regardless of the config schema.
Because with 1=many you can't have 1=many.someAttribute=XYZ without that *.someAttribute=XYZ declaring something for all of 1=many, whereas if it's 1=1 then 1=1.someAttribute.XYZ obviously applies only to that 1=1.
Quoted 8 lines
I think this is moot, since we are moving to "all config hooks have a name", but my plan previously was to let this be set on a hookcmd. Essentially, your suggestion is to make every hook a hookcmd. My point was that it's easy to extend [object which represents an executable] in the config to include "always run me in series" or "run me in series for this specific event" regardless. That is, one could imagine, discarding entirely the hookcmd junk and going with the schema I sketched in my last email (which lands somewhere between yours and mine):
Just to be clear, I don't have any concrete suggestion in mind right now (actually as I write this I can only vaguely recall what I suggested before).
What I have been suggesting is not any specific implementation, but that we have a bias for the simple over the complex for an initial implementation.
Complexity can always be added later, whereas coming up with a config schema that's irregular compared to other existing config in git is something we might regret sooner than later.
Quoted 25 lines
[hook "linter"]
command =~/linter.sh
event = pre-commit
parallel = falseor...
[hook "linter"]
command =~/linter.sh
event = pre-commit
event = commit-msg[hook "linter.commit-msg"]
parallel = falseOr even...
[hook "linter"]
command =~/linter.sh
event = pre-commit
event = commit-msg
parallel = commit-msgThe possibilities go on, as far as configuration goes.
Quoted 7 lines
To me, the harder part of this problem is actually implementing the execution. We had some discussions at length early on in the config-based hook series about ways to do this kind of complex "some stuff needs synchronous execution and some stuff doesn't, in the same event" and decided that it mostly resolved to "you ain't gonna need it" principle. So I would prefer to discuss this when we find out we do actually need it.
What I was mainly going for with "we really can't figure it out as easily later" above was not that this tweaking of "jobs" or parallelism was essential per-hook.
But that it was a handy shorthand for a config attribute you might want to define for hooks, and having what are effectively groups of hooks, with N "command" or "event" in one section might make things more complex once you'd want to define optional attributes for one of those commands or events.
Quoted 40 lines
Quoted 9 lines
[...]
Quoted 4 lines
- I do see value in having an explicit .skip field rather than mapping .command to a noop, so "hook.name-of-hook.skip" as described above. Of course the method you described will work regardless, since its mechanism is based on the inherent result of executing
/bin/true.I think we've mainly focused on the theoretical aspect of this, but FWIW I'm still entirely unclear on what this feature is even aimed for.
Quoted 7 lines
All of the rest of our config does not have an explicit "skip" for anything, just last-set-wins. In terms of a real-world use-case wouldn't a user just edit or comment out the config earlier in ~/.gitconfig, and not "skip" it at the end with "git config [...] --add"?
I suspect that the use-case is some Googly centrally managed
/etc/gitconfig, but that's just speculation...Yep, this is exactly why. We've talked often on-list about how we ship and configure Git for Googlers, but the upshot is "we pack up 'next' and also ship an '
/etc/gitconfig'".But I can also think of one really basic scenario when I'd want to skip a hook in one repo without just commenting out my ~/.gitconfig: the Gerrit Change-Id hook.
Gerrit requires all commit messages to contain this Change-Id: abc123 footer. It adds the footer by way of a commit-msg hook. That hook works the same regardless of what your Gerrit remote is, so you can run the same script on any project that uses Gerrit for code review. If, as I have in the past, the vast majority of my projects use Gerrit, but I have one project which does not, then I would love to configure the Gerrit Change-Id hook globally and un-configure it for my one non-Gerrit project.
(At that time, I maintained a subsystem in a project based on Yocto, so I needed to regularly contribute to 5-10 projects, all but one of which used Gerrit. The one non-Gerrit one used a mailing list. I also had a hobby project and my dotfiles, neither of which used Gerrit. This is not an uncommon use case.)
Quoted 4 lines
I disagree fundamentally that "find and run a noop command like
/bin/true" is simpler to average users than "skip it by setting a config". Like I said below, by including "skip" both approaches will work.
In reply to this, and moving things around a bit in the reply:
Quoted 7 lines
Quoted 7 lines
All of that's something you'll need to explain in detail to users, which all seems way more complex than a simple:
To skip a previously defined hook insert a noop-command, any will do, but setting it to "true" (usually
/bin/true) is a handy convention for doing nothing.
Quoted 7 lines
Quoted 2 lines
I.e. by keeping the config field as doing one thing only you avoid any such collisions etc.
I disagree fundamentally that "find and run a noop command like
/bin/true" is simpler to average users than "skip it by setting a config". Like I said below, by including "skip" both approaches will work.
To clarify, I haven't been advocating for that "skip = true" convention because I think it's a sensible thing per-se, but that I think this use-case is something that an individual configurable feature in git doesn't need stateful syntax to deal with.
We have any number of multi-value and single-value config within git. I just don't see why on balance hooks need a special syntax to skip earlier set config for hooks specifically.
E.g. this gerrit example would also be true of someone in a corporate setting using git-send-email, and wanting a list of sendemail.cc on all but their dotfiles project, or one other non-work project.
Does that mean we need a sendemail.skipCC and special handling for it in git-send-email.perl? No, I think we'd generally advice users to just put those projects under ~/work or whatever, and then use config includes to set config for that group of projects based on the path:
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig.d/work
Or, if a hook is really so special that it's needed everywhere define it in /etc/gitconfig, and then just make the hook itself do:
if test "$(git config --bool googleHooks.disableOurGlobalHook)" = "true"
then
exit 0
fiWhich is pretty much (with the hook.* config prefix) how we've adviced users to do this since approximately forever with the sample hooks we ship.
The advantage of using includes in that way is e.g. that you can easily see how your hook came to be configured with:
git config --list --show-origin
I.e. that (by convention) it comes via a conditionally included ~/.gitconfig.d/gerrit file. If it's a multi-value like sendemail.cc the semantics are also clear, e.g. you can get all values we'll use with "git config --get-all".
Whereas choosing to implement this with something that *looks* like a config keyword, but really isn't is just confusing.
We need to explain in one way how users might arrange for the likes of sendemail.cc to be defined for some, but not all of their repos, and explain it differently when it comes to hooks. There's inherent value in that explanation being the same for both.
Re: [PATCH 8/9] hook: teach 'hookcmd' config to alias hook scripts
Quoted 53 lines · Ævar Arnfjörð Bjarmason
Quoted 40 lines · Emily Shaffer
Quoted 31 lines · Ævar Arnfjörð Bjarmason
[...]
Quoted 9 lines
No, that's in fact as designed, with my model B. The user configured "echo hi" to run on "verify-commit" events; if those events are initially used by some wrapper, but later we decide they're a great idea and absorb the verify-commit event into native Git, then this is working as intended. I think your argument is based on a misunderstanding of the design.
Could it be that the way I provided the examples (my schema after A: and your schema after B:) made it hard to parse? Sorry about that if so.
Aren't you assuming that users who specify a verify-commit will be happy because git's usurping of that will 1=1 match what they were using "verify-commit" for.
I'm pointing out that we can't know that, and since you want to make "git hook run" a general thing that runs any <name> of script you've configured, and not just what's in githooks(5) that it becomes very likely that if we add a new hook with some obvious name that we'll either break things for users, or subtly change behavior.
Which isn't just theoretical, e.g. I tend to run something like a "git log --check @{u}.." before I run git-send-email, with this configurable hook mechanism having a "git hook run sendemail-check" would be a way I might expose that in my own ~/.gitconfig.
But if git-send-email learns a "sendemail-check" and the behavior doesn't exactly match mine; E.g. maybe it similar to pre-auto-gc expects me to return a status code to ask me if I want to abort on a failed --check, but mine expects a revision range to run "log --check".
Quoted 7 lines
In practice that's a non-issue with the current hook mechanism, i.e. nobody's sticking a script into .git/hooks/my-custom-name and expecting it to do anything useful (and if they are, they have only themselves to blame).
Whereas we'd now be actively inviting users to squat on the same namespace we ourselves will need for future hooks.
Quoted 8 lines
Yeah, this is a good point. Seems worth a note in the 'git hook run' doc, making a point that "you can use this for your wrapper to run specific hooks, but be careful about namespace collisions". We're a lot less likely to add a hook named "repotool-verify-commit" than we are to add a hook named "verify-commit", for example.
I think it's enough to warn about future namespace collisions and make an "at your own risk" note.
Quoted 6 lines
I might have lost track at this point, but later examples in this E-Mail you show don't seem to require such a note.
I.e. it's only an issue if we conflate a semantically meaningful slot like "pre-commit" in the config with one that can also have the meaning of simply defining an arbitrary user-decided name.
No, I don't think that's the case. The examples I used later in the mail don't have anything to do with this "wrapper wants to use 'git hooks run'" case.
Let's imagine a current commonly-used wrapper, 'repo'
(https://gerrit.googlesource.com/git-repo/+/refs/heads/master/README.md).
This wrapper is used to manage multiple Git repos into something semantically similar to submodules.
Let's say that 'repo' decides that instead of rolling its own hook system, it would like to use Git's native hook system. Let's say that it wants to call user-defined hooks any time someone has just completed running 'repo sync'. It could ask users to define a config like so:
[hook "myrepohook"]
event = post-sync
command = ~/some/downloaded/myrepohook --with --argsThen 'repo' itself can, at the end of its 'repo sync' implementation, call 'git hook run post-sync' (instead of using some other hook specification schema).
But Git itself has a 'git submodule sync' command, and let's imagine that we decided we want to run some user-defined hook at the end of 'git submodule sync'. Naturally, we can call it 'post-sync', and then we will accidentally invoke the hooks which someone configured for 'repo' - as you pointed out would be a concern with my design, this will *also* be a concern with your design.
This specific use case is what I think we can get around with some "at your own risk" documentation, because we are much less likely to collide with someone asking their users to configure like so:
[hook "myrepohook"]
event = repo-tool-post-sync
command = ~/some/downloaded/myrepohook --with-argsQuoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
No, but it's something I'm interested in passing as an environment variable. I didn't add it to this series because it seemed to me to distract from the core feature. We would like to add it to simplify our invocations of https://github.com/awslabs/git-secrets, so it's on my radar.
Quoted 7 lines
Quoted 2 lines
Having such an env var as part of the initial series seems like a sensible thing to have.
Eh. To me, it feels like feature creep. It also is something we could add today to the existing hook mechanism (even if it's a little pointless since you can basename, like you say), so it feels orthogonal. I would prefer not to add it in this series.
Quoted 5 lines
Sure, I guess you can add two hook sections to replace e.g. your {pre,post}-receive hook (which are commonly routed to the same script with file-based hooks). Having a single
setenv()seems easy enough, and I'd bet a way more common use-case than wanting to skip earlier defined hooks...
More than anything else, I think this comment convinced me that the skip config also falls under YAGNI and I can drop it (and we can add it later if "hook.myhook.command = true" isn't clear enough).
I snipped the rest because I don't disagree or have anything further to say. Look for a reroll this weekend, we hope.
- Emily
Re: [PATCH 8/9] hook: teach 'hookcmd' config to alias hook scripts
In reply to Ævar Arnfjörð BjarmasonQuoted 11 lines
Quoted 8 lines
I would also need twice as many lines to run a script I wrote as a hook
- that is, the base case which is probably all most people will need. So with your proposal I *must* name every hook I want to add. Instead of "hook.pre-commit.command = ~/check-for-debug-strings" I need to configure "hook.check-debug-strings.event = pre-commit" and "hook.check-debug-strings.command = ~/check-for-debug-strings". That's a little frustrating, and if I never want to skip that check or move it later or something, it will be extra overhead for no gain for me.The gain is that "git hook list" becomes a trivial "git config -show-origin --show-scope --get-regexp" wrapper.
I think that in both schemes, "git hook list" is not difficult to implement.
Quoted 6 lines
Quoted 3 lines
I do agree that your approach bans the regrettably awkward "hookcmd.~/check-for-debug-strings.skip = true", but I'm not sure whether or not it's worth it.
That design choice also means that you can't expand the path using "git config --get --type=path.
I'm not sure what the use of this is, especially when the more important position is in "hook.pre-commit.command" (which can be expanded, I believe).
Quoted 4 lines
As noted in the follow-up reply while we don't case normalize the LeVeL" part of "ThReE.LeVeL.KeY" that's
tolower(), which we know isn't a 1=1 mapping on some
FS's. https://lore.kernel.org/git/[email protected]/
We're comparing strings in the config file, though, not strings against what's on the filesystem.
Quoted 6 lines
To reply to all the above, yes, your suggestion comes out ahead in being less verbose.
But isn't the real difference not in the differing prefixes, i.e. hook.* and hookcmd.* (A) v.s. always hook.* (B, which is what I'm mainly focusing on, i.e. what requires the added complexity.
OK, this is a good point - it may be confusing for the user to remember the difference between "hook" and "hookcmd", and your scheme eliminates that.
Quoted 7 lines
But that in that in your proposed way of doing it it's:
hook.<event-or-name>.*V.s. my suggestion of:
hook.<name>.*
[snip discussion of how hook.<event-or-name> is bad]
I think you were thinking that someone would say "let's unify 'hook' and 'hookcmd' then" in response to you saying "'hook' vs 'hookcmd' is bad", and you're arguing against this new point. I don't think anyone is saying that, though.
Quoted 3 lines
That complexity being that you use <event-or-name> and I use <name>, and you want to in turn support N number of "*.command" for any "hook.<event-or-name>".
I don't know if anyone is wanting to support this.
Quoted 10 lines
The way to skip hooks in my proposal is:
hook.<name>.command = true
Or whatever noop command you want on the RHS. In practice we wouldn't invoke "true", just like we don't invoke "cat" for "PAGER=cat".
But unlike "*.skip" that doesn't require complexity in implementation or user understanding, it just falls naturally out of the rest of the model.
This is a good point.
Quoted 7 lines
Even with I think it's fair to say deep knowledge of your proposal at this point I still needed to read this a few times to see if that:
command = reusable-hook
Is referring to:
Quoted 8 lines
[hookcmd "reusable-hook"]
I.e. is it going to run:
command = /long/path/reusable-hook
Or is it just re-specifying /long/path/reusable-hook but relying on a $PATH lookup?
This is true - seeing "hook.post-commit.command = reusable-hook", we need to look at the rest of the config to see how it is interpreted.
Going back to the central idea, though, I think that the main advantage of the scheme Emily proposed is the ability to write, more concisely:
[hook.pre-commit]
command = /path/to/command-1
command = /path/to/command-2
instead of
[hook.command-1]
event = pre-commit
command = /path/to/command-1
[hook.command-2]
event = pre-commit
command = /path/to/command-2
But Ævar's scheme gives us the advantage that if need to do anything more complicated (even merely slightly more complicated - for example, skipping a hook or overriding the command of a hook), it would be hard to write (and even to just figure it out) in Emily's scheme. In Ævar's scheme, just following the standard "last config wins" rule (and knowing about /usr/bin/true) can already do a lot. For this reason, I think it's worth considering Ævar's scheme - writing 3 lines instead of 2 is not much more difficult to teach and to do.
PATCH v4 9/9 hook: implement hookcmd.<name>.skip
In reply to Emily ShafferIf a user wants a specific repo to skip execution of a hook which is set at a global or system level, they will be able to do so by specifying 'skip' in their repo config:
~/.gitconfig
[hook.pre-commit]
command = skippable-oneliner
command = skippable-hookcmd
[hookcmd.skippable-hookcmd]
command = foo.sh
$GIT_DIR/.git/config
[hookcmd.skippable-oneliner]
skip = true
[hookcmd.skippable-hookcmd]
skip = true
Later it may make sense to add an option like "hookcmd.<name>.<hook-event>-skip" - but for simplicity, let's start with a universal skip setting like this.
--- Documentation/config/hook.txt | 8 ++++++++ Documentation/git-hook.txt | 30 +++++++++++++++++++++++++++++ hook.c | 31 +++++++++++++++++++++--------- t/t1360-config-based-hooks.sh | 36 +++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 9 deletions(-)
Documentation/config/hook.txt +8 -0
@@ -8,6 +8,14 @@ hookcmd.<name>.command:: as a command. This can be an executable on your device or a oneliner for your shell. See linkgit:git-hook[1]. +hookcmd.<name>.skip:: + Specify this boolean to remove a command from earlier in the execution + order. Useful if you want to make a single repo an exception to hook + configured at the system or global scope. If there is no hookcmd + specified for the command you want to skip, you can use the value of + `hook.<command>.command` as <name> as a shortcut. The "skip" setting + must be specified after the "hook.<command>.command" to have an effect. + hook.jobs:: Specifies how many hooks can be run simultaneously during parallelized hook execution. If unspecified, defaults to the number of processors on
Documentation/git-hook.txt +30 -0
@@ -57,6 +57,36 @@ and prepare-commit-msg hooks in this order: /bin/linter --c .git/hooks/prepare-commit-msg (if present) +If there is a command you wish to run in most cases but have one or two +exceptional repos where it should be skipped, you can specify +`hookcmd.<name>.skip`, for example: + +System config +---- + [hook "post-commit"] + command = check-for-secrets + + [hookcmd "check-for-secrets"] + command = /bin/secret-checker --aggressive +---- + +Local config +---- + [hookcmd "check-for-secrets"] + skip = true + # This works for inlined hook commands, too: + [hookcmd "~/typocheck.sh"] + skip = true +---- + +After these configs are added, and including the earlier example configs, the +hook list becomes: + +post-commit: + /bin/linter --c + python ~/run-test-suite.py + .git/hooks/post-commit (if present) + In general, when instructions suggest adding a script to `.git/hooks/<something>`, you can specify it in the config instead by running `git config --add hook.<something>.command <path-to-script>` - this way you can
hook.c +22 -9
@@ -18,6 +18,7 @@ static void free_hook(struct hook *ptr)*/ static struct hook *find_hook_by_command (struct list_head *head,const char *command) { +/* check if the hook is already in the list */ struct list_head *pos =NULL , *tmp =NULL ;struct hook *found =NULL ;
@@ -40,7 +41,6 @@ static struct hook * find_hook_by_command(struct list_head *head, const char *co*/ static struct hook *append_or_move_hook (struct list_head *head,const char *command) { -/* check if the hook is already in the list */ struct hook *to_add =find_hook_by_command (head, command);if (!to_add) {
@@ -175,14 +175,15 @@ static int hook_config_lookup(const char *key, const char *value, void *cb_data)if (!strcmp (key, hook_key)) {const char *command = value;struct strbuf hookcmd_name =STRBUF_INIT ; +int skip =0 ; - -if (!command) { -strbuf_release (&hookcmd_name); -BUG ("git_config_get_value overwrote a string it shouldn't have" ); - } - -/* TODO: implement skipping hooks */ +/* +* Check if we're removing that hook instead. Hookcmds are +* removed by name, and inlined hooks are removed by command +* content. +*/ +strbuf_addf (&hookcmd_name,"hookcmd.%s.skip" , command); +git_config_get_bool (hookcmd_name.buf, &skip);/* * Check if a hookcmd with that name exists. If it doesn't,
@@ -193,12 +194,24 @@ static int hook_config_lookup(const char *key, const char *value, void *cb_data)strbuf_addf (&hookcmd_name,"hookcmd.%s.command" , command);git_config_get_value (hookcmd_name.buf, &command); +if (!command) { +strbuf_release (&hookcmd_name); +BUG ("git_config_get_value overwrote a string it shouldn't have" ); + } +/* * TODO: implement an option-getting callback, e.g. * get configs by pattern hookcmd.$value.* * for each key+value, do_callback(key, value, cb_data) */ -append_or_move_hook (head, command); + +if (skip) { +struct hook *to_remove =find_hook_by_command (head, command); +if (to_remove) +remove_hook (&(to_remove->list)); + }else { +append_or_move_hook (head, command); + }strbuf_release (&hookcmd_name); }
t/t1360-config-based-hooks.sh +36 -0
@@ -103,6 +103,42 @@ test_expect_success 'git hook list shows hooks from the hookdir' ' test_cmp expected actual ' +test_expect_success 'git hook list removes skipped hookcmd' ' + setup_hookcmd && + test_config hookcmd.abc.skip "true" --add && + + cat >expected <<-EOF && + no commands configured for hook '\''pre-commit'\'' + EOF + + git hook list pre-commit >actual && + test_cmp expected actual +' + +test_expect_success 'git hook list ignores skip referring to unused hookcmd' ' + test_config hookcmd.abc.command "/path/abc" --add && + test_config hookcmd.abc.skip "true" --add && + + cat >expected <<-EOF && + no commands configured for hook '\''pre-commit'\'' + EOF + + git hook list pre-commit >actual && + test_cmp expected actual +' + +test_expect_success 'git hook list removes skipped inlined hook' ' + setup_hooks && + test_config hookcmd."$ROOT/path/ghi".skip "true" --add && + + cat >expected <<-EOF && + $ROOT/path/def + EOF + + git hook list pre-commit >actual && + test_cmp expected actual +' + test_expect_success 'inline hook definitions execute oneliners' ' test_config hook.pre-commit.command "echo \"Hello World\"" &&
Signature · 1 line
2.32.0.402.g57bb445576-goog
Re: [PATCH 00/27] Base for "config-based-hooks"
In reply to Ævar Arnfjörð BjarmasonQuoted 3 lines · Ævar Arnfjörð Bjarmason
This v3 of the "Base for 'config-based-hooks'" topic is >95% a slimmed-down versio nof Emily Schaffer's work to introduce a mechanism
(Tiniest of nits - I only have a 'c' in my middle name ;) )
Quoted 26 lines
to drive hooks via config.
This topic doesn't do that, but moves all hook execution (C libary and Perl|Python script) to either the new hook.[ch] library code, or the "git hook run" utility.
See previous iterations for more details:
v0 (Emily's): http://lore.kernel.org/git/[email protected]
v1: https://lore.kernel.org/git/[email protected]/
v2: http://lore.kernel.org/git/[email protected]This series gained two new dependencies since v2, my just-submitted preparatory topics:
https://lore.kernel.org/git/[email protected]/ https://lore.kernel.org/git/[email protected]/And hopefully addreses all the feedback on v2, mainly/entirely from Emily at https://lore.kernel.org/git/[email protected]
I'd normally have waited longer for a v3, but as discussed in the small hook.[ch] topic that precedes this one building the new
hook-list.hhad an error on Windows CI due to a missing CMake change, that's now fixed.
Quoted 7 lines
Other changes can be seen in the range-diff, all rather trivial fixes like comment fixes, trimming off "argc/argv" before we pass things to "git hook run", removing a redundant test setup etc.
I also added a GIT_TEST_FAKE_HOOKS=true for use in the test suite to make us support a "test-hook" and a "does-not-exist" hook, we'd previously accept those outside the test environment.
I initially wasn't sure this is necessary, and I'm still not entirely sure - it makes more sense to me to see test-hook and does-not-exist use essentially
https://lore.kernel.org/git/[email protected].
Would it make sense for you to absorb that much - "when we're 'git hook run'ing, don't validate hookname" - into your series to avoid this test-fake-hooks thing, and also teach the test-helper to use (or not use) the allow_unknown flag?
[...]
Quoted 4 lines
Range-diff:
1:447d349c73! 1:cf4b06bfdfhook: add 'run' subcommand
@@Documentation/git-hook.txt(new)
@@Documentation/githooks.txt: and "0" meaning they were not.
Quoted 8 lines
-@@
Makefile:LIB_OBJS+= hash-lookup.o
-LIB_OBJS+= hashmap.o
-LIB_OBJS+= help.o
-LIB_OBJS+= hex.o
-+LIB_OBJS += hook.o
-LIB_OBJS+= ident.o
-LIB_OBJS+= json-writer.o
-LIB_OBJS+= kwset.o
Ah, no longer adding hook.o to the Makefile because it's been added in one of your splintered-off dependency topics. Ok.
Quoted 5 lines
@@ builtin/hook.c (new)
+#include "strvec.h"
+
+static const char * const builtin_hook_usage[] = {
++ N_("git hook <command> [...]"),Quoted 8 lines
++ N_("git hook run <hook-name> [-- <hook-args>]"),
++ NULL
++};
++
++static const char * const builtin_hook_run_usage[] = {
+ N_("git hook run <hook-name> [-- <hook-args>]"),
+ NULL
+};Cool, so now we have a separate 'git hook<enter>' usage and 'git hook run<enter>' usage.
I was going to complain about the strings being duplicated, but it appears that other commands that use this pattern - like 'git remote' - do it the same way as you're doing here. So seems like that's a problem to ponder later, unrelated to this topic ;)
Quoted 11 lines
@@ builtin/hook.c (new)
+ };
+
+ argc = parse_options(argc, argv, prefix, run_options,
-+ builtin_hook_usage, PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
++ builtin_hook_run_usage,
++ PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
+
-+ if (argc > 2) {
-+ if (strcmp(argv[2], "--") &&
-+ strcmp(argv[2], "--end-of-options"))Quoted 8 lines
++ if (argc > 1) {
++ if (strcmp(argv[1], "--") &&
++ strcmp(argv[1], "--end-of-options"))
+ /* Having a -- for "run" is mandatory */
+ usage_with_options(builtin_hook_usage, run_options);
+ /* Add our arguments, start after -- */
-+ for (i = 3 ; i < argc; i++)
++ for (i = 2 ; i < argc; i++)Nice, so the implementation of run_hooks() has one less stray arg to worry about.
Quoted 5 lines
+ strvec_push(&opt.args, argv[i]);
+ }
+
+ /* Need to take into account core.hooksPath */
+ git_config(git_default_config, NULL);Quoted 8 lines
+
-+ hook_name = argv[1];
++ /*
++ * We are not using run_hooks() because we'd like to detect
++ * missing hooks. Let's find it ourselves and call
++ * run_found_hooks() instead.
++ */
++ hook_name = argv[0];And a more explicit comment. Thanks.
Quoted 11 lines
+ hook_path = find_hook(hook_name);
+ if (!hook_path) {
+ error("cannot find a hook named %s", hook_name);
@@ builtin/hook.c (new)
+ struct option builtin_hook_options[] = {
+ OPT_END(),
+ };
++ argc = parse_options(argc, argv, NULL, builtin_hook_options,
++ builtin_hook_usage, PARSE_OPT_STOP_AT_NON_OPTION);
++ if (!argc)
++ usage_with_options(builtin_hook_usage, builtin_hook_options);Nice - parse_options() removes the 'hook' part of the invocation for us, so we don't need to carry it around into the subcommands. Thanks.
Quoted 9 lines
+
-+ if (!strcmp(argv[1], "run"))
++ if (!strcmp(argv[0], "run"))
+ return run(argc, argv, prefix);
-+ usage_with_options(builtin_hook_usage, builtin_hook_options);
-+ return 1;
++ else
++ usage_with_options(builtin_hook_usage, builtin_hook_options);
+}Ah, and here we lose the awkward "return something even though usage_with_options() exits". Ok.
Quoted 7 lines
+ ##
hook.c##
+ static intknown_hook(const char *name)
+ {
+ const char **p;
+size_tlen = strlen(name);
++ static inttest_hooks_ok= -1;
++
Setting aside whether or not I think it's a good idea, this seems correct - we either allow test hooks for the entire lifetime of the process (e.g. from the test runner) or enforce hookname validation for the entire lifetime of the process (e.g. 'git hook run' or normal Git operation). So it makes sense to cache it in a static. Ok.
Quoted 7 lines
+ for (p = hook_name_list; *p; p++) {
+ const char *hook = *p;
+
+@@ hook.c: static int known_hook(const char *name)
+ return 1;
+ }
+ Quoted 8 lines
++ if (test_hooks_ok == -1)
++ test_hooks_ok = git_env_bool("GIT_TEST_FAKE_HOOKS", 0);
++
++ if (test_hooks_ok &&
++ (!strcmp(name, "test-hook") ||
++ !strcmp(name, "does-not-exist")))
++ return 1;
++As I mentioned above, this still seems too restrictive to me, but I don't need to rehash it here.
Quoted 7 lines
@@ hook.c (new)
+ struct hook_cb_data *hook_cb = pp_cb;
+ struct hook *run_me = hook_cb->run_me;
+
-+ if (!run_me)
-+ BUG("did we not return 1 in notify_hook_finished?");
-+I think the logic here is not quite correct, and I patched it in my series on top when I enabled multiple hooks.
My understanding of the contract with run_processes_parallel() is this:
- When you're done assigning tasks, return '0' from pick_next_hook()
- If you want to stop all task processing because of one task's exit state, return '1' from notify_hook_finished()
But you instead are unconditionally returning '1' from 'notify_hook_finished()', and not handling "no more tasks to assign" in pick_next_hook(). I think it is more correct to return 0 from 'notify_hook_finished()' - "this hook ran correctly, or if it ran poorly, at least we don't want to prevent anybody else from running, as that's not our problem". And then in your pick_next_hook(), since you know you only want to run one hook and you aren't using a list yet, at the end after you prepare the child_process, you can set hook_cb->run_me = NULL - "there is no next hook to pick" - and then use "if (!run_me)" to return 0 to tell run_processes_parallel() that there are no more tasks to assign.
I mention it because without noticing your changed flow in this patch, I wrote a fairly pernicious bug in my rebase on top. By leaving 'notify_hook_finished()' to return 1, but enabling parallelism, multihooks *appeared* to work - because all the tasks were assigned and started when running in parallel, even though the first one to finish said "wait don't start any more tasks please". But when running multiple hooks with j=1, only the first hook would run, even though the list was populated correctly - and stepping through pick_next_hook() didn't indicate why.
I *think* you did it this way to avoid calling pick_next_hook() again with nothing to iterate over, but since run_processes_parallel() is written for lists of tasks, I don't think that kludge is the "correct" approach.
Quoted 9 lines
+@@
hook.h: const char *find_hook(const char *name);
-+ /* Number of threads to parallelize across */
++ /*
++ * Number of threads to parallelize across, currently a stub,
++ * we use the parallel API for future-proofing, but we always
++ * have one hook of a given name, so this is always an
++ * implicit 1 for now.
++ */
+ int jobs;
Makes me wonder whether you really even want to leave it in here. I guess it makes your call to run_processes_parallel() cleaner. But it'd also be handy to swap '1' for 'options->jobs' in
https://lore.kernel.org/git/[email protected]
instead, if you'd prefer.
Quoted 9 lines
+};
+
@@ hook.h (new)
+ .args = STRVEC_INIT, \
+}
+
-+/*
-+ * Callback provided to feed_pipe_fn and consume_sideband_fn.
-+ */Why delete the comment? What was it hurting? ;)
Quoted 2 lines
@@
t/t1800-hook.sh(new)
++test_expect_success 'git hook run: nonexistent hook' '
Ah, thanks for '%s/--/:/'. I know it was a nit, but that was bugging me :)
Quoted 25 lines
+test_expect_success 'git -c core.hooksPath=<PATH> hook run' '
+ mkdir my-hooks &&
-+ write_script my-hooks/test-hook <<-EOF &&
-+ echo Hook ran >>actual
++ write_script my-hooks/test-hook <<-\EOF &&
++ echo Hook ran $1 >>actual
+ EOF
+
+ cat >expect <<-\EOF &&
+ Test hook
-+ Hook ran
-+ Hook ran
-+ Hook ran
-+ Hook ran
++ Hook ran one
++ Hook ran two
++ Hook ran three
++ Hook ran four
+ EOF
+
+ # Test various ways of specifying the path. See also
+ # t1350-config-hooks-path.sh
+ >actual &&
-+ git hook run test-hook 2>>actual &&
-+ git -c core.hooksPath=my-hooks hook run test-hook 2>>actual &&Quoted 8 lines
-+ git -c core.hooksPath=my-hooks/ hook run test-hook 2>>actual &&
-+ git -c core.hooksPath="$PWD/my-hooks" hook run test-hook 2>>actual &&
-+ git -c core.hooksPath="$PWD/my-hooks/" hook run test-hook 2>>actual &&
++ git hook run test-hook -- ignored 2>>actual &&
++ git -c core.hooksPath=my-hooks hook run test-hook -- one 2>>actual &&
++ git -c core.hooksPath=my-hooks/ hook run test-hook -- two 2>>actual &&
++ git -c core.hooksPath="$PWD/my-hooks" hook run test-hook -- three 2>>actual &&
++ git -c core.hooksPath="$PWD/my-hooks/" hook run test-hook -- four 2>>actual &&Thanks - this will be much easier to debug when someone (me, probably) breaks one of these by accident ;)
Quoted 5 lines
+ test_cmp expect actual
+'
+
-+test_expect_success 'set up a pre-commit hook in core.hooksPath' '
-+ >actual &&Quoted 8 lines
-+ mkdir -p .git/custom-hooks .git/hooks &&
-+ write_script .git/custom-hooks/pre-commit <<-\EOF &&
-+ echo CUSTOM >>actual
-+ EOF
-+ write_script .git/hooks/pre-commit <<-\EOF
-+ echo NORMAL >>actual
-+ EOF
-+'Hm, is this removed because we feel like the test-hook test above gives us enough coverage? Ok.
Quoted 10 lines
9:
8d8b2d2645! 7:12347d901bgit hook run: add an --ignore-missing flag
##t/t1800-hook.sh##
+-test_expect_success 'git hook run: basic' ' ++test_expect_success 'git hook run: nonexistent hook with --ignore-missing' '
+ git hook run --ignore-missing does-not-exist 2>stderr.actual &&
+test_must_be_emptystderr.actual
+'
+
-test_expect_success'git hook run -- basic' ' ++test_expect_success 'git hook run -- basic' '
Ah, I see the range-diff caught s/--/:/ earlier but that step was missed in patch 9, which made this bit of the diff a little confused.
Quoted 10 lines
11:
aa970a8175! 9:246a82b55bgit-p4: use 'git hook' to run hooks
@@ Commit message
Python, we can directly call 'git hook run'. We emulate the existence check with the --ignore-missing flag.+ As this is the last hook execution in git.git to not go through "git
+ hook run" or the hook.[ch] library we can now be absolutely sure that
+ our assertion inhook.cthat only hooks known by the generated (from
+ githooks(5))hook-list.hare permitted.
+
\o/
Quoted 12 lines
17:
c4f60db606! 15:b7c0ee9719hook: support passing stdin to hooks## builtin/hook.c ## static const char * const builtin_hook_usage[] = { + N_("git hook <command> [...]"), - N_("git hook run <hook-name> [-- <hook-args>]"), ++ N_("git hook run [<args>] <hook-name> [-- <hook-args>]"), + NULL + }; + + static const char * const builtin_hook_run_usage[] = { + N_("git hook run <hook-name> [-- <hook-args>]"), + N_("git hook run [--to-stdin=<path>] <hook-name> [-- <hook-args>]"),
Nice, and here the strings in builtin_hook_usage and builtin_hook_run_usage start to differ, which makes my earlier concern entirely moot. Thanks.
--
Aside from the questions about a) this test hook envvar and b) specific mechanics of terminating run_processes_parallel(), this series looks good to me.
- Emily
v4 cover Run hooks via "git run hook" & hook library
In reply to Ævar Arnfjörð BjarmasonThis is a v4 re-roll of the "Base for "config-based-hooks" topic.
It's grown from 27 to 36 patches mainly because I re-folded the two topics it depended on into it, i.e.:
https://lore.kernel.org/git/[email protected]/ https://lore.kernel.org/git/[email protected]/
I think those have long since reached a level of stability suitable for being merged down, but since Junio didn't pick them up separately[1][2] there wasn't much point in keeping them split-up.
This re-roll mostly addresses Emily's comments on the v3:
https://lore.kernel.org/git/[email protected]/
I.e.:
* I did not change the part where I die on hooks that aren't known to git itself. I think it makes sense to keep this topic purely for bug-for-bug compatibility with existing behavior, and to flip the RUN_SETUP_GENTLY flag in her follow-up feature topic.
* The (ab)use of the run_processes_parallel() API should be gone. I just misunderstood how it worked.
* The v3 would segfault on a plain "git hook run". I've changed builtin/hook.c to use the same pattern for subcommands as builtin/commit-graph.c et al. This also makes the usage output more consistent.
* The stub "jobs" member of the struct is now gone, and it's hardcoded to 1. Emily's feature topic can add it back (per her suggestion).
Other updates:
* In the base topic the s/Signed-off-by/Reviewed-by/g from René change that Junio applied locally has been folded in.
* Almost all the callers were just "one-shot" callers, I introduced a new run_hooks_oneshot() function for those, which gets rid of the verbosity around memory management, see e.g. the "builtin/gc.c" in the range-diff below. That run_hooks_oneshot() can also take a NULL set of options.
* We'd leak memory from "my_hook.feed_pipe_cb_data" in hook.c, we now free() it.
* Some s/STRING_LIST_INIT_DUP/STRING_LIST_INIT_NODUP & strbuf_detach()/ changes in transport.c and reference-transaction to avoid needless verbosity around memory management.
* A new 'hook.c users: use "hook_exists()" insted of "find_hook()"' patch in what was previously one of the base topics, makes subsequent changes smaller.
1. https://lore.kernel.org/git/[email protected]/
2. https://lore.kernel.org/git/[email protected]/
Emily Shaffer (26):
hook.c: add a hook_exists() wrapper and use it in bugreport.c
hook: add 'run' subcommand
gc: use hook library for pre-auto-gc hook
rebase: convert pre-rebase to use hook.h
am: convert applypatch to use hook.h
hooks: convert 'post-checkout' hook to hook library
merge: convert post-merge to use hook.h
send-email: use 'git hook run' for 'sendemail-validate'
git-p4: use 'git hook' to run hooks
commit: convert {pre-commit,prepare-commit-msg} hook to hook.h
read-cache: convert post-index-change to use hook.h
receive-pack: convert push-to-checkout hook to hook.h
run-command: remove old run_hook_{le,ve}() hook API
run-command: allow stdin for run_processes_parallel
hook: support passing stdin to hooks
am: convert 'post-rewrite' hook to hook.h
run-command: add stdin callback for parallelization
hook: provide stdin by string_list or callback
hook: convert 'post-rewrite' hook in sequencer.c to hook.h
transport: convert pre-push hook to hook.h
reference-transaction: use hook.h to run hooks
run-command: allow capturing of collated output
hooks: allow callers to capture output
receive-pack: convert 'update' hook to hook.h
post-update: use hook.h library
receive-pack: convert receive hooks to hook.h
Ævar Arnfjörð Bjarmason (10):
Makefile: mark "check" target as .PHONY
Makefile: stop hardcoding {command,config}-list.h
Makefile: remove an out-of-date comment
hook.[ch]: move find_hook() to this new library hook.c users: use "hook_exists()" insted of "find_hook()" hook-list.h: add a generated list of hooks, like config-list.h git hook run: add an --ignore-missing flag
hook tests: test for exact "pre-push" hook input hook tests: use a modern style for "pre-push" tests
hooks: fix a TOCTOU in "did we run a hook?" heuristic
.gitignore | 2 + Documentation/git-hook.txt | 51 +++++ Documentation/githooks.txt | 4 + Makefile | 26 ++- builtin.h | 1 + builtin/am.c | 29 +-- builtin/bugreport.c | 46 +---- builtin/checkout.c | 14 +- builtin/clone.c | 6 +- builtin/commit.c | 19 +- builtin/fetch.c | 1 + builtin/gc.c | 3 +- builtin/hook.c | 92 +++++++++ builtin/merge.c | 21 +- builtin/rebase.c | 6 +- builtin/receive-pack.c | 285 +++++++++++++--------------- builtin/submodule--helper.c | 2 +- builtin/worktree.c | 29 ++- command-list.txt | 1 + commit.c | 16 +- commit.h | 3 +- compat/vcbuild/README | 2 +- config.mak.uname | 6 +- contrib/buildsystems/CMakeLists.txt | 7 + generate-hooklist.sh | 18 ++ git-p4.py | 72 +------ git-send-email.perl | 20 +- git.c | 1 + hook.c | 246 ++++++++++++++++++++++++ hook.h | 126 ++++++++++++ read-cache.c | 11 +- refs.c | 41 ++-- reset.c | 14 +- run-command.c | 157 +++++++-------- run-command.h | 55 +++--- sequencer.c | 86 ++++----- submodule.c | 1 + t/helper/test-run-command.c | 46 ++++- t/t0061-run-command.sh | 37 ++++ t/t1800-hook.sh | 156 +++++++++++++++ t/t5571-pre-push-hook.sh | 94 +++++---- t/t9001-send-email.sh | 4 +- transport.c | 57 ++---- 43 files changed, 1303 insertions(+), 611 deletions(-) create mode 100644 Documentation/git-hook.txt create mode 100644 builtin/hook.c create mode 100755 generate-hooklist.sh create mode 100644 hook.c create mode 100644 hook.h create mode 100755 t/t1800-hook.sh
Range-diff against v3:
1: 27c94247f87 = 1: 81fe1ed90d5 Makefile: mark "check" target as .PHONY
2: 6e164edb0b0 ! 2: 0f749530777 Makefile: stop hardcoding {command,config}-list.h @@ Commit message
added in 029bac01a8 (Makefile: add {program,xdiff,test,git,fuzz}-objs & objects targets, 2021-02-23).
+ A subsequent commit will add a new generated hook-list.h. By doing
+ this refactoring we'll only need to add the new file to the
+ GENERATED_H variable, not EXCEPT_HDRS, the vcbuild/README etc.
+
I have not tested the Windows-specific change in config.mak.uname being made here, but we use other variables from the Makefile in the same block, and the GENERATED_H is fully defined before we include config.mak.uname.
Hardcoding command-list.h there seems to have been a case of
- copy/paste programming in dce7d29551 (msvc: support building Git using
- MS Visual C++, 2019-06-25). The config-list.h was added later in
- 709df95b78 (help: move list_config_help to builtin/help, 2020-04-16).
+ copy/paste programming in 976aaedca0 (msvc: add a Makefile target to
+ pre-generate the Visual Studio solution, 2019-07-29). The
+ config-list.h was added later in 709df95b78 (help: move
+ list_config_help to builtin/help, 2020-04-16).
3: ddae86802e2 ! 3: 644b31fe281 Makefile: remove an out-of-date comment
@@ Commit message
The rest of it was also somewhere between inaccurate and outdated, since as of b8ba629264 (Makefile: fold MISC_H into LIB_H, 2012-06-20) it's not followed by a list of header files, that got moved earlier in
- the file into LIB_H in b8ba629264 (Makefile: fold MISC_H into LIB_H,
- 2012-06-20).
+ the file into LIB_H in 60d24dd255 (Makefile: fold XDIFF_H and VCSSVN_H
+ into LIB_H, 2012-07-06).
Let's just remove it entirely, to the extent that we have anything useful to say here the comment on the
@@ Makefile: ifneq ($(dep_files_present),)
-# Dependencies on automatically generated headers such as command-list.h -# should _not_ be included here, since they are necessary even when -# building an object for the first time.
-
+-
$(OBJECTS): $(LIB_H) $(GENERATED_H)
endif
+
4: 58c37e4f06e = 4: 89c4d44b0c3 hook.[ch]: move find_hook() to this new library
5: 0cf7e078ef4 = 5: 3514e0c0251 hook.c: add a hook_exists() wrapper and use it in bugreport.c
-: ----------- > 6: d5ef40f77dc hook.c users: use "hook_exists()" insted of "find_hook()"
6: f343fc7ae66 ! 7: 4cfd72722c1 hook-list.h: add a generated list of hooks, like config-list.h
@@ Commit message
- 976aaedca0 (msvc: add a Makefile target to pre-generate the Visual
Studio solution, 2019-07-29)
+ The LC_ALL=C is needed because at least in my locale the dash ("-") is
+ ignored for the purposes of sorting, which results in a different
+ order. I'm not aware of anything in git that has a hard dependency on
+ the order, but e.g. the bugreport output would end up using whatever
+ locale was in effect when git was compiled.
+
Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]>
+ Helped-by: René Scharfe <[email protected]>
## .gitignore ##
@@
@@ Makefile: command-list.h: $(wildcard Documentation/git*.txt)
$(patsubst %,--exclude-program %,$(EXCLUDED_PROGRAMS)) \
command-list.txt >$@+ && mv $@+ $@
-+hook-list.h: generate-hooklist.sh
-+hook-list.h: Documentation/githooks.txt
++hook-list.h: generate-hooklist.sh Documentation/githooks.txt
+ $(QUIET_GEN)$(SHELL_PATH) ./generate-hooklist.sh \
+ >$@+ && mv $@+ $@
+
@@ contrib/buildsystems/CMakeLists.txt: if(NOT EXISTS ${CMAKE_BINARY_DIR}/config-li
## generate-hooklist.sh (new) ##
@@
+#!/bin/sh
++#
++# Usage: ./generate-hooklist.sh >hook-list.h
+
-+echo "/* Automatically generated by generate-hooklist.sh */"
++cat <<EOF
++/* Automatically generated by generate-hooklist.sh */
+
-+print_hook_list () {
-+ cat <<EOF
+static const char *hook_name_list[] = {
+EOF
-+ perl -ne '
-+ chomp;
-+ @l[$.] = $_;
-+ push @h => $l[$. - 1] if /^~~~+$/s;
-+ END {
-+ print qq[\t"$_",\n] for sort @h;
-+ }
-+ ' <Documentation/githooks.txt
-+ cat <<EOF
++
++sed -n -e '/^~~~~*$/ {x; s/^.*$/ "&",/; p;}; x' \
++ <Documentation/githooks.txt |
++ LC_ALL=C sort
++
++cat <<EOF
+ NULL,
+};
+EOF
-+}
-+
-+echo
-+print_hook_list
## hook.c ##
@@
7: cf4b06bfdf8 ! 8: 7cb4a4cb69e hook: add 'run' subcommand
@@ Commit message
let's start with the bare minimum required to support our simplest
hooks.
+ In terms of implementation the usage_with_options() and "goto usage"
+ pattern here mirrors that of
+ builtin/{commit-graph,multi-pack-index}.c.
+
Signed-off-by: Emily Shaffer <[email protected]>
Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]>
@@ builtin/hook.c (new)
+#include "strbuf.h"
+#include "strvec.h"
+
++#define BUILTIN_HOOK_RUN_USAGE \
++ N_("git hook run <hook-name> [-- <hook-args>]")
++
+static const char * const builtin_hook_usage[] = {
-+ N_("git hook <command> [...]"),
-+ N_("git hook run <hook-name> [-- <hook-args>]"),
++ BUILTIN_HOOK_RUN_USAGE,
+ NULL
+};
+
+static const char * const builtin_hook_run_usage[] = {
-+ N_("git hook run <hook-name> [-- <hook-args>]"),
++ BUILTIN_HOOK_RUN_USAGE,
+ NULL
+};
+
@@ builtin/hook.c (new)
+{
+ int i;
+ struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
-+ int rc = 0;
+ const char *hook_name;
+ const char *hook_path;
-+
+ struct option run_options[] = {
+ OPT_END(),
+ };
++ int ret;
+
+ argc = parse_options(argc, argv, prefix, run_options,
+ builtin_hook_run_usage,
-+ PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
-+
-+ if (argc > 1) {
-+ if (strcmp(argv[1], "--") &&
-+ strcmp(argv[1], "--end-of-options"))
-+ /* Having a -- for "run" is mandatory */
-+ usage_with_options(builtin_hook_usage, run_options);
-+ /* Add our arguments, start after -- */
-+ for (i = 2 ; i < argc; i++)
-+ strvec_push(&opt.args, argv[i]);
-+ }
++ PARSE_OPT_KEEP_DASHDASH);
+
-+ /* Need to take into account core.hooksPath */
-+ git_config(git_default_config, NULL);
++ if (!argc)
++ goto usage;
+
+ /*
-+ * We are not using run_hooks() because we'd like to detect
-+ * missing hooks. Let's find it ourselves and call
-+ * run_found_hooks() instead.
++ * Having a -- for "run" when providing <hook-args> is
++ * mandatory.
+ */
++ if (argc > 1 && strcmp(argv[1], "--") &&
++ strcmp(argv[1], "--end-of-options"))
++ goto usage;
++
++ /* Add our arguments, start after -- */
++ for (i = 2 ; i < argc; i++)
++ strvec_push(&opt.args, argv[i]);
++
++ /* Need to take into account core.hooksPath */
++ git_config(git_default_config, NULL);
++
+ hook_name = argv[0];
+ hook_path = find_hook(hook_name);
+ if (!hook_path) {
+ error("cannot find a hook named %s", hook_name);
+ return 1;
+ }
-+ rc = run_found_hooks(hook_name, hook_path, &opt);
+
++ ret = run_hooks(hook_name, hook_path, &opt);
+ run_hooks_opt_clear(&opt);
-+
-+ return rc;
++ return ret;
++usage:
++ usage_with_options(builtin_hook_run_usage, run_options);
+}
+
+int cmd_hook(int argc, const char **argv, const char *prefix)
@@ builtin/hook.c (new)
+ struct option builtin_hook_options[] = {
+ OPT_END(),
+ };
++
+ argc = parse_options(argc, argv, NULL, builtin_hook_options,
+ builtin_hook_usage, PARSE_OPT_STOP_AT_NON_OPTION);
+ if (!argc)
-+ usage_with_options(builtin_hook_usage, builtin_hook_options);
++ goto usage;
+
+ if (!strcmp(argv[0], "run"))
+ return run(argc, argv, prefix);
-+ else
-+ usage_with_options(builtin_hook_usage, builtin_hook_options);
++
++usage:
++ usage_with_options(builtin_hook_usage, builtin_hook_options);
+}
## command-list.txt ##
@@ hook.c: int hook_exists(const char *name)
+ struct hook_cb_data *hook_cb = pp_cb;
+ struct hook *run_me = hook_cb->run_me;
+
++ if (!run_me)
++ return 0;
++
+ cp->no_stdin = 1;
+ cp->env = hook_cb->options->env.v;
+ cp->stdout_to_stderr = 1;
@@ hook.c: int hook_exists(const char *name)
+ /* Provide context for errors if necessary */
+ *pp_task_cb = run_me;
+
++ /*
++ * This pick_next_hook() will be called again, we're only
++ * running one hook, so indicate that no more work will be
++ * done.
++ */
++ hook_cb->run_me = NULL;
++
+ return 1;
+}
+
@@ hook.c: int hook_exists(const char *name)
+
+ hook_cb->rc |= result;
+
-+ return 1;
++ return 0;
+}
+
-+int run_found_hooks(const char *hook_name, const char *hook_path,
-+ struct run_hooks_opt *options)
++int run_hooks(const char *hook_name, const char *hook_path,
++ struct run_hooks_opt *options)
+{
+ struct hook my_hook = {
+ .hook_path = hook_path,
@@ hook.c: int hook_exists(const char *name)
+ .hook_name = hook_name,
+ .options = options,
+ };
-+ cb_data.run_me = &my_hook;
++ int jobs = 1;
++
++ if (!options)
++ BUG("a struct run_hooks_opt must be provided to run_hooks");
+
-+ if (options->jobs != 1)
-+ BUG("we do not handle %d or any other != 1 job number yet", options->jobs);
++ cb_data.run_me = &my_hook;
+
-+ run_processes_parallel_tr2(options->jobs,
++ run_processes_parallel_tr2(jobs,
+ pick_next_hook,
+ notify_start_failure,
+ notify_hook_finished,
@@ hook.c: int hook_exists(const char *name)
+ hook_name);
+
+ return cb_data.rc;
-+}
-+
-+int run_hooks(const char *hook_name, struct run_hooks_opt *options)
-+{
-+ const char *hook_path;
-+ int ret;
-+ if (!options)
-+ BUG("a struct run_hooks_opt must be provided to run_hooks");
-+
-+ hook_path = find_hook(hook_name);
-+
-+ /*
-+ * If you need to act on a missing hook, use run_found_hooks()
-+ * instead
-+ */
-+ if (!hook_path)
-+ return 0;
-+
-+ ret = run_found_hooks(hook_name, hook_path, options);
-+ return ret;
+}
## hook.h ##
@@ hook.h: const char *find_hook(const char *name);
+
+ /* Args to be passed to each hook */
+ struct strvec args;
-+
-+ /*
-+ * Number of threads to parallelize across, currently a stub,
-+ * we use the parallel API for future-proofing, but we always
-+ * have one hook of a given name, so this is always an
-+ * implicit 1 for now.
-+ */
-+ int jobs;
+};
+
+#define RUN_HOOKS_OPT_INIT { \
-+ .jobs = 1, \
+ .env = STRVEC_INIT, \
+ .args = STRVEC_INIT, \
+}
+
++/*
++ * Callback provided to feed_pipe_fn and consume_sideband_fn.
++ */
+struct hook_cb_data {
+ /* rc reflects the cumulative failure state */
+ int rc;
@@ hook.h: const char *find_hook(const char *name);
+
+void run_hooks_opt_clear(struct run_hooks_opt *o);
+
-+/*
-+ * Calls find_hook(hookname) and runs the hooks (if any) with
-+ * run_found_hooks().
-+ */
-+int run_hooks(const char *hook_name, struct run_hooks_opt *options);
-+
-+/*
-+ * Takes an already resolved hook and runs it. Internally the simpler
-+ * run_hooks() will call this.
++/**
++ * Takes an already resolved hook found via find_hook() and runs
++ * it. Does not call run_hooks_opt_clear() for you.
+ */
-+int run_found_hooks(const char *hookname, const char *hook_path,
-+ struct run_hooks_opt *options);
++int run_hooks(const char *hookname, const char *hook_path,
++ struct run_hooks_opt *options);
#endif
## t/t1800-hook.sh (new) ##
@@ t/t1800-hook.sh (new)
+
+test_expect_success 'git hook usage' '
+ test_expect_code 129 git hook &&
-+ test_expect_code 129 git hook -h &&
-+ test_expect_code 129 git hook run -h
++ test_expect_code 129 git hook run &&
++ test_expect_code 129 git hook run -h &&
++ test_expect_code 129 git hook run --unknown 2>err &&
++ grep "unknown option" err
+'
+
+test_expect_success 'setup GIT_TEST_FAKE_HOOKS=true to permit "test-hook" and "does-not-exist" names"' '
8: 7209f73f281 ! 9: 2b8500aa675 gc: use hook library for pre-auto-gc hook
@@ Metadata
## Commit message ##
gc: use hook library for pre-auto-gc hook
- Using the hook.h library instead of the run-command.h library to run
- pre-auto-gc means that those hooks can be set up in config files, as
- well as in the hookdir. pre-auto-gc is called only from builtin/gc.c.
+ Move the pre-auto-gc hook away from run-command.h to and over to the
+ new hook.h library.
+
+ To do this introduce a simple run_hooks_oneshot() wrapper, we'll be
+ using it extensively for these simple cases of wanting to run a single
+ hook under a given name, and having it free the memory we allocate for
+ us.
Signed-off-by: Emily Shaffer <[email protected]>
Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]>
@@ builtin/gc.c
#define FAILED_RUN "failed to run %s"
-@@ builtin/gc.c: static void add_repack_incremental_option(void)
-
- static int need_to_gc(void)
- {
-+ struct run_hooks_opt hook_opt = RUN_HOOKS_OPT_INIT;
-+
- /*
- * Setting gc.auto to 0 or negative can disable the
- * automatic gc.
@@ builtin/gc.c: static int need_to_gc(void)
else
return 0;
- if (run_hook_le(NULL, "pre-auto-gc", NULL))
-+ if (run_hooks("pre-auto-gc", &hook_opt)) {
-+ run_hooks_opt_clear(&hook_opt);
++ if (run_hooks_oneshot("pre-auto-gc", NULL))
return 0;
-+ }
-+ run_hooks_opt_clear(&hook_opt);
return 1;
}
+
+ ## hook.c ##
+@@ hook.c: int run_hooks(const char *hook_name, const char *hook_path,
+ return cb_data.rc;
+ }
++
++int run_hooks_oneshot(const char *hook_name, struct run_hooks_opt *options)
++{
++ const char *hook_path;
++ int ret;
++ struct run_hooks_opt hook_opt_scratch = RUN_HOOKS_OPT_INIT;
++
++ if (!options)
++ options = &hook_opt_scratch;
++
++ hook_path = find_hook(hook_name);
++ if (!hook_path) {
++ ret = 0;
++ goto cleanup;
++ }
++
++ ret = run_hooks(hook_name, hook_path, options);
++cleanup:
++ run_hooks_opt_clear(options);
++ return ret;
++}
+
+ ## hook.h ##
+@@ hook.h: void run_hooks_opt_clear(struct run_hooks_opt *o);
+ /**
+ * Takes an already resolved hook found via find_hook() and runs
+ * it. Does not call run_hooks_opt_clear() for you.
++ *
++ * See run_hooks_oneshot() for the simpler one-shot API.
+ */
+ int run_hooks(const char *hookname, const char *hook_path,
+ struct run_hooks_opt *options);
++
++/**
++ * Calls find_hook() on your "hook_name" and runs the hooks (if any)
++ * with run_hooks().
++ *
++ * If "options" is provided calls run_hooks_opt_clear() on it for
++ * you. If "options" is NULL a scratch one will be provided for you
++ * before calling run_hooks().
++ */
++int run_hooks_oneshot(const char *hook_name, struct run_hooks_opt *options);
++
+ #endif 9: e9a1e7cf61e ! 10: 3ee55d2c10f rebase: teach pre-rebase to use hook.h
@@ Metadata
Author: Emily Shaffer <[email protected]>
## Commit message ##
- rebase: teach pre-rebase to use hook.h
+ rebase: convert pre-rebase to use hook.h
Move the pre-rebase hook away from run-command.h to and over to the new hook.h library.
@@ builtin/rebase.c: int cmd_rebase(int argc, const char **argv, const char *prefix
if (!ok_to_skip_pre_rebase &&
- run_hook_le(NULL, "pre-rebase", options.upstream_arg,
- argc ? argv[0] : NULL, NULL))
-+ run_hooks("pre-rebase", &hook_opt)) {
-+ run_hooks_opt_clear(&hook_opt);
++ run_hooks_oneshot("pre-rebase", &hook_opt))
die(_("The pre-rebase hook refused to rebase."));
-+ }
-+ run_hooks_opt_clear(&hook_opt);
if (options.flags & REBASE_DIFFSTAT) {
- struct diff_options opts;
10: 1d087269303 ! 11: 050f20d14f0 am: convert applypatch hooks to use config
@@ Metadata
Author: Emily Shaffer <[email protected]>
## Commit message ##
- am: convert applypatch hooks to use config
+ am: convert applypatch to use hook.h
Teach pre-applypatch, post-applypatch, and applypatch-msg to use the
hook.h library instead of the run-command.h library.
@@ builtin/am.c: static void am_destroy(const struct am_state *state)
assert(state->msg);
- ret = run_hook_le(NULL, "applypatch-msg", am_path(state, "final-commit"), NULL);
+ strvec_push(&opt.args, am_path(state, "final-commit"));
-+ ret = run_hooks("applypatch-msg", &opt);
-+ run_hooks_opt_clear(&opt);
++ ret = run_hooks_oneshot("applypatch-msg", &opt);
if (!ret) {
FREE_AND_NULL(state->msg);
@@ builtin/am.c: static void do_commit(const struct am_state *state)
- struct commit_list *parents = NULL;
const char *reflog_msg, *author, *committer = NULL;
struct strbuf sb = STRBUF_INIT;
-+ struct run_hooks_opt hook_opt_pre = RUN_HOOKS_OPT_INIT;
-+ struct run_hooks_opt hook_opt_post = RUN_HOOKS_OPT_INIT;
- if (run_hook_le(NULL, "pre-applypatch", NULL))
-+ if (run_hooks("pre-applypatch", &hook_opt_pre)) {
-+ run_hooks_opt_clear(&hook_opt_pre);
++ if (run_hooks_oneshot("pre-applypatch", NULL))
exit(1);
-+ }
if (write_cache_as_tree(&tree, 0, NULL))
- die(_("git write-tree failed to write a tree"));
@@ builtin/am.c: static void do_commit(const struct am_state *state)
fclose(fp);
}
- run_hook_le(NULL, "post-applypatch", NULL);
-+ run_hooks("post-applypatch", &hook_opt_post);
++ run_hooks_oneshot("post-applypatch", NULL);
-+ run_hooks_opt_clear(&hook_opt_pre);
-+ run_hooks_opt_clear(&hook_opt_post);
strbuf_release(&sb);
}
-
11: 32eec5dc2f0 ! 12: ac875d284da hooks: convert 'post-checkout' hook to hook library
@@ builtin/checkout.c: struct branch_info {
- oid_to_hex(new_commit ? &new_commit->object.oid : null_oid()),
- changed ? "1" : "0", NULL);
+ struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
-+ int rc;
+
/* "new_commit" can be NULL when checking out from the index before
a commit exists. */
@@ builtin/checkout.c: struct branch_info {
+ oid_to_hex(new_commit ? &new_commit->object.oid : null_oid()),
+ changed ? "1" : "0",
+ NULL);
-+ rc = run_hooks("post-checkout", &opt);
-+ run_hooks_opt_clear(&opt);
-+ return rc;
++ return run_hooks_oneshot("post-checkout", &opt);
}
static int update_some(const struct object_id *oid, struct strbuf *base,
@@ builtin/clone.c: static int checkout(int submodule_progress)
- err |= run_hook_le(NULL, "post-checkout", oid_to_hex(null_oid()),
- oid_to_hex(&oid), "1", NULL);
+ strvec_pushl(&hook_opt.args, oid_to_hex(null_oid()), oid_to_hex(&oid), "1", NULL);
-+ err |= run_hooks("post-checkout", &hook_opt);
-+ run_hooks_opt_clear(&hook_opt);
++ err |= run_hooks_oneshot("post-checkout", &hook_opt);
if (!err && (option_recurse_submodules.nr > 0)) {
struct strvec args = STRVEC_INIT;
@@ builtin/worktree.c: static int add_worktree(const char *path, const char *refnam
+ opt.dir = path;
+ opt.absolute_path = 1;
+
-+ ret = run_hooks("post-checkout", &opt);
-+
-+ run_hooks_opt_clear(&opt);
++ ret = run_hooks_oneshot("post-checkout", &opt);
}
strvec_clear(&child_env);
@@ hook.c: static int pick_next_hook(struct child_process *cp,
/* add command */
strvec_push(&cp->args, run_me->hook_path);
@@ hook.c: static int notify_hook_finished(int result,
- int run_found_hooks(const char *hook_name, const char *hook_path,
- struct run_hooks_opt *options)
+ int run_hooks(const char *hook_name, const char *hook_path,
+ struct run_hooks_opt *options)
{
+ struct strbuf abs_path = STRBUF_INIT;
struct hook my_hook = {
.hook_path = hook_path,
};
-@@ hook.c: int run_found_hooks(const char *hook_name, const char *hook_path,
- .hook_name = hook_name,
- .options = options,
- };
+@@ hook.c: int run_hooks(const char *hook_name, const char *hook_path,
+ if (!options)
+ BUG("a struct run_hooks_opt must be provided to run_hooks");
+
+ if (options->absolute_path) {
+ strbuf_add_absolute_path(&abs_path, hook_path);
+ my_hook.hook_path = abs_path.buf;
+ }
cb_data.run_me = &my_hook;
- if (options->jobs != 1)
-@@ hook.c: int run_found_hooks(const char *hook_name, const char *hook_path,
- &cb_data,
+ run_processes_parallel_tr2(jobs,
+@@ hook.c: int run_hooks(const char *hook_name, const char *hook_path,
"hook",
hook_name);
+
+ if (options->absolute_path)
+ strbuf_release(&abs_path);
-
++
return cb_data.rc;
}
+
## hook.h ##
@@ hook.h: struct run_hooks_opt
- * implicit 1 for now.
- */
- int jobs;
+
+ /* Args to be passed to each hook */
+ struct strvec args;
+
+ /* Resolve and run the "absolute_path(hook)" instead of
+ * "hook". Used for "git worktree" hooks
@@ hook.h: struct run_hooks_opt
+
+ /* Path to initial working directory for subprocess */
+ const char *dir;
-+
};
#define RUN_HOOKS_OPT_INIT { \
## read-cache.c ##
@@
- #include "progress.h"
#include "sparse-index.h"
#include "csum-file.h"
+ #include "promisor-remote.h"
+#include "hook.h"
/* Mask for the name length in ce_flags in the on-disk index */
@@ reset.c: int reset_head(struct repository *r, struct object_id *oid, const char
+ oid_to_hex(oid),
+ "1",
+ NULL);
-+ run_hooks("post-checkout", &opt);
-+ run_hooks_opt_clear(&opt);
++ run_hooks_oneshot("post-checkout", &opt);
+ }
leave_reset_head:12: e9fa3f67593 ! 13: 69763bc2255 merge: use config-based hooks for post-merge hook
@@ Metadata
Author: Emily Shaffer <[email protected]>
## Commit message ##
- merge: use config-based hooks for post-merge hook
+ merge: convert post-merge to use hook.h
- Teach post-merge to use the hook.h library instead of the run-command.h
- library to run hooks. This means that post-merge hooks can come from the
- config as well as from the hookdir. post-merge is invoked only from
- builtin/merge.c.
+ Teach post-merge to use the hook.h library instead of the
+ run-command.h library to run hooks.
@@ 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);
+ strvec_push(&opt.args, squash ? "1" : "0");
-+ run_hooks("post-merge", &opt);
-+ run_hooks_opt_clear(&opt);
++ run_hooks_oneshot("post-merge", &opt);
apply_autostash(git_path_merge_autostash(the_repository)); strbuf_release(&reflog_message);
-@@ builtin/merge.c: static void prepare_to_commit(struct commit_list *remoteheads)
- * and write it out as a tree. We must do this before we invoke
- * the editor and after we invoke run_status above.
- */
-- if (find_hook("pre-merge-commit"))
-+ if (hook_exists("pre-merge-commit"))
- discard_cache();
- read_cache_from(index_file);
- strbuf_addbuf(&msg, &merge_msg);
13: 12347d901bb ! 14: 2ca1ca1b8e4 git hook run: add an --ignore-missing flag
@@ Documentation/git-hook.txt: optional `--` (or `--end-of-options`, see linkgit:gi
linkgit:githooks[5]
## builtin/hook.c ##
+@@
+ #include "strvec.h"
+
+ #define BUILTIN_HOOK_RUN_USAGE \
+- N_("git hook run <hook-name> [-- <hook-args>]")
++ N_("git hook run [--ignore-missing] <hook-name> [-- <hook-args>]")
+
+ static const char * const builtin_hook_usage[] = {
+ BUILTIN_HOOK_RUN_USAGE,
@@ builtin/hook.c: static int run(int argc, const char **argv, const char *prefix)
+ {
int i;
struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
- int rc = 0;
+ int ignore_missing = 0;
const char *hook_name;
const char *hook_path;
-
struct option run_options[] = {
+ OPT_BOOL(0, "ignore-missing", &ignore_missing,
+ N_("exit quietly with a zero exit code if the requested hook cannot be found")),
OPT_END(),
};
-
+ int ret;
@@ builtin/hook.c: static int run(int argc, const char **argv, const char *prefix)
- /*
- * We are not using run_hooks() because we'd like to detect
- * missing hooks. Let's find it ourselves and call
-- * run_found_hooks() instead.
-+ * run_found_hooks() instead...
- */
+ git_config(git_default_config, NULL);
+
hook_name = argv[0];
++ if (ignore_missing)
++ return run_hooks_oneshot(hook_name, &opt);
hook_path = find_hook(hook_name);
if (!hook_path) {
-+ /* ... act like run_hooks() under --ignore-missing */
-+ if (ignore_missing)
-+ return 0;
error("cannot find a hook named %s", hook_name);
- return 1;
- }
## t/t1800-hook.sh ##
@@ t/t1800-hook.sh: test_expect_success 'git hook run: nonexistent hook' '
test_cmp stderr.expect stderr.actual
'
--test_expect_success 'git hook run: basic' '
+test_expect_success 'git hook run: nonexistent hook with --ignore-missing' '
+ git hook run --ignore-missing does-not-exist 2>stderr.actual &&
+ test_must_be_empty stderr.actual
+'
+
-+test_expect_success 'git hook run -- basic' '
+ test_expect_success 'git hook run: basic' '
write_script .git/hooks/test-hook <<-EOF &&
echo Test hook
- EOF
14: 71d209b4077 ! 15: 5b66b04bec7 send-email: use 'git hook run' for 'sendemail-validate'
@@ git-send-email.perl: sub validate_patch {
if ($repo) {
+ my $hook_name = 'sendemail-validate';
my $hooks_path = $repo->command_oneline('rev-parse', '--git-path', 'hooks');
-- my $validate_hook = catfile($hooks_path,
+ require File::Spec;
+- my $validate_hook = File::Spec->catfile($hooks_path,
- 'sendemail-validate');
-+ my $validate_hook = catfile($hooks_path, $hook_name);
++ my $validate_hook = File::Spec->catfile($hooks_path, $hook_name);
my $hook_error;
if (-x $validate_hook) {
- my $target = abs_path($fn);
+ require Cwd;
@@ git-send-email.perl: sub validate_patch {
chdir($repo->wc_path() or $repo->repo_path())
or die("chdir: $!");
15: 246a82b55b2 = 16: 14a37a43db2 git-p4: use 'git hook' to run hooks
16: e3f8482d803 ! 17: ad5d0e0e7de commit: use hook.h to execute hooks @@ Metadata
Author: Emily Shaffer <[email protected]>
## Commit message ##
- commit: use hook.h to execute hooks
+ commit: convert {pre-commit,prepare-commit-msg} hook to hook.h
- Teach run_commit_hook() to call hook.h instead of run-command.h. This
- covers 'pre-commit', 'commit-msg', and
- 'prepare-commit-msg'.
-
- Additionally, ask the hook library - not run-command - whether any
- hooks will be run, as it's possible hooks may exist in the config but
- not the hookdir.
-
- Because all but 'post-commit' hooks are expected to make some state
- change, force all but 'post-commit' hook to run in series. 'post-commit'
- "is meant primarily for notification, and cannot affect the outcome of
- git commit," so it is fine to run in parallel.
+ Move these hooks hook away from run-command.h to and over to the new
+ hook.h library.
- ## builtin/commit.c ##
-@@ builtin/commit.c: static int prepare_to_commit(const char *index_file, const char *prefix,
- return 0;
- }
-
-- if (!no_verify && find_hook("pre-commit")) {
-+ if (!no_verify && hook_exists("pre-commit")) {
- /*
- * Re-read the index as pre-commit hook could have updated it,
- * and write it out as a tree. We must do this before we invoke
-
## commit.c ##
@@
#include "commit-reach.h"
@@ commit.c: size_t ignore_non_trailer(const char *buf, size_t len)
- struct strvec hook_env = STRVEC_INIT;
+ struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
va_list args;
+- int ret;
+ const char *arg;
- int ret;
--
+
- strvec_pushf(&hook_env, "GIT_INDEX_FILE=%s", index_file);
+ strvec_pushf(&opt.env, "GIT_INDEX_FILE=%s", index_file);
@@ commit.c: size_t ignore_non_trailer(const char *buf, size_t len)
+ strvec_push(&opt.args, arg);
va_end(args);
- strvec_clear(&hook_env);
-+
-+ ret = run_hooks(name, &opt);
-+ run_hooks_opt_clear(&opt);
- return ret;
+- return ret;
++ return run_hooks_oneshot(name, &opt);
}
-
- ## sequencer.c ##
-@@ sequencer.c: static int try_to_commit(struct repository *r,
- }
- }
-
-- if (find_hook("prepare-commit-msg")) {
-+ if (hook_exists("prepare-commit-msg")) {
- res = run_prepare_commit_msg_hook(r, msg, hook_commit);
- if (res)
- goto out;
17: 6ed61071c5e ! 18: 3d3a33e2674 read-cache: convert post-index-change hook to use config
@@ Metadata
Author: Emily Shaffer <[email protected]>
## Commit message ##
- read-cache: convert post-index-change hook to use config
+ read-cache: convert post-index-change to use hook.h
- By using hook.h instead of run-command.h to run, post-index-change hooks
- can now be specified in the config in addition to the hookdir.
- post-index-change is not run anywhere besides in read-cache.c.
+ Move the post-index-change hook away from run-command.h to and over to
+ the new hook.h library.
This removes the last direct user of run_hook_ve(), so we can make the function static now. It'll be removed entirely soon.
@@ read-cache.c: static int do_write_locked_index(struct index_state *istate, struc
+ istate->updated_workdir ? "1" : "0",
+ istate->updated_skipworktree ? "1" : "0",
+ NULL);
-+ run_hooks("post-index-change", &hook_opt);
-+ run_hooks_opt_clear(&hook_opt);
++ run_hooks_oneshot("post-index-change", &hook_opt);
+
istate->updated_workdir = 0;
istate->updated_skipworktree = 0;
18: e4ef3f4548a ! 19: 893f8666301 receive-pack: convert push-to-checkout hook to hook.h
@@ Metadata
## Commit message ##
receive-pack: convert push-to-checkout hook to hook.h
- By using hook.h instead of run-command.h to invoke push-to-checkout,
- hooks can now be specified in the config as well as in the hookdir.
- push-to-checkout is not called anywhere but in builtin/receive-pack.c.
+ Move the push-to-checkout hook away from run-command.h to and over to
+ the new hook.h library.
@@ builtin/receive-pack.c: static const char *push_to_checkout(unsigned char *hash,
- hash_to_hex(hash), NULL))
+ strvec_pushv(&opt.env, env->v);
+ strvec_push(&opt.args, hash_to_hex(hash));
-+ if (run_hooks(push_to_checkout_hook, &opt)) {
-+ run_hooks_opt_clear(&opt);
++ if (run_hooks_oneshot(push_to_checkout_hook, &opt))
return "push-to-checkout hook declined";
-- else
-+ } else {
-+ run_hooks_opt_clear(&opt);
- return NULL;
-+ }
- }
-
- static const char *update_worktree(unsigned char *sha1, const struct worktree *worktree) -@@ builtin/receive-pack.c: static const char *update_worktree(unsigned char *sha1, const struct worktree *w -
- strvec_pushf(&env, "GIT_DIR=%s", absolute_path(git_dir));
-
-- if (!find_hook(push_to_checkout_hook))
-+ if (!hook_exists(push_to_checkout_hook))
- retval = push_to_deploy(sha1, &env, work_tree);
else
- retval = push_to_checkout(sha1, &env, work_tree);
+ return NULL;
19: e3dda367ec9 = 20: 070433deba5 run-command: remove old run_hook_{le,ve}() hook API
20: 477d75bf579 = 21: 1028e0c1667 run-command: allow stdin for run_processes_parallel
21: b7c0ee9719a ! 22: 639e59e9ed0 hook: support passing stdin to hooks @@ Documentation/git-hook.txt: what those are.
## builtin/hook.c ##
@@
+ #include "strvec.h"
- static const char * const builtin_hook_usage[] = {
- N_("git hook <command> [...]"),
-- N_("git hook run <hook-name> [-- <hook-args>]"),
-+ N_("git hook run [<args>] <hook-name> [-- <hook-args>]"),
- NULL
- };
-
- static const char * const builtin_hook_run_usage[] = {
- N_("git hook run <hook-name> [-- <hook-args>]"),
-+ N_("git hook run [--to-stdin=<path>] <hook-name> [-- <hook-args>]"),
- NULL
- };
+ #define BUILTIN_HOOK_RUN_USAGE \
+- N_("git hook run [--ignore-missing] <hook-name> [-- <hook-args>]")
++ N_("git hook run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]")
+ static const char * const builtin_hook_usage[] = {
+ BUILTIN_HOOK_RUN_USAGE,
@@ builtin/hook.c: static int run(int argc, const char **argv, const char *prefix)
struct option run_options[] = {
OPT_BOOL(0, "ignore-missing", &ignore_missing,
@@ builtin/hook.c: static int run(int argc, const char **argv, const char *prefix)
+ N_("file to read into hooks' stdin")),
OPT_END(),
};
-
+ int ret;
## hook.c ##
@@ hook.c: static int pick_next_hook(struct child_process *cp,
- struct hook_cb_data *hook_cb = pp_cb;
- struct hook *run_me = hook_cb->run_me;
+ if (!run_me)
+ return 0;
- cp->no_stdin = 1;
-+
+ /* reopen the file for stdin; run_command closes it. */
+ if (hook_cb->options->path_to_stdin) {
+ cp->no_stdin = 0;
@@ hook.c: static int pick_next_hook(struct child_process *cp,
## hook.h ##
@@ hook.h: struct run_hooks_opt
+
/* Path to initial working directory for subprocess */ const char *dir;
-
++
+ /* Path to file which should be piped to stdin for each hook */
+ const char *path_to_stdin;
};
22: 4035069a98c ! 23: 7d1925cca48 am: convert 'post-rewrite' hook to hook.h
@@ builtin/am.c: static int run_applypatch_msg_hook(struct am_state *state)
{
- struct child_process cp = CHILD_PROCESS_INIT;
- const char *hook = find_hook("post-rewrite");
-+ struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
- int ret;
-
+- int ret;
+-
- if (!hook)
- return 0;
-
@@ builtin/am.c: static int run_applypatch_msg_hook(struct am_state *state)
- cp.in = xopen(am_path(state, "rewritten"), O_RDONLY);
- cp.stdout_to_stderr = 1;
- cp.trace2_hook_name = "post-rewrite";
-+ strvec_push(&opt.args, "rebase");
-+ opt.path_to_stdin = am_path(state, "rewritten");
++ struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
- ret = run_command(&cp);
-+ ret = run_hooks("post-rewrite", &opt);
++ strvec_push(&opt.args, "rebase");
++ opt.path_to_stdin = am_path(state, "rewritten");
- close(cp.in);
-+ run_hooks_opt_clear(&opt);
- return ret;
+- return ret;
++ return run_hooks_oneshot("post-rewrite", &opt);
}
+ /**23: c847a19581a ! 24: 0c24221b522 run-command: add stdin callback for parallelization
@@ builtin/submodule--helper.c: static int update_submodules(struct submodule_updat
## hook.c ##
-@@ hook.c: int run_found_hooks(const char *hook_name, const char *hook_path,
- run_processes_parallel_tr2(options->jobs,
+@@ hook.c: int run_hooks(const char *hook_name, const char *hook_path,
+ run_processes_parallel_tr2(jobs,
pick_next_hook, notify_start_failure,
+ NULL,
@@ run-command.h: typedef int (*task_finished_fn)(int result,
+ feed_pipe_fn, task_finished_fn, void *pp_cb,
const char *tr2_category, const char *tr2_label);
- #endif
+ /**
## submodule.c ##
@@ submodule.c: int fetch_populated_submodules(struct repository *r,
24: da46c859c1c ! 25: 05d1085f7eb hook: provide stdin by string_list or callback
@@ hook.c: static int pick_next_hook(struct child_process *cp,
} else {
cp->no_stdin = 1;
}
-@@ hook.c: int run_found_hooks(const char *hook_name, const char *hook_path,
- run_processes_parallel_tr2(options->jobs,
+@@ hook.c: int run_hooks(const char *hook_name, const char *hook_path,
+ run_processes_parallel_tr2(jobs,
pick_next_hook,
notify_start_failure,
- NULL,
@@ hook.c: int run_found_hooks(const char *hook_name, const char *hook_path,
notify_hook_finished,
&cb_data,
"hook",
-@@ hook.c: int run_hooks(const char *hook_name, struct run_hooks_opt *options) +@@ hook.c: int run_hooks(const char *hook_name, const char *hook_path, +
+ if (options->absolute_path)
+ strbuf_release(&abs_path);
++ free(my_hook.feed_pipe_cb_data);
+
+ return cb_data.rc;
+ }
+@@ hook.c: int run_hooks_oneshot(const char *hook_name, struct run_hooks_opt *options)
if (!options)
- BUG("a struct run_hooks_opt must be provided to run_hooks");
+ options = &hook_opt_scratch;
+ if (options->path_to_stdin && options->feed_pipe)
+ BUG("choose only one method to populate stdin");
+
hook_path = find_hook(hook_name);
-
- /*
+ if (!hook_path) {
+ ret = 0;
## hook.h ##
@@ hook.h: int hook_exists(const char *hookname);
@@ hook.h: struct run_hooks_opt
+ */
+int pipe_from_string_list(struct strbuf *pipe, void *pp_cb, void *pp_task_cb); +
- struct hook_cb_data {
- /* rc reflects the cumulative failure state */
- int rc;
+ /*
+ * Callback provided to feed_pipe_fn and consume_sideband_fn.
+ */
25: 7343be28ef4 ! 26: 4b7175af2e5 hook: convert 'post-rewrite' hook in sequencer.c to hook.h
@@ sequencer.c: int update_head_with_reflog(const struct commit *old_head,
+ opt.feed_pipe = pipe_from_string_list;
+ opt.feed_pipe_ctx = &to_stdin;
+
-+ code = run_hooks("post-rewrite", &opt);
++ code = run_hooks_oneshot("post-rewrite", &opt);
+
-+ run_hooks_opt_clear(&opt);
+ strbuf_release(&tmp);
+ string_list_clear(&to_stdin, 0);
+ return code;
@@ sequencer.c: static int pick_commits(struct repository *r,
+
+ hook_opt.path_to_stdin = rebase_path_rewritten_list();
+ strvec_push(&hook_opt.args, "rebase");
-+ run_hooks("post-rewrite", &hook_opt);
-+ run_hooks_opt_clear(&hook_opt);
++ run_hooks_oneshot("post-rewrite", &hook_opt);
}
apply_autostash(rebase_path_autostash());26: 85bf13a0835 ! 27: 3f24e056410 transport: convert pre-push hook to use config
@@ Metadata
Author: Emily Shaffer <[email protected]>
## Commit message ##
- transport: convert pre-push hook to use config
+ transport: convert pre-push hook to hook.h
- By using the hook.h:run_hooks API, pre-push hooks can be specified in
- the config as well as in the hookdir.
+ Move the pre-push hook away from run-command.h to and over to the new
+ hook.h library.
Signed-off-by: Emily Shaffer <[email protected]>
Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]>
@@ transport.c: static void die_with_unpushed_submodules(struct string_list *needs_
- int ret = 0, x;
+ int ret = 0;
+ struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
-+ struct strbuf tmp = STRBUF_INIT;
struct ref *r;
- struct child_process proc = CHILD_PROCESS_INIT;
- struct strbuf buf;
@@ transport.c: static void die_with_unpushed_submodules(struct string_list *needs_
- finish_command(&proc);
- return -1;
- }
--
-- sigchain_push(SIGPIPE, SIG_IGN);
-+ struct string_list to_stdin = STRING_LIST_INIT_DUP;
++ struct string_list to_stdin = STRING_LIST_INIT_NODUP;
+- sigchain_push(SIGPIPE, SIG_IGN);
+-
- strbuf_init(&buf, 256);
+ strvec_push(&opt.args, transport->remote->name);
+ strvec_push(&opt.args, transport->url);
for (r = remote_refs; r; r = r->next) {
++ struct strbuf buf = STRBUF_INIT;
++
if (!r->peer_ref) continue;
-@@ transport.c: static int run_pre_push_hook(struct transport *transport,
+ if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
+ if (r->status == REF_STATUS_REJECT_STALE) continue;
if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
if (r->status == REF_STATUS_UPTODATE) continue;
- strbuf_reset(&buf);
- strbuf_addf( &buf, "%s %s %s %s\n",
-+ strbuf_reset(&tmp);
-+ strbuf_addf(&tmp, "%s %s %s %s",
++ strbuf_addf(&buf, "%s %s %s %s",
r->peer_ref->name, oid_to_hex(&r->new_oid),
r->name, oid_to_hex(&r->old_oid));
-
@@ transport.c: static int run_pre_push_hook(struct transport *transport,
- ret = -1;
- break;
- }
-+ string_list_append(&to_stdin, tmp.buf);
++ string_list_append(&to_stdin, strbuf_detach(&buf, NULL));
}
- strbuf_release(&buf);
@@ transport.c: static int run_pre_push_hook(struct transport *transport,
- x = finish_command(&proc);
- if (!ret)
- ret = x;
-+ ret = run_hooks("pre-push", &opt);
-+ run_hooks_opt_clear(&opt);
-+ strbuf_release(&tmp);
++ ret = run_hooks_oneshot("pre-push", &opt);
++ to_stdin.strdup_strings = 1;
+ string_list_clear(&to_stdin, 0);
return ret;
-: ----------- > 28: ecf75f33233 hook tests: test for exact "pre-push" hook input
-: ----------- > 29: 2c961be94b4 hook tests: use a modern style for "pre-push" tests
27: 331014bad17 ! 30: 1ce456f9d9d reference-transaction: use hook.h to run hooks
@@ refs.c: int ref_update_reject_duplicates(struct string_list *refnames,
const char *state)
{
- struct child_process proc = CHILD_PROCESS_INIT;
- struct strbuf buf = STRBUF_INIT;
+- struct strbuf buf = STRBUF_INIT;
- const char *hook;
+ struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
-+ struct string_list to_stdin = STRING_LIST_INIT_DUP;
++ struct string_list to_stdin = STRING_LIST_INIT_NODUP;
int ret = 0, i;
-+ char o[GIT_MAX_HEXSZ + 1], n[GIT_MAX_HEXSZ + 1];
- hook = find_hook("reference-transaction");
- if (!hook)
-+ if (!hook_exists("reference-transaction"))
- return ret;
-
+- return ret;
+-
- strvec_pushl(&proc.args, hook, state, NULL);
- proc.in = -1;
- proc.stdout_to_stderr = 1;
@@ refs.c: int ref_update_reject_duplicates(struct string_list *refnames,
-
- ret = start_command(&proc);
- if (ret)
-- return ret;
--
++ if (!hook_exists("reference-transaction"))
+ return ret;
+
- sigchain_push(SIGPIPE, SIG_IGN);
+ strvec_push(&opt.args, state);
for (i = 0; i < transaction->nr; i++) {
struct ref_update *update = transaction->updates[i];
-+ oid_to_hex_r(o, &update->old_oid);
-+ oid_to_hex_r(n, &update->new_oid);
++ struct strbuf buf = STRBUF_INIT;
- strbuf_reset(&buf);
+- strbuf_reset(&buf);
- strbuf_addf(&buf, "%s %s %s\n",
-- oid_to_hex(&update->old_oid),
-- oid_to_hex(&update->new_oid),
-- update->refname);
++ strbuf_addf(&buf, "%s %s %s",
+ oid_to_hex(&update->old_oid),
+ oid_to_hex(&update->new_oid),
+ update->refname);
-
- if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
- if (errno != EPIPE)
- ret = -1;
- break;
- }
-+ strbuf_addf(&buf, "%s %s %s", o, n, update->refname);
-+ string_list_append(&to_stdin, buf.buf);
++ string_list_append(&to_stdin, strbuf_detach(&buf, NULL));
}
- close(proc.in);
- sigchain_pop(SIGPIPE);
+- strbuf_release(&buf);
+ opt.feed_pipe = pipe_from_string_list;
+ opt.feed_pipe_ctx = &to_stdin;
+
-+ ret = run_hooks("reference-transaction", &opt);
-+ run_hooks_opt_clear(&opt);
- strbuf_release(&buf);
++ ret = run_hooks_oneshot("reference-transaction", &opt);
++ to_stdin.strdup_strings = 1;
+ string_list_clear(&to_stdin, 0);
- ret |= finish_command(&proc);28: f7f56d0a3bb ! 31: 6e5f1f5bd3a run-command: allow capturing of collated output
@@ builtin/submodule--helper.c: static int update_submodules(struct submodule_updat
## hook.c ##
-@@ hook.c: int run_found_hooks(const char *hook_name, const char *hook_path, +@@ hook.c: int run_hooks(const char *hook_name, const char *hook_path,
pick_next_hook, notify_start_failure, options->feed_pipe,
@@ run-command.h: int run_processes_parallel(int n,
+ task_finished_fn, void *pp_cb,
const char *tr2_category, const char *tr2_label);
- #endif
+ /**
## submodule.c ##
@@ submodule.c: int fetch_populated_submodules(struct repository *r,
29: 7f7fcc06885 ! 32: 0b6e9c6d07a hooks: allow callers to capture output
@@ Commit message
## hook.c ##
-@@ hook.c: int run_found_hooks(const char *hook_name, const char *hook_path, +@@ hook.c: int run_hooks(const char *hook_name, const char *hook_path,
pick_next_hook, notify_start_failure, options->feed_pipe,
@@ hook.c: int run_found_hooks(const char *hook_name, const char *hook_path,
notify_hook_finished,
&cb_data,
"hook",
+ hook_name);
+
++
+ if (options->absolute_path)
+ strbuf_release(&abs_path);
+ free(my_hook.feed_pipe_cb_data);
## hook.h ##
@@ hook.h: struct run_hooks_opt
30: e74d49e5593 ! 33: dcf63634338 receive-pack: convert 'update' hook to hook.h
@@ builtin/receive-pack.c: static int run_receive_hook(struct command *commands,
+static int run_update_hook(struct command *cmd)
+{
+ struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
-+ int code;
+
+ strvec_pushl(&opt.args,
+ cmd->ref_name,
@@ builtin/receive-pack.c: static int run_receive_hook(struct command *commands,
- return finish_command(&proc);
+ opt.consume_sideband = hook_output_to_sideband;
+
-+ code = run_hooks("update", &opt);
-+ run_hooks_opt_clear(&opt);
-+ return code;
++ return run_hooks_oneshot("update", &opt);
}
static struct command *find_command_by_refname(struct command *list,
31: 0bdc4878ac8 ! 34: f352a485e59 post-update: use hook.h library
@@ builtin/receive-pack.c: static const char *update(struct command *cmd, struct sh
- copy_to_sideband(proc.err, -1, NULL);
- finish_command(&proc);
- }
-+ run_hooks("post-update", &opt);
-+ run_hooks_opt_clear(&opt);
++ run_hooks_oneshot("post-update", &opt);
}
static void check_aliased_update_internal(struct command *cmd,
32: db70b59b3bd ! 35: ceef2f3e804 receive-pack: convert receive hooks to hook.h
@@ builtin/receive-pack.c: static void hook_output_to_sideband(struct strbuf *outpu
+{
+ struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
+ struct receive_hook_feed_context ctx;
-+ int rc;
+ struct command *iter = commands;
+
+ /* if there are no valid commands, don't invoke the hook at all. */
@@ builtin/receive-pack.c: static void hook_output_to_sideband(struct strbuf *outpu
+ if (!iter)
+ return 0;
+
-+ /* pre-receive hooks should run in series as the hook updates refs */
-+ if (!strcmp(hook_name, "pre-receive"))
-+ opt.jobs = 1;
-+
+ if (push_options) {
+ int i;
+ for (i = 0; i < push_options->nr; i++)
@@ builtin/receive-pack.c: static void hook_output_to_sideband(struct strbuf *outpu
+ opt.feed_pipe = feed_receive_hook_cb;
+ opt.feed_pipe_ctx = &ctx;
+
-+ rc = run_hooks(hook_name, &opt);
-+ run_hooks_opt_clear(&opt);
-+ return rc;
++ return run_hooks_oneshot(hook_name, &opt);
+}
+
static int run_update_hook(struct command *cmd)33: d86fedf041c ! 36: b71d7628b40 hooks: fix a TOCTOU in "did we run a hook?" heuristic
@@ hook.c: static int notify_hook_finished(int result,
+ if (hook_cb->invoked_hook)
+ *hook_cb->invoked_hook = 1;
+
- return 1;
+ return 0;
}
-@@ hook.c: int run_found_hooks(const char *hook_name, const char *hook_path, +@@ hook.c: int run_hooks(const char *hook_name, const char *hook_path,
.rc = 0,
.hook_name = hook_name,
.options = options,
+ .invoked_hook = options->invoked_hook,
};
- if (options->absolute_path) {
- strbuf_add_absolute_path(&abs_path, hook_path);
+ int jobs = 1;
+
## hook.h ##
@@ hook.h: struct run_hooks_opt
Signature · 1 line
2.33.0.rc0.595.ge31e012651d
PATCH v4 1/36 Makefile: mark "check" target as .PHONY
Fix a bug in 44c9e8594e (Fix up header file dependencies and add sparse checking rules, 2005-07-03), we never marked the phony "check" target as such.
Perhaps we should just remove it, since as of a combination of 912f9980d2 (Makefile: help people who run 'make check' by mistake, 2008-11-11) 0bcd9ae85d (sparse: Fix errors due to missing target-specific variables, 2011-04-21) we've been suggesting the user run "make sparse" directly.
But under that mode it still does something, as well as directing the user to run "make test" under non-sparse. So let's punt that and narrowly fix the PHONY bug.
--- Makefile | 1 + 1 file changed, 1 insertion(+)
Makefile +1 -0
@@ -2931,6 +2931,7 @@ hdr-check: $(HCO) style: git clang-format --style file --diff --extensions c,h +.PHONY: check check: config-list.h command-list.h @if sparse; \ then \
Signature · 1 line
2.33.0.rc0.595.ge31e012651d
Re: [PATCH v4 01/36] Makefile: mark "check" target as .PHONY
Quoted 4 lines · Ævar Arnfjörð Bjarmason
Fix a bug in
44c9e8594e(Fix up header file dependencies and add sparse checking rules, 2005-07-03), we never marked the phony "check" target as such.
A simple enough change, and it gave me an excuse to go and look up what .PHONY actually does. Thanks ;)
Quoted 7 lines
Perhaps we should just remove it, since as of a combination of
912f9980d2(Makefile: help people who run 'make check' by mistake, 2008-11-11)0bcd9ae85d(sparse: Fix errors due to missing target-specific variables, 2011-04-21) we've been suggesting the user run "make sparse" directly.
Quoted 5 lines
But under that mode it still does something, as well as directing the user to run "make test" under non-sparse. So let's punt that and narrowly fix the PHONY bug.
Signed-off-by Ævar Arnfjörð Bjarmason <[email protected]>
Quoted 19 lines
--- Makefile | 1 + 1 file changed, 1 insertion(+)Makefile +1
-0@@ -2931,6 +2931,7 @@ hdr-check: $(HCO) style: git clang-format --style file --diff --extensions c,h +.PHONY: check check: config-list.h command-list.h @if sparse; \ then \Signature · 1 line
2.33.0.rc0.595.ge31e012651d
PATCH v4 2/36 Makefile: stop hardcoding {command,config}-list.h
In reply to Ævar Arnfjörð BjarmasonChange various places that hardcode the names of these two files to refer to either $(GENERATED_H), or to a new generated-hdrs target. That target is consistent with the *-objs targets I recently added in 029bac01a8 (Makefile: add {program,xdiff,test,git,fuzz}-objs & objects targets, 2021-02-23).
A subsequent commit will add a new generated hook-list.h. By doing this refactoring we'll only need to add the new file to the GENERATED_H variable, not EXCEPT_HDRS, the vcbuild/README etc.
I have not tested the Windows-specific change in config.mak.uname being made here, but we use other variables from the Makefile in the same block, and the GENERATED_H is fully defined before we include config.mak.uname.
Hardcoding command-list.h there seems to have been a case of copy/paste programming in 976aaedca0 (msvc: add a Makefile target to pre-generate the Visual Studio solution, 2019-07-29). The config-list.h was added later in 709df95b78 (help: move list_config_help to builtin/help, 2020-04-16).
--- Makefile | 6 ++++-- compat/vcbuild/README | 2 +- config.mak.uname | 6 +++--- 3 files changed, 8 insertions(+), 6 deletions(-)
Makefile +4 -2
@@ -823,6 +823,8 @@ XDIFF_LIB = xdiff/lib.a GENERATED_H += command-list.h GENERATED_H += config-list.h +.PHONY: generated-hdrs +generated-hdrs: $(GENERATED_H) LIB_H := $(sort $(patsubst ./%,%,$(shell git ls-files '*.h' ':!t/' ':!Documentation/' 2>/dev/null || \ $(FIND) . \
@@ -2909,7 +2911,7 @@ $(SP_OBJ): %.sp: %.c GIT-CFLAGS FORCE .PHONY: sparse $(SP_OBJ) sparse: $(SP_OBJ) -EXCEPT_HDRS := command-list.h config-list.h unicode-width.h compat/% xdiff/% +EXCEPT_HDRS := $(GENERATED_H) unicode-width.h compat/% xdiff/% ifndef GCRYPT_SHA256 EXCEPT_HDRS += sha256/gcrypt.h endif
@@ -2932,7 +2934,7 @@ style: git clang-format --style file --diff --extensions c,h .PHONY: check -check: config-list.h command-list.h +check: $(GENERATED_H) @if sparse; \ then \ echo >&2 "Use 'make sparse' instead"; \
compat/vcbuild/README +1 -1
@@ -92,7 +92,7 @@ The Steps of Build Git with VS2008 the git operations. 3. Inside Git's directory run the command: - make command-list.h config-list.h + make generated-hdrs to generate the header file needed to compile git. 4. Then either build Git with the GNU Make Makefile in the Git projects
config.mak.uname +3 -3
@@ -732,9 +732,9 @@ vcxproj: echo '</Project>') >git-remote-http/LinkOrCopyRemoteHttp.targets git add -f git/LinkOrCopyBuiltins.targets git-remote-http/LinkOrCopyRemoteHttp.targets - # Add command-list.h and config-list.h - $(MAKE) MSVC=1 SKIP_VCPKG=1 prefix=/mingw64 config-list.h command-list.h - git add -f config-list.h command-list.h + # Add generated headers + $(MAKE) MSVC=1 SKIP_VCPKG=1 prefix=/mingw64 $(GENERATED_H) + git add -f $(GENERATED_H) # Add scripts rm -f perl/perl.mak
Signature · 1 line
2.33.0.rc0.595.ge31e012651d
Re: [PATCH v4 02/36] Makefile: stop hardcoding {command,config}-list.h
Quoted 7 lines · Ævar Arnfjörð Bjarmason
Change various places that hardcode the names of these two files to refer to either $(GENERATED_H), or to a new generated-hdrs target. That target is consistent with the *-objs targets I recently added in
029bac01a8(Makefile: add {program,xdiff,test,git,fuzz}-objs & objects targets, 2021-02-23).
Quoted 8 lines
A subsequent commit will add a new generated
hook-list.h. By doing this refactoring we'll only need to add the new file to theGENERATED_Hvariable, notEXCEPT_HDRS, the vcbuild/README etc.I have not tested the Windows-specific change in config.mak.uname being made here, but we use other variables from the
Makefilein the same block, and theGENERATED_His fully defined before we include config.mak.uname.
Is it not something you can get coverage for via, for example, the GitHub Actions CI suite? I wonder if that means we want some test to check that these generated lists came together correctly?
Otherwise the diff looks straightforward.
Quoted 8 lines
Hardcoding
command-list.hthere seems to have been a case of copy/paste programming in976aaedca0(msvc: add aMakefiletarget to pre-generate the Visual Studio solution, 2019-07-29). Theconfig-list.hwas added later in709df95b78(help: movelist_config_helpto builtin/help, 2020-04-16).Signed-off-by Ævar Arnfjörð Bjarmason <[email protected]>
Re: [PATCH v4 02/36] Makefile: stop hardcoding {command,config}-list.h
Quoted 12 lines · Emily Shaffer
Quoted 11 lines · Ævar Arnfjörð Bjarmason
Change various places that hardcode the names of these two files to refer to either $(GENERATED_H), or to a new generated-hdrs target. That target is consistent with the *-objs targets I recently added in
029bac01a8(Makefile: add {program,xdiff,test,git,fuzz}-objs & objects targets, 2021-02-23).A subsequent commit will add a new generated
hook-list.h. By doing this refactoring we'll only need to add the new file to theGENERATED_Hvariable, notEXCEPT_HDRS, the vcbuild/README etc.
Quoted 8 lines
Quoted 4 lines
I have not tested the Windows-specific change in config.mak.uname being made here, but we use other variables from the
Makefilein the same block, and theGENERATED_His fully defined before we include config.mak.uname.Is it not something you can get coverage for via, for example, the GitHub Actions CI suite? I wonder if that means we want some test to check that these generated lists came together correctly?
I'll amend this. Yes it's being tested via the CI, and it's the CI that failed before. I should have said "I have not tested this directly", i.e. I don't have a local Windows machine, but pushing it to the CI build that uses this works...
Quoted 10 lines
Otherwise the diff looks straightforward.
Quoted 8 lines
Hardcoding
command-list.hthere seems to have been a case of copy/paste programming in976aaedca0(msvc: add aMakefiletarget to pre-generate the Visual Studio solution, 2019-07-29). Theconfig-list.hwas added later in709df95b78(help: movelist_config_helpto builtin/help, 2020-04-16).Signed-off-by Ævar Arnfjörð Bjarmason <[email protected]>Reviewed-by Emily Shaffer <[email protected]>
PATCH v4 3/36 Makefile: remove an out-of-date comment
In reply to Ævar Arnfjörð BjarmasonThis comment added in dfea575017 (Makefile: lazily compute header dependencies, 2010-01-26) has been out of date since 92b88eba9f (Makefile: use git ls-files to list header files, if possible, 2019-03-04), when we did exactly what it tells us not to do and added $(GENERATED_H) to $(OBJECTS) dependencies.
The rest of it was also somewhere between inaccurate and outdated, since as of b8ba629264 (Makefile: fold MISC_H into LIB_H, 2012-06-20) it's not followed by a list of header files, that got moved earlier in the file into LIB_H in 60d24dd255 (Makefile: fold XDIFF_H and VCSSVN_H into LIB_H, 2012-07-06).
Let's just remove it entirely, to the extent that we have anything useful to say here the comment on the "USE_COMPUTED_HEADER_DEPENDENCIES" variable a few lines above this change does the job for us.
--- Makefile | 7 ------- 1 file changed, 7 deletions(-)
Makefile +0 -7
@@ -2519,13 +2519,6 @@ ifneq ($(dep_files_present),) include $(dep_files_present) endif else -# Dependencies on header files, for platforms that do not support -# the gcc -MMD option. -# -# Dependencies on automatically generated headers such as command-list.h -# should _not_ be included here, since they are necessary even when -# building an object for the first time. - $(OBJECTS): $(LIB_H) $(GENERATED_H) endif
Signature · 1 line
2.33.0.rc0.595.ge31e012651d
Re: [PATCH v4 03/36] Makefile: remove an out-of-date comment
Quoted 13 lines · Ævar Arnfjörð Bjarmason
This comment added in
dfea575017(Makefile: lazily compute header dependencies, 2010-01-26) has been out of date since92b88eba9f(Makefile: usegit ls-filesto list header files, if possible, 2019-03-04), when we did exactly what it tells us not to do and added $(GENERATED_H) to $(OBJECTS) dependencies.The rest of it was also somewhere between inaccurate and outdated, since as of
b8ba629264(Makefile: foldMISC_HintoLIB_H, 2012-06-20) it's not followed by a list of header files, that got moved earlier in the file intoLIB_Hin60d24dd255(Makefile: foldXDIFF_HandVCSSVN_HintoLIB_H, 2012-07-06).
Quoted 6 lines
Let's just remove it entirely, to the extent that we have anything useful to say here the comment on the "
USE_COMPUTED_HEADER_DEPENDENCIES" variable a few lines above this change does the job for us.Signed-off-by Ævar Arnfjörð Bjarmason <[email protected]>
Quoted 25 lines
--- Makefile | 7 ------- 1 file changed, 7 deletions(-)Makefile +0
-7@@ -2519,13 +2519,6 @@ ifneq ($(dep_files_present),) include $(dep_files_present) endif else -# Dependencies on header files, for platforms that do not support -# the gcc -MMD option. -# -# Dependencies on automatically generated headers such as command-list.h -# should _not_ be included here, since they are necessary even when -# building an object for the first time. - $(OBJECTS): $(LIB_H) $(GENERATED_H) endifSignature · 1 line
2.33.0.rc0.595.ge31e012651d
PATCH v4 4/36 hook.[ch]: move find_hook() to this new library
In reply to Ævar Arnfjörð BjarmasonMove the find_hook() function from run-command.c to a new hook.c library. This change establishes a stub library that's pretty pointless right now, but will see much wider use with Emily Shaffer's upcoming "configuration-based hooks" series.
Eventually all the hook related code will live in hook.[ch]. Let's start that process by moving the simple find_hook() function over as-is.
--- Makefile | 1 + builtin/am.c | 1 + builtin/bugreport.c | 2 +- builtin/commit.c | 1 + builtin/merge.c | 1 + builtin/receive-pack.c | 1 + builtin/worktree.c | 1 + hook.c | 37 +++++++++++++++++++++++++++++++++++++ hook.h | 11 +++++++++++ refs.c | 1 + run-command.c | 35 +---------------------------------- run-command.h | 7 ------- sequencer.c | 1 + transport.c | 1 + 14 files changed, 59 insertions(+), 42 deletions(-) create mode 100644 hook.c create mode 100644 hook.h
Makefile +1 -0
@@ -910,6 +910,7 @@ LIB_OBJS += hash-lookup.o LIB_OBJS += hashmap.o LIB_OBJS += help.o LIB_OBJS += hex.o +LIB_OBJS += hook.o LIB_OBJS += ident.o LIB_OBJS += json-writer.o LIB_OBJS += kwset.o
builtin/am.c +1 -0
@@ -11,6 +11,7 @@#include "parse-options.h" #include "dir.h" #include "run-command.h" +#include "hook.h" #include "quote.h" #include "tempfile.h" #include "lockfile.h"
builtin/bugreport.c +1 -1
@@ -3,7 +3,7 @@#include "strbuf.h" #include "help.h" #include "compat/compiler.h" -#include "run-command.h" +#include "hook.h" static void get_system_info (struct strbuf *sys_info)
builtin/commit.c +1 -0
@@ -19,6 +19,7 @@#include "revision.h" #include "wt-status.h" #include "run-command.h" +#include "hook.h" #include "refs.h" #include "log-tree.h" #include "strbuf.h"
builtin/merge.c +1 -0
@@ -13,6 +13,7 @@#include "builtin.h" #include "lockfile.h" #include "run-command.h" +#include "hook.h" #include "diff.h" #include "diff-merges.h" #include "refs.h"
builtin/receive-pack.c +1 -0
@@ -7,6 +7,7 @@#include "pkt-line.h" #include "sideband.h" #include "run-command.h" +#include "hook.h" #include "exec-cmd.h" #include "commit.h" #include "object.h"
builtin/worktree.c +1 -0
@@ -8,6 +8,7 @@#include "branch.h" #include "refs.h" #include "run-command.h" +#include "hook.h" #include "sigchain.h" #include "submodule.h" #include "utf8.h"
hook.c new file +37 -0
@@ -0,0 +1,37 @@ +#include "cache.h" +#include "hook.h" +#include "run-command.h" + +const char *find_hook (const char *name) +{ +static struct strbuf path =STRBUF_INIT ; + +strbuf_reset (&path); +strbuf_git_path (&path,"hooks/%s" , name); +if (access (path.buf,X_OK ) <0 ) { +int err = errno; + +#ifdef STRIP_EXTENSION +strbuf_addstr (&path,STRIP_EXTENSION ); +if (access (path.buf,X_OK ) >=0 ) +return path.buf; +if (errno ==EACCES ) + err = errno; +#endif + +if (err ==EACCES && advice_ignored_hook) { +static struct string_list advise_given =STRING_LIST_INIT_DUP ; + +if (!string_list_lookup (&advise_given, name)) { +string_list_insert (&advise_given, name); +advise (_ ("The '%s' hook was ignored because " +"it's not set as executable.\n" +"You can disable this warning with " +"`git config advice.ignoredHook false`." ), + path.buf); + } + } +return NULL ; + } +return path.buf; +}
hook.h new file +11 -0
@@ -0,0 +1,11 @@ +#ifndef HOOK_H +#define HOOK_H + +/* +* Returns the path to the hook file, or NULL if the hook is missing +* or disabled. Note that this points to static storage that will be +* overwritten by further calls to find_hook and run_hook_*. +*/ +const char *find_hook (const char *name); + +#endif
refs.c +1 -0
@@ -10,6 +10,7 @@#include "refs.h" #include "refs/refs-internal.h" #include "run-command.h" +#include "hook.h" #include "object-store.h" #include "object.h" #include "tag.h"
run-command.c +1 -34
@@ -8,6 +8,7 @@#include "string-list.h" #include "quote.h" #include "config.h" +#include "hook.h" void child_process_init (struct child_process *child) {
@@ -1319,40 +1320,6 @@ int async_with_fork(void)#endif } -const char *find_hook (const char *name) -{ -static struct strbuf path =STRBUF_INIT ; - -strbuf_reset (&path); -strbuf_git_path (&path,"hooks/%s" , name); -if (access (path.buf,X_OK ) <0 ) { -int err = errno; - -#ifdef STRIP_EXTENSION -strbuf_addstr (&path,STRIP_EXTENSION ); -if (access (path.buf,X_OK ) >=0 ) -return path.buf; -if (errno ==EACCES ) - err = errno; -#endif - -if (err ==EACCES && advice_ignored_hook) { -static struct string_list advise_given =STRING_LIST_INIT_DUP ; - -if (!string_list_lookup (&advise_given, name)) { -string_list_insert (&advise_given, name); -advise (_ ("The '%s' hook was ignored because " -"it's not set as executable.\n" -"You can disable this warning with " -"`git config advice.ignoredHook false`." ), - path.buf); - } - } -return NULL ; - } -return path.buf; -} -int run_hook_ve (const char *const *env,const char *name,va_list args) {struct child_process hook =CHILD_PROCESS_INIT ;
run-command.h +0 -7
@@ -204,13 +204,6 @@ int finish_command_in_signal(struct child_process *);*/ int run_command (struct child_process *); -/* -* Returns the path to the hook file, or NULL if the hook is missing -* or disabled. Note that this points to static storage that will be -* overwritten by further calls to find_hook and run_hook_*. -*/ -const char *find_hook (const char *name); -/** * Run a hook. * The first argument is a pathname to an index file, or NULL
sequencer.c +1 -0
@@ -8,6 +8,7 @@#include "sequencer.h" #include "tag.h" #include "run-command.h" +#include "hook.h" #include "exec-cmd.h" #include "utf8.h" #include "cache-tree.h"
transport.c +1 -0
@@ -2,6 +2,7 @@#include "config.h" #include "transport.h" #include "run-command.h" +#include "hook.h" #include "pkt-line.h" #include "fetch-pack.h" #include "remote.h"
Signature · 1 line
2.33.0.rc0.595.ge31e012651d
Re: [PATCH v4 04/36] hook.[ch]: move find_hook() to this new library
On Tue, Aug 03, 2021 at 09:38:30PM +0200, Ævar Arnfjörð Bjarmason wrote:
The subject is now a little confusing (and I guess it would have been before, too). Can we remove "this new library" and replace it with something that makes sense on its own?
Quoted 6 lines
Move the
find_hook()function fromrun-command.cto a newhook.clibrary. This change establishes a stub library that's pretty pointless right now, but will see much wider use with Emily Shaffer's upcoming "configuration-based hooks" series.
Quoted 6 lines
Eventually all the hook related code will live in hook.[ch]. Let's start that process by moving the simple
find_hook()function over as-is.Signed-off-by Emily Shaffer <[email protected]>Signed-off-by Ævar Arnfjörð Bjarmason <[email protected]>
It doesn't make sense for me to add a Reviewed-by line here, I guess. But it seems to me to be fine to do this early on instead of at the very end.
- Emily
PATCH v4 5/36 hook.c: add a hook_exists() wrapper and use it in bugreport.c
In reply to Ævar Arnfjörð BjarmasonFrom: Emily Shaffer <[email protected]>
Add a boolean version of the find_hook() function for those callers who are only interested in checking whether the hook exists, not what the path to it is.
--- builtin/bugreport.c | 2 +- hook.c | 5 +++++ hook.h | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-)
builtin/bugreport.c +1 -1
@@ -82,7 +82,7 @@ static void get_populated_hooks(struct strbuf *hook_info, int nongit) }for (i =0 ; i <ARRAY_SIZE (hook); i++) -if (find_hook (hook[i])) +if (hook_exists (hook[i]))strbuf_addf (hook_info,"%s\n" , hook[i]); }
hook.c +5 -0
@@ -35,3 +35,8 @@ const char *find_hook(const char *name) }return path.buf; } + +int hook_exists (const char *name) +{ +return !!find_hook (name); +}
hook.h +5 -0
@@ -8,4 +8,9 @@*/ const char *find_hook (const char *name); +/* +* A boolean version of find_hook() +*/ +int hook_exists (const char *hookname); +#endif
Signature · 1 line
2.33.0.rc0.595.ge31e012651d
Re: [PATCH v4 05/36] hook.c: add a hook_exists() wrapper and use it in bugreport.c
Quoted 5 lines · Ævar Arnfjörð Bjarmason
Add a boolean version of the
find_hook()function for those callers who are only interested in checking whether the hook exists, not what the path to it is.
Seems fine.
Quoted 27 lines
Signed-off-by Emily Shaffer <[email protected]>Signed-off-by Ævar Arnfjörð Bjarmason <[email protected]>--- builtin/bugreport.c | 2 +- hook.c | 5 +++++ hook.h | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-)builtin/bugreport.c +1
-1@@ -82,7 +82,7 @@ static void get_populated_hooks(struct strbuf *hook_info, int nongit) }for (i =0 ; i <ARRAY_SIZE (hook); i++) -if (find_hook (hook[i])) +if (hook_exists (hook[i]))strbuf_addf (hook_info,"%s\n" , hook[i]); }hook.c +0
-0
hook.c const char *find_hook(const char *name) · line 41
Quoted 7 lines
hook.c +4
-0@@ -35,3 +35,8 @@ const char *find_hook(const char *name) }return path.buf; } + +int hook_exists (const char *name) +{ +return !!find_hook (name);
Later on I'll change this to refer to the output of list_hooks() instead.
Quoted 18 lines
hook.c +1
-0+}hook.h +5
-0@@ -8,4 +8,9 @@*/ const char *find_hook (const char *name); +/* +* A boolean version of find_hook() +*/ +int hook_exists (const char *hookname); +#endif Signature · 1 line
2.33.0.rc0.595.ge31e012651d
PATCH v4 6/36 hook.c users: use "hook_exists()" insted of "find_hook()"
In reply to Ævar Arnfjörð BjarmasonUse the new hook_exists() function instead of find_hook() where the latter was called in boolean contexts. This make subsequent changes in a series where we further refactor the hook API clearer, as we won't conflate wanting to get the path of the hook with checking for its existence.
--- builtin/commit.c | 2 +- builtin/merge.c | 2 +- builtin/receive-pack.c | 2 +- sequencer.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)
builtin/commit.c +1 -1
@@ -1052,7 +1052,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,return 0 ; } -if (!no_verify &&find_hook ("pre-commit" )) { +if (!no_verify &&hook_exists ("pre-commit" )) {/* * Re-read the index as pre-commit hook could have updated it, * and write it out as a tree. We must do this before we invoke
builtin/merge.c +1 -1
@@ -849,7 +849,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)* and write it out as a tree. We must do this before we invoke * the editor and after we invoke run_status above. */ -if (find_hook ("pre-merge-commit" )) +if (hook_exists ("pre-merge-commit" ))discard_cache ();read_cache_from (index_file);strbuf_addbuf (&msg, &merge_msg);
builtin/receive-pack.c +1 -1
@@ -1464,7 +1464,7 @@ static const char *update_worktree(unsigned char *sha1, const struct worktree *wstrvec_pushf (&env,"GIT_DIR=%s" ,absolute_path (git_dir)); -if (!find_hook (push_to_checkout_hook)) +if (!hook_exists (push_to_checkout_hook)) retval =push_to_deploy (sha1, &env, work_tree);else retval =push_to_checkout (sha1, &env, work_tree);
sequencer.c +1 -1
@@ -1446,7 +1446,7 @@ static int try_to_commit(struct repository *r, } } -if (find_hook ("prepare-commit-msg" )) { +if (hook_exists ("prepare-commit-msg" )) { res =run_prepare_commit_msg_hook (r, msg, hook_commit);if (res) goto out;
Signature · 1 line
2.33.0.rc0.595.ge31e012651d
Re: [PATCH v4 06/36] hook.c users: use "hook_exists()" insted of "find_hook()"
Quoted 7 lines · Ævar Arnfjörð Bjarmason
Use the new
hook_exists()function instead offind_hook()where the latter was called in boolean contexts. This make subsequent changes in a series where we further refactor the hook API clearer, as we won't conflate wanting to get the path of the hook with checking for its existence.
None of these callsites capture the return string from find_hook(), so this looks fine.
Quoted 1 line
Signed-off-by Ævar Arnfjörð Bjarmason <[email protected]>
Quoted 62 lines
--- builtin/commit.c | 2 +- builtin/merge.c | 2 +- builtin/receive-pack.c | 2 +- sequencer.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)builtin/commit.c +1
-1@@ -1052,7 +1052,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,return 0 ; } -if (!no_verify &&find_hook ("pre-commit" )) { +if (!no_verify &&hook_exists ("pre-commit" )) {/* * Re-read the index as pre-commit hook could have updated it, * and write it out as a tree. We must do this before we invoke builtin/merge.c +1
-1@@ -849,7 +849,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)* and write it out as a tree. We must do this before we invoke * the editor and after we invoke run_status above. */ -if (find_hook ("pre-merge-commit" )) +if (hook_exists ("pre-merge-commit" ))discard_cache ();read_cache_from (index_file);strbuf_addbuf (&msg, &merge_msg);builtin/receive-pack.c +1
-1@@ -1464,7 +1464,7 @@ static const char *update_worktree(unsigned char *sha1, const struct worktree *wstrvec_pushf (&env,"GIT_DIR=%s" ,absolute_path (git_dir)); -if (!find_hook (push_to_checkout_hook)) +if (!hook_exists (push_to_checkout_hook)) retval =push_to_deploy (sha1, &env, work_tree);else retval =push_to_checkout (sha1, &env, work_tree);sequencer.c +1
-1@@ -1446,7 +1446,7 @@ static int try_to_commit(struct repository *r, } } -if (find_hook ("prepare-commit-msg" )) { +if (hook_exists ("prepare-commit-msg" )) { res =run_prepare_commit_msg_hook (r, msg, hook_commit);if (res) goto out;Signature · 1 line
2.33.0.rc0.595.ge31e012651d
PATCH v4 7/36 hook-list.h: add a generated list of hooks, like config-list.h
In reply to Ævar Arnfjörð BjarmasonMake githooks(5) the source of truth for what hooks git supports, and die hooks we don't know about in find_hook(). This ensures that the documentation and the C code's idea about existing hooks doesn't diverge.
We still have Perl and Python code running its own hooks, but that'll be addressed by Emily Shaffer's upcoming "git hook run" command.
This resolves a long-standing TODO item in bugreport.c of there being no centralized listing of hooks, and fixes a bug with the bugreport listing only knowing about 1/4 of the p4 hooks. It didn't know about the recent "reference-transaction" hook either.
I have not been able to directly test the CMake change being made here. Since 4c2c38e800 (ci: modification of main.yml to use cmake for vs-build job, 2020-06-26) some of the Windows CI has a hard dependency on CMake, this change works there, and is to my eyes an obviously correct use of a pattern established in previous CMake changes, namely:
- 061c2240b1 (Introduce CMake support for configuring Git,
2020-06-12)
- 709df95b78 (help: move list_config_help to builtin/help,
2020-04-16) - 976aaedca0 (msvc: add a Makefile target to pre-generate the Visual Studio solution, 2019-07-29)
The LC_ALL=C is needed because at least in my locale the dash ("-") is ignored for the purposes of sorting, which results in a different order. I'm not aware of anything in git that has a hard dependency on the order, but e.g. the bugreport output would end up using whatever locale was in effect when git was compiled.
--- .gitignore | 1 + Makefile | 10 ++++++- builtin/bugreport.c | 44 ++++++----------------------- contrib/buildsystems/CMakeLists.txt | 7 +++++ generate-hooklist.sh | 18 ++++++++++++ hook.c | 19 +++++++++++++ 6 files changed, 62 insertions(+), 37 deletions(-) create mode 100755 generate-hooklist.sh
.gitignore +1 -0
@@ -190,6 +190,7 @@ /gitweb/static/gitweb.min.* /config-list.h /command-list.h +/hook-list.h *.tar.gz *.dsc *.deb
Makefile +9 -1
@@ -823,6 +823,8 @@ XDIFF_LIB = xdiff/lib.a GENERATED_H += command-list.h GENERATED_H += config-list.h +GENERATED_H += hook-list.h + .PHONY: generated-hdrs generated-hdrs: $(GENERATED_H)
@@ -2226,7 +2228,9 @@ git$X: git.o GIT-LDFLAGS $(BUILTIN_OBJS) $(GITLIBS) help.sp help.s help.o: command-list.h -builtin/help.sp builtin/help.s builtin/help.o: config-list.h GIT-PREFIX +hook.sp hook.s hook.o: hook-list.h + +builtin/help.sp builtin/help.s builtin/help.o: config-list.h hook-list.h GIT-PREFIX builtin/help.sp builtin/help.s builtin/help.o: EXTRA_CPPFLAGS = \ '-DGIT_HTML_PATH="$(htmldir_relative_SQ)"' \ '-DGIT_MAN_PATH="$(mandir_relative_SQ)"' \
@@ -2259,6 +2263,10 @@ command-list.h: $(wildcard Documentation/git*.txt) $(patsubst %,--exclude-program %,$(EXCLUDED_PROGRAMS)) \ command-list.txt >$@+ && mv $@+ $@ +hook-list.h: generate-hooklist.sh Documentation/githooks.txt + $(QUIET_GEN)$(SHELL_PATH) ./generate-hooklist.sh \ + >$@+ && mv $@+ $@ + SCRIPT_DEFINES = $(SHELL_PATH_SQ):$(DIFF_SQ):$(GIT_VERSION):\ $(localedir_SQ):$(NO_CURL):$(USE_GETTEXT_SCHEME):$(SANE_TOOL_PATH_SQ):\ $(gitwebdir_SQ):$(PERL_PATH_SQ):$(SANE_TEXT_GREP):$(PAGER_ENV):\
builtin/bugreport.c +8 -36
62 lines not on this page · raw message
contrib/buildsystems/CMakeLists.txt +7 -0
@@ -624,6 +624,13 @@ if(NOT EXISTS ${CMAKE_BINARY_DIR}/config-list.h) OUTPUT_FILE ${CMAKE_BINARY_DIR}/config-list.h) endif() +if(NOT EXISTS ${CMAKE_BINARY_DIR}/hook-list.h) + message("Generating hook-list.h") + execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/generate-hooklist.sh + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + OUTPUT_FILE ${CMAKE_BINARY_DIR}/hook-list.h) +endif() + include_directories(${CMAKE_BINARY_DIR}) #build
generate-hooklist.sh new file +18 -0
@@ -0,0 +1,18 @@ +#!/bin/sh +# +# Usage: ./generate-hooklist.sh >hook-list.h + +cat <<EOF +/* Automatically generated by generate-hooklist.sh */ + +static const char *hook_name_list[] = { +EOF + +sed -n -e '/^~~~~*$/ {x; s/^.*$/ "&",/; p;}; x' \ + <Documentation/githooks.txt | + LC_ALL=C sort + +cat <<EOF + NULL, +}; +EOF
hook.c +19 -0
@@ -1,11 +1,30 @@#include "cache.h" #include "hook.h" #include "run-command.h" +#include "hook-list.h" + +static int known_hook (const char *name) +{ +const char **p; +size_t len =strlen (name); +for (p = hook_name_list; *p; p++) { +const char *hook = *p; + +if (!strncmp (name, hook, len) && hook[len] =='\0' ) +return 1 ; + } + +return 0 ; +}const char *find_hook (const char *name) {static struct strbuf path =STRBUF_INIT ; +if (!known_hook (name)) +die (_ ("the hook '%s' is not known to git, should be in hook-list.h via githooks(5)" ), + name); +strbuf_reset (&path);strbuf_git_path (&path,"hooks/%s" , name);if (access (path.buf,X_OK ) <0 ) {
Signature · 1 line
2.33.0.rc0.595.ge31e012651d