commit: use config-based hooks (config-based hooks part II)
PATCH 1/1 commit: use config-based hooks (config-based hooks part II)
As part of the adoption of config-based hooks, 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.
---
This is based on es/config-based-hooks (v5).
Since config-based hooks v4, I split a new branch for the conversions of existing hook callers. I'm hoping this will make it easier for me to work on the architecture without needing to juggle these hopefully-slow-moving patches as frequently, and will reduce the load on reviewers.
Since v4 also I removed the second patch changing the run_commit_hook() API. I had figured it might not be well received when I sent it, and it wasn't, so no worries.
I'm hoping to have updates to this series pretty soon too with new conversions... so I guess this one may just be noise. Ah well.
- Emily
builtin/commit.c | 3 ++- builtin/merge.c | 3 ++- commit.c | 10 +++++++++- ...503-pre-commit-and-pre-merge-commit-hooks.sh | 17 +++++++++++++++-- 4 files changed, 28 insertions(+), 5 deletions(-)
builtin/commit.c +2 -1
@@ -36,6 +36,7 @@#include "help.h" #include "commit-reach.h" #include "commit-graph.h" +#include "hook.h" static const char *const builtin_commit_usage[] = {N_ ("git commit [<options>] [--] <pathspec>..." ),
@@ -983,7 +984,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" ,configured_hookdir_opt ())) {/* * 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 +2 -1
@@ -41,6 +41,7 @@#include "commit-reach.h" #include "wt-status.h" #include "commit-graph.h" +#include "hook.h" #define DEFAULT_TWOHEAD (1<<0)#define DEFAULT_OCTOPUS (1<<1)
@@ -829,7 +830,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" ,configured_hookdir_opt ()))discard_cache ();read_cache_from (index_file);strbuf_addbuf (&msg, &merge_msg);
commit.c +9 -1
@@ -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 **);
@@ -1635,8 +1636,11 @@ int run_commit_hook(int editor_is_used, const char *index_file, {struct strvec hook_env =STRVEC_INIT ;va_list args; +const char *arg; +struct strvec hook_args =STRVEC_INIT ;int ret; +strvec_pushf (&hook_env,"GIT_INDEX_FILE=%s" , index_file);/*
@@ -1646,9 +1650,13 @@ int run_commit_hook(int editor_is_used, const char *index_file,strvec_push (&hook_env,"GIT_EDITOR=:" );va_start (args, name); - ret =run_hook_ve (hook_env.v, name, args); + while ((arg = va_arg(args, const char *))) +strvec_push (&hook_args, arg);va_end (args); + + ret =run_hooks (hook_env.v, name, &hook_args,configured_hookdir_opt ());strvec_clear (&hook_env); +strvec_clear (&hook_args);return ret; }
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +15 -2
@@ -5,8 +5,8 @@ test_description='pre-commit and pre-merge-commit hooks' . ./test-lib.sh HOOKDIR="$(git rev-parse --git-dir)/hooks" -PRECOMMIT="$HOOKDIR/pre-commit" -PREMERGE="$HOOKDIR/pre-merge-commit" +PRECOMMIT="$(pwd)/$HOOKDIR/pre-commit" +PREMERGE="$(pwd)/$HOOKDIR/pre-merge-commit" # Prepare sample scripts that write their $0 to actual_hooks test_expect_success 'sample script setup' '
@@ -103,6 +103,19 @@ test_expect_success 'with succeeding hook' ' test_cmp expected_hooks actual_hooks ' +# NEEDSWORK: when 'git hook add' and 'git hook remove' have been added, use that +# instead +test_expect_success 'with succeeding hook (config-based)' ' + test_when_finished "git config --unset hook.pre-commit.command success.sample" && + test_when_finished "rm -f expected_hooks actual_hooks" && + git config hook.pre-commit.command "$HOOKDIR/success.sample" && + echo "$HOOKDIR/success.sample" >expected_hooks && + echo "more" >>file && + git add file && + git commit -m "more" && + test_cmp expected_hooks actual_hooks +' + test_expect_success 'with succeeding hook (merge)' ' test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" && cp "$HOOKDIR/success.sample" "$PREMERGE" &&
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
builtin/commit.c static int prepare_to_commit(const char *index_file, const char *prefix, · line 987
Quoted 6 lines · Emily Shaffer
builtin/commit.c +1
-1@@ -983,7 +984,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" ,configured_hookdir_opt ())) {
So, the migration from find_hook(), which is the traditional "only on the filesystem in $GIT_DIR/hooks", to hook_exists(), which knows the ones defined in the configuration files, is the same as the previous round's. Understood.
Quoted 13 lines
commit.c +1
-0@@ -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 **);
commit.c int run_commit_hook(int editor_is_used, const char *index_file, · line 1644
Quoted 9 lines
commit.c +3
-0@@ -1635,8 +1636,11 @@ int run_commit_hook(int editor_is_used, const char *index_file, {struct strvec hook_env =STRVEC_INIT ;va_list args; +const char *arg; +struct strvec hook_args =STRVEC_INIT ;int ret; +strvec_pushf (&hook_env,"GIT_INDEX_FILE=%s" , index_file);
Noise.
commit.c int run_commit_hook(int editor_is_used, const char *index_file, · line 1657
Quoted 10 lines
commit.c +4
-1@@ -1646,9 +1650,13 @@ int run_commit_hook(int editor_is_used, const char *index_file,strvec_push (&hook_env,"GIT_EDITOR=:" );va_start (args, name); - ret =run_hook_ve (hook_env.v, name, args); + while ((arg = va_arg(args, const char *))) +strvec_push (&hook_args, arg);va_end (args); + + ret =run_hooks (hook_env.v, name, &hook_args,configured_hookdir_opt ());
And this is the calling convention change. Generally speaking, run_hook_ve() and friends (does it still have friends?) are on their way out, and run_hooks() will be the only thing we need to learn in the future.
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh test_description='pre-commit and pre-merge-commit hooks' · line 9
Quoted 12 lines
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +2
-2@@ -5,8 +5,8 @@ test_description='pre-commit and pre-merge-commit hooks' . ./test-lib.sh HOOKDIR="$(git rev-parse --git-dir)/hooks" -PRECOMMIT="$HOOKDIR/pre-commit" -PREMERGE="$HOOKDIR/pre-merge-commit" +PRECOMMIT="$(pwd)/$HOOKDIR/pre-commit" +PREMERGE="$(pwd)/$HOOKDIR/pre-merge-commit"
What makes this change necessary? Are we going to chdir around or something? Isn't the configuration file the ONLY thing that needs to know their full location?
Should and does hook.*.command configuration honor ~ expansion? I think our $TRASH_DIRECTORY is our $HOME during tests, so
git config hook.foo.command "~/.git/hooks/success.sample"
might be a worthy thing to test. Also, do we have a clear definition on what happens to a relative pathname specified for the hook commands, or is it left "undefined--do not do that"? If the former, that would also be worth testing. I'd imagine that majority of hooks defined in ~/.gitconfig WILL point at full path so testing relative path may not matter too much for that particular use case, but for ones defined in .git/config, I suspect it would be most natural to take it as relative to the root of the working tree.
It may be a good change in the longer term, but it felt surprising to see such a change that would affect 103-5=98 lines of existing tests was made centrally here, without any explanation in the proposed log message.
Thanks.
Quoted 22 lines
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +13
-0# Prepare sample scripts that write their $0 to actual_hooks test_expect_success 'sample script setup' '@@ -103,6 +103,19 @@ test_expect_success 'with succeeding hook' ' test_cmp expected_hooks actual_hooks ' +# NEEDSWORK: when 'git hook add' and 'git hook remove' have been added, use that +# instead +test_expect_success 'with succeeding hook (config-based)' ' + test_when_finished "git config --unset hook.pre-commit.command success.sample" && + test_when_finished "rm -f expected_hooks actual_hooks" && + git config hook.pre-commit.command "$HOOKDIR/success.sample" && + echo "$HOOKDIR/success.sample" >expected_hooks && + echo "more" >>file && + git add file && + git commit -m "more" && + test_cmp expected_hooks actual_hooks +' + test_expect_success 'with succeeding hook (merge)' ' test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" && cp "$HOOKDIR/success.sample" "$PREMERGE" &&
cover use config-based hooks (config-based hooks part
In reply to Emily ShafferAs of rebasing today, these are all the hooks, converted to hook.h.
I chose between parallel and serial execution based only on reading the docs - please, please correct me if I'm wrong.
Most of these are pretty straightforward but there are a couple outstanding ones on the server side, which I think could use some extra attention.
Thanks!
- Emily
Emily Shaffer (17):
commit: use config-based hooks
am: convert applypatch hooks to use config
merge: use config-based hooks for post-merge hook
gc: use hook library for pre-auto-gc hook
rebase: teach pre-rebase to use hook.h
read-cache: convert post-index-change hook to use config
receive-pack: convert push-to-checkout hook to hook.h
git-p4: use 'git hook' to run hooks
hooks: convert 'post-checkout' hook to hook library
hook: convert 'post-rewrite' hook to config
transport: convert pre-push hook to use config
reference-transaction: look for hooks in config
receive-pack: convert 'update' hook to hook.h
proc-receive: acquire hook list from hook.h
post-update: use hook.h library
receive-pack: convert receive hooks to hook.h
run-command: stop thinking about hooks
Documentation/githooks.txt | 45 +++ builtin/am.c | 30 +- builtin/checkout.c | 16 +- builtin/clone.c | 7 +- builtin/commit.c | 11 +- builtin/gc.c | 4 +- builtin/merge.c | 14 +- builtin/rebase.c | 7 +- builtin/receive-pack.c | 326 ++++++++++-------- builtin/worktree.c | 30 +- commit.c | 20 +- commit.h | 3 +- git-p4.py | 70 +--- hook.c | 39 ++- read-cache.c | 12 +- refs.c | 33 +- reset.c | 15 +- run-command.c | 66 ---- run-command.h | 24 -- sequencer.c | 83 ++--- t/t1416-ref-transaction-hooks.sh | 12 +- t/t5411/test-0015-too-many-hooks-error.sh | 47 +++ ...3-pre-commit-and-pre-merge-commit-hooks.sh | 17 +- transport.c | 55 +-- 24 files changed, 492 insertions(+), 494 deletions(-) create mode 100644 t/t5411/test-0015-too-many-hooks-error.sh
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH 1/17 commit: use config-based hooks
As part of the adoption of config-based hooks, 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.
--- Documentation/githooks.txt | 10 ++++++++++ builtin/commit.c | 11 +++++----- builtin/merge.c | 9 +++++---- commit.c | 20 +++++++++++++------ commit.h | 3 ++- sequencer.c | 7 ++++--- ...3-pre-commit-and-pre-merge-commit-hooks.sh | 17 ++++++++++++++-- 7 files changed, 56 insertions(+), 21 deletions(-)
Documentation/githooks.txt +10 -0
@@ -103,6 +103,8 @@ The default 'pre-commit' hook, when enabled--and with the `hooks.allownonascii` config option unset or set to false--prevents the use of non-ASCII filenames. +Hooks executed during 'pre-commit' will not be parallelized. + pre-merge-commit ~~~~~~~~~~~~~~~~
@@ -125,6 +127,8 @@ need to be resolved and the result committed separately (see linkgit:git-merge[1]). At that point, this hook will not be executed, but the 'pre-commit' hook will, if it is enabled. +Hooks executed during 'pre-merge-commit' will not be parallelized. + prepare-commit-msg ~~~~~~~~~~~~~~~~~~
@@ -150,6 +154,8 @@ be used as replacement for pre-commit hook. The sample `prepare-commit-msg` hook that comes with Git removes the help message found in the commented portion of the commit template. +Hooks executed during 'prepare-commit-msg' will not be parallelized. + commit-msg ~~~~~~~~~~
@@ -166,6 +172,8 @@ file. The default 'commit-msg' hook, when enabled, detects duplicate `Signed-off-by` trailers, and aborts the commit if one is found. +Hooks executed during 'commit-msg' will not be parallelized. + post-commit ~~~~~~~~~~~
@@ -175,6 +183,8 @@ invoked after a commit is made. This hook is meant primarily for notification, and cannot affect the outcome of `git commit`. +Hooks executed during 'post-commit' will run in parallel by default. + pre-rebase ~~~~~~~~~~
builtin/commit.c +6 -5
@@ -36,6 +36,7 @@#include "help.h" #include "commit-reach.h" #include "commit-graph.h" +#include "hook.h" static const char *const builtin_commit_usage[] = {N_ ("git commit [<options>] [--] <pathspec>..." ),
@@ -699,7 +700,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,/* This checks and barfs if author is badly specified */ determine_author_info (author_ident); -if (!no_verify &&run_commit_hook (use_editor, index_file,"pre-commit" ,NULL )) +if (!no_verify &&run_commit_hook (use_editor,0 , index_file,"pre-commit" ,NULL ))return 0 ;if (squash_message) {
@@ -983,7 +984,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" ,configured_hookdir_opt ())) {/* * 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
@@ -998,7 +999,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,0 , index_file,"prepare-commit-msg" ,git_path_commit_editmsg (), hook_arg1, hook_arg2,NULL ))return 0 ;
@@ -1015,7 +1016,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix, }if (!no_verify && -run_commit_hook (use_editor, index_file,"commit-msg" ,git_path_commit_editmsg (),NULL )) { +run_commit_hook (use_editor,0 , index_file,"commit-msg" ,git_path_commit_editmsg (),NULL )) {return 0 ; }
@@ -1701,7 +1702,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)repo_rerere (the_repository,0 );run_auto_maintenance (quiet); -run_commit_hook (use_editor,get_index_file (),"post-commit" ,NULL ); +run_commit_hook (use_editor,1 ,get_index_file (),"post-commit" ,NULL );if (amend && !no_post_rewrite) {commit_post_rewrite (the_repository, current_head, &oid); }
builtin/merge.c +5 -4
@@ -42,6 +42,7 @@#include "commit-reach.h" #include "wt-status.h" #include "commit-graph.h" +#include "hook.h" #define DEFAULT_TWOHEAD (1<<0)#define DEFAULT_OCTOPUS (1<<1)
@@ -836,14 +837,14 @@ static void prepare_to_commit(struct commit_list *remoteheads)struct strbuf msg =STRBUF_INIT ;const char *index_file =get_index_file (); -if (!no_verify &&run_commit_hook (0 < option_edit, index_file,"pre-merge-commit" ,NULL )) +if (!no_verify &&run_commit_hook (0 < option_edit,0 , index_file,"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 * the editor and after we invoke run_status above. */ -if (find_hook ("pre-merge-commit" )) +if (hook_exists ("pre-merge-commit" ,configured_hookdir_opt ()))discard_cache ();read_cache_from (index_file);strbuf_addbuf (&msg, &merge_msg);
@@ -864,7 +865,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)append_signoff (&msg,ignore_non_trailer (msg.buf, msg.len),0 );write_merge_heads (remoteheads);write_file_buf (git_path_merge_msg (the_repository), msg.buf, msg.len); -if (run_commit_hook (0 < option_edit,get_index_file (),"prepare-commit-msg" , +if (run_commit_hook (0 < option_edit,0 ,get_index_file (),"prepare-commit-msg" ,git_path_merge_msg (the_repository),"merge" ,NULL ))abort_commit (remoteheads,NULL );if (0 < option_edit) {
@@ -872,7 +873,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)abort_commit (remoteheads,NULL ); } -if (!no_verify &&run_commit_hook (0 < option_edit,get_index_file (), +if (!no_verify &&run_commit_hook (0 < option_edit,0 ,get_index_file (),"commit-msg" ,git_path_merge_msg (the_repository),NULL ))abort_commit (remoteheads,NULL );
commit.c +14 -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 **);
@@ -1630,25 +1631,32 @@ size_t ignore_non_trailer(const char *buf, size_t len)return boc ? len - boc : len - cutoff; } -int run_commit_hook (int editor_is_used,const char *index_file, +int run_commit_hook (int editor_is_used,int parallelize,const char *index_file,const char *name, ...) { -struct strvec hook_env =STRVEC_INIT ; +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT_SYNC ;va_list args; +const char *arg;int ret; -strvec_pushf (&hook_env,"GIT_INDEX_FILE=%s" , index_file); +if (parallelize) + opt.jobs =configured_hook_jobs (); + +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; }
commit.h +2 -1
@@ -352,6 +352,7 @@ int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)int compare_commits_by_gen_then_commit_date (const void *a_,const void *b_,void *unused);LAST_ARG_MUST_BE_NULL -int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...); +int run_commit_hook(int editor_is_used, int parallelize, const char *index_file, +const char *name, ...);#endif /* COMMIT_H */
sequencer.c +4 -3
@@ -34,6 +34,7 @@#include "commit-reach.h" #include "rebase-interactive.h" #include "reset.h" +#include "hook.h" #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
@@ -1203,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 ,0 , r->index_file,"prepare-commit-msg" , name, arg1, arg2,NULL )) ret =error (_ ("'prepare-commit-msg' hook failed" ));
@@ -1438,7 +1439,7 @@ static int try_to_commit(struct repository *r, } } -if (find_hook ("prepare-commit-msg" )) { +if (hook_exists ("prepare-commit-msg" ,configured_hookdir_opt ())) { res =run_prepare_commit_msg_hook (r, msg, hook_commit);if (res) goto out;
@@ -1528,7 +1529,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 ,1 , r->index_file,"post-commit" ,NULL );if (flags &AMEND_MSG )commit_post_rewrite (r, current_head, oid);
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +15 -2
@@ -5,8 +5,8 @@ test_description='pre-commit and pre-merge-commit hooks' . ./test-lib.sh HOOKDIR="$(git rev-parse --git-dir)/hooks" -PRECOMMIT="$HOOKDIR/pre-commit" -PREMERGE="$HOOKDIR/pre-merge-commit" +PRECOMMIT="$(pwd)/$HOOKDIR/pre-commit" +PREMERGE="$(pwd)/$HOOKDIR/pre-merge-commit" # Prepare sample scripts that write their $0 to actual_hooks test_expect_success 'sample script setup' '
@@ -103,6 +103,19 @@ test_expect_success 'with succeeding hook' ' test_cmp expected_hooks actual_hooks ' +# NEEDSWORK: when 'git hook add' and 'git hook remove' have been added, use that +# instead +test_expect_success 'with succeeding hook (config-based)' ' + test_when_finished "git config --unset hook.pre-commit.command success.sample" && + test_when_finished "rm -f expected_hooks actual_hooks" && + git config hook.pre-commit.command "$HOOKDIR/success.sample" && + echo "$HOOKDIR/success.sample" >expected_hooks && + echo "more" >>file && + git add file && + git commit -m "more" && + test_cmp expected_hooks actual_hooks +' + test_expect_success 'with succeeding hook (merge)' ' test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" && cp "$HOOKDIR/success.sample" "$PREMERGE" &&
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH 2/17 am: convert applypatch hooks to use config
In reply to Emily ShafferTeach pre-applypatch, post-applypatch, and applypatch-msg to use the hook.h library instead of the run-command.h library. This enables use of hooks specified in the config, in addition to those in the hookdir. These three hooks are called only by builtin/am.c.
--- Documentation/githooks.txt | 6 ++++++ builtin/am.c | 12 +++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-)
Documentation/githooks.txt +6 -0
@@ -58,6 +58,8 @@ the message file. The default 'applypatch-msg' hook, when enabled, runs the 'commit-msg' hook, if the latter is enabled. +Hooks run during 'applypatch-msg' will not be parallelized. + pre-applypatch ~~~~~~~~~~~~~~
@@ -73,6 +75,8 @@ make a commit if it does not pass certain test. The default 'pre-applypatch' hook, when enabled, runs the 'pre-commit' hook, if the latter is enabled. +Hooks run during 'pre-applypatch' will be run in parallel by default. + post-applypatch ~~~~~~~~~~~~~~~
@@ -82,6 +86,8 @@ and is invoked after the patch is applied and a commit is made. This hook is meant primarily for notification, and cannot affect the outcome of `git am`. +Hooks run during 'post-applypatch' will be run in parallel by default. + pre-commit ~~~~~~~~~~
builtin/am.c +9 -3
@@ -33,6 +33,7 @@#include "string-list.h" #include "packfile.h" #include "repository.h" +#include "hook.h" /** * Returns the length of the first line of msg.
@@ -426,9 +427,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_SYNC ;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);
@@ -1558,8 +1562,9 @@ 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 =RUN_HOOKS_OPT_INIT_ASYNC ; -if (run_hook_le (NULL ,"pre-applypatch" ,NULL )) +if (run_hooks ("pre-applypatch" , &hook_opt))exit (1 );if (write_cache_as_tree (&tree,0 ,NULL ))
@@ -1611,8 +1616,9 @@ static void do_commit(const struct am_state *state)fclose (fp); } -run_hook_le (NULL ,"post-applypatch" ,NULL ); +run_hooks ("post-applypatch" , &hook_opt); +run_hooks_opt_clear (&hook_opt);strbuf_release (&sb); }
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH 3/17 merge: use config-based hooks for post-merge hook
In reply to Emily ShafferTeach 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.
--- Documentation/githooks.txt | 2 ++ builtin/merge.c | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-)
Documentation/githooks.txt +2 -0
@@ -236,6 +236,8 @@ save and restore any form of metadata associated with the working tree (e.g.: permissions/ownership, ACLS, etc). See contrib/hooks/setgitperms.perl for an example of how to do this. +Hooks executed during 'post-merge' will run in parallel by default. + pre-push ~~~~~~~~
builtin/merge.c +4 -1
@@ -443,6 +443,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_ASYNC ;const struct object_id *head = &head_commit->object.oid;if (!msg)
@@ -484,7 +485,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);
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH 4/17 gc: use hook library for pre-auto-gc hook
In reply to Emily ShafferUsing 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.
--- Documentation/githooks.txt | 2 ++ builtin/gc.c | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-)
Documentation/githooks.txt +2 -0
@@ -553,6 +553,8 @@ This hook is invoked by `git gc --auto` (see linkgit:git-gc[1]). It takes no parameter, and exiting with non-zero status from this script causes the `git gc --auto` to abort. +Hooks run during 'pre-auto-gc' will be run in parallel by default. + post-rewrite ~~~~~~~~~~~~
builtin/gc.c +3 -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"
@@ -340,6 +341,7 @@ static void add_repack_incremental_option(void)static int need_to_gc (void ) { +struct run_hooks_opt hook_opt =RUN_HOOKS_OPT_INIT_ASYNC ;/* * Setting gc.auto to 0 or negative can disable the * automatic gc.
@@ -386,7 +388,7 @@ 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))return 0 ;return 1 ; }
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH 5/17 rebase: teach pre-rebase to use hook.h
In reply to Emily ShafferBy using hook.h instead of run-command.h to run hooks, pre-rebase hooks can now be specified in the config as well as in the hookdir. pre-rebase is not called anywhere besides builtin/rebase.c.
--- Documentation/githooks.txt | 2 ++ builtin/rebase.c | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-)
Documentation/githooks.txt +2 -0
@@ -200,6 +200,8 @@ two parameters. The first parameter is the upstream from which the series was forked. The second parameter is the branch being rebased, and is not set when rebasing the current branch. +Hooks executed during 'pre-rebase' will run in parallel by default. + post-checkout ~~~~~~~~~~~~~
builtin/rebase.c +5 -2
@@ -28,6 +28,7 @@#include "sequencer.h" #include "rebase-interactive.h" #include "reset.h" +#include "hook.h" #define DEFAULT_REFLOG_ACTION "rebase"
@@ -1312,6 +1313,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_ASYNC ;struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, N_("revision"),
@@ -2024,9 +2026,9 @@ 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))die (_ ("The pre-rebase hook refused to rebase." ));if (options.flags &REBASE_DIFFSTAT ) {
@@ -2106,6 +2108,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) ret = !!run_specific_rebase (&options, action); cleanup: +run_hooks_opt_clear (&hook_opt);strbuf_release (&buf);strbuf_release (&revisions);free (options.head_name);
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH 6/17 read-cache: convert post-index-change hook to use config
In reply to Emily ShafferBy 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.
--- Documentation/githooks.txt | 2 ++ read-cache.c | 12 +++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-)
Documentation/githooks.txt +2 -0
@@ -711,6 +711,8 @@ 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. +Hooks run during 'post-index-change' will be run in parallel by default. + GIT --- Part of the linkgit:git[1] suite
read-cache.c +9 -3
@@ -25,6 +25,7 @@#include "fsmonitor.h" #include "thread-utils.h" #include "progress.h" +#include "hook.h" /* Mask for the name length in ce_flags in the on-disk index */
@@ -3052,6 +3053,7 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l unsigned flags) {int ret; +struct run_hooks_opt hook_opt =RUN_HOOKS_OPT_INIT_ASYNC ;/* * TODO trace2: replace "the_repository" with the actual repo instance
@@ -3070,9 +3072,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 ;
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH 7/17 receive-pack: convert push-to-checkout hook to hook.h
In reply to Emily ShafferBy 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.
--- Documentation/githooks.txt | 1 + builtin/receive-pack.c | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-)
Documentation/githooks.txt +1 -0
@@ -547,6 +547,7 @@ that switches branches while keeping the local changes in the working tree that do not interfere with the difference between the branches. +Hooks executed during 'push-to-checkout' will not be parallelized. pre-auto-gc ~~~~~~~~~~~
builtin/receive-pack.c +11 -4
@@ -29,6 +29,7 @@#include "commit-reach.h" #include "worktree.h" #include "shallow.h" +#include "hook.h" static const char *const receive_pack_usage[] = {N_ ("git receive-pack <git-dir>" ),
@@ -1427,12 +1428,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_SYNC; +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)
@@ -1456,7 +1463,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,configured_hookdir_opt ())) retval =push_to_deploy (sha1, &env, work_tree);else retval =push_to_checkout (sha1, &env, work_tree);
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH 8/17 git-p4: use 'git hook' to run hooks
In reply to Emily ShafferInstead of duplicating the behavior of run-command.h:run_hook_le() in Python, we can directly call 'git hook run'. As a bonus, this means git-p4 learns how to find hook specifications from the Git config as well as from the hookdir.
---
Notes:
Maybe there is a better way to do this - I had a hard time getting this to run locally, and Python is not my forte, so if anybody has a better approach I'd love to just take that patch instead :)
git-p4.py | 70 +++++++------------------------------------------------ 1 file changed, 9 insertions(+), 61 deletions(-)
git-p4.py +9 -61
@@ -208,70 +208,18 @@ def decode_path(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): + print('ESS: entering run_git_hook') + if not cmd: 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'] + + ["-a" + arg for arg in param] + + [cmd]) + print ('ESS: args:') + print (args) + return subprocess.call(args) == 0 def write_pipe(c, stdin): if verbose:
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
Re: [PATCH 08/17] git-p4: use 'git hook' to run hooks
Quoted 28 lines · Emily Shaffer
git-p4.py +0
-20@@ -208,70 +208,18 @@ def decode_path(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()
git-p4.py def decode_path(path): · line 211
Quoted 10 lines
git-p4.py +1
-9- 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): + print('ESS: entering run_git_hook')
Looks like a stray debug message here?
Quoted 30 lines
git-p4.py +1
-27+ if not cmd: 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:
git-p4.py def decode_path(path): · line 219
Quoted 10 lines
git-p4.py +5
-5- 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'] + + ["-a" + arg for arg in param] + + [cmd]) + print ('ESS: args:')
Here as well.
Quoted 9 lines
git-p4.py +2
-0+ print (args) + return subprocess.call(args) == 0 def write_pipe(c, stdin): if verbose:Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
Re: [PATCH 08/17] git-p4: use 'git hook' to run hooks
Quoted 6 lines · Josh Steadmon
Quoted 1 line · Emily Shaffer
+
print('ESS: entering run_git_hook')Looks like a stray debug message here?
...
Quoted 3 lines
Quoted 1 line
+ print ('ESS: args:')
Here as well.
:X Have fixed locally and will include with a new reroll this week. Oops. :)
- Emily
PATCH 9/17 hooks: convert 'post-checkout' hook to hook library
In reply to Emily ShafferBy using the 'hook.h' library, 'post-checkout' hooks can now be specified in the config as well as in the hook directory.
--- Documentation/githooks.txt | 2 ++ builtin/checkout.c | 16 +++++++++++----- builtin/clone.c | 7 +++++-- builtin/worktree.c | 30 ++++++++++++++---------------- reset.c | 15 +++++++++++---- 5 files changed, 43 insertions(+), 27 deletions(-)
Documentation/githooks.txt +2 -0
@@ -224,6 +224,8 @@ This hook can be used to perform repository validity checks, auto-display differences from the previous HEAD if different, or set working dir metadata properties. +Hooks executed during 'post-checkout' will not be parallelized. + post-merge ~~~~~~~~~~
builtin/checkout.c +11 -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"
@@ -104,13 +105,18 @@ 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_SYNC ; +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:
@@ -771,6 +772,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_SYNC ;if (option_no_checkout)return 0 ;
@@ -816,8 +818,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
@@ -12,6 +12,7 @@#include "submodule.h" #include "utf8.h" #include "worktree.h" +#include "hook.h" static const char *const worktree_usage[] = {N_ ("git worktree add [<options>] <path> [<commit-ish>]" ),
@@ -455,22 +456,19 @@ 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_SYNC ; + +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; + + ret =run_hooks ("post-checkout" , &opt); + +run_hooks_opt_clear (&opt); }strvec_clear (&child_env);
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_SYNC ; +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.28.0.rc0.142.g3c755180ce-goog
PATCH 10/17 hook: convert 'post-rewrite' hook to config
In reply to Emily ShafferBy using 'hook.h' for 'post-rewrite', we simplify hook invocations by not needing to put together our own 'struct child_process' and we also learn to run hooks specified in the config as well as the hook dir.
--- Documentation/githooks.txt | 2 + builtin/am.c | 18 +++------ sequencer.c | 76 +++++++++++++++----------------------- 3 files changed, 36 insertions(+), 60 deletions(-)
Documentation/githooks.txt +2 -0
@@ -585,6 +585,8 @@ The hook always runs after the automatic note copying (see "notes.rewrite.<command>" in linkgit:git-config[1]) has happened, and thus has access to these notes. +Hooks run during 'post-rewrite' will be run in parallel by default. + The following command-specific comments apply: rebase::
builtin/am.c +5 -13
@@ -449,23 +449,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_ASYNC ;int ret; -if (!hook) -return 0 ; - -strvec_push (&cp.args, hook); -strvec_push (&cp.args,"rebase" ); +strvec_push (&opt.args,"rebase" ); + opt.path_to_stdin =am_path (state,"rewritten" ); - cp.in =xopen (am_path (state,"rewritten" ),O_RDONLY ); - cp.stdout_to_stderr =1 ; - cp.trace2_hook_name ="post-rewrite" ; + ret =run_hooks ("post-rewrite" , &opt); - ret =run_command (&cp); - -close (cp.in); +run_hooks_opt_clear (&opt);return ret; }
sequencer.c +29 -47
98 lines not on this page · raw message
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
Re: [PATCH 10/17] hook: convert 'post-rewrite' hook to config
Quoted 31 lines · Emily Shaffer
sequencer.c +4
-9@@ -35,6 +35,7 @@#include "rebase-interactive.h" #include "reset.h" #include "hook.h" +#include "string-list.h" #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"@@ -1143,33 +1144,23 @@ 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_ASYNC ; +struct strbuf tmp =STRBUF_INIT ;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 ; -
sequencer.c int update_head_with_reflog(const struct commit *old_head, · line 1167
Quoted 10 lines
sequencer.c +0
-10- 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 );
Here and in a few other later patches, we're removing some signal handling that doesn't seem to be replicated in the run_hooks() implementation. Can you add a note to the commit message about why this is OK?
Quoted 67 lines
sequencer.c +25
-28-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 (&opt.str_stdin, tmp.buf); + + code =run_hooks ("post-rewrite" , &opt); + +run_hooks_opt_clear (&opt); +strbuf_release (&tmp); +return code; } void commit_post_rewrite(struct repository *r,@@ -4317,30 +4308,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_ASYNC ; + + 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.28.0.rc0.142.g3c755180ce-goog
Re: [PATCH 10/17] hook: convert 'post-rewrite' hook to config
Quoted 40 lines · Josh Steadmon
Quoted 38 lines · Emily Shaffer
sequencer.c +4
-16@@ -35,6 +35,7 @@#include "rebase-interactive.h" #include "reset.h" #include "hook.h" +#include "string-list.h" #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"@@ -1143,33 +1144,23 @@ 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_ASYNC ; +struct strbuf tmp =STRBUF_INIT ;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)
Quoted 8 lines
sequencer.c int update_head_with_reflog(const struct commit *old_head, · line 1167
Quoted 3 lines
sequencer.c +0
-3-return code; -strbuf_addf (&sb,"%s %s\n" ,oid_to_hex (oldoid),oid_to_hex (newoid)); -sigchain_push (SIGPIPE ,SIG_IGN );Here and in a few other later patches, we're removing some signal handling that doesn't seem to be replicated in the
run_hooks()implementation. Can you add a note to the commit message about why this is OK?
Yeah, I have added it to the commit message for next rollup. I think offline you said you found it - but this signal handling was added when I taught run_processes_parallel() to use a callback to provide stdin. It lives in run-command.h now.
- Emily
PATCH 11/17 transport: convert pre-push hook to use config
In reply to Emily ShafferBy using the hook.h:run_hooks API, pre-push hooks can be specified in the config as well as in the hookdir.
--- Documentation/githooks.txt | 2 ++ transport.c | 55 +++++++++----------------------------- 2 files changed, 14 insertions(+), 43 deletions(-)
Documentation/githooks.txt +2 -0
@@ -271,6 +271,8 @@ If this hook exits with a non-zero status, `git push` will abort without pushing anything. Information about why the push is rejected may be sent to the user by writing to standard error. +Hooks executed during 'pre-push' will run in parallel by default. + [[pre-receive]] pre-receive ~~~~~~~~~~~
transport.c +12 -43
79 lines not on this page · raw message
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH 12/17 reference-transaction: look for hooks in config
In reply to Emily ShafferBy using the hook.h library, reference-transaction hooks can be specified in the config instead.
The expected output of the test is not fully updated to reflect the absolute path of the hook called because the 'update' hook has not yet been converted to use hook.h.
--- Documentation/githooks.txt | 2 ++ refs.c | 33 +++++++------------------------- t/t1416-ref-transaction-hooks.sh | 8 ++++---- 3 files changed, 13 insertions(+), 30 deletions(-)
Documentation/githooks.txt +2 -0
@@ -521,6 +521,8 @@ The exit status of the hook is ignored for any state except for the cause the transaction to be aborted. The hook will not be called with "aborted" state in that case. +Hooks run during 'reference-transaction' will be run in parallel by default. + push-to-checkout ~~~~~~~~~~~~~~~~
refs.c +7 -26
@@ -18,6 +18,7 @@#include "strvec.h" #include "repository.h" #include "sigchain.h" +#include "hook.h" /* * List of all available backends
@@ -1957,47 +1958,27 @@ 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_ASYNC ;int ret =0 , i; - hook =find_hook ("reference-transaction" ); -if (!hook) -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];strbuf_reset (&buf); -strbuf_addf (&buf,"%s %s %s\n" , +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 ; - } +string_list_append (&opt.str_stdin, buf.buf); } -close (proc.in); -sigchain_pop (SIGPIPE ); + ret =run_hooks ("reference-transaction" , &opt); +run_hooks_opt_clear (&opt);strbuf_release (&buf); - ret |=finish_command (&proc);return ret; }
t/t1416-ref-transaction-hooks.sh +4 -4
@@ -122,11 +122,11 @@ test_expect_success 'interleaving hook calls succeed' ' cat >expect <<-EOF && hooks/update refs/tags/PRE $ZERO_OID $PRE_OID - hooks/reference-transaction prepared - hooks/reference-transaction committed + $(pwd)/target-repo.git/hooks/reference-transaction prepared + $(pwd)/target-repo.git/hooks/reference-transaction committed hooks/update refs/tags/POST $ZERO_OID $POST_OID - hooks/reference-transaction prepared - hooks/reference-transaction committed + $(pwd)/target-repo.git/hooks/reference-transaction prepared + $(pwd)/target-repo.git/hooks/reference-transaction committed EOF git push ./target-repo.git PRE POST &&
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH 13/17 receive-pack: convert 'update' hook to hook.h
In reply to Emily ShafferBy using hook.h to invoke the 'update' hook, now hooks can be specified in the config in addition to the hookdir.
--- Documentation/githooks.txt | 2 + builtin/receive-pack.c | 65 +++++++++++++++++++++----------- t/t1416-ref-transaction-hooks.sh | 4 +- 3 files changed, 48 insertions(+), 23 deletions(-)
Documentation/githooks.txt +2 -0
@@ -359,6 +359,8 @@ The default 'update' hook, when enabled--and with `hooks.allowunannotated` config option unset or set to false--prevents unannotated tags to be pushed. +Hooks executed during 'update' are run in parallel by default. + [[proc-receive]] proc-receive ~~~~~~~~~~~~
builtin/receive-pack.c +44 -21
77 lines not on this page · raw message
t/t1416-ref-transaction-hooks.sh +2 -2
@@ -121,10 +121,10 @@ test_expect_success 'interleaving hook calls succeed' ' EOF cat >expect <<-EOF && - hooks/update refs/tags/PRE $ZERO_OID $PRE_OID + $(pwd)/target-repo.git/hooks/update refs/tags/PRE $ZERO_OID $PRE_OID $(pwd)/target-repo.git/hooks/reference-transaction prepared $(pwd)/target-repo.git/hooks/reference-transaction committed - hooks/update refs/tags/POST $ZERO_OID $POST_OID + $(pwd)/target-repo.git/hooks/update refs/tags/POST $ZERO_OID $POST_OID $(pwd)/target-repo.git/hooks/reference-transaction prepared $(pwd)/target-repo.git/hooks/reference-transaction committed EOF
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH 14/17 proc-receive: acquire hook list from hook.h
In reply to Emily ShafferThe proc-receive hook differs from most other hooks Git invokes because the hook and the parent Git process engage in bidirectional communication via stdin/stdout. This bidirectional communication is unsuitable for multiple hooks, whether they are in series or in parallel, and is incompatible with run-command.h:run_processes_parallel:
- The proc-receive hook is intended to modify the state of the Git repo. From 'git help githooks':
This [proc-receive] hook is responsible for updating the relevant references and reporting the results back to 'receive-pack'.
This prevents parallelization and implies, at least, specific ordering of hook execution.
- The proc-receive hook can reject a push by aborting early with an error code. If a former hook ran through the entire push contents successfully but a later hook rejects some of the push, the repo may be left in a partially-updated (and corrupt) state.
- The callback model of the run_processes_parallel() API is unsuited to the current implementation of proc-receive, which loops through "send-receive-consider" with the child process. proc-receive today relies on stateful communication with the child process, which would be unwieldy to implement with callbacks and saved state.
- Additionally, run_processes_parallel() is designed to collate the output of many child processes into a single output (stderr or callback), and would require significant work to tell the caller which process sent the output, and indeed to collect any output before the child process has exited.
So, rather than using hook.h:run_hooks() to invoke the proc-receive hook, receive-pack.c can learn to ask hook.h:hook_list() for the location of a hook to run. This allows users to configure their proc-receive in a global config for all repos if they want, or a local config if they just don't want to use the hookdir. Because running more than one proc-receive hook doesn't make sense from a repo state perspective, we can explicitly ban configuring more than one proc-receive hook at a time.
If a user wants to globally configure one proc-receive hook for most of their repos, but override that hook in a single repo, they should use 'skip' to manually remove the global hook in their special repo:
~/.gitconfig:
[hook.proc-receive]
command = /usr/bin/usual-proc-receive
~/special-repo/.git/config:
[hookcmd./usr/bin/usual-proc-receive]
skip = true
[hook.proc-receive]
command = /usr/bin/special-proc-receive
--- Documentation/githooks.txt | 4 ++ builtin/receive-pack.c | 33 +++++++++++++++- t/t5411/test-0015-too-many-hooks-error.sh | 47 +++++++++++++++++++++++ 3 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 t/t5411/test-0015-too-many-hooks-error.sh
Documentation/githooks.txt +4 -0
@@ -423,6 +423,10 @@ the input. The exit status of the 'proc-receive' hook only determines the success or failure of the group of commands sent to it, unless atomic push is in use. +It is forbidden to specify more than one hook for 'proc-receive'. If a +globally-configured 'proc-receive' must be overridden, use +'hookcmd.<global-hook>.skip = true' to ignore it. + [[post-receive]] post-receive ~~~~~~~~~~~~
builtin/receive-pack.c +31 -2
@@ -1137,11 +1137,40 @@ static int run_proc_receive_hook(struct command *commands,int version =0 ;int code; - argv[0 ] =find_hook ("proc-receive" ); -if (!argv[0 ]) { +struct strbuf hookname =STRBUF_INIT ; +struct hook *proc_receive =NULL ; +struct list_head *pos, *hooks; + +strbuf_addstr (&hookname,"proc-receive" ); + hooks =hook_list (&hookname); + +list_for_each (pos, hooks) { +if (proc_receive) { +rp_error ("only one 'proc-receive' hook can be specified" ); +return -1 ; + } + proc_receive = list_entry(pos, struct hook, list); +/* check if the hookdir hook should be ignored */ +if (proc_receive->from_hookdir) { +switch (configured_hookdir_opt ()) { +case hookdir_interactive: +case hookdir_no: + proc_receive =NULL ; +break ; +default : +break ; + } + } + + } + +if (!proc_receive) {rp_error ("cannot find hook 'proc-receive'" );return -1 ; } + + + argv[0 ] = proc_receive->command.buf; argv[1 ] =NULL ; proc.argv = argv;
t/t5411/test-0015-too-many-hooks-error.sh new file +47 -0
47 lines not on this page · raw message
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH 15/17 post-update: use hook.h library
In reply to Emily ShafferBy using run_hooks() instead of run_hook_le(), 'post-update' hooks can be specified in the config as well as the hookdir.
--- Documentation/githooks.txt | 2 ++ builtin/receive-pack.c | 26 +++++++------------------- 2 files changed, 9 insertions(+), 19 deletions(-)
Documentation/githooks.txt +2 -0
@@ -498,6 +498,8 @@ Both standard output and standard error output are forwarded to `git send-pack` on the other end, so you can simply `echo` messages for the user. +Hooks run during 'post-update' will be run in parallel by default. + reference-transaction ~~~~~~~~~~~~~~~~~~~~~
builtin/receive-pack.c +7 -19
@@ -1678,33 +1678,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_ASYNC ;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.28.0.rc0.142.g3c755180ce-goog
PATCH 16/17 receive-pack: convert receive hooks to hook.h
In reply to Emily ShafferBy using the hook.h library to run receive hooks, they can be specified in the config as well as in the hookdir.
--- Documentation/githooks.txt | 4 + builtin/receive-pack.c | 197 +++++++++++++++++-------------------- 2 files changed, 94 insertions(+), 107 deletions(-)
Documentation/githooks.txt +4 -0
@@ -314,6 +314,8 @@ will be set to zero, `GIT_PUSH_OPTION_COUNT=0`. See the section on "Quarantine Environment" in linkgit:git-receive-pack[1] for some caveats. +Hooks executed during 'pre-receive' will not be parallelized. + [[update]] update ~~~~~~
@@ -466,6 +468,8 @@ environment variables will not be set. If the client selects to use push options, but doesn't transmit any, the count variable will be set to zero, `GIT_PUSH_OPTION_COUNT=0`. +Hooks executed during 'post-receive' are run in parallel by default. + [[post-update]] post-update ~~~~~~~~~~~
builtin/receive-pack.c +90 -107
254 lines not on this page · raw message
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH 17/17 run-command: stop thinking about hooks
In reply to Emily Shafferhook.h has replaced all run-command.h hook-related functionality. run-command.h:run_hooks_le/ve and find_hook are no longer used anywhere in the codebase. So, let's delete the dead code - or, in the one case where it's still needed, move it to an internal function in hook.c.
--- hook.c | 39 ++++++++++++++++++++++++++++-- run-command.c | 66 --------------------------------------------------- run-command.h | 24 ------------------- 3 files changed, 37 insertions(+), 92 deletions(-)
hook.c +37 -2
@@ -197,6 +197,41 @@ static int should_include_hookdir(const char *path, enum hookdir_opt cfg) } } +static const char *find_legacy_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; +} + +struct list_head *hook_list (const struct strbuf * hookname) {struct strbuf hook_key =STRBUF_INIT ;
@@ -214,7 +249,7 @@ struct list_head* hook_list(const struct strbuf* hookname)git_config (hook_config_lookup, (void *)&cb_data);if (have_git_dir ()) - legacy_hook_path =find_hook (hookname->buf); + legacy_hook_path =find_legacy_hook (hookname->buf);/* Unconditionally add legacy hook, but annotate it. */ if (legacy_hook_path) {
@@ -245,7 +280,7 @@ int hook_exists(const char *hookname, enum hookdir_opt should_run_hookdir)int could_run_hookdir = (should_run_hookdir == hookdir_interactive || should_run_hookdir == hookdir_warn || should_run_hookdir == hookdir_yes) - && !!find_hook (hookname); + && !!find_legacy_hook (hookname);strbuf_addf (&hook_key,"hook.%s.command" , hookname);
run-command.c +0 -66
72 lines not on this page · raw message
run-command.h +0 -24
@@ -194,30 +194,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 -* 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, ...); -int run_hook_ve (const char *const *env,const char *name,va_list args); -/* * Trigger an auto-gc */
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
v3 cover use config-based hooks (config-based hooks part II)
In reply to Emily ShafferSince v2:
- Renamed 'master' to 'main' in new t5411 (proc-receive) test.
- Removed some accidentally included debug strings.
- Fixed a nasty bug in the reference-transaction hook conversion where calling 'oid_to_hex()' would invalidate references to the output of earlier 'oid_to_hex()' runs farther up the callstack. Instead, the hook callsite now uses 'oid_to_hex_r()'.
Another thing I wanted to do in this series but ended up not having time for before the holidays was to figure out a way to consolidate Documentation/githooks.txt and Documentation/git-hook.txt. My personal preference would be to remove githooks.txt's contents, move the "Hooks" header from there into git-hook.txt, and have 'git help githooks'/'git help hooks' redirect to git-hook.txt; I don't have a patch to share here because I ran out of time before vacation :) What do others envision the documentation looking like?
Thanks!
- Emily
CI run: https://github.com/nasamuffin/git/actions/runs/436905873
Emily Shaffer (17):
commit: use config-based hooks
am: convert applypatch hooks to use config
merge: use config-based hooks for post-merge hook
gc: use hook library for pre-auto-gc hook
rebase: teach pre-rebase to use hook.h
read-cache: convert post-index-change hook to use config
receive-pack: convert push-to-checkout hook to hook.h
git-p4: use 'git hook' to run hooks
hooks: convert 'post-checkout' hook to hook library
hook: convert 'post-rewrite' hook to config
transport: convert pre-push hook to use config
reference-transaction: look for hooks in config
receive-pack: convert 'update' hook to hook.h
proc-receive: acquire hook list from hook.h
post-update: use hook.h library
receive-pack: convert receive hooks to hook.h
run-command: stop thinking about hooks
Documentation/githooks.txt | 45 +++ builtin/am.c | 30 +- builtin/checkout.c | 16 +- builtin/clone.c | 7 +- builtin/commit.c | 11 +- builtin/gc.c | 4 +- builtin/merge.c | 14 +- builtin/rebase.c | 7 +- builtin/receive-pack.c | 326 ++++++++++-------- builtin/worktree.c | 30 +- commit.c | 20 +- commit.h | 3 +- git-p4.py | 67 +--- hook.c | 39 ++- read-cache.c | 12 +- refs.c | 38 +- reset.c | 15 +- run-command.c | 66 ---- run-command.h | 24 -- sequencer.c | 83 ++--- t/t1416-ref-transaction-hooks.sh | 12 +- t/t5411/test-0015-too-many-hooks-error.sh | 47 +++ ...3-pre-commit-and-pre-merge-commit-hooks.sh | 17 +- transport.c | 55 +-- 24 files changed, 493 insertions(+), 495 deletions(-) create mode 100644 t/t5411/test-0015-too-many-hooks-error.sh
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH v3 1/17 commit: use config-based hooks
As part of the adoption of config-based hooks, 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.
--- Documentation/githooks.txt | 10 ++++++++++ builtin/commit.c | 11 +++++----- builtin/merge.c | 9 +++++---- commit.c | 20 +++++++++++++------ commit.h | 3 ++- sequencer.c | 7 ++++--- ...3-pre-commit-and-pre-merge-commit-hooks.sh | 17 ++++++++++++++-- 7 files changed, 56 insertions(+), 21 deletions(-)
Documentation/githooks.txt +10 -0
@@ -103,6 +103,8 @@ The default 'pre-commit' hook, when enabled--and with the `hooks.allownonascii` config option unset or set to false--prevents the use of non-ASCII filenames. +Hooks executed during 'pre-commit' will not be parallelized. + pre-merge-commit ~~~~~~~~~~~~~~~~
@@ -125,6 +127,8 @@ need to be resolved and the result committed separately (see linkgit:git-merge[1]). At that point, this hook will not be executed, but the 'pre-commit' hook will, if it is enabled. +Hooks executed during 'pre-merge-commit' will not be parallelized. + prepare-commit-msg ~~~~~~~~~~~~~~~~~~
@@ -150,6 +154,8 @@ be used as replacement for pre-commit hook. The sample `prepare-commit-msg` hook that comes with Git removes the help message found in the commented portion of the commit template. +Hooks executed during 'prepare-commit-msg' will not be parallelized. + commit-msg ~~~~~~~~~~
@@ -166,6 +172,8 @@ file. The default 'commit-msg' hook, when enabled, detects duplicate `Signed-off-by` trailers, and aborts the commit if one is found. +Hooks executed during 'commit-msg' will not be parallelized. + post-commit ~~~~~~~~~~~
@@ -175,6 +183,8 @@ invoked after a commit is made. This hook is meant primarily for notification, and cannot affect the outcome of `git commit`. +Hooks executed during 'post-commit' will run in parallel by default. + pre-rebase ~~~~~~~~~~
builtin/commit.c +6 -5
@@ -36,6 +36,7 @@#include "help.h" #include "commit-reach.h" #include "commit-graph.h" +#include "hook.h" static const char *const builtin_commit_usage[] = {N_ ("git commit [<options>] [--] <pathspec>..." ),
@@ -699,7 +700,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,/* This checks and barfs if author is badly specified */ determine_author_info (author_ident); -if (!no_verify &&run_commit_hook (use_editor, index_file,"pre-commit" ,NULL )) +if (!no_verify &&run_commit_hook (use_editor,0 , index_file,"pre-commit" ,NULL ))return 0 ;if (squash_message) {
@@ -983,7 +984,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" ,configured_hookdir_opt ())) {/* * 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
@@ -998,7 +999,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,0 , index_file,"prepare-commit-msg" ,git_path_commit_editmsg (), hook_arg1, hook_arg2,NULL ))return 0 ;
@@ -1015,7 +1016,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix, }if (!no_verify && -run_commit_hook (use_editor, index_file,"commit-msg" ,git_path_commit_editmsg (),NULL )) { +run_commit_hook (use_editor,0 , index_file,"commit-msg" ,git_path_commit_editmsg (),NULL )) {return 0 ; }
@@ -1701,7 +1702,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)repo_rerere (the_repository,0 );run_auto_maintenance (quiet); -run_commit_hook (use_editor,get_index_file (),"post-commit" ,NULL ); +run_commit_hook (use_editor,1 ,get_index_file (),"post-commit" ,NULL );if (amend && !no_post_rewrite) {commit_post_rewrite (the_repository, current_head, &oid); }
builtin/merge.c +5 -4
@@ -42,6 +42,7 @@#include "commit-reach.h" #include "wt-status.h" #include "commit-graph.h" +#include "hook.h" #define DEFAULT_TWOHEAD (1<<0)#define DEFAULT_OCTOPUS (1<<1)
@@ -836,14 +837,14 @@ static void prepare_to_commit(struct commit_list *remoteheads)struct strbuf msg =STRBUF_INIT ;const char *index_file =get_index_file (); -if (!no_verify &&run_commit_hook (0 < option_edit, index_file,"pre-merge-commit" ,NULL )) +if (!no_verify &&run_commit_hook (0 < option_edit,0 , index_file,"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 * the editor and after we invoke run_status above. */ -if (find_hook ("pre-merge-commit" )) +if (hook_exists ("pre-merge-commit" ,configured_hookdir_opt ()))discard_cache ();read_cache_from (index_file);strbuf_addbuf (&msg, &merge_msg);
@@ -864,7 +865,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)append_signoff (&msg,ignore_non_trailer (msg.buf, msg.len),0 );write_merge_heads (remoteheads);write_file_buf (git_path_merge_msg (the_repository), msg.buf, msg.len); -if (run_commit_hook (0 < option_edit,get_index_file (),"prepare-commit-msg" , +if (run_commit_hook (0 < option_edit,0 ,get_index_file (),"prepare-commit-msg" ,git_path_merge_msg (the_repository),"merge" ,NULL ))abort_commit (remoteheads,NULL );if (0 < option_edit) {
@@ -872,7 +873,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)abort_commit (remoteheads,NULL ); } -if (!no_verify &&run_commit_hook (0 < option_edit,get_index_file (), +if (!no_verify &&run_commit_hook (0 < option_edit,0 ,get_index_file (),"commit-msg" ,git_path_merge_msg (the_repository),NULL ))abort_commit (remoteheads,NULL );
commit.c +14 -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 **);
@@ -1630,25 +1631,32 @@ size_t ignore_non_trailer(const char *buf, size_t len)return boc ? len - boc : len - cutoff; } -int run_commit_hook (int editor_is_used,const char *index_file, +int run_commit_hook (int editor_is_used,int parallelize,const char *index_file,const char *name, ...) { -struct strvec hook_env =STRVEC_INIT ; +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT_SYNC ;va_list args; +const char *arg;int ret; -strvec_pushf (&hook_env,"GIT_INDEX_FILE=%s" , index_file); +if (parallelize) + opt.jobs =configured_hook_jobs (); + +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; }
commit.h +2 -1
@@ -352,6 +352,7 @@ int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)int compare_commits_by_gen_then_commit_date (const void *a_,const void *b_,void *unused);LAST_ARG_MUST_BE_NULL -int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...); +int run_commit_hook(int editor_is_used, int parallelize, const char *index_file, +const char *name, ...);#endif /* COMMIT_H */
sequencer.c +4 -3
@@ -34,6 +34,7 @@#include "commit-reach.h" #include "rebase-interactive.h" #include "reset.h" +#include "hook.h" #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
@@ -1203,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 ,0 , r->index_file,"prepare-commit-msg" , name, arg1, arg2,NULL )) ret =error (_ ("'prepare-commit-msg' hook failed" ));
@@ -1438,7 +1439,7 @@ static int try_to_commit(struct repository *r, } } -if (find_hook ("prepare-commit-msg" )) { +if (hook_exists ("prepare-commit-msg" ,configured_hookdir_opt ())) { res =run_prepare_commit_msg_hook (r, msg, hook_commit);if (res) goto out;
@@ -1528,7 +1529,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 ,1 , r->index_file,"post-commit" ,NULL );if (flags &AMEND_MSG )commit_post_rewrite (r, current_head, oid);
t/t7503-pre-commit-and-pre-merge-commit-hooks.sh +15 -2
@@ -5,8 +5,8 @@ test_description='pre-commit and pre-merge-commit hooks' . ./test-lib.sh HOOKDIR="$(git rev-parse --git-dir)/hooks" -PRECOMMIT="$HOOKDIR/pre-commit" -PREMERGE="$HOOKDIR/pre-merge-commit" +PRECOMMIT="$(pwd)/$HOOKDIR/pre-commit" +PREMERGE="$(pwd)/$HOOKDIR/pre-merge-commit" # Prepare sample scripts that write their $0 to actual_hooks test_expect_success 'sample script setup' '
@@ -103,6 +103,19 @@ test_expect_success 'with succeeding hook' ' test_cmp expected_hooks actual_hooks ' +# NEEDSWORK: when 'git hook add' and 'git hook remove' have been added, use that +# instead +test_expect_success 'with succeeding hook (config-based)' ' + test_when_finished "git config --unset hook.pre-commit.command success.sample" && + test_when_finished "rm -f expected_hooks actual_hooks" && + git config hook.pre-commit.command "$HOOKDIR/success.sample" && + echo "$HOOKDIR/success.sample" >expected_hooks && + echo "more" >>file && + git add file && + git commit -m "more" && + test_cmp expected_hooks actual_hooks +' + test_expect_success 'with succeeding hook (merge)' ' test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" && cp "$HOOKDIR/success.sample" "$PREMERGE" &&
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
Re: [PATCH v3 01/17] commit: use config-based hooks
Quoted 2 lines · Emily Shaffer
Because all but 'post-commit' hooks are expected to make some state change, force all but 'post-commit' hook to run in series.
OK. And a sequence of hooks that run in series are expected to use the output from the previous one as its input?
Quoted 3 lines
'post-commit'
"is meant primarily for notification, and cannot affect the outcome ofgit commit," so it is fine to run in parallel.
Makes sense.
Re: [PATCH v3 01/17] commit: use config-based hooks
Quoted 8 lines · Junio C Hamano
Quoted 2 lines · Emily Shaffer
Because all but 'post-commit' hooks are expected to make some state change, force all but 'post-commit' hook to run in series.
OK. And a sequence of hooks that run in series are expected to use the output from the previous one as its input?
Kind of, but phrasing it that way is a little odd (to me). The commit hooks take as input the path to a tempfile which holds the proposed commit message, and they modify the contents of that file. They don't make output via stdout or receive the commit message via stdin. Most hooks work this way, I think with the sole exception of 'proc-receive', which is implemented weirdly (I said more about it when you asked about it in your part I review, here:
https://lore.kernel.org/git/[email protected]).
I'm not sure how to phrase it better. If I were writing a hook I imagine the combination of taking a filename as an argument and the note about not running these hooks in parallel would be pretty self-evident. Hmmm. Maybe instead of the copy/pasted warning about "will not be parallelized" I could write:
Hooks executed during prepare-commit-msg will not be parallelized, because the file provided in the first argument is expected to be edited by each hook.
Quoted 7 lines
Quoted 4 lines
'post-commit'
"is meant primarily for notification, and cannot affect the outcome ofgit commit," so it is fine to run in parallel.Makes sense.
Re: [PATCH v3 01/17] commit: use config-based hooks
Quoted 5 lines · Emily Shaffer
parallelized" I could write:
Hooks executed during prepare-commit-msg will not be parallelized, because the file provided in the first argument is expected to be edited by each hook.
Yeah. It's been quite a while since I wrote the message you are responding to, and it took me a while to get the context back in my head, but the above looks reasonable to me. We could tweak it a bit to say "the file ... serves as an input to be modified in place by each hook" to make it even clearer, but I think yours is clear enough.
Thanks.
Re: [PATCH v3 01/17] commit: use config-based hooks
In reply to Emily ShafferDocumentation/githooks.txt invoked after a commit is made. · line 186
Quoted 5 lines · Emily Shaffer
Documentation/githooks.txt +1
-0@@ -175,6 +183,8 @@ invoked after a commit is made. This hook is meant primarily for notification, and cannot affect the outcome of `git commit`. +Hooks executed during 'post-commit' will run in parallel by default.
The "by default" makes it sound as if there is a way to force 'post-commit' alone serially, but other than hook.jobs that can be used to uniformly make everything run serially, I didn't find a way to do so.
The comment applies to all the "by default" in this latter half of the series.
Thanks.
Re: [PATCH v3 01/17] commit: use config-based hooks
Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Emily Shaffer
Documentation/githooks.txt +0
-0@@ -175,6 +183,8 @@ invoked after a commit is made. This hook is meant primarily for notification, and cannot affect the outcome of `git commit`.
Quoted 6 lines
Documentation/githooks.txt invoked after a commit is made. · line 186
Quoted 1 line
Documentation/githooks.txt +1
-0+Hooks executed during 'post-commit' will run in parallel by default.The "by default" makes it sound as if there is a way to force 'post-commit' alone serially, but other than hook.jobs that can be used to uniformly make everything run serially, I didn't find a way to do so.
Yeah, hook.jobs is the way to make it run serially, and as you say, it can't be applied everywhere. I'll try to change the language.
Quoted 5 lines
The comment applies to all the "by default" in this latter half of the series.
Thanks.
PATCH v3 2/17 am: convert applypatch hooks to use config
In reply to Emily ShafferTeach pre-applypatch, post-applypatch, and applypatch-msg to use the hook.h library instead of the run-command.h library. This enables use of hooks specified in the config, in addition to those in the hookdir. These three hooks are called only by builtin/am.c.
--- Documentation/githooks.txt | 6 ++++++ builtin/am.c | 12 +++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-)
Documentation/githooks.txt +6 -0
@@ -58,6 +58,8 @@ the message file. The default 'applypatch-msg' hook, when enabled, runs the 'commit-msg' hook, if the latter is enabled. +Hooks run during 'applypatch-msg' will not be parallelized. + pre-applypatch ~~~~~~~~~~~~~~
@@ -73,6 +75,8 @@ make a commit if it does not pass certain test. The default 'pre-applypatch' hook, when enabled, runs the 'pre-commit' hook, if the latter is enabled. +Hooks run during 'pre-applypatch' will be run in parallel by default. + post-applypatch ~~~~~~~~~~~~~~~
@@ -82,6 +86,8 @@ and is invoked after the patch is applied and a commit is made. This hook is meant primarily for notification, and cannot affect the outcome of `git am`. +Hooks run during 'post-applypatch' will be run in parallel by default. + pre-commit ~~~~~~~~~~
builtin/am.c +9 -3
@@ -33,6 +33,7 @@#include "string-list.h" #include "packfile.h" #include "repository.h" +#include "hook.h" /** * Returns the length of the first line of msg.
@@ -426,9 +427,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_SYNC ;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);
@@ -1558,8 +1562,9 @@ 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 =RUN_HOOKS_OPT_INIT_ASYNC ; -if (run_hook_le (NULL ,"pre-applypatch" ,NULL )) +if (run_hooks ("pre-applypatch" , &hook_opt))exit (1 );if (write_cache_as_tree (&tree,0 ,NULL ))
@@ -1611,8 +1616,9 @@ static void do_commit(const struct am_state *state)fclose (fp); } -run_hook_le (NULL ,"post-applypatch" ,NULL ); +run_hooks ("post-applypatch" , &hook_opt); +run_hooks_opt_clear (&hook_opt);strbuf_release (&sb); }
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
Re: [PATCH v3 02/17] am: convert applypatch hooks to use config
Quoted 4 lines · Emily Shaffer
Teach pre-applypatch, post-applypatch, and applypatch-msg to use the
hook.hlibrary instead of therun-command.hlibrary. This enables use of hooks specified in the config, in addition to those in the hookdir. These three hooks are called only bybuiltin/am.c.
I was interested in the applypatch-msg the most; it gets fed the "final-commit" file as its input and expected to modify it in place, so if you allow two or more to work in series, you do not have to anything extra (like swapping the output from the previous step to the input for the next step), which makes this a relatively easy conversion.
PATCH v3 3/17 merge: use config-based hooks for post-merge hook
In reply to Emily ShafferTeach 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.
--- Documentation/githooks.txt | 2 ++ builtin/merge.c | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-)
Documentation/githooks.txt +2 -0
@@ -236,6 +236,8 @@ save and restore any form of metadata associated with the working tree (e.g.: permissions/ownership, ACLS, etc). See contrib/hooks/setgitperms.perl for an example of how to do this. +Hooks executed during 'post-merge' will run in parallel by default. + pre-push ~~~~~~~~
builtin/merge.c +4 -1
@@ -443,6 +443,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_ASYNC ;const struct object_id *head = &head_commit->object.oid;if (!msg)
@@ -484,7 +485,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);
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH v3 4/17 gc: use hook library for pre-auto-gc hook
In reply to Emily ShafferUsing 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.
--- Documentation/githooks.txt | 2 ++ builtin/gc.c | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-)
Documentation/githooks.txt +2 -0
@@ -553,6 +553,8 @@ This hook is invoked by `git gc --auto` (see linkgit:git-gc[1]). It takes no parameter, and exiting with non-zero status from this script causes the `git gc --auto` to abort. +Hooks run during 'pre-auto-gc' will be run in parallel by default. + post-rewrite ~~~~~~~~~~~~
builtin/gc.c +3 -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"
@@ -340,6 +341,7 @@ static void add_repack_incremental_option(void)static int need_to_gc (void ) { +struct run_hooks_opt hook_opt =RUN_HOOKS_OPT_INIT_ASYNC ;/* * Setting gc.auto to 0 or negative can disable the * automatic gc.
@@ -386,7 +388,7 @@ 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))return 0 ;return 1 ; }
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH v3 5/17 rebase: teach pre-rebase to use hook.h
In reply to Emily ShafferBy using hook.h instead of run-command.h to run hooks, pre-rebase hooks can now be specified in the config as well as in the hookdir. pre-rebase is not called anywhere besides builtin/rebase.c.
--- Documentation/githooks.txt | 2 ++ builtin/rebase.c | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-)
Documentation/githooks.txt +2 -0
@@ -200,6 +200,8 @@ two parameters. The first parameter is the upstream from which the series was forked. The second parameter is the branch being rebased, and is not set when rebasing the current branch. +Hooks executed during 'pre-rebase' will run in parallel by default. + post-checkout ~~~~~~~~~~~~~
builtin/rebase.c +5 -2
@@ -28,6 +28,7 @@#include "sequencer.h" #include "rebase-interactive.h" #include "reset.h" +#include "hook.h" #define DEFAULT_REFLOG_ACTION "rebase"
@@ -1312,6 +1313,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_ASYNC ;struct option builtin_rebase_options[] = { OPT_STRING(0, "onto", &options.onto_name, N_("revision"),
@@ -2024,9 +2026,9 @@ 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))die (_ ("The pre-rebase hook refused to rebase." ));if (options.flags &REBASE_DIFFSTAT ) {
@@ -2106,6 +2108,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) ret = !!run_specific_rebase (&options, action); cleanup: +run_hooks_opt_clear (&hook_opt);strbuf_release (&buf);strbuf_release (&revisions);free (options.head_name);
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH v3 6/17 read-cache: convert post-index-change hook to use config
In reply to Emily ShafferBy 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.
--- Documentation/githooks.txt | 2 ++ read-cache.c | 12 +++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-)
Documentation/githooks.txt +2 -0
@@ -711,6 +711,8 @@ 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. +Hooks run during 'post-index-change' will be run in parallel by default. + GIT --- Part of the linkgit:git[1] suite
read-cache.c +9 -3
@@ -25,6 +25,7 @@#include "fsmonitor.h" #include "thread-utils.h" #include "progress.h" +#include "hook.h" /* Mask for the name length in ce_flags in the on-disk index */
@@ -3052,6 +3053,7 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l unsigned flags) {int ret; +struct run_hooks_opt hook_opt =RUN_HOOKS_OPT_INIT_ASYNC ;/* * TODO trace2: replace "the_repository" with the actual repo instance
@@ -3070,9 +3072,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 ;
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH v3 7/17 receive-pack: convert push-to-checkout hook to hook.h
In reply to Emily ShafferBy 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.
--- Documentation/githooks.txt | 1 + builtin/receive-pack.c | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-)
Documentation/githooks.txt +1 -0
@@ -547,6 +547,7 @@ that switches branches while keeping the local changes in the working tree that do not interfere with the difference between the branches. +Hooks executed during 'push-to-checkout' will not be parallelized. pre-auto-gc ~~~~~~~~~~~
builtin/receive-pack.c +11 -4
@@ -29,6 +29,7 @@#include "commit-reach.h" #include "worktree.h" #include "shallow.h" +#include "hook.h" static const char *const receive_pack_usage[] = {N_ ("git receive-pack <git-dir>" ),
@@ -1435,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_SYNC; +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)
@@ -1464,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,configured_hookdir_opt ())) retval =push_to_deploy (sha1, &env, work_tree);else retval =push_to_checkout (sha1, &env, work_tree);
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH v3 8/17 git-p4: use 'git hook' to run hooks
In reply to Emily ShafferInstead of duplicating the behavior of run-command.h:run_hook_le() in Python, we can directly call 'git hook run'. As a bonus, this means git-p4 learns how to find hook specifications from the Git config as well as from the hookdir.
---
Notes:
Maybe there is a better way to do this - I had a hard time getting this to run locally, and Python is not my forte, so if anybody has a better approach I'd love to just take that patch instead :)
Since v6, removed the developer debug print statements.... :X
Maybe there is a better way to do this - I had a hard time getting this to run locally, and Python is not my forte, so if anybody has a better approach I'd love to just take that patch instead :)
git-p4.py | 67 +++++-------------------------------------------------- 1 file changed, 6 insertions(+), 61 deletions(-)
git-p4.py +6 -61
@@ -208,70 +208,15 @@ def decode_path(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): + if not cmd: 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'] + + ["-a" + arg for arg in param] + + [cmd]) + return subprocess.call(args) == 0 def write_pipe(c, stdin): if verbose:
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH v3 9/17 hooks: convert 'post-checkout' hook to hook library
In reply to Emily ShafferBy using the 'hook.h' library, 'post-checkout' hooks can now be specified in the config as well as in the hook directory.
--- Documentation/githooks.txt | 2 ++ builtin/checkout.c | 16 +++++++++++----- builtin/clone.c | 7 +++++-- builtin/worktree.c | 30 ++++++++++++++---------------- reset.c | 15 +++++++++++---- 5 files changed, 43 insertions(+), 27 deletions(-)
Documentation/githooks.txt +2 -0
@@ -224,6 +224,8 @@ This hook can be used to perform repository validity checks, auto-display differences from the previous HEAD if different, or set working dir metadata properties. +Hooks executed during 'post-checkout' will not be parallelized. + post-merge ~~~~~~~~~~
builtin/checkout.c +11 -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"
@@ -104,13 +105,18 @@ 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_SYNC ; +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:
@@ -771,6 +772,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_SYNC ;if (option_no_checkout)return 0 ;
@@ -816,8 +818,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
@@ -12,6 +12,7 @@#include "submodule.h" #include "utf8.h" #include "worktree.h" +#include "hook.h" static const char *const worktree_usage[] = {N_ ("git worktree add [<options>] <path> [<commit-ish>]" ),
@@ -455,22 +456,19 @@ 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_SYNC ; + +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; + + ret =run_hooks ("post-checkout" , &opt); + +run_hooks_opt_clear (&opt); }strvec_clear (&child_env);
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_SYNC ; +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.28.0.rc0.142.g3c755180ce-goog
PATCH v3 10/17 hook: convert 'post-rewrite' hook to config
In reply to Emily ShafferBy using 'hook.h' for 'post-rewrite', we simplify hook invocations by not needing to put together our own 'struct child_process' and we also learn to run hooks specified in the config as well as the hook dir.
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.
--- Documentation/githooks.txt | 2 + builtin/am.c | 18 +++------ sequencer.c | 76 +++++++++++++++----------------------- 3 files changed, 36 insertions(+), 60 deletions(-)
Documentation/githooks.txt +2 -0
@@ -585,6 +585,8 @@ The hook always runs after the automatic note copying (see "notes.rewrite.<command>" in linkgit:git-config[1]) has happened, and thus has access to these notes. +Hooks run during 'post-rewrite' will be run in parallel by default. + The following command-specific comments apply: rebase::
builtin/am.c +5 -13
@@ -449,23 +449,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_ASYNC ;int ret; -if (!hook) -return 0 ; - -strvec_push (&cp.args, hook); -strvec_push (&cp.args,"rebase" ); +strvec_push (&opt.args,"rebase" ); + opt.path_to_stdin =am_path (state,"rewritten" ); - cp.in =xopen (am_path (state,"rewritten" ),O_RDONLY ); - cp.stdout_to_stderr =1 ; - cp.trace2_hook_name ="post-rewrite" ; + ret =run_hooks ("post-rewrite" , &opt); - ret =run_command (&cp); - -close (cp.in); +run_hooks_opt_clear (&opt);return ret; }
sequencer.c +29 -47
98 lines not on this page · raw message
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH v3 11/17 transport: convert pre-push hook to use config
In reply to Emily ShafferBy using the hook.h:run_hooks API, pre-push hooks can be specified in the config as well as in the hookdir.
--- Documentation/githooks.txt | 2 ++ transport.c | 55 +++++++++----------------------------- 2 files changed, 14 insertions(+), 43 deletions(-)
Documentation/githooks.txt +2 -0
@@ -271,6 +271,8 @@ If this hook exits with a non-zero status, `git push` will abort without pushing anything. Information about why the push is rejected may be sent to the user by writing to standard error. +Hooks executed during 'pre-push' will run in parallel by default. + [[pre-receive]] pre-receive ~~~~~~~~~~~
transport.c +12 -43
79 lines not on this page · raw message
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH v3 12/17 reference-transaction: look for hooks in config
In reply to Emily ShafferBy using the hook.h library, reference-transaction hooks can be specified in the config instead.
The expected output of the test is not fully updated to reflect the absolute path of the hook called because the 'update' hook has not yet been converted to use hook.h.
--- Documentation/githooks.txt | 2 ++ refs.c | 38 +++++++++----------------------- t/t1416-ref-transaction-hooks.sh | 8 +++---- 3 files changed, 17 insertions(+), 31 deletions(-)
Documentation/githooks.txt +2 -0
@@ -521,6 +521,8 @@ The exit status of the hook is ignored for any state except for the cause the transaction to be aborted. The hook will not be called with "aborted" state in that case. +Hooks run during 'reference-transaction' will be run in parallel by default. + push-to-checkout ~~~~~~~~~~~~~~~~
refs.c +11 -27
@@ -18,6 +18,7 @@#include "strvec.h" #include "repository.h" #include "sigchain.h" +#include "hook.h" /* * List of all available backends
@@ -1957,47 +1958,30 @@ 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_ASYNC ;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" ,configured_hookdir_opt ()))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 (&opt.str_stdin, buf.buf); } -close (proc.in); -sigchain_pop (SIGPIPE ); + ret =run_hooks ("reference-transaction" , &opt); +run_hooks_opt_clear (&opt);strbuf_release (&buf); - ret |=finish_command (&proc);return ret; }
t/t1416-ref-transaction-hooks.sh +4 -4
@@ -122,11 +122,11 @@ test_expect_success 'interleaving hook calls succeed' ' cat >expect <<-EOF && hooks/update refs/tags/PRE $ZERO_OID $PRE_OID - hooks/reference-transaction prepared - hooks/reference-transaction committed + $(pwd)/target-repo.git/hooks/reference-transaction prepared + $(pwd)/target-repo.git/hooks/reference-transaction committed hooks/update refs/tags/POST $ZERO_OID $POST_OID - hooks/reference-transaction prepared - hooks/reference-transaction committed + $(pwd)/target-repo.git/hooks/reference-transaction prepared + $(pwd)/target-repo.git/hooks/reference-transaction committed EOF git push ./target-repo.git PRE POST &&
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH v3 13/17 receive-pack: convert 'update' hook to hook.h
In reply to Emily ShafferBy using hook.h to invoke the 'update' hook, now hooks can be specified in the config in addition to the hookdir.
--- Documentation/githooks.txt | 2 + builtin/receive-pack.c | 65 +++++++++++++++++++++----------- t/t1416-ref-transaction-hooks.sh | 4 +- 3 files changed, 48 insertions(+), 23 deletions(-)
Documentation/githooks.txt +2 -0
@@ -359,6 +359,8 @@ The default 'update' hook, when enabled--and with `hooks.allowunannotated` config option unset or set to false--prevents unannotated tags to be pushed. +Hooks executed during 'update' are run in parallel by default. + [[proc-receive]] proc-receive ~~~~~~~~~~~~
builtin/receive-pack.c +44 -21
77 lines not on this page · raw message
t/t1416-ref-transaction-hooks.sh +2 -2
@@ -121,10 +121,10 @@ test_expect_success 'interleaving hook calls succeed' ' EOF cat >expect <<-EOF && - hooks/update refs/tags/PRE $ZERO_OID $PRE_OID + $(pwd)/target-repo.git/hooks/update refs/tags/PRE $ZERO_OID $PRE_OID $(pwd)/target-repo.git/hooks/reference-transaction prepared $(pwd)/target-repo.git/hooks/reference-transaction committed - hooks/update refs/tags/POST $ZERO_OID $POST_OID + $(pwd)/target-repo.git/hooks/update refs/tags/POST $ZERO_OID $POST_OID $(pwd)/target-repo.git/hooks/reference-transaction prepared $(pwd)/target-repo.git/hooks/reference-transaction committed EOF
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH v3 14/17 proc-receive: acquire hook list from hook.h
In reply to Emily ShafferThe proc-receive hook differs from most other hooks Git invokes because the hook and the parent Git process engage in bidirectional communication via stdin/stdout. This bidirectional communication is unsuitable for multiple hooks, whether they are in series or in parallel, and is incompatible with run-command.h:run_processes_parallel:
- The proc-receive hook is intended to modify the state of the Git repo. From 'git help githooks':
This [proc-receive] hook is responsible for updating the relevant references and reporting the results back to 'receive-pack'.
This prevents parallelization and implies, at least, specific ordering of hook execution.
- The proc-receive hook can reject a push by aborting early with an error code. If a former hook ran through the entire push contents successfully but a later hook rejects some of the push, the repo may be left in a partially-updated (and corrupt) state.
- The callback model of the run_processes_parallel() API is unsuited to the current implementation of proc-receive, which loops through "send-receive-consider" with the child process. proc-receive today relies on stateful communication with the child process, which would be unwieldy to implement with callbacks and saved state.
- Additionally, run_processes_parallel() is designed to collate the output of many child processes into a single output (stderr or callback), and would require significant work to tell the caller which process sent the output, and indeed to collect any output before the child process has exited.
So, rather than using hook.h:run_hooks() to invoke the proc-receive hook, receive-pack.c can learn to ask hook.h:hook_list() for the location of a hook to run. This allows users to configure their proc-receive in a global config for all repos if they want, or a local config if they just don't want to use the hookdir. Because running more than one proc-receive hook doesn't make sense from a repo state perspective, we can explicitly ban configuring more than one proc-receive hook at a time.
If a user wants to globally configure one proc-receive hook for most of their repos, but override that hook in a single repo, they should use 'skip' to manually remove the global hook in their special repo:
~/.gitconfig:
[hook.proc-receive]
command = /usr/bin/usual-proc-receive
~/special-repo/.git/config:
[hookcmd./usr/bin/usual-proc-receive]
skip = true
[hook.proc-receive]
command = /usr/bin/special-proc-receive
--- Documentation/githooks.txt | 4 ++ builtin/receive-pack.c | 33 +++++++++++++++- t/t5411/test-0015-too-many-hooks-error.sh | 47 +++++++++++++++++++++++ 3 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 t/t5411/test-0015-too-many-hooks-error.sh
Documentation/githooks.txt +4 -0
@@ -423,6 +423,10 @@ the input. The exit status of the 'proc-receive' hook only determines the success or failure of the group of commands sent to it, unless atomic push is in use. +It is forbidden to specify more than one hook for 'proc-receive'. If a +globally-configured 'proc-receive' must be overridden, use +'hookcmd.<global-hook>.skip = true' to ignore it. + [[post-receive]] post-receive ~~~~~~~~~~~~
builtin/receive-pack.c +31 -2
@@ -1145,11 +1145,40 @@ static int run_proc_receive_hook(struct command *commands,int version =0 ;int code; - argv[0 ] =find_hook ("proc-receive" ); -if (!argv[0 ]) { +struct strbuf hookname =STRBUF_INIT ; +struct hook *proc_receive =NULL ; +struct list_head *pos, *hooks; + +strbuf_addstr (&hookname,"proc-receive" ); + hooks =hook_list (&hookname); + +list_for_each (pos, hooks) { +if (proc_receive) { +rp_error ("only one 'proc-receive' hook can be specified" ); +return -1 ; + } + proc_receive = list_entry(pos, struct hook, list); +/* check if the hookdir hook should be ignored */ +if (proc_receive->from_hookdir) { +switch (configured_hookdir_opt ()) { +case HOOKDIR_INTERACTIVE : +case HOOKDIR_NO : + proc_receive =NULL ; +break ; +default : +break ; + } + } + + } + +if (!proc_receive) {rp_error ("cannot find hook 'proc-receive'" );return -1 ; } + + + argv[0 ] = proc_receive->command.buf; argv[1 ] =NULL ; proc.argv = argv;
t/t5411/test-0015-too-many-hooks-error.sh new file +47 -0
47 lines not on this page · raw message
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH v3 15/17 post-update: use hook.h library
In reply to Emily ShafferBy using run_hooks() instead of run_hook_le(), 'post-update' hooks can be specified in the config as well as the hookdir.
--- Documentation/githooks.txt | 2 ++ builtin/receive-pack.c | 26 +++++++------------------- 2 files changed, 9 insertions(+), 19 deletions(-)
Documentation/githooks.txt +2 -0
@@ -498,6 +498,8 @@ Both standard output and standard error output are forwarded to `git send-pack` on the other end, so you can simply `echo` messages for the user. +Hooks run during 'post-update' will be run in parallel by default. + reference-transaction ~~~~~~~~~~~~~~~~~~~~~
builtin/receive-pack.c +7 -19
@@ -1686,33 +1686,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_ASYNC ;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.28.0.rc0.142.g3c755180ce-goog
PATCH v3 16/17 receive-pack: convert receive hooks to hook.h
In reply to Emily ShafferBy using the hook.h library to run receive hooks, they can be specified in the config as well as in the hookdir.
--- Documentation/githooks.txt | 4 + builtin/receive-pack.c | 197 +++++++++++++++++-------------------- 2 files changed, 94 insertions(+), 107 deletions(-)
Documentation/githooks.txt +4 -0
@@ -314,6 +314,8 @@ will be set to zero, `GIT_PUSH_OPTION_COUNT=0`. See the section on "Quarantine Environment" in linkgit:git-receive-pack[1] for some caveats. +Hooks executed during 'pre-receive' will not be parallelized. + [[update]] update ~~~~~~
@@ -466,6 +468,8 @@ environment variables will not be set. If the client selects to use push options, but doesn't transmit any, the count variable will be set to zero, `GIT_PUSH_OPTION_COUNT=0`. +Hooks executed during 'post-receive' are run in parallel by default. + [[post-update]] post-update ~~~~~~~~~~~
builtin/receive-pack.c +90 -107
254 lines not on this page · raw message
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
PATCH v3 17/17 run-command: stop thinking about hooks
In reply to Emily Shafferhook.h has replaced all run-command.h hook-related functionality. run-command.h:run_hooks_le/ve and find_hook are no longer used anywhere in the codebase. So, let's delete the dead code - or, in the one case where it's still needed, move it to an internal function in hook.c.
--- hook.c | 39 ++++++++++++++++++++++++++++-- run-command.c | 66 --------------------------------------------------- run-command.h | 24 ------------------- 3 files changed, 37 insertions(+), 92 deletions(-)
hook.c +37 -2
@@ -196,6 +196,41 @@ static int should_include_hookdir(const char *path, enum hookdir_opt cfg) } } +static const char *find_legacy_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; +} + +struct list_head *hook_list (const struct strbuf * hookname) {struct strbuf hook_key =STRBUF_INIT ;
@@ -213,7 +248,7 @@ struct list_head* hook_list(const struct strbuf* hookname)git_config (hook_config_lookup, (void *)&cb_data);if (have_git_dir ()) - legacy_hook_path =find_hook (hookname->buf); + legacy_hook_path =find_legacy_hook (hookname->buf);/* Unconditionally add legacy hook, but annotate it. */ if (legacy_hook_path) {
@@ -244,7 +279,7 @@ int hook_exists(const char *hookname, enum hookdir_opt should_run_hookdir)int could_run_hookdir = (should_run_hookdir ==HOOKDIR_INTERACTIVE || should_run_hookdir ==HOOKDIR_WARN || should_run_hookdir ==HOOKDIR_YES ) - && !!find_hook (hookname); + && !!find_legacy_hook (hookname);strbuf_addf (&hook_key,"hook.%s.command" , hookname);
run-command.c +0 -66
72 lines not on this page · raw message
run-command.h +0 -24
@@ -194,30 +194,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 -* 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, ...); -int run_hook_ve (const char *const *env,const char *name,va_list args); -/* * Trigger an auto-gc */
Signature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
[PATCH v3 18/17] doc: make git-hook.txt point of truth
In reply to Emily ShafferBy showing the list of all hooks in 'git help hook' for users to refer to, 'git help hook' becomes a one-stop shop for hook authorship. Since some may still have muscle memory for 'git help githooks', though, reference the 'git hook' commands and otherwise don't remove content.
---
Sorry for the wonky subject. It seemed unnecessary to send the entirety of the topic again when there were no changes. (I was able to push this patch to my fork without -f, so indeed the rest is unchanged.)
I'd really prefer if 'git help githooks' opens the 'git-hook.txt' manpage, but I couldn't figure out how to do that. This seemed like the next best thing to me - users can still find information at the old manpage, but get a little nudge towards the new manpage in case they didn't notice it.
Extremely open to other notes about the direction of these docs; I'm not confident in anything except that it'd be annoying to have 'git help githooks' remain unchanged.
- Emily
Documentation/git-hook.txt | 5 + Documentation/githooks.txt | 701 +------------------------------------ 2 files changed, 13 insertions(+), 693 deletions(-)
Documentation/git-hook.txt +5 -0
@@ -112,6 +112,11 @@ CONFIGURATION ------------- include::config/hook.txt[] +HOOKS +----- + +include::native-hooks.txt[] + GIT --- Part of the linkgit:git[1] suite
Documentation/githooks.txt +8 -693
720 lines not on this page · raw message
Signature · 1 line
2.29.2.490.gc7ae633391
Re: [PATCH v3 18/17] doc: make git-hook.txt point of truth
Quoted 13 lines · Emily Shaffer
By showing the list of all hooks in 'git help hook' for users to refer to, 'git help hook' becomes a one-stop shop for hook authorship. Since some may still have muscle memory for 'git help githooks', though, reference the 'git hook' commands and otherwise don't remove content.
Signed-off-by Emily Shaffer <[email protected]>---
Sorry for the wonky subject. It seemed unnecessary to send the entirety of the topic again when there were no changes. (I was able to push this patch to my fork without -f, so indeed the rest is unchanged.)
Quoted 8 lines
I'd really prefer if 'git help githooks' opens the 'git-hook.txt' manpage, but I couldn't figure out how to do that. This seemed like the next best thing to me - users can still find information at the old manpage, but get a little nudge towards the new manpage in case they didn't notice it.
Extremely open to other notes about the direction of these docs; I'm not confident in anything except that it'd be annoying to have 'git help githooks' remain unchanged.
Even now, this is missing the native-hooks.txt from the commit. Ugh. This commit breaks the build; please do not add it to the topic. I will include a fixed version with the next reroll. Sorry.
That said, the intent is clear enough for me to still feel comfortable inviting review.
- Emily
Re: [PATCH v3 00/17] use config-based hooks (config-based hooks part II)
In reply to Emily ShafferQuoted 9 lines · Emily Shaffer
Another thing I wanted to do in this series but ended up not having time for before the holidays was to figure out a way to consolidate
Documentation/githooks.txtandDocumentation/git-hook.txt. My personal preference would be to remove githooks.txt's contents, move the "Hooks" header from there into git-hook.txt, and have 'git help githooks'/'git help hooks' redirect to git-hook.txt; I don't have a patch to share here because I ran out of time before vacation :) What do others envision the documentation looking like?
I could use some tips on this, actually. I don't see anywhere that we make one document redirect to another, or else I don't know what to look for. If I replace the contents of githooks.txt with include::git-hook.txt[], generate-cmdlist.sh chokes because there is no name to grep for in githooks.txt. Is there a documented example of a manpage redirecting to a different manpage?
- Emily
Re: [PATCH v3 00/17] use config-based hooks (config-based hooks part II)
In reply to Emily ShafferQuoted 59 lines · Emily Shaffer
Since v2:
- Renamed 'master' to 'main' in new t5411 (proc-receive) test.
- Removed some accidentally included debug strings.
- Fixed a nasty bug in the reference-transaction hook conversion where calling 'oid_to_hex()' would invalidate references to the output of earlier 'oid_to_hex()' runs farther up the callstack. Instead, the hook callsite now uses 'oid_to_hex_r()'.Another thing I wanted to do in this series but ended up not having time for before the holidays was to figure out a way to consolidate
Documentation/githooks.txtandDocumentation/git-hook.txt. My personal preference would be to remove githooks.txt's contents, move the "Hooks" header from there into git-hook.txt, and have 'git help githooks'/'git help hooks' redirect to git-hook.txt; I don't have a patch to share here because I ran out of time before vacation :) What do others envision the documentation looking like?Thanks!
- EmilyCI run: https://github.com/nasamuffin/git/actions/runs/436905873
Emily Shaffer (17):
commit: use config-based hooks
am: convert applypatch hooks to use config
merge: use config-based hooks for post-merge hook
gc: use hook library for pre-auto-gc hook
rebase: teach pre-rebase to usehook.h
read-cache: convert post-index-change hook to use config
receive-pack: convert push-to-checkout hook tohook.h
git-p4: use 'git hook' to run hooks
hooks: convert 'post-checkout' hook to hook library
hook: convert 'post-rewrite' hook to config
transport: convert pre-push hook to use config
reference-transaction: look for hooks in config
receive-pack: convert 'update' hook tohook.h
proc-receive: acquire hook list fromhook.h
post-update: usehook.hlibrary
receive-pack: convert receive hooks tohook.h
run-command: stop thinking about hooksDocumentation/githooks.txt | 45 +++ builtin/am.c | 30 +- builtin/checkout.c | 16 +- builtin/clone.c | 7 +- builtin/commit.c | 11 +- builtin/gc.c | 4 +- builtin/merge.c | 14 +- builtin/rebase.c | 7 +- builtin/receive-pack.c | 326 ++++++++++-------- builtin/worktree.c | 30 +- commit.c | 20 +- commit.h | 3 +- git-p4.py | 67 +--- hook.c | 39 ++- read-cache.c | 12 +- refs.c | 38 +- reset.c | 15 +- run-command.c | 66 ----
Quoted 12 lines
run-command.h | 24 -- sequencer.c | 83 ++--- t/t1416-ref-transaction-hooks.sh | 12 +- t/t5411/test-0015-too-many-hooks-error.sh | 47 +++ ...3-pre-commit-and-pre-merge-commit-hooks.sh | 17 +- transport.c | 55 +-- 24 files changed, 493 insertions(+), 495 deletions(-) create mode 100644 t/t5411/test-0015-too-many-hooks-error.shSignature · 1 line
2.28.0.rc0.142.g3c755180ce-goog
My only complaints on the previous version of this series have now been resolved, so LGTM.
Quoted 8 lines · Emily Shaffer
As part of the adoption of config-based hooks, teach
run_commit_hook()to callhook.hinstead ofrun-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.Signed-off-by Emily Shaffer <[email protected]>---
Apart from the minor issues I noted on patches 8 & 10, this looks good to me.