New execute-commands hook for centralized workflow
Subsystems (2)
cover New execute-commands hook for centralized workflow
In my planned speech at the Git Merge 2020 conference, this series of patches is a core part of AGit-Flow (a centralized workflow like Gerrit). Due to a coronavirus outbreak in China, I was unable to attend the meeting. I wrote a blog "AGit-Flow and git-repo" based on my planned speech, see:
https://git-repo.info/en/2020/03/agit-flow-and-git-repo/
Git calls an internal execute_commands function to handle commands sent from the client to git-receive-pack. Regardless of what references the user pushes, git creates or updates the corresponding references if the user has write permission. A contributor who has no write permission cannot push to repository directly. So, the contributor has to write commits to an alternate location and sends pull request by emails or by other ways. We call this distributed workflow.
It would be more convenient to work in a centralized workflow like what Gerrit provided for some cases. For example, a read-only user may run the following git push command to push commits to a special reference to create a code review, instead of updating a reference directly.
git push -o reviewers=user1,user2 \
-o oldoid=89c082363ac950d224a7259bfba3ccfbf4c560c4 \
origin \
HEAD:refs/for/<branch-name>/<session>The <branch-name> in the above example can be as simple as "master", or a more complicated branch name like "foo/bar". The <session> in the above example command can be the local branch name of the clien- side, such as "my/topic".
To support this kind of workflow in CGit, add a filter and a new handler. The filter will check the prefix of the reference name, and if the command has a special reference name, the filter will add a specific tag (exec_by_hook) to the command. Commands with this specific tag will be executed by a new handler (an external hook named "execute-commands") instead of the internal execute_commands function.
We can use the external "execute-commands" hook to create pull requests or send emails. The centralized workflow is not a replacement for the distributed workflow, but a supplement. Especially for lightweight code contribution or for working on a project with multiple repositories.
We also implement a command line tool for this kind of workflow, see:
https://github.com/aliyun/git-repo-go
Jiang Xin (7):
receive-pack: new external execute-commands hook
receive-pack: feed all commands to post-receive
receive-pack: try execute-commands --pre-receive
receive-pack: read env from execute-commands output refs.c: refactor to reuse ref_is_hidden()
receive-pack: new config receive.executeCommandsHookRefs
hook: add document and example for "execute-commands" hook
Documentation/config/receive.txt | 11 + Documentation/githooks.txt | 43 ++ builtin/receive-pack.c | 228 +++++++- refs.c | 11 +- refs.h | 1 + t/t5411-execute-commands-hook.sh | 698 +++++++++++++++++++++++ templates/hooks--execute-commands.sample | 131 +++++ 7 files changed, 1093 insertions(+), 30 deletions(-) create mode 100755 t/t5411-execute-commands-hook.sh create mode 100755 templates/hooks--execute-commands.sample
Signature · 1 line
2.25.1.362.g51ebf55b93
PATCH 1/7 receive-pack: new external execute-commands hook
Git calls an internal execute_commands function to handle commands sent from client to git-receive-pack. Regardless of what references the user pushes, git creates or updates the corresponding references if the user has write permission. A contributor who has no write permission, cannot push to the the repository directly. So, the contributor has to write commits to an alternate location, and sends pull request by emails or by other ways. We call this distributed workflow.
It would be more convenient to work in a centralized workflow like what Gerrit provided for some cases. For example, a read-only user may run the following git push command to push commits to a special reference to create a code review, instead of updating a reference directly.
git push -o reviewers=user1,user2 \
-o oldoid=89c082363ac950d224a7259bfba3ccfbf4c560c4 \
origin \
HEAD:refs/for/<branch-name>/<session>The <branch-name> in the above example can be as simple as "master", or a more complicated branch name like "foo/bar". The <session> in the above example command can be the local branch name of the client side, such as "my/topic".
In order to support this kind of workflow in CGit, add a filter and a new handler. The filter will check the prefix of the reference name, and if the command has a special reference name, the filter will add a specific tag (exec_by_hook) to the command. Commands with this specific tag will be executed by a new handler (an exeternal hook named "execute-commands") instead of the internal execute_commands function.
There are two phases involved to run "execute-commands" hook:
* In order to check permissions for special git-push command, run a new "execute-commands--pre-receive" hook (or execute-commands --pre-receive, implemented in latter commit) instead of the "pre-receive" hook, because adding a new hook won't break the old implementation of the "pre-receive" hook.
* Then will call the "execute-commands" hook (without any parameter). This hook may call an external API to create a code review or send emails.
--- builtin/receive-pack.c | 135 ++++++++-- t/t5411-execute-commands-hook.sh | 427 +++++++++++++++++++++++++++++++ 2 files changed, 540 insertions(+), 22 deletions(-) create mode 100755 t/t5411-execute-commands-hook.sh
builtin/receive-pack.c +113 -22
@@ -311,7 +311,8 @@ struct command {struct command *next;const char *error_string; unsigned int skip_update:1, - did_not_exist:1; + did_not_exist:1, + exec_by_hook:1;int index;struct object_id old_oid;struct object_id new_oid;
@@ -668,6 +669,8 @@ static void prepare_push_cert_sha1(struct child_process *proc)struct receive_hook_feed_state {struct command *cmd; +int exec_by_hook; +int hook_must_exist;int skip_broken;struct strbuf buf;const struct string_list *push_options;
@@ -683,8 +686,13 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed,int code; argv[0 ] =find_hook (hook_name); -if (!argv[0 ]) -return 0 ; +if (!argv[0 ]) { +if (feed_state->hook_must_exist) { +rp_error ("cannot to find hook '%s'" , hook_name); +return 1 ; + }else +return 0 ; + } argv[1 ] =NULL ;
@@ -750,9 +758,15 @@ static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)struct receive_hook_feed_state *state = state_;struct command *cmd = state->cmd; -while (cmd && - state->skip_broken && (cmd->error_string || cmd->did_not_exist)) - cmd = cmd->next; +while (cmd) +if (state->skip_broken && (cmd->error_string || cmd->did_not_exist)) + cmd = cmd->next; +else if (state->exec_by_hook && !cmd->exec_by_hook) + cmd = cmd->next; +else if (!state->exec_by_hook && cmd->exec_by_hook) + cmd = cmd->next; +else +break ;if (!cmd)return -1 ;/* EOF */ strbuf_reset (&state->buf);
@@ -777,6 +791,8 @@ static int run_receive_hook(struct command *commands,strbuf_init (&state.buf,0 ); state.cmd = commands; + state.exec_by_hook =0 ; + state.hook_must_exist =0 ; state.skip_broken = skip_broken;if (feed_receive_hook (&state,NULL ,NULL ))return 0 ;
@@ -816,14 +832,45 @@ static int run_update_hook(struct command *cmd)return finish_command (&proc); } -static int is_ref_checked_out (const char *ref) +static int run_execute_commands_pre_receive_hook (struct command *commands, +const struct string_list *push_options) { -if (is_bare_repository ()) +struct receive_hook_feed_state state; +int status; + +strbuf_init (&state.buf,0 ); + state.cmd = commands; + state.exec_by_hook =1 ; + state.hook_must_exist =0 ; + state.skip_broken =0 ; +if (feed_receive_hook (&state,NULL ,NULL ))return 0 ; + state.cmd = commands; + state.push_options = push_options; + status =run_and_feed_hook ("execute-commands--pre-receive" , + feed_receive_hook, &state); +strbuf_release (&state.buf); +return status; +} + +static int run_execute_commands_hook (struct command *commands, +const struct string_list *push_options) +{ +struct receive_hook_feed_state state; +int status; -if (!head_name) +strbuf_init (&state.buf,0 ); + state.cmd = commands; + state.exec_by_hook =1 ; + state.hook_must_exist =1 ; + state.skip_broken =1 ; +if (feed_receive_hook (&state,NULL ,NULL ))return 0 ; -return !strcmp (head_name, ref); + state.cmd = commands; + state.push_options = push_options; + status =run_and_feed_hook ("execute-commands" , feed_receive_hook, &state); +strbuf_release (&state.buf); +return status; } static char *refuse_unconfigured_deny_msg =
@@ -1373,7 +1420,7 @@ static void warn_if_skipped_connectivity_check(struct command *commands,int checked_connectivity =1 ;for (cmd = commands; cmd; cmd = cmd->next) { -if (should_process_cmd (cmd) && si->shallow_ref[cmd->index]) { +if (should_process_cmd (cmd) && !cmd->exec_by_hook && si->shallow_ref[cmd->index]) {error ("BUG: connectivity check has not been run on ref %s" , cmd->ref_name); checked_connectivity =0 ;
@@ -1390,7 +1437,7 @@ static void execute_commands_non_atomic(struct command *commands,struct strbuf err =STRBUF_INIT ;for (cmd = commands; cmd; cmd = cmd->next) { -if (!should_process_cmd (cmd)) +if (!should_process_cmd (cmd) || cmd->exec_by_hook)continue ; transaction =ref_transaction_begin (&err);
@@ -1430,7 +1477,7 @@ static void execute_commands_atomic(struct command *commands, }for (cmd = commands; cmd; cmd = cmd->next) { -if (!should_process_cmd (cmd)) +if (!should_process_cmd (cmd) || cmd->exec_by_hook)continue ; cmd->error_string =update (cmd, si);
@@ -1466,6 +1513,8 @@ static void execute_commands(struct command *commands,struct iterate_data data;struct async muxer;int err_fd =0 ; +int seen_exec_by_hook =0 ; +int seen_internal_exec =0 ;if (unpacker_error) {for (cmd = commands; cmd; cmd = cmd->next)
@@ -1495,14 +1544,45 @@ static void execute_commands(struct command *commands,reject_updates_to_hidden (commands); -if (run_receive_hook (commands,"pre-receive" ,0 , push_options)) { -for (cmd = commands; cmd; cmd = cmd->next) { -if (!cmd->error_string) - cmd->error_string ="pre-receive hook declined" ; +/* Try to find commands that have special prefix, and will run these +* commands using an external "execute-commands" hook. +*/ +for (cmd = commands; cmd; cmd = cmd->next) { +if (!should_process_cmd (cmd)) +continue ; + +/* TODO: replace the fixed prefix by looking up git config variables. */ +if (!strncmp (cmd->ref_name,"refs/for/" ,9 )) { + cmd->exec_by_hook =1 ; + seen_exec_by_hook =1 ; + }else + seen_internal_exec =1 ; + } + +if (seen_exec_by_hook) { +/* Try to find and run the `execute-commands--pre-receive` hook to check +* permissions on the special commands. +* +* If it does not exists, try to run `execute-commands --pre-receive`. +*/ +if (run_execute_commands_pre_receive_hook (commands, push_options)) { +for (cmd = commands; cmd; cmd = cmd->next) { +if (!cmd->error_string) + cmd->error_string ="execute-commands hook declined" ; + } +return ; } -return ; } +if (seen_internal_exec) +if (run_receive_hook (commands,"pre-receive" ,0 , push_options)) { +for (cmd = commands; cmd; cmd = cmd->next) { +if (!cmd->error_string) + cmd->error_string ="pre-receive hook declined" ; + } +return ; + } +/* * Now we'll start writing out refs, which means the objects need * to be in their final positions so that other processes can see them.
@@ -1521,10 +1601,21 @@ static void execute_commands(struct command *commands,free (head_name_to_free); head_name = head_name_to_free =resolve_refdup ("HEAD" ,0 ,NULL ,NULL ); -if (use_atomic) -execute_commands_atomic (commands, si); -else -execute_commands_non_atomic (commands, si); +if (seen_exec_by_hook) { +if (run_execute_commands_hook (commands, push_options)) { +for (cmd = commands; cmd; cmd = cmd->next) { +if (!cmd->error_string && (cmd->exec_by_hook || use_atomic)) + cmd->error_string ="fail to run execute-commands hook" ; + } + } + } + +if (seen_internal_exec) { +if (use_atomic) +execute_commands_atomic (commands, si); +else +execute_commands_non_atomic (commands, si); + }if (shallow_update)warn_if_skipped_connectivity_check (commands, si);
t/t5411-execute-commands-hook.sh new file +427 -0
427 lines not on this page · raw message
Signature · 1 line
2.25.1.362.g51ebf55b93
PATCH 2/7 receive-pack: feed all commands to post-receive
In reply to Jiang XinFeed all commands to "post-receive" hook, not only normal commands, but also commands with "exec_by_hook" flag.
--- builtin/receive-pack.c | 11 +++++++++-- t/t5411-execute-commands-hook.sh | 8 ++++---- 2 files changed, 13 insertions(+), 6 deletions(-)
builtin/receive-pack.c +9 -2
@@ -669,6 +669,7 @@ static void prepare_push_cert_sha1(struct child_process *proc)struct receive_hook_feed_state {struct command *cmd; +int exec_all;int exec_by_hook;int hook_must_exist;int skip_broken;
@@ -761,6 +762,8 @@ static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)while (cmd)if (state->skip_broken && (cmd->error_string || cmd->did_not_exist)) cmd = cmd->next; +else if (state->exec_all) +break ;else if (state->exec_by_hook && !cmd->exec_by_hook) cmd = cmd->next;else if (!state->exec_by_hook && cmd->exec_by_hook)
@@ -784,6 +787,7 @@ static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)static int run_receive_hook (struct command *commands,const char *hook_name,int skip_broken, +int exec_all,const struct string_list *push_options) {struct receive_hook_feed_state state;
@@ -791,6 +795,7 @@ static int run_receive_hook(struct command *commands,strbuf_init (&state.buf,0 ); state.cmd = commands; + state.exec_all = exec_all; state.exec_by_hook =0 ; state.hook_must_exist =0 ; state.skip_broken = skip_broken;
@@ -840,6 +845,7 @@ static int run_execute_commands_pre_receive_hook(struct command *commands,strbuf_init (&state.buf,0 ); state.cmd = commands; + state.exec_all =0 ; state.exec_by_hook =1 ; state.hook_must_exist =0 ; state.skip_broken =0 ;
@@ -861,6 +867,7 @@ static int run_execute_commands_hook(struct command *commands,strbuf_init (&state.buf,0 ); state.cmd = commands; + state.exec_all =0 ; state.exec_by_hook =1 ; state.hook_must_exist =1 ; state.skip_broken =1 ;
@@ -1575,7 +1582,7 @@ static void execute_commands(struct command *commands, }if (seen_internal_exec) -if (run_receive_hook (commands,"pre-receive" ,0 , push_options)) { +if (run_receive_hook (commands,"pre-receive" ,0 ,0 , push_options)) {for (cmd = commands; cmd; cmd = cmd->next) {if (!cmd->error_string) cmd->error_string ="pre-receive hook declined" ;
@@ -2114,7 +2121,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)unlink_or_warn (pack_lockfile);if (report_status)report (commands, unpack_status); -run_receive_hook (commands,"post-receive" ,1 , +run_receive_hook (commands,"post-receive" ,1 ,1 , &push_options);run_update_post_hook (commands);string_list_clear (&push_options,0 );
t/t5411-execute-commands-hook.sh +4 -4
@@ -125,7 +125,7 @@ test_expect_success "create local topic branch" ' ) ' -test_expect_failure "push one special ref: refs/for/master" ' +test_expect_success "push one special ref: refs/for/master" ' ( cd work && git update-ref HEAD $B &&
@@ -182,7 +182,7 @@ test_expect_success "add back the execute-commands hook" ' mv $bare/hooks/execute-commands.ok $bare/hooks/execute-commands ' -test_expect_failure "push one special ref: refs/for/a/b/c" ' +test_expect_success "push one special ref: refs/for/a/b/c" ' ( cd work && git push origin HEAD:refs/for/a/b/c/my/topic
@@ -199,7 +199,7 @@ test_expect_failure "push one special ref: refs/for/a/b/c" ' test_cmp expect actual ' -test_expect_failure "push two special references" ' +test_expect_success "push two special references" ' ( cd work && git push origin \
@@ -300,7 +300,7 @@ test_expect_success "restore execute-commands hook" ' mv $bare/hooks/execute-commands.ok $bare/hooks/execute-commands ' -test_expect_failure "push mixed references successfully" ' +test_expect_success "push mixed references successfully" ' ( cd work && git push origin \
Signature · 1 line
2.25.1.362.g51ebf55b93
PATCH 3/7 receive-pack: try `execute-commands --pre-receive`
In reply to Jiang XinFirst we try to find the hook "execute-command--pre-receive" to check permissions for special commands. If the hook does not exist, will try to find the hook "execute-commands" and run command execute-commands --pre-receive instead.
--- builtin/receive-pack.c | 30 ++++++++++---- t/t5411-execute-commands-hook.sh | 68 +++++++++++++++++++++++--------- 2 files changed, 71 insertions(+), 27 deletions(-)
builtin/receive-pack.c +22 -8
@@ -683,20 +683,34 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed, {struct child_process proc =CHILD_PROCESS_INIT ;struct async muxer; -const char *argv[2 ]; +const char *argv[3 ];int code; argv[0 ] =find_hook (hook_name);if (!argv[0 ]) { -if (feed_state->hook_must_exist) { -rp_error ("cannot to find hook '%s'" , hook_name); -return 1 ; - }else -return 0 ; +char *hook_helper; +char *opt; + + opt =strstr (hook_name,"--" ); +if (opt) { + hook_helper =xstrdup (hook_name); + hook_helper[opt - hook_name] ='\0' ; + argv[0 ] =find_hook (hook_helper); +free (hook_helper); + } +if (!argv[0 ]) { +if (feed_state->hook_must_exist) { +rp_error ("cannot to find hook '%s'" , hook_name); +return 1 ; + }else +return 0 ; + } + argv[1 ] = opt; + argv[2 ] =NULL ; + }else { + argv[1 ] =NULL ; } - argv[1 ] =NULL ; - proc.argv = argv; proc.in =-1 ; proc.stdout_to_stderr =1 ;
t/t5411-execute-commands-hook.sh +49 -19
160 lines not on this page · raw message
Signature · 1 line
2.25.1.362.g51ebf55b93
PATCH 4/7 receive-pack: read env from execute-commands output
In reply to Jiang XinThe “post-receive” hook may need the pull request ID generated by the “execute-commands” hook. The results can be passed between hooks by environment variables.
Each line of the message received from the standard output of the “execute-commands” in the key=value format is parsed as environment and these variables will be sent to environment of the “post-receive” hook.
--- builtin/receive-pack.c | 42 +++++++++++++++-- t/t5411-execute-commands-hook.sh | 79 ++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 4 deletions(-)
builtin/receive-pack.c +38 -4
@@ -85,6 +85,7 @@ static const char *nonce_status;static long nonce_stamp_slop;static timestamp_t nonce_stamp_slop_limit;static struct ref_transaction *transaction; +struct argv_array post_receive_env_array; static enum { KEEPALIVE_NEVER = 0,
@@ -678,7 +679,9 @@ struct receive_hook_feed_state { };typedef int (*feed_fn )(void *,const char **,size_t *); +typedef void (*stdout_handler_fn )(int out);static int run_and_feed_hook (const char *hook_name,feed_fn feed, +stdout_handler_fn stdout_handler,struct receive_hook_feed_state *feed_state) {struct child_process proc =CHILD_PROCESS_INIT ;
@@ -713,9 +716,15 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed, proc.argv = argv; proc.in =-1 ; - proc.stdout_to_stderr =1 ; +if (stdout_handler) + proc.out =-1 ; +else + proc.stdout_to_stderr =1 ; proc.trace2_hook_name = hook_name; +if (!strcmp (hook_name,"post-receive" ) && post_receive_env_array.argc >0 ) +argv_array_pushv (&proc.env_array, post_receive_env_array.argv); +if (feed_state->push_options) {int i;for (i =0 ; i < feed_state->push_options->nr; i++)
@@ -760,6 +769,10 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed,break ; }close (proc.in); + +if (stdout_handler) +stdout_handler (proc.out); +if (use_sideband)finish_async (&muxer);
@@ -817,7 +830,7 @@ static int run_receive_hook(struct command *commands,return 0 ; state.cmd = commands; state.push_options = push_options; - status =run_and_feed_hook (hook_name, feed_receive_hook, &state); + status =run_and_feed_hook (hook_name, feed_receive_hook,NULL , &state);strbuf_release (&state.buf);return status; }
@@ -868,11 +881,29 @@ static int run_execute_commands_pre_receive_hook(struct command *commands, state.cmd = commands; state.push_options = push_options; status =run_and_feed_hook ("execute-commands--pre-receive" , - feed_receive_hook, &state); + feed_receive_hook,NULL , &state);strbuf_release (&state.buf);return status; } + +static void prepare_post_receive_env (int in) +{ +struct strbuf stdout_buf =STRBUF_INIT ; + +while (strbuf_getwholeline_fd (&stdout_buf, in,'\n' ) !=EOF ) { +char *p = stdout_buf.buf + stdout_buf.len -1 ; +if (*p =='\n' ) + *p ='\0' ; + p =strchr (stdout_buf.buf,'=' ); +if (p ==NULL ) +continue ; +argv_array_push (&post_receive_env_array, stdout_buf.buf); +strbuf_reset (&stdout_buf); + } +strbuf_release (&stdout_buf); +} +static int run_execute_commands_hook (struct command *commands,const struct string_list *push_options) {
@@ -889,7 +920,8 @@ static int run_execute_commands_hook(struct command *commands,return 0 ; state.cmd = commands; state.push_options = push_options; - status =run_and_feed_hook ("execute-commands" , feed_receive_hook, &state); + status =run_and_feed_hook ("execute-commands" , + feed_receive_hook, prepare_post_receive_env, &state);strbuf_release (&state.buf);return status; }
@@ -2052,6 +2084,8 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix) OPT_END() }; +argv_array_init (&post_receive_env_array); +packet_trace_identity ("receive-pack" ); argc =parse_options (argc, argv, prefix, options, receive_pack_usage,0 );
t/t5411-execute-commands-hook.sh +79 -0
83 lines not on this page · raw message
Signature · 1 line
2.25.1.362.g51ebf55b93
PATCH 5/7 refs.c: refactor to reuse ref_is_hidden()
In reply to Jiang XinAdd new function ref_is_matched() to reuse ref_is_hidden(). Test case t5512 covered this change.
--- refs.c | 11 ++++++++--- refs.h | 1 + 2 files changed, 9 insertions(+), 3 deletions(-)
refs.c +8 -3
@@ -1389,13 +1389,18 @@ int parse_hide_refs_config(const char *var, const char *value, const char *secti }int ref_is_hidden (const char *refname,const char *refname_full) +{ +return ref_is_matched (hide_refs, refname, refname_full); +} + +int ref_is_matched (struct string_list *match_refs,const char *refname,const char *refname_full) {int i; -if (!hide_refs) +if (!match_refs)return 0 ; -for (i = hide_refs->nr -1 ; i >=0 ; i--) { -const char *match = hide_refs->items[i].string; +for (i = match_refs->nr -1 ; i >=0 ; i--) { +const char *match = match_refs->items[i].string;const char *subject;int neg =0 ;const char *p;
refs.h +1 -0
@@ -739,6 +739,7 @@ int parse_hide_refs_config(const char *var, const char *value, const char *);* parameter always points to the full ref name. */ int ref_is_hidden (const char *,const char *); +int ref_is_matched (struct string_list *,const char *,const char *);enum ref_type {REF_TYPE_PER_WORKTREE ,/* refs inside refs/ but not shared */
Signature · 1 line
2.25.1.362.g51ebf55b93
PATCH 6/7 receive-pack: new config receive.executeCommandsHookRefs
In reply to Jiang XinAdd a new multi-valued config variable "receive.executeCommandsHookRefs" for receive-pack command, like the follows:
git config --system --add receive.executeCommandsHookRefs refs/for/
git config --system --add receive.executeCommandsHookRefs refs/drafts/If the specific prefix strings match the reference names of the commands which are sent by git client to receive-pack, these commands will be executed by an external hook (named "execute-commands"), instead of the internal execute_commands function.
For example, if it is set to "refs/for/", pushing to a reference such as "refs/for/master" will not create or update reference "refs/for/master", but may create or update a pull request directly by running the external hook. Use more than one definition to specify multiple prefix strings.
--- Documentation/config/receive.txt | 11 +++++ builtin/receive-pack.c | 48 ++++++++++++++++----- t/t5411-execute-commands-hook.sh | 72 ++++++++++++++++++++++++++++++-- 3 files changed, 117 insertions(+), 14 deletions(-)
Documentation/config/receive.txt +11 -0
@@ -108,6 +108,17 @@ receive.denyNonFastForwards:: even if that push is forced. This configuration variable is set when initializing a shared repository. +receive.executeCommandsHookRefs:: + If the specific prefix matches the reference name of the command + which is sent by the client to `receive-pack`, the command will be + executed by an external hook (named "execute-commands"), instead of + the internal `execute_commands` function. For example, if it is + set to "refs/for/", pushing to reference such as "refs/for/master" + will not create or update a reference named "refs/for/master", but + may create or update a pull request directly by running the external + hook. Use more than one definition to specify multiple prefix + strings. + receive.hideRefs:: This variable is the same as `transfer.hideRefs`, but applies only to `receive-pack` (and so affects pushes, but not fetches).
builtin/receive-pack.c +38 -10
73 lines not on this page · raw message
t/t5411-execute-commands-hook.sh +68 -4
101 lines not on this page · raw message
Signature · 1 line
2.25.1.362.g51ebf55b93
PATCH 7/7 hook: add document and example for "execute-commands" hook
In reply to Jiang Xin--- Documentation/githooks.txt | 43 ++++++++ t/t5411-execute-commands-hook.sh | 98 +++++++++++++++++ templates/hooks--execute-commands.sample | 131 +++++++++++++++++++++++ 3 files changed, 272 insertions(+) create mode 100755 templates/hooks--execute-commands.sample
Documentation/githooks.txt +43 -0
@@ -58,6 +58,49 @@ the message file. The default 'applypatch-msg' hook, when enabled, runs the 'commit-msg' hook, if the latter is enabled. +execute-commands--pre-receive +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +This hook is invoked by linkgit:git-receive-pack[1] when it reacts to +special `git push` command. Just before starting to execute the +external 'execute-commands' hook to make changes to the repository, +the 'execute-commands--pre-receive' hook is invoked. This hook has +the same functionality as 'pre-receive' hook, while it only acts on +special commands which 'git-push' sends to 'git-receive-pack'. +The refnames of these spacial commands have special prefix (such as +"refs/for/") which matches what the `receive.executeCommandsHookRefs` +configuration variable defines. + +If this hook does not exist, will try to find the 'execute-commands' +hook, and run `execute-command --pre-receive` command to do some +checks. Its exit status determines the success or failure of the +update. If the hook exits with non-zero status, won't execute any of +the commands, no metter calling the internal `execute_commands` +function or calling the external "execute-commands" hook. + +This hook executes once for the receive operation, and gets the +information from its standard input. +See <<pre-receive,'pre-receive'>> for details. + +execute-commands +~~~~~~~~~~~~~~~~ +This hook is invoked by linkgit:git-receive-pack[1] when it reacts to +special `git push` command. According to refnames of the commands which +`git push` sends to 'git-receive-pack', the commands will be devided +into two groups by matching what the `receive.executeCommandsHookRefs` +configuration variable defines. One group of the commands will execute +the internal `execute_commands` function to update the corresponding +refnames, and the other group of commands which have matching refnames +will execute this external 'execute-commands' hook to create pull +requests, etc. + +Its exit status only determines the success or failure of the group of +commands with special refnames, unless atomic push is in use. + +This hook executes once if there are any special commands with special +refnames. There is no argument taken by this hook, and the push options +(if any) and command(s) will be fed to this book by its standard input. +See the <<pre-receive,'pre-receive'>> hook for the format of the input. + pre-applypatch ~~~~~~~~~~~~~~
t/t5411-execute-commands-hook.sh +98 -0
102 lines not on this page · raw message
templates/hooks--execute-commands.sample new file +131 -0
131 lines not on this page · raw message
Signature · 1 line
2.25.1.362.g51ebf55b93
Quoted 15 lines · Jiang Xin
It would be more convenient to work in a centralized workflow like what Gerrit provided for some cases. For example, a read-only user may run the following
git pushcommand to push commits to a special reference to create a code review, instead of updating a reference directly.git push -o reviewers=user1,user2 \ -o oldoid=89c082363ac950d224a7259bfba3ccfbf4c560c4 \ origin \ HEAD:refs/for/<branch-name>/<session>The
<branch-name>in the above example can be as simple as "master", or a more complicated branch name like "foo/bar". The<session>in the above example command can be the local branch name of the clien- side, such as "my/topic".
Quoted 6 lines
To support this kind of workflow in CGit, add a filter and a new handler. The filter will check the prefix of the reference name, and if the command has a special reference name, the filter will add a specific tag (
exec_by_hook) to the command. Commands with this specific tag will be executed by a new handler (an external hook named "execute-commands") instead of the internalexecute_commandsfunction.
I do not claim to be great at naming, but you are worse ;-)
- Any hook is about executing command(s), so "execute-commands" hook does not give any information to users.
- IIUC, this is only about what happens when accepting a push and is not called at any other time. Naming your hook without "receive" anywhere in its name would mean other people won't be able to add hook that "executes" commands upon cues other than receiving a push.
I can guess why you chose that name, because I know there is a function called execute_commands() in "git receive-pack", but that is not somethhing you can expect your end users, who are not intimate to our codebase, to know.
Quoted 2 lines
We can use the external "execute-commands" hook to create pull requests or send emails.
You can create pull requests or send emails out of the post-receive hook, so that is not a convincing justification why we want a new hook.
Now, I understand that Gerrit-style "notice a push to for/<target>, take over the whole operation that happens after receiving the pack data and do something entirely different, such as attempting to run a merge with refs/heads/<target> and update refs/heads/<target> instead, or fail the push if automerge fails" is not easy to arrange within the current "pre-receive" + "post-receive" framework (by the way, we should start considering to deprecate "update", and "post-update" hooks as these "*-receive" hooks were added to replace them, perhaps we should leave a #leftoverbits mark here). And I think it is reasonable to add a new hook that takes over the whole flow in "git receive-pack" to do so.
I just do not think "the execute-commands hook" is a good name for it. Perhaps "divert-receive" (as it diverts large portion of what receive does) or something? I dunno.
How do Gerrit folks deal with the "we pushed to the server, so let's pretend to have turned around and fetched from the same server immediately after doing so" client-side hack, by the way?
A vanilla "git push" on the client side does not know a push to refs/for/master would result in an update to refs/heads/master on the server side, and it would not know the result of the munging done on the server side (whether it is to rebase what is received on top of 'master' or to merge it to 'master') anyway, the remote-tracking branch refs/remotes/origin/master on the client side would be left stale. If we wanted to help them pretend to have fetched immediately after, I think we need to extend the protocol. Right now, after accepting "git push", the server end will say, for each proposed update for a ref, if the push updated successfully or not, but to support the "push to for/<target>, get heads/<target> updated" interaction, the reporting of the result (done in the report() function in builtin/receive-pack.c) needs to be able to say what ref (it may be a ref that "git push" did not think it pushed to) got updated to what value (it may be an object the client does not yet have---and we may have to actually turn around and fetch from them internally if we want to keep the illusion).
Quoted 2 lines · Junio C Hamano <[email protected]> 于2020年3月5日周四 上午4:39写道:
I do not claim to be great at naming, but you are worse ;-)
I totally agree that I am not good at naming, for example my daughter's name.
Quoted 9 lines
- Any hook is about executing command(s), so "execute-commands" hook does not give any information to users.
- IIUC, this is only about what happens when accepting a push and is not called at any other time. Naming your hook without "receive" anywhere in its name would mean other people won't be able to add hook that "executes" commands upon cues other than receiving a push.
Quoted 4 lines
I can guess why you chose that name, because I know there is a function called
execute_commands()in "git receive-pack", but that is not somethhing you can expect your end users, who are not intimate to our codebase, to know.
Yes, it's better to name the hook "* -receive", because the hooks are for different commands, such as "commit-msg" is for git commit.
Quoted 6 lines
Quoted 2 lines
We can use the external "execute-commands" hook to create pull requests or send emails.
You can create pull requests or send emails out of the post-receive hook, so that is not a convincing justification why we want a new hook.
Another solution is using "pre-receive" + "post-receive" to handle a push to "refs/for/master". The "post-receive" hook is used to create a pull requst and delete the special reference "refs/for/master" created between these two hooks. But having a temporary reference created is not safe for concurrent pushes.
Quoted 12 lines
Now, I understand that Gerrit-style "notice a push to for/<target>, take over the whole operation that happens after receiving the pack data and do something entirely different, such as attempting to run a merge with refs/heads/<target> and update refs/heads/<target> instead, or fail the push if automerge fails" is not easy to arrange within the current "pre-receive" + "post-receive" framework (by the way, we should start considering to deprecate "update", and "post-update" hooks as these "*-receive" hooks were added to replace them, perhaps we should leave a #leftoverbits mark here). And I think it is reasonable to add a new hook that takes over the whole flow in "git receive-pack" to do so.
Quoted 3 lines
I just do not think "the execute-commands hook" is a good name for it. Perhaps "divert-receive" (as it diverts large portion of what receive does) or something? I dunno.
I suggest naming the hook as "process-receive", which is executed between the other two "p*-receive" hooks, and no need to create a special "pre-receive" for "process-receive".
Quoted 3 lines
How do Gerrit folks deal with the "we pushed to the server, so let's pretend to have turned around and fetched from the same server immediately after doing so" client-side hack, by the way?
In the following example, I push a local commit to a special reference (refs/for/master) of the remote Gerrit server. The "report()" function (if Gerrit has one) says a new reference "refs/for/master" has been created. But in deed, there is no such reference created in the remote repository, Gerrit will create another reference instead, such as "refs/changes/71/623871/1", for user to download the code review . Because the local repository only has normal "remote.<name>.fetch" config variables for remote tracking, so git will not create a tracking reference for "refs/for/master". Command line tool, such as Android "repo" (or the reimplemented git-repo in Golang), will create a special reference (refs/published/<local/branch>) for tracking, and these tools are responsible for banch tracking.
$ git push --receive-pack="gerrit receive-pack" origin
refs/heads/master:refs/for/master
Enumerating objects: 13, done.
Counting objects: 100% (13/13), done.
Delta compression using up to 8 threads
Compressing objects: 100% (11/11), done.
Writing objects: 100% (12/12), 1.34 KiB | 171.00 KiB/s, done.
Total 12 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2)
remote: Processing changes: refs: 1, new: 1, done
remote:
remote: SUCCESS
remote:
remote: New Changes:
remote: http://gerrit.example.com/c/my/repo/+/623889 Test commit
To ssh://gerrit.example.com:29418/my/repo
* [new branch] master -> refs/for/masterQuoted 5 lines
A vanilla "git push" on the client side does not know a push to refs/for/master would result in an update to refs/heads/master on the server side, and it would not know the result of the munging done on the server side (whether it is to rebase what is received on top of 'master' or to merge it to 'master') anyway, the
Neither Gerrit nor our AGit-Flow server will update the master branch. Our AGit-Flow server will create a special reference (like GitHub's "refs/pull/<number>/head") for reviewers to download commits.
Quoted 4 lines
remote-tracking branch refs/remotes/origin/master on the client side would be left stale. If we wanted to help them pretend to have fetched immediately after, I think we need to extend the protocol. Right now, after accepting "git push", the server end will say, for
Quoted 8 lines
each proposed update for a ref, if the push updated successfully or not, but to support the "push to for/<target>, get heads/<target> updated" interaction, the reporting of the result (done in the
report()function inbuiltin/receive-pack.c) needs to be able to say what ref (it may be a ref that "git push" did not think it pushed to) got updated to what value (it may be an object the client does not yet have---and we may have to actually turn around and fetch from them internally if we want to keep the illusion).
I have no idea now how to make a simple patch to give an accurate report.
v2 cover New proc-receive hook for centralized workflow
Changes since v1:
* Use a better name "proc-receive" for the new hook.
* Rename the new config variable to "receive.procReceiveRefs".
--
Jiang Xin (5):
receive-pack: add new proc-receive hook refs.c: refactor to reuse ref_is_hidden()
receive-pack: new config receive.procReceiveRefs
receive-pack: read env from proc-receive output
hook: add document and example for "proc-receive" hook
Documentation/config/receive.txt | 14 + Documentation/githooks.txt | 22 + builtin/receive-pack.c | 160 +++++- refs.c | 11 +- refs.h | 1 + t/t5411-proc-receive-hook.sh | 831 +++++++++++++++++++++++++++ templates/hooks--proc-receive.sample | 119 ++++ 7 files changed, 1141 insertions(+), 17 deletions(-) create mode 100755 t/t5411-proc-receive-hook.sh create mode 100755 templates/hooks--proc-receive.sample
Signature · 1 line
2.26.0.rc0.5.gb02b988a14.dirty
PATCH v2 1/5 receive-pack: add new proc-receive hook
In reply to Jiang XinGit calls an internal execute_commands function to handle commands sent from client to git-receive-pack. Regardless of what references the user pushes, git creates or updates the corresponding references if the user has write-permission. A contributor who has no write-permission, cannot push to the repository directly. So, the contributor has to write commits to an alternate location, and sends pull request by emails or by other ways. We call this workflow as a distributed workflow.
It would be more convenient to work in a centralized workflow like what Gerrit provided for some cases. For example, a read-only user who cannot push to a branch directly can run the following git push command to push commits to a pseudo reference (has a prefix "refs/for/", not "refs/heads/") to create a code review.
git push origin \
HEAD:refs/for/<branch-name>/<session>The <branch-name> in the above example can be as simple as "master", or a more complicated branch name like "foo/bar". The <session> in the above example command can be the local branch name of the client side, such as "my/topic".
We cannot implement a centralized workflow elegantly by using "pre-receive" + "post-receive", because Git will call the internal function "execute_commands" to create references (even the special pseudo reference) between these two hooks. Even though we can delete the temporarily created pseudo reference via the "post-receive" hook, having a temporary reference is not safe for concurrent pushes.
So, add a filter and a new handler to support this kind of workflow. The filter will check the prefix of the reference name, and if the command has a special reference name, the filter will turn a specific field (have_special_ref) on for the command. Commands with this filed turned on will be executed by a new handler (an hook named "proc-receive") instead of the internal execute_commands function. We can use this "proc-receive" command to create pull requests or send emails for code review.
This "proc-receive" hook executes once for the receive operation. It takes no arguments, but gets the same information as the pre-receive hook does on its standard input.
--- builtin/receive-pack.c | 93 +++++- t/t5411-proc-receive-hook.sh | 547 +++++++++++++++++++++++++++++++++++ 2 files changed, 628 insertions(+), 12 deletions(-) create mode 100755 t/t5411-proc-receive-hook.sh
builtin/receive-pack.c +81 -12
@@ -312,7 +312,8 @@ struct command {struct command *next;const char *error_string; unsigned int skip_update:1, - did_not_exist:1; + did_not_exist:1, + have_special_ref:1;int index;struct object_id old_oid;struct object_id new_oid;
@@ -669,6 +670,9 @@ static void prepare_push_cert_sha1(struct child_process *proc)struct receive_hook_feed_state {struct command *cmd; +int feed_normal_ref; +int feed_special_ref; +int hook_must_exist;int skip_broken;struct strbuf buf;const struct string_list *push_options;
@@ -684,8 +688,14 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed,int code; argv[0 ] =find_hook (hook_name); -if (!argv[0 ]) -return 0 ; +if (!argv[0 ]) { +if (feed_state->hook_must_exist) { +rp_error ("cannot to find hook '%s'" , hook_name); +return 1 ; + }else { +return 0 ; + } + } argv[1 ] =NULL ;
@@ -751,9 +761,17 @@ static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)struct receive_hook_feed_state *state = state_;struct command *cmd = state->cmd; -while (cmd && - state->skip_broken && (cmd->error_string || cmd->did_not_exist)) - cmd = cmd->next; +while (cmd) { +if (state->skip_broken && (cmd->error_string || cmd->did_not_exist)) + cmd = cmd->next; +else if (state->feed_special_ref && cmd->have_special_ref) +break ; +else if (state->feed_normal_ref && !cmd->have_special_ref) +break ; +else + cmd = cmd->next; + } +if (!cmd)return -1 ;/* EOF */ strbuf_reset (&state->buf);
@@ -778,6 +796,9 @@ static int run_receive_hook(struct command *commands,strbuf_init (&state.buf,0 ); state.cmd = commands; + state.feed_normal_ref =1 ; + state.feed_special_ref =1 ; + state.hook_must_exist =0 ; state.skip_broken = skip_broken;if (feed_receive_hook (&state,NULL ,NULL ))return 0 ;
@@ -817,6 +838,27 @@ static int run_update_hook(struct command *cmd)return finish_command (&proc); } +static int run_proc_receive_hook (struct command *commands, +const struct string_list *push_options) +{ +struct receive_hook_feed_state state; +int status; + +strbuf_init (&state.buf,0 ); + state.cmd = commands; + state.feed_normal_ref =0 ; + state.feed_special_ref =1 ; + state.hook_must_exist =1 ; + state.skip_broken =1 ; +if (feed_receive_hook (&state,NULL ,NULL )) +return 0 ; + state.cmd = commands; + state.push_options = push_options; + status =run_and_feed_hook ("proc-receive" , feed_receive_hook, &state); +strbuf_release (&state.buf); +return status; +} +static char *refuse_unconfigured_deny_msg =N_ ("By default, updating the current branch in a non-bare repository\n" "is denied, because it will make the index and work tree inconsistent\n"
@@ -1392,7 +1434,7 @@ static void execute_commands_non_atomic(struct command *commands,struct strbuf err =STRBUF_INIT ;for (cmd = commands; cmd; cmd = cmd->next) { -if (!should_process_cmd (cmd)) +if (!should_process_cmd (cmd) || cmd->have_special_ref)continue ; transaction =ref_transaction_begin (&err);
@@ -1432,7 +1474,7 @@ static void execute_commands_atomic(struct command *commands, }for (cmd = commands; cmd; cmd = cmd->next) { -if (!should_process_cmd (cmd)) +if (!should_process_cmd (cmd) || cmd->have_special_ref)continue ; cmd->error_string =update (cmd, si);
@@ -1468,6 +1510,8 @@ static void execute_commands(struct command *commands,struct iterate_data data;struct async muxer;int err_fd =0 ; +int have_special_ref =0 ; +int have_normal_ref =0 ;if (unpacker_error) {for (cmd = commands; cmd; cmd = cmd->next)
@@ -1497,6 +1541,22 @@ static void execute_commands(struct command *commands,reject_updates_to_hidden (commands); +/* Try to find commands that have special prefix in their reference names, +* and mark them to run an external "proc-receive" hook later. +*/ +for (cmd = commands; cmd; cmd = cmd->next) { +if (!should_process_cmd (cmd)) +continue ; + +/* TODO: replace the fixed prefix by looking up git config variables. */ +if (!strncmp (cmd->ref_name,"refs/for/" ,9 )) { + cmd->have_special_ref =1 ; + have_special_ref =1 ; + }else { + have_normal_ref =1 ; + } + } +if (run_receive_hook (commands,"pre-receive" ,0 , push_options)) {for (cmd = commands; cmd; cmd = cmd->next) {if (!cmd->error_string)
@@ -1523,10 +1583,19 @@ static void execute_commands(struct command *commands,free (head_name_to_free); head_name = head_name_to_free =resolve_refdup ("HEAD" ,0 ,NULL ,NULL ); -if (use_atomic) -execute_commands_atomic (commands, si); -else -execute_commands_non_atomic (commands, si); +if (have_special_ref &&run_proc_receive_hook (commands, push_options)) { +for (cmd = commands; cmd; cmd = cmd->next) { +if (!cmd->error_string && (cmd->have_special_ref || use_atomic)) + cmd->error_string ="fail to run proc-receive hook" ; + } + } + +if (have_normal_ref) { +if (use_atomic) +execute_commands_atomic (commands, si); +else +execute_commands_non_atomic (commands, si); + }if (shallow_update)warn_if_skipped_connectivity_check (commands, si);
t/t5411-proc-receive-hook.sh new file +547 -0
547 lines not on this page · raw message
Signature · 1 line
2.26.0.rc0.5.gb02b988a14.dirty
Re: [PATCH v2 1/5] receive-pack: add new proc-receive hook
Quoted 35 lines · Jiang Xin
builtin/receive-pack.c | 93 +++++- t/t5411-proc-receive-hook.sh | 547 +++++++++++++++++++++++++++++++++++ 2 files changed, 628 insertions(+), 12 deletions(-) create mode 100755 t/t5411-proc-receive-hook.shbuiltin/receive-pack.c +5
-3@@ -312,7 +312,8 @@ struct command {struct command *next;const char *error_string; unsigned int skip_update:1, - did_not_exist:1; + did_not_exist:1, + have_special_ref:1;int index;struct object_id old_oid;struct object_id new_oid;@@ -669,6 +670,9 @@ static void prepare_push_cert_sha1(struct child_process *proc)struct receive_hook_feed_state {struct command *cmd; +int feed_normal_ref; +int feed_special_ref; +int hook_must_exist;int skip_broken;struct strbuf buf;const struct string_list *push_options;@@ -684,8 +688,14 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed,int code; argv[0 ] =find_hook (hook_name); -if (!argv[0 ]) -return 0 ;
builtin/receive-pack.c static int run_and_feed_hook(const char *hook_name, feed_fn feed, · line 700
Quoted 10 lines
builtin/receive-pack.c +8
-0+if (!argv[0 ]) { +if (feed_state->hook_must_exist) { +rp_error ("cannot to find hook '%s'" , hook_name); +return 1 ; + }else { +return 0 ; + } + } argv[1 ] =NULL ;
Why do you even need "must-exist" in the first place? In other parts of Git, when a hook does not exist, processing by that missing hook is simply not done. Why does it have to cause an error?
If there is a reasonable answer to the above question, then the concept that hooks can be marked as "must exist -- otherwise it is an error" would be independently useful and the support for that concept should be split out as a separate step before this one, I would think.
Also, I am not sure why you have to have one single "special" ref hierarchy to be handled by the hook. Why not feed everything and let the hook decide?
I would have expected a design more along the lines of "if the hook exists, then feed all the refs to the hook, let the hook do whatever it wants and report what it did back to the receive-pack process, so that the client side can learn what got accepted and what got rejected (and possibly other things that may require protocol extension later)". One of the possible things the hook can report may be "I did not do anything for this ref, please do whatever you normally do". That way, you do not need to introduce "there are two classes of refs, normal and special"---that becomes an unused and unnecessary concept. For whichever ref the hook considers "normal", it can use the mechanism to say "I want you to do the normal thing". And when it sees refs/for/ prefix (or whatever is considered "special" at the hook's discretion), it can do whatever it wants, and report "OK, I ate it" or "No, I reject it" (which are only the two things that can be returned back to the client within the current protocol, IIUC). We may later want to extend the mechanism to allow richer response by the hook at the same time to extend the on-the-wire protocol and then it may become possible for the hook to take a push to refs/for/master, update refs/heads/master to the commit (or to some other commit), and report "OK, I accepted it and also updated refs/heads/master to this other commit".
Re: [PATCH v2 1/5] receive-pack: add new proc-receive hook
Quoted 27 lines · Junio C Hamano <[email protected]> 于2020年3月10日周二 上午1:12写道:
Why do you even need "must-exist" in the first place? In other parts of Git, when a hook does not exist, processing by that missing hook is simply not done. Why does it have to cause an error?
If there is a reasonable answer to the above question, then the concept that hooks can be marked as "must exist -- otherwise it is an error" would be independently useful and the support for that concept should be split out as a separate step before this one, I would think.
Also, I am not sure why you have to have one single "special" ref hierarchy to be handled by the hook. Why not feed everything and let the hook decide?
I would have expected a design more along the lines of "if the hook exists, then feed all the refs to the hook, let the hook do whatever it wants and report what it did back to the receive-pack process, so that the client side can learn what got accepted and what got rejected (and possibly other things that may require protocol extension later)". One of the possible things the hook can report may be "I did not do anything for this ref, please do whatever you normally do". That way, you do not need to introduce "there are two classes of refs, normal and special"---that becomes an unused and unnecessary concept. For whichever ref the hook considers "normal", it can use the mechanism to say "I want you to do the normal thing". And when it sees refs/for/ prefix (or whatever is considered "special" at the hook's discretion), it can do whatever it wants,
Quoted 8 lines
and report "OK, I ate it" or "No, I reject it" (which are only the two things that can be returned back to the client within the current protocol, IIUC). We may later want to extend the mechanism to allow richer response by the hook at the same time to extend the on-the-wire protocol and then it may become possible for the hook to take a push to refs/for/master, update refs/heads/master to the commit (or to some other commit), and report "OK, I accepted it and also updated refs/heads/master to this other commit".
In patch 3/5, I add a new config variable "receive.procReceiveRefs" for developers to choose whether all of the commands or only a part of them should pipe to the "proc-receive" hook. For example, a developer may set the config variable on the server like this:
git config --system --add receive.procReceiveRefs refs/
Then all commands will pipe to the "proc-receive" hook.
Think there may be thousands of commands in the initial stage for setting up a new repository, and all of the commands are "normal" (creating branches or tags). If the "proc-receive" hook has to handle all of the "normal" commands by piping these commands to a "git update-ref --stdin" process, or pass these commands back to "receive-pack" by a new protocol, this will greatly impact the performance. I think it's better to let the developer to choose by using a config variable like "receive.procReceiveRefs".
Patch 4/5 (receive-pack: read env from proc-receive output) is unnecessary, and extend the on-the-wire protocol is a good idea. The protocol may using pkt-line format, and negotiation between receive-pack (S) and the hook (H) like this:
S: PKT-LINE(version=1\0side-band-64k ...) # I'm not sure it is
necessary for the capabilities for all controllable server side softwares.
S: flush-pkt
H: PKT-LINE(version=1\0side-band-64k ...)
H: flush-pkt
S: PKT-LINE(old-oid new-oid ref)
S: ... ...
S: flush-pkt
S: PKT-LINE(push-option)
S: ... ...
S: flush-pkt
H: PKT-LINE( sideband< progress... > )
H: ... ...
H: PKT-LINE(old-oid new-oid ref OK) # OK, I ate it
H: PKT-LINE(old-oid new-oid ref error-msg...) # NO, I reject it
H: PKT-LINE(old-oid new-oid ref) # Back to receive-pack to handle it
H: ... ...
H: flush-pkt
After piping the "special" commands to the "proc-receive" hook, receive-pack should discard the "special" commands, and add the commands back from the output of the "proc-receive" hook. Then "receive-pack" will continue to execute the internal "execute_commands" function for the unexecuted commands.
In order to have performance similar to the vanilla receive-pack, it is necessary to keep the config variable and the concept of "normal" and "special" references. If the config variable "receive.procReceiveRefs" is defined on the server, but the "proc-receive" hook does not exist, it's better give an error instead of fall back to the internal "execute_commands" function or doing nothing.
--
Jiang Xin
v3 cover New proc-receive hook for centralized workflow
Before writing test cases, I want to make sure whether it is on the right direction.
Changes since v2:
* Implement a on-the-wire protocol, see commit 1/4.
* Refactor report() on both server and client side for reporting of proc-receive.
--
Jiang Xin (4):
receive-pack: add new proc-receive hook
receive-pack: refactor report for proc-receive refs.c: refactor to reuse ref_is_hidden()
receive-pack: new config receive.procReceiveRefs
Documentation/config/receive.txt | 14 ++ builtin/receive-pack.c | 294 ++++++++++++++++++++++++++++++- refs.c | 11 +- refs.h | 1 + transport.c | 33 +++- 5 files changed, 332 insertions(+), 21 deletions(-)
Signature · 1 line
2.26.0.rc1.5.gca1e965b06
v4 cover New proc-receive hook for centralized workflow
Changes since v2 and v3:
* Implement a on-the-wire protocol, and add new test helper to test the on-the-wire protocol. See patch 2/5.
* Refactor report() on both server and client side for reporting of proc-receive. See patch 5/5.
Jiang Xin (5):
transport: not report a non-head push as a branch
receive-pack: add new proc-receive hook refs.c: refactor to reuse ref_is_hidden()
receive-pack: new config receive.procReceiveRefs
receive-pack: refactor report for proc-receive
Documentation/config/receive.txt | 14 + Makefile | 1 + builtin/receive-pack.c | 323 ++++++++++- refs.c | 11 +- refs.h | 1 + t/helper/test-proc-receive.c | 164 ++++++ t/helper/test-tool.c | 1 + t/helper/test-tool.h | 1 + t/t5411-proc-receive-hook.sh | 954 +++++++++++++++++++++++++++++++ transport.c | 28 +- 10 files changed, 1478 insertions(+), 20 deletions(-) create mode 100644 t/helper/test-proc-receive.c create mode 100755 t/t5411-proc-receive-hook.sh
Signature · 1 line
2.26.0.rc1.33.g4914ba4bcf
Re: [PATCH v4 0/5] New proc-receive hook for centralized workflow
Quoted 9 lines · Jiang Xin
Changes since v2 and v3:
* Implement a on-the-wire protocol, and add new test helper to test the on-the-wire protocol. See patch 2/5.
* Refactor
report()on both server and client side for reporting of proc-receive. See patch 5/5.
Quoted 6 lines
Jiang Xin (5):
transport: not report a non-head push as a branch
receive-pack: add new proc-receive hookrefs.c: refactor to reuseref_is_hidden()
receive-pack: new config receive.procReceiveRefs
receive-pack: refactor report for proc-receive
t5516 detects breakage of these patches that are queued directly on top of 2.26.
The tip of 'pu' tonight includes this, and fails.
E.g. https://travis-ci.org/github/git/git/builds/666579791
Thanks.
PATCH v4 1/5 transport: not report a non-head push as a branch
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
When pushing a new reference (not a head or tag), report it as a new reference instead of a new branch.
--- t/t5411-proc-receive-hook.sh | 144 +++++++++++++++++++++++++++++++++++ transport.c | 3 +- 2 files changed, 146 insertions(+), 1 deletion(-) create mode 100755 t/t5411-proc-receive-hook.sh
t/t5411-proc-receive-hook.sh new file +144 -0
@@ -0,0 +1,144 @@ +#!/bin/sh +# +# Copyright (c) 2020 Jiang Xin +# + +test_description='Test proc-receive hook' + +. ./test-lib.sh + +# Create commits in <repo> and assign each commit's oid to shell variables +# given in the arguments (A, B, and C). E.g.: +# +# create_commits_in <repo> A B C +# +# NOTE: Avoid calling this function from a subshell since variable +# assignments will disappear when subshell exits. +create_commits_in () { + repo="$1" && + if ! parent=$(git -C "$repo" rev-parse HEAD^{} 2>/dev/null) + then + parent= + fi && + T=$(git -C "$repo" write-tree) && + shift && + while test $# -gt 0 + do + name=$1 && + test_tick && + if test -z "$parent" + then + oid=$(echo $name | git -C "$repo" commit-tree $T) + else + oid=$(echo $name | git -C "$repo" commit-tree -p $parent $T) + fi && + eval $name=$oid && + parent=$oid && + shift || + return 1 + done && + git -C "$repo" update-ref refs/heads/master $oid +} + +format_git_output () { + sed \ + -e "s/ *\$//g" \ + -e "s/$A/<COMMIT-A>/g" \ + -e "s/$B/<COMMIT-B>/g" \ + -e "s/$TAG/<COMMIT-T>/g" \ + -e "s/$ZERO_OID/<ZERO-OID>/g" \ + -e "s/'/\"/g" +} + +test_expect_success "setup" ' + git init --bare upstream && + git init workbench && + create_commits_in workbench A B && + ( + cd workbench && + git remote add origin ../upstream && + git config core.abbrev 7 && + git update-ref refs/heads/master $A && + git tag -m "v1.0.0" v1.0.0 $A && + git push origin \ + $B:refs/heads/master \ + $A:refs/heads/next + ) && + TAG=$(cd workbench; git rev-parse v1.0.0) && + + # setup pre-receive hook + cat >upstream/hooks/pre-receive <<-EOF && + #!/bin/sh + + printf >&2 "# pre-receive hook\n" + + while read old new ref + do + printf >&2 "pre-receive< \$old \$new \$ref\n" + done + EOF + + # setup post-receive hook + cat >upstream/hooks/post-receive <<-EOF && + #!/bin/sh + + printf >&2 "# post-receive hook\n" + + while read old new ref + do + printf >&2 "post-receive< \$old \$new \$ref\n" + done + EOF + + chmod a+x \ + upstream/hooks/pre-receive \ + upstream/hooks/post-receive +' + +test_expect_success "normal git-push command" ' + ( + cd workbench && + git push -f origin \ + refs/tags/v1.0.0 \ + :refs/heads/next \ + HEAD:refs/heads/master \ + HEAD:refs/review/master/topic \ + HEAD:refs/heads/a/b/c + ) >out 2>&1 && + format_git_output <out >actual && + cat >expect <<-EOF && + remote: # pre-receive hook + remote: pre-receive< <COMMIT-B> <COMMIT-A> refs/heads/master + remote: pre-receive< <COMMIT-A> <ZERO-OID> refs/heads/next + remote: pre-receive< <ZERO-OID> <COMMIT-T> refs/tags/v1.0.0 + remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/review/master/topic + remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/a/b/c + remote: # post-receive hook + remote: post-receive< <COMMIT-B> <COMMIT-A> refs/heads/master + remote: post-receive< <COMMIT-A> <ZERO-OID> refs/heads/next + remote: post-receive< <ZERO-OID> <COMMIT-T> refs/tags/v1.0.0 + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/review/master/topic + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/a/b/c + To ../upstream + + ce858e6...1029397 HEAD -> master (forced update) + - [deleted] next + * [new tag] v1.0.0 -> v1.0.0 + * [new reference] HEAD -> refs/review/master/topic + * [new branch] HEAD -> a/b/c + EOF + test_cmp expect actual && + ( + cd upstream && + git show-ref + ) >out && + format_git_output <out >actual && + cat >expect <<-EOF && + <COMMIT-A> refs/heads/a/b/c + <COMMIT-A> refs/heads/master + <COMMIT-A> refs/review/master/topic + <COMMIT-T> refs/tags/v1.0.0 + EOF + test_cmp expect actual +' + +test_done
transport.c +2 -1
@@ -501,7 +501,8 @@ static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widtelse if (is_null_oid (&ref->old_oid))print_ref_status ('*' , (starts_with (ref->name,"refs/tags/" ) ?"[new tag]" : -"[new branch]" ), + (starts_with (ref->name,"refs/heads/" ) ?"[new branch]" : +"[new reference]" )), ref, ref->peer_ref,NULL , porcelain, summary_width);else {struct strbuf quickref =STRBUF_INIT ;
Signature · 1 line
2.26.0.rc1.33.g4914ba4bcf
Re: [PATCH v4 1/5] transport: not report a non-head push as a branch
t/t5411-proc-receive-hook.sh line 16
Quoted 2 lines · Jiang Xin
t/t5411-proc-receive-hook.sh new file +2
-0+# NOTE: Avoid calling this function from a subshell since variable +# assignments will disappear when subshell exits.
s/Avoid/Never/; the test_tick helper used in this function has the same issue.
t/t5411-proc-receive-hook.sh line 19
Quoted 3 lines
t/t5411-proc-receive-hook.sh new file +3
-0+create_commits_in () { + repo="$1" && + if ! parent=$(git -C "$repo" rev-parse HEAD^{} 2>/dev/null)
Do we need to discard the standard error stream like this? As long as this helper function is called inside a test_expect_thing, the error will not be seen and when debugging the test, we would want to see a failure (which indicates that we are creating a root commit).
Quoted 1 line
+format_git_output () {
I suspect this is to make output from _some_ "git" subcommand into a symbolic form so that exact object names would not have to be used in comparison, but this obviously cannot take _any_ git subcommand, but a specific one. The name does not say which one, which is disservice to readers.
t/t5411-proc-receive-hook.sh line 45
Quoted 2 lines
t/t5411-proc-receive-hook.sh new file +2
-0+ sed \ + -e "s/ *\$//g" \
What's the point of /g? You are anchoring the pattern (i.e. one or more SP) to the right end of the line, so it's not like it would
transform "a b c " into "abc". Also it would be sufficient to
say "zero or more" and that would be shorter, I think, i.e.
-e 's/ *$//' \
t/t5411-proc-receive-hook.sh line 48
Quoted 3 lines
t/t5411-proc-receive-hook.sh new file +3
-0+ -e "s/$A/<COMMIT-A>/g" \ + -e "s/$B/<COMMIT-B>/g" \ + -e "s/$TAG/<COMMIT-T>/g" \
A and B are commits, so the symbolic <COMMIT-A> and <COMMIT-B> do make sense, but wouldn't TAG be an annotated tag? Wouldn't it be <TAG-A> perhaps?
t/t5411-proc-receive-hook.sh line 49
Quoted 1 line
t/t5411-proc-receive-hook.sh new file +1
-0+ -e "s/$ZERO_OID/<ZERO-OID>/g" \
Maekes sense.
t/t5411-proc-receive-hook.sh line 50
Quoted 1 line
t/t5411-proc-receive-hook.sh new file +1
-0+ -e "s/'/\"/g"
I am not sure what is going on here. Why turn <don't> into <don"t>? Without exactly knowing what is getting munged, a reader cannot tell what is going on here. Let's read on to figure it out.
In any case, I think most of this helper is not about "formatting" output, but hiding, getting rid of, redacting or censoring the details for easier comparison. I'd prefer to see some different verb to be used for its name.
t/t5411-proc-receive-hook.sh line 60
Quoted 10 lines
t/t5411-proc-receive-hook.sh new file +10
-0+} + +test_expect_success "setup" ' + git init --bare upstream && + git init workbench && + create_commits_in workbench A B && + ( + cd workbench && + git remote add origin ../upstream && + git config core.abbrev 7 &&
This '7' is "use at least seven hexdigits"; is that really what we want? Depending on chance, some objects may be shown using 8 or more---is our "munge output into symbolic form for comparison" script prepared for such a case?
t/t5411-proc-receive-hook.sh line 67
Quoted 7 lines
t/t5411-proc-receive-hook.sh new file +7
-0+ git update-ref refs/heads/master $A && + git tag -m "v1.0.0" v1.0.0 $A && + git push origin \ + $B:refs/heads/master \ + $A:refs/heads/next + ) && + TAG=$(cd workbench; git rev-parse v1.0.0) &&
Why not "git -C workbench rev-parse v1.0.0"?
So, at this point, there are two repositories, upstream and workbench, and two commits A and B (B is newer). workbench has A at the tip of its 'master'; upstream has A at the tip of 'next' and B (which is newer than A) at the tip of 'master'.
t/t5411-proc-receive-hook.sh line 70
Quoted 3 lines
t/t5411-proc-receive-hook.sh new file +3
-0+ + # setup pre-receive hook + cat >upstream/hooks/pre-receive <<-EOF &&
Wouldn't it make it easier to read the resulting text if you quoted the end-of-here-text marker here, i.e. "<<\-EOF"? That way, you can lose backslash before $old, $new and $ref.
t/t5411-proc-receive-hook.sh line 82
Quoted 11 lines
t/t5411-proc-receive-hook.sh new file +11
-0+ #!/bin/sh + + printf >&2 "# pre-receive hook\n" + + while read old new ref + do + printf >&2 "pre-receive< \$old \$new \$ref\n" + done + EOF + # setup post-receive hook + cat >upstream/hooks/post-receive <<-EOF &&
Likewise.
t/t5411-proc-receive-hook.sh line 107
Quoted 10 lines
t/t5411-proc-receive-hook.sh new file +10
-0+test_expect_success "normal git-push command" ' + ( + cd workbench && + git push -f origin \ + refs/tags/v1.0.0 \ + :refs/heads/next \ + HEAD:refs/heads/master \ + HEAD:refs/review/master/topic \ + HEAD:refs/heads/a/b/c + ) >out 2>&1 &&
Do we need to worry about progress output (which we would want to squelch, I presume, for the purpose of comparing with the "expectation")? Would it be just the matter of giving --no-progress?
t/t5411-proc-receive-hook.sh line 115
Quoted 8 lines
t/t5411-proc-receive-hook.sh new file +8
-0+ format_git_output <out >actual && + cat >expect <<-EOF && + remote: # pre-receive hook + remote: pre-receive< <COMMIT-B> <COMMIT-A> refs/heads/master + remote: pre-receive< <COMMIT-A> <ZERO-OID> refs/heads/next + remote: pre-receive< <ZERO-OID> <COMMIT-T> refs/tags/v1.0.0 + remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/review/master/topic + remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/a/b/c
Do we guarantee the order in which these received refs are reported, or do we somehow need to sort (presumably inside the hook)? The same question applies to the post-receive side, too.
Quoted 7 lines
t/t5411-proc-receive-hook.sh new file +7
-0+ remote: # post-receive hook + remote: post-receive< <COMMIT-B> <COMMIT-A> refs/heads/master + remote: post-receive< <COMMIT-A> <ZERO-OID> refs/heads/next + remote: post-receive< <ZERO-OID> <COMMIT-T> refs/tags/v1.0.0 + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/review/master/topic + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/a/b/c + To ../upstream
t/t5411-proc-receive-hook.sh line 132
Quoted 10 lines
t/t5411-proc-receive-hook.sh new file +10
-0+ + ce858e6...1029397 HEAD -> master (forced update) + - [deleted] next + * [new tag] v1.0.0 -> v1.0.0 + * [new reference] HEAD -> refs/review/master/topic + * [new branch] HEAD -> a/b/c + EOF + test_cmp expect actual && + ( + cd upstream && + git show-ref
This one I know we give output in a reliable order, but I do not offhand know if we give any written guarantee. Perhaps we should document it if we haven't already (i.e. it is OK the "expected" output below assumes that the output is sorted by full refnames in ASCII order).
Quoted 17 lines
t/t5411-proc-receive-hook.sh new file +12
-0+ ) >out && + format_git_output <out >actual && + cat >expect <<-EOF && + <COMMIT-A> refs/heads/a/b/c + <COMMIT-A> refs/heads/master + <COMMIT-A> refs/review/master/topic + <COMMIT-T> refs/tags/v1.0.0 + EOF + test_cmp expect actual +' + +test_donetransport.c +0
-0
transport.c static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widt · line 506
Quoted 7 lines
transport.c +2
-1@@ -501,7 +501,8 @@ static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widtelse if (is_null_oid (&ref->old_oid))print_ref_status ('*' , (starts_with (ref->name,"refs/tags/" ) ?"[new tag]" : -"[new branch]" ), + (starts_with (ref->name,"refs/heads/" ) ?"[new branch]" : +"[new reference]" )), ref, ref->peer_ref,NULL , porcelain, summary_width);
The original is largely to blame, but I couldn't read the above and understand what the above is doing, before reformatting it like so:
else if (is_null_oid(&ref->old_oid))
print_ref_status('*',
(starts_with(ref->name, "refs/tags/")
? "[new tag]"
: (starts_with(ref->name, "refs/heads/")
? "[new branch]"
: "[new reference]")),
ref, ref->peer_ref, NULL, porcelain, summary_width);When long subexpressions are involved, ternary operator ?: is easier to read, especially when nested, if you can see its parse tree when you tilt your head 90-degrees sideways (i.e. the same direction as you can see a smile in ;-).
Thanks.
PATCH v4 2/5 receive-pack: add new proc-receive hook
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Git calls an internal execute_commands function to handle commands sent from client to git-receive-pack. Regardless of what references the user pushes, git creates or updates the corresponding references if the user has write-permission. A contributor who has no write-permission, cannot push to the repository directly. So, the contributor has to write commits to an alternate location, and sends pull request by emails or by other ways. We call this workflow as a distributed workflow.
It would be more convenient to work in a centralized workflow like what Gerrit provided for some cases. For example, a read-only user who cannot push to a branch directly can run the following git push command to push commits to a pseudo reference (has a prefix "refs/for/", not "refs/heads/") to create a code review.
git push origin \
HEAD:refs/for/<branch-name>/<session>The <branch-name> in the above example can be as simple as "master", or a more complicated branch name like "foo/bar". The <session> in the above example command can be the local branch name of the client side, such as "my/topic".
We cannot implement a centralized workflow elegantly by using "pre-receive" + "post-receive", because Git will call the internal function "execute_commands" to create references (even the special pseudo reference) between these two hooks. Even though we can delete the temporarily created pseudo reference via the "post-receive" hook, having a temporary reference is not safe for concurrent pushes.
So, add a filter and a new handler to support this kind of workflow. The filter will check the prefix of the reference name, and if the command has a special reference name, the filter will turn a specific field (run_proc_receive) on for the command. Commands with this filed turned on will be executed by a new handler (an hook named "proc-receive") instead of the internal execute_commands function. We can use this "proc-receive" command to create pull requests or send emails for code review.
This "proc-receive" hook reads commands, push-options (optional), and send result using a protocol in pkt-line format. In the following example, The letter "S" stands for "receive-pack" and letter "H" stands for the hook.
S: PKT-LINE(version=1\0push-options ...)
S: flush-pkt
H: PKT-LINE(version=1\0push-options ...)
H: flush-pkt
S: PKT-LINE(old-oid new-oid ref)
S: ... ...
S: flush-pkt
# Optional, only if push-options is negotiated.
S: PKT-LINE(push-option)
S: ... ...
S: flush-pkt
# OK, run this command successfully.
H: PKT-LINE(old-oid new-oid ref ok)
# NO, I reject it.
H: PKT-LINE(old-oid new-oid ref ng reason)
# OK, but use an alternate reference. (in latter commit)
H: PKT-LINE(old-oid new-oid ref ok ref:alt-ref)
# It will fallthrough to receive-pack to execute. (in latter commit)
H: PKT-LINE(old-oid new-oid ref ft)
H: ... ...
H: flush-pkt
After receiving a command, the hook can create/update another alternate reference. For example, a command for a reference "refs/for/master" may create a special reference, such as "refs/pull/123/head". The alternate reference can be returned from the result in an extensible format like "<old-oid> <new-oid> <reference> <status> [<message>]".
The result will be stored in a command list, and "receive-pack" will use the result to replace the commands that have specific run_proc_receive field turned on.
--- Makefile | 1 + builtin/receive-pack.c | 275 +++++++++++++++++- t/helper/test-proc-receive.c | 164 +++++++++++ t/helper/test-tool.c | 1 + t/helper/test-tool.h | 1 + t/t5411-proc-receive-hook.sh | 528 +++++++++++++++++++++++++++++++++++ 6 files changed, 965 insertions(+), 5 deletions(-) create mode 100644 t/helper/test-proc-receive.c
Makefile +1 -0
@@ -725,6 +725,7 @@ TEST_BUILTINS_OBJS += test-parse-pathspec-file.o TEST_BUILTINS_OBJS += test-path-utils.o TEST_BUILTINS_OBJS += test-pkt-line.o TEST_BUILTINS_OBJS += test-prio-queue.o +TEST_BUILTINS_OBJS += test-proc-receive.o TEST_BUILTINS_OBJS += test-progress.o TEST_BUILTINS_OBJS += test-reach.o TEST_BUILTINS_OBJS += test-read-cache.o
builtin/receive-pack.c +270 -5
332 lines not on this page · raw message
t/helper/test-proc-receive.c new file +164 -0
164 lines not on this page · raw message
t/helper/test-tool.c +1 -0
@@ -43,6 +43,7 @@ static struct test_cmd cmds[] = { { "path-utils", cmd__path_utils }, { "pkt-line", cmd__pkt_line }, { "prio-queue", cmd__prio_queue }, + { "proc-receive", cmd__proc_receive}, { "progress", cmd__progress }, { "reach", cmd__reach }, { "read-cache", cmd__read_cache },
t/helper/test-tool.h +1 -0
@@ -33,6 +33,7 @@ int cmd__parse_pathspec_file(int argc, const char** argv);int cmd__path_utils (int argc,const char **argv);int cmd__pkt_line (int argc,const char **argv);int cmd__prio_queue (int argc,const char **argv); +int cmd__proc_receive (int argc,const char **argv);int cmd__progress (int argc,const char **argv);int cmd__reach (int argc,const char **argv);int cmd__read_cache (int argc,const char **argv);
t/t5411-proc-receive-hook.sh +528 -0
538 lines not on this page · raw message
Signature · 1 line
2.26.0.rc1.33.g4914ba4bcf
PATCH 1/3 t5543: never report what we do not push
When we push some references to the git server, we expect git to report the status of the references we are pushing; no more, no less. But when pusing with atomic mode, if some references cannot be pushed, Git reports the reject message on all references in the remote repository.
Add new test cases in t5543, and fix them in latter commit.
--- t/t5543-atomic-push.sh | 92 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+)
t/t5543-atomic-push.sh +92 -0
@@ -27,6 +27,13 @@ test_refs () { test_cmp expect actual } +format_git_output () { + awk '/^(To| !) / {print}' | \ + sed \ + -e "s/ *\$//g" \ + -e "s/'/\"/g" +} + test_expect_success 'atomic push works for a single branch' ' mk_repo_pair && (
@@ -191,4 +198,89 @@ test_expect_success 'atomic push is not advertised if configured' ' test_refs master HEAD@{1} ' +# References in upstream : master(1) one(1) foo(1) +# References in workbench: master(2) foo(1) two(2) bar(2) +# Atomic push : master(2) two(2) bar(2) +test_expect_failure 'atomic push reports (reject by update hook)' ' + mk_repo_pair && + ( + cd workbench && + # Keep constant output. + git config core.abbrev 7 && + test_commit one && + git branch foo && + git push up master one foo && + git tag -d one + ) && + ( + mkdir -p upstream/.git/hooks && + cat >upstream/.git/hooks/update <<-EOF && + #!/bin/sh + + if test "\$1" = "refs/heads/bar" + then + echo >2 "Pusing to branch bar is prohibited" + exit 1 + fi + EOF + chmod a+x upstream/.git/hooks/update + ) && + ( + cd workbench && + test_commit two && + git branch bar + ) && + test_must_fail git -C workbench \ + push --atomic up master two bar >out 2>&1 && + format_git_output <out >actual && + cat >expect <<-EOF && + To ../upstream + ! [remote rejected] master -> master (atomic push failure) + ! [remote rejected] two -> two (atomic push failure) + ! [remote rejected] bar -> bar (hook declined) + EOF + test_cmp expect actual +' + +# References in upstream : master(1) one(1) foo(1) +# References in workbench: master(2) foo(1) two(2) bar(2) +test_expect_failure 'atomic push reports (mirror, but reject by update hook)' ' + ( + cd workbench && + git remote remove up && + git remote add up ../upstream + ) && + test_must_fail git -C workbench \ + push --atomic --mirror up >out 2>&1 && + format_git_output <out >actual && + cat >expect <<-EOF && + To ../upstream + ! [remote rejected] master -> master (atomic push failure) + ! [remote rejected] one (atomic push failure) + ! [remote rejected] bar -> bar (hook declined) + ! [remote rejected] two -> two (atomic push failure) + EOF + test_cmp expect actual +' + +# References in upstream : master(2) one(1) foo(1) +# References in workbench: master(1) foo(1) two(2) bar(2) +test_expect_failure 'atomic push reports (reject by non-ff)' ' + rm upstream/.git/hooks/update && + ( + cd workbench && + git push up master && + git reset --hard HEAD^ + ) && + test_must_fail git -C workbench \ + push --atomic up master foo bar >out 2>&1 && + format_git_output <out >actual && + cat >expect <<-EOF && + To ../upstream + ! [rejected] master -> master (non-fast-forward) + ! [rejected] bar -> bar (atomic push failed) + EOF + test_cmp expect actual +' + test_done
Signature · 1 line
2.26.0.3.ga7a9d752d4
Re: [PATCH 1/3] t5543: never report what we do not push
Quoted 1 line · Jiang Xin
+format_git_output () {
Unless this helper is able to take any git output and massage, please describe what kind of git output it is meant to handle.
Also, "format" does not tell anything to the readers why it wants to transform its input or what its output is supposed to look like. It does not help readers and future developers.
t/t5543-atomic-push.sh test_refs () { · line 33
Quoted 3 lines
t/t5543-atomic-push.sh +3
-0+ awk '/^(To| !) / {print}' | \ + sed \ + -e "s/ *\$//g" \
What's the point of /g? You are anchoring the pattern (i.e. one or more SP) to the right end of the line, so it's not like it would
transform "a b c " into "abc". Also it would be sufficient to
say "zero or more" and that would be shorter, I think.
t/t5543-atomic-push.sh test_refs () { · line 34
Quoted 1 line
t/t5543-atomic-push.sh +1
-0+ -e "s/'/\"/g"
It is unclear what this thing is for. If the output from a git subcommand you are munging with the helper says <don't>, this will turn it into <don"t>, presumably before comparing it with the expected output you'd literally prepare in the test script. Are the git subcommands whose output you are munging unstable and sometimes use single and sometimes use double quotes?
If not, if you used single quotes when preparing the expected output, wouldn't you be able to do without this?
Is it because you'd have the code that prepares the expected output inside a sq pair (because it is in test_expect_thing), and it is cumbersome to write a literal single quote? If that is the reason, that is understandable, but I think readers deserve some comments explaining why these transformations are done. Please do not waste readers' time.
It looks wasteful to pipe awk output to sed. I wonder if something along the lines of
sed -ne "/^\(To\| !\) /{
s/ *\$//
s/'/\"/g
p
}"would do the same thing with a single tool.
t/t5543-atomic-push.sh test_expect_success 'atomic push is not advertised if configured' ' · line 209
Quoted 9 lines
t/t5543-atomic-push.sh +9
-0+# References in upstream : master(1) one(1) foo(1) +# References in workbench: master(2) foo(1) two(2) bar(2) +# Atomic push : master(2) two(2) bar(2) +test_expect_failure 'atomic push reports (reject by update hook)' ' + mk_repo_pair && + ( + cd workbench && + # Keep constant output. + git config core.abbrev 7 &&
This '7' is "use at least seven hexdigits", so it does not give any guarantee that your output will be stable. Depending on chance, some objects may be shown using 8 or more---is our "munge output before comparison" helper prepared for such a case?
t/t5543-atomic-push.sh test_expect_success 'atomic push is not advertised if configured' ' · line 222
Quoted 13 lines
t/t5543-atomic-push.sh +13
-0+ test_commit one && + git branch foo && + git push up master one foo && + git tag -d one + ) && + ( + mkdir -p upstream/.git/hooks && + cat >upstream/.git/hooks/update <<-EOF && + #!/bin/sh + + if test "\$1" = "refs/heads/bar" + then + echo >2 "Pusing to branch bar is prohibited"
Meant to redirect to the standard error stream, not a file "2"?
t/t5543-atomic-push.sh test_expect_success 'atomic push is not advertised if configured' ' · line 232
Quoted 10 lines
t/t5543-atomic-push.sh +10
-0+ exit 1 + fi + EOF + chmod a+x upstream/.git/hooks/update + ) && + ( + cd workbench && + test_commit two && + git branch bar + ) &&
At this point, we have the refs described in the comment at the beginning (which is greatly appreciated---it hels understanding what is going on).
t/t5543-atomic-push.sh test_expect_success 'atomic push is not advertised if configured' ' · line 234
Quoted 2 lines
t/t5543-atomic-push.sh +2
-0+ test_must_fail git -C workbench \ + push --atomic up master two bar >out 2>&1 &&
As "git push" does not auto-follow tags, what we push will be exactly these three refs.
t/t5543-atomic-push.sh test_expect_success 'atomic push is not advertised if configured' ' · line 241
Quoted 7 lines
t/t5543-atomic-push.sh +7
-0+ format_git_output <out >actual && + cat >expect <<-EOF && + To ../upstream + ! [remote rejected] master -> master (atomic push failure) + ! [remote rejected] two -> two (atomic push failure) + ! [remote rejected] bar -> bar (hook declined) + EOF
And we expect them to be rejected, as 'bar' needs to stay constant there.
Review
Quoted 1 line
(quoted diff) +1
-0+ test_cmp expect actual
All makes sense.
Re: [PATCH 1/3] t5543: never report what we do not push
Quoted 8 lines · Junio C Hamano <[email protected]> 于2020年3月25日周三 下午11:05写道:
Quoted 1 line · Jiang Xin
+format_git_output () {
Unless this helper is able to take any git output and massage, please describe what kind of git output it is meant to handle.
Wil use "cut_status_report_and_tidy" as the function name.
Quoted 4 lines
Also, "format" does not tell anything to the readers why it wants to transform its input or what its output is supposed to look like. It does not help readers and future developers.
Quoted 8 lines
t/t5543-atomic-push.sh test_refs () { · line 33
Quoted 3 lines
t/t5543-atomic-push.sh +3
-0+ awk '/^(To| !) / {print}' | \ + sed \ + -e "s/ *\$//g" \What's the point of /g? You are anchoring the pattern (i.e. one or more SP) to the right end of the line, so it's not like it would
transform "a b c " into "abc". Also it would be sufficient to
say "zero or more" and that would be shorter, I think.
I want to remove the trailing spaces in git output. I don't know why there are trailing spaces in each "remote: " message.
But there is not trailing spaces in status report message, so I will remote it in next reroll.
Quoted 13 lines
Quoted 2 lines
+ -e "s/'/\"/g"
It is unclear what this thing is for. If the output from a git subcommand you are munging with the helper says <don't>, this will turn it into <don"t>, presumably before comparing it with the expected output you'd literally prepare in the test script. Are the git subcommands whose output you are munging unstable and sometimes use single and sometimes use double quotes?
If not, if you used single quotes when preparing the expected output, wouldn't you be able to do without this?
Quoted 6 lines
Is it because you'd have the code that prepares the expected output inside a sq pair (because it is in
test_expect_thing), and it is cumbersome to write a literal single quote? If that is the reason, that is understandable, but I think readers deserve some comments explaining why these transformations are done. Please do not waste readers' time.
To prepare expect message, sometime I have to escape the single quote like this
cat >expect <<-EOF
error: failed to push some refs to '"'"'../upstream'"'"'
EOF
It's boring to type '"'"' instead of a double quote.
Quoted 11 lines
It looks wasteful to pipe awk output to sed. I wonder if something along the lines of
sed -ne "/^\(To\| !\) /{ s/ *\$// s/'/\"/g p }"would do the same thing with a single tool.
That's better.
Quoted 6 lines
Quoted 6 lines
t/t5543-atomic-push.sh +6
-0+# References in upstream : master(1) one(1) foo(1) +# References in workbench: master(2) foo(1) two(2) bar(2) +# Atomic push : master(2) two(2) bar(2) +test_expect_failure 'atomic push reports (reject by update hook)' ' + mk_repo_pair && + (
Quoted 8 lines
t/t5543-atomic-push.sh test_expect_success 'atomic push is not advertised if configured' ' · line 209
Quoted 3 lines
t/t5543-atomic-push.sh +3
-0+ cd workbench && + # Keep constant output. + git config core.abbrev 7 &&This '7' is "use at least seven hexdigits", so it does not give any guarantee that your output will be stable. Depending on chance, some objects may be shown using 8 or more---is our "munge output before comparison" helper prepared for such a case?
Will use sed -e 's/ */ /g' on output message to make it stable.
Quoted 15 lines
Quoted 4 lines
t/t5543-atomic-push.sh +3
-0+ test_commit one && + git branch foo && + git push up master one foo &&t/t5543-atomic-push.sh test_expect_success 'atomic push is not advertised if configured' ' · line 222
Quoted 10 lines
t/t5543-atomic-push.sh +10
-0+ git tag -d one + ) && + ( + mkdir -p upstream/.git/hooks && + cat >upstream/.git/hooks/update <<-EOF && + #!/bin/sh + + if test "\$1" = "refs/heads/bar" + then + echo >2 "Pusing to branch bar is prohibited"
Quoted 1 line
Meant to redirect to the standard error stream, not a file "2"?
It's a typo, should be "echo >&2".
cover Never report references we not push
In reply to Jiang XinA strange behavior is found when writing test cases for topic 'jx/proc-receive-hook':
Quoted 16 lines
# TODO: report for the failure of master branch is unnecessary.
test_expect_success"no proc-receive hook, fail all for atomic push" '( cd workbench && test_must_fail git push --atomic origin \ HEAD:next \ HEAD:refs/for/master/topic ) >out 2>&1 && format_git_output <out >actual && cat >expect <<-EOF && remote: # pre-receive hook remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic remote: error: cannot to find hook "proc-receive" To ../upstream ! [rejected] master (atomic push failed)
We do not push "master" branch, but git reports status for it.
Quoted 14 lines
! [remote rejected] HEAD -> next (fail to run proc-receive hook) ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook) error: failed to push some refs to "../upstream" EOF test_cmp expect actual && ( cd upstream && git show-ref ) >out && format_git_output <out >actual && cat >expect <<-EOF && <COMMIT-A> refs/heads/master EOF test_cmp expect actual
Quoted 1 line
'
This issue is introduced by commit v2.22.0-1-g3bca1e7f9f (transport-helper:
enforce atomic in push_refs_with_push, 2019-07-11). That commit wanted to fix report issue of HTTP protocol, but marked all remote references failure for atomic push.
--
Jiang Xin (3):
t5543: never report what we do not push
send-pack: mark failure of atomic push properly
transport-helper: enforce atomic in push_refs_with_push
send-pack.c | 2 + t/t5541-http-push-smart.sh | 12 ++++- t/t5543-atomic-push.sh | 92 ++++++++++++++++++++++++++++++++++++++ transport-helper.c | 15 +++++++ transport.c | 14 ------ 5 files changed, 119 insertions(+), 16 deletions(-)
Signature · 1 line
2.26.0.3.ga7a9d752d4
v2 cover Never report references we not push
From: Jiang Xin <[email protected]>
Hi,
Patches 2/4, 4/3 and 4/4 can be squashed into one commit.
## Changes since v1:
* Add a new common method reject_atomic_push(), which is reused for atomic push rejection for SSH and HTTP protocol.
* Other changes see the attached range diff.
## Range diff v1..v2
1: e1c7aa3501 ! 1: d84ebf30b5 t5543: never report what we do not push
@@ t/t5543-atomic-push.sh: test_refs () {
test_cmp expect actual
}
-+format_git_output () {
-+ awk '/^(To| !) / {print}' | \
-+ sed \
-+ -e "s/ *\$//g" \
-+ -e "s/'/\"/g"
++fmt_status_report () {
++ sed -n \
++ -e "/^To / { s/ */ /g; p; }" \
++ -e "/^ ! / { s/ */ /g; p; }"
+}
+
test_expect_success 'atomic push works for a single branch' '
@@ t/t5543-atomic-push.sh: test_expect_success 'atomic push is not advertised if co
+ mk_repo_pair &&
+ (
+ cd workbench &&
-+ # Keep constant output.
-+ git config core.abbrev 7 &&
+ test_commit one &&
+ git branch foo &&
+ git push up master one foo &&
@@ t/t5543-atomic-push.sh: test_expect_success 'atomic push is not advertised if co
+
+ if test "\$1" = "refs/heads/bar"
+ then
-+ echo >2 "Pusing to branch bar is prohibited"
++ echo >&2 "Pusing to branch bar is prohibited"
+ exit 1
+ fi
+ EOF
@@ t/t5543-atomic-push.sh: test_expect_success 'atomic push is not advertised if co
+ ) &&
+ test_must_fail git -C workbench \
+ push --atomic up master two bar >out 2>&1 &&
-+ format_git_output <out >actual &&
++ fmt_status_report <out >actual &&
+ cat >expect <<-EOF &&
+ To ../upstream
+ ! [remote rejected] master -> master (atomic push failure)
@@ t/t5543-atomic-push.sh: test_expect_success 'atomic push is not advertised if co
+ ) &&
+ test_must_fail git -C workbench \
+ push --atomic --mirror up >out 2>&1 &&
-+ format_git_output <out >actual &&
++ fmt_status_report <out >actual &&
+ cat >expect <<-EOF &&
+ To ../upstream
+ ! [remote rejected] master -> master (atomic push failure)
@@ t/t5543-atomic-push.sh: test_expect_success 'atomic push is not advertised if co
+ ) &&
+ test_must_fail git -C workbench \
+ push --atomic up master foo bar >out 2>&1 &&
-+ format_git_output <out >actual &&
++ fmt_status_report <out >actual &&
+ cat >expect <<-EOF &&
+ To ../upstream
-+ ! [rejected] master -> master (non-fast-forward)
-+ ! [rejected] bar -> bar (atomic push failed)
++ ! [rejected] master -> master (non-fast-forward)
++ ! [rejected] bar -> bar (atomic push failed)
+ EOF
+ test_cmp expect actual
+'2: 50f1e02a2f ! 2: a76340633b send-pack: mark failure of atomic push properly
@@ Metadata
## Commit message ##
send-pack: mark failure of atomic push properly
- When pusing with SSH or other smart protocol, references are validated
- by function check_to_send_update() beforce they are sent in commands
+ When pushing with SSH or other smart protocol, references are validated
+ by function check_to_send_update() before they are sent in commands
to send_pack() of "receve-pack". For atomic push, if a reference is rejected after the validation, only references pushed by user should be marked as failure, instead of report failure on all remote references.
@@ Commit message
push_refs_with_push, 2019-07-11) wanted to fix report issue of HTTP protocol, but marked all remote references failure for atomic push.
- Revert part of that commit and add additional status for function
- atomic_push_failure(). The additional status for it except the
- "REF_STATUS_EXPECTING_REPORT" status are:
+ In order to fix the issue of status report for SSH or other built-in
+ smart protocol, revert part of that commit and add additional status
+ for function atomic_push_failure(). The additional status for it
+ except the "REF_STATUS_EXPECTING_REPORT" status are:
- REF_STATUS_NONE : Not marked as "REF_STATUS_EXPECTING_REPORT" yet.
- REF_STATUS_OK : Assume OK for dryrun or status_report is disabled.
- This commit break test case in t5541, and will fix in other commit.
+ This fix won't resolve the issue of status report in transport-helper
+ for HTTP or other protocols, and breaks test case in t5541. Will fix
+ it in additional commit.
3: 50bb31ed3a ! 3: 82a8b19430 transport-helper: enforce atomic in push_refs_with_push
@@ Metadata
Author: Jiang Xin <[email protected]>
## Commit message ##
- transport-helper: enforce atomic in push_refs_with_push
+ transport-helper: mark failure for atomic push
Commit v2.22.0-1-g3bca1e7f9f (transport-helper: enforce atomic in push_refs_with_push, 2019-07-11) noticed the incomplete report of
-: ---------- > 4: fc8c1af33c transport-helper: new method reject_atomic_push()
Jiang Xin (4):
t5543: never report what we do not push
send-pack: mark failure of atomic push properly
transport-helper: mark failure for atomic push
transport-helper: new method reject_atomic_push()
send-pack.c | 27 ++---------- t/t5541-http-push-smart.sh | 12 ++++- t/t5543-atomic-push.sh | 89 ++++++++++++++++++++++++++++++++++++++ transport-helper.c | 23 ++++++++++ transport.c | 14 ------ transport.h | 3 ++ 6 files changed, 128 insertions(+), 40 deletions(-)
Signature · 1 line
2.26.0.4.g39bcdcb101.dirty
Re: [PATCH v2 0/4] Never report references we not push
Quoted 5 lines · Jiang Xin <[email protected]> 于2020年3月29日周日 下午10:33写道:
## Changes since v1:
* Add a new common method
reject_atomic_push(), which is reused for atomic push rejection for SSH and HTTP protocol.
I first try to add some common methods in transport_push() of "transport.c", but I find it is hard to do this. Because the capabilities has not been set after calling transport->vtable->get_refs_list for HTTP protocol.
And for the same reason, the following code in send-pack.c cannot move to set_ref_status_for_push().
/*
* NEEDSWORK: why does delete-refs have to be so specific to
* send-pack machinery that set_ref_status_for_push() cannot
* set this bit for us???
*/
for (ref = remote_refs; ref; ref = ref->next)
if (ref->deletion && !allow_deleting_refs)
ref->status = REF_STATUS_REJECT_NODELETEI wonder if we can extend method get_refs_list() of "transport-helper.c" and "remote-curl.c", so we can all method server_supports() in transport_push() of "transport.c".
PATCH v3 1/5 send-pack: fix inconsistent porcelain output
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
The porcelain output of a failed git-push command is inconsistent for different protocols. For example, the following git-push command failed because of the failure of pre-receive hook.
git push --porcelain origin HEAD:refs/heads/master
For SSH protocol, the porcelain output does not end with a "Done" message:
To <URL/of/upstream.git>
! HEAD:refs/heads/master [remote rejected] (pre-receive hook declined)
While for HTTP protocol, the porcelain output does end with a "Done" message;
To <URL/of/upstream.git>
! HEAD:refs/heads/master [remote rejected] (pre-receive hook declined) Done
The following code at the end of function send_pack() indicates that send_pack() should not return an error if some references are rejected in porcelain mode.
int send_pack(...)
... ...
if (args->porcelain)
return 0;
for (ref = remote_refs; ref; ref = ref->next) {
switch (ref->status) {
case REF_STATUS_NONE:
case REF_STATUS_UPTODATE:
case REF_STATUS_OK:
break;
default:
return -1;
}
}
return 0;
}So if atomic push failed, must check the porcelain mode before return an error. And receive_status() should not return an error for a failed updated reference, because send_pack() will check them instead.
--- send-pack.c | 7 +- t/t5548-push-porcelain.sh | 301 ++++++++++++++++++++++++++++++++++++++ transport.c | 10 +- 3 files changed, 313 insertions(+), 5 deletions(-) create mode 100755 t/t5548-push-porcelain.sh
send-pack.c +3 -4
@@ -190,10 +190,8 @@ static int receive_status(struct packet_reader *reader, struct ref *refs)if (reader->line[0 ] =='o' && reader->line[1 ] =='k' ) hint->status =REF_STATUS_OK ; -else { +else hint->status =REF_STATUS_REMOTE_REJECT ; - ret =-1 ; - } hint->remote_status =xstrdup_or_null (msg);/* start our next search from the next ref */ hint = hint->next;
@@ -489,7 +487,8 @@ int send_pack(struct send_pack_args *args,if (use_atomic) {strbuf_release (&req_buf);strbuf_release (&cap_buf); -return atomic_push_failure (args, remote_refs, ref); +atomic_push_failure (args, remote_refs, ref); +return args->porcelain ?0 :-1 ; }/* else fallthrough */ default :
t/t5548-push-porcelain.sh new file +301 -0
301 lines not on this page · raw message
transport.c +9 -1
@@ -715,7 +715,15 @@ static int git_transport_push(struct transport *transport, struct ref *remote_reclose (data->fd[1 ]);close (data->fd[0 ]); - ret |=finish_connect (data->conn); +/* +* Atomic push may abort the connection early and close the pipe, +* which may cause an error for `finish_connect()`. Ignore this error +* for atomic git-push. +*/ +if (ret || args.atomic) +finish_connect (data->conn); +else + ret =finish_connect (data->conn); data->conn =NULL ; data->got_remote_heads =0 ;
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
v3 cover fix git-push porcelain output and atomic report issue
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Find a new issue on porcelain output for git-push, add a new patch 1/5 to fix this issue. This fix overlaps with and the topic jx/atomic-push, so merge them together.
I find this issue when adding test cases of porcelain output for topic jx/proc-receive-hook, which depends on this fix.
## Details of this new issue
The porcelain output of a failed git-push command is inconsistent for different protocols. For example, the following git-push command failed because of the failure of pre-receive hook.
git push --porcelain origin HEAD:refs/heads/master
For SSH protocol, the porcelain output does not end with a "Done" message:
To <URL/of/upstream.git>
! HEAD:refs/heads/master [remote rejected] (pre-receive hook declined)
While for HTTP protocol, the porcelain output does end with a "Done" message;
To <URL/of/upstream.git>
! HEAD:refs/heads/master [remote rejected] (pre-receive hook declined) Done
The following code at the end of function send_pack() indicates that send_pack() should not return an error if some references are rejected in porcelain mode.
int send_pack(...)
... ...
if (args->porcelain)
return 0;
for (ref = remote_refs; ref; ref = ref->next) {
switch (ref->status) {
case REF_STATUS_NONE:
case REF_STATUS_UPTODATE:
case REF_STATUS_OK:
break;
default:
return -1;
}
}
return 0;
}So if atomic push failed, must check the porcelain mode before return an error. And receive_status() should not return an error for a failed updated reference, because send_pack() will check them instead.
## Range-diff v2...v3
-: ---------- > 1: d9ea3c35a3 send-pack: fix inconsistent porcelain output 1: 7a0579ba13 = 2: bb07d5c330 t5543: never report what we do not push 2: 9b4bca8f4c ! 3: 1aa917b097 send-pack: mark failure of atomic push properly
@@ t/t5543-atomic-push.sh: test_expect_failure 'atomic push reports (mirror, but re
(
cd workbench &&
+ ## t/t5548-push-porcelain.sh ##
+@@ t/t5548-push-porcelain.sh: run_git_push_porcelain_output_test() {
+ # Refs of upstream : master(A) bar(B) baz(A) next(A)
+ # Refs of workbench: master(B) bar(A) baz(A) next(A)
+ # git-push : master(B) bar(A) NULL next(A)
+- test_expect_success "atomic push failed ($PROTOCOL)" '
++ test_expect_failure "atomic push failed ($PROTOCOL)" '
+ (
+ cd workbench &&
+ git update-ref refs/heads/master $B &&
+
## transport.c ##
@@ transport.c: int transport_push(struct repository *r,
err = push_had_errors(remote_refs);
3: a7e8d7c893 ! 4: 2848088852 transport-helper: mark failure for atomic push
@@ t/t5541-http-push-smart.sh: test_expect_failure 'push --atomic also prevents bra
test_expect_success 'push --atomic fails on server-side errors' '
+ ## t/t5548-push-porcelain.sh ##
+@@ t/t5548-push-porcelain.sh: run_git_push_porcelain_output_test() {
+ # Refs of upstream : master(A) bar(B) baz(A) next(A)
+ # Refs of workbench: master(B) bar(A) baz(A) next(A)
+ # git-push : master(B) bar(A) NULL next(A)
+- test_expect_failure "atomic push failed ($PROTOCOL)" '
++ test_expect_success "atomic push failed ($PROTOCOL)" '
+ (
+ cd workbench &&
+ git update-ref refs/heads/master $B &&
+@@ t/t5548-push-porcelain.sh: run_git_push_porcelain_output_test() {
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ To <URL/of/upstream.git>
++ = refs/heads/next:refs/heads/next [up to date]
+ ! refs/heads/bar:refs/heads/bar [rejected] (non-fast-forward)
+ ! (delete):refs/heads/baz [rejected] (atomic push failed)
+ ! refs/heads/master:refs/heads/master [rejected] (atomic push failed)
+- ! refs/heads/next:refs/heads/next [rejected] (atomic push failed)
+ Done
+ EOF
+ test_cmp expect actual &&
+@@ t/t5548-push-porcelain.sh: run_git_push_porcelain_output_test() {
+ EOF
+ test_cmp expect actual
+ '
+-
+ test_expect_success "prepare pre-receive hook ($PROTOCOL)" '
+ write_script "$upstream/hooks/pre-receive" <<-EOF
+ exit 1
+
## transport-helper.c ##
@@ transport-helper.c: static int push_refs_with_push(struct transport *transport,
case REF_STATUS_REJECT_STALE:
4: 94b13f5dcd ! 5: d2f0b50395 transport-helper: new method reject_atomic_push()
@@ send-pack.c: int send_pack(struct send_pack_args *args,
if (use_atomic) {
strbuf_release(&req_buf);
strbuf_release(&cap_buf);
-- return atomic_push_failure(args, remote_refs, ref);
+- atomic_push_failure(args, remote_refs, ref);
+ reject_atomic_push(remote_refs, args->send_mirror);
-+ return error("atomic push failed for ref %s. status: %d\n",
-+ ref->name, ref->status);
++ error("atomic push failed for ref %s. status: %d\n",
++ ref->name, ref->status);
+ return args->porcelain ? 0 : -1;
}
/* else fallthrough */
- default:
## transport-helper.c ##
@@ transport-helper.c: static int push_refs_with_push(struct transport *transport,---
Jiang Xin (5):
send-pack: fix inconsistent porcelain output
t5543: never report what we do not push
send-pack: mark failure of atomic push properly
transport-helper: mark failure for atomic push
transport-helper: new method reject_atomic_push()
send-pack.c | 32 +--- t/t5541-http-push-smart.sh | 12 +- t/t5543-atomic-push.sh | 89 +++++++++++ t/t5548-push-porcelain.sh | 300 +++++++++++++++++++++++++++++++++++++ transport-helper.c | 23 +++ transport.c | 24 ++- transport.h | 3 + 7 files changed, 439 insertions(+), 44 deletions(-) create mode 100755 t/t5548-push-porcelain.sh
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
v4 cover fix git-push porcelain output and atomic report issue
From: Jiang Xin <[email protected]>
Patch 1/5 fixes format of porcelain output for git-push, and it should belong to a new topic, but it has a overlap with topic jx/atomic-push.
So I rebased jx/atomic-push on top of it, and resolved conflicts between these two features. Patch 2/5 ~ 5/5 are belong to the original topic jx/atomic-push.
Note: Some patches of jx/proc-receive-hook also have overlap with this
patch series, so v13 of jx/proc-receive-hook will base on this topic, instead of master branch of Git.
## Changes since jx/atomic-push v3:
* Fix broken test cases in t5504, and t5516, which is instroduced by patch 1/5..
## Changes since jx/atomic-push v2:
Add new patch 1/5 which fixes new issue found in git-push porcelain output. This fix overlaps with and the topic jx/atomic-push, so merge them together.
I find this issue when adding test cases of porcelain output for topic jx/proc-receive-hook, which depends on this fix.
Details of this new issue:
The porcelain output of a failed git-push command is inconsistent for different protocols. For example, the following git-push command may fail due to the failure of the pre-receive hook.
git push --porcelain origin HEAD:refs/heads/master
For SSH protocol, the porcelain output does not end with a "Done" message:
To <URL/of/upstream.git>
! HEAD:refs/heads/master [remote rejected] (pre-receive hook declined)
While for HTTP protocol, the porcelain output does end with a "Done" message:
To <URL/of/upstream.git>
! HEAD:refs/heads/master [remote rejected] (pre-receive hook declined) Done
The following code at the end of function send_pack() indicates that send_pack() should not return an error if some references are rejected in porcelain mode.
int send_pack(...)
... ...
if (args->porcelain)
return 0;
for (ref = remote_refs; ref; ref = ref->next) {
switch (ref->status) {
case REF_STATUS_NONE:
case REF_STATUS_UPTODATE:
case REF_STATUS_OK:
break;
default:
return -1;
}
}
return 0;
}So if atomic push failed, must check the porcelain mode before return an error. And receive_status() should not return an error for a failed updated reference, because send_pack() will check them instead.
## Range-diff v2...v4
-: ---------- > 1: 96028ebf7d send-pack: fix inconsistent porcelain output 1: 7a0579ba13 = 2: 77f66ebfd2 t5543: never report what we do not push 2: 9b4bca8f4c ! 3: cb63c4a9f4 send-pack: mark failure of atomic push properly
@@ t/t5543-atomic-push.sh: test_expect_failure 'atomic push reports (mirror, but re
(
cd workbench &&
+ ## t/t5548-push-porcelain.sh ##
+@@ t/t5548-push-porcelain.sh: run_git_push_porcelain_output_test() {
+ # Refs of upstream : master(A) bar(B) baz(A) next(A)
+ # Refs of workbench: master(B) bar(A) baz(A) next(A)
+ # git-push : master(B) bar(A) NULL next(A)
+- test_expect_success "atomic push failed ($PROTOCOL)" '
++ test_expect_failure "atomic push failed ($PROTOCOL)" '
+ (
+ cd workbench &&
+ git update-ref refs/heads/master $B &&
+
## transport.c ##
@@ transport.c: int transport_push(struct repository *r,
err = push_had_errors(remote_refs);
3: a7e8d7c893 ! 4: 3fb57569da transport-helper: mark failure for atomic push
@@ t/t5541-http-push-smart.sh: test_expect_failure 'push --atomic also prevents bra
test_expect_success 'push --atomic fails on server-side errors' '
+ ## t/t5548-push-porcelain.sh ##
+@@ t/t5548-push-porcelain.sh: run_git_push_porcelain_output_test() {
+ # Refs of upstream : master(A) bar(B) baz(A) next(A)
+ # Refs of workbench: master(B) bar(A) baz(A) next(A)
+ # git-push : master(B) bar(A) NULL next(A)
+- test_expect_failure "atomic push failed ($PROTOCOL)" '
++ test_expect_success "atomic push failed ($PROTOCOL)" '
+ (
+ cd workbench &&
+ git update-ref refs/heads/master $B &&
+@@ t/t5548-push-porcelain.sh: run_git_push_porcelain_output_test() {
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ To <URL/of/upstream.git>
++ = refs/heads/next:refs/heads/next [up to date]
+ ! refs/heads/bar:refs/heads/bar [rejected] (non-fast-forward)
+ ! (delete):refs/heads/baz [rejected] (atomic push failed)
+ ! refs/heads/master:refs/heads/master [rejected] (atomic push failed)
+- ! refs/heads/next:refs/heads/next [rejected] (atomic push failed)
+ Done
+ EOF
+ test_cmp expect actual &&
+@@ t/t5548-push-porcelain.sh: run_git_push_porcelain_output_test() {
+ EOF
+ test_cmp expect actual
+ '
+-
+ test_expect_success "prepare pre-receive hook ($PROTOCOL)" '
+ write_script "$upstream/hooks/pre-receive" <<-EOF
+ exit 1
+
## transport-helper.c ##
@@ transport-helper.c: static int push_refs_with_push(struct transport *transport,
case REF_STATUS_REJECT_STALE:
4: 94b13f5dcd ! 5: 1c73ddff0d transport-helper: new method reject_atomic_push()
@@ send-pack.c: int send_pack(struct send_pack_args *args,
if (use_atomic) {
strbuf_release(&req_buf);
strbuf_release(&cap_buf);
-- return atomic_push_failure(args, remote_refs, ref);
+- atomic_push_failure(args, remote_refs, ref);
+ reject_atomic_push(remote_refs, args->send_mirror);
-+ return error("atomic push failed for ref %s. status: %d\n",
-+ ref->name, ref->status);
++ error("atomic push failed for ref %s. status: %d\n",
++ ref->name, ref->status);
+ return args->porcelain ? 0 : -1;
}
/* else fallthrough */
- default:
## transport-helper.c ##
@@ transport-helper.c: static int push_refs_with_push(struct transport *transport,---
Jiang Xin (5):
send-pack: fix inconsistent porcelain output
t5543: never report what we do not push
send-pack: mark failure of atomic push properly
transport-helper: mark failure for atomic push
transport-helper: new method reject_atomic_push()
send-pack.c | 32 +--- t/t5504-fetch-receive-strict.sh | 1 + t/t5516-fetch-push.sh | 1 + t/t5541-http-push-smart.sh | 12 +- t/t5543-atomic-push.sh | 89 ++++++++++ t/t5548-push-porcelain.sh | 300 ++++++++++++++++++++++++++++++++ transport-helper.c | 23 +++ transport.c | 24 +-- transport.h | 3 + 9 files changed, 441 insertions(+), 44 deletions(-) create mode 100755 t/t5548-push-porcelain.sh
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v4 1/5 send-pack: fix inconsistent porcelain output
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
The porcelain output of a failed git-push command is inconsistent for different protocols. For example, the following git-push command may fail due to the failure of the pre-receive hook.
git push --porcelain origin HEAD:refs/heads/master
For SSH protocol, the porcelain output does not end with a "Done" message:
To <URL/of/upstream.git>
! HEAD:refs/heads/master [remote rejected] (pre-receive hook declined)
While for HTTP protocol, the porcelain output does end with a "Done" message:
To <URL/of/upstream.git>
! HEAD:refs/heads/master [remote rejected] (pre-receive hook declined) Done
The following code at the end of function send_pack() indicates that send_pack() should not return an error if some references are rejected in porcelain mode.
int send_pack(...)
... ...
if (args->porcelain)
return 0;
for (ref = remote_refs; ref; ref = ref->next) {
switch (ref->status) {
case REF_STATUS_NONE:
case REF_STATUS_UPTODATE:
case REF_STATUS_OK:
break;
default:
return -1;
}
}
return 0;
}So if atomic push failed, must check the porcelain mode before return an error. And receive_status() should not return an error for a failed updated reference, because send_pack() will check them instead.
--- send-pack.c | 7 +- t/t5504-fetch-receive-strict.sh | 1 + t/t5516-fetch-push.sh | 1 + t/t5548-push-porcelain.sh | 280 ++++++++++++++++++++++++++++++++ transport.c | 10 +- 5 files changed, 294 insertions(+), 5 deletions(-) create mode 100755 t/t5548-push-porcelain.sh
send-pack.c +3 -4
@@ -190,10 +190,8 @@ static int receive_status(struct packet_reader *reader, struct ref *refs)if (reader->line[0 ] =='o' && reader->line[1 ] =='k' ) hint->status =REF_STATUS_OK ; -else { +else hint->status =REF_STATUS_REMOTE_REJECT ; - ret =-1 ; - } hint->remote_status =xstrdup_or_null (msg);/* start our next search from the next ref */ hint = hint->next;
@@ -489,7 +487,8 @@ int send_pack(struct send_pack_args *args,if (use_atomic) {strbuf_release (&req_buf);strbuf_release (&cap_buf); -return atomic_push_failure (args, remote_refs, ref); +atomic_push_failure (args, remote_refs, ref); +return args->porcelain ?0 :-1 ; }/* else fallthrough */ default :
t/t5504-fetch-receive-strict.sh +1 -0
@@ -65,6 +65,7 @@ test_expect_success 'fetch with transfer.fsckobjects' ' cat >exp <<EOF To dst ! refs/heads/master:refs/heads/test [remote rejected] (missing necessary objects) +Done EOF test_expect_success 'push without strict' '
t/t5516-fetch-push.sh +1 -0
@@ -1066,6 +1066,7 @@ test_expect_success 'push --porcelain rejected' ' echo >.git/foo "To testrepo" && echo >>.git/foo "! refs/heads/master:refs/heads/master [remote rejected] (branch is currently checked out)" && + echo >>.git/foo "Done" && test_must_fail git push >.git/bar --porcelain testrepo refs/heads/master:refs/heads/master && test_cmp .git/foo .git/bar
t/t5548-push-porcelain.sh new file +280 -0
280 lines not on this page · raw message
transport.c +9 -1
@@ -715,7 +715,15 @@ static int git_transport_push(struct transport *transport, struct ref *remote_reclose (data->fd[1 ]);close (data->fd[0 ]); - ret |=finish_connect (data->conn); +/* +* Atomic push may abort the connection early and close the pipe, +* which may cause an error for `finish_connect()`. Ignore this error +* for atomic git-push. +*/ +if (ret || args.atomic) +finish_connect (data->conn); +else + ret =finish_connect (data->conn); data->conn =NULL ; data->got_remote_heads =0 ;
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
Re: [PATCH v4 1/5] send-pack: fix inconsistent porcelain output
Quoted 8 lines · Jiang Xin
transport.c +0
-0@@ -715,7 +715,15 @@ static int git_transport_push(struct transport *transport, struct ref *remote_reclose (data->fd[1 ]);close (data->fd[0 ]);
transport.c static int git_transport_push(struct transport *transport, struct ref *remote_re · line 726
Quoted 10 lines
transport.c +9
-1- ret |=finish_connect (data->conn); +/* +* Atomic push may abort the connection early and close the pipe, +* which may cause an error for `finish_connect()`. Ignore this error +* for atomic git-push. +*/ +if (ret || args.atomic) +finish_connect (data->conn); +else + ret =finish_connect (data->conn);
Which means that the return value from this function under the atomic mode is what we got from an earlier call to send_pack().
Which may have failed, in which case we do want to relay the error to our caller, or may have succeeded, which is also fine to relay to our caller.
Quoted 3 lines
The following code at the end of function
send_pack()indicates thatsend_pack()should not return an error if some references are rejected in porcelain mode.
Under porcelain mode, if we already saw an error before we get to the "if porcelain, do not bother checking the remote status", we do relay the error to our caller. That happens in the part you omitted from your quote:
Quoted 2 lines
int send_pack(...)
... ...if (ret < 0) return ret;
Quoted 16 lines
if (args->porcelain)
return 0;
for (ref = remote_refs; ref; ref = ref->next) {
switch (ref->status) {
case REF_STATUS_NONE:
case REF_STATUS_UPTODATE:
case REF_STATUS_OK:
break;
default:
return -1;
}
}
return 0;
}So it is not like we are disabling all errors under the porcelain mode.
send-pack.c int send_pack(struct send_pack_args *args, · line 492
Quoted 12 lines
send-pack.c +2
-1@@ -489,7 +487,8 @@ int send_pack(struct send_pack_args *args,if (use_atomic) {strbuf_release (&req_buf);strbuf_release (&cap_buf); -return atomic_push_failure (args, remote_refs, ref); +atomic_push_failure (args, remote_refs, ref); +return args->porcelain ?0 :-1 ; }
And this is in line with the way how rejected pushes are handled, I guess.
Sounds a bit convoluted but correct ;-)
PATCH v4 2/5 t5543: never report what we do not push
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
When we push some references to the git server, we expect git to report the status of the references we are pushing; no more, no less. But when pusing with atomic mode, if some references cannot be pushed, Git reports the reject message on all references in the remote repository.
Add new test cases in t5543, and fix them in latter commit.
--- t/t5543-atomic-push.sh | 89 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+)
t/t5543-atomic-push.sh +89 -0
@@ -27,6 +27,12 @@ test_refs () { test_cmp expect actual } +fmt_status_report () { + sed -n \ + -e "/^To / { s/ */ /g; p; }" \ + -e "/^ ! / { s/ */ /g; p; }" +} + test_expect_success 'atomic push works for a single branch' ' mk_repo_pair && (
@@ -191,4 +197,87 @@ test_expect_success 'atomic push is not advertised if configured' ' test_refs master HEAD@{1} ' +# References in upstream : master(1) one(1) foo(1) +# References in workbench: master(2) foo(1) two(2) bar(2) +# Atomic push : master(2) two(2) bar(2) +test_expect_failure 'atomic push reports (reject by update hook)' ' + mk_repo_pair && + ( + cd workbench && + test_commit one && + git branch foo && + git push up master one foo && + git tag -d one + ) && + ( + mkdir -p upstream/.git/hooks && + cat >upstream/.git/hooks/update <<-EOF && + #!/bin/sh + + if test "\$1" = "refs/heads/bar" + then + echo >&2 "Pusing to branch bar is prohibited" + exit 1 + fi + EOF + chmod a+x upstream/.git/hooks/update + ) && + ( + cd workbench && + test_commit two && + git branch bar + ) && + test_must_fail git -C workbench \ + push --atomic up master two bar >out 2>&1 && + fmt_status_report <out >actual && + cat >expect <<-EOF && + To ../upstream + ! [remote rejected] master -> master (atomic push failure) + ! [remote rejected] two -> two (atomic push failure) + ! [remote rejected] bar -> bar (hook declined) + EOF + test_cmp expect actual +' + +# References in upstream : master(1) one(1) foo(1) +# References in workbench: master(2) foo(1) two(2) bar(2) +test_expect_failure 'atomic push reports (mirror, but reject by update hook)' ' + ( + cd workbench && + git remote remove up && + git remote add up ../upstream + ) && + test_must_fail git -C workbench \ + push --atomic --mirror up >out 2>&1 && + fmt_status_report <out >actual && + cat >expect <<-EOF && + To ../upstream + ! [remote rejected] master -> master (atomic push failure) + ! [remote rejected] one (atomic push failure) + ! [remote rejected] bar -> bar (hook declined) + ! [remote rejected] two -> two (atomic push failure) + EOF + test_cmp expect actual +' + +# References in upstream : master(2) one(1) foo(1) +# References in workbench: master(1) foo(1) two(2) bar(2) +test_expect_failure 'atomic push reports (reject by non-ff)' ' + rm upstream/.git/hooks/update && + ( + cd workbench && + git push up master && + git reset --hard HEAD^ + ) && + test_must_fail git -C workbench \ + push --atomic up master foo bar >out 2>&1 && + fmt_status_report <out >actual && + cat >expect <<-EOF && + To ../upstream + ! [rejected] master -> master (non-fast-forward) + ! [rejected] bar -> bar (atomic push failed) + EOF + test_cmp expect actual +' + test_done
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v4 3/5 send-pack: mark failure of atomic push properly
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
When pushing with SSH or other smart protocol, references are validated by function check_to_send_update() before they are sent in commands to send_pack() of "receve-pack". For atomic push, if a reference is rejected after the validation, only references pushed by user should be marked as failure, instead of report failure on all remote references.
Commit v2.22.0-1-g3bca1e7f9f (transport-helper: enforce atomic in push_refs_with_push, 2019-07-11) wanted to fix report issue of HTTP protocol, but marked all remote references failure for atomic push.
In order to fix the issue of status report for SSH or other built-in smart protocol, revert part of that commit and add additional status for function atomic_push_failure(). The additional status for it except the "REF_STATUS_EXPECTING_REPORT" status are:
- REF_STATUS_NONE : Not marked as "REF_STATUS_EXPECTING_REPORT" yet.
- REF_STATUS_OK : Assume OK for dryrun or status_report is disabled.
This fix won't resolve the issue of status report in transport-helper for HTTP or other protocols, and breaks test case in t5541. Will fix it in additional commit.
--- send-pack.c | 2 ++ t/t5541-http-push-smart.sh | 2 +- t/t5543-atomic-push.sh | 6 +++--- t/t5548-push-porcelain.sh | 2 +- transport.c | 14 -------------- 5 files changed, 7 insertions(+), 19 deletions(-)
send-pack.c +2 -0
@@ -332,6 +332,8 @@ static int atomic_push_failure(struct send_pack_args *args,continue ;switch (ref->status) { +case REF_STATUS_NONE : +case REF_STATUS_OK :case REF_STATUS_EXPECTING_REPORT : ref->status =REF_STATUS_ATOMIC_PUSH_FAILED ;continue ;
t/t5541-http-push-smart.sh +1 -1
@@ -177,7 +177,7 @@ test_expect_success 'push (chunked)' ' test $HEAD = $(git rev-parse --verify HEAD)) ' -test_expect_success 'push --atomic also prevents branch creation, reports collateral' ' +test_expect_failure 'push --atomic also prevents branch creation, reports collateral' ' # Setup upstream repo - empty for now d=$HTTPD_DOCUMENT_ROOT_PATH/atomic-branches.git && git init --bare "$d" &&
t/t5543-atomic-push.sh +3 -3
@@ -200,7 +200,7 @@ test_expect_success 'atomic push is not advertised if configured' ' # References in upstream : master(1) one(1) foo(1) # References in workbench: master(2) foo(1) two(2) bar(2) # Atomic push : master(2) two(2) bar(2) -test_expect_failure 'atomic push reports (reject by update hook)' ' +test_expect_success 'atomic push reports (reject by update hook)' ' mk_repo_pair && ( cd workbench &&
@@ -241,7 +241,7 @@ test_expect_failure 'atomic push reports (reject by update hook)' ' # References in upstream : master(1) one(1) foo(1) # References in workbench: master(2) foo(1) two(2) bar(2) -test_expect_failure 'atomic push reports (mirror, but reject by update hook)' ' +test_expect_success 'atomic push reports (mirror, but reject by update hook)' ' ( cd workbench && git remote remove up &&
@@ -262,7 +262,7 @@ test_expect_failure 'atomic push reports (mirror, but reject by update hook)' ' # References in upstream : master(2) one(1) foo(1) # References in workbench: master(1) foo(1) two(2) bar(2) -test_expect_failure 'atomic push reports (reject by non-ff)' ' +test_expect_success 'atomic push reports (reject by non-ff)' ' rm upstream/.git/hooks/update && ( cd workbench &&
t/t5548-push-porcelain.sh +1 -1
@@ -136,7 +136,7 @@ run_git_push_porcelain_output_test() { # Refs of upstream : master(A) bar(B) baz(A) next(A) # Refs of workbench: master(B) bar(A) baz(A) next(A) # git-push : master(B) bar(A) NULL next(A) - test_expect_success "atomic push failed ($PROTOCOL)" ' + test_expect_failure "atomic push failed ($PROTOCOL)" ' ( cd workbench && git update-ref refs/heads/master $B &&
transport.c +0 -14
@@ -1248,20 +1248,6 @@ int transport_push(struct repository *r, err =push_had_errors (remote_refs); ret = push_ret | err; -if ((flags &TRANSPORT_PUSH_ATOMIC ) && err) { -struct ref *it; -for (it = remote_refs; it; it = it->next) -switch (it->status) { -case REF_STATUS_NONE : -case REF_STATUS_UPTODATE : -case REF_STATUS_OK : - it->status =REF_STATUS_ATOMIC_PUSH_FAILED ; -break ; -default : -break ; - } - } -if (!quiet || err) transport_print_push_status(transport->url, remote_refs, verbose | porcelain, porcelain,
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v4 4/5 transport-helper: mark failure for atomic push
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Commit v2.22.0-1-g3bca1e7f9f (transport-helper: enforce atomic in push_refs_with_push, 2019-07-11) noticed the incomplete report of failure of an atomic push for HTTP protocol. But the implementation has a flaw that mark all remote references as failure.
Only mark necessary references as failure in push_refs_with_push() of transport-helper.
--- t/t5541-http-push-smart.sh | 14 +++++++++++--- t/t5548-push-porcelain.sh | 5 ++--- transport-helper.c | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 6 deletions(-)
t/t5541-http-push-smart.sh +11 -3
@@ -177,7 +177,10 @@ test_expect_success 'push (chunked)' ' test $HEAD = $(git rev-parse --verify HEAD)) ' -test_expect_failure 'push --atomic also prevents branch creation, reports collateral' ' +## References of remote: atomic1(1) master(2) collateral(2) other(2) +## References of local : atomic2(2) master(1) collateral(3) other(2) collateral1(3) atomic(1) +## Atomic push : master(1) collateral(3) atomic(1) +test_expect_success 'push --atomic also prevents branch creation, reports collateral' ' # Setup upstream repo - empty for now d=$HTTPD_DOCUMENT_ROOT_PATH/atomic-branches.git && git init --bare "$d" &&
@@ -189,7 +192,8 @@ test_expect_failure 'push --atomic also prevents branch creation, reports collat test_commit atomic2 && git branch collateral && git branch other && - git push "$up" master collateral other && + git push "$up" atomic1 master collateral other && + git tag -d atomic1 && # collateral is a valid push, but should be failed by atomic push git checkout collateral &&
@@ -224,7 +228,11 @@ test_expect_failure 'push --atomic also prevents branch creation, reports collat # the collateral failure refs should be indicated to the user grep "^ ! .*rejected.* atomic -> atomic .*atomic push failed" output && - grep "^ ! .*rejected.* collateral -> collateral .*atomic push failed" output + grep "^ ! .*rejected.* collateral -> collateral .*atomic push failed" output && + + # never report what we do not push + ! grep "^ ! .*rejected.* atomic1 " output && + ! grep "^ ! .*rejected.* other " output ' test_expect_success 'push --atomic fails on server-side errors' '
t/t5548-push-porcelain.sh +2 -3
@@ -136,7 +136,7 @@ run_git_push_porcelain_output_test() { # Refs of upstream : master(A) bar(B) baz(A) next(A) # Refs of workbench: master(B) bar(A) baz(A) next(A) # git-push : master(B) bar(A) NULL next(A) - test_expect_failure "atomic push failed ($PROTOCOL)" ' + test_expect_success "atomic push failed ($PROTOCOL)" ' ( cd workbench && git update-ref refs/heads/master $B &&
@@ -150,10 +150,10 @@ run_git_push_porcelain_output_test() { make_user_friendly_and_stable_output <out >actual && cat >expect <<-EOF && To <URL/of/upstream.git> + = refs/heads/next:refs/heads/next [up to date] ! refs/heads/bar:refs/heads/bar [rejected] (non-fast-forward) ! (delete):refs/heads/baz [rejected] (atomic push failed) ! refs/heads/master:refs/heads/master [rejected] (atomic push failed) - ! refs/heads/next:refs/heads/next [rejected] (atomic push failed) Done EOF test_cmp expect actual &&
@@ -168,7 +168,6 @@ run_git_push_porcelain_output_test() { EOF test_cmp expect actual ' - test_expect_success "prepare pre-receive hook ($PROTOCOL)" ' write_script "$upstream/hooks/pre-receive" <<-EOF exit 1
transport-helper.c +15 -0
@@ -894,6 +894,21 @@ static int push_refs_with_push(struct transport *transport,case REF_STATUS_REJECT_STALE :case REF_STATUS_REJECT_ALREADY_EXISTS :if (atomic) { +/* Mark other refs as failed */ +for (ref = remote_refs; ref; ref = ref->next) { +if (!ref->peer_ref && !mirror) +continue ; + +switch (ref->status) { +case REF_STATUS_NONE : +case REF_STATUS_OK : +case REF_STATUS_EXPECTING_REPORT : + ref->status =REF_STATUS_ATOMIC_PUSH_FAILED ; +continue ; +default : +break ;/* do nothing */ + } + }string_list_clear (&cas_options,0 );return 0 ; }else
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v4 5/5 transport-helper: new method reject_atomic_push()
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Add new method in transport-helper to reject all references if any reference is failed for atomic push.
This method is reused in "send-pack.c" and "transport-helper.c", one for SSH, git and file protocols, and the other for HTTP protocol.
--- send-pack.c | 29 +++-------------------------- transport-helper.c | 38 +++++++++++++++++++++++--------------- transport.h | 3 +++ 3 files changed, 29 insertions(+), 41 deletions(-)
send-pack.c +3 -26
@@ -320,31 +320,6 @@ static int generate_push_cert(struct strbuf *req_buf,return update_seen; } - -static int atomic_push_failure (struct send_pack_args *args, -struct ref *remote_refs, -struct ref *failing_ref) -{ -struct ref *ref; -/* Mark other refs as failed */ -for (ref = remote_refs; ref; ref = ref->next) { -if (!ref->peer_ref && !args->send_mirror) -continue ; - -switch (ref->status) { -case REF_STATUS_NONE : -case REF_STATUS_OK : -case REF_STATUS_EXPECTING_REPORT : - ref->status =REF_STATUS_ATOMIC_PUSH_FAILED ; -continue ; -default : -break ;/* do nothing */ - } - } -return error ("atomic push failed for ref %s. status: %d\n" , - failing_ref->name, failing_ref->status); -} -#define NONCE_LEN_LIMIT 256static void reject_invalid_nonce (const char *nonce,int len)
@@ -489,7 +464,9 @@ int send_pack(struct send_pack_args *args,if (use_atomic) {strbuf_release (&req_buf);strbuf_release (&cap_buf); -atomic_push_failure (args, remote_refs, ref); +reject_atomic_push (remote_refs, args->send_mirror); +error ("atomic push failed for ref %s. status: %d\n" , + ref->name, ref->status);return args->porcelain ?0 :-1 ; }/* else fallthrough */
transport-helper.c +23 -15
@@ -894,21 +894,7 @@ static int push_refs_with_push(struct transport *transport,case REF_STATUS_REJECT_STALE :case REF_STATUS_REJECT_ALREADY_EXISTS :if (atomic) { -/* Mark other refs as failed */ -for (ref = remote_refs; ref; ref = ref->next) { -if (!ref->peer_ref && !mirror) -continue ; - -switch (ref->status) { -case REF_STATUS_NONE : -case REF_STATUS_OK : -case REF_STATUS_EXPECTING_REPORT : - ref->status =REF_STATUS_ATOMIC_PUSH_FAILED ; -continue ; -default : -break ;/* do nothing */ - } - } +reject_atomic_push (remote_refs, mirror);string_list_clear (&cas_options,0 );return 0 ; }else
@@ -1503,3 +1489,25 @@ int bidirectional_transfer_loop(int input, int output)return tloop_spawnwait_tasks (&state); } + +void reject_atomic_push (struct ref *remote_refs,int mirror_mode) +{ +struct ref *ref; + +/* Mark other refs as failed */ +for (ref = remote_refs; ref; ref = ref->next) { +if (!ref->peer_ref && !mirror_mode) +continue ; + +switch (ref->status) { +case REF_STATUS_NONE : +case REF_STATUS_OK : +case REF_STATUS_EXPECTING_REPORT : + ref->status =REF_STATUS_ATOMIC_PUSH_FAILED ; +continue ; +default : +break ;/* do nothing */ + } + } +return ; +}
transport.h +3 -0
@@ -265,4 +265,7 @@ int transport_refs_pushed(struct ref *ref);void transport_print_push_status (const char *dest,struct ref *refs,int verbose,int porcelain,unsigned int *reject_reasons); +/* common method used by transport-helper.c and send-pack.c */ +void reject_atomic_push (struct ref *refs,int mirror_mode); +#endif
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v3 2/5 t5543: never report what we do not push
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
When we push some references to the git server, we expect git to report the status of the references we are pushing; no more, no less. But when pusing with atomic mode, if some references cannot be pushed, Git reports the reject message on all references in the remote repository.
Add new test cases in t5543, and fix them in latter commit.
--- t/t5543-atomic-push.sh | 89 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+)
t/t5543-atomic-push.sh +89 -0
@@ -27,6 +27,12 @@ test_refs () { test_cmp expect actual } +fmt_status_report () { + sed -n \ + -e "/^To / { s/ */ /g; p; }" \ + -e "/^ ! / { s/ */ /g; p; }" +} + test_expect_success 'atomic push works for a single branch' ' mk_repo_pair && (
@@ -191,4 +197,87 @@ test_expect_success 'atomic push is not advertised if configured' ' test_refs master HEAD@{1} ' +# References in upstream : master(1) one(1) foo(1) +# References in workbench: master(2) foo(1) two(2) bar(2) +# Atomic push : master(2) two(2) bar(2) +test_expect_failure 'atomic push reports (reject by update hook)' ' + mk_repo_pair && + ( + cd workbench && + test_commit one && + git branch foo && + git push up master one foo && + git tag -d one + ) && + ( + mkdir -p upstream/.git/hooks && + cat >upstream/.git/hooks/update <<-EOF && + #!/bin/sh + + if test "\$1" = "refs/heads/bar" + then + echo >&2 "Pusing to branch bar is prohibited" + exit 1 + fi + EOF + chmod a+x upstream/.git/hooks/update + ) && + ( + cd workbench && + test_commit two && + git branch bar + ) && + test_must_fail git -C workbench \ + push --atomic up master two bar >out 2>&1 && + fmt_status_report <out >actual && + cat >expect <<-EOF && + To ../upstream + ! [remote rejected] master -> master (atomic push failure) + ! [remote rejected] two -> two (atomic push failure) + ! [remote rejected] bar -> bar (hook declined) + EOF + test_cmp expect actual +' + +# References in upstream : master(1) one(1) foo(1) +# References in workbench: master(2) foo(1) two(2) bar(2) +test_expect_failure 'atomic push reports (mirror, but reject by update hook)' ' + ( + cd workbench && + git remote remove up && + git remote add up ../upstream + ) && + test_must_fail git -C workbench \ + push --atomic --mirror up >out 2>&1 && + fmt_status_report <out >actual && + cat >expect <<-EOF && + To ../upstream + ! [remote rejected] master -> master (atomic push failure) + ! [remote rejected] one (atomic push failure) + ! [remote rejected] bar -> bar (hook declined) + ! [remote rejected] two -> two (atomic push failure) + EOF + test_cmp expect actual +' + +# References in upstream : master(2) one(1) foo(1) +# References in workbench: master(1) foo(1) two(2) bar(2) +test_expect_failure 'atomic push reports (reject by non-ff)' ' + rm upstream/.git/hooks/update && + ( + cd workbench && + git push up master && + git reset --hard HEAD^ + ) && + test_must_fail git -C workbench \ + push --atomic up master foo bar >out 2>&1 && + fmt_status_report <out >actual && + cat >expect <<-EOF && + To ../upstream + ! [rejected] master -> master (non-fast-forward) + ! [rejected] bar -> bar (atomic push failed) + EOF + test_cmp expect actual +' + test_done
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v3 3/5 send-pack: mark failure of atomic push properly
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
When pushing with SSH or other smart protocol, references are validated by function check_to_send_update() before they are sent in commands to send_pack() of "receve-pack". For atomic push, if a reference is rejected after the validation, only references pushed by user should be marked as failure, instead of report failure on all remote references.
Commit v2.22.0-1-g3bca1e7f9f (transport-helper: enforce atomic in push_refs_with_push, 2019-07-11) wanted to fix report issue of HTTP protocol, but marked all remote references failure for atomic push.
In order to fix the issue of status report for SSH or other built-in smart protocol, revert part of that commit and add additional status for function atomic_push_failure(). The additional status for it except the "REF_STATUS_EXPECTING_REPORT" status are:
- REF_STATUS_NONE : Not marked as "REF_STATUS_EXPECTING_REPORT" yet.
- REF_STATUS_OK : Assume OK for dryrun or status_report is disabled.
This fix won't resolve the issue of status report in transport-helper for HTTP or other protocols, and breaks test case in t5541. Will fix it in additional commit.
--- send-pack.c | 2 ++ t/t5541-http-push-smart.sh | 2 +- t/t5543-atomic-push.sh | 6 +++--- t/t5548-push-porcelain.sh | 2 +- transport.c | 14 -------------- 5 files changed, 7 insertions(+), 19 deletions(-)
send-pack.c +2 -0
@@ -332,6 +332,8 @@ static int atomic_push_failure(struct send_pack_args *args,continue ;switch (ref->status) { +case REF_STATUS_NONE : +case REF_STATUS_OK :case REF_STATUS_EXPECTING_REPORT : ref->status =REF_STATUS_ATOMIC_PUSH_FAILED ;continue ;
t/t5541-http-push-smart.sh +1 -1
@@ -177,7 +177,7 @@ test_expect_success 'push (chunked)' ' test $HEAD = $(git rev-parse --verify HEAD)) ' -test_expect_success 'push --atomic also prevents branch creation, reports collateral' ' +test_expect_failure 'push --atomic also prevents branch creation, reports collateral' ' # Setup upstream repo - empty for now d=$HTTPD_DOCUMENT_ROOT_PATH/atomic-branches.git && git init --bare "$d" &&
t/t5543-atomic-push.sh +3 -3
@@ -200,7 +200,7 @@ test_expect_success 'atomic push is not advertised if configured' ' # References in upstream : master(1) one(1) foo(1) # References in workbench: master(2) foo(1) two(2) bar(2) # Atomic push : master(2) two(2) bar(2) -test_expect_failure 'atomic push reports (reject by update hook)' ' +test_expect_success 'atomic push reports (reject by update hook)' ' mk_repo_pair && ( cd workbench &&
@@ -241,7 +241,7 @@ test_expect_failure 'atomic push reports (reject by update hook)' ' # References in upstream : master(1) one(1) foo(1) # References in workbench: master(2) foo(1) two(2) bar(2) -test_expect_failure 'atomic push reports (mirror, but reject by update hook)' ' +test_expect_success 'atomic push reports (mirror, but reject by update hook)' ' ( cd workbench && git remote remove up &&
@@ -262,7 +262,7 @@ test_expect_failure 'atomic push reports (mirror, but reject by update hook)' ' # References in upstream : master(2) one(1) foo(1) # References in workbench: master(1) foo(1) two(2) bar(2) -test_expect_failure 'atomic push reports (reject by non-ff)' ' +test_expect_success 'atomic push reports (reject by non-ff)' ' rm upstream/.git/hooks/update && ( cd workbench &&
t/t5548-push-porcelain.sh +1 -1
@@ -110,7 +110,7 @@ run_git_push_porcelain_output_test() { # Refs of upstream : master(A) bar(B) baz(A) next(A) # Refs of workbench: master(B) bar(A) baz(A) next(A) # git-push : master(B) bar(A) NULL next(A) - test_expect_success "atomic push failed ($PROTOCOL)" ' + test_expect_failure "atomic push failed ($PROTOCOL)" ' ( cd workbench && git update-ref refs/heads/master $B &&
transport.c +0 -14
@@ -1248,20 +1248,6 @@ int transport_push(struct repository *r, err =push_had_errors (remote_refs); ret = push_ret | err; -if ((flags &TRANSPORT_PUSH_ATOMIC ) && err) { -struct ref *it; -for (it = remote_refs; it; it = it->next) -switch (it->status) { -case REF_STATUS_NONE : -case REF_STATUS_UPTODATE : -case REF_STATUS_OK : - it->status =REF_STATUS_ATOMIC_PUSH_FAILED ; -break ; -default : -break ; - } - } -if (!quiet || err) transport_print_push_status(transport->url, remote_refs, verbose | porcelain, porcelain,
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v3 4/5 transport-helper: mark failure for atomic push
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Commit v2.22.0-1-g3bca1e7f9f (transport-helper: enforce atomic in push_refs_with_push, 2019-07-11) noticed the incomplete report of failure of an atomic push for HTTP protocol. But the implementation has a flaw that mark all remote references as failure.
Only mark necessary references as failure in push_refs_with_push() of transport-helper.
--- t/t5541-http-push-smart.sh | 14 +++++++++++--- t/t5548-push-porcelain.sh | 5 ++--- transport-helper.c | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 6 deletions(-)
t/t5541-http-push-smart.sh +11 -3
@@ -177,7 +177,10 @@ test_expect_success 'push (chunked)' ' test $HEAD = $(git rev-parse --verify HEAD)) ' -test_expect_failure 'push --atomic also prevents branch creation, reports collateral' ' +## References of remote: atomic1(1) master(2) collateral(2) other(2) +## References of local : atomic2(2) master(1) collateral(3) other(2) collateral1(3) atomic(1) +## Atomic push : master(1) collateral(3) atomic(1) +test_expect_success 'push --atomic also prevents branch creation, reports collateral' ' # Setup upstream repo - empty for now d=$HTTPD_DOCUMENT_ROOT_PATH/atomic-branches.git && git init --bare "$d" &&
@@ -189,7 +192,8 @@ test_expect_failure 'push --atomic also prevents branch creation, reports collat test_commit atomic2 && git branch collateral && git branch other && - git push "$up" master collateral other && + git push "$up" atomic1 master collateral other && + git tag -d atomic1 && # collateral is a valid push, but should be failed by atomic push git checkout collateral &&
@@ -224,7 +228,11 @@ test_expect_failure 'push --atomic also prevents branch creation, reports collat # the collateral failure refs should be indicated to the user grep "^ ! .*rejected.* atomic -> atomic .*atomic push failed" output && - grep "^ ! .*rejected.* collateral -> collateral .*atomic push failed" output + grep "^ ! .*rejected.* collateral -> collateral .*atomic push failed" output && + + # never report what we do not push + ! grep "^ ! .*rejected.* atomic1 " output && + ! grep "^ ! .*rejected.* other " output ' test_expect_success 'push --atomic fails on server-side errors' '
t/t5548-push-porcelain.sh +2 -3
@@ -110,7 +110,7 @@ run_git_push_porcelain_output_test() { # Refs of upstream : master(A) bar(B) baz(A) next(A) # Refs of workbench: master(B) bar(A) baz(A) next(A) # git-push : master(B) bar(A) NULL next(A) - test_expect_failure "atomic push failed ($PROTOCOL)" ' + test_expect_success "atomic push failed ($PROTOCOL)" ' ( cd workbench && git update-ref refs/heads/master $B &&
@@ -124,10 +124,10 @@ run_git_push_porcelain_output_test() { make_user_friendly_and_stable_output <out >actual && cat >expect <<-EOF && To <URL/of/upstream.git> + = refs/heads/next:refs/heads/next [up to date] ! refs/heads/bar:refs/heads/bar [rejected] (non-fast-forward) ! (delete):refs/heads/baz [rejected] (atomic push failed) ! refs/heads/master:refs/heads/master [rejected] (atomic push failed) - ! refs/heads/next:refs/heads/next [rejected] (atomic push failed) Done EOF test_cmp expect actual &&
@@ -142,7 +142,6 @@ run_git_push_porcelain_output_test() { EOF test_cmp expect actual ' - test_expect_success "prepare pre-receive hook ($PROTOCOL)" ' write_script "$upstream/hooks/pre-receive" <<-EOF exit 1
transport-helper.c +15 -0
@@ -894,6 +894,21 @@ static int push_refs_with_push(struct transport *transport,case REF_STATUS_REJECT_STALE :case REF_STATUS_REJECT_ALREADY_EXISTS :if (atomic) { +/* Mark other refs as failed */ +for (ref = remote_refs; ref; ref = ref->next) { +if (!ref->peer_ref && !mirror) +continue ; + +switch (ref->status) { +case REF_STATUS_NONE : +case REF_STATUS_OK : +case REF_STATUS_EXPECTING_REPORT : + ref->status =REF_STATUS_ATOMIC_PUSH_FAILED ; +continue ; +default : +break ;/* do nothing */ + } + }string_list_clear (&cas_options,0 );return 0 ; }else
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v3 5/5 transport-helper: new method reject_atomic_push()
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Add new method in transport-helper to reject all references if any reference is failed for atomic push.
This method is reused in "send-pack.c" and "transport-helper.c", one for SSH, git and file protocols, and the other for HTTP protocol.
--- send-pack.c | 29 +++-------------------------- transport-helper.c | 38 +++++++++++++++++++++++--------------- transport.h | 3 +++ 3 files changed, 29 insertions(+), 41 deletions(-)
send-pack.c +3 -26
@@ -320,31 +320,6 @@ static int generate_push_cert(struct strbuf *req_buf,return update_seen; } - -static int atomic_push_failure (struct send_pack_args *args, -struct ref *remote_refs, -struct ref *failing_ref) -{ -struct ref *ref; -/* Mark other refs as failed */ -for (ref = remote_refs; ref; ref = ref->next) { -if (!ref->peer_ref && !args->send_mirror) -continue ; - -switch (ref->status) { -case REF_STATUS_NONE : -case REF_STATUS_OK : -case REF_STATUS_EXPECTING_REPORT : - ref->status =REF_STATUS_ATOMIC_PUSH_FAILED ; -continue ; -default : -break ;/* do nothing */ - } - } -return error ("atomic push failed for ref %s. status: %d\n" , - failing_ref->name, failing_ref->status); -} -#define NONCE_LEN_LIMIT 256static void reject_invalid_nonce (const char *nonce,int len)
@@ -489,7 +464,9 @@ int send_pack(struct send_pack_args *args,if (use_atomic) {strbuf_release (&req_buf);strbuf_release (&cap_buf); -atomic_push_failure (args, remote_refs, ref); +reject_atomic_push (remote_refs, args->send_mirror); +error ("atomic push failed for ref %s. status: %d\n" , + ref->name, ref->status);return args->porcelain ?0 :-1 ; }/* else fallthrough */
transport-helper.c +23 -15
@@ -894,21 +894,7 @@ static int push_refs_with_push(struct transport *transport,case REF_STATUS_REJECT_STALE :case REF_STATUS_REJECT_ALREADY_EXISTS :if (atomic) { -/* Mark other refs as failed */ -for (ref = remote_refs; ref; ref = ref->next) { -if (!ref->peer_ref && !mirror) -continue ; - -switch (ref->status) { -case REF_STATUS_NONE : -case REF_STATUS_OK : -case REF_STATUS_EXPECTING_REPORT : - ref->status =REF_STATUS_ATOMIC_PUSH_FAILED ; -continue ; -default : -break ;/* do nothing */ - } - } +reject_atomic_push (remote_refs, mirror);string_list_clear (&cas_options,0 );return 0 ; }else
@@ -1503,3 +1489,25 @@ int bidirectional_transfer_loop(int input, int output)return tloop_spawnwait_tasks (&state); } + +void reject_atomic_push (struct ref *remote_refs,int mirror_mode) +{ +struct ref *ref; + +/* Mark other refs as failed */ +for (ref = remote_refs; ref; ref = ref->next) { +if (!ref->peer_ref && !mirror_mode) +continue ; + +switch (ref->status) { +case REF_STATUS_NONE : +case REF_STATUS_OK : +case REF_STATUS_EXPECTING_REPORT : + ref->status =REF_STATUS_ATOMIC_PUSH_FAILED ; +continue ; +default : +break ;/* do nothing */ + } + } +return ; +}
transport.h +3 -0
@@ -265,4 +265,7 @@ int transport_refs_pushed(struct ref *ref);void transport_print_push_status (const char *dest,struct ref *refs,int verbose,int porcelain,unsigned int *reject_reasons); +/* common method used by transport-helper.c and send-pack.c */ +void reject_atomic_push (struct ref *refs,int mirror_mode); +#endif
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v2 1/4 t5543: never report what we do not push
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
When we push some references to the git server, we expect git to report the status of the references we are pushing; no more, no less. But when pusing with atomic mode, if some references cannot be pushed, Git reports the reject message on all references in the remote repository.
Add new test cases in t5543, and fix them in latter commit.
--- t/t5543-atomic-push.sh | 89 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+)
t/t5543-atomic-push.sh +89 -0
@@ -27,6 +27,12 @@ test_refs () { test_cmp expect actual } +fmt_status_report () { + sed -n \ + -e "/^To / { s/ */ /g; p; }" \ + -e "/^ ! / { s/ */ /g; p; }" +} + test_expect_success 'atomic push works for a single branch' ' mk_repo_pair && (
@@ -191,4 +197,87 @@ test_expect_success 'atomic push is not advertised if configured' ' test_refs master HEAD@{1} ' +# References in upstream : master(1) one(1) foo(1) +# References in workbench: master(2) foo(1) two(2) bar(2) +# Atomic push : master(2) two(2) bar(2) +test_expect_failure 'atomic push reports (reject by update hook)' ' + mk_repo_pair && + ( + cd workbench && + test_commit one && + git branch foo && + git push up master one foo && + git tag -d one + ) && + ( + mkdir -p upstream/.git/hooks && + cat >upstream/.git/hooks/update <<-EOF && + #!/bin/sh + + if test "\$1" = "refs/heads/bar" + then + echo >&2 "Pusing to branch bar is prohibited" + exit 1 + fi + EOF + chmod a+x upstream/.git/hooks/update + ) && + ( + cd workbench && + test_commit two && + git branch bar + ) && + test_must_fail git -C workbench \ + push --atomic up master two bar >out 2>&1 && + fmt_status_report <out >actual && + cat >expect <<-EOF && + To ../upstream + ! [remote rejected] master -> master (atomic push failure) + ! [remote rejected] two -> two (atomic push failure) + ! [remote rejected] bar -> bar (hook declined) + EOF + test_cmp expect actual +' + +# References in upstream : master(1) one(1) foo(1) +# References in workbench: master(2) foo(1) two(2) bar(2) +test_expect_failure 'atomic push reports (mirror, but reject by update hook)' ' + ( + cd workbench && + git remote remove up && + git remote add up ../upstream + ) && + test_must_fail git -C workbench \ + push --atomic --mirror up >out 2>&1 && + fmt_status_report <out >actual && + cat >expect <<-EOF && + To ../upstream + ! [remote rejected] master -> master (atomic push failure) + ! [remote rejected] one (atomic push failure) + ! [remote rejected] bar -> bar (hook declined) + ! [remote rejected] two -> two (atomic push failure) + EOF + test_cmp expect actual +' + +# References in upstream : master(2) one(1) foo(1) +# References in workbench: master(1) foo(1) two(2) bar(2) +test_expect_failure 'atomic push reports (reject by non-ff)' ' + rm upstream/.git/hooks/update && + ( + cd workbench && + git push up master && + git reset --hard HEAD^ + ) && + test_must_fail git -C workbench \ + push --atomic up master foo bar >out 2>&1 && + fmt_status_report <out >actual && + cat >expect <<-EOF && + To ../upstream + ! [rejected] master -> master (non-fast-forward) + ! [rejected] bar -> bar (atomic push failed) + EOF + test_cmp expect actual +' + test_done
Signature · 1 line
2.26.0.4.g39bcdcb101.dirty
PATCH v2 2/4 send-pack: mark failure of atomic push properly
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
When pushing with SSH or other smart protocol, references are validated by function check_to_send_update() before they are sent in commands to send_pack() of "receve-pack". For atomic push, if a reference is rejected after the validation, only references pushed by user should be marked as failure, instead of report failure on all remote references.
Commit v2.22.0-1-g3bca1e7f9f (transport-helper: enforce atomic in push_refs_with_push, 2019-07-11) wanted to fix report issue of HTTP protocol, but marked all remote references failure for atomic push.
In order to fix the issue of status report for SSH or other built-in smart protocol, revert part of that commit and add additional status for function atomic_push_failure(). The additional status for it except the "REF_STATUS_EXPECTING_REPORT" status are:
- REF_STATUS_NONE : Not marked as "REF_STATUS_EXPECTING_REPORT" yet.
- REF_STATUS_OK : Assume OK for dryrun or status_report is disabled.
This fix won't resolve the issue of status report in transport-helper for HTTP or other protocols, and breaks test case in t5541. Will fix it in additional commit.
--- send-pack.c | 2 ++ t/t5541-http-push-smart.sh | 2 +- t/t5543-atomic-push.sh | 6 +++--- transport.c | 14 -------------- 4 files changed, 6 insertions(+), 18 deletions(-)
send-pack.c +2 -0
@@ -334,6 +334,8 @@ static int atomic_push_failure(struct send_pack_args *args,continue ;switch (ref->status) { +case REF_STATUS_NONE : +case REF_STATUS_OK :case REF_STATUS_EXPECTING_REPORT : ref->status =REF_STATUS_ATOMIC_PUSH_FAILED ;continue ;
t/t5541-http-push-smart.sh +1 -1
@@ -177,7 +177,7 @@ test_expect_success 'push (chunked)' ' test $HEAD = $(git rev-parse --verify HEAD)) ' -test_expect_success 'push --atomic also prevents branch creation, reports collateral' ' +test_expect_failure 'push --atomic also prevents branch creation, reports collateral' ' # Setup upstream repo - empty for now d=$HTTPD_DOCUMENT_ROOT_PATH/atomic-branches.git && git init --bare "$d" &&
t/t5543-atomic-push.sh +3 -3
@@ -200,7 +200,7 @@ test_expect_success 'atomic push is not advertised if configured' ' # References in upstream : master(1) one(1) foo(1) # References in workbench: master(2) foo(1) two(2) bar(2) # Atomic push : master(2) two(2) bar(2) -test_expect_failure 'atomic push reports (reject by update hook)' ' +test_expect_success 'atomic push reports (reject by update hook)' ' mk_repo_pair && ( cd workbench &&
@@ -241,7 +241,7 @@ test_expect_failure 'atomic push reports (reject by update hook)' ' # References in upstream : master(1) one(1) foo(1) # References in workbench: master(2) foo(1) two(2) bar(2) -test_expect_failure 'atomic push reports (mirror, but reject by update hook)' ' +test_expect_success 'atomic push reports (mirror, but reject by update hook)' ' ( cd workbench && git remote remove up &&
@@ -262,7 +262,7 @@ test_expect_failure 'atomic push reports (mirror, but reject by update hook)' ' # References in upstream : master(2) one(1) foo(1) # References in workbench: master(1) foo(1) two(2) bar(2) -test_expect_failure 'atomic push reports (reject by non-ff)' ' +test_expect_success 'atomic push reports (reject by non-ff)' ' rm upstream/.git/hooks/update && ( cd workbench &&
transport.c +0 -14
@@ -1240,20 +1240,6 @@ int transport_push(struct repository *r, err =push_had_errors (remote_refs); ret = push_ret | err; -if ((flags &TRANSPORT_PUSH_ATOMIC ) && err) { -struct ref *it; -for (it = remote_refs; it; it = it->next) -switch (it->status) { -case REF_STATUS_NONE : -case REF_STATUS_UPTODATE : -case REF_STATUS_OK : - it->status =REF_STATUS_ATOMIC_PUSH_FAILED ; -break ; -default : -break ; - } - } -if (!quiet || err) transport_print_push_status(transport->url, remote_refs, verbose | porcelain, porcelain,
Signature · 1 line
2.26.0.4.g39bcdcb101.dirty
PATCH v2 3/4 transport-helper: mark failure for atomic push
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Commit v2.22.0-1-g3bca1e7f9f (transport-helper: enforce atomic in push_refs_with_push, 2019-07-11) noticed the incomplete report of failure of an atomic push for HTTP protocol. But the implementation has a flaw that mark all remote references as failure.
Only mark necessary references as failure in push_refs_with_push() of transport-helper.
--- t/t5541-http-push-smart.sh | 14 +++++++++++--- transport-helper.c | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-)
t/t5541-http-push-smart.sh +11 -3
@@ -177,7 +177,10 @@ test_expect_success 'push (chunked)' ' test $HEAD = $(git rev-parse --verify HEAD)) ' -test_expect_failure 'push --atomic also prevents branch creation, reports collateral' ' +## References of remote: atomic1(1) master(2) collateral(2) other(2) +## References of local : atomic2(2) master(1) collateral(3) other(2) collateral1(3) atomic(1) +## Atomic push : master(1) collateral(3) atomic(1) +test_expect_success 'push --atomic also prevents branch creation, reports collateral' ' # Setup upstream repo - empty for now d=$HTTPD_DOCUMENT_ROOT_PATH/atomic-branches.git && git init --bare "$d" &&
@@ -189,7 +192,8 @@ test_expect_failure 'push --atomic also prevents branch creation, reports collat test_commit atomic2 && git branch collateral && git branch other && - git push "$up" master collateral other && + git push "$up" atomic1 master collateral other && + git tag -d atomic1 && # collateral is a valid push, but should be failed by atomic push git checkout collateral &&
@@ -224,7 +228,11 @@ test_expect_failure 'push --atomic also prevents branch creation, reports collat # the collateral failure refs should be indicated to the user grep "^ ! .*rejected.* atomic -> atomic .*atomic push failed" output && - grep "^ ! .*rejected.* collateral -> collateral .*atomic push failed" output + grep "^ ! .*rejected.* collateral -> collateral .*atomic push failed" output && + + # never report what we do not push + ! grep "^ ! .*rejected.* atomic1 " output && + ! grep "^ ! .*rejected.* other " output ' test_expect_success 'push --atomic fails on server-side errors' '
transport-helper.c +15 -0
@@ -894,6 +894,21 @@ static int push_refs_with_push(struct transport *transport,case REF_STATUS_REJECT_STALE :case REF_STATUS_REJECT_ALREADY_EXISTS :if (atomic) { +/* Mark other refs as failed */ +for (ref = remote_refs; ref; ref = ref->next) { +if (!ref->peer_ref && !mirror) +continue ; + +switch (ref->status) { +case REF_STATUS_NONE : +case REF_STATUS_OK : +case REF_STATUS_EXPECTING_REPORT : + ref->status =REF_STATUS_ATOMIC_PUSH_FAILED ; +continue ; +default : +break ;/* do nothing */ + } + }string_list_clear (&cas_options,0 );return 0 ; }else
Signature · 1 line
2.26.0.4.g39bcdcb101.dirty
PATCH v2 4/4 transport-helper: new method reject_atomic_push()
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Add new method in transport-helper to reject all references if any reference is failed for atomic push.
This method is reused in "send-pack.c" and "transport-helper.c", one for SSH, git and file protocols, and the other for HTTP protocol.
--- send-pack.c | 29 +++-------------------------- transport-helper.c | 38 +++++++++++++++++++++++--------------- transport.h | 3 +++ 3 files changed, 29 insertions(+), 41 deletions(-)
send-pack.c +3 -26
@@ -322,31 +322,6 @@ static int generate_push_cert(struct strbuf *req_buf,return update_seen; } - -static int atomic_push_failure (struct send_pack_args *args, -struct ref *remote_refs, -struct ref *failing_ref) -{ -struct ref *ref; -/* Mark other refs as failed */ -for (ref = remote_refs; ref; ref = ref->next) { -if (!ref->peer_ref && !args->send_mirror) -continue ; - -switch (ref->status) { -case REF_STATUS_NONE : -case REF_STATUS_OK : -case REF_STATUS_EXPECTING_REPORT : - ref->status =REF_STATUS_ATOMIC_PUSH_FAILED ; -continue ; -default : -break ;/* do nothing */ - } - } -return error ("atomic push failed for ref %s. status: %d\n" , - failing_ref->name, failing_ref->status); -} -#define NONCE_LEN_LIMIT 256static void reject_invalid_nonce (const char *nonce,int len)
@@ -491,7 +466,9 @@ int send_pack(struct send_pack_args *args,if (use_atomic) {strbuf_release (&req_buf);strbuf_release (&cap_buf); -return atomic_push_failure (args, remote_refs, ref); +reject_atomic_push (remote_refs, args->send_mirror); +return error ("atomic push failed for ref %s. status: %d\n" , + ref->name, ref->status); }/* else fallthrough */ default :
transport-helper.c +23 -15
@@ -894,21 +894,7 @@ static int push_refs_with_push(struct transport *transport,case REF_STATUS_REJECT_STALE :case REF_STATUS_REJECT_ALREADY_EXISTS :if (atomic) { -/* Mark other refs as failed */ -for (ref = remote_refs; ref; ref = ref->next) { -if (!ref->peer_ref && !mirror) -continue ; - -switch (ref->status) { -case REF_STATUS_NONE : -case REF_STATUS_OK : -case REF_STATUS_EXPECTING_REPORT : - ref->status =REF_STATUS_ATOMIC_PUSH_FAILED ; -continue ; -default : -break ;/* do nothing */ - } - } +reject_atomic_push (remote_refs, mirror);string_list_clear (&cas_options,0 );return 0 ; }else
@@ -1503,3 +1489,25 @@ int bidirectional_transfer_loop(int input, int output)return tloop_spawnwait_tasks (&state); } + +void reject_atomic_push (struct ref *remote_refs,int mirror_mode) +{ +struct ref *ref; + +/* Mark other refs as failed */ +for (ref = remote_refs; ref; ref = ref->next) { +if (!ref->peer_ref && !mirror_mode) +continue ; + +switch (ref->status) { +case REF_STATUS_NONE : +case REF_STATUS_OK : +case REF_STATUS_EXPECTING_REPORT : + ref->status =REF_STATUS_ATOMIC_PUSH_FAILED ; +continue ; +default : +break ;/* do nothing */ + } + } +return ; +}
transport.h +3 -0
@@ -265,4 +265,7 @@ int transport_refs_pushed(struct ref *ref);void transport_print_push_status (const char *dest,struct ref *refs,int verbose,int porcelain,unsigned int *reject_reasons); +/* common method used by transport-helper.c and send-pack.c */ +void reject_atomic_push (struct ref *refs,int mirror_mode); +#endif
Signature · 1 line
2.26.0.4.g39bcdcb101.dirty
PATCH 2/3 send-pack: mark failure of atomic push properly
In reply to Jiang XinWhen pusing with SSH or other smart protocol, references are validated by function check_to_send_update() beforce they are sent in commands to send_pack() of "receve-pack". For atomic push, if a reference is rejected after the validation, only references pushed by user should be marked as failure, instead of report failure on all remote references.
Commit v2.22.0-1-g3bca1e7f9f (transport-helper: enforce atomic in push_refs_with_push, 2019-07-11) wanted to fix report issue of HTTP protocol, but marked all remote references failure for atomic push.
Revert part of that commit and add additional status for function atomic_push_failure(). The additional status for it except the "REF_STATUS_EXPECTING_REPORT" status are:
- REF_STATUS_NONE : Not marked as "REF_STATUS_EXPECTING_REPORT" yet.
- REF_STATUS_OK : Assume OK for dryrun or status_report is disabled.
This commit break test case in t5541, and will fix in other commit.
--- send-pack.c | 2 ++ t/t5541-http-push-smart.sh | 2 +- t/t5543-atomic-push.sh | 6 +++--- transport.c | 14 -------------- 4 files changed, 6 insertions(+), 18 deletions(-)
send-pack.c +2 -0
@@ -334,6 +334,8 @@ static int atomic_push_failure(struct send_pack_args *args,continue ;switch (ref->status) { +case REF_STATUS_NONE : +case REF_STATUS_OK :case REF_STATUS_EXPECTING_REPORT : ref->status =REF_STATUS_ATOMIC_PUSH_FAILED ;continue ;
t/t5541-http-push-smart.sh +1 -1
@@ -177,7 +177,7 @@ test_expect_success 'push (chunked)' ' test $HEAD = $(git rev-parse --verify HEAD)) ' -test_expect_success 'push --atomic also prevents branch creation, reports collateral' ' +test_expect_failure 'push --atomic also prevents branch creation, reports collateral' ' # Setup upstream repo - empty for now d=$HTTPD_DOCUMENT_ROOT_PATH/atomic-branches.git && git init --bare "$d" &&
t/t5543-atomic-push.sh +3 -3
@@ -201,7 +201,7 @@ test_expect_success 'atomic push is not advertised if configured' ' # References in upstream : master(1) one(1) foo(1) # References in workbench: master(2) foo(1) two(2) bar(2) # Atomic push : master(2) two(2) bar(2) -test_expect_failure 'atomic push reports (reject by update hook)' ' +test_expect_success 'atomic push reports (reject by update hook)' ' mk_repo_pair && ( cd workbench &&
@@ -244,7 +244,7 @@ test_expect_failure 'atomic push reports (reject by update hook)' ' # References in upstream : master(1) one(1) foo(1) # References in workbench: master(2) foo(1) two(2) bar(2) -test_expect_failure 'atomic push reports (mirror, but reject by update hook)' ' +test_expect_success 'atomic push reports (mirror, but reject by update hook)' ' ( cd workbench && git remote remove up &&
@@ -265,7 +265,7 @@ test_expect_failure 'atomic push reports (mirror, but reject by update hook)' ' # References in upstream : master(2) one(1) foo(1) # References in workbench: master(1) foo(1) two(2) bar(2) -test_expect_failure 'atomic push reports (reject by non-ff)' ' +test_expect_success 'atomic push reports (reject by non-ff)' ' rm upstream/.git/hooks/update && ( cd workbench &&
transport.c +0 -14
@@ -1240,20 +1240,6 @@ int transport_push(struct repository *r, err =push_had_errors (remote_refs); ret = push_ret | err; -if ((flags &TRANSPORT_PUSH_ATOMIC ) && err) { -struct ref *it; -for (it = remote_refs; it; it = it->next) -switch (it->status) { -case REF_STATUS_NONE : -case REF_STATUS_UPTODATE : -case REF_STATUS_OK : - it->status =REF_STATUS_ATOMIC_PUSH_FAILED ; -break ; -default : -break ; - } - } -if (!quiet || err) transport_print_push_status(transport->url, remote_refs, verbose | porcelain, porcelain,
Signature · 1 line
2.26.0.3.ga7a9d752d4
Re: [PATCH 2/3] send-pack: mark failure of atomic push properly
Quoted 2 lines · Jiang Xin
When pusing with SSH or other smart protocol, references are validated by function
check_to_send_update()beforce they are sent in commands
beforce -> before
Quoted 8 lines
to
send_pack()of "receve-pack". For atomic push, if a reference is rejected after the validation, only references pushed by user should be marked as failure, instead of report failure on all remote references.Commit v2.22.0-1-g3bca1e7f9f (transport-helper: enforce atomic in
push_refs_with_push, 2019-07-11) wanted to fix report issue of HTTP protocol, but marked all remote references failure for atomic push.
Quoted 8 lines
Revert part of that commit and add additional status for function
atomic_push_failure(). The additional status for it except the "REF_STATUS_EXPECTING_REPORT" status are:-
REF_STATUS_NONE: Not marked as "REF_STATUS_EXPECTING_REPORT" yet.
-REF_STATUS_OK: Assume OK for dryrun orstatus_reportis disabled.This commit break test case in t5541, and will fix in other commit.
Presumably the fix in 3/3 is too big to be included in here? Let's see how it goes, as such a strategy sometimes helps understand the change and sometimes does not.
send-pack.c static int atomic_push_failure(struct send_pack_args *args, · line 341
Quoted 13 lines
send-pack.c +2
-0@@ -334,6 +334,8 @@ static int atomic_push_failure(struct send_pack_args *args,continue ;switch (ref->status) { +case REF_STATUS_NONE : +case REF_STATUS_OK :case REF_STATUS_EXPECTING_REPORT : ref->status =REF_STATUS_ATOMIC_PUSH_FAILED ;continue ;
This is fed the whole *remote_refs list, iterates over them but we only come here if the ref is actually pushed (i.e. peer_ref is set, or we are doing a mirror push), so it does sound like the right place to do this.
PATCH 3/3 transport-helper: enforce atomic in push_refs_with_push
In reply to Jiang XinCommit v2.22.0-1-g3bca1e7f9f (transport-helper: enforce atomic in push_refs_with_push, 2019-07-11) noticed the incomplete report of failure of an atomic push for HTTP protocol. But the implementation has a flaw that mark all remote references as failure.
Only mark necessary references as failure in push_refs_with_push() of transport-helper.
--- t/t5541-http-push-smart.sh | 14 +++++++++++--- transport-helper.c | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-)
t/t5541-http-push-smart.sh +11 -3
@@ -177,7 +177,10 @@ test_expect_success 'push (chunked)' ' test $HEAD = $(git rev-parse --verify HEAD)) ' -test_expect_failure 'push --atomic also prevents branch creation, reports collateral' ' +## References of remote: atomic1(1) master(2) collateral(2) other(2) +## References of local : atomic2(2) master(1) collateral(3) other(2) collateral1(3) atomic(1) +## Atomic push : master(1) collateral(3) atomic(1) +test_expect_success 'push --atomic also prevents branch creation, reports collateral' ' # Setup upstream repo - empty for now d=$HTTPD_DOCUMENT_ROOT_PATH/atomic-branches.git && git init --bare "$d" &&
@@ -189,7 +192,8 @@ test_expect_failure 'push --atomic also prevents branch creation, reports collat test_commit atomic2 && git branch collateral && git branch other && - git push "$up" master collateral other && + git push "$up" atomic1 master collateral other && + git tag -d atomic1 && # collateral is a valid push, but should be failed by atomic push git checkout collateral &&
@@ -224,7 +228,11 @@ test_expect_failure 'push --atomic also prevents branch creation, reports collat # the collateral failure refs should be indicated to the user grep "^ ! .*rejected.* atomic -> atomic .*atomic push failed" output && - grep "^ ! .*rejected.* collateral -> collateral .*atomic push failed" output + grep "^ ! .*rejected.* collateral -> collateral .*atomic push failed" output && + + # never report what we do not push + ! grep "^ ! .*rejected.* atomic1 " output && + ! grep "^ ! .*rejected.* other " output ' test_expect_success 'push --atomic fails on server-side errors' '
transport-helper.c +15 -0
@@ -894,6 +894,21 @@ static int push_refs_with_push(struct transport *transport,case REF_STATUS_REJECT_STALE :case REF_STATUS_REJECT_ALREADY_EXISTS :if (atomic) { +/* Mark other refs as failed */ +for (ref = remote_refs; ref; ref = ref->next) { +if (!ref->peer_ref && !mirror) +continue ; + +switch (ref->status) { +case REF_STATUS_NONE : +case REF_STATUS_OK : +case REF_STATUS_EXPECTING_REPORT : + ref->status =REF_STATUS_ATOMIC_PUSH_FAILED ; +continue ; +default : +break ;/* do nothing */ + } + }string_list_clear (&cas_options,0 );return 0 ; }else
Signature · 1 line
2.26.0.3.ga7a9d752d4
Re: [PATCH 3/3] transport-helper: enforce atomic in push_refs_with_push
Quoted 13 lines · Jiang Xin
transport-helper.c +5
-0@@ -894,6 +894,21 @@ static int push_refs_with_push(struct transport *transport,case REF_STATUS_REJECT_STALE :case REF_STATUS_REJECT_ALREADY_EXISTS :if (atomic) { +/* Mark other refs as failed */ +for (ref = remote_refs; ref; ref = ref->next) { +if (!ref->peer_ref && !mirror) +continue ; +
transport-helper.c static int push_refs_with_push(struct transport *transport, · line 911
Quoted 10 lines
transport-helper.c +10
-0+switch (ref->status) { +case REF_STATUS_NONE : +case REF_STATUS_OK : +case REF_STATUS_EXPECTING_REPORT : + ref->status =REF_STATUS_ATOMIC_PUSH_FAILED ; +continue ; +default : +break ;/* do nothing */ + } + }
OK, so this is more in line with the check done in send_pack() that fails the push before we even send any pack data. I wonder if it is worth considering to move the logic of this loop into a helper function so that this and the other one in 2/3 can call it and stay in sync, something along the lines of
void reject_push_to_other_refs(struct ref *ref, int mirror_mode)
{
for (; ref; ref = ref->next) {
... one iteration of the above loop ...
}
}Then the above part would become
case REF_STATUS_REJECT_FOO: if (atomic) reject_push_to_other_refs(remote_refs, mirror);
and the part modified by 2/3 would also become
static int atomic_push_failure(...)
{
reject_push_to_other_refs(remote_refs, args->send_mirror);
return error("atomic push failed ...");
}I am not sure if it is better to keep 2/3 and 3/3 separate or make them into a single step, but perhaps it is because I am not getting the true reason why you made them separate in the first place.
Thanks.
PATCH v4 3/5 refs.c: refactor to reuse ref_is_hidden()
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Add new function ref_is_matched() to reuse ref_is_hidden(). Will use this function for receive-pack to check commands with specific prefixes.
Test case t5512 covered this change.
--- refs.c | 11 ++++++++--- refs.h | 1 + 2 files changed, 9 insertions(+), 3 deletions(-)
refs.c +8 -3
@@ -1389,13 +1389,18 @@ int parse_hide_refs_config(const char *var, const char *value, const char *secti }int ref_is_hidden (const char *refname,const char *refname_full) +{ +return ref_is_matched (hide_refs, refname, refname_full); +} + +int ref_is_matched (struct string_list *match_refs,const char *refname,const char *refname_full) {int i; -if (!hide_refs) +if (!match_refs)return 0 ; -for (i = hide_refs->nr -1 ; i >=0 ; i--) { -const char *match = hide_refs->items[i].string; +for (i = match_refs->nr -1 ; i >=0 ; i--) { +const char *match = match_refs->items[i].string;const char *subject;int neg =0 ;const char *p;
refs.h +1 -0
@@ -739,6 +739,7 @@ int parse_hide_refs_config(const char *var, const char *value, const char *);* parameter always points to the full ref name. */ int ref_is_hidden (const char *,const char *); +int ref_is_matched (struct string_list *,const char *,const char *);enum ref_type {REF_TYPE_PER_WORKTREE ,/* refs inside refs/ but not shared */
Signature · 1 line
2.26.0.rc1.33.g4914ba4bcf
PATCH v4 4/5 receive-pack: new config receive.procReceiveRefs
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Add a new multi-valued config variable "receive.procReceiveRefs" for receive-pack command, like the follows:
git config --system --add receive.procReceiveRefs refs/for/
git config --system --add receive.procReceiveRefs refs/drafts/If the specific prefix strings match the reference names of the commands which are sent by git client to receive-pack, these commands will be executed by an external hook (named "proc-receive"), instead of the internal execute_commands function.
For example, if it is set to "refs/for/", pushing to a reference such as "refs/for/master" will not create or update reference "refs/for/master", but may create or update a pull request directly by running the external hook.
--- Documentation/config/receive.txt | 14 +++ builtin/receive-pack.c | 43 +++++-- t/t5411-proc-receive-hook.sh | 204 +++++++++++++++++++++++++++++++ 3 files changed, 254 insertions(+), 7 deletions(-)
Documentation/config/receive.txt +14 -0
@@ -114,6 +114,20 @@ receive.hideRefs:: An attempt to update or delete a hidden ref by `git push` is rejected. +receive.procReceiveRefs:: + This is a multi-valued variable that defines reference prefixes + to match the commands in `receive-pack`. Commands matching the + prefixes will be executed by an external hooks "proc-receive", + instead of the internal `execute_commands` function. If this + variable is not defined, the "proc-receive" hook will never be + used, and all commands will be executed by the internal + `execute_commands` function. + + For example, if this variable is set to "refs/for/", pushing to + reference such as "refs/for/master" will not create or update a + reference named "refs/for/master", but may create or update a + pull request directly by running an external hook. + receive.updateServerInfo:: If set to true, git-receive-pack will run git-update-server-info after receiving data from git-push and updating refs.
builtin/receive-pack.c +36 -7
74 lines not on this page · raw message
t/t5411-proc-receive-hook.sh +204 -0
214 lines not on this page · raw message
Signature · 1 line
2.26.0.rc1.33.g4914ba4bcf
PATCH v4 5/5 receive-pack: refactor report for proc-receive
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
The "proc-receive" may update one or more references, and will send its result one by one in pkt-line format. Each line of the result has four fields and one optional message field, as "<old-oid> <new-oid> <ref> <status> [<message>]". See the following example:
# OK, run this command successfully.
PKT-LINE(old-oid new-oid ref ok)
# NO, I reject it.
PKT-LINE(old-oid new-oid ref ng reason)
# OK, but use an alternate reference.
PKT-LINE(old-oid new-oid ref ok ref:alt-ref)
# It will fallthrough to receive-pack to execute. PKT-LINE(old-oid new-oid ref ft)
The first three fields have the same foramt as a command.
The forth field has a two-letter status code. Available status code:
* ok: The command runs successfully. If the optional message has a prefix "ref:", the hook has created/updated an alternate reference instead.
* ng: Fail to run the command. Error message is in the optional message field.
* ft: Will fallthrough to receive-pack to execute.
--- builtin/receive-pack.c | 21 +++++++-- t/t5411-proc-receive-hook.sh | 90 +++++++++++++++++++++++++++++++++--- transport.c | 27 +++++++---- 3 files changed, 119 insertions(+), 19 deletions(-)
builtin/receive-pack.c +16 -5
@@ -326,6 +326,7 @@ static void write_head_info(void)struct command {struct command *next;const char *error_string; +const char *extra_string;unsigned int skip_update:1 , did_not_exist:1 , run_proc_receive:1 ;
@@ -883,7 +884,12 @@ static int read_proc_receive_result(struct packet_reader *reader,else cmd->error_string ="failed" ; code =1 ; - }else if (strcmp ("ok" , status)) { + }else if (!strcmp ("ok" , status)) { + cmd->extra_string =xstrdup_or_null (msg); + }else if (!strcmp ("ft" , status)) { +/* Reset "run_proc_receive" field, and continue to run in "receive-pack" */ + cmd->run_proc_receive =0 ; + }else {die ("protocol error: proc-receive has bad status '%s' for '%s'" , status, reader->line); }
@@ -2190,12 +2196,17 @@ static void report(struct command *commands, const char *unpack_status)packet_buf_write (&buf,"unpack %s\n" , unpack_status ? unpack_status :"ok" );for (cmd = commands; cmd; cmd = cmd->next) { -if (!cmd->error_string) -packet_buf_write (&buf,"ok %s\n" , - cmd->ref_name); -else +if (!cmd->error_string) { +if (!cmd->extra_string) +packet_buf_write (&buf,"ok %s\n" , + cmd->ref_name); +else +packet_buf_write (&buf,"ok %s%c%s\n" , + cmd->ref_name,' ' , cmd->extra_string); + }else {packet_buf_write (&buf,"ng %s %s\n" , cmd->ref_name, cmd->error_string); + } }packet_buf_flush (&buf);
t/t5411-proc-receive-hook.sh +84 -6
114 lines not on this page · raw message
transport.c +19 -8
@@ -463,11 +463,16 @@ static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain, int summary_width) { + char *to_name = to->name; + +if (to->remote_status && !strncmp ("ref:" , to->remote_status,4 )) + to_name = to->remote_status +4 ; +if (porcelain) {if (from) - fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name); +fprintf (stdout,"%c\t%s:%s\t" , flag, from->name, to_name);else -fprintf (stdout,"%c\t:%s\t" , flag, to->name); +fprintf (stdout,"%c\t:%s\t" , flag, to_name);if (msg)fprintf (stdout,"%s (%s)\n" , summary, msg);else
@@ -481,9 +486,9 @@ static void print_ref_status(char flag, const char *summary,fprintf (stderr," %s%c %-*s%s " , red, flag, summary_width, summary, reset);if (from) -fprintf (stderr,"%s -> %s" ,prettify_refname (from->name),prettify_refname (to->name)); +fprintf (stderr,"%s -> %s" ,prettify_refname (from->name),prettify_refname (to_name));else -fputs (prettify_refname (to->name), stderr); +fputs (prettify_refname (to_name), stderr);if (msg) {fputs (" (" , stderr);fputs (msg, stderr);
@@ -498,13 +503,19 @@ static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widtif (ref->deletion)print_ref_status ('-' ,"[deleted]" , ref,NULL ,NULL , porcelain, summary_width); -else if (is_null_oid (&ref->old_oid)) +else if (is_null_oid (&ref->old_oid)) { +char *refname; + +if (ref->remote_status && !strncmp (ref->remote_status,"ref:" ,4 )) + refname = ref->remote_status +4 ; +else + refname = ref->name;print_ref_status ('*' , - (starts_with (ref->name,"refs/tags/" ) ?"[new tag]" : - (starts_with (ref->name,"refs/heads/" ) ?"[new branch]" : + (starts_with (refname,"refs/tags/" ) ?"[new tag]" : + (starts_with (refname,"refs/heads/" ) ?"[new branch]" :"[new reference]" )), ref, ref->peer_ref,NULL , porcelain, summary_width); -else { + }else {struct strbuf quickref =STRBUF_INIT ;char type;const char *msg;
Signature · 1 line
2.26.0.rc1.33.g4914ba4bcf
PATCH v3 1/4 receive-pack: add new proc-receive hook
In reply to Jiang XinGit calls an internal execute_commands function to handle commands sent from client to git-receive-pack. Regardless of what references the user pushes, git creates or updates the corresponding references if the user has write-permission. A contributor who has no write-permission, cannot push to the repository directly. So, the contributor has to write commits to an alternate location, and sends pull request by emails or by other ways. We call this workflow as a distributed workflow.
It would be more convenient to work in a centralized workflow like what Gerrit provided for some cases. For example, a read-only user who cannot push to a branch directly can run the following git push command to push commits to a pseudo reference (has a prefix "refs/for/", not "refs/heads/") to create a code review.
git push origin \
HEAD:refs/for/<branch-name>/<session>The <branch-name> in the above example can be as simple as "master", or a more complicated branch name like "foo/bar". The <session> in the above example command can be the local branch name of the client side, such as "my/topic".
We cannot implement a centralized workflow elegantly by using "pre-receive" + "post-receive", because Git will call the internal function "execute_commands" to create references (even the special pseudo reference) between these two hooks. Even though we can delete the temporarily created pseudo reference via the "post-receive" hook, having a temporary reference is not safe for concurrent pushes.
So, add a filter and a new handler to support this kind of workflow. The filter will check the prefix of the reference name, and if the command has a special reference name, the filter will turn a specific field (have_special_ref) on for the command. Commands with this filed turned on will be executed by a new handler (an hook named "proc-receive") instead of the internal execute_commands function. We can use this "proc-receive" command to create pull requests or send emails for code review.
This "proc-receive" hook reads commands, push-options (optional), and send result using a protocol in pkt-line format. In the following example, The letter "S" stands for "receive-pack" and letter "H" stands for the hook.
S: PKT-LINE(version=1\0push-options ...)
S: flush-pkt
H: PKT-LINE(version=1\0push-options ...)
H: flush-pkt
S: PKT-LINE(old-oid new-oid ref)
S: ... ...
S: flush-pkt
# Optional, only if push-options is negotiated.
S: PKT-LINE(push-option)
S: ... ...
S: flush-pkt
# OK, run this command successfully.
H: PKT-LINE(old-oid new-oid ref ok)
# NO, I reject it.
H: PKT-LINE(old-oid new-oid ref ng reason)
# OK, but use an alternate reference. (in latter commit)
H: PKT-LINE(old-oid new-oid ref ok ref:alt-ref)
# It will fallthrough to receive-pack to execute. (in latter commit)
H: PKT-LINE(old-oid new-oid ref ft)
H: ... ...
H: flush-pkt
After receiving a command, the hook can create/update another alternate reference. For example, a command for a reference "refs/for/master" may create a special reference, such as "refs/pull/123/head". The alternate reference can be returned from the result in an extensible format like "<old-oid> <new-oid> <reference> <status> [<message>]".
The result will be stored in a command list, and "receive-pack" will use the result to replace the commands that have specific have_special_ref field turned on.
--- builtin/receive-pack.c | 244 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 239 insertions(+), 5 deletions(-)
builtin/receive-pack.c +239 -5
@@ -312,7 +312,8 @@ struct command {struct command *next;const char *error_string; unsigned int skip_update:1, - did_not_exist:1; + did_not_exist:1, + have_special_ref:1;int index;struct object_id old_oid;struct object_id new_oid;
@@ -817,6 +818,209 @@ static int run_update_hook(struct command *cmd)return finish_command (&proc); } +static int read_proc_receive_result (struct packet_reader *reader, +struct command **commands) +{ +struct command **tail = commands; +int code =0 ; + +for (;;) { +struct object_id old_oid, new_oid; +struct command *cmd; +const char *refname; +const char *p; +char *status; +char *msg =NULL ; + +if (packet_reader_read (reader) !=PACKET_READ_NORMAL ) { +break ; + } + +if (parse_oid_hex (reader->line, &old_oid, &p) || + *p++ !=' ' || +parse_oid_hex (p, &new_oid, &p) || + *p++ !=' ' ) +die ("protocol error: proc-receive expected 'old new ref status [msg]', got '%s'" , + reader->line); + + refname = p; + status =strchr (p,' ' ); +if (!status) +die ("protocol error: proc-receive expected 'old new ref status [msg]', got '%s'" , + reader->line); + *status++ ='\0' ; + +FLEX_ALLOC_MEM (cmd, ref_name, refname,strlen (refname)); +oidcpy (&cmd->old_oid, &old_oid); +oidcpy (&cmd->new_oid, &new_oid); + cmd->have_special_ref =1 ; + +if (strlen (status) >2 && *(status +2 ) ==' ' ) { + msg = status +2 ; + *msg++ ='\0' ; + } +if (strlen (status) !=2 ) +die ("protocol error: proc-receive has bad status '%s' for '%s'" , + status, reader->line); +if (!strcmp (status,"ng" )) { +if (msg) + cmd->error_string =xstrdup (msg); +else + cmd->error_string ="failed" ; + code =1 ; + }else if (strcmp ("ok" , status)) { +rp_warning ("unknown proc-receive status '%s' for '%s'" , + status, reader->line); + cmd->error_string ="bad status" ; + code =1 ; + } + + *tail = cmd; + tail = &cmd->next; + } +return code; +} + +static int run_proc_receive_hook (struct command **commands, +const struct string_list *push_options) +{ +struct child_process proc =CHILD_PROCESS_INIT ; +struct async muxer; +struct command *result_commands =NULL ; +struct command *cmd; +const char *argv[2 ]; +struct packet_reader reader; +int pr_use_push_options =0 ; +int version =0 ; +int code; + + argv[0 ] =find_hook ("proc-receive" ); +if (!argv[0 ]) { +rp_error ("cannot to find hook 'proc-receive'" ); +return 1 ; + } + argv[1 ] =NULL ; + + proc.argv = argv; + proc.in =-1 ; + proc.out =-1 ; + proc.trace2_hook_name ="proc-receive" ; + +if (use_sideband) { +memset (&muxer,0 ,sizeof (muxer)); + muxer.proc = copy_to_sideband; + muxer.in =-1 ; + code =start_async (&muxer); +if (code) +return code; + proc.err = muxer.in; + }else { + proc.err =0 ; + } + + code =start_command (&proc); +if (code) { +if (use_sideband) +finish_async (&muxer); +return code; + } + +sigchain_push (SIGPIPE ,SIG_IGN ); + +/* Version negotiaton */ +packet_reader_init (&reader, proc.out,NULL ,0 , +PACKET_READ_CHOMP_NEWLINE | +PACKET_READ_DIE_ON_ERR_PACKET ); +packet_write_fmt (proc.in,"version=1%c%s\n" , +'\0' , + use_push_options ?"push-options" :"" ); +for (;;) { +int linelen; + +if (packet_reader_read (&reader) !=PACKET_READ_NORMAL ) +break ; + +if (reader.pktlen >8 &&starts_with (reader.line,"version=" )) { + version =atoi (reader.line+8 ); + linelen =strlen (reader.line); +if (linelen < reader.pktlen) { +const char *feature_list = reader.line + linelen +1 ; +if (parse_feature_request (feature_list,"push-options" )) + pr_use_push_options =1 ; + } + } + } + +if (version !=1 ) +die ("protocol error: unknown proc-receive version '%d'" , version); + +/* Send commands */ +for (cmd = *commands; cmd; cmd = cmd->next) { +char *old_hex, *new_hex; + +if (!cmd->have_special_ref || cmd->skip_update || + cmd->did_not_exist || cmd->error_string) +continue ; + + old_hex =oid_to_hex (&cmd->old_oid); + new_hex =oid_to_hex (&cmd->new_oid); + +packet_write_fmt (proc.in,"%s %s %s" , + old_hex, new_hex, cmd->ref_name); + } +packet_flush (proc.in); + +/* Send push options */ +if (pr_use_push_options) { +struct string_list_item *item; + +for_each_string_list_item (item, push_options) +packet_write_fmt (proc.in,"%s" , item->string); + +packet_flush (proc.in); + } + +/* Read result from proc-receive */ + code =read_proc_receive_result (&reader, &result_commands); +close (proc.in); +close (proc.out); +if (finish_command (&proc)) +die ("proc-receive did not exit properly" ); + +/* After receiving the result from the "proc-receive" hook, +* "receive-pack" will use the result to replace commands that +* have specific `have_special_ref` field. +*/ +for (cmd = *commands; cmd; cmd = cmd->next) +if (!cmd->have_special_ref) +break ; + +if (!cmd || cmd->have_special_ref) { + cmd = result_commands; + }else { +struct command *tail = cmd; +for (;;) { +if (!tail->next) +break ; +else if (tail->next->have_special_ref) + tail->next = tail->next->next; +else + tail = tail->next; + } + tail->next = result_commands; + } + *commands = cmd; + +// TODO: sort commands or not? + +if (use_sideband) +finish_async (&muxer); + +sigchain_pop (SIGPIPE ); + +return code; +} +static char *refuse_unconfigured_deny_msg =N_ ("By default, updating the current branch in a non-bare repository\n" "is denied, because it will make the index and work tree inconsistent\n"
@@ -1392,7 +1596,7 @@ static void execute_commands_non_atomic(struct command *commands,struct strbuf err =STRBUF_INIT ;for (cmd = commands; cmd; cmd = cmd->next) { -if (!should_process_cmd (cmd)) +if (!should_process_cmd (cmd) || cmd->have_special_ref)continue ; transaction =ref_transaction_begin (&err);
@@ -1432,7 +1636,7 @@ static void execute_commands_atomic(struct command *commands, }for (cmd = commands; cmd; cmd = cmd->next) { -if (!should_process_cmd (cmd)) +if (!should_process_cmd (cmd) || cmd->have_special_ref)continue ; cmd->error_string =update (cmd, si);
@@ -1458,16 +1662,19 @@ static void execute_commands_atomic(struct command *commands,strbuf_release (&err); } -static void execute_commands (struct command *commands, +static void execute_commands (struct command **orig_commands,const char *unpacker_error,struct shallow_info *si,const struct string_list *push_options) { +struct command *commands = *orig_commands;struct check_connected_options opt =CHECK_CONNECTED_INIT ;struct command *cmd;struct iterate_data data;struct async muxer;int err_fd =0 ; +int have_special_ref =0 ; +int have_normal_ref =0 ;if (unpacker_error) {for (cmd = commands; cmd; cmd = cmd->next)
@@ -1497,6 +1704,22 @@ static void execute_commands(struct command *commands,reject_updates_to_hidden (commands); +/* Try to find commands that have special prefix in their reference names, +* and mark them to run an external "proc-receive" hook later. +*/ +for (cmd = commands; cmd; cmd = cmd->next) { +if (!should_process_cmd (cmd)) +continue ; + +/* TODO: replace the fixed prefix by looking up git config variables. */ +if (!strncmp (cmd->ref_name,"refs/for/" ,9 )) { + cmd->have_special_ref =1 ; + have_special_ref =1 ; + }else { + have_normal_ref =1 ; + } + } +if (run_receive_hook (commands,"pre-receive" ,0 , push_options)) {for (cmd = commands; cmd; cmd = cmd->next) {if (!cmd->error_string)
@@ -1523,6 +1746,17 @@ static void execute_commands(struct command *commands,free (head_name_to_free); head_name = head_name_to_free =resolve_refdup ("HEAD" ,0 ,NULL ,NULL ); +if (have_special_ref) { +if (run_proc_receive_hook (orig_commands, push_options)) { +for (cmd = commands; cmd; cmd = cmd->next) { +if (!cmd->error_string && (cmd->have_special_ref || use_atomic)) + cmd->error_string ="fail to run proc-receive hook" ; + } + }else { + commands = *orig_commands; + } + } +if (use_atomic)execute_commands_atomic (commands, si);else
@@ -2019,7 +2253,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)update_shallow_info (commands, &si, &ref); } use_keepalive =KEEPALIVE_ALWAYS ; -execute_commands (commands, unpack_status, &si, +execute_commands (&commands, unpack_status, &si, &push_options);if (pack_lockfile)unlink_or_warn (pack_lockfile);
Signature · 1 line
2.26.0.rc1.5.gca1e965b06
PATCH v3 2/4 receive-pack: refactor report for proc-receive
In reply to Jiang XinThe "proc-receive" may update one or more references, and will send its result one by one in pkt-line format. Each line of the result has four fields and one optional message field, as "<old-oid> <new-oid> <ref> <status> [<message>]". See the following example:
# OK, run this command successfully.
PKT-LINE(old-oid new-oid ref ok)
# NO, I reject it.
PKT-LINE(old-oid new-oid ref ng reason)
# OK, but use an alternate reference.
PKT-LINE(old-oid new-oid ref ok ref:alt-ref)
# It will fallthrough to receive-pack to execute. PKT-LINE(old-oid new-oid ref ft)
The first three fields have the same foramt as a command.
The forth field has a two-letter status code. Available status code:
* ok: The command runs successfully. If the optional message has a prefix "ref:", the hook has created/updated an alternate reference instead.
* ng: Fail to run the command. Error message is in the optional message field.
* ft: Will fallthrough to receive-pack to execute.
--- builtin/receive-pack.c | 21 ++++++++++++++++----- transport.c | 33 ++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 14 deletions(-)
builtin/receive-pack.c +16 -5
@@ -311,6 +311,7 @@ static void write_head_info(void)struct command {struct command *next;const char *error_string; +const char *extra_string;unsigned int skip_update:1 , did_not_exist:1 , have_special_ref:1 ;
@@ -868,7 +869,12 @@ static int read_proc_receive_result(struct packet_reader *reader,else cmd->error_string ="failed" ; code =1 ; - }else if (strcmp ("ok" , status)) { + }else if (!strcmp ("ok" , status)) { + cmd->extra_string =xstrdup_or_null (msg); + }else if (!strcmp ("ft" , status)) { +/* Unset "have_special_ref" field, will execute by "receive-pack" */ + cmd->have_special_ref =0 ; + }else {rp_warning ("unknown proc-receive status '%s' for '%s'" , status, reader->line); cmd->error_string ="bad status" ;
@@ -2133,12 +2139,17 @@ static void report(struct command *commands, const char *unpack_status)packet_buf_write (&buf,"unpack %s\n" , unpack_status ? unpack_status :"ok" );for (cmd = commands; cmd; cmd = cmd->next) { -if (!cmd->error_string) -packet_buf_write (&buf,"ok %s\n" , - cmd->ref_name); -else +if (!cmd->error_string) { +if (!cmd->extra_string) +packet_buf_write (&buf,"ok %s\n" , + cmd->ref_name); +else +packet_buf_write (&buf,"ok %s%c%s\n" , + cmd->ref_name,' ' , cmd->extra_string); + }else {packet_buf_write (&buf,"ng %s %s\n" , cmd->ref_name, cmd->error_string); + } }packet_buf_flush (&buf);
transport.c +24 -9
@@ -463,11 +463,17 @@ static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain, int summary_width) { + char *to_name = to->name; + +if (to->remote_status) { +if (!strncmp ("ref:" , to->remote_status,4 )) + to_name = to->remote_status +4 ; + }if (porcelain) {if (from) - fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name); +fprintf (stdout,"%c\t%s:%s\t" , flag, from->name, to_name);else -fprintf (stdout,"%c\t:%s\t" , flag, to->name); +fprintf (stdout,"%c\t:%s\t" , flag, to_name);if (msg)fprintf (stdout,"%s (%s)\n" , summary, msg);else
@@ -481,9 +487,9 @@ static void print_ref_status(char flag, const char *summary,fprintf (stderr," %s%c %-*s%s " , red, flag, summary_width, summary, reset);if (from) -fprintf (stderr,"%s -> %s" ,prettify_refname (from->name),prettify_refname (to->name)); +fprintf (stderr,"%s -> %s" ,prettify_refname (from->name),prettify_refname (to_name));else -fputs (prettify_refname (to->name), stderr); +fputs (prettify_refname (to_name), stderr);if (msg) {fputs (" (" , stderr);fputs (msg, stderr);
@@ -498,12 +504,21 @@ static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widtif (ref->deletion)print_ref_status ('-' ,"[deleted]" , ref,NULL ,NULL , porcelain, summary_width); -else if (is_null_oid (&ref->old_oid)) +else if (is_null_oid (&ref->old_oid)) { +char *refname, *summary; +if (ref->remote_status && !strncmp (ref->remote_status,"ref:" ,4 )) + refname = ref->remote_status +4 ; +else + refname = ref->name; +if (starts_with (refname,"refs/tags/" )) + summary ="[new tag]" ; +else if (starts_with (refname,"refs/heads/" )) + summary ="[new branch]" ; +else + summary ="[new reference]" ;print_ref_status ('*' , - (starts_with (ref->name,"refs/tags/" ) ?"[new tag]" : -"[new branch]" ), - ref, ref->peer_ref,NULL , porcelain, summary_width); -else { + summary, ref, ref->peer_ref,NULL , porcelain, summary_width); + }else {struct strbuf quickref =STRBUF_INIT ;char type;const char *msg;
Signature · 1 line
2.26.0.rc1.5.gca1e965b06
PATCH v3 3/4 refs.c: refactor to reuse ref_is_hidden()
In reply to Jiang XinAdd new function ref_is_matched() to reuse ref_is_hidden(). Will use this function for receive-pack to check commands with specific prefixes.
Test case t5512 covered this change.
--- refs.c | 11 ++++++++--- refs.h | 1 + 2 files changed, 9 insertions(+), 3 deletions(-)
refs.c +8 -3
@@ -1389,13 +1389,18 @@ int parse_hide_refs_config(const char *var, const char *value, const char *secti }int ref_is_hidden (const char *refname,const char *refname_full) +{ +return ref_is_matched (hide_refs, refname, refname_full); +} + +int ref_is_matched (struct string_list *match_refs,const char *refname,const char *refname_full) {int i; -if (!hide_refs) +if (!match_refs)return 0 ; -for (i = hide_refs->nr -1 ; i >=0 ; i--) { -const char *match = hide_refs->items[i].string; +for (i = match_refs->nr -1 ; i >=0 ; i--) { +const char *match = match_refs->items[i].string;const char *subject;int neg =0 ;const char *p;
refs.h +1 -0
@@ -739,6 +739,7 @@ int parse_hide_refs_config(const char *var, const char *value, const char *);* parameter always points to the full ref name. */ int ref_is_hidden (const char *,const char *); +int ref_is_matched (struct string_list *,const char *,const char *);enum ref_type {REF_TYPE_PER_WORKTREE ,/* refs inside refs/ but not shared */
Signature · 1 line
2.26.0.rc1.5.gca1e965b06
PATCH v3 4/4 receive-pack: new config receive.procReceiveRefs
In reply to Jiang XinAdd a new multi-valued config variable "receive.procReceiveRefs" for receive-pack command, like the follows:
git config --system --add receive.procReceiveRefs refs/for/
git config --system --add receive.procReceiveRefs refs/drafts/If the specific prefix strings match the reference names of the commands which are sent by git client to receive-pack, these commands will be executed by an external hook (named "proc-receive"), instead of the internal execute_commands function.
For example, if it is set to "refs/for/", pushing to a reference such as "refs/for/master" will not create or update reference "refs/for/master", but may create or update a pull request directly by running the external hook.
--- Documentation/config/receive.txt | 14 +++++++++ builtin/receive-pack.c | 49 ++++++++++++++++++++++++++------ 2 files changed, 54 insertions(+), 9 deletions(-)
Documentation/config/receive.txt +14 -0
@@ -114,6 +114,20 @@ receive.hideRefs:: An attempt to update or delete a hidden ref by `git push` is rejected. +receive.procReceiveRefs:: + This is a multi-valued variable that defines reference prefixes + to match the commands in `receive-pack`. Commands matching the + prefixes will be executed by an external hooks "proc-receive", + instead of the internal `execute_commands` function. If this + variable is not defined, the "proc-receive" hook will never be + used, and all commands will be executed by the internal + `execute_commands` function. + + For example, if this variable is set to "refs/for/", pushing to + reference such as "refs/for/master" will not create or update a + reference named "refs/for/master", but may create or update a + pull request directly by running an external hook. + receive.updateServerInfo:: If set to true, git-receive-pack will run git-update-server-info after receiving data from git-push and updating refs.
builtin/receive-pack.c +40 -9
80 lines not on this page · raw message
Signature · 1 line
2.26.0.rc1.5.gca1e965b06
PATCH v2 2/5 refs.c: refactor to reuse ref_is_hidden()
In reply to Jiang XinAdd new function ref_is_matched() to reuse ref_is_hidden(). Will use this function for receive-pack to check commands with specific prefixes.
Test case t5512 covered this change.
--- refs.c | 11 ++++++++--- refs.h | 1 + 2 files changed, 9 insertions(+), 3 deletions(-)
refs.c +8 -3
@@ -1389,13 +1389,18 @@ int parse_hide_refs_config(const char *var, const char *value, const char *secti }int ref_is_hidden (const char *refname,const char *refname_full) +{ +return ref_is_matched (hide_refs, refname, refname_full); +} + +int ref_is_matched (struct string_list *match_refs,const char *refname,const char *refname_full) {int i; -if (!hide_refs) +if (!match_refs)return 0 ; -for (i = hide_refs->nr -1 ; i >=0 ; i--) { -const char *match = hide_refs->items[i].string; +for (i = match_refs->nr -1 ; i >=0 ; i--) { +const char *match = match_refs->items[i].string;const char *subject;int neg =0 ;const char *p;
refs.h +1 -0
@@ -739,6 +739,7 @@ int parse_hide_refs_config(const char *var, const char *value, const char *);* parameter always points to the full ref name. */ int ref_is_hidden (const char *,const char *); +int ref_is_matched (struct string_list *,const char *,const char *);enum ref_type {REF_TYPE_PER_WORKTREE ,/* refs inside refs/ but not shared */
Signature · 1 line
2.26.0.rc0.5.gb02b988a14.dirty
PATCH v2 3/5 receive-pack: new config receive.procReceiveRefs
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Add a new multi-valued config variable "receive.procReceiveRefs" for receive-pack command, like the follows:
git config --system --add receive.procReceiveRefs refs/for/
git config --system --add receive.procReceiveRefs refs/drafts/If the specific prefix strings match the reference names of the commands which are sent by git client to receive-pack, these commands will be executed by an external hook (named "proc-receive"), instead of the internal execute_commands function.
For example, if it is set to "refs/for/", pushing to a reference such as "refs/for/master" will not create or update reference "refs/for/master", but may create or update a pull request directly by running the external hook.
--- Documentation/config/receive.txt | 14 +++ builtin/receive-pack.c | 49 ++++++-- t/t5411-proc-receive-hook.sh | 189 +++++++++++++++++++++++++++---- 3 files changed, 220 insertions(+), 32 deletions(-)
Documentation/config/receive.txt +14 -0
@@ -114,6 +114,20 @@ receive.hideRefs:: An attempt to update or delete a hidden ref by `git push` is rejected. +receive.procReceiveRefs:: + This is a multi-valued variable that defines reference prefixes + to match the commands in `receive-pack`. Commands matching the + prefixes will be executed by an external hooks "proc-receive", + instead of the internal `execute_commands` function. If this + variable is not defined, the "proc-receive" hook will never be + used, and all commands will be executed by the internal + `execute_commands` function. + + For example, if this variable is set to "refs/for/", pushing to + reference such as "refs/for/master" will not create or update a + reference named "refs/for/master", but may create or update a + pull request directly by running an external hook. + receive.updateServerInfo:: If set to true, git-receive-pack will run git-update-server-info after receiving data from git-push and updating refs.
builtin/receive-pack.c +40 -9
80 lines not on this page · raw message
t/t5411-proc-receive-hook.sh +166 -23
327 lines not on this page · raw message
Signature · 1 line
2.26.0.rc0.5.gb02b988a14.dirty
PATCH v2 4/5 receive-pack: read env from proc-receive output
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
The “post-receive” hook may need the pull request ID generated by the “proc-receive” hook. The results can be passed between hooks by environment variables.
Each line of the message received from the standard output of the “proc-receive” hook in the key=value format is parsed as environment and these variables will be sent to environment of the “post-receive” hook.
--- builtin/receive-pack.c | 38 ++++++++++++++++++-- t/t5411-proc-receive-hook.sh | 70 ++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 3 deletions(-)
builtin/receive-pack.c +35 -3
@@ -87,6 +87,7 @@ static const char *nonce_status;static long nonce_stamp_slop;static timestamp_t nonce_stamp_slop_limit;static struct ref_transaction *transaction; +struct argv_array post_receive_env_array; static enum { KEEPALIVE_NEVER = 0,
@@ -694,7 +695,9 @@ struct receive_hook_feed_state { };typedef int (*feed_fn )(void *,const char **,size_t *); +typedef void (*stdout_handler_fn )(int out);static int run_and_feed_hook (const char *hook_name,feed_fn feed, +stdout_handler_fn stdout_handler,struct receive_hook_feed_state *feed_state) {struct child_process proc =CHILD_PROCESS_INIT ;
@@ -716,9 +719,15 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed, proc.argv = argv; proc.in =-1 ; - proc.stdout_to_stderr =1 ; +if (stdout_handler) + proc.out =-1 ; +else + proc.stdout_to_stderr =1 ; proc.trace2_hook_name = hook_name; +if (!strcmp (hook_name,"post-receive" ) && post_receive_env_array.argc >0 ) +argv_array_pushv (&proc.env_array, post_receive_env_array.argv); +if (feed_state->push_options) {int i;for (i =0 ; i < feed_state->push_options->nr; i++)
@@ -763,6 +772,10 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed,break ; }close (proc.in); + +if (stdout_handler) +stdout_handler (proc.out); +if (use_sideband)finish_async (&muxer);
@@ -819,7 +832,7 @@ static int run_receive_hook(struct command *commands,return 0 ; state.cmd = commands; state.push_options = push_options; - status =run_and_feed_hook (hook_name, feed_receive_hook, &state); + status =run_and_feed_hook (hook_name, feed_receive_hook,NULL , &state);strbuf_release (&state.buf);return status; }
@@ -853,6 +866,23 @@ static int run_update_hook(struct command *cmd)return finish_command (&proc); } +static void prepare_post_receive_env (int in) +{ +struct strbuf stdout_buf =STRBUF_INIT ; + +while (strbuf_getwholeline_fd (&stdout_buf, in,'\n' ) !=EOF ) { +char *p = stdout_buf.buf + stdout_buf.len -1 ; +if (*p =='\n' ) + *p ='\0' ; + p =strchr (stdout_buf.buf,'=' ); +if (p ==NULL ) +continue ; +argv_array_push (&post_receive_env_array, stdout_buf.buf); +strbuf_reset (&stdout_buf); + } +strbuf_release (&stdout_buf); +} +static int run_proc_receive_hook (struct command *commands,const struct string_list *push_options) {
@@ -869,7 +899,8 @@ static int run_proc_receive_hook(struct command *commands,return 0 ; state.cmd = commands; state.push_options = push_options; - status =run_and_feed_hook ("proc-receive" , feed_receive_hook, &state); + status =run_and_feed_hook ("proc-receive" , + feed_receive_hook, prepare_post_receive_env, &state);strbuf_release (&state.buf);return status; }
@@ -2040,6 +2071,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix) };string_list_init (&proc_receive_refs,0 ); +argv_array_init (&post_receive_env_array);packet_trace_identity ("receive-pack" );
t/t5411-proc-receive-hook.sh +70 -0
74 lines not on this page · raw message
Signature · 1 line
2.26.0.rc0.5.gb02b988a14.dirty
PATCH v2 5/5 hook: add document and example for "proc-receive" hook
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
--- Documentation/githooks.txt | 22 +++++ t/t5411-proc-receive-hook.sh | 71 ++++++++++++++++ templates/hooks--proc-receive.sample | 119 +++++++++++++++++++++++++++ 3 files changed, 212 insertions(+) create mode 100755 templates/hooks--proc-receive.sample
Documentation/githooks.txt +22 -0
@@ -333,6 +333,28 @@ The default 'update' hook, when enabled--and with `hooks.allowunannotated` config option unset or set to false--prevents unannotated tags to be pushed. +[[proc-receive]] +proc-receive +~~~~~~~~~~~~ +This hook is invoked by linkgit:git-receive-pack[1] when it reacts to +special `git push` command. According to refnames of the commands which +`git push` sends to 'git-receive-pack', the commands will be devided +into two groups by matching what the `receive.procReceiveRefs` +configuration variable defines. One group of the commands will execute +the internal `execute_commands` function to update the corresponding +refnames, and the other group of commands which have matching refnames +will execute this 'proc-receive' hook to create pull requests, etc. +If there is no `receive.procReceiveRefs` settings, this hook won't +execute at all, and all commands are sent to the internal +`execute_commands` function. + +Its exit status only determines the success or failure of the group of +commands with special refnames, unless atomic push is in use. + +This hook executes once for the receive operation. It takes no +arguments, but gets the same information as the +<<pre-receive,'pre-receive'>> hook does on its standard input. + [[post-receive]] post-receive ~~~~~~~~~~~~
t/t5411-proc-receive-hook.sh +71 -0
75 lines not on this page · raw message
templates/hooks--proc-receive.sample new file +119 -0
119 lines not on this page · raw message
Signature · 1 line
2.26.0.rc0.5.gb02b988a14.dirty
PATCH v5 1/6 transport: not report a non-head push as a branch
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
When pushing a new reference (not a head or tag), report it as a new reference instead of a new branch.
--- t/t5411-proc-receive-hook.sh | 153 +++++++++++++++++++++++++++++++++++ t/t5516-fetch-push.sh | 2 +- transport.c | 9 ++- 3 files changed, 160 insertions(+), 4 deletions(-) create mode 100755 t/t5411-proc-receive-hook.sh
t/t5411-proc-receive-hook.sh new file +153 -0
@@ -0,0 +1,153 @@ +#!/bin/sh +# +# Copyright (c) 2020 Jiang Xin +# + +test_description='Test proc-receive hook' + +. ./test-lib.sh + +# Create commits in <repo> and assign each commit's oid to shell variables +# given in the arguments (A, B, and C). E.g.: +# +# create_commits_in <repo> A B C +# +# NOTE: Never calling this function from a subshell since variable +# assignments will disappear when subshell exits. +create_commits_in () { + repo="$1" && + if ! parent=$(git -C "$repo" rev-parse HEAD^{}) + then + parent= + fi && + T=$(git -C "$repo" write-tree) && + shift && + while test $# -gt 0 + do + name=$1 && + test_tick && + if test -z "$parent" + then + oid=$(echo $name | git -C "$repo" commit-tree $T) + else + oid=$(echo $name | git -C "$repo" commit-tree -p $parent $T) + fi && + eval $name=$oid && + parent=$oid && + shift || + return 1 + done && + git -C "$repo" update-ref refs/heads/master $oid +} + +# Format the output of git-push, git-show-ref and other commands to make a +# user-friendly and stable text. We can easily prepare the expect text +# without having to worry about future changes of the commit ID and spaces +# of the output. We also replce single quotes with double quotes, because +# it is boring to prepare unquoted single quotes in expect txt. +make_user_friendly_and_stable_output () { + sed \ + -e "s/ *\$//" \ + -e "s/ */ /g" \ + -e "s/'/\"/g" \ + -e "s/$A/<COMMIT-A>/g" \ + -e "s/$B/<COMMIT-B>/g" \ + -e "s/$TAG/<TAG-v123>/g" \ + -e "s/$ZERO_OID/<ZERO-OID>/g" \ + -e "s/[0-9a-f]\{7,\}/<OID>/g" +} + +# Refs of upstream : master(B) next(A) +# Refs of workbench: master(A) tags/v123 +test_expect_success "setup" ' + git init --bare upstream && + git init workbench && + create_commits_in workbench A B && + ( + cd workbench && + # Try to make a stable fixed width for abbreviated commit ID, + # this fixed-width oid will be replaced with "<OID>". + git config core.abbrev 7 && + git remote add origin ../upstream && + git update-ref refs/heads/master $A && + git tag -m "v123" v123 $A && + git push origin \ + $B:refs/heads/master \ + $A:refs/heads/next + ) && + TAG=$(git -C workbench rev-parse v123) && + + # setup pre-receive hook + cat >upstream/hooks/pre-receive <<-\EOF && + #!/bin/sh + + echo >&2 "# pre-receive hook" + + while read old new ref + do + echo >&2 "pre-receive< $old $new $ref" + done + EOF + + # setup post-receive hook + cat >upstream/hooks/post-receive <<-\EOF && + #!/bin/sh + + echo >&2 "# post-receive hook" + + while read old new ref + do + echo >&2 "post-receive< $old $new $ref" + done + EOF + + chmod a+x \ + upstream/hooks/pre-receive \ + upstream/hooks/post-receive +' + +# Refs of upstream : master(B) next(A) +# Refs of workbench: master(A) tags/v123 +# git-push -f : master(A) NULL tags/v123 refs/review/master/topic(A) a/b/c(A) +test_expect_success "normal git-push command" ' + git -C workbench push -f origin \ + refs/tags/v123 \ + :refs/heads/next \ + HEAD:refs/heads/master \ + HEAD:refs/review/master/topic \ + HEAD:refs/heads/a/b/c \ + >out 2>&1 && + make_user_friendly_and_stable_output <out >actual && + cat >expect <<-EOF && + remote: # pre-receive hook + remote: pre-receive< <COMMIT-B> <COMMIT-A> refs/heads/master + remote: pre-receive< <COMMIT-A> <ZERO-OID> refs/heads/next + remote: pre-receive< <ZERO-OID> <TAG-v123> refs/tags/v123 + remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/review/master/topic + remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/a/b/c + remote: # post-receive hook + remote: post-receive< <COMMIT-B> <COMMIT-A> refs/heads/master + remote: post-receive< <COMMIT-A> <ZERO-OID> refs/heads/next + remote: post-receive< <ZERO-OID> <TAG-v123> refs/tags/v123 + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/review/master/topic + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/a/b/c + To ../upstream + + <OID>...<OID> HEAD -> master (forced update) + - [deleted] next + * [new tag] v123 -> v123 + * [new reference] HEAD -> refs/review/master/topic + * [new branch] HEAD -> a/b/c + EOF + test_cmp expect actual && + git -C upstream show-ref >out && + make_user_friendly_and_stable_output <out >actual && + cat >expect <<-EOF && + <COMMIT-A> refs/heads/a/b/c + <COMMIT-A> refs/heads/master + <COMMIT-A> refs/review/master/topic + <TAG-v123> refs/tags/v123 + EOF + test_cmp expect actual +' + +test_done
t/t5516-fetch-push.sh +1 -1
@@ -1039,7 +1039,7 @@ test_force_fetch_tag "annotated tag" "-f -a -m'tag message'" test_expect_success 'push --porcelain' ' mk_empty testrepo && echo >.git/foo "To testrepo" && - echo >>.git/foo "* refs/heads/master:refs/remotes/origin/master [new branch]" && + echo >>.git/foo "* refs/heads/master:refs/remotes/origin/master [new reference]" && echo >>.git/foo "Done" && git push >.git/bar --porcelain testrepo refs/heads/master:refs/remotes/origin/master && (
transport.c +6 -3
@@ -500,9 +500,12 @@ static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widt porcelain, summary_width); else if (is_null_oid(&ref->old_oid))print_ref_status ('*' , - (starts_with (ref->name,"refs/tags/" ) ?"[new tag]" : -"[new branch]" ), - ref, ref->peer_ref,NULL , porcelain, summary_width); + (starts_with (ref->name,"refs/tags/" ) + ?"[new tag]" + : (starts_with (ref->name,"refs/heads/" ) + ?"[new branch]" + :"[new reference]" )), + ref, ref->peer_ref,NULL , porcelain, summary_width); else {struct strbuf quickref =STRBUF_INIT ;char type;
Signature · 1 line
2.26.0.4.g39bcdcb101.dirty
v5 cover New proc-receive hook for centralized workflow
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
## Changes since v4
* Fixed code review issues.
* Reimplement read_proc_receive_result() of patch 2/6.
* Add new parameter for function print_ref_status() of patch 5/6.
* Documentation in patch 6/6.
## Range-diff v4...v5
1: 7c08735833 ! 1: a8bcc20439 transport: not report a non-head push as a branch
@@ t/t5411-proc-receive-hook.sh (new)
+#
+# create_commits_in <repo> A B C
+#
-+# NOTE: Avoid calling this function from a subshell since variable
++# NOTE: Never calling this function from a subshell since variable
+# assignments will disappear when subshell exits.
+create_commits_in () {
+ repo="$1" &&
-+ if ! parent=$(git -C "$repo" rev-parse HEAD^{} 2>/dev/null)
++ if ! parent=$(git -C "$repo" rev-parse HEAD^{})
+ then
+ parent=
+ fi &&
@@ t/t5411-proc-receive-hook.sh (new)
+ git -C "$repo" update-ref refs/heads/master $oid
+}
+
-+format_git_output () {
++# Format the output of git-push, git-show-ref and other commands to make a
++# user-friendly and stable text. We can easily prepare the expect text
++# without having to worry about future changes of the commit ID and spaces
++# of the output. We also replce single quotes with double quotes, because
++# it is boring to prepare unquoted single quotes in expect txt.
++make_user_friendly_and_stable_output () {
+ sed \
-+ -e "s/ *\$//g" \
++ -e "s/ *\$//" \
++ -e "s/ */ /g" \
++ -e "s/'/\"/g" \
+ -e "s/$A/<COMMIT-A>/g" \
+ -e "s/$B/<COMMIT-B>/g" \
-+ -e "s/$TAG/<COMMIT-T>/g" \
++ -e "s/$TAG/<TAG-v123>/g" \
+ -e "s/$ZERO_OID/<ZERO-OID>/g" \
-+ -e "s/'/\"/g"
++ -e "s/[0-9a-f]\{7,\}/<OID>/g"
+}
+
++# Refs of upstream : master(B) next(A)
++# Refs of workbench: master(A) tags/v123
+test_expect_success "setup" '
+ git init --bare upstream &&
+ git init workbench &&
+ create_commits_in workbench A B &&
+ (
+ cd workbench &&
-+ git remote add origin ../upstream &&
++ # Try to make a stable fixed width for abbreviated commit ID,
++ # this fixed-width oid will be replaced with "<OID>".
+ git config core.abbrev 7 &&
++ git remote add origin ../upstream &&
+ git update-ref refs/heads/master $A &&
-+ git tag -m "v1.0.0" v1.0.0 $A &&
++ git tag -m "v123" v123 $A &&
+ git push origin \
+ $B:refs/heads/master \
+ $A:refs/heads/next
+ ) &&
-+ TAG=$(cd workbench; git rev-parse v1.0.0) &&
++ TAG=$(git -C workbench rev-parse v123) &&
+
+ # setup pre-receive hook
-+ cat >upstream/hooks/pre-receive <<-EOF &&
++ cat >upstream/hooks/pre-receive <<-\EOF &&
+ #!/bin/sh
+
-+ printf >&2 "# pre-receive hook\n"
++ echo >&2 "# pre-receive hook"
+
+ while read old new ref
+ do
-+ printf >&2 "pre-receive< \$old \$new \$ref\n"
++ echo >&2 "pre-receive< $old $new $ref"
+ done
+ EOF
+
+ # setup post-receive hook
-+ cat >upstream/hooks/post-receive <<-EOF &&
++ cat >upstream/hooks/post-receive <<-\EOF &&
+ #!/bin/sh
+
-+ printf >&2 "# post-receive hook\n"
++ echo >&2 "# post-receive hook"
+
+ while read old new ref
+ do
-+ printf >&2 "post-receive< \$old \$new \$ref\n"
++ echo >&2 "post-receive< $old $new $ref"
+ done
+ EOF
+
@@ t/t5411-proc-receive-hook.sh (new)
+ upstream/hooks/post-receive
+'
+
++# Refs of upstream : master(B) next(A)
++# Refs of workbench: master(A) tags/v123
++# git-push -f : master(A) NULL tags/v123 refs/review/master/topic(A) a/b/c(A)
+test_expect_success "normal git-push command" '
-+ (
-+ cd workbench &&
-+ git push -f origin \
-+ refs/tags/v1.0.0 \
-+ :refs/heads/next \
-+ HEAD:refs/heads/master \
-+ HEAD:refs/review/master/topic \
-+ HEAD:refs/heads/a/b/c
-+ ) >out 2>&1 &&
-+ format_git_output <out >actual &&
++ git -C workbench push -f origin \
++ refs/tags/v123 \
++ :refs/heads/next \
++ HEAD:refs/heads/master \
++ HEAD:refs/review/master/topic \
++ HEAD:refs/heads/a/b/c \
++ >out 2>&1 &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ remote: # pre-receive hook
+ remote: pre-receive< <COMMIT-B> <COMMIT-A> refs/heads/master
+ remote: pre-receive< <COMMIT-A> <ZERO-OID> refs/heads/next
-+ remote: pre-receive< <ZERO-OID> <COMMIT-T> refs/tags/v1.0.0
++ remote: pre-receive< <ZERO-OID> <TAG-v123> refs/tags/v123
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/review/master/topic
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/a/b/c
+ remote: # post-receive hook
+ remote: post-receive< <COMMIT-B> <COMMIT-A> refs/heads/master
+ remote: post-receive< <COMMIT-A> <ZERO-OID> refs/heads/next
-+ remote: post-receive< <ZERO-OID> <COMMIT-T> refs/tags/v1.0.0
++ remote: post-receive< <ZERO-OID> <TAG-v123> refs/tags/v123
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/review/master/topic
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/a/b/c
+ To ../upstream
-+ + ce858e6...1029397 HEAD -> master (forced update)
-+ - [deleted] next
-+ * [new tag] v1.0.0 -> v1.0.0
-+ * [new reference] HEAD -> refs/review/master/topic
-+ * [new branch] HEAD -> a/b/c
++ + <OID>...<OID> HEAD -> master (forced update)
++ - [deleted] next
++ * [new tag] v123 -> v123
++ * [new reference] HEAD -> refs/review/master/topic
++ * [new branch] HEAD -> a/b/c
+ EOF
+ test_cmp expect actual &&
-+ (
-+ cd upstream &&
-+ git show-ref
-+ ) >out &&
-+ format_git_output <out >actual &&
++ git -C upstream show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/a/b/c
+ <COMMIT-A> refs/heads/master
+ <COMMIT-A> refs/review/master/topic
-+ <COMMIT-T> refs/tags/v1.0.0
++ <TAG-v123> refs/tags/v123
+ EOF
+ test_cmp expect actual
+'
+
+test_done
+ ## t/t5516-fetch-push.sh ##
+@@ t/t5516-fetch-push.sh: test_force_fetch_tag "annotated tag" "-f -a -m'tag message'"
+ test_expect_success 'push --porcelain' '
+ mk_empty testrepo &&
+ echo >.git/foo "To testrepo" &&
+- echo >>.git/foo "* refs/heads/master:refs/remotes/origin/master [new branch]" &&
++ echo >>.git/foo "* refs/heads/master:refs/remotes/origin/master [new reference]" &&
+ echo >>.git/foo "Done" &&
+ git push >.git/bar --porcelain testrepo refs/heads/master:refs/remotes/origin/master &&
+ (
+
## transport.c ##
@@ transport.c: static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widt
+ porcelain, summary_width);
else if (is_null_oid(&ref->old_oid))
print_ref_status('*',
- (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
+- (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
- "[new branch]"),
-+ (starts_with(ref->name, "refs/heads/") ? "[new branch]" :
-+ "[new reference]")),
- ref, ref->peer_ref, NULL, porcelain, summary_width);
+- ref, ref->peer_ref, NULL, porcelain, summary_width);
++ (starts_with(ref->name, "refs/tags/")
++ ? "[new tag]"
++ : (starts_with(ref->name, "refs/heads/")
++ ? "[new branch]"
++ : "[new reference]")),
++ ref, ref->peer_ref, NULL, porcelain, summary_width);
else {
struct strbuf quickref = STRBUF_INIT;
+ char type;
2: 67dea721b7 ! 2: 14641ec57e receive-pack: add new proc-receive hook
@@ Commit message
We can use this "proc-receive" command to create pull requests or send
emails for code review.
- This "proc-receive" hook reads commands, push-options (optional), and
- send result using a protocol in pkt-line format. In the following
- example, The letter "S" stands for "receive-pack" and letter "H" stands
- for the hook.
+ Suggested by Junio, this "proc-receive" hook reads the commands,
+ push-options (optional), and send result using a protocol in pkt-line
+ format. In the following example, The letter "S" stands for
+ "receive-pack" and letter "H" stands for the hook.
- S: PKT-LINE(version=1\0push-options ...)
+ # Version and capabilities negotiation.
+ S: PKT-LINE(version=1\0push-options atomic...)
S: flush-pkt
-
- H: PKT-LINE(version=1\0push-options ...)
+ H: PKT-LINE(version=1\0push-options...)
H: flush-pkt
+ # Send commands from server to the hook.
S: PKT-LINE(old-oid new-oid ref)
S: ... ...
S: flush-pkt
-
- # Optional, only if push-options is negotiated.
+ # Only if push-options have been negotiated.
S: PKT-LINE(push-option)
S: ... ...
S: flush-pkt
+ # Receive result from the hook.
# OK, run this command successfully.
H: PKT-LINE(old-oid new-oid ref ok)
-
# NO, I reject it.
H: PKT-LINE(old-oid new-oid ref ng reason)
-
# OK, but use an alternate reference. (in latter commit)
H: PKT-LINE(old-oid new-oid ref ok ref:alt-ref)
-
# It will fallthrough to receive-pack to execute. (in latter commit)
H: PKT-LINE(old-oid new-oid ref ft)
-
H: ... ...
H: flush-pkt
@@ Commit message
the result to replace the commands that have specific `run_proc_receive`
field turned on.
+ Suggested-by: Junio C Hamano <[email protected]>
Signed-off-by: Jiang Xin <[email protected]>
## Makefile ##
@@ Makefile: TEST_BUILTINS_OBJS += test-parse-pathspec-file.o
TEST_BUILTINS_OBJS += test-read-cache.o
## builtin/receive-pack.c ##
-@@ builtin/receive-pack.c: struct command {
+@@ builtin/receive-pack.c: static void write_head_info(void)
+ packet_flush(1);
+ }
+
++#define RUN_PROC_RECEIVE_SCHEDULE 1
++#define RUN_PROC_RECEIVE_RETURNED 2
+ struct command {
struct command *next;
const char *error_string;
unsigned int skip_update:1,
- did_not_exist:1;
+ did_not_exist:1,
-+ run_proc_receive:1;
++ run_proc_receive:2;
int index;
struct object_id old_oid;
struct object_id new_oid;
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
return finish_command(&proc);
}
++static struct command *find_command_by_refname(const struct command *list,
++ const char *refname)
++{
++ for ( ; list; list = list->next)
++ if (!strcmp(list->ref_name, refname))
++ return (struct command *)list;
++ return NULL;
++}
++
+static int read_proc_receive_result(struct packet_reader *reader,
-+ struct command **commands)
++ struct command *commands)
+{
-+ struct command **tail = commands;
++ struct command *hint;
++ struct command *cmd;
+ int code = 0;
+
++ hint = NULL;
+ for (;;) {
+ struct object_id old_oid, new_oid;
-+ struct command *cmd;
+ const char *refname;
+ const char *p;
+ char *status;
+ char *msg = NULL;
+
-+ if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
++ if (packet_reader_read(reader) != PACKET_READ_NORMAL)
+ break;
-+ }
-+
+ if (parse_oid_hex(reader->line, &old_oid, &p) ||
+ *p++ != ' ' ||
+ parse_oid_hex(p, &new_oid, &p) ||
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ die("protocol error: proc-receive has bad status '%s' for '%s'",
+ status, reader->line);
+
-+ FLEX_ALLOC_MEM(cmd, ref_name, refname, strlen(refname));
-+ oidcpy(&cmd->old_oid, &old_oid);
-+ oidcpy(&cmd->new_oid, &new_oid);
-+ cmd->run_proc_receive = 1;
-+
++ /* first try searching at our hint, falling back to all refs */
++ if (hint)
++ hint = find_command_by_refname(hint, refname);
++ if (!hint)
++ hint = find_command_by_refname(commands, refname);
++ if (!hint) {
++ warning("proc-receive reported status on unknown ref: %s",
++ refname);
++ continue;
++ }
++ if (!hint->run_proc_receive) {
++ warning("proc-receive reported status on ref of builtin command: %s",
++ refname);
++ continue;
++ }
++ hint->run_proc_receive |= RUN_PROC_RECEIVE_RETURNED;
++ oidcpy(&hint->old_oid, &old_oid);
++ oidcpy(&hint->new_oid, &new_oid);
+ if (!strcmp(status, "ng")) {
+ if (msg)
-+ cmd->error_string = xstrdup(msg);
++ hint->error_string = xstrdup(msg);
+ else
-+ cmd->error_string = "failed";
++ hint->error_string = "failed";
+ code = 1;
+ } else if (strcmp("ok", status)) {
+ die("protocol error: proc-receive has bad status '%s' for '%s'",
+ status, reader->line);
+ }
-+
-+ *tail = cmd;
-+ tail = &cmd->next;
+ }
++
++ for (cmd = commands; cmd; cmd = cmd->next)
++ if (cmd->run_proc_receive &&
++ !(cmd->run_proc_receive & RUN_PROC_RECEIVE_RETURNED))
++ cmd->error_string = "no report from proc-receive";
++
+ return code;
+}
+
-+static int run_proc_receive_hook(struct command **commands,
++static int run_proc_receive_hook(struct command *commands,
+ const struct string_list *push_options)
+{
+ struct child_process proc = CHILD_PROCESS_INIT;
+ struct async muxer;
-+ struct command *result_commands = NULL;
+ struct command *cmd;
+ const char *argv[2];
+ struct packet_reader reader;
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ packet_reader_init(&reader, proc.out, NULL, 0,
+ PACKET_READ_CHOMP_NEWLINE |
+ PACKET_READ_DIE_ON_ERR_PACKET);
-+ if (use_push_options)
-+ strbuf_addstr(&cap, " push-options");
+ if (use_atomic)
+ strbuf_addstr(&cap, " atomic");
++ if (use_push_options)
++ strbuf_addstr(&cap, " push-options");
+ if (cap.len) {
+ packet_write_fmt(proc.in, "version=1%c%s\n", '\0', cap.buf + 1);
+ strbuf_release(&cap);
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ die("protocol error: unknown proc-receive version '%d'", version);
+
+ /* Send commands */
-+ for (cmd = *commands; cmd; cmd = cmd->next) {
++ for (cmd = commands; cmd; cmd = cmd->next) {
+ char *old_hex, *new_hex;
+
+ if (!cmd->run_proc_receive || cmd->skip_update || cmd->error_string)
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ }
+
+ /* Read result from proc-receive */
-+ code = read_proc_receive_result(&reader, &result_commands);
++ code = read_proc_receive_result(&reader, commands);
+ close(proc.in);
+ close(proc.out);
+ if (use_sideband)
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+
+ sigchain_pop(SIGPIPE);
+
-+ /* After receiving the result from the "proc-receive" hook,
-+ * "receive-pack" will use the result to replace commands that
-+ * have specific `run_proc_receive` field.
-+ */
-+ for (cmd = *commands; cmd; cmd = cmd->next)
-+ if (!cmd->run_proc_receive)
-+ break;
-+
-+ /* Merge commands with result_commands and sort */
-+ if (!cmd) {
-+ *commands = result_commands;
-+ } else {
-+ struct command *next_cmd = cmd;
-+ struct command *next_result = result_commands;
-+ struct command *head = NULL;
-+ struct command *tail = NULL;
-+
-+ if (!next_result ||
-+ strcmp(next_cmd->ref_name, next_result->ref_name) < 0) {
-+ head = next_cmd;
-+ next_cmd = next_cmd->next;
-+ } else {
-+ head = next_result;
-+ next_result = next_result->next;
-+ }
-+ tail = head;
-+
-+ for (;;) {
-+ if (!next_cmd) {
-+ tail->next = next_result;
-+ break;
-+ } else if (next_cmd->run_proc_receive) {
-+ next_cmd = next_cmd->next;
-+ } else if (!next_result) {
-+ tail->next = next_cmd;
-+ next_cmd = next_cmd->next;
-+ tail = tail->next;
-+ } else {
-+ if (strcmp(next_cmd->ref_name, next_result->ref_name) < 0) {
-+ tail->next = next_cmd;
-+ next_cmd = next_cmd->next;
-+ tail = tail->next;
-+ } else {
-+ tail->next = next_result;
-+ next_result = next_result->next;
-+ tail = tail->next;
-+ }
-+ }
-+ }
-+ *commands = head;
-+ }
-+
+ return code;
+}
+
@@ builtin/receive-pack.c: static void execute_commands_atomic(struct command *comm
continue;
cmd->error_string = update(cmd, si);
-@@ builtin/receive-pack.c: static void execute_commands_atomic(struct command *commands,
- strbuf_release(&err);
- }
-
--static void execute_commands(struct command *commands,
-+static void execute_commands(struct command **orig_commands,
- const char *unpacker_error,
- struct shallow_info *si,
- const struct string_list *push_options)
- {
-+ struct command *commands = *orig_commands;
- struct check_connected_options opt = CHECK_CONNECTED_INIT;
- struct command *cmd;
+@@ builtin/receive-pack.c: static void execute_commands(struct command *commands,
struct iterate_data data;
struct async muxer;
int err_fd = 0;
@@ builtin/receive-pack.c: static void execute_commands(struct command *commands,
+ if (run_proc_receive) {
+ int code;
+
-+ code = run_proc_receive_hook(orig_commands, push_options);
-+ commands = *orig_commands;
++ code = run_proc_receive_hook(commands, push_options);
+ if (code) {
+ for (cmd = commands; cmd; cmd = cmd->next) {
+ if (!cmd->error_string && (cmd->run_proc_receive || use_atomic))
@@ builtin/receive-pack.c: static void execute_commands(struct command *commands,
if (use_atomic)
execute_commands_atomic(commands, si);
else
-@@ builtin/receive-pack.c: int cmd_receive_pack(int argc, const char **argv, const char *prefix)
- update_shallow_info(commands, &si, &ref);
- }
- use_keepalive = KEEPALIVE_ALWAYS;
-- execute_commands(commands, unpack_status, &si,
-+ execute_commands(&commands, unpack_status, &si,
- &push_options);
- if (pack_lockfile)
- unlink_or_warn(pack_lockfile);
## t/helper/test-proc-receive.c (new) ##
@@
@@ t/helper/test-proc-receive.c (new)
+static int version = 1;
+static int verbose = 0;
+static int no_push_options = 0;
++static int use_atomic = 0;
+static int use_push_options = 0;
+static struct string_list returns = STRING_LIST_INIT_NODUP;
+
@@ t/helper/test-proc-receive.c (new)
+ linelen = strlen(reader->line);
+ if (linelen < reader->pktlen) {
+ const char *feature_list = reader->line + linelen + 1;
++ if (parse_feature_request(feature_list, "atomic"))
++ use_atomic= 1;
+ if (parse_feature_request(feature_list, "push-options"))
+ use_push_options = 1;
+ }
@@ t/helper/test-proc-receive.c (new)
+ if (verbose) {
+ struct command *cmd;
+
++ if (use_push_options || use_atomic)
++ fprintf(stderr, "proc-receive:%s%s\n",
++ use_atomic? " atomic": "",
++ use_push_options ? " push_options": "");
++
+ for (cmd = commands; cmd; cmd = cmd->next) {
+ char *old_hex, *new_hex;
+
@@ t/helper/test-tool.h: int cmd__parse_pathspec_file(int argc, const char** argv);
int cmd__read_cache(int argc, const char **argv);
## t/t5411-proc-receive-hook.sh ##
-@@ t/t5411-proc-receive-hook.sh: format_git_output () {
- -e "s/'/\"/g"
+@@ t/t5411-proc-receive-hook.sh: make_user_friendly_and_stable_output () {
+ -e "s/[0-9a-f]\{7,\}/<OID>/g"
}
+# Asynchronous sideband may generate inconsistent output messages,
+# sort before comparison.
+test_sorted_cmp () {
-+ if ! $GIT_TEST_CMP "$@"
++ if ! $GIT_TEST_CMP "$@" >/dev/null 2>&1
+ then
+ cmd=$GIT_TEST_CMP
+ for f in "$@"
@@ t/t5411-proc-receive-hook.sh: format_git_output () {
+ sort "$f" >"$f.sorted"
+ cmd="$cmd \"$f.sorted\""
+ done
-+ if ! eval $cmd
++ if ! eval $cmd >/dev/null 2>&1
+ then
+ $GIT_TEST_CMP "$@"
+ fi
+ fi
+}
+
+ # Refs of upstream : master(B) next(A)
+ # Refs of workbench: master(A) tags/v123
test_expect_success "setup" '
- git init --bare upstream &&
- git init workbench &&
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
test_cmp expect actual
'
++# Refs of upstream : master(A) tags/v123 refs/review/master/topic(A) a/b/c(A)
++# Refs of workbench: master(A) tags/v123
+test_expect_success "cleanup" '
+ (
+ cd upstream &&
+ git update-ref -d refs/review/master/topic &&
-+ git update-ref -d refs/tags/v1.0.0 &&
++ git update-ref -d refs/tags/v123 &&
+ git update-ref -d refs/heads/a/b/c
+ )
+'
+
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push : next(A) refs/for/master/topic(A)
+test_expect_success "no proc-receive hook, fail to push special ref" '
-+ (
-+ cd workbench &&
-+ test_must_fail git push origin \
-+ HEAD:next \
-+ HEAD:refs/for/master/topic
-+ ) >out 2>&1 &&
-+ format_git_output <out >actual &&
++ test_must_fail git -C workbench push origin \
++ HEAD:next \
++ HEAD:refs/for/master/topic \
++ >out 2>&1 &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ remote: # pre-receive hook
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ remote: # post-receive hook
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/next
+ To ../upstream
-+ * [new branch] HEAD -> next
++ * [new branch] HEAD -> next
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ error: failed to push some refs to "../upstream"
+ EOF
+ test_cmp expect actual &&
-+ (
-+ cd upstream &&
-+ git show-ref
-+ ) >out &&
-+ format_git_output <out >actual &&
++ git -C upstream show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
+ <COMMIT-A> refs/heads/next
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ test_cmp expect actual
+'
+
++# Refs of upstream : master(A) next(A)
++# Refs of workbench: master(A) tags/v123
+test_expect_success "cleanup" '
-+ (
-+ cd upstream &&
-+ git update-ref -d refs/heads/next
-+ )
++ git -C upstream update-ref -d refs/heads/next
+'
+
-+# TODO: report for the failure of master branch is unnecessary.
-+test_expect_success "no proc-receive hook, fail all for atomic push" '
-+ (
-+ cd workbench &&
-+ test_must_fail git push --atomic origin \
-+ HEAD:next \
-+ HEAD:refs/for/master/topic
-+ ) >out 2>&1 &&
-+ format_git_output <out >actual &&
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push --atomic: next(A) refs/for/master/topic(A)
++test_expect_failure "no proc-receive hook, fail all for atomic push" '
++ test_must_fail git -C workbench push --atomic origin \
++ HEAD:next \
++ HEAD:refs/for/master/topic >out 2>&1 &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ remote: # pre-receive hook
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: error: cannot to find hook "proc-receive"
+ To ../upstream
-+ ! [rejected] master (atomic push failed)
+ ! [remote rejected] HEAD -> next (fail to run proc-receive hook)
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ error: failed to push some refs to "../upstream"
+ EOF
+ test_cmp expect actual &&
-+ (
-+ cd upstream &&
-+ git show-ref
-+ ) >out &&
-+ format_git_output <out >actual &&
++ git -C upstream show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
+ EOF
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ chmod a+x upstream/hooks/proc-receive
+'
+
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push : refs/for/master/topic(A)
+test_expect_success "proc-receive bad protocol: unknown version" '
-+ (
-+ cd workbench &&
-+ test_must_fail git push origin \
-+ HEAD:refs/for/master/topic
-+ ) >out 2>&1 &&
-+ format_git_output <out | grep "protocol error" >actual &&
++ test_must_fail git -C workbench push origin \
++ HEAD:refs/for/master/topic \
++ >out 2>&1 &&
++ make_user_friendly_and_stable_output <out | grep "protocol error" >actual &&
+ cat >expect <<-EOF &&
+ fatal: protocol error: unknown proc-receive version "2"
+ EOF
+ test_cmp expect actual &&
-+ (
-+ cd upstream &&
-+ git show-ref
-+ ) >out &&
-+ format_git_output <out >actual &&
++ git -C upstream show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
+ EOF
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ EOF
+'
+
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push : next(A) refs/for/master/topic(A)
+test_expect_success "proc-receive bad protocol: no report" '
-+ (
-+ cd workbench &&
-+ test_must_fail git push origin \
-+ HEAD:refs/for/master/topic
-+ ) >out 2>&1 &&
-+ format_git_output <out >actual &&
++ test_must_fail git -C workbench push origin \
++ HEAD:refs/heads/next \
++ HEAD:refs/for/master/topic >out 2>&1 &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ remote: # pre-receive hook
++ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: # post-receive hook
++ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/next
+ To ../upstream
-+ ! [remote failure] HEAD -> refs/for/master/topic (remote failed to report status)
++ * [new branch] HEAD -> next
++ ! [remote rejected] HEAD -> refs/for/master/topic (no report from proc-receive)
+ error: failed to push some refs to "../upstream"
+ EOF
+ test_cmp expect actual &&
-+ (
-+ cd upstream &&
-+ git show-ref
-+ ) >out &&
-+ format_git_output <out >actual &&
++ git -C upstream show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
++ <COMMIT-A> refs/heads/next
+ EOF
+ test_cmp expect actual
+'
+
++# Refs of upstream : master(A) next(A)
++# Refs of workbench: master(A) tags/v123
++test_expect_success "cleanup" '
++ git -C upstream update-ref -d refs/heads/next
++
++'
++
+test_expect_success "setup proc-receive hook (bad oid)" '
+ cat >upstream/hooks/proc-receive <<-EOF
+ #!/bin/sh
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ EOF
+'
+
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push : refs/for/master/topic
+test_expect_success "proc-receive bad protocol: bad oid" '
-+ (
-+ cd workbench &&
-+ test_must_fail git push origin \
-+ HEAD:refs/for/master/topic
-+ ) >out 2>&1 &&
-+ format_git_output <out | grep "protocol error" >actual &&
++ test_must_fail git -C workbench push origin \
++ HEAD:refs/for/master/topic\
++ >out 2>&1 &&
++ make_user_friendly_and_stable_output <out | grep "protocol error" >actual &&
+ cat >expect <<-EOF &&
+ fatal: protocol error: proc-receive expected "old new ref status [msg]", got "bad-id new-id ref ok"
+ EOF
+ test_cmp expect actual &&
-+ (
-+ cd upstream &&
-+ git show-ref
-+ ) >out &&
-+ format_git_output <out >actual &&
++ git -C upstream show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
+ EOF
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ EOF
+'
+
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push : refs/for/master/topic
+test_expect_success "proc-receive bad protocol: no status" '
-+ (
-+ cd workbench &&
-+ test_must_fail git push origin \
-+ HEAD:refs/for/master/topic
-+ ) >out 2>&1 &&
-+ format_git_output <out | grep "protocol error" >actual &&
++ test_must_fail git -C workbench push origin \
++ HEAD:refs/for/master/topic \
++ >out 2>&1 &&
++ make_user_friendly_and_stable_output <out | grep "protocol error" >actual &&
+ cat >expect <<-EOF &&
+ fatal: protocol error: proc-receive expected "old new ref status [msg]", got "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
+ EOF
+ test_cmp expect actual &&
-+ (
-+ cd upstream &&
-+ git show-ref
-+ ) >out &&
-+ format_git_output <out >actual &&
++ git -C upstream show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
+ EOF
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ EOF
+'
+
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push : refs/for/master/topic
+test_expect_success "proc-receive bad protocol: unknown status" '
-+ (
-+ cd workbench &&
-+ test_must_fail git push origin \
-+ HEAD:refs/for/master/topic
-+ ) >out 2>&1 &&
-+ format_git_output <out | grep "protocol error" >actual &&
++ test_must_fail git -C workbench push origin \
++ HEAD:refs/for/master/topic \
++ >out 2>&1 &&
++ make_user_friendly_and_stable_output <out | grep "protocol error" >actual &&
+ cat >expect <<-EOF &&
+ fatal: protocol error: proc-receive has bad status "xx" for "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
+ EOF
+ test_cmp expect actual &&
-+ (
-+ cd upstream &&
-+ git show-ref
-+ ) >out &&
-+ format_git_output <out >actual &&
++ git -C upstream show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
+ EOF
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ EOF
+'
+
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push : refs/for/master/topic
+test_expect_success "proc-receive bad protocol: bad status" '
-+ (
-+ cd workbench &&
-+ test_must_fail git push origin \
-+ HEAD:refs/for/master/topic
-+ ) >out 2>&1 &&
-+ format_git_output <out | grep "protocol error" >actual &&
++ test_must_fail git -C workbench push origin \
++ HEAD:refs/for/master/topic \
++ >out 2>&1 &&
++ make_user_friendly_and_stable_output <out | grep "protocol error" >actual &&
+ cat >expect <<-EOF &&
+ fatal: protocol error: proc-receive has bad status "bad status" for "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
+ EOF
+ test_cmp expect actual &&
-+ (
-+ cd upstream &&
-+ git show-ref
-+ ) >out &&
-+ format_git_output <out >actual &&
++ git -C upstream show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
+ EOF
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ EOF
+'
+
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push : refs/for/master/topic
+test_expect_success "proc-receive: fail to update (no message)" '
-+ (
-+ cd workbench &&
-+ test_must_fail git push origin \
-+ HEAD:refs/for/master/topic
-+ ) >out 2>&1 &&
-+ format_git_output <out >actual &&
++ test_must_fail git -C workbench push origin \
++ HEAD:refs/for/master/topic \
++ >out 2>&1 &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ remote: # pre-receive hook
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ error: failed to push some refs to "../upstream"
+ EOF
+ test_cmp expect actual &&
-+ (
-+ cd upstream &&
-+ git show-ref
-+ ) >out &&
-+ format_git_output <out >actual &&
++ git -C upstream show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
+ EOF
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ EOF
+'
+
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push : refs/for/master/topic
+test_expect_success "proc-receive: fail to update (has message)" '
-+ (
-+ cd workbench &&
-+ test_must_fail git push origin \
-+ HEAD:refs/for/master/topic
-+ ) >out 2>&1 &&
-+ format_git_output <out >actual &&
++ test_must_fail git -C workbench push origin \
++ HEAD:refs/for/master/topic \
++ >out 2>&1 &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ remote: # pre-receive hook
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ error: failed to push some refs to "../upstream"
+ EOF
+ test_cmp expect actual &&
-+ (
-+ cd upstream &&
-+ git show-ref
-+ ) >out &&
-+ format_git_output <out >actual &&
++ git -C upstream show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
+ EOF
+ test_cmp expect actual
+'
+
++test_expect_success "setup proc-receive hook (report status on builtin command)" '
++ cat >upstream/hooks/proc-receive <<-EOF
++ #!/bin/sh
++
++ printf >&2 "# proc-receive hook\n"
++
++ test-tool proc-receive -v \
++ -r "$ZERO_OID $A refs/heads/master ok"
++ EOF
++'
++
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push : (B) refs/for/master/topic
++test_expect_success "proc-receive: warning on report for builtin command" '
++ test_must_fail git -C workbench push origin \
++ $B:refs/heads/master \
++ HEAD:refs/for/master/topic \
++ >out 2>&1 &&
++ make_user_friendly_and_stable_output <out >actual &&
++ cat >expect <<-EOF &&
++ remote: # pre-receive hook
++ remote: pre-receive< <COMMIT-A> <COMMIT-B> refs/heads/master
++ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: # proc-receive hook
++ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/heads/master ok
++ warning: proc-receive reported status on ref of builtin command: refs/heads/master
++ remote: # post-receive hook
++ remote: post-receive< <COMMIT-A> <COMMIT-B> refs/heads/master
++ To ../upstream
++ <OID>..<OID> <COMMIT-B> -> master
++ ! [remote rejected] HEAD -> refs/for/master/topic (no report from proc-receive)
++ error: failed to push some refs to "../upstream"
++ EOF
++ test_sorted_cmp expect actual &&
++ git -C upstream show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
++ cat >expect <<-EOF &&
++ <COMMIT-B> refs/heads/master
++ EOF
++ test_cmp expect actual
++'
++
++test_expect_success "cleanup" '
++ git -C upstream update-ref refs/heads/master $A
++'
++
+test_expect_success "setup proc-receive hook (ok)" '
+ cat >upstream/hooks/proc-receive <<-EOF
+ #!/bin/sh
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ EOF
+'
+
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push : refs/for/master/topic
+test_expect_success "proc-receive: ok" '
-+ (
-+ cd workbench &&
-+ git push origin \
-+ HEAD:refs/for/master/topic
-+ ) >out 2>&1 &&
-+ format_git_output <out >actual &&
++ git -C workbench push origin \
++ HEAD:refs/for/master/topic \
++ >out 2>&1 &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ remote: # pre-receive hook
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ remote: # post-receive hook
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ To ../upstream
-+ * [new reference] HEAD -> refs/for/master/topic
++ * [new reference] HEAD -> refs/for/master/topic
+ EOF
+ test_cmp expect actual &&
-+ (
-+ cd upstream &&
-+ git show-ref
-+ ) >out &&
-+ format_git_output <out >actual &&
++ git -C upstream show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
+ EOF
+ test_cmp expect actual
+'
+
-+test_expect_success "proc-receive: report unknown ref" '
-+ (
-+ cd workbench &&
-+ test_must_fail git push origin \
-+ HEAD:refs/for/a/b/c/my/topic
-+ ) >out 2>&1 &&
-+ format_git_output <out >actual &&
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push : refs/for/a/b/c/my/topic
++test_expect_success "proc-receive: no report from proc-receive" '
++ test_must_fail git -C workbench push origin \
++ HEAD:refs/for/a/b/c/my/topic \
++ >out 2>&1 &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ remote: # pre-receive hook
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/my/topic
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/my/topic
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic ok
-+ warning: remote reported status on unknown ref: refs/for/master/topic
-+ remote: # post-receive hook
-+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ warning: proc-receive reported status on unknown ref: refs/for/master/topic
+ To ../upstream
-+ ! [remote failure] HEAD -> refs/for/a/b/c/my/topic (remote failed to report status)
++ ! [remote rejected] HEAD -> refs/for/a/b/c/my/topic (no report from proc-receive)
+ error: failed to push some refs to "../upstream"
+ EOF
-+ test_cmp expect actual &&
-+ (
-+ cd upstream &&
-+ git show-ref
-+ ) >out &&
-+ format_git_output <out >actual &&
++ test_sorted_cmp expect actual &&
++ git -C upstream show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
+ EOF
+ test_cmp expect actual
+'
+
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push -o ... : refs/for/master/topic
+test_expect_success "not support push options" '
-+ (
-+ cd workbench &&
-+ test_must_fail git push \
-+ -o issue=123 \
-+ -o reviewer=user1 \
-+ origin \
-+ HEAD:refs/for/master/topic
-+ ) >out 2>&1 &&
-+ format_git_output <out >actual &&
++ test_must_fail git -C workbench push \
++ -o issue=123 \
++ -o reviewer=user1 \
++ origin \
++ HEAD:refs/for/master/topic \
++ >out 2>&1 &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ fatal: the receiving end does not support push options
+ fatal: the remote end hung up unexpectedly
+ EOF
+ test_cmp expect actual &&
-+ (
-+ cd upstream &&
-+ git show-ref
-+ ) >out &&
-+ format_git_output <out >actual &&
++ git -C upstream show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
+ EOF
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+'
+
+test_expect_success "enable push options" '
-+ (
-+ cd upstream &&
-+ git config receive.advertisePushOptions true
-+ )
++ git -C upstream config receive.advertisePushOptions true
+'
+
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push -o ... : next(A) refs/for/master/topic
+test_expect_success "push with options" '
-+ (
-+ cd workbench &&
-+ git push \
-+ -o issue=123 \
-+ -o reviewer=user1 \
-+ origin \
-+ HEAD:refs/heads/next \
-+ HEAD:refs/for/master/topic
-+ ) >out 2>&1 &&
-+ format_git_output <out >actual &&
++ git -C workbench push \
++ --atomic \
++ -o issue=123 \
++ -o reviewer=user1 \
++ origin \
++ HEAD:refs/heads/next \
++ HEAD:refs/for/master/topic \
++ >out 2>&1 &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ remote: # pre-receive hook
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: # proc-receive hook
++ remote: proc-receive: atomic push_options
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: proc-receive< issue=123
+ remote: proc-receive< reviewer=user1
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic ok
+ remote: # post-receive hook
-+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/next
++ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ To ../upstream
-+ * [new branch] HEAD -> next
-+ * [new reference] HEAD -> refs/for/master/topic
++ * [new branch] HEAD -> next
++ * [new reference] HEAD -> refs/for/master/topic
+ EOF
+ test_cmp expect actual &&
-+ (
-+ cd upstream &&
-+ git show-ref
-+ ) >out &&
-+ format_git_output <out >actual &&
++ git -C upstream show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
+ <COMMIT-A> refs/heads/next
3: d3d4ee428b = 3: 2440a474bd refs.c: refactor to reuse ref_is_hidden()
4: 5e049d89b1 ! 4: e041582643 receive-pack: new config receive.procReceiveRefs
@@ builtin/receive-pack.c: static int receive_pack_config(const char *var, const ch
return git_default_config(var, value, cb);
}
-@@ builtin/receive-pack.c: static void execute_commands(struct command **orig_commands,
+@@ builtin/receive-pack.c: static void execute_commands(struct command *commands,
/* Try to find commands that have special prefix in their reference names,
* and mark them to run an external "proc-receive" hook later.
*/
@@ t/t5411-proc-receive-hook.sh: test_expect_success "cleanup" '
+ )
+'
+
- test_expect_success "no proc-receive hook, fail to push special ref" '
- (
- cd workbench &&
+ # Refs of upstream : master(A)
+ # Refs of workbench: master(A) tags/v123
+ # git push : next(A) refs/for/master/topic(A)
@@ t/t5411-proc-receive-hook.sh: test_expect_success "push with options" '
test_cmp expect actual
'
++# Refs of upstream : master(A) next(A)
++# Refs of workbench: master(A) tags/v123
+test_expect_success "cleanup" '
-+ (
-+ cd upstream &&
-+ git update-ref -d refs/heads/next
-+ )
++ git -C upstream update-ref -d refs/heads/next
+'
+
+test_expect_success "setup proc-receive hook" '
@@ t/t5411-proc-receive-hook.sh: test_expect_success "push with options" '
+ chmod a+x upstream/hooks/proc-receive
+'
+
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push : refs/for/next/topic(A) refs/review/a/b/c/topic(A) refs/for/master/topic(A)
+test_expect_success "report update of all special refs" '
-+ (
-+ cd workbench &&
-+ git push origin \
-+ HEAD:refs/for/next/topic \
-+ HEAD:refs/review/a/b/c/topic \
-+ HEAD:refs/for/master/topic
-+ ) >out 2>&1 &&
-+ format_git_output <out >actual &&
++ git -C workbench push origin \
++ HEAD:refs/for/next/topic \
++ HEAD:refs/review/a/b/c/topic \
++ HEAD:refs/for/master/topic \
++ >out 2>&1 &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ remote: # pre-receive hook
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
@@ t/t5411-proc-receive-hook.sh: test_expect_success "push with options" '
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/review/a/b/c/topic
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ To ../upstream
-+ * [new reference] HEAD -> refs/for/next/topic
-+ * [new reference] HEAD -> refs/review/a/b/c/topic
-+ * [new reference] HEAD -> refs/for/master/topic
++ * [new reference] HEAD -> refs/for/next/topic
++ * [new reference] HEAD -> refs/review/a/b/c/topic
++ * [new reference] HEAD -> refs/for/master/topic
+ EOF
+ test_cmp expect actual &&
-+ (
-+ cd upstream &&
-+ git show-ref
-+ ) >out &&
-+ format_git_output <out >actual &&
++ git -C upstream show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
+ EOF
@@ t/t5411-proc-receive-hook.sh: test_expect_success "push with options" '
+ chmod a+x upstream/hooks/proc-receive
+'
+
-+test_expect_success "report mixed refs update (head first)" '
-+ (
-+ cd workbench &&
-+ git push origin \
-+ HEAD:refs/heads/zzz \
-+ HEAD:refs/for/next/topic \
-+ HEAD:refs/heads/yyy \
-+ HEAD:refs/for/master/topic
-+ ) >out 2>&1 &&
-+ format_git_output <out >actual &&
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push : bar(A) baz(A) refs/for/next/topic(A) foo(A) refs/for/master/topic(A)
++test_expect_success "report mixed refs update" '
++ git -C workbench push origin \
++ HEAD:refs/heads/bar \
++ HEAD:refs/heads/baz \
++ HEAD:refs/for/next/topic \
++ HEAD:refs/heads/foo \
++ HEAD:refs/for/master/topic \
++ >out 2>&1 &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ remote: # pre-receive hook
-+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/zzz
++ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/bar
++ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/baz
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
-+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/yyy
++ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/foo
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
@@ t/t5411-proc-receive-hook.sh: test_expect_success "push with options" '
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/next/topic ok
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic ok
+ remote: # post-receive hook
++ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/bar
++ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/baz
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
++ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/foo
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
-+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/zzz
-+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/yyy
+ To ../upstream
-+ * [new branch] HEAD -> zzz
-+ * [new reference] HEAD -> refs/for/next/topic
-+ * [new branch] HEAD -> yyy
-+ * [new reference] HEAD -> refs/for/master/topic
++ * [new branch] HEAD -> bar
++ * [new branch] HEAD -> baz
++ * [new reference] HEAD -> refs/for/next/topic
++ * [new branch] HEAD -> foo
++ * [new reference] HEAD -> refs/for/master/topic
+ EOF
+ test_cmp expect actual &&
-+ (
-+ cd upstream &&
-+ git show-ref
-+ ) >out &&
-+ format_git_output <out >actual &&
-+ cat >expect <<-EOF &&
-+ <COMMIT-A> refs/heads/master
-+ <COMMIT-A> refs/heads/yyy
-+ <COMMIT-A> refs/heads/zzz
-+ EOF
-+ test_cmp expect actual
-+'
-+
-+test_expect_success "cleanup" '
-+ (
-+ cd upstream &&
-+ git update-ref -d refs/heads/yyy &&
-+ git update-ref -d refs/heads/zzz
-+ )
-+'
-+
-+test_expect_success "setup proc-receive hook" '
-+ cat >upstream/hooks/proc-receive <<-EOF &&
-+ #!/bin/sh
-+
-+ printf >&2 "# proc-receive hook\n"
-+
-+ test-tool proc-receive -v \
-+ -r "$ZERO_OID $A refs/for/next/topic ok" \
-+ -r "$ZERO_OID $A refs/review/a/b/c/topic ok" \
-+ -r "$ZERO_OID $A refs/for/master/topic ok"
-+ EOF
-+ chmod a+x upstream/hooks/proc-receive
-+'
-+
-+test_expect_success "report mixed refs update (special ref first)" '
-+ (
-+ cd workbench &&
-+ git push origin \
-+ HEAD:refs/for/next/topic \
-+ $B:refs/heads/zzz \
-+ HEAD:refs/review/a/b/c/topic \
-+ HEAD:refs/heads/yyy \
-+ HEAD:refs/for/master/topic
-+ ) >out 2>&1 &&
-+ format_git_output <out >actual &&
-+ cat >expect <<-EOF &&
-+ remote: # pre-receive hook
-+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
-+ remote: pre-receive< <ZERO-OID> <COMMIT-B> refs/heads/zzz
-+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/review/a/b/c/topic
-+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/yyy
-+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
-+ remote: # proc-receive hook
-+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
-+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/review/a/b/c/topic
-+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
-+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/next/topic ok
-+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/review/a/b/c/topic ok
-+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic ok
-+ remote: # post-receive hook
-+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
-+ remote: post-receive< <ZERO-OID> <COMMIT-B> refs/heads/zzz
-+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/yyy
-+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/review/a/b/c/topic
-+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
-+ To ../upstream
-+ * [new reference] HEAD -> refs/for/next/topic
-+ * [new branch] <COMMIT-B> -> zzz
-+ * [new reference] HEAD -> refs/review/a/b/c/topic
-+ * [new branch] HEAD -> yyy
-+ * [new reference] HEAD -> refs/for/master/topic
-+ EOF
-+ test_cmp expect actual &&
-+ (
-+ cd upstream &&
-+ git show-ref
-+ ) >out &&
-+ format_git_output <out >actual &&
++ git -C upstream show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
++ <COMMIT-A> refs/heads/bar
++ <COMMIT-A> refs/heads/baz
++ <COMMIT-A> refs/heads/foo
+ <COMMIT-A> refs/heads/master
-+ <COMMIT-A> refs/heads/yyy
-+ <COMMIT-B> refs/heads/zzz
+ EOF
+ test_cmp expect actual
+'
5: 624538c1b7 < -: ---------- receive-pack: refactor report for proc-receive
-: ---------- > 5: 439acbdb62 receive-pack: refactor report for proc-receive
6: 56543573cf = 6: 24e5cddee0 doc: add documentation for the proc-receive hookJiang Xin (6):
transport: not report a non-head push as a branch
receive-pack: add new proc-receive hook refs.c: refactor to reuse ref_is_hidden()
receive-pack: new config receive.procReceiveRefs
receive-pack: refactor report for proc-receive
doc: add documentation for the proc-receive hook
Documentation/config/receive.txt | 14 + Documentation/githooks.txt | 70 +++ Makefile | 1 + builtin/receive-pack.c | 290 +++++++++- refs.c | 11 +- refs.h | 1 + t/helper/test-proc-receive.c | 172 ++++++ t/helper/test-tool.c | 1 + t/helper/test-tool.h | 1 + t/t5411-proc-receive-hook.sh | 916 +++++++++++++++++++++++++++++++ t/t5516-fetch-push.sh | 2 +- transport-helper.c | 64 +-- transport.c | 62 ++- 13 files changed, 1539 insertions(+), 66 deletions(-) create mode 100644 t/helper/test-proc-receive.c create mode 100755 t/t5411-proc-receive-hook.sh
Signature · 1 line
2.26.0.4.g39bcdcb101.dirty
PATCH v5 2/6 receive-pack: add new proc-receive hook
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Git calls an internal execute_commands function to handle commands sent from client to git-receive-pack. Regardless of what references the user pushes, git creates or updates the corresponding references if the user has write-permission. A contributor who has no write-permission, cannot push to the repository directly. So, the contributor has to write commits to an alternate location, and sends pull request by emails or by other ways. We call this workflow as a distributed workflow.
It would be more convenient to work in a centralized workflow like what Gerrit provided for some cases. For example, a read-only user who cannot push to a branch directly can run the following git push command to push commits to a pseudo reference (has a prefix "refs/for/", not "refs/heads/") to create a code review.
git push origin \
HEAD:refs/for/<branch-name>/<session>The <branch-name> in the above example can be as simple as "master", or a more complicated branch name like "foo/bar". The <session> in the above example command can be the local branch name of the client side, such as "my/topic".
We cannot implement a centralized workflow elegantly by using "pre-receive" + "post-receive", because Git will call the internal function "execute_commands" to create references (even the special pseudo reference) between these two hooks. Even though we can delete the temporarily created pseudo reference via the "post-receive" hook, having a temporary reference is not safe for concurrent pushes.
So, add a filter and a new handler to support this kind of workflow. The filter will check the prefix of the reference name, and if the command has a special reference name, the filter will turn a specific field (run_proc_receive) on for the command. Commands with this filed turned on will be executed by a new handler (an hook named "proc-receive") instead of the internal execute_commands function. We can use this "proc-receive" command to create pull requests or send emails for code review.
Suggested by Junio, this "proc-receive" hook reads the commands, push-options (optional), and send result using a protocol in pkt-line format. In the following example, The letter "S" stands for "receive-pack" and letter "H" stands for the hook.
# Version and capabilities negotiation.
S: PKT-LINE(version=1\0push-options atomic...)
S: flush-pkt
H: PKT-LINE(version=1\0push-options...)
H: flush-pkt
# Send commands from server to the hook.
S: PKT-LINE(old-oid new-oid ref)
S: ... ...
S: flush-pkt
# Only if push-options have been negotiated.
S: PKT-LINE(push-option)
S: ... ...
S: flush-pkt
# Receive result from the hook.
# OK, run this command successfully.
H: PKT-LINE(old-oid new-oid ref ok)
# NO, I reject it.
H: PKT-LINE(old-oid new-oid ref ng reason)
# OK, but use an alternate reference. (in latter commit)
H: PKT-LINE(old-oid new-oid ref ok ref:alt-ref)
# It will fallthrough to receive-pack to execute. (in latter commit)
H: PKT-LINE(old-oid new-oid ref ft)
H: ... ...
H: flush-pkt
After receiving a command, the hook can create/update another alternate reference. For example, a command for a reference "refs/for/master" may create a special reference, such as "refs/pull/123/head". The alternate reference can be returned from the result in an extensible format like "<old-oid> <new-oid> <reference> <status> [<message>]".
The result will be stored in a command list, and "receive-pack" will use the result to replace the commands that have specific run_proc_receive field turned on.
--- Makefile | 1 + builtin/receive-pack.c | 242 ++++++++++++++- t/helper/test-proc-receive.c | 172 +++++++++++ t/helper/test-tool.c | 1 + t/helper/test-tool.h | 1 + t/t5411-proc-receive-hook.sh | 555 +++++++++++++++++++++++++++++++++++ 6 files changed, 969 insertions(+), 3 deletions(-) create mode 100644 t/helper/test-proc-receive.c
Makefile +1 -0
@@ -725,6 +725,7 @@ TEST_BUILTINS_OBJS += test-parse-pathspec-file.o TEST_BUILTINS_OBJS += test-path-utils.o TEST_BUILTINS_OBJS += test-pkt-line.o TEST_BUILTINS_OBJS += test-prio-queue.o +TEST_BUILTINS_OBJS += test-proc-receive.o TEST_BUILTINS_OBJS += test-progress.o TEST_BUILTINS_OBJS += test-reach.o TEST_BUILTINS_OBJS += test-read-cache.o
builtin/receive-pack.c +239 -3
288 lines not on this page · raw message
t/helper/test-proc-receive.c new file +172 -0
172 lines not on this page · raw message
t/helper/test-tool.c +1 -0
@@ -43,6 +43,7 @@ static struct test_cmd cmds[] = { { "path-utils", cmd__path_utils }, { "pkt-line", cmd__pkt_line }, { "prio-queue", cmd__prio_queue }, + { "proc-receive", cmd__proc_receive}, { "progress", cmd__progress }, { "reach", cmd__reach }, { "read-cache", cmd__read_cache },
t/helper/test-tool.h +1 -0
@@ -33,6 +33,7 @@ int cmd__parse_pathspec_file(int argc, const char** argv);int cmd__path_utils (int argc,const char **argv);int cmd__pkt_line (int argc,const char **argv);int cmd__prio_queue (int argc,const char **argv); +int cmd__proc_receive (int argc,const char **argv);int cmd__progress (int argc,const char **argv);int cmd__reach (int argc,const char **argv);int cmd__read_cache (int argc,const char **argv);
t/t5411-proc-receive-hook.sh +555 -0
565 lines not on this page · raw message
Signature · 1 line
2.26.0.4.g39bcdcb101.dirty
Re: [PATCH v5 2/6] receive-pack: add new proc-receive hook
builtin/receive-pack.c static int run_update_hook(struct command *cmd) · line 923
Quoted 3 lines · Jiang Xin
builtin/receive-pack.c +3
-0+ argv[0 ] =find_hook ("proc-receive" ); +if (!argv[0 ]) { +rp_error ("cannot to find hook 'proc-receive'" );
Perhaps discard "to" from here. Also I notice that other messages given to rp_error() are marked for _(i18n), but this one is not.
Quoted 4 lines
t/t5411-proc-receive-hook.sh +4
-0+ make_user_friendly_and_stable_output <out >actual && + cat >expect <<-EOF && + remote: # pre-receive hook + remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next
t/t5411-proc-receive-hook.sh test_expect_success "normal git-push command" ' · line 203
Quoted 10 lines
t/t5411-proc-receive-hook.sh +10
-0+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic + remote: error: cannot to find hook "proc-receive" + remote: # post-receive hook + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/next + To ../upstream + * [new branch] HEAD -> next + ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook) + error: failed to push some refs to "../upstream" + EOF + test_cmp expect actual &&
This breaks gettext-poison test.
See https://travis-ci.org/github/git/git/jobs/669008025 for example.
Re: [PATCH v5 2/6] receive-pack: add new proc-receive hook
In reply to Jiang Xint/helper/test-proc-receive.c line 1
Quoted 7 lines · Jiang Xin
t/helper/test-proc-receive.c new file +1
-0@@ -0,0 +1,172 @@ +static void proc_receive_verison (struct packet_reader *reader) {
Is that a phone company ;-)?
Quoted 17 lines
t/helper/test-proc-receive.c new file +17
-0+int server_version =0 ; + +for (;;) { +... +static void proc_receive_read_commands (struct packet_reader *reader, +struct command **commands) +{ +struct command **tail = commands; + +for (;;) { +struct object_id old_oid, new_oid; +struct command *cmd; +const char *refname; +const char *p; + +if (packet_reader_read (reader) !=PACKET_READ_NORMAL ) { +break ;
t/helper/test-proc-receive.c line 84
Quoted 10 lines
t/helper/test-proc-receive.c new file +10
-0+ } + +if (parse_oid_hex (reader->line, &old_oid, &p) || + *p++ !=' ' || +parse_oid_hex (p, &new_oid, &p) || + *p++ !=' ' ) +die ("protocol error: expected 'old new ref', got '%s'" , + reader->line); + refname = p; +FLEX_ALLOC_MEM (cmd, ref_name, refname,strlen (refname));
FLEX_ALLOC_STR(cmd, ref_name, refname);
Quoted 7 lines
t/helper/test-proc-receive.c new file +7
-0+oidcpy (&cmd->old_oid, &old_oid); +oidcpy (&cmd->new_oid, &new_oid); + + *tail = cmd; + tail = &cmd->next; + } +}
PATCH v5 3/6 refs.c: refactor to reuse ref_is_hidden()
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Add new function ref_is_matched() to reuse ref_is_hidden(). Will use this function for receive-pack to check commands with specific prefixes.
Test case t5512 covered this change.
--- refs.c | 11 ++++++++--- refs.h | 1 + 2 files changed, 9 insertions(+), 3 deletions(-)
refs.c +8 -3
@@ -1389,13 +1389,18 @@ int parse_hide_refs_config(const char *var, const char *value, const char *secti }int ref_is_hidden (const char *refname,const char *refname_full) +{ +return ref_is_matched (hide_refs, refname, refname_full); +} + +int ref_is_matched (struct string_list *match_refs,const char *refname,const char *refname_full) {int i; -if (!hide_refs) +if (!match_refs)return 0 ; -for (i = hide_refs->nr -1 ; i >=0 ; i--) { -const char *match = hide_refs->items[i].string; +for (i = match_refs->nr -1 ; i >=0 ; i--) { +const char *match = match_refs->items[i].string;const char *subject;int neg =0 ;const char *p;
refs.h +1 -0
@@ -739,6 +739,7 @@ int parse_hide_refs_config(const char *var, const char *value, const char *);* parameter always points to the full ref name. */ int ref_is_hidden (const char *,const char *); +int ref_is_matched (struct string_list *,const char *,const char *);enum ref_type {REF_TYPE_PER_WORKTREE ,/* refs inside refs/ but not shared */
Signature · 1 line
2.26.0.4.g39bcdcb101.dirty
PATCH v5 4/6 receive-pack: new config receive.procReceiveRefs
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Add a new multi-valued config variable "receive.procReceiveRefs" for receive-pack command, like the follows:
git config --system --add receive.procReceiveRefs refs/for/
git config --system --add receive.procReceiveRefs refs/drafts/If the specific prefix strings match the reference names of the commands which are sent by git client to receive-pack, these commands will be executed by an external hook (named "proc-receive"), instead of the internal execute_commands function.
For example, if it is set to "refs/for/", pushing to a reference such as "refs/for/master" will not create or update reference "refs/for/master", but may create or update a pull request directly by running the external hook.
--- Documentation/config/receive.txt | 14 ++++ builtin/receive-pack.c | 43 ++++++++-- t/t5411-proc-receive-hook.sh | 130 +++++++++++++++++++++++++++++++ 3 files changed, 180 insertions(+), 7 deletions(-)
Documentation/config/receive.txt +14 -0
@@ -114,6 +114,20 @@ receive.hideRefs:: An attempt to update or delete a hidden ref by `git push` is rejected. +receive.procReceiveRefs:: + This is a multi-valued variable that defines reference prefixes + to match the commands in `receive-pack`. Commands matching the + prefixes will be executed by an external hooks "proc-receive", + instead of the internal `execute_commands` function. If this + variable is not defined, the "proc-receive" hook will never be + used, and all commands will be executed by the internal + `execute_commands` function. + + For example, if this variable is set to "refs/for/", pushing to + reference such as "refs/for/master" will not create or update a + reference named "refs/for/master", but may create or update a + pull request directly by running an external hook. + receive.updateServerInfo:: If set to true, git-receive-pack will run git-update-server-info after receiving data from git-push and updating refs.
builtin/receive-pack.c +36 -7
74 lines not on this page · raw message
t/t5411-proc-receive-hook.sh +130 -0
140 lines not on this page · raw message
Signature · 1 line
2.26.0.4.g39bcdcb101.dirty
PATCH v5 5/6 receive-pack: refactor report for proc-receive
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
The "proc-receive" may update one or more references, and will send its result one by one in pkt-line format. Each line of the result has four fields and one optional message field, as "<old-oid> <new-oid> <ref> <status> [<message>]". See the following example:
# OK, run this command successfully.
PKT-LINE(old-oid new-oid ref ok)
# NO, I reject it.
PKT-LINE(old-oid new-oid ref ng reason)
# OK, but use an alternate reference.
PKT-LINE(old-oid new-oid ref ok ref:alt-ref)
# It will fallthrough to receive-pack to execute. PKT-LINE(old-oid new-oid ref ft)
The first three fields have the same foramt as a command.
The forth field has a two-letter status code. Available status code:
* ok: The command runs successfully. If the optional message has a prefix "ref:", the hook has created/updated an alternate reference instead.
* ng: Fail to run the command. Error message is in the optional message field.
* ft: Will fallthrough to receive-pack to execute.
--- builtin/receive-pack.c | 21 +++++++-- t/t5411-proc-receive-hook.sh | 90 +++++++++++++++++++++++++++++++++--- transport-helper.c | 64 ++++++++++++------------- transport.c | 59 +++++++++++++---------- 4 files changed, 168 insertions(+), 66 deletions(-)
builtin/receive-pack.c +16 -5
@@ -328,6 +328,7 @@ static void write_head_info(void)struct command {struct command *next;const char *error_string; +const char *extra_string;unsigned int skip_update:1 , did_not_exist:1 , run_proc_receive:2 ;
@@ -906,7 +907,12 @@ static int read_proc_receive_result(struct packet_reader *reader,else hint->error_string ="failed" ; code =1 ; - }else if (strcmp ("ok" , status)) { + }else if (!strcmp ("ok" , status)) { + hint->extra_string =xstrdup_or_null (msg); + }else if (!strcmp ("ft" , status)) { +/* Reset "run_proc_receive" field, and continue to run in "receive-pack" */ + hint->run_proc_receive =0 ; + }else {die ("protocol error: proc-receive has bad status '%s' for '%s'" , status, reader->line); }
@@ -2161,12 +2167,17 @@ static void report(struct command *commands, const char *unpack_status)packet_buf_write (&buf,"unpack %s\n" , unpack_status ? unpack_status :"ok" );for (cmd = commands; cmd; cmd = cmd->next) { -if (!cmd->error_string) -packet_buf_write (&buf,"ok %s\n" , - cmd->ref_name); -else +if (!cmd->error_string) { +if (!cmd->extra_string) +packet_buf_write (&buf,"ok %s\n" , + cmd->ref_name); +else +packet_buf_write (&buf,"ok %s%c%s\n" , + cmd->ref_name,' ' , cmd->extra_string); + }else {packet_buf_write (&buf,"ng %s %s\n" , cmd->ref_name, cmd->error_string); + } }packet_buf_flush (&buf);
t/t5411-proc-receive-hook.sh +84 -6
140 lines not on this page · raw message
transport-helper.c +33 -31
70 lines not on this page · raw message
transport.c +35 -24
140 lines not on this page · raw message
Signature · 1 line
2.26.0.4.g39bcdcb101.dirty
PATCH v5 6/6 doc: add documentation for the proc-receive hook
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Add documentation for the new "proc-receive" hook.
--- Documentation/githooks.txt | 70 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+)
Documentation/githooks.txt +70 -0
@@ -333,6 +333,76 @@ The default 'update' hook, when enabled--and with `hooks.allowunannotated` config option unset or set to false--prevents unannotated tags to be pushed. +[[proc-receive]] +proc-receive +~~~~~~~~~~~~ +This hook is invoked by linkgit:git-receive-pack[1] when it reacts to +special `git push` command. According to refnames of the commands which +`git push` sends to 'git-receive-pack', the commands will be devided +into two groups by matching what the `receive.procReceiveRefs` +configuration variable defines. One group of the commands will execute +the internal `execute_commands` function to update the corresponding +refnames, and the other group of commands which have matching refnames +will execute this 'proc-receive' hook to create pull requests, etc. +If there is no `receive.procReceiveRefs` settings, this hook won't +execute at all, and all commands are sent to the internal +`execute_commands` function. + +Its exit status only determines the success or failure of the group of +commands with special refnames, unless atomic push is in use. + +This hook executes once for the receive operation. It takes no +arguments, but will talk a protocol in pkt-line format with the +'receive-pack' for reading commands, push-options (optional), and +sending result. In the following example, The letter "S" stands for +"receive-pack" and letter "H" stands for the hook. + + # Version and capabilities negotiation. + S: PKT-LINE(version=1\0push-options atomic...) + S: flush-pkt + H: PKT-LINE(version=1\0push-options...) + H: flush-pkt + + # Send commands from server to the hook. + S: PKT-LINE(old-oid new-oid ref) + S: ... ... + S: flush-pkt + # Only if push-options have been negotiated. + S: PKT-LINE(push-option) + S: ... ... + S: flush-pkt + + # Receive result from the hook. + # OK, run this command successfully. + H: PKT-LINE(old-oid new-oid ref ok) + # NO, I reject it. + H: PKT-LINE(old-oid new-oid ref ng reason) + # OK, but use an alternate reference. + H: PKT-LINE(old-oid new-oid ref ok ref:alt-ref) + # It will fallthrough to receive-pack to execute. + H: PKT-LINE(old-oid new-oid ref ft) + H: ... ... + H: flush-pkt + +The "proc-receive" hook may update one or more references, and will send +its result one by one in pkt-line format. Each line of the result has +four fields and one optional message field, like "<old-oid> <new-oid> +<ref> <status> [<message>]". + +The first three fields are the same as those of the commands for +"receive-pack". + +The forth field has a two-letter status code. Available status codes: + +* ok: The command runs successfully. If the optional message has a + prefix "ref:", the hook has created/updated an alternate reference + instead. + +* ng: Fail to run the command. Error message is given in the optional + message field. + +* ft: Will fallthrough to receive-pack to execute. + [[post-receive]] post-receive ~~~~~~~~~~~~
Signature · 1 line
2.26.0.4.g39bcdcb101.dirty
PATCH v6 1/7 transport: not report a non-head push as a branch
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
When pushing a new reference (not a head or tag), report it as a new reference instead of a new branch.
--- t/t5411-proc-receive-hook.sh | 153 +++++++++++++++++++++++++++++++++++ t/t5516-fetch-push.sh | 2 +- transport.c | 9 ++- 3 files changed, 160 insertions(+), 4 deletions(-) create mode 100755 t/t5411-proc-receive-hook.sh
t/t5411-proc-receive-hook.sh new file +153 -0
@@ -0,0 +1,153 @@ +#!/bin/sh +# +# Copyright (c) 2020 Jiang Xin +# + +test_description='Test proc-receive hook' + +. ./test-lib.sh + +# Create commits in <repo> and assign each commit's oid to shell variables +# given in the arguments (A, B, and C). E.g.: +# +# create_commits_in <repo> A B C +# +# NOTE: Never calling this function from a subshell since variable +# assignments will disappear when subshell exits. +create_commits_in () { + repo="$1" && + if ! parent=$(git -C "$repo" rev-parse HEAD^{}) + then + parent= + fi && + T=$(git -C "$repo" write-tree) && + shift && + while test $# -gt 0 + do + name=$1 && + test_tick && + if test -z "$parent" + then + oid=$(echo $name | git -C "$repo" commit-tree $T) + else + oid=$(echo $name | git -C "$repo" commit-tree -p $parent $T) + fi && + eval $name=$oid && + parent=$oid && + shift || + return 1 + done && + git -C "$repo" update-ref refs/heads/master $oid +} + +# Format the output of git-push, git-show-ref and other commands to make a +# user-friendly and stable text. We can easily prepare the expect text +# without having to worry about future changes of the commit ID and spaces +# of the output. We also replce single quotes with double quotes, because +# it is boring to prepare unquoted single quotes in expect txt. +make_user_friendly_and_stable_output () { + sed \ + -e "s/ *\$//" \ + -e "s/ */ /g" \ + -e "s/'/\"/g" \ + -e "s/$A/<COMMIT-A>/g" \ + -e "s/$B/<COMMIT-B>/g" \ + -e "s/$TAG/<TAG-v123>/g" \ + -e "s/$ZERO_OID/<ZERO-OID>/g" \ + -e "s/[0-9a-f]\{7,\}/<OID>/g" +} + +# Refs of upstream : master(B) next(A) +# Refs of workbench: master(A) tags/v123 +test_expect_success "setup" ' + git init --bare upstream && + git init workbench && + create_commits_in workbench A B && + ( + cd workbench && + # Try to make a stable fixed width for abbreviated commit ID, + # this fixed-width oid will be replaced with "<OID>". + git config core.abbrev 7 && + git remote add origin ../upstream && + git update-ref refs/heads/master $A && + git tag -m "v123" v123 $A && + git push origin \ + $B:refs/heads/master \ + $A:refs/heads/next + ) && + TAG=$(git -C workbench rev-parse v123) && + + # setup pre-receive hook + cat >upstream/hooks/pre-receive <<-\EOF && + #!/bin/sh + + echo >&2 "# pre-receive hook" + + while read old new ref + do + echo >&2 "pre-receive< $old $new $ref" + done + EOF + + # setup post-receive hook + cat >upstream/hooks/post-receive <<-\EOF && + #!/bin/sh + + echo >&2 "# post-receive hook" + + while read old new ref + do + echo >&2 "post-receive< $old $new $ref" + done + EOF + + chmod a+x \ + upstream/hooks/pre-receive \ + upstream/hooks/post-receive +' + +# Refs of upstream : master(B) next(A) +# Refs of workbench: master(A) tags/v123 +# git-push -f : master(A) NULL tags/v123 refs/review/master/topic(A) a/b/c(A) +test_expect_success "normal git-push command" ' + git -C workbench push -f origin \ + refs/tags/v123 \ + :refs/heads/next \ + HEAD:refs/heads/master \ + HEAD:refs/review/master/topic \ + HEAD:refs/heads/a/b/c \ + >out 2>&1 && + make_user_friendly_and_stable_output <out >actual && + cat >expect <<-EOF && + remote: # pre-receive hook + remote: pre-receive< <COMMIT-B> <COMMIT-A> refs/heads/master + remote: pre-receive< <COMMIT-A> <ZERO-OID> refs/heads/next + remote: pre-receive< <ZERO-OID> <TAG-v123> refs/tags/v123 + remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/review/master/topic + remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/a/b/c + remote: # post-receive hook + remote: post-receive< <COMMIT-B> <COMMIT-A> refs/heads/master + remote: post-receive< <COMMIT-A> <ZERO-OID> refs/heads/next + remote: post-receive< <ZERO-OID> <TAG-v123> refs/tags/v123 + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/review/master/topic + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/a/b/c + To ../upstream + + <OID>...<OID> HEAD -> master (forced update) + - [deleted] next + * [new tag] v123 -> v123 + * [new reference] HEAD -> refs/review/master/topic + * [new branch] HEAD -> a/b/c + EOF + test_cmp expect actual && + git -C upstream show-ref >out && + make_user_friendly_and_stable_output <out >actual && + cat >expect <<-EOF && + <COMMIT-A> refs/heads/a/b/c + <COMMIT-A> refs/heads/master + <COMMIT-A> refs/review/master/topic + <TAG-v123> refs/tags/v123 + EOF + test_cmp expect actual +' + +test_done
t/t5516-fetch-push.sh +1 -1
@@ -1039,7 +1039,7 @@ test_force_fetch_tag "annotated tag" "-f -a -m'tag message'" test_expect_success 'push --porcelain' ' mk_empty testrepo && echo >.git/foo "To testrepo" && - echo >>.git/foo "* refs/heads/master:refs/remotes/origin/master [new branch]" && + echo >>.git/foo "* refs/heads/master:refs/remotes/origin/master [new reference]" && echo >>.git/foo "Done" && git push >.git/bar --porcelain testrepo refs/heads/master:refs/remotes/origin/master && (
transport.c +6 -3
@@ -500,9 +500,12 @@ static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widt porcelain, summary_width); else if (is_null_oid(&ref->old_oid))print_ref_status ('*' , - (starts_with (ref->name,"refs/tags/" ) ?"[new tag]" : -"[new branch]" ), - ref, ref->peer_ref,NULL , porcelain, summary_width); + (starts_with (ref->name,"refs/tags/" ) + ?"[new tag]" + : (starts_with (ref->name,"refs/heads/" ) + ?"[new branch]" + :"[new reference]" )), + ref, ref->peer_ref,NULL , porcelain, summary_width); else {struct strbuf quickref =STRBUF_INIT ;char type;
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v6 2/7 receive-pack: add new proc-receive hook
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Git calls an internal execute_commands function to handle commands sent from client to git-receive-pack. Regardless of what references the user pushes, git creates or updates the corresponding references if the user has write-permission. A contributor who has no write-permission, cannot push to the repository directly. So, the contributor has to write commits to an alternate location, and sends pull request by emails or by other ways. We call this workflow as a distributed workflow.
It would be more convenient to work in a centralized workflow like what Gerrit provided for some cases. For example, a read-only user who cannot push to a branch directly can run the following git push command to push commits to a pseudo reference (has a prefix "refs/for/", not "refs/heads/") to create a code review.
git push origin \
HEAD:refs/for/<branch-name>/<session>The <branch-name> in the above example can be as simple as "master", or a more complicated branch name like "foo/bar". The <session> in the above example command can be the local branch name of the client side, such as "my/topic".
We cannot implement a centralized workflow elegantly by using "pre-receive" + "post-receive", because Git will call the internal function "execute_commands" to create references (even the special pseudo reference) between these two hooks. Even though we can delete the temporarily created pseudo reference via the "post-receive" hook, having a temporary reference is not safe for concurrent pushes.
So, add a filter and a new handler to support this kind of workflow. The filter will check the prefix of the reference name, and if the command has a special reference name, the filter will turn a specific field (run_proc_receive) on for the command. Commands with this filed turned on will be executed by a new handler (an hook named "proc-receive") instead of the internal execute_commands function. We can use this "proc-receive" command to create pull requests or send emails for code review.
Suggested by Junio, this "proc-receive" hook reads the commands, push-options (optional), and send result using a protocol in pkt-line format. In the following example, The letter "S" stands for "receive-pack" and letter "H" stands for the hook.
# Version and capabilities negotiation.
S: PKT-LINE(version=1\0push-options atomic...)
S: flush-pkt
H: PKT-LINE(version=1\0push-options...)
H: flush-pkt
# Send commands from server to the hook.
S: PKT-LINE(old-oid new-oid ref)
S: ... ...
S: flush-pkt
# Only if push-options have been negotiated.
S: PKT-LINE(push-option)
S: ... ...
S: flush-pkt
# Receive result from the hook.
# OK, run this command successfully.
H: PKT-LINE(old-oid new-oid ref ok)
# NO, I reject it.
H: PKT-LINE(old-oid new-oid ref ng reason)
# OK, but use an alternate reference. (in latter commit)
H: PKT-LINE(old-oid new-oid ref ok ref:alt-ref)
# It will fallthrough to receive-pack to execute. (in latter commit)
H: PKT-LINE(old-oid new-oid ref ft)
H: ... ...
H: flush-pkt
After receiving a command, the hook can create/update another alternate reference. For example, a command for a reference "refs/for/master" may create a special reference, such as "refs/pull/123/head". The alternate reference can be returned from the result in an extensible format like "<old-oid> <new-oid> <reference> <status> [<message>]".
The result will be stored in a command list, and "receive-pack" will use the result to replace the commands that have specific run_proc_receive field turned on.
--- Makefile | 1 + builtin/receive-pack.c | 252 ++++++++++++++- t/helper/test-proc-receive.c | 172 ++++++++++ t/helper/test-tool.c | 1 + t/helper/test-tool.h | 1 + t/t5411-proc-receive-hook.sh | 611 ++++++++++++++++++++++++++++++++++- 6 files changed, 1024 insertions(+), 14 deletions(-) create mode 100644 t/helper/test-proc-receive.c
Makefile +1 -0
@@ -725,6 +725,7 @@ TEST_BUILTINS_OBJS += test-parse-pathspec-file.o TEST_BUILTINS_OBJS += test-path-utils.o TEST_BUILTINS_OBJS += test-pkt-line.o TEST_BUILTINS_OBJS += test-prio-queue.o +TEST_BUILTINS_OBJS += test-proc-receive.o TEST_BUILTINS_OBJS += test-progress.o TEST_BUILTINS_OBJS += test-reach.o TEST_BUILTINS_OBJS += test-read-cache.o
builtin/receive-pack.c +249 -3
298 lines not on this page · raw message
t/helper/test-proc-receive.c new file +172 -0
172 lines not on this page · raw message
t/helper/test-tool.c +1 -0
@@ -44,6 +44,7 @@ static struct test_cmd cmds[] = { { "path-utils", cmd__path_utils }, { "pkt-line", cmd__pkt_line }, { "prio-queue", cmd__prio_queue }, + { "proc-receive", cmd__proc_receive}, { "progress", cmd__progress }, { "reach", cmd__reach }, { "read-cache", cmd__read_cache },
t/helper/test-tool.h +1 -0
@@ -34,6 +34,7 @@ int cmd__parse_pathspec_file(int argc, const char** argv);int cmd__path_utils (int argc,const char **argv);int cmd__pkt_line (int argc,const char **argv);int cmd__prio_queue (int argc,const char **argv); +int cmd__proc_receive (int argc,const char **argv);int cmd__progress (int argc,const char **argv);int cmd__reach (int argc,const char **argv);int cmd__read_cache (int argc,const char **argv);
t/t5411-proc-receive-hook.sh +600 -11
668 lines not on this page · raw message
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v6 3/7 refs.c: refactor to reuse ref_is_hidden()
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Add new function ref_is_matched() to reuse ref_is_hidden(). Will use this function for receive-pack to check commands with specific prefixes.
Test case t5512 covered this change.
--- refs.c | 11 ++++++++--- refs.h | 1 + 2 files changed, 9 insertions(+), 3 deletions(-)
refs.c +8 -3
@@ -1389,13 +1389,18 @@ int parse_hide_refs_config(const char *var, const char *value, const char *secti }int ref_is_hidden (const char *refname,const char *refname_full) +{ +return ref_is_matched (hide_refs, refname, refname_full); +} + +int ref_is_matched (struct string_list *match_refs,const char *refname,const char *refname_full) {int i; -if (!hide_refs) +if (!match_refs)return 0 ; -for (i = hide_refs->nr -1 ; i >=0 ; i--) { -const char *match = hide_refs->items[i].string; +for (i = match_refs->nr -1 ; i >=0 ; i--) { +const char *match = match_refs->items[i].string;const char *subject;int neg =0 ;const char *p;
refs.h +1 -0
@@ -739,6 +739,7 @@ int parse_hide_refs_config(const char *var, const char *value, const char *);* parameter always points to the full ref name. */ int ref_is_hidden (const char *,const char *); +int ref_is_matched (struct string_list *,const char *,const char *);enum ref_type {REF_TYPE_PER_WORKTREE ,/* refs inside refs/ but not shared */
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v6 4/7 receive-pack: new config receive.procReceiveRefs
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Add a new multi-valued config variable "receive.procReceiveRefs" for receive-pack command, like the follows:
git config --system --add receive.procReceiveRefs refs/for/
git config --system --add receive.procReceiveRefs refs/drafts/If the specific prefix strings match the reference names of the commands which are sent by git client to receive-pack, these commands will be executed by an external hook (named "proc-receive"), instead of the internal execute_commands function.
For example, if it is set to "refs/for/", pushing to a reference such as "refs/for/master" will not create or update reference "refs/for/master", but may create or update a pull request directly by running the external hook.
--- Documentation/config/receive.txt | 14 ++++ builtin/receive-pack.c | 43 ++++++++-- t/t5411-proc-receive-hook.sh | 130 +++++++++++++++++++++++++++++++ 3 files changed, 180 insertions(+), 7 deletions(-)
Documentation/config/receive.txt +14 -0
@@ -114,6 +114,20 @@ receive.hideRefs:: An attempt to update or delete a hidden ref by `git push` is rejected. +receive.procReceiveRefs:: + This is a multi-valued variable that defines reference prefixes + to match the commands in `receive-pack`. Commands matching the + prefixes will be executed by an external hooks "proc-receive", + instead of the internal `execute_commands` function. If this + variable is not defined, the "proc-receive" hook will never be + used, and all commands will be executed by the internal + `execute_commands` function. + + For example, if this variable is set to "refs/for/", pushing to + reference such as "refs/for/master" will not create or update a + reference named "refs/for/master", but may create or update a + pull request directly by running an external hook. + receive.updateServerInfo:: If set to true, git-receive-pack will run git-update-server-info after receiving data from git-push and updating refs.
builtin/receive-pack.c +36 -7
74 lines not on this page · raw message
t/t5411-proc-receive-hook.sh +130 -0
140 lines not on this page · raw message
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
v6 cover New proc-receive hook for centralized workflow
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
## Changes since v5
+ Fixed code review issues on v5.
* s/cannot to/cannot/
* s/FLEX_ALLOC_MEM/FLEX_ALLOC_STR/
* Add flag C_LOCALE_OUTPUT on test cases which may break gettext-poison test.
* Note: rp_errors() in "receive-pack" is executed on the server side, I think leave them unmarked for i18n is better.
+ Add t5412 for "proc-receive" hook test on HTTP protocol.
+ Some test cases of t5412 will hang because of "receive-pack" die earlier. Replace die() with rp_error().
## Range-diff v5...v6
1: a8bcc20439 = 1: 1647024dfe transport: not report a non-head push as a branch
2: 14641ec57e ! 2: ed8d2c0e82 receive-pack: add new proc-receive hook
@@ builtin/receive-pack.c: static void write_head_info(void)
packet_flush(1);
}
-+#define RUN_PROC_RECEIVE_SCHEDULE 1
++#define RUN_PROC_RECEIVE_SCHEDULED 1
+#define RUN_PROC_RECEIVE_RETURNED 2
struct command {
struct command *next;
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ if (parse_oid_hex(reader->line, &old_oid, &p) ||
+ *p++ != ' ' ||
+ parse_oid_hex(p, &new_oid, &p) ||
-+ *p++ != ' ')
-+ die("protocol error: proc-receive expected 'old new ref status [msg]', got '%s'",
-+ reader->line);
++ *p++ != ' ') {
++ rp_error("protocol error: proc-receive expected 'old new ref status [msg]', got '%s'",
++ reader->line);
++ return -1;
++ }
+
+ refname = p;
+ status = strchr(p, ' ');
-+ if (!status)
-+ die("protocol error: proc-receive expected 'old new ref status [msg]', got '%s'",
-+ reader->line);
++ if (!status) {
++ rp_error("protocol error: proc-receive expected 'old new ref status [msg]', got '%s'",
++ reader->line);
++ return -1;
++ }
+ *status++ = '\0';
+ if (strlen(status) > 2 && *(status + 2) == ' ') {
+ msg = status + 2;
+ *msg++ = '\0';
+ }
-+ if (strlen(status) != 2)
-+ die("protocol error: proc-receive has bad status '%s' for '%s'",
-+ status, reader->line);
++ if (strlen(status) != 2) {
++ rp_error("protocol error: proc-receive has bad status '%s' for '%s'",
++ status, reader->line);
++ return -1;
++ }
+
+ /* first try searching at our hint, falling back to all refs */
+ if (hint)
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ hint->error_string = "failed";
+ code = 1;
+ } else if (strcmp("ok", status)) {
-+ die("protocol error: proc-receive has bad status '%s' for '%s'",
-+ status, reader->line);
++ rp_error("protocol error: proc-receive has bad status '%s' for '%s'",
++ status, reader->line);
++ return -1;
+ }
+ }
+
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+
+ argv[0] = find_hook("proc-receive");
+ if (!argv[0]) {
-+ rp_error("cannot to find hook 'proc-receive'");
-+ return 1;
++ rp_error("cannot find hook 'proc-receive'");
++ return -1;
+ }
+ argv[1] = NULL;
+
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ }
+ }
+
-+ if (version != 1)
-+ die("protocol error: unknown proc-receive version '%d'", version);
++ if (version != 1) {
++ rp_error("protocol error: unknown proc-receive version '%d'",
++ version);
++ code = -1;
++ goto cleanup;
++ }
+
+ /* Send commands */
+ for (cmd = commands; cmd; cmd = cmd->next) {
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+
+ if (!cmd->run_proc_receive || cmd->skip_update || cmd->error_string)
+ continue;
-+
+ old_hex = oid_to_hex(&cmd->old_oid);
+ new_hex = oid_to_hex(&cmd->new_oid);
-+
+ packet_write_fmt(proc.in, "%s %s %s",
+ old_hex, new_hex, cmd->ref_name);
+ }
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+
+ for_each_string_list_item(item, push_options)
+ packet_write_fmt(proc.in, "%s", item->string);
-+
+ packet_flush(proc.in);
+ }
+
+ /* Read result from proc-receive */
+ code = read_proc_receive_result(&reader, commands);
++
++cleanup:
+ close(proc.in);
+ close(proc.out);
+ if (use_sideband)
+ finish_async(&muxer);
-+ if (finish_command(&proc))
-+ die("proc-receive did not exit properly");
-+
++ if (finish_command(&proc)) {
++ rp_error("proc-receive did not exit properly");
++ code = -1;
++ }
+ sigchain_pop(SIGPIPE);
-+
+ return code;
+}
+
@@ builtin/receive-pack.c: static void execute_commands(struct command *commands,
+
+ /* TODO: replace the fixed prefix by looking up git config variables. */
+ if (!strncmp(cmd->ref_name, "refs/for/", 9)) {
-+ cmd->run_proc_receive = 1;
++ cmd->run_proc_receive = RUN_PROC_RECEIVE_SCHEDULED;
+ run_proc_receive = 1;
+ }
+ }
@@ t/helper/test-proc-receive.c (new)
+ die("protocol error: expected 'old new ref', got '%s'",
+ reader->line);
+ refname = p;
-+ FLEX_ALLOC_MEM(cmd, ref_name, refname, strlen(refname));
++ FLEX_ALLOC_STR(cmd, ref_name, refname);
+ oidcpy(&cmd->old_oid, &old_oid);
+ oidcpy(&cmd->new_oid, &new_oid);
+
@@ t/helper/test-tool.h: int cmd__parse_pathspec_file(int argc, const char** argv);
int cmd__read_cache(int argc, const char **argv);
## t/t5411-proc-receive-hook.sh ##
+@@ t/t5411-proc-receive-hook.sh: create_commits_in () {
+ # Format the output of git-push, git-show-ref and other commands to make a
+ # user-friendly and stable text. We can easily prepare the expect text
+ # without having to worry about future changes of the commit ID and spaces
+-# of the output. We also replce single quotes with double quotes, because
+-# it is boring to prepare unquoted single quotes in expect txt.
++# of the output. Single quotes are replaced with double quotes, because
++# it is boring to prepare unquoted single quotes in expect txt. We also
++# remove some locale error messages, which break test if we turn on
++# `GIT_TEST_GETTEXT_POISON=true` in order to test unintentional translations
++# on plumbing commands.
+ make_user_friendly_and_stable_output () {
+ sed \
+ -e "s/ *\$//" \
@@ t/t5411-proc-receive-hook.sh: make_user_friendly_and_stable_output () {
- -e "s/[0-9a-f]\{7,\}/<OID>/g"
- }
-
+ -e "s/$B/<COMMIT-B>/g" \
+ -e "s/$TAG/<TAG-v123>/g" \
+ -e "s/$ZERO_OID/<ZERO-OID>/g" \
+- -e "s/[0-9a-f]\{7,\}/<OID>/g"
++ -e "s/[0-9a-f]\{7,\}/<OID>/g" \
++ -e "s#To ../upstream.git#To <URL/of/upstream.git>#" \
++ -e "/^error: / d"
++}
++
+# Asynchronous sideband may generate inconsistent output messages,
+# sort before comparison.
+test_sorted_cmp () {
@@ t/t5411-proc-receive-hook.sh: make_user_friendly_and_stable_output () {
+ $GIT_TEST_CMP "$@"
+ fi
+ fi
-+}
-+
+ }
+
# Refs of upstream : master(B) next(A)
# Refs of workbench: master(A) tags/v123
test_expect_success "setup" '
+- git init --bare upstream &&
++ upstream=upstream.git &&
++ git init --bare "$upstream" &&
+ git init workbench &&
+ create_commits_in workbench A B &&
+ (
+@@ t/t5411-proc-receive-hook.sh: test_expect_success "setup" '
+ # Try to make a stable fixed width for abbreviated commit ID,
+ # this fixed-width oid will be replaced with "<OID>".
+ git config core.abbrev 7 &&
+- git remote add origin ../upstream &&
++ git remote add origin ../$upstream &&
+ git update-ref refs/heads/master $A &&
+ git tag -m "v123" v123 $A &&
+ git push origin \
+@@ t/t5411-proc-receive-hook.sh: test_expect_success "setup" '
+ TAG=$(git -C workbench rev-parse v123) &&
+
+ # setup pre-receive hook
+- cat >upstream/hooks/pre-receive <<-\EOF &&
++ cat >"$upstream/hooks/pre-receive" <<-\EOF &&
+ #!/bin/sh
+
+ echo >&2 "# pre-receive hook"
+@@ t/t5411-proc-receive-hook.sh: test_expect_success "setup" '
+ EOF
+
+ # setup post-receive hook
+- cat >upstream/hooks/post-receive <<-\EOF &&
++ cat >"$upstream/hooks/post-receive" <<-\EOF &&
+ #!/bin/sh
+
+ echo >&2 "# post-receive hook"
+@@ t/t5411-proc-receive-hook.sh: test_expect_success "setup" '
+ EOF
+
+ chmod a+x \
+- upstream/hooks/pre-receive \
+- upstream/hooks/post-receive
++ "$upstream/hooks/pre-receive" \
++ "$upstream/hooks/post-receive"
+ '
+
+ # Refs of upstream : master(B) next(A)
+@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ remote: post-receive< <ZERO-OID> <TAG-v123> refs/tags/v123
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/review/master/topic
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/a/b/c
+- To ../upstream
++ To <URL/of/upstream.git>
+ + <OID>...<OID> HEAD -> master (forced update)
+ - [deleted] next
+ * [new tag] v123 -> v123
+@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ * [new branch] HEAD -> a/b/c
+ EOF
+ test_cmp expect actual &&
+- git -C upstream show-ref >out &&
++ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/a/b/c
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
test_cmp expect actual
'
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+# Refs of workbench: master(A) tags/v123
+test_expect_success "cleanup" '
+ (
-+ cd upstream &&
++ cd "$upstream" &&
+ git update-ref -d refs/review/master/topic &&
+ git update-ref -d refs/tags/v123 &&
+ git update-ref -d refs/heads/a/b/c
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ remote: # pre-receive hook
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
-+ remote: error: cannot to find hook "proc-receive"
++ remote: error: cannot find hook "proc-receive"
+ remote: # post-receive hook
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/next
-+ To ../upstream
++ To <URL/of/upstream.git>
+ * [new branch] HEAD -> next
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
-+ error: failed to push some refs to "../upstream"
+ EOF
-+ test_cmp expect actual &&
-+ git -C upstream show-ref >out &&
++ test_sorted_cmp expect actual &&
++ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+# Refs of upstream : master(A) next(A)
+# Refs of workbench: master(A) tags/v123
+test_expect_success "cleanup" '
-+ git -C upstream update-ref -d refs/heads/next
++ git -C "$upstream" update-ref -d refs/heads/next
+'
+
+# Refs of upstream : master(A)
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ remote: # pre-receive hook
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
-+ remote: error: cannot to find hook "proc-receive"
-+ To ../upstream
++ remote: error: cannot find hook "proc-receive"
++ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> next (fail to run proc-receive hook)
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
-+ error: failed to push some refs to "../upstream"
+ EOF
-+ test_cmp expect actual &&
-+ git -C upstream show-ref >out &&
++ test_sorted_cmp expect actual &&
++ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+'
+
+test_expect_success "setup proc-receive hook (bad version)" '
-+ cat >upstream/hooks/proc-receive <<-EOF &&
++ cat >"$upstream/hooks/proc-receive" <<-EOF &&
+ #!/bin/sh
+
+ printf >&2 "# proc-receive hook\n"
+
+ test-tool proc-receive -v --version 2
+ EOF
-+ chmod a+x upstream/hooks/proc-receive
++ chmod a+x "$upstream/hooks/proc-receive"
+'
+
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push : refs/for/master/topic(A)
-+test_expect_success "proc-receive bad protocol: unknown version" '
++test_expect_success C_LOCALE_OUTPUT "proc-receive bad protocol: unknown version" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/master/topic \
+ >out 2>&1 &&
-+ make_user_friendly_and_stable_output <out | grep "protocol error" >actual &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
-+ fatal: protocol error: unknown proc-receive version "2"
++ remote: # pre-receive hook
++ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: # proc-receive hook
++ remote: error: protocol error: unknown proc-receive version "2"
++ remote: fatal: the remote end hung up unexpectedly
++ remote: error: proc-receive did not exit properly
++ To <URL/of/upstream.git>
++ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
-+ test_cmp expect actual &&
-+ git -C upstream show-ref >out &&
++ test_sorted_cmp expect actual &&
++ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+'
+
+test_expect_success "setup proc-receive hook (no report)" '
-+ cat >upstream/hooks/proc-receive <<-EOF
++ cat >"$upstream/hooks/proc-receive" <<-EOF
+ #!/bin/sh
+
+ printf >&2 "# proc-receive hook\n"
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: # post-receive hook
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/next
-+ To ../upstream
++ To <URL/of/upstream.git>
+ * [new branch] HEAD -> next
+ ! [remote rejected] HEAD -> refs/for/master/topic (no report from proc-receive)
-+ error: failed to push some refs to "../upstream"
+ EOF
+ test_cmp expect actual &&
-+ git -C upstream show-ref >out &&
++ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+# Refs of upstream : master(A) next(A)
+# Refs of workbench: master(A) tags/v123
+test_expect_success "cleanup" '
-+ git -C upstream update-ref -d refs/heads/next
++ git -C "$upstream" update-ref -d refs/heads/next
+
+'
+
+test_expect_success "setup proc-receive hook (bad oid)" '
-+ cat >upstream/hooks/proc-receive <<-EOF
++ cat >"$upstream/hooks/proc-receive" <<-EOF
+ #!/bin/sh
+
+ printf >&2 "# proc-receive hook\n"
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/master/topic\
+ >out 2>&1 &&
-+ make_user_friendly_and_stable_output <out | grep "protocol error" >actual &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
-+ fatal: protocol error: proc-receive expected "old new ref status [msg]", got "bad-id new-id ref ok"
++ remote: # pre-receive hook
++ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: # proc-receive hook
++ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: proc-receive> bad-id new-id ref ok
++ remote: error: protocol error: proc-receive expected "old new ref status [msg]", got "bad-id new-id ref ok"
++ To <URL/of/upstream.git>
++ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
-+ test_cmp expect actual &&
-+ git -C upstream show-ref >out &&
++ test_sorted_cmp expect actual &&
++ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+'
+
+test_expect_success "setup proc-receive hook (no status)" '
-+ cat >upstream/hooks/proc-receive <<-EOF
++ cat >"$upstream/hooks/proc-receive" <<-EOF
+ #!/bin/sh
+
+ printf >&2 "# proc-receive hook\n"
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/master/topic \
+ >out 2>&1 &&
-+ make_user_friendly_and_stable_output <out | grep "protocol error" >actual &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
-+ fatal: protocol error: proc-receive expected "old new ref status [msg]", got "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
++ remote: # pre-receive hook
++ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: # proc-receive hook
++ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: error: protocol error: proc-receive expected "old new ref status [msg]", got "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
++ To <URL/of/upstream.git>
++ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
-+ test_cmp expect actual &&
-+ git -C upstream show-ref >out &&
++ test_sorted_cmp expect actual &&
++ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+'
+
+test_expect_success "setup proc-receive hook (unknown status)" '
-+ cat >upstream/hooks/proc-receive <<-EOF
++ cat >"$upstream/hooks/proc-receive" <<-EOF
+ #!/bin/sh
+
+ printf >&2 "# proc-receive hook\n"
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/master/topic \
+ >out 2>&1 &&
-+ make_user_friendly_and_stable_output <out | grep "protocol error" >actual &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
-+ fatal: protocol error: proc-receive has bad status "xx" for "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
++ remote: # pre-receive hook
++ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: # proc-receive hook
++ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic xx msg
++ remote: error: protocol error: proc-receive has bad status "xx" for "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
++ To <URL/of/upstream.git>
++ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
-+ test_cmp expect actual &&
-+ git -C upstream show-ref >out &&
++ test_sorted_cmp expect actual &&
++ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+'
+
+test_expect_success "setup proc-receive hook (bad status)" '
-+ cat >upstream/hooks/proc-receive <<-EOF
++ cat >"$upstream/hooks/proc-receive" <<-EOF
+ #!/bin/sh
+
+ printf >&2 "# proc-receive hook\n"
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/master/topic \
+ >out 2>&1 &&
-+ make_user_friendly_and_stable_output <out | grep "protocol error" >actual &&
++ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
-+ fatal: protocol error: proc-receive has bad status "bad status" for "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
++ remote: # pre-receive hook
++ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: # proc-receive hook
++ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic bad status
++ remote: error: protocol error: proc-receive has bad status "bad status" for "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
++ To <URL/of/upstream.git>
++ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
-+ test_cmp expect actual &&
-+ git -C upstream show-ref >out &&
++ test_sorted_cmp expect actual &&
++ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+'
+
+test_expect_success "setup proc-receive hook (ng)" '
-+ cat >upstream/hooks/proc-receive <<-EOF
++ cat >"$upstream/hooks/proc-receive" <<-EOF
+ #!/bin/sh
+
+ printf >&2 "# proc-receive hook\n"
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic ng
-+ To ../upstream
++ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/master/topic (failed)
-+ error: failed to push some refs to "../upstream"
+ EOF
+ test_cmp expect actual &&
-+ git -C upstream show-ref >out &&
++ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+'
+
+test_expect_success "setup proc-receive hook (ng message)" '
-+ cat >upstream/hooks/proc-receive <<-EOF
++ cat >"$upstream/hooks/proc-receive" <<-EOF
+ #!/bin/sh
+
+ printf >&2 "# proc-receive hook\n"
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic ng error msg
-+ To ../upstream
++ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/master/topic (error msg)
-+ error: failed to push some refs to "../upstream"
+ EOF
+ test_cmp expect actual &&
-+ git -C upstream show-ref >out &&
++ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+'
+
+test_expect_success "setup proc-receive hook (report status on builtin command)" '
-+ cat >upstream/hooks/proc-receive <<-EOF
++ cat >"$upstream/hooks/proc-receive" <<-EOF
+ #!/bin/sh
+
+ printf >&2 "# proc-receive hook\n"
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ warning: proc-receive reported status on ref of builtin command: refs/heads/master
+ remote: # post-receive hook
+ remote: post-receive< <COMMIT-A> <COMMIT-B> refs/heads/master
-+ To ../upstream
++ To <URL/of/upstream.git>
+ <OID>..<OID> <COMMIT-B> -> master
+ ! [remote rejected] HEAD -> refs/for/master/topic (no report from proc-receive)
-+ error: failed to push some refs to "../upstream"
+ EOF
+ test_sorted_cmp expect actual &&
-+ git -C upstream show-ref >out &&
++ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-B> refs/heads/master
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+'
+
+test_expect_success "cleanup" '
-+ git -C upstream update-ref refs/heads/master $A
++ git -C "$upstream" update-ref refs/heads/master $A
+'
+
+test_expect_success "setup proc-receive hook (ok)" '
-+ cat >upstream/hooks/proc-receive <<-EOF
++ cat >"$upstream/hooks/proc-receive" <<-EOF
+ #!/bin/sh
+
+ printf >&2 "# proc-receive hook\n"
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic ok
+ remote: # post-receive hook
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
-+ To ../upstream
++ To <URL/of/upstream.git>
+ * [new reference] HEAD -> refs/for/master/topic
+ EOF
+ test_cmp expect actual &&
-+ git -C upstream show-ref >out &&
++ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/my/topic
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic ok
+ warning: proc-receive reported status on unknown ref: refs/for/master/topic
-+ To ../upstream
++ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/a/b/c/my/topic (no report from proc-receive)
-+ error: failed to push some refs to "../upstream"
+ EOF
+ test_sorted_cmp expect actual &&
-+ git -C upstream show-ref >out &&
++ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push -o ... : refs/for/master/topic
-+test_expect_success "not support push options" '
++test_expect_success C_LOCALE_OUTPUT "not support push options" '
+ test_must_fail git -C workbench push \
+ -o issue=123 \
+ -o reviewer=user1 \
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ fatal: the remote end hung up unexpectedly
+ EOF
+ test_cmp expect actual &&
-+ git -C upstream show-ref >out &&
++ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+'
+
+test_expect_success "enable push options" '
-+ git -C upstream config receive.advertisePushOptions true
++ git -C "$upstream" config receive.advertisePushOptions true
+'
+
+# Refs of upstream : master(A)
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ remote: # post-receive hook
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/next
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
-+ To ../upstream
++ To <URL/of/upstream.git>
+ * [new branch] HEAD -> next
+ * [new reference] HEAD -> refs/for/master/topic
+ EOF
+ test_cmp expect actual &&
-+ git -C upstream show-ref >out &&
++ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
3: 2440a474bd = 3: 1aa84fdf6f refs.c: refactor to reuse ref_is_hidden()
4: e041582643 ! 4: a301edb86c receive-pack: new config receive.procReceiveRefs
@@ builtin/receive-pack.c: static void execute_commands(struct command *commands,
- /* TODO: replace the fixed prefix by looking up git config variables. */
- if (!strncmp(cmd->ref_name, "refs/for/", 9)) {
-- cmd->run_proc_receive = 1;
+- cmd->run_proc_receive = RUN_PROC_RECEIVE_SCHEDULED;
- run_proc_receive = 1;
+ for (cmd = commands; cmd; cmd = cmd->next) {
+ if (!should_process_cmd(cmd))
@@ builtin/receive-pack.c: static void execute_commands(struct command *commands,
+ strbuf_setlen(&refname_full, prefix_len);
+ strbuf_addstr(&refname_full, cmd->ref_name);
+ if (ref_is_matched(&proc_receive_refs, cmd->ref_name, refname_full.buf)) {
-+ cmd->run_proc_receive = 1;
++ cmd->run_proc_receive = RUN_PROC_RECEIVE_SCHEDULED;
+ run_proc_receive = 1;
+ }
}
@@ t/t5411-proc-receive-hook.sh: test_expect_success "cleanup" '
+test_expect_success "add two receive.procReceiveRefs settings" '
+ (
-+ cd upstream &&
++ cd "$upstream" &&
+ git config --add receive.procReceiveRefs refs/for/ &&
+ git config --add receive.procReceiveRefs refs/review/
+ )
@@ t/t5411-proc-receive-hook.sh: test_expect_success "push with options" '
+# Refs of upstream : master(A) next(A)
+# Refs of workbench: master(A) tags/v123
+test_expect_success "cleanup" '
-+ git -C upstream update-ref -d refs/heads/next
++ git -C "$upstream" update-ref -d refs/heads/next
+'
+
+test_expect_success "setup proc-receive hook" '
-+ cat >upstream/hooks/proc-receive <<-EOF &&
++ cat >"$upstream/hooks/proc-receive" <<-EOF &&
+ #!/bin/sh
+
+ printf >&2 "# proc-receive hook\n"
+
+ test-tool proc-receive -v \
-+ -r "$ZERO_OID $A refs/for/next/topic ok" \
+ -r "$ZERO_OID $A refs/review/a/b/c/topic ok" \
++ -r "$ZERO_OID $A refs/for/next/topic ok" \
+ -r "$ZERO_OID $A refs/for/master/topic ok"
+ EOF
-+ chmod a+x upstream/hooks/proc-receive
++ chmod a+x "$upstream/hooks/proc-receive"
+'
+
+# Refs of upstream : master(A)
@@ t/t5411-proc-receive-hook.sh: test_expect_success "push with options" '
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/review/a/b/c/topic
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
-+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/next/topic ok
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/review/a/b/c/topic ok
++ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/next/topic ok
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic ok
+ remote: # post-receive hook
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/review/a/b/c/topic
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
-+ To ../upstream
++ To <URL/of/upstream.git>
+ * [new reference] HEAD -> refs/for/next/topic
+ * [new reference] HEAD -> refs/review/a/b/c/topic
+ * [new reference] HEAD -> refs/for/master/topic
+ EOF
+ test_cmp expect actual &&
-+ git -C upstream show-ref >out &&
++ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/master
@@ t/t5411-proc-receive-hook.sh: test_expect_success "push with options" '
+'
+
+test_expect_success "setup proc-receive hook" '
-+ cat >upstream/hooks/proc-receive <<-EOF &&
++ cat >"$upstream/hooks/proc-receive" <<-EOF &&
+ #!/bin/sh
+
+ printf >&2 "# proc-receive hook\n"
@@ t/t5411-proc-receive-hook.sh: test_expect_success "push with options" '
+ -r "$ZERO_OID $A refs/for/next/topic ok" \
+ -r "$ZERO_OID $A refs/for/master/topic ok"
+ EOF
-+ chmod a+x upstream/hooks/proc-receive
++ chmod a+x "$upstream/hooks/proc-receive"
+'
+
+# Refs of upstream : master(A)
@@ t/t5411-proc-receive-hook.sh: test_expect_success "push with options" '
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/foo
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
-+ To ../upstream
++ To <URL/of/upstream.git>
+ * [new branch] HEAD -> bar
+ * [new branch] HEAD -> baz
+ * [new reference] HEAD -> refs/for/next/topic
@@ t/t5411-proc-receive-hook.sh: test_expect_success "push with options" '
+ * [new reference] HEAD -> refs/for/master/topic
+ EOF
+ test_cmp expect actual &&
-+ git -C upstream show-ref >out &&
++ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/heads/bar5: 439acbdb62 ! 5: 538fa71f48 receive-pack: refactor report for proc-receive
@@ builtin/receive-pack.c: static int read_proc_receive_result(struct packet_reader
+ /* Reset "run_proc_receive" field, and continue to run in "receive-pack" */
+ hint->run_proc_receive = 0;
+ } else {
- die("protocol error: proc-receive has bad status '%s' for '%s'",
- status, reader->line);
- }
+ rp_error("protocol error: proc-receive has bad status '%s' for '%s'",
+ status, reader->line);
+ return -1;
@@ builtin/receive-pack.c: static void report(struct command *commands, const char *unpack_status)
packet_buf_write(&buf, "unpack %s\n",
unpack_status ? unpack_status : "ok");
@@ builtin/receive-pack.c: static void report(struct command *commands, const char
## t/t5411-proc-receive-hook.sh ##
@@ t/t5411-proc-receive-hook.sh: test_expect_success "setup proc-receive hook" '
- printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \
-- -r "$ZERO_OID $A refs/for/next/topic ok" \
-+ -r "$ZERO_OID $A refs/for/next/topic ok ref:refs/pull/123/head" \
-r "$ZERO_OID $A refs/review/a/b/c/topic ok" \
+- -r "$ZERO_OID $A refs/for/next/topic ok" \
- -r "$ZERO_OID $A refs/for/master/topic ok"
++ -r "$ZERO_OID $A refs/for/next/topic ok ref:refs/pull/123/head" \
+ -r "$ZERO_OID $A refs/for/master/topic ok ref:refs/pull/124/head"
EOF
- chmod a+x upstream/hooks/proc-receive
+ chmod a+x "$upstream/hooks/proc-receive"
'
@@ t/t5411-proc-receive-hook.sh: test_expect_success "report update of all special refs" '
- remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/review/a/b/c/topic
remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
-- remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/next/topic ok
-+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/next/topic ok ref:refs/pull/123/head
remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/review/a/b/c/topic ok
+- remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/next/topic ok
- remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic ok
++ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/next/topic ok ref:refs/pull/123/head
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic ok ref:refs/pull/124/head
remote: # post-receive hook
remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
remote: post-receive< <ZERO-OID> <COMMIT-A> refs/review/a/b/c/topic
remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
- To ../upstream
+ To <URL/of/upstream.git>
- * [new reference] HEAD -> refs/for/next/topic
+ * [new reference] HEAD -> refs/pull/123/head
* [new reference] HEAD -> refs/review/a/b/c/topic
@@ t/t5411-proc-receive-hook.sh: test_expect_success "report update of all special
+ * [new reference] HEAD -> refs/pull/124/head
EOF
test_cmp expect actual &&
- git -C upstream show-ref >out &&
+ git -C "$upstream" show-ref >out &&
@@ t/t5411-proc-receive-hook.sh: test_expect_success "setup proc-receive hook" '
# git push : bar(A) baz(A) refs/for/next/topic(A) foo(A) refs/for/master/topic(A)
test_expect_success "report mixed refs update" '
@@ t/t5411-proc-receive-hook.sh: test_expect_success "report mixed refs update" '
remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/foo
remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
- To ../upstream
+ To <URL/of/upstream.git>
+ <OID>..<OID> <COMMIT-B> -> master
* [new branch] HEAD -> bar
* [new branch] HEAD -> baz
@@ t/t5411-proc-receive-hook.sh: test_expect_success "report mixed refs update" '
+'
+
+test_expect_success "config receive.procReceiveRefs for all ref/" '
-+ git -C upstream config --add receive.procReceiveRefs refs/
++ git -C "$upstream" config --add receive.procReceiveRefs refs/
+'
+
+test_expect_success "setup proc-receive hook" '
-+ cat >upstream/hooks/proc-receive <<-EOF &&
++ cat >"$upstream/hooks/proc-receive" <<-EOF &&
+ #!/bin/sh
+
+ printf >&2 "# proc-receive hook\n"
@@ t/t5411-proc-receive-hook.sh: test_expect_success "report mixed refs update" '
+ -r "$A $B refs/for/master/topic ok ref:refs/pull/123/head" \
+ -r "$B $A refs/for/next/topic ok ref:refs/pull/124/head"
+ EOF
-+ chmod a+x upstream/hooks/proc-receive
++ chmod a+x "$upstream/hooks/proc-receive"
+'
+
+# Refs of upstream : master(B) foo(A) bar(A)) baz(A)
@@ t/t5411-proc-receive-hook.sh: test_expect_success "report mixed refs update" '
+ remote: post-receive< <COMMIT-B> <COMMIT-A> refs/heads/master
+ remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/master/topic
+ remote: post-receive< <COMMIT-B> <COMMIT-A> refs/for/next/topic
-+ To ../upstream
++ To <URL/of/upstream.git>
+ <OID>..<OID> <COMMIT-B> -> bar
+ - [deleted] foo
+ + <OID>...<OID> HEAD -> master (forced update)
@@ t/t5411-proc-receive-hook.sh: test_expect_success "report mixed refs update" '
+ * [new reference] HEAD -> refs/pull/124/head
+ EOF
+ test_cmp expect actual &&
-+ git -C upstream show-ref >out &&
++ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-B> refs/heads/bar
-: ---------- > 6: 20289547f3 t5412: test proc-receive hook on HTTP protocol 6: 24e5cddee0 = 7: da9106a42d doc: add documentation for the proc-receive hook
Jiang Xin (7):
transport: not report a non-head push as a branch
receive-pack: add new proc-receive hook refs.c: refactor to reuse ref_is_hidden()
receive-pack: new config receive.procReceiveRefs
receive-pack: refactor report for proc-receive
t5412: test proc-receive hook on HTTP protocol
doc: add documentation for the proc-receive hook
Documentation/config/receive.txt | 14 + Documentation/githooks.txt | 70 ++ Makefile | 1 + builtin/receive-pack.c | 300 ++++++- refs.c | 11 +- refs.h | 1 + t/helper/test-proc-receive.c | 172 ++++ t/helper/test-tool.c | 1 + t/helper/test-tool.h | 1 + t/t5411-proc-receive-hook.sh | 950 ++++++++++++++++++++ t/t5412-proc-receive-hook-http-protocol.sh | 960 +++++++++++++++++++++ t/t5516-fetch-push.sh | 2 +- transport-helper.c | 64 +- transport.c | 62 +- 14 files changed, 2543 insertions(+), 66 deletions(-) create mode 100644 t/helper/test-proc-receive.c create mode 100755 t/t5411-proc-receive-hook.sh create mode 100755 t/t5412-proc-receive-hook-http-protocol.sh
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
Re: [PATCH v6 0/7] New proc-receive hook for centralized workflow
Quoted 9 lines · Jiang Xin
From: Jiang Xin <[email protected]>
## Changes since v5
+ Fixed code review issues on v5.
* s/cannot to/cannot/
* s/FLEX_ALLOC_MEM/FLEX_ALLOC_STR/
OK
Quoted 2 lines
* Add flag
C_LOCALE_OUTPUTon test cases which may break gettext-poison test.
We prefer to avoid the C_LOCALE_OUTPUT prerequiste when we can use test_i18n<stuff> to make the poison test not care. If it is too hard to arrange in these tests, it is OK to omit, though.
Quoted 2 lines
* Note:
rp_errors()in "receive-pack" is executed on the server side, I think leave them unmarked for i18n is better.
Hmph, I vaguely recall saying something similar and then got a disagreement from somebody. Perhaps Jonathan (cc'ed)?
Thanks.
PATCH v7 1/7 transport: not report a non-head push as a branch
From: Jiang Xin <[email protected]>
When pushing a new reference (not a head or tag), report it as a new reference instead of a new branch.
--- t/t5411-proc-receive-hook.sh | 153 +++++++++++++++++++++++++++++++++++ t/t5516-fetch-push.sh | 2 +- transport.c | 9 ++- 3 files changed, 160 insertions(+), 4 deletions(-) create mode 100755 t/t5411-proc-receive-hook.sh
t/t5411-proc-receive-hook.sh new file +153 -0
@@ -0,0 +1,153 @@ +#!/bin/sh +# +# Copyright (c) 2020 Jiang Xin +# + +test_description='Test proc-receive hook' + +. ./test-lib.sh + +# Create commits in <repo> and assign each commit's oid to shell variables +# given in the arguments (A, B, and C). E.g.: +# +# create_commits_in <repo> A B C +# +# NOTE: Never calling this function from a subshell since variable +# assignments will disappear when subshell exits. +create_commits_in () { + repo="$1" && + if ! parent=$(git -C "$repo" rev-parse HEAD^{}) + then + parent= + fi && + T=$(git -C "$repo" write-tree) && + shift && + while test $# -gt 0 + do + name=$1 && + test_tick && + if test -z "$parent" + then + oid=$(echo $name | git -C "$repo" commit-tree $T) + else + oid=$(echo $name | git -C "$repo" commit-tree -p $parent $T) + fi && + eval $name=$oid && + parent=$oid && + shift || + return 1 + done && + git -C "$repo" update-ref refs/heads/master $oid +} + +# Format the output of git-push, git-show-ref and other commands to make a +# user-friendly and stable text. We can easily prepare the expect text +# without having to worry about future changes of the commit ID and spaces +# of the output. We also replce single quotes with double quotes, because +# it is boring to prepare unquoted single quotes in expect txt. +make_user_friendly_and_stable_output () { + sed \ + -e "s/ *\$//" \ + -e "s/ */ /g" \ + -e "s/'/\"/g" \ + -e "s/$A/<COMMIT-A>/g" \ + -e "s/$B/<COMMIT-B>/g" \ + -e "s/$TAG/<TAG-v123>/g" \ + -e "s/$ZERO_OID/<ZERO-OID>/g" \ + -e "s/[0-9a-f]\{7,\}/<OID>/g" +} + +# Refs of upstream : master(B) next(A) +# Refs of workbench: master(A) tags/v123 +test_expect_success "setup" ' + git init --bare upstream && + git init workbench && + create_commits_in workbench A B && + ( + cd workbench && + # Try to make a stable fixed width for abbreviated commit ID, + # this fixed-width oid will be replaced with "<OID>". + git config core.abbrev 7 && + git remote add origin ../upstream && + git update-ref refs/heads/master $A && + git tag -m "v123" v123 $A && + git push origin \ + $B:refs/heads/master \ + $A:refs/heads/next + ) && + TAG=$(git -C workbench rev-parse v123) && + + # setup pre-receive hook + cat >upstream/hooks/pre-receive <<-\EOF && + #!/bin/sh + + echo >&2 "# pre-receive hook" + + while read old new ref + do + echo >&2 "pre-receive< $old $new $ref" + done + EOF + + # setup post-receive hook + cat >upstream/hooks/post-receive <<-\EOF && + #!/bin/sh + + echo >&2 "# post-receive hook" + + while read old new ref + do + echo >&2 "post-receive< $old $new $ref" + done + EOF + + chmod a+x \ + upstream/hooks/pre-receive \ + upstream/hooks/post-receive +' + +# Refs of upstream : master(B) next(A) +# Refs of workbench: master(A) tags/v123 +# git-push -f : master(A) NULL tags/v123 refs/review/master/topic(A) a/b/c(A) +test_expect_success "normal git-push command" ' + git -C workbench push -f origin \ + refs/tags/v123 \ + :refs/heads/next \ + HEAD:refs/heads/master \ + HEAD:refs/review/master/topic \ + HEAD:refs/heads/a/b/c \ + >out 2>&1 && + make_user_friendly_and_stable_output <out >actual && + cat >expect <<-EOF && + remote: # pre-receive hook + remote: pre-receive< <COMMIT-B> <COMMIT-A> refs/heads/master + remote: pre-receive< <COMMIT-A> <ZERO-OID> refs/heads/next + remote: pre-receive< <ZERO-OID> <TAG-v123> refs/tags/v123 + remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/review/master/topic + remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/a/b/c + remote: # post-receive hook + remote: post-receive< <COMMIT-B> <COMMIT-A> refs/heads/master + remote: post-receive< <COMMIT-A> <ZERO-OID> refs/heads/next + remote: post-receive< <ZERO-OID> <TAG-v123> refs/tags/v123 + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/review/master/topic + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/a/b/c + To ../upstream + + <OID>...<OID> HEAD -> master (forced update) + - [deleted] next + * [new tag] v123 -> v123 + * [new reference] HEAD -> refs/review/master/topic + * [new branch] HEAD -> a/b/c + EOF + test_cmp expect actual && + git -C upstream show-ref >out && + make_user_friendly_and_stable_output <out >actual && + cat >expect <<-EOF && + <COMMIT-A> refs/heads/a/b/c + <COMMIT-A> refs/heads/master + <COMMIT-A> refs/review/master/topic + <TAG-v123> refs/tags/v123 + EOF + test_cmp expect actual +' + +test_done
t/t5516-fetch-push.sh +1 -1
@@ -1039,7 +1039,7 @@ test_force_fetch_tag "annotated tag" "-f -a -m'tag message'" test_expect_success 'push --porcelain' ' mk_empty testrepo && echo >.git/foo "To testrepo" && - echo >>.git/foo "* refs/heads/master:refs/remotes/origin/master [new branch]" && + echo >>.git/foo "* refs/heads/master:refs/remotes/origin/master [new reference]" && echo >>.git/foo "Done" && git push >.git/bar --porcelain testrepo refs/heads/master:refs/remotes/origin/master && (
transport.c +6 -3
@@ -500,9 +500,12 @@ static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widt porcelain, summary_width); else if (is_null_oid(&ref->old_oid))print_ref_status ('*' , - (starts_with (ref->name,"refs/tags/" ) ?"[new tag]" : -"[new branch]" ), - ref, ref->peer_ref,NULL , porcelain, summary_width); + (starts_with (ref->name,"refs/tags/" ) + ?"[new tag]" + : (starts_with (ref->name,"refs/heads/" ) + ?"[new branch]" + :"[new reference]" )), + ref, ref->peer_ref,NULL , porcelain, summary_width); else {struct strbuf quickref =STRBUF_INIT ;char type;
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v7 2/7 receive-pack: add new proc-receive hook
In reply to Junio C HamanoFrom: Jiang Xin <[email protected]>
Git calls an internal execute_commands function to handle commands sent from client to git-receive-pack. Regardless of what references the user pushes, git creates or updates the corresponding references if the user has write-permission. A contributor who has no write-permission, cannot push to the repository directly. So, the contributor has to write commits to an alternate location, and sends pull request by emails or by other ways. We call this workflow as a distributed workflow.
It would be more convenient to work in a centralized workflow like what Gerrit provided for some cases. For example, a read-only user who cannot push to a branch directly can run the following git push command to push commits to a pseudo reference (has a prefix "refs/for/", not "refs/heads/") to create a code review.
git push origin \
HEAD:refs/for/<branch-name>/<session>The <branch-name> in the above example can be as simple as "master", or a more complicated branch name like "foo/bar". The <session> in the above example command can be the local branch name of the client side, such as "my/topic".
We cannot implement a centralized workflow elegantly by using "pre-receive" + "post-receive", because Git will call the internal function "execute_commands" to create references (even the special pseudo reference) between these two hooks. Even though we can delete the temporarily created pseudo reference via the "post-receive" hook, having a temporary reference is not safe for concurrent pushes.
So, add a filter and a new handler to support this kind of workflow. The filter will check the prefix of the reference name, and if the command has a special reference name, the filter will turn a specific field (run_proc_receive) on for the command. Commands with this filed turned on will be executed by a new handler (an hook named "proc-receive") instead of the internal execute_commands function. We can use this "proc-receive" command to create pull requests or send emails for code review.
Suggested by Junio, this "proc-receive" hook reads the commands, push-options (optional), and send result using a protocol in pkt-line format. In the following example, The letter "S" stands for "receive-pack" and letter "H" stands for the hook.
# Version and capabilities negotiation.
S: PKT-LINE(version=1\0push-options atomic...)
S: flush-pkt
H: PKT-LINE(version=1\0push-options...)
H: flush-pkt
# Send commands from server to the hook.
S: PKT-LINE(old-oid new-oid ref)
S: ... ...
S: flush-pkt
# Only if push-options have been negotiated.
S: PKT-LINE(push-option)
S: ... ...
S: flush-pkt
# Receive result from the hook.
# OK, run this command successfully.
H: PKT-LINE(old-oid new-oid ref ok)
# NO, I reject it.
H: PKT-LINE(old-oid new-oid ref ng reason)
# OK, but use an alternate reference. (in latter commit)
H: PKT-LINE(old-oid new-oid ref ok ref:alt-ref)
# It will fallthrough to receive-pack to execute. (in latter commit)
H: PKT-LINE(old-oid new-oid ref ft)
H: ... ...
H: flush-pkt
After receiving a command, the hook can create/update another alternate reference. For example, a command for a reference "refs/for/master" may create a special reference, such as "refs/pull/123/head". The alternate reference can be returned from the result in an extensible format like "<old-oid> <new-oid> <reference> <status> [<message>]".
The result will be stored in a command list, and "receive-pack" will use the result to replace the commands that have specific run_proc_receive field turned on.
--- Makefile | 1 + builtin/receive-pack.c | 264 +++++++++++++++- t/helper/test-proc-receive.c | 172 ++++++++++ t/helper/test-tool.c | 1 + t/helper/test-tool.h | 1 + t/t5411-proc-receive-hook.sh | 593 ++++++++++++++++++++++++++++++++++- 6 files changed, 1018 insertions(+), 14 deletions(-) create mode 100644 t/helper/test-proc-receive.c
Makefile +1 -0
@@ -725,6 +725,7 @@ TEST_BUILTINS_OBJS += test-parse-pathspec-file.o TEST_BUILTINS_OBJS += test-path-utils.o TEST_BUILTINS_OBJS += test-pkt-line.o TEST_BUILTINS_OBJS += test-prio-queue.o +TEST_BUILTINS_OBJS += test-proc-receive.o TEST_BUILTINS_OBJS += test-progress.o TEST_BUILTINS_OBJS += test-reach.o TEST_BUILTINS_OBJS += test-read-cache.o
builtin/receive-pack.c +261 -3
310 lines not on this page · raw message
t/helper/test-proc-receive.c new file +172 -0
172 lines not on this page · raw message
t/helper/test-tool.c +1 -0
@@ -44,6 +44,7 @@ static struct test_cmd cmds[] = { { "path-utils", cmd__path_utils }, { "pkt-line", cmd__pkt_line }, { "prio-queue", cmd__prio_queue }, + { "proc-receive", cmd__proc_receive}, { "progress", cmd__progress }, { "reach", cmd__reach }, { "read-cache", cmd__read_cache },
t/helper/test-tool.h +1 -0
@@ -34,6 +34,7 @@ int cmd__parse_pathspec_file(int argc, const char** argv);int cmd__path_utils (int argc,const char **argv);int cmd__pkt_line (int argc,const char **argv);int cmd__prio_queue (int argc,const char **argv); +int cmd__proc_receive (int argc,const char **argv);int cmd__progress (int argc,const char **argv);int cmd__reach (int argc,const char **argv);int cmd__read_cache (int argc,const char **argv);
t/t5411-proc-receive-hook.sh +582 -11
650 lines not on this page · raw message
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v7 3/7 refs.c: refactor to reuse ref_is_hidden()
In reply to Junio C HamanoFrom: Jiang Xin <[email protected]>
Add new function ref_is_matched() to reuse ref_is_hidden(). Will use this function for receive-pack to check commands with specific prefixes.
Test case t5512 covered this change.
--- refs.c | 11 ++++++++--- refs.h | 1 + 2 files changed, 9 insertions(+), 3 deletions(-)
refs.c +8 -3
@@ -1389,13 +1389,18 @@ int parse_hide_refs_config(const char *var, const char *value, const char *secti }int ref_is_hidden (const char *refname,const char *refname_full) +{ +return ref_is_matched (hide_refs, refname, refname_full); +} + +int ref_is_matched (struct string_list *match_refs,const char *refname,const char *refname_full) {int i; -if (!hide_refs) +if (!match_refs)return 0 ; -for (i = hide_refs->nr -1 ; i >=0 ; i--) { -const char *match = hide_refs->items[i].string; +for (i = match_refs->nr -1 ; i >=0 ; i--) { +const char *match = match_refs->items[i].string;const char *subject;int neg =0 ;const char *p;
refs.h +1 -0
@@ -739,6 +739,7 @@ int parse_hide_refs_config(const char *var, const char *value, const char *);* parameter always points to the full ref name. */ int ref_is_hidden (const char *,const char *); +int ref_is_matched (struct string_list *,const char *,const char *);enum ref_type {REF_TYPE_PER_WORKTREE ,/* refs inside refs/ but not shared */
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v7 4/7 receive-pack: new config receive.procReceiveRefs
In reply to Junio C HamanoFrom: Jiang Xin <[email protected]>
Add a new multi-valued config variable "receive.procReceiveRefs" for receive-pack command, like the follows:
git config --system --add receive.procReceiveRefs refs/for/
git config --system --add receive.procReceiveRefs refs/drafts/If the specific prefix strings match the reference names of the commands which are sent by git client to receive-pack, these commands will be executed by an external hook (named "proc-receive"), instead of the internal execute_commands function.
For example, if it is set to "refs/for/", pushing to a reference such as "refs/for/master" will not create or update reference "refs/for/master", but may create or update a pull request directly by running the external hook.
--- Documentation/config/receive.txt | 14 ++++ builtin/receive-pack.c | 43 ++++++++-- t/t5411-proc-receive-hook.sh | 130 +++++++++++++++++++++++++++++++ 3 files changed, 180 insertions(+), 7 deletions(-)
Documentation/config/receive.txt +14 -0
@@ -114,6 +114,20 @@ receive.hideRefs:: An attempt to update or delete a hidden ref by `git push` is rejected. +receive.procReceiveRefs:: + This is a multi-valued variable that defines reference prefixes + to match the commands in `receive-pack`. Commands matching the + prefixes will be executed by an external hooks "proc-receive", + instead of the internal `execute_commands` function. If this + variable is not defined, the "proc-receive" hook will never be + used, and all commands will be executed by the internal + `execute_commands` function. + + For example, if this variable is set to "refs/for/", pushing to + reference such as "refs/for/master" will not create or update a + reference named "refs/for/master", but may create or update a + pull request directly by running an external hook. + receive.updateServerInfo:: If set to true, git-receive-pack will run git-update-server-info after receiving data from git-push and updating refs.
builtin/receive-pack.c +36 -7
74 lines not on this page · raw message
t/t5411-proc-receive-hook.sh +130 -0
140 lines not on this page · raw message
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
v7 cover New proc-receive hook for centralized workflow
In reply to Junio C HamanoFrom: Jiang Xin <[email protected]>
## Changes since v6
* Test breaks because of the conflict between copy_to_sideband() and rp_error(). Store error messages in errmsg and output the errmsg after sideband muxer closed.
* Use test_i18ncmp instead of mark the whole test case using C_LOCALE_OUTPUT.
* Depends on topic jx/atomic-push. One test case will fail without it.
## Range-diff v6...v7
1: ed8d2c0e82 ! 1: 4259d31426 receive-pack: add new proc-receive hook
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+}
+
+static int read_proc_receive_result(struct packet_reader *reader,
-+ struct command *commands)
++ struct command *commands,
++ struct strbuf *errmsg)
+{
+ struct command *hint;
+ struct command *cmd;
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ *p++ != ' ' ||
+ parse_oid_hex(p, &new_oid, &p) ||
+ *p++ != ' ') {
-+ rp_error("protocol error: proc-receive expected 'old new ref status [msg]', got '%s'",
-+ reader->line);
++ strbuf_addf(errmsg, "proc-receive expected 'old new ref status [msg]', got '%s'\n",
++ reader->line);
+ return -1;
+ }
+
+ refname = p;
+ status = strchr(p, ' ');
+ if (!status) {
-+ rp_error("protocol error: proc-receive expected 'old new ref status [msg]', got '%s'",
-+ reader->line);
++ strbuf_addf(errmsg, "proc-receive expected 'old new ref status [msg]', got '%s'\n",
++ reader->line);
+ return -1;
+ }
+ *status++ = '\0';
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ *msg++ = '\0';
+ }
+ if (strlen(status) != 2) {
-+ rp_error("protocol error: proc-receive has bad status '%s' for '%s'",
-+ status, reader->line);
++ strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'\n",
++ status, reader->line);
+ return -1;
+ }
+
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ if (!hint)
+ hint = find_command_by_refname(commands, refname);
+ if (!hint) {
-+ warning("proc-receive reported status on unknown ref: %s",
-+ refname);
++ strbuf_addf(errmsg, "proc-receive reported status on unknown ref: %s\n",
++ refname);
+ continue;
+ }
+ if (!hint->run_proc_receive) {
-+ warning("proc-receive reported status on ref of builtin command: %s",
-+ refname);
++ strbuf_addf(errmsg, "proc-receive reported status on ref of builtin command: %s\n",
++ refname);
+ continue;
+ }
+ hint->run_proc_receive |= RUN_PROC_RECEIVE_RETURNED;
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ hint->error_string = "failed";
+ code = 1;
+ } else if (strcmp("ok", status)) {
-+ rp_error("protocol error: proc-receive has bad status '%s' for '%s'",
-+ status, reader->line);
++ strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'\n",
++ status, reader->line);
+ return -1;
+ }
+ }
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ const char *argv[2];
+ struct packet_reader reader;
+ struct strbuf cap = STRBUF_INIT;
++ struct strbuf errmsg = STRBUF_INIT;
+ int pr_use_push_options = 0;
+ int version = 0;
+ int code;
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ }
+
+ if (version != 1) {
-+ rp_error("protocol error: unknown proc-receive version '%d'",
-+ version);
++ strbuf_addf(&errmsg, "proc-receive version '%d' is not supported\n",
++ version);
+ code = -1;
+ goto cleanup;
+ }
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ }
+
+ /* Read result from proc-receive */
-+ code = read_proc_receive_result(&reader, commands);
++ code = read_proc_receive_result(&reader, commands, &errmsg);
+
+cleanup:
+ close(proc.in);
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ if (use_sideband)
+ finish_async(&muxer);
+ if (finish_command(&proc)) {
-+ rp_error("proc-receive did not exit properly");
++ strbuf_addstr(&errmsg, "proc-receive did not exit properly\n");
+ code = -1;
+ }
++ if (errmsg.len >0) {
++ char *p = errmsg.buf;
++
++ p += errmsg.len - 1;
++ if (*p == '\n')
++ *p = '\0';
++ rp_error("%s", errmsg.buf);
++ strbuf_release(&errmsg);
++ }
+ sigchain_pop(SIGPIPE);
++
+ return code;
+}
+
@@ t/t5411-proc-receive-hook.sh: make_user_friendly_and_stable_output () {
+ -e "s/[0-9a-f]\{7,\}/<OID>/g" \
+ -e "s#To ../upstream.git#To <URL/of/upstream.git>#" \
+ -e "/^error: / d"
-+}
-+
-+# Asynchronous sideband may generate inconsistent output messages,
-+# sort before comparison.
-+test_sorted_cmp () {
-+ if ! $GIT_TEST_CMP "$@" >/dev/null 2>&1
-+ then
-+ cmd=$GIT_TEST_CMP
-+ for f in "$@"
-+ do
-+ sort "$f" >"$f.sorted"
-+ cmd="$cmd \"$f.sorted\""
-+ done
-+ if ! eval $cmd >/dev/null 2>&1
-+ then
-+ $GIT_TEST_CMP "$@"
-+ fi
-+ fi
}
# Refs of upstream : master(B) next(A)
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ * [new branch] HEAD -> next
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
-+ test_sorted_cmp expect actual &&
++ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push --atomic: next(A) refs/for/master/topic(A)
-+test_expect_failure "no proc-receive hook, fail all for atomic push" '
++test_expect_success "no proc-receive hook, fail all for atomic push" '
+ test_must_fail git -C workbench push --atomic origin \
+ HEAD:next \
+ HEAD:refs/for/master/topic >out 2>&1 &&
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ ! [remote rejected] HEAD -> next (fail to run proc-receive hook)
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
-+ test_sorted_cmp expect actual &&
++ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push : refs/for/master/topic(A)
-+test_expect_success C_LOCALE_OUTPUT "proc-receive bad protocol: unknown version" '
++test_expect_success "proc-receive bad protocol: unknown version" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/master/topic \
+ >out 2>&1 &&
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ remote: # pre-receive hook
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: # proc-receive hook
-+ remote: error: protocol error: unknown proc-receive version "2"
+ remote: fatal: the remote end hung up unexpectedly
-+ remote: error: proc-receive did not exit properly
++ remote: error: proc-receive version "2" is not supported
++ remote: proc-receive did not exit properly
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
-+ test_sorted_cmp expect actual &&
++ test_i18ncmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: proc-receive> bad-id new-id ref ok
-+ remote: error: protocol error: proc-receive expected "old new ref status [msg]", got "bad-id new-id ref ok"
++ remote: error: proc-receive expected "old new ref status [msg]", got "bad-id new-id ref ok"
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
-+ test_sorted_cmp expect actual &&
++ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic
-+ remote: error: protocol error: proc-receive expected "old new ref status [msg]", got "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
++ remote: error: proc-receive expected "old new ref status [msg]", got "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
-+ test_sorted_cmp expect actual &&
++ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic xx msg
-+ remote: error: protocol error: proc-receive has bad status "xx" for "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
++ remote: error: proc-receive has bad status "xx" for "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
-+ test_sorted_cmp expect actual &&
++ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic bad status
-+ remote: error: protocol error: proc-receive has bad status "bad status" for "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
++ remote: error: proc-receive has bad status "bad status" for "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
-+ test_sorted_cmp expect actual &&
++ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/heads/master ok
-+ warning: proc-receive reported status on ref of builtin command: refs/heads/master
++ remote: error: proc-receive reported status on ref of builtin command: refs/heads/master
+ remote: # post-receive hook
+ remote: post-receive< <COMMIT-A> <COMMIT-B> refs/heads/master
+ To <URL/of/upstream.git>
+ <OID>..<OID> <COMMIT-B> -> master
+ ! [remote rejected] HEAD -> refs/for/master/topic (no report from proc-receive)
+ EOF
-+ test_sorted_cmp expect actual &&
++ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/my/topic
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic ok
-+ warning: proc-receive reported status on unknown ref: refs/for/master/topic
++ remote: error: proc-receive reported status on unknown ref: refs/for/master/topic
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/a/b/c/my/topic (no report from proc-receive)
+ EOF
-+ test_sorted_cmp expect actual &&
++ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push -o ... : refs/for/master/topic
-+test_expect_success C_LOCALE_OUTPUT "not support push options" '
++test_expect_success "not support push options" '
+ test_must_fail git -C workbench push \
+ -o issue=123 \
+ -o reviewer=user1 \
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ fatal: the receiving end does not support push options
+ fatal: the remote end hung up unexpectedly
+ EOF
-+ test_cmp expect actual &&
++ test_i18ncmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
2: 1aa84fdf6f = 2: b47379a7fe refs.c: refactor to reuse ref_is_hidden()
3: a301edb86c = 3: 461f3a4141 receive-pack: new config receive.procReceiveRefs
4: 538fa71f48 ! 4: 82e6d94b0b receive-pack: refactor report for proc-receive @@ builtin/receive-pack.c: static int read_proc_receive_result(struct packet_reader
+ /* Reset "run_proc_receive" field, and continue to run in "receive-pack" */
+ hint->run_proc_receive = 0;
+ } else {
- rp_error("protocol error: proc-receive has bad status '%s' for '%s'",
- status, reader->line);
+ strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'\n",
+ status, reader->line);
return -1;
@@ builtin/receive-pack.c: static void report(struct command *commands, const char *unpack_status)
packet_buf_write(&buf, "unpack %s\n",
5: 20289547f3 ! 5: f90bacec1d t5412: test proc-receive hook on HTTP protocol
@@ t/t5412-proc-receive-hook-http-protocol.sh (new)
+ -e "/^remote: fatal: /d"
+}
+
-+# Asynchronous sideband may generate inconsistent output messages,
-+# sort before comparison.
-+test_sorted_cmp () {
-+ if ! $GIT_TEST_CMP "$@" >/dev/null 2>&1
-+ then
-+ cmd=$GIT_TEST_CMP
-+ for f in "$@"
-+ do
-+ sort "$f" >"$f.sorted"
-+ cmd="$cmd \"$f.sorted\""
-+ done
-+ if ! eval $cmd >/dev/null 2>&1
-+ then
-+ $GIT_TEST_CMP "$@"
-+ fi
-+ fi
-+}
-+
+# Refs of upstream : master(B) next(A)
+# Refs of workbench: master(A) tags/v123
+test_expect_success "setup" '
@@ t/t5412-proc-receive-hook-http-protocol.sh (new)
+ * [new branch] HEAD -> next
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
-+ test_sorted_cmp expect actual &&
++ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
@@ t/t5412-proc-receive-hook-http-protocol.sh (new)
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push --atomic: next(A) refs/for/master/topic(A)
-+test_expect_failure "no proc-receive hook, fail all for atomic push" '
++test_expect_success "no proc-receive hook, fail all for atomic push" '
+ test_must_fail git -C workbench push --atomic origin \
+ HEAD:next \
+ HEAD:refs/for/master/topic >out 2>&1 &&
@@ t/t5412-proc-receive-hook-http-protocol.sh (new)
+ ! [remote rejected] HEAD -> next (fail to run proc-receive hook)
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
-+ test_sorted_cmp expect actual &&
++ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
@@ t/t5412-proc-receive-hook-http-protocol.sh (new)
+ remote: # pre-receive hook
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: # proc-receive hook
-+ remote: error: protocol error: unknown proc-receive version "2"
-+ remote: error: proc-receive did not exit properly
++ remote: error: proc-receive version "2" is not supported
++ remote: proc-receive did not exit properly
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
-+ test_sorted_cmp expect actual &&
++ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
@@ t/t5412-proc-receive-hook-http-protocol.sh (new)
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: proc-receive> bad-id new-id ref ok
-+ remote: error: protocol error: proc-receive expected "old new ref status [msg]", got "bad-id new-id ref ok"
++ remote: error: proc-receive expected "old new ref status [msg]", got "bad-id new-id ref ok"
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
-+ test_sorted_cmp expect actual &&
++ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
@@ t/t5412-proc-receive-hook-http-protocol.sh (new)
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic
-+ remote: error: protocol error: proc-receive expected "old new ref status [msg]", got "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
++ remote: error: proc-receive expected "old new ref status [msg]", got "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
-+ test_sorted_cmp expect actual &&
++ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
@@ t/t5412-proc-receive-hook-http-protocol.sh (new)
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic xx msg
-+ remote: error: protocol error: proc-receive has bad status "xx" for "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
++ remote: error: proc-receive has bad status "xx" for "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
-+ test_sorted_cmp expect actual &&
++ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
@@ t/t5412-proc-receive-hook-http-protocol.sh (new)
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic bad status
-+ remote: error: protocol error: proc-receive has bad status "bad status" for "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
++ remote: error: proc-receive has bad status "bad status" for "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
-+ test_sorted_cmp expect actual &&
++ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
@@ t/t5412-proc-receive-hook-http-protocol.sh (new)
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/heads/master ok
++ remote: error: proc-receive reported status on ref of builtin command: refs/heads/master
+ remote: # post-receive hook
+ remote: post-receive< <COMMIT-A> <COMMIT-B> refs/heads/master
+ To <URL/of/upstream.git>
+ <OID>..<OID> <COMMIT-B> -> master
+ ! [remote rejected] HEAD -> refs/for/master/topic (no report from proc-receive)
+ EOF
-+ test_sorted_cmp expect actual &&
++ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
@@ t/t5412-proc-receive-hook-http-protocol.sh (new)
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/my/topic
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic ok
++ remote: error: proc-receive reported status on unknown ref: refs/for/master/topic
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/a/b/c/my/topic (no report from proc-receive)
+ EOF
-+ test_sorted_cmp expect actual &&
++ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
@@ t/t5412-proc-receive-hook-http-protocol.sh (new)
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push -o ... : refs/for/master/topic
-+test_expect_success C_LOCALE_OUTPUT "not support push options" '
++test_expect_success "not support push options" '
+ test_must_fail git -C workbench push \
+ -o issue=123 \
+ -o reviewer=user1 \
@@ t/t5412-proc-receive-hook-http-protocol.sh (new)
+ fatal: the receiving end does not support push options
+ fatal: the remote end hung up unexpectedly
+ EOF
-+ test_cmp expect actual &&
++ test_i18ncmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&6: da9106a42d = 6: 5338d358c3 doc: add documentation for the proc-receive hook
---
Jiang Xin (7):
transport: not report a non-head push as a branch
receive-pack: add new proc-receive hook refs.c: refactor to reuse ref_is_hidden()
receive-pack: new config receive.procReceiveRefs
receive-pack: refactor report for proc-receive
t5412: test proc-receive hook on HTTP protocol
doc: add documentation for the proc-receive hook
Documentation/config/receive.txt | 14 + Documentation/githooks.txt | 70 ++ Makefile | 1 + builtin/receive-pack.c | 312 ++++++- refs.c | 11 +- refs.h | 1 + t/helper/test-proc-receive.c | 172 ++++ t/helper/test-tool.c | 1 + t/helper/test-tool.h | 1 + t/t5411-proc-receive-hook.sh | 932 ++++++++++++++++++++ t/t5412-proc-receive-hook-http-protocol.sh | 944 +++++++++++++++++++++ t/t5516-fetch-push.sh | 2 +- transport-helper.c | 64 +- transport.c | 62 +- 14 files changed, 2521 insertions(+), 66 deletions(-) create mode 100644 t/helper/test-proc-receive.c create mode 100755 t/t5411-proc-receive-hook.sh create mode 100755 t/t5412-proc-receive-hook-http-protocol.sh
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
v8 cover New proc-receive hook for centralized workflow
From: Jiang Xin <[email protected]>
## Changes since v7
* Parallel test (make -j7 test) failed because of the failure message on calling finish_command() is not stable for test. Remove the error message in this revision.
- if (finish_command(&proc)) {
- strbuf_addstr(&errmsg, "proc-receive did not exit properly\n");
+ if (finish_command(&proc))
code = -1;
- }## Changes since v6
* Test failed because two message streams of copy_to_sideband() and rp_error() are mixed together. Store error messages in errmsg and output the errmsg after closing the sideband muxer.
* Use test_i18ncmp instead of mark the whole test case using C_LOCALE_OUTPUT.
* Please merge this topic after topic jx/atomic-push. One test case depends on it.
## Range-diff v6...v7
1: 64c9ca7086 = 1: 19c66785d1 transport: not report a non-head push as a branch
2: 0d27cc5e80 ! 2: 085ded61f5 receive-pack: add new proc-receive hook
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ *p++ != ' ' ||
+ parse_oid_hex(p, &new_oid, &p) ||
+ *p++ != ' ') {
-+ strbuf_addf(errmsg, "proc-receive expected 'old new ref status [msg]', got '%s'\n",
++ strbuf_addf(errmsg, "proc-receive expected 'old new ref status [msg]', got '%s'",
+ reader->line);
+ return -1;
+ }
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ refname = p;
+ status = strchr(p, ' ');
+ if (!status) {
-+ strbuf_addf(errmsg, "proc-receive expected 'old new ref status [msg]', got '%s'\n",
++ strbuf_addf(errmsg, "proc-receive expected 'old new ref status [msg]', got '%s'",
+ reader->line);
+ return -1;
+ }
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ *msg++ = '\0';
+ }
+ if (strlen(status) != 2) {
-+ strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'\n",
++ strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'",
+ status, reader->line);
+ return -1;
+ }
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ hint->error_string = xstrdup(msg);
+ else
+ hint->error_string = "failed";
-+ code = 1;
++ code = -1;
+ } else if (strcmp("ok", status)) {
-+ strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'\n",
++ strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'",
+ status, reader->line);
+ return -1;
+ }
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+
+ for (cmd = commands; cmd; cmd = cmd->next)
+ if (cmd->run_proc_receive &&
-+ !(cmd->run_proc_receive & RUN_PROC_RECEIVE_RETURNED))
++ !(cmd->run_proc_receive & RUN_PROC_RECEIVE_RETURNED)) {
+ cmd->error_string = "no report from proc-receive";
++ code = -1;
++ }
+
+ return code;
+}
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ }
+
+ if (version != 1) {
-+ strbuf_addf(&errmsg, "proc-receive version '%d' is not supported\n",
++ strbuf_addf(&errmsg, "proc-receive version '%d' is not supported",
+ version);
+ code = -1;
+ goto cleanup;
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ close(proc.out);
+ if (use_sideband)
+ finish_async(&muxer);
-+ if (finish_command(&proc)) {
-+ strbuf_addstr(&errmsg, "proc-receive did not exit properly\n");
++ if (finish_command(&proc))
+ code = -1;
-+ }
+ if (errmsg.len >0) {
+ char *p = errmsg.buf;
+
@@ t/t5411-proc-receive-hook.sh: test_expect_success "normal git-push command" '
+ remote: # proc-receive hook
+ remote: fatal: the remote end hung up unexpectedly
+ remote: error: proc-receive version "2" is not supported
-+ remote: proc-receive did not exit properly
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF
3: 81f086188c = 3: 230f28198f refs.c: refactor to reuse ref_is_hidden()
4: 52675b55a2 ! 4: e6a7608a84 receive-pack: new config receive.procReceiveRefs
@@ t/t5411-proc-receive-hook.sh: test_expect_success "cleanup" '
+test_expect_success "add two receive.procReceiveRefs settings" '
+ (
+ cd "$upstream" &&
-+ git config --add receive.procReceiveRefs refs/for/ &&
++ git config --add receive.procReceiveRefs refs/for &&
+ git config --add receive.procReceiveRefs refs/review/
+ )
+'5: 65876b1e75 ! 5: e426775925 receive-pack: refactor report for proc-receive
@@ builtin/receive-pack.c: static void write_head_info(void)
@@ builtin/receive-pack.c: static int read_proc_receive_result(struct packet_reader *reader,
else
hint->error_string = "failed";
- code = 1;
+ code = -1;
- } else if (strcmp("ok", status)) {
+ } else if (!strcmp("ok", status)) {
+ hint->extra_string = xstrdup_or_null(msg);
@@ builtin/receive-pack.c: static int read_proc_receive_result(struct packet_reader
+ /* Reset "run_proc_receive" field, and continue to run in "receive-pack" */
+ hint->run_proc_receive = 0;
+ } else {
- strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'\n",
+ strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'",
status, reader->line);
return -1;
@@ builtin/receive-pack.c: static void report(struct command *commands, const char *unpack_status)
6: 04ec372c35 ! 6: c5982067be t5412: test proc-receive hook on HTTP protocol
@@ Metadata
Author: Jiang Xin <[email protected]>
## Commit message ##
- t5412: test proc-receive hook on HTTP protocol
+ t5412: test the proc-receive hook on HTTP protocol
- Copy from t5411 to test "proc-receive" hook on HTTP protocol.
+ Copy from t5411 to test the "proc-receive" hook on the HTTP protocol.
Signed-off-by: Jiang Xin <[email protected]>
@@ t/t5412-proc-receive-hook-http-protocol.sh (new)
+ -e "s/$ZERO_OID/<ZERO-OID>/g" \
+ -e "s/[0-9a-f]\{7,\}/<OID>/g" \
+ -e "s#To .*/upstream.git#To <URL/of/upstream.git>#" \
-+ -e "/^error: / d" \
-+ -e "/^remote: fatal: /d"
++ -e "/^error: / d"
+}
+
+# Refs of upstream : master(B) next(A)
@@ t/t5412-proc-receive-hook-http-protocol.sh (new)
+test_expect_success "add two receive.procReceiveRefs settings" '
+ (
+ cd "$upstream" &&
-+ git config --add receive.procReceiveRefs refs/for/ &&
++ git config --add receive.procReceiveRefs refs/for &&
+ git config --add receive.procReceiveRefs refs/review/
+ )
+'
@@ t/t5412-proc-receive-hook-http-protocol.sh (new)
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/master/topic \
+ >out 2>&1 &&
-+ make_user_friendly_and_stable_output <out >actual &&
++ # Apache calls "receive-pack" with system locale settings, which
++ # makes the message "remote: fatal: ..." unstable for test.
++ sed -e "/^remote: fatal: / d" out | \
++ make_user_friendly_and_stable_output >actual &&
+ cat >expect <<-EOF &&
+ remote: # pre-receive hook
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: # proc-receive hook
+ remote: error: proc-receive version "2" is not supported
-+ remote: proc-receive did not exit properly
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
+ EOF7: 4ab8873f12 = 7: 823b7f2ea6 doc: add documentation for the proc-receive hook
---
Jiang Xin (7):
transport: not report a non-head push as a branch
receive-pack: add new proc-receive hook refs.c: refactor to reuse ref_is_hidden()
receive-pack: new config receive.procReceiveRefs
receive-pack: refactor report for proc-receive
t5412: test the proc-receive hook on HTTP protocol
doc: add documentation for the proc-receive hook
Documentation/config/receive.txt | 14 + Documentation/githooks.txt | 70 ++ Makefile | 1 + builtin/receive-pack.c | 312 ++++++- refs.c | 11 +- refs.h | 1 + t/helper/test-proc-receive.c | 172 ++++ t/helper/test-tool.c | 1 + t/helper/test-tool.h | 1 + t/t5411-proc-receive-hook.sh | 931 ++++++++++++++++++++ t/t5412-proc-receive-hook-http-protocol.sh | 945 +++++++++++++++++++++ t/t5516-fetch-push.sh | 2 +- transport-helper.c | 64 +- transport.c | 62 +- 14 files changed, 2521 insertions(+), 66 deletions(-) create mode 100644 t/helper/test-proc-receive.c create mode 100755 t/t5411-proc-receive-hook.sh create mode 100755 t/t5412-proc-receive-hook-http-protocol.sh
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
v9 cover New proc-receive hook for centralized workflow
From: Jiang Xin <[email protected]>
## Changes since v8
* One test case failed in CI for pu branch (see:
https://github.com/git/git/runs/565992235) with the following error:
--- expect 2020-04-06 23:41:27.552286900 +0000
+++ actual 2020-04-06 23:41:27.458511100 +0000
@@ -3,6 +3,7 @@
remote: # proc-receive hook
remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic
+remote: fatal: unable to write flush packet: Broken pipe
remote: error: proc-receive expected "old new ref status [msg]", got "<ZERO-OID> <COMMIT-A> refs/for/master/topic" To <URL/of/upstream.git>
! [remote rejected] HEAD -> refs/for/master/topic (fail to run proc-receive hook)
error: last command exited with $?=1
not ok 16 - proc-receive bad protocol: no status
This can be reproduced with the following changes on "t/helper/test-proc-receive.c":
+++ b/t/helper/test-proc-receive.c
@@ -2,6 +2,7 @@
#include "connect.h"
#include "parse-options.h"
#include "pkt-line.h"
+#include "sigchain.h"
#include "string-list.h"
#include "test-tool.h"
@@ -130,6 +131,7 @@ int cmd__proc_receive(int argc, const char **argv)
PACKET_READ_CHOMP_NEWLINE |
PACKET_READ_DIE_ON_ERR_PACKET);
+ sigchain_push(SIGPIPE, SIG_IGN);
proc_receive_verison(&reader);
proc_receive_read_commands(&reader, &commands);
proc_receive_read_push_options(&reader, &push_options);
@@ -163,10 +165,14 @@ int cmd__proc_receive(int argc, const char **argv)
}
if (returns.nr) {
- for_each_string_list_item(item, &returns)
+ for_each_string_list_item(item, &returns) {
packet_write_fmt(1, "%s\n", item->string);
+ sleep(1);
+ }
}
packet_flush(1);
+ sigchain_pop(SIGPIPE);
return 0;
}"test-proc-receive" send results one by one to "receive-pack" and send a flush-pkt to end the communication. But "receive-pack" will close the pipe, if a syntax error is found in the result sent from "test-proc-receive". The closed pipe makes "test-proc-receive" complain.
Make modifications in patch 2/6 of this reroll, so we don't have to adjust test cases. E.g.,
/* receive-pack */
static int read_proc_receive_result(...)
{
... ...
for (;;) {
... ...
if (packet_reader_read(reader) != PACKET_READ_NORMAL)
break;
if (parse_oid_hex(reader->line, &old_oid, &p) ||
*p++ != ' ' ||
parse_oid_hex(p, &new_oid, &p) ||
*p++ != ' ') {
strbuf_addf(errmsg, "proc-receive expected 'old new ref status [msg]', got '%s'\n",
reader->line);
- return -1;
+ code = -1;
+ continue;
}* Patch 1/6: Add t/t5411/common-functions.sh and t/t5411/common-test-cases.sh to reuse test cases between t5411 and t5412.
* Refactor for a smaller patch 5/6 (receive-pack: refactor report for proc-receive).
## Range-diff v8...v9
1: 19c66785d1 ! 1: ba6222899b transport: not report a non-head push as a branch
t/t5411-proc-receive-hook.sh | 75 ++++++++++++++++++++++++++
t/t5411/common-functions.sh | 49 +++++++++++++++++
t/t5411/common-test-cases.sh | 43 +++++++++++++++
t/t5412-proc-receive-hook-http-protocol.sh | 86 ++++++++++++++++++++++++++++++2: 085ded61f5 ! 2: 195c5b0a0c receive-pack: add new proc-receive hook
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ *p++ != ' ' ||
+ parse_oid_hex(p, &new_oid, &p) ||
+ *p++ != ' ') {
-+ strbuf_addf(errmsg, "proc-receive expected 'old new ref status [msg]', got '%s'",
++ strbuf_addf(errmsg, "proc-receive expected 'old new ref status [msg]', got '%s'\n",
+ reader->line);
-+ return -1;
++ code = -1;
++ continue;
+ }
+
+ refname = p;
+ status = strchr(p, ' ');
+ if (!status) {
-+ strbuf_addf(errmsg, "proc-receive expected 'old new ref status [msg]', got '%s'",
++ strbuf_addf(errmsg, "proc-receive expected 'old new ref status [msg]', got '%s'\n",
+ reader->line);
-+ return -1;
++ code = -1;
++ continue;
+ }
+ *status++ = '\0';
+ if (strlen(status) > 2 && *(status + 2) == ' ') {
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ *msg++ = '\0';
+ }
+ if (strlen(status) != 2) {
-+ strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'",
++ strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'\n",
+ status, reader->line);
-+ return -1;
++ code = -1;
++ continue;
+ }
+
+ /* first try searching at our hint, falling back to all refs */
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ refname);
+ continue;
+ }
-+ hint->run_proc_receive |= RUN_PROC_RECEIVE_RETURNED;
-+ oidcpy(&hint->old_oid, &old_oid);
-+ oidcpy(&hint->new_oid, &new_oid);
+ if (!strcmp(status, "ng")) {
+ if (msg)
+ hint->error_string = xstrdup(msg);
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ hint->error_string = "failed";
+ code = -1;
+ } else if (strcmp("ok", status)) {
-+ strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'",
++ strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'\n",
+ status, reader->line);
-+ return -1;
++ code = -1;
++ /* Skip marking it as RUN_PROC_RECEIVE_RETURNED */
++ continue;
+ }
++ oidcpy(&hint->old_oid, &old_oid);
++ oidcpy(&hint->new_oid, &new_oid);
++ hint->run_proc_receive |= RUN_PROC_RECEIVE_RETURNED;
+ }
+
+ for (cmd = commands; cmd; cmd = cmd->next)
@@ t/helper/test-proc-receive.c (new)
+#include "connect.h"
+#include "parse-options.h"
+#include "pkt-line.h"
++#include "sigchain.h"
+#include "string-list.h"
+#include "test-tool.h"
+
@@ t/helper/test-proc-receive.c (new)
+ PACKET_READ_CHOMP_NEWLINE |
+ PACKET_READ_DIE_ON_ERR_PACKET);
+
++ sigchain_push(SIGPIPE, SIG_IGN);
+ proc_receive_verison(&reader);
+ proc_receive_read_commands(&reader, &commands);
+ proc_receive_read_push_options(&reader, &push_options);
@@ t/helper/test-proc-receive.c (new)
+ packet_write_fmt(1, "%s\n", item->string);
+ }
+ packet_flush(1);
++ sigchain_pop(SIGPIPE);
+
+ return 0;
+}3: 230f28198f = 3: cde556e9c7 refs.c: refactor to reuse ref_is_hidden()
4: e6a7608a84 ! 4: 3200327695 receive-pack: new config receive.procReceiveRefs
@@ Documentation/config/receive.txt: receive.hideRefs::
+ used, and all commands will be executed by the internal
+ execute_commands function.
+
-+ For example, if this variable is set to "refs/for/", pushing to
++ For example, if this variable is set to "refs/for", pushing to
+ reference such as "refs/for/master" will not create or update a
+ reference named "refs/for/master", but may create or update a
+ pull request directly by running an external hook.
5: e426775925 ! 5: 5f2ab02b01 receive-pack: refactor report for proc-receive
@@ builtin/receive-pack.c: static int read_proc_receive_result(struct packet_reader
+ /* Reset "run_proc_receive" field, and continue to run in "receive-pack" */
+ hint->run_proc_receive = 0;
+ } else {
- strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'",
+ strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'\n",
status, reader->line);
- return -1;
+ code = -1;
@@ builtin/receive-pack.c: static int read_proc_receive_result(struct packet_reader *reader,
+ }
+ oidcpy(&hint->old_oid, &old_oid);
+ oidcpy(&hint->new_oid, &new_oid);
+- hint->run_proc_receive |= RUN_PROC_RECEIVE_RETURNED;
++ if (hint->run_proc_receive)
++ hint->run_proc_receive |= RUN_PROC_RECEIVE_RETURNED;
+ }
+
+ for (cmd = commands; cmd; cmd = cmd->next)
@@ transport.c: void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int v
- }
- }
-
--static void print_ref_status(char flag, const char *summary,
-+static void print_ref_status(char flag, const char *summary, char *target_refname,
+@@ transport.c: static void print_ref_status(char flag, const char *summary,
struct ref *to, struct ref *from, const char *msg,
int porcelain, int summary_width)
{
-+ if (!target_refname)
-+ target_refname = to->name;
++ char *from_name = NULL;
++ char *to_name = NULL;
++
++ if (from) {
++ if (from->remote_status && !strncmp(from->remote_status, "ref:", 4))
++ from_name = from->remote_status + 4;
++ else
++ from_name = from->name;
++ }
++
++ if (to) {
++ if (to->remote_status && !strncmp(to->remote_status, "ref:", 4))
++ to_name = to->remote_status + 4;
++ else
++ to_name = to->name;
++ }
+
if (porcelain) {
if (from)
- fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
-+ fprintf(stdout, "%c\t%s:%s\t", flag, from->name, target_refname);
++ fprintf(stdout, "%c\t%s:%s\t", flag, from_name, to_name);
else
- fprintf(stdout, "%c\t:%s\t", flag, to->name);
-+ fprintf(stdout, "%c\t:%s\t", flag, target_refname);
++ fprintf(stdout, "%c\t:%s\t", flag, to_name);
if (msg)
fprintf(stdout, "%s (%s)\n", summary, msg);
else
@@ transport.c: static void print_ref_status(char flag, const char *summary,
summary, reset);
if (from)
- fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
-+ fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(target_refname));
++ fprintf(stderr, "%s -> %s", prettify_refname(from_name), prettify_refname(to_name));
else
- fputs(prettify_refname(to->name), stderr);
-+ fputs(prettify_refname(target_refname), stderr);
++ fputs(prettify_refname(to_name), stderr);
if (msg) {
fputs(" (", stderr);
fputs(msg, stderr);
-@@ transport.c: static void print_ref_status(char flag, const char *summary,
-
- static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width)
- {
-+ char *refname;
-+
-+ if (ref->remote_status && !strncmp(ref->remote_status, "ref:", 4))
-+ refname = ref->remote_status + 4;
-+ else
-+ refname = ref->name;
-+
- if (ref->deletion)
-- print_ref_status('-', "[deleted]", ref, NULL, NULL,
-+ print_ref_status('-', "[deleted]", refname, ref, NULL, NULL,
- porcelain, summary_width);
-- else if (is_null_oid(&ref->old_oid))
-+ else if (is_null_oid(&ref->old_oid)) {
-+
- print_ref_status('*',
-- (starts_with(ref->name, "refs/tags/")
-+ (starts_with(refname, "refs/tags/")
- ? "[new tag]"
-- : (starts_with(ref->name, "refs/heads/")
-+ : (starts_with(refname, "refs/heads/")
- ? "[new branch]"
- : "[new reference]")),
-- ref, ref->peer_ref, NULL, porcelain, summary_width);
-- else {
-+ refname, ref, ref->peer_ref, NULL, porcelain, summary_width);
-+ } else {
- struct strbuf quickref = STRBUF_INIT;
- char type;
- const char *msg;
-@@ transport.c: static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widt
- strbuf_add_unique_abbrev(&quickref, &ref->new_oid,
- DEFAULT_ABBREV);
-
-- print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
-+ print_ref_status(type, quickref.buf, refname, ref, ref->peer_ref, msg,
- porcelain, summary_width);
- strbuf_release(&quickref);
- }
-@@ transport.c: static int print_one_push_status(struct ref *ref, const char *dest, int count,
-
- switch(ref->status) {
- case REF_STATUS_NONE:
-- print_ref_status('X', "[no match]", ref, NULL, NULL,
-+ print_ref_status('X', "[no match]", NULL, ref, NULL, NULL,
- porcelain, summary_width);
- break;
- case REF_STATUS_REJECT_NODELETE:
-- print_ref_status('!', "[rejected]", ref, NULL,
-+ print_ref_status('!', "[rejected]", NULL, ref, NULL,
- "remote does not support deleting refs",
- porcelain, summary_width);
- break;
- case REF_STATUS_UPTODATE:
-- print_ref_status('=', "[up to date]", ref,
-+ print_ref_status('=', "[up to date]", NULL, ref,
- ref->peer_ref, NULL, porcelain, summary_width);
- break;
- case REF_STATUS_REJECT_NONFASTFORWARD:
-- print_ref_status('!', "[rejected]", ref, ref->peer_ref,
-+ print_ref_status('!', "[rejected]", NULL, ref, ref->peer_ref,
- "non-fast-forward", porcelain, summary_width);
- break;
- case REF_STATUS_REJECT_ALREADY_EXISTS:
-- print_ref_status('!', "[rejected]", ref, ref->peer_ref,
-+ print_ref_status('!', "[rejected]", NULL, ref, ref->peer_ref,
- "already exists", porcelain, summary_width);
- break;
- case REF_STATUS_REJECT_FETCH_FIRST:
-- print_ref_status('!', "[rejected]", ref, ref->peer_ref,
-+ print_ref_status('!', "[rejected]", NULL, ref, ref->peer_ref,
- "fetch first", porcelain, summary_width);
- break;
- case REF_STATUS_REJECT_NEEDS_FORCE:
-- print_ref_status('!', "[rejected]", ref, ref->peer_ref,
-+ print_ref_status('!', "[rejected]", NULL, ref, ref->peer_ref,
- "needs force", porcelain, summary_width);
- break;
- case REF_STATUS_REJECT_STALE:
-- print_ref_status('!', "[rejected]", ref, ref->peer_ref,
-+ print_ref_status('!', "[rejected]", NULL, ref, ref->peer_ref,
- "stale info", porcelain, summary_width);
- break;
- case REF_STATUS_REJECT_SHALLOW:
-- print_ref_status('!', "[rejected]", ref, ref->peer_ref,
-+ print_ref_status('!', "[rejected]", NULL, ref, ref->peer_ref,
- "new shallow roots not allowed",
- porcelain, summary_width);
- break;
- case REF_STATUS_REMOTE_REJECT:
-- print_ref_status('!', "[remote rejected]", ref,
-+ print_ref_status('!', "[remote rejected]", NULL, ref,
- ref->deletion ? NULL : ref->peer_ref,
- ref->remote_status, porcelain, summary_width);
- break;
- case REF_STATUS_EXPECTING_REPORT:
-- print_ref_status('!', "[remote failure]", ref,
-+ print_ref_status('!', "[remote failure]", NULL, ref,
- ref->deletion ? NULL : ref->peer_ref,
- "remote failed to report status",
- porcelain, summary_width);
- break;
- case REF_STATUS_ATOMIC_PUSH_FAILED:
-- print_ref_status('!', "[rejected]", ref, ref->peer_ref,
-+ print_ref_status('!', "[rejected]", NULL, ref, ref->peer_ref,
- "atomic push failed", porcelain, summary_width);
- break;
- case REF_STATUS_OK:6: c5982067be < -: ---------- t5412: test the proc-receive hook on HTTP protocol
7: 823b7f2ea6 = 6: b7d7175d89 doc: add documentation for the proc-receive hook
---
Jiang Xin (6):
transport: not report a non-head push as a branch
receive-pack: add new proc-receive hook refs.c: refactor to reuse ref_is_hidden()
receive-pack: new config receive.procReceiveRefs
receive-pack: refactor report for proc-receive
doc: add documentation for the proc-receive hook
Documentation/config/receive.txt | 14 + Documentation/githooks.txt | 70 ++ Makefile | 1 + builtin/receive-pack.c | 318 +++++++- refs.c | 11 +- refs.h | 1 + t/helper/test-proc-receive.c | 175 +++++ t/helper/test-tool.c | 1 + t/helper/test-tool.h | 1 + t/t5411-proc-receive-hook.sh | 75 ++ t/t5411/common-functions.sh | 53 ++ t/t5411/common-test-cases.sh | 827 +++++++++++++++++++++ t/t5412-proc-receive-hook-http-protocol.sh | 86 +++ t/t5516-fetch-push.sh | 2 +- transport-helper.c | 64 +- transport.c | 34 +- 16 files changed, 1684 insertions(+), 49 deletions(-) create mode 100644 t/helper/test-proc-receive.c create mode 100755 t/t5411-proc-receive-hook.sh create mode 100644 t/t5411/common-functions.sh create mode 100644 t/t5411/common-test-cases.sh create mode 100755 t/t5412-proc-receive-hook-http-protocol.sh
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
v10 cover New proc-receive hook for centralized workflow
From: Jiang Xin <[email protected]>
## Changes since v9
1. Minor refactors on patch 1/8, 2/8, 4/8, and 8/8.
2. New patch 5/8: export function parse_feature_value().
3. Split patch 5/6 of reroll v9 into two patches (6/8 and 7/8), one for the server-side and one for the client-side. so can make a compatible test on old version of a Git client.
4. Patch 6/8: Use null character to split extended status of the report line, and add other key-value pairs for extension. For example:
When we push to a pseudo reference using the following command:
git push origin HEAD:refs/for/master/topic
Even if the push command updates an existing pull request (such as refs/pull/123/head) instead of creating a new one, git push always give the following report:
To <URL/of/upstream.git>
* [new reference] HEAD -> refs/pull/123/head
After add more key-value pairs in the extended status, such as:
'old-oid=<...>', 'forced-update', we get a nicer status report like:
To upstream.git
+ 1234567...7654321 HEAD -> refs/pull/124/head (forced update)
## Range-diff v9...v10
1: ba6222899b ! 1: a293077bcf transport: not report a non-head push as a branch
@@ t/t5411-proc-receive-hook.sh (new)
+
+# Format the output of git-push, git-show-ref and other commands to make a
+# user-friendly and stable text. In addition to the common format method,
-+# we also replace URL of different protocol for the upstream repository to
-+# a fixed pattern.
++# we also replace the URL of different protocol for the upstream repository
++# with a fixed pattern.
+make_user_friendly_and_stable_output () {
+ make_user_friendly_and_stable_output_common | sed \
+ -e "s#To ../upstream.git#To <URL/of/upstream.git>#"
@@ t/t5411/common-functions.sh (new)
+ -e "s/$B/<COMMIT-B>/g" \
+ -e "s/$TAG/<TAG-v123>/g" \
+ -e "s/$ZERO_OID/<ZERO-OID>/g" \
-+ -e "s/[0-9a-f]\{7,\}/<OID>/g"
++ -e "s/$(echo $A | cut -c1-7)[0-9a-f]*/<OID-A>/g" \
++ -e "s/$(echo $B | cut -c1-7)[0-9a-f]*/<OID-B>/g"
+}
## t/t5411/common-test-cases.sh (new) ##
@@ t/t5411/common-test-cases.sh (new)
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/review/master/topic
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/a/b/c
+ To <URL/of/upstream.git>
-+ + <OID>...<OID> HEAD -> master (forced update)
++ + <OID-B>...<OID-A> HEAD -> master (forced update)
+ - [deleted] next
+ * [new tag] v123 -> v123
+ * [new reference] HEAD -> refs/review/master/topic
@@ t/t5412-proc-receive-hook-http-protocol.sh (new)
+
+# Format the output of git-push, git-show-ref and other commands to make a
+# user-friendly and stable text. In addition to the common format method,
-+# we also replace URL of different protocol for the upstream repository to
-+# a fixed pattern.
++# we also replace the URL of different protocol for the upstream repository
++# with a fixed pattern.
+make_user_friendly_and_stable_output () {
+ make_user_friendly_and_stable_output_common | sed \
+ -e "s#To http:.*/upstream.git#To <URL/of/upstream.git>#"
2: 195c5b0a0c ! 2: 915b9ef616 receive-pack: add new proc-receive hook
@@ Commit message
format. In the following example, The letter "S" stands for
"receive-pack" and letter "H" stands for the hook.
- # Version and capabilities negotiation.
+ # Version and features negotiation.
S: PKT-LINE(version=1\0push-options atomic...)
S: flush-pkt
H: PKT-LINE(version=1\0push-options...)
H: flush-pkt
# Send commands from server to the hook.
- S: PKT-LINE(old-oid new-oid ref)
+ S: PKT-LINE(<old-oid> <new-oid> <ref>)
S: ... ...
S: flush-pkt
- # Only if push-options have been negotiated.
+ # Send push-options only if the 'push-options' feature is enabled.
S: PKT-LINE(push-option)
S: ... ...
S: flush-pkt
# Receive result from the hook.
# OK, run this command successfully.
- H: PKT-LINE(old-oid new-oid ref ok)
+ H: PKT-LINE(<old-oid> <new-oid> <ref> ok)
# NO, I reject it.
- H: PKT-LINE(old-oid new-oid ref ng reason)
- # OK, but use an alternate reference. (in latter commit)
- H: PKT-LINE(old-oid new-oid ref ok ref:alt-ref)
- # It will fallthrough to receive-pack to execute. (in latter commit)
- H: PKT-LINE(old-oid new-oid ref ft)
+ H: PKT-LINE(<old-oid> <new-oid> <ref> ng <reason>)
+ # Fall through, let 'receive-pack' to execute it.
+ H: PKT-LINE(<old-oid> <new-oid> <ref> ft)
+ # OK, but has an alternate reference. The alternate reference name
+ # and other status are given in key=value pairs after the null
+ # character.
+ H: PKT-LINE(<old-oid> <new-oid> <ref> ok\0ref=refs/pull/123/head
+ forced-update)
H: ... ...
H: flush-pkt
- After receiving a command, the hook can create/update another alternate
- reference. For example, a command for a reference "refs/for/master" may
- create a special reference, such as "refs/pull/123/head". The alternate
- reference can be returned from the result in an extensible format like
- "<old-oid> <new-oid> <reference> <status> [<message>]".
+ After receiving a command, the hook will execute the command, and may
+ create/update different reference. For example, a command for a pseudo
+ reference "refs/for/master/topic" may create/update different reference
+ such as "refs/pull/123/head". The alternate reference name and other
+ status are given in key-value pairs as extended status of the report
+ line.
- The result will be stored in a command list, and "receive-pack" will use
- the result to replace the commands that have specific `run_proc_receive`
- field turned on.
+ The list of commands returned from "proc-receive" will replace the
+ relevant commands that are sent from user to "receive-pack", and
+ "receive-pack" will continue to run the "execute_commands" function and
+ other routines. Finally, the result of the execution of these commands
+ will be reported to end user.
Suggested-by: Junio C Hamano <[email protected]>
Signed-off-by: Jiang Xin <[email protected]>
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+static struct command *find_command_by_refname(const struct command *list,
+ const char *refname)
+{
-+ for ( ; list; list = list->next)
++ for (; list; list = list->next)
+ if (!strcmp(list->ref_name, refname))
+ return (struct command *)list;
+ return NULL;
+}
+
-+static int read_proc_receive_result(struct packet_reader *reader,
++static int read_proc_receive_report(struct packet_reader *reader,
+ struct command *commands,
+ struct strbuf *errmsg)
+{
-+ struct command *hint;
+ struct command *cmd;
++ struct command *hint = NULL;
+ int code = 0;
+
-+ hint = NULL;
+ for (;;) {
+ struct object_id old_oid, new_oid;
+ const char *refname;
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ }
+
+ for (cmd = commands; cmd; cmd = cmd->next)
-+ if (cmd->run_proc_receive &&
++ if (cmd->run_proc_receive && !cmd->error_string &&
+ !(cmd->run_proc_receive & RUN_PROC_RECEIVE_RETURNED)) {
+ cmd->error_string = "no report from proc-receive";
+ code = -1;
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+
+ /* Send commands */
+ for (cmd = commands; cmd; cmd = cmd->next) {
-+ char *old_hex, *new_hex;
-+
+ if (!cmd->run_proc_receive || cmd->skip_update || cmd->error_string)
+ continue;
-+ old_hex = oid_to_hex(&cmd->old_oid);
-+ new_hex = oid_to_hex(&cmd->new_oid);
+ packet_write_fmt(proc.in, "%s %s %s",
-+ old_hex, new_hex, cmd->ref_name);
++ oid_to_hex(&cmd->old_oid),
++ oid_to_hex(&cmd->new_oid),
++ cmd->ref_name);
+ }
+ packet_flush(proc.in);
+
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ }
+
+ /* Read result from proc-receive */
-+ code = read_proc_receive_result(&reader, commands, &errmsg);
++ code = read_proc_receive_report(&reader, commands, &errmsg);
+
+cleanup:
+ close(proc.in);
@@ builtin/receive-pack.c: static void execute_commands(struct command *commands,
free(head_name_to_free);
head_name = head_name_to_free = resolve_refdup("HEAD", 0, NULL, NULL);
-+ if (run_proc_receive) {
-+ int code;
-+
-+ code = run_proc_receive_hook(commands, push_options);
-+ if (code) {
-+ for (cmd = commands; cmd; cmd = cmd->next) {
-+ if (!cmd->error_string && (cmd->run_proc_receive || use_atomic))
-+ cmd->error_string = "fail to run proc-receive hook";
-+ }
-+ }
-+ }
++ if (run_proc_receive &&
++ run_proc_receive_hook(commands, push_options))
++ for (cmd = commands; cmd; cmd = cmd->next)
++ if (!cmd->error_string && (cmd->run_proc_receive || use_atomic))
++ cmd->error_string = "fail to run proc-receive hook";
+
if (use_atomic)
execute_commands_atomic(commands, si);
@@ t/helper/test-proc-receive.c (new)
+ const char *refname;
+ const char *p;
+
-+ if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
++ if (packet_reader_read(reader) != PACKET_READ_NORMAL)
+ break;
-+ }
+
+ if (parse_oid_hex(reader->line, &old_oid, &p) ||
+ *p++ != ' ' ||
@@ t/helper/test-proc-receive.c (new)
+int cmd__proc_receive(int argc, const char **argv)
+{
+ struct packet_reader reader;
-+ struct command *commands;
++ struct command *commands = NULL;
+ struct string_list push_options = STRING_LIST_INIT_DUP;
+ struct string_list_item *item;
+ struct option options[] = {
@@ t/helper/test-proc-receive.c (new)
+ use_atomic? " atomic": "",
+ use_push_options ? " push_options": "");
+
-+ for (cmd = commands; cmd; cmd = cmd->next) {
-+ char *old_hex, *new_hex;
-+
-+ old_hex = oid_to_hex(&cmd->old_oid);
-+ new_hex = oid_to_hex(&cmd->new_oid);
++ for (cmd = commands; cmd; cmd = cmd->next)
+ fprintf(stderr, "proc-receive< %s %s %s\n",
-+ old_hex, new_hex, cmd->ref_name);
-+ }
++ oid_to_hex(&cmd->old_oid),
++ oid_to_hex(&cmd->new_oid),
++ cmd->ref_name);
+
-+ if (push_options.nr > 0) {
++ if (push_options.nr > 0)
+ for_each_string_list_item(item, &push_options)
+ fprintf(stderr, "proc-receive< %s\n", item->string);
-+ }
+
-+ if (returns.nr) {
++ if (returns.nr)
+ for_each_string_list_item(item, &returns)
+ fprintf(stderr, "proc-receive> %s\n", item->string);
-+ }
+ }
+
-+ if (returns.nr) {
++ if (returns.nr)
+ for_each_string_list_item(item, &returns)
+ packet_write_fmt(1, "%s\n", item->string);
-+ }
+ packet_flush(1);
+ sigchain_pop(SIGPIPE);
+
@@ t/t5411/common-functions.sh: create_commits_in () {
sed \
-e "s/ *\$//" \
@@ t/t5411/common-functions.sh: make_user_friendly_and_stable_output_common () {
- -e "s/$B/<COMMIT-B>/g" \
-e "s/$TAG/<TAG-v123>/g" \
-e "s/$ZERO_OID/<ZERO-OID>/g" \
-- -e "s/[0-9a-f]\{7,\}/<OID>/g"
-+ -e "s/[0-9a-f]\{7,\}/<OID>/g" \
+ -e "s/$(echo $A | cut -c1-7)[0-9a-f]*/<OID-A>/g" \
+- -e "s/$(echo $B | cut -c1-7)[0-9a-f]*/<OID-B>/g"
++ -e "s/$(echo $B | cut -c1-7)[0-9a-f]*/<OID-B>/g" \
+ -e "/^error: / d"
}
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+ remote: # post-receive hook
+ remote: post-receive< <COMMIT-A> <COMMIT-B> refs/heads/master
+ To <URL/of/upstream.git>
-+ <OID>..<OID> <COMMIT-B> -> master
++ <OID-A>..<OID-B> <COMMIT-B> -> master
+ ! [remote rejected] HEAD -> refs/for/master/topic (no report from proc-receive)
+ EOF
+ test_cmp expect actual &&
3: cde556e9c7 = 3: 16257616ec refs.c: refactor to reuse ref_is_hidden()
4: 3200327695 ! 4: cd49b57c17 receive-pack: new config receive.procReceiveRefs @@ Commit message
Add a new multi-valued config variable "receive.procReceiveRefs" for receive-pack command, like the follows:
- git config --system --add receive.procReceiveRefs refs/for/
- git config --system --add receive.procReceiveRefs refs/drafts/
+ git config --system --add receive.procReceiveRefs refs/for
+ git config --system --add receive.procReceiveRefs refs/drafts
If the specific prefix strings match the reference names of the commands
- which are sent by git client to receive-pack, these commands will be
+ which are sent from git client to receive-pack, these commands will be
executed by an external hook (named "proc-receive"), instead of the internal execute_commands function.
- For example, if it is set to "refs/for/", pushing to a reference such as
+ For example, if it is set to "refs/for", pushing to a reference such as
"refs/for/master" will not create or update reference "refs/for/master",
- but may create or update a pull request directly by running the external
- hook.
+ but may create or update a pull request directly by running the hook
+ "proc-receive".
@@ Documentation/config/receive.txt: receive.hideRefs::
+receive.procReceiveRefs::
+ This is a multi-valued variable that defines reference prefixes
+ to match the commands in receive-pack. Commands matching the
-+ prefixes will be executed by an external hooks "proc-receive",
++ prefixes will be executed by an external hook "proc-receive",
+ instead of the internal execute_commands function. If this
+ variable is not defined, the "proc-receive" hook will never be
+ used, and all commands will be executed by the internal
@@ Documentation/config/receive.txt: receive.hideRefs::
+ For example, if this variable is set to "refs/for", pushing to
+ reference such as "refs/for/master" will not create or update a
+ reference named "refs/for/master", but may create or update a
-+ pull request directly by running an external hook.
++ pull request directly by running the hook "proc-receive".
+
receive.updateServerInfo::
If set to true, git-receive-pack will run git-update-server-info
@@ t/t5411/common-test-cases.sh: test_expect_success "push with options" '
+ test-tool proc-receive -v \
+ -r "$ZERO_OID $A refs/review/a/b/c/topic ok" \
+ -r "$ZERO_OID $A refs/for/next/topic ok" \
-+ -r "$ZERO_OID $A refs/for/master/topic ok"
++ -r "$B $A refs/for/master/topic ok"
+ EOF
+ chmod a+x "$upstream/hooks/proc-receive"
+'
@@ t/t5411/common-test-cases.sh: test_expect_success "push with options" '
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/review/a/b/c/topic ok
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/next/topic ok
-+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic ok
++ remote: proc-receive> <COMMIT-B> <COMMIT-A> refs/for/master/topic ok
+ remote: # post-receive hook
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/review/a/b/c/topic
-+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: post-receive< <COMMIT-B> <COMMIT-A> refs/for/master/topic
+ To <URL/of/upstream.git>
+ * [new reference] HEAD -> refs/for/next/topic
+ * [new reference] HEAD -> refs/review/a/b/c/topic
@@ t/t5411/common-test-cases.sh: test_expect_success "push with options" '
+
+ test-tool proc-receive -v \
+ -r "$ZERO_OID $A refs/for/next/topic ok" \
-+ -r "$ZERO_OID $A refs/for/master/topic ok"
++ -r "$A $B refs/for/master/topic ok"
+ EOF
+ chmod a+x "$upstream/hooks/proc-receive"
+'
@@ t/t5411/common-test-cases.sh: test_expect_success "push with options" '
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/next/topic ok
-+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic ok
++ remote: proc-receive> <COMMIT-A> <COMMIT-B> refs/for/master/topic ok
+ remote: # post-receive hook
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/bar
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/baz
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/foo
-+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/master/topic
+ To <URL/of/upstream.git>
+ * [new branch] HEAD -> bar
+ * [new branch] HEAD -> baz
5: 5f2ab02b01 < -: ---------- receive-pack: refactor report for proc-receive 6: b7d7175d89 < -: ---------- doc: add documentation for the proc-receive hook -: ---------- > 5: 75b178fc19 connect: export parse_feature_value() -: ---------- > 6: 860860536e receive-pack: extension for server-side report -: ---------- > 7: d1677b61d1 send-pack: extension for client-side status report -: ---------- > 8: 41159c2ef2 doc: add documentation for the proc-receive hook
Jiang Xin (8):
transport: not report a non-head push as a branch
receive-pack: add new proc-receive hook refs.c: refactor to reuse ref_is_hidden()
receive-pack: new config receive.procReceiveRefs
connect: export parse_feature_value()
receive-pack: extension for server-side report
send-pack: extension for client-side status report
doc: add documentation for the proc-receive hook
Documentation/config/receive.txt | 14 + Documentation/githooks.txt | 58 ++ Makefile | 1 + builtin/receive-pack.c | 334 ++++++++- connect.c | 3 +- connect.h | 1 + refs.c | 11 +- refs.h | 1 + remote.c | 28 + remote.h | 3 + send-pack.c | 13 +- t/helper/test-proc-receive.c | 178 +++++ t/helper/test-tool.c | 1 + t/helper/test-tool.h | 1 + t/t5411-proc-receive-hook.sh | 75 ++ t/t5411/common-functions.sh | 54 ++ t/t5411/common-test-cases.sh | 827 +++++++++++++++++++++ t/t5412-proc-receive-hook-http-protocol.sh | 86 +++ t/t5516-fetch-push.sh | 2 +- transport-helper.c | 66 +- transport.c | 42 +- 21 files changed, 1745 insertions(+), 54 deletions(-) create mode 100644 t/helper/test-proc-receive.c create mode 100755 t/t5411-proc-receive-hook.sh create mode 100644 t/t5411/common-functions.sh create mode 100644 t/t5411/common-test-cases.sh create mode 100755 t/t5412-proc-receive-hook-http-protocol.sh
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
v11 cover New proc-receive hook for centralized workflow
From: Jiang Xin <[email protected]>
## Changes since v10
* Patch 3/7 of v11 is a squash commit of patch 2/8 and patch 6/8 of v10.
* Fixed issues discovered for code review of v10.
## Range-diff v10...v11
1: a293077bcf ! 1: 77a731c151 transport: not report a non-head push as a branch
@@ t/t5411-proc-receive-hook.sh (new)
+# we also replace the URL of different protocol for the upstream repository
+# with a fixed pattern.
+make_user_friendly_and_stable_output () {
-+ make_user_friendly_and_stable_output_common | sed \
-+ -e "s#To ../upstream.git#To <URL/of/upstream.git>#"
++ make_user_friendly_and_stable_output_common |
++ sed -e "s#To ../upstream.git#To <URL/of/upstream.git>#"
+}
+
+# Refs of upstream : master(B) next(A)
@@ t/t5411-proc-receive-hook.sh (new)
+ TAG=$(git -C workbench rev-parse v123) &&
+
+ # setup pre-receive hook
-+ cat >upstream.git/hooks/pre-receive <<-\EOF &&
-+ #!/bin/sh
-+
-+ echo >&2 "# pre-receive hook"
-+
++ write_script upstream.git/hooks/pre-receive <<-\EOF &&
++ exec >&2
++ echo "# pre-receive hook"
+ while read old new ref
+ do
-+ echo >&2 "pre-receive< $old $new $ref"
++ echo "pre-receive< $old $new $ref"
+ done
+ EOF
+
+ # setup post-receive hook
-+ cat >upstream.git/hooks/post-receive <<-\EOF &&
-+ #!/bin/sh
-+
-+ echo >&2 "# post-receive hook"
-+
++ write_script upstream.git/hooks/post-receive <<-\EOF &&
++ exec >&2
++ echo "# post-receive hook"
+ while read old new ref
+ do
-+ echo >&2 "post-receive< $old $new $ref"
++ echo "post-receive< $old $new $ref"
+ done
+ EOF
+
-+ chmod a+x \
-+ upstream.git/hooks/pre-receive \
-+ upstream.git/hooks/post-receive &&
-+
+ upstream=upstream.git
+'
+
@@ t/t5411/common-functions.sh (new)
+# user-friendly and stable text. We can easily prepare the expect text
+# without having to worry about future changes of the commit ID and spaces
+# of the output. We also replce single quotes with double quotes, because
-+# it is boring to prepare unquoted single quotes in expect txt.
++# it is boring to prepare unquoted single quotes in expect text.
+make_user_friendly_and_stable_output_common () {
+ sed \
+ -e "s/ *\$//" \
@@ t/t5412-proc-receive-hook-http-protocol.sh (new)
+# we also replace the URL of different protocol for the upstream repository
+# with a fixed pattern.
+make_user_friendly_and_stable_output () {
-+ make_user_friendly_and_stable_output_common | sed \
-+ -e "s#To http:.*/upstream.git#To <URL/of/upstream.git>#"
++ make_user_friendly_and_stable_output_common |
++ sed -e "s#To http:.*/upstream.git#To <URL/of/upstream.git>#"
+}
+
+# Refs of upstream : master(B) next(A)
@@ t/t5412-proc-receive-hook-http-protocol.sh (new)
+ TAG=$(git -C workbench rev-parse v123) &&
+
+ # setup pre-receive hook
-+ cat >upstream.git/hooks/pre-receive <<-\EOF &&
-+ #!/bin/sh
-+
-+ echo >&2 "# pre-receive hook"
-+
++ write_script upstream.git/hooks/pre-receive <<-\EOF &&
++ exec >&2
++ echo "# pre-receive hook"
+ while read old new ref
+ do
-+ echo >&2 "pre-receive< $old $new $ref"
++ echo "pre-receive< $old $new $ref"
+ done
+ EOF
+
+ # setup post-receive hook
-+ cat >upstream.git/hooks/post-receive <<-\EOF &&
-+ #!/bin/sh
-+
-+ echo >&2 "# post-receive hook"
-+
++ write_script upstream.git/hooks/post-receive <<-\EOF &&
++ exec >&2
++ echo "# post-receive hook"
+ while read old new ref
+ do
-+ echo >&2 "post-receive< $old $new $ref"
++ echo "post-receive< $old $new $ref"
+ done
+ EOF
+
-+ chmod a+x \
-+ upstream.git/hooks/pre-receive \
-+ upstream.git/hooks/post-receive &&
-+
+ upstream="$HTTPD_DOCUMENT_ROOT_PATH/upstream.git" &&
+ mv upstream.git "$upstream" &&
+ git -C workbench remote set-url origin $HTTPD_URL/smart/upstream.git
5: 75b178fc19 = 2: 4c4f7cffff connect: export parse_feature_value()
2: 915b9ef616 ! 3: bef4360e58 receive-pack: add new proc-receive hook
@@ Commit message
other routines. Finally, the result of the execution of these commands
will be reported to end user.
+ The reporting function from "receive-pack" to "send-pack" is also
+ extended using a backward compatible way by adding key-value pairs
+ after an null character, like:
+
+ # OK, run this command successfully with optional extended-status.
+ ok <reference>\0ref=refs/pull/123/head old-oid=...
+
+ # NO, I reject it.
+ ng <reference> <error message>
+
+ Parsing the extended status of the report lines for "send-pack" is not
+ implemented in this commit, so we can make a compatible test on old
+ version of a Git client.
+
Suggested-by: Junio C Hamano <[email protected]>
Signed-off-by: Jiang Xin <[email protected]>
@@ builtin/receive-pack.c: static void write_head_info(void)
struct command {
struct command *next;
const char *error_string;
++ const char *extended_status;
unsigned int skip_update:1,
- did_not_exist:1;
+ did_not_exist:1,
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+{
+ struct command *cmd;
+ struct command *hint = NULL;
++ struct strbuf extended_status = STRBUF_INIT;
+ int code = 0;
+
+ for (;;) {
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ const char *p;
+ char *status;
+ char *msg = NULL;
++ int len;
+
+ if (packet_reader_read(reader) != PACKET_READ_NORMAL)
+ break;
++ len = strlen(reader->line);
+ if (parse_oid_hex(reader->line, &old_oid, &p) ||
+ *p++ != ' ' ||
+ parse_oid_hex(p, &new_oid, &p) ||
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ continue;
+ }
+ *status++ = '\0';
-+ if (strlen(status) > 2 && *(status + 2) == ' ') {
-+ msg = status + 2;
++ msg = strchr(status, ' ');
++ if (msg)
+ *msg++ = '\0';
-+ }
-+ if (strlen(status) != 2) {
-+ strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'\n",
-+ status, reader->line);
-+ code = -1;
-+ continue;
-+ }
+
+ /* first try searching at our hint, falling back to all refs */
+ if (hint)
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ continue;
+ }
+ if (!hint->run_proc_receive) {
-+ strbuf_addf(errmsg, "proc-receive reported status on ref of builtin command: %s\n",
++ strbuf_addf(errmsg, "proc-receive reported status on unexpected ref: %s\n",
+ refname);
+ continue;
+ }
@@ builtin/receive-pack.c: static int run_update_hook(struct command *cmd)
+ else
+ hint->error_string = "failed";
+ code = -1;
++ } else if (!strcmp("ft", status)) {
++ /* Reset "run_proc_receive" field, and continue to run in "receive-pack" */
++ hint->run_proc_receive = 0;
+ } else if (strcmp("ok", status)) {
-+ strbuf_addf(errmsg, "proc-receive has bad status '%s' for '%s'\n",
-+ status, reader->line);
++ strbuf_addf(errmsg, "proc-receive reported bad status '%s' on ref '%s'\n",
++ status, refname);
+ code = -1;
+ /* Skip marking it as RUN_PROC_RECEIVE_RETURNED */
+ continue;
+ }
-+ oidcpy(&hint->old_oid, &old_oid);
-+ oidcpy(&hint->new_oid, &new_oid);
-+ hint->run_proc_receive |= RUN_PROC_RECEIVE_RETURNED;
++ if (reader->pktlen > len)
++ strbuf_addstr(&extended_status, (char *)reader->line + len + 1);
++ if (oidcmp(&hint->old_oid, &old_oid)) {
++ oidcpy(&hint->old_oid, &old_oid);
++ strbuf_addf(&extended_status, "%sold-oid=%s",
++ extended_status.len > 0 ? " ": "",
++ oid_to_hex(&old_oid));
++ }
++ if (oidcmp(&hint->new_oid, &new_oid)) {
++ oidcpy(&hint->new_oid, &new_oid);
++ strbuf_addf(&extended_status, "%snew-oid=%s",
++ extended_status.len > 0 ? " ": "",
++ oid_to_hex(&new_oid));
++ }
++ if (extended_status.len > 0)
++ hint->extended_status = strbuf_detach(&extended_status, NULL);
++ if (hint->run_proc_receive)
++ hint->run_proc_receive |= RUN_PROC_RECEIVE_RETURNED;
+ }
+
+ for (cmd = commands; cmd; cmd = cmd->next)
+ if (cmd->run_proc_receive && !cmd->error_string &&
+ !(cmd->run_proc_receive & RUN_PROC_RECEIVE_RETURNED)) {
-+ cmd->error_string = "no report from proc-receive";
++ cmd->error_string = "proc-receive failed to report status";
+ code = -1;
+ }
+
@@ builtin/receive-pack.c: static void execute_commands(struct command *commands,
reject_updates_to_hidden(commands);
-+ /* Try to find commands that have special prefix in their reference names,
++ /*
++ * Try to find commands that have special prefix in their reference names,
+ * and mark them to run an external "proc-receive" hook later.
+ */
+ for (cmd = commands; cmd; cmd = cmd->next) {
@@ builtin/receive-pack.c: static void execute_commands(struct command *commands,
if (use_atomic)
execute_commands_atomic(commands, si);
else
+@@ builtin/receive-pack.c: static void report(struct command *commands, const char *unpack_status)
+ packet_buf_write(&buf, "unpack %s\n",
+ unpack_status ? unpack_status : "ok");
+ for (cmd = commands; cmd; cmd = cmd->next) {
+- if (!cmd->error_string)
+- packet_buf_write(&buf, "ok %s\n",
+- cmd->ref_name);
+- else
+- packet_buf_write(&buf, "ng %s %s\n",
+- cmd->ref_name, cmd->error_string);
++ if (!cmd->error_string) {
++ if (cmd->extended_status)
++ packet_buf_write(&buf, "ok %s%c%s\n",
++ cmd->ref_name, '\0',
++ cmd->extended_status);
++ else
++ packet_buf_write(&buf, "ok %s\n",
++ cmd->ref_name);
++ } else {
++ if (cmd->extended_status)
++ packet_buf_write(&buf, "ng %s %s%c%s\n",
++ cmd->ref_name, cmd->error_string,
++ '\0', cmd->extended_status);
++ else
++ packet_buf_write(&buf, "ng %s %s\n",
++ cmd->ref_name, cmd->error_string);
++ }
+ }
+ packet_buf_flush(&buf);
+
## t/helper/test-proc-receive.c (new) ##
@@
@@ t/helper/test-proc-receive.c (new)
+ }
+
+ if (returns.nr)
-+ for_each_string_list_item(item, &returns)
-+ packet_write_fmt(1, "%s\n", item->string);
++ for_each_string_list_item(item, &returns) {
++ char *p;
++
++ p = strstr(item->string, "\\0");
++ if (p) {
++ *p = '\0';
++ p += 2;
++ packet_write_fmt(1, "%s%c%s\n", item->string, '\0', p);
++ } else {
++ packet_write_fmt(1, "%s\n", item->string);
++ }
++ }
+ packet_flush(1);
+ sigchain_pop(SIGPIPE);
+
@@ t/t5411/common-functions.sh: create_commits_in () {
# user-friendly and stable text. We can easily prepare the expect text
# without having to worry about future changes of the commit ID and spaces
-# of the output. We also replce single quotes with double quotes, because
--# it is boring to prepare unquoted single quotes in expect txt.
+-# it is boring to prepare unquoted single quotes in expect text.
+# of the output. Single quotes are replaced with double quotes, because
-+# it is boring to prepare unquoted single quotes in expect txt. We also
++# it is boring to prepare unquoted single quotes in expect text. We also
+# remove some locale error messages, which break test if we turn on
+# `GIT_TEST_GETTEXT_POISON=true` in order to test unintentional translations
+# on plumbing commands.
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push : next(A) refs/for/master/topic(A)
-+test_expect_success "no proc-receive hook, fail to push special ref" '
++test_expect_success "proc-receive: no hook, fail to push special ref" '
+ test_must_fail git -C workbench push origin \
+ HEAD:next \
+ HEAD:refs/for/master/topic \
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push --atomic: (B) next(A) refs/for/master/topic(A)
-+test_expect_success "no proc-receive hook, fail all for atomic push" '
++test_expect_success "proc-receive: no hook, fail all for atomic push" '
+ test_must_fail git -C workbench push --atomic origin \
+ $B:master \
+ HEAD:next \
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+ test_cmp expect actual
+'
+
-+test_expect_success "setup proc-receive hook (bad version)" '
-+ cat >"$upstream/hooks/proc-receive" <<-EOF &&
-+ #!/bin/sh
-+
++test_expect_success "setup proc-receive hook (unknown version)" '
++ write_script "$upstream/hooks/proc-receive" <<-EOF
+ printf >&2 "# proc-receive hook\n"
-+
+ test-tool proc-receive -v --version 2
+ EOF
-+ chmod a+x "$upstream/hooks/proc-receive"
+'
+
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push : refs/for/master/topic(A)
-+test_expect_success "proc-receive bad protocol: unknown version" '
++test_expect_success "proc-receive: bad protocol (unknown version)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/master/topic \
+ >out 2>&1 &&
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+'
+
+test_expect_success "setup proc-receive hook (no report)" '
-+ cat >"$upstream/hooks/proc-receive" <<-EOF
-+ #!/bin/sh
-+
++ write_script "$upstream/hooks/proc-receive" <<-EOF
+ printf >&2 "# proc-receive hook\n"
-+
+ test-tool proc-receive -v
+ EOF
+'
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push : next(A) refs/for/master/topic(A)
-+test_expect_success "proc-receive bad protocol: no report" '
++test_expect_success "proc-receive: bad protocol (no report)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/heads/next \
+ HEAD:refs/for/master/topic >out 2>&1 &&
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/next
+ To <URL/of/upstream.git>
+ * [new branch] HEAD -> next
-+ ! [remote rejected] HEAD -> refs/for/master/topic (no report from proc-receive)
++ ! [remote rejected] HEAD -> refs/for/master/topic (proc-receive failed to report status)
+ EOF
+ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+'
+
+test_expect_success "setup proc-receive hook (bad oid)" '
-+ cat >"$upstream/hooks/proc-receive" <<-EOF
-+ #!/bin/sh
-+
++ write_script "$upstream/hooks/proc-receive" <<-EOF
+ printf >&2 "# proc-receive hook\n"
-+
+ test-tool proc-receive -v \
+ -r "bad-id new-id ref ok"
+ EOF
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push : refs/for/master/topic
-+test_expect_success "proc-receive bad protocol: bad oid" '
++test_expect_success "proc-receive: bad protocol (bad oid)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/master/topic\
+ >out 2>&1 &&
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+ remote: proc-receive> bad-id new-id ref ok
+ remote: error: proc-receive expected "old new ref status [msg]", got "bad-id new-id ref ok"
+ To <URL/of/upstream.git>
-+ ! [remote rejected] HEAD -> refs/for/master/topic (no report from proc-receive)
++ ! [remote rejected] HEAD -> refs/for/master/topic (proc-receive failed to report status)
+ EOF
+ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+'
+
+test_expect_success "setup proc-receive hook (no status)" '
-+ cat >"$upstream/hooks/proc-receive" <<-EOF
-+ #!/bin/sh
-+
++ write_script "$upstream/hooks/proc-receive" <<-EOF
+ printf >&2 "# proc-receive hook\n"
-+
+ test-tool proc-receive -v \
+ -r "$ZERO_OID $A refs/for/master/topic"
+ EOF
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push : refs/for/master/topic
-+test_expect_success "proc-receive bad protocol: no status" '
++test_expect_success "proc-receive: bad protocol (no status)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/master/topic \
+ >out 2>&1 &&
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: error: proc-receive expected "old new ref status [msg]", got "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
+ To <URL/of/upstream.git>
-+ ! [remote rejected] HEAD -> refs/for/master/topic (no report from proc-receive)
++ ! [remote rejected] HEAD -> refs/for/master/topic (proc-receive failed to report status)
+ EOF
+ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+'
+
+test_expect_success "setup proc-receive hook (unknown status)" '
-+ cat >"$upstream/hooks/proc-receive" <<-EOF
-+ #!/bin/sh
-+
++ write_script "$upstream/hooks/proc-receive" <<-EOF
+ printf >&2 "# proc-receive hook\n"
-+
+ test-tool proc-receive -v \
+ -r "$ZERO_OID $A refs/for/master/topic xx msg"
+ EOF
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push : refs/for/master/topic
-+test_expect_success "proc-receive bad protocol: unknown status" '
++test_expect_success "proc-receive: bad protocol (unknown status)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/master/topic \
+ >out 2>&1 &&
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic xx msg
-+ remote: error: proc-receive has bad status "xx" for "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
++ remote: error: proc-receive reported bad status "xx" on ref "refs/for/master/topic"
+ To <URL/of/upstream.git>
-+ ! [remote rejected] HEAD -> refs/for/master/topic (no report from proc-receive)
-+ EOF
-+ test_cmp expect actual &&
-+ git -C "$upstream" show-ref >out &&
-+ make_user_friendly_and_stable_output <out >actual &&
-+ cat >expect <<-EOF &&
-+ <COMMIT-A> refs/heads/master
-+ EOF
-+ test_cmp expect actual
-+'
-+
-+test_expect_success "setup proc-receive hook (bad status)" '
-+ cat >"$upstream/hooks/proc-receive" <<-EOF
-+ #!/bin/sh
-+
-+ printf >&2 "# proc-receive hook\n"
-+
-+ test-tool proc-receive -v \
-+ -r "$ZERO_OID $A refs/for/master/topic bad status"
-+ EOF
-+'
-+
-+# Refs of upstream : master(A)
-+# Refs of workbench: master(A) tags/v123
-+# git push : refs/for/master/topic
-+test_expect_success "proc-receive bad protocol: bad status" '
-+ test_must_fail git -C workbench push origin \
-+ HEAD:refs/for/master/topic \
-+ >out 2>&1 &&
-+ make_user_friendly_and_stable_output <out >actual &&
-+ cat >expect <<-EOF &&
-+ remote: # pre-receive hook
-+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
-+ remote: # proc-receive hook
-+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
-+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic bad status
-+ remote: error: proc-receive has bad status "bad status" for "<ZERO-OID> <COMMIT-A> refs/for/master/topic"
-+ To <URL/of/upstream.git>
-+ ! [remote rejected] HEAD -> refs/for/master/topic (no report from proc-receive)
++ ! [remote rejected] HEAD -> refs/for/master/topic (proc-receive failed to report status)
+ EOF
+ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+'
+
+test_expect_success "setup proc-receive hook (ng)" '
-+ cat >"$upstream/hooks/proc-receive" <<-EOF
-+ #!/bin/sh
-+
++ write_script "$upstream/hooks/proc-receive" <<-EOF
+ printf >&2 "# proc-receive hook\n"
-+
+ test-tool proc-receive -v \
+ -r "$ZERO_OID $A refs/for/master/topic ng"
+ EOF
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push : refs/for/master/topic
-+test_expect_success "proc-receive: fail to update (no message)" '
++test_expect_success "proc-receive: fail to update (ng)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/master/topic \
+ >out 2>&1 &&
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+'
+
+test_expect_success "setup proc-receive hook (ng message)" '
-+ cat >"$upstream/hooks/proc-receive" <<-EOF
-+ #!/bin/sh
-+
++ write_script "$upstream/hooks/proc-receive" <<-EOF
+ printf >&2 "# proc-receive hook\n"
-+
+ test-tool proc-receive -v \
+ -r "$ZERO_OID $A refs/for/master/topic ng error msg"
+ EOF
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push : refs/for/master/topic
-+test_expect_success "proc-receive: fail to update (has message)" '
++test_expect_success "proc-receive: fail to update (ng message)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/master/topic \
+ >out 2>&1 &&
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+ test_cmp expect actual
+'
+
-+test_expect_success "setup proc-receive hook (report status on builtin command)" '
-+ cat >"$upstream/hooks/proc-receive" <<-EOF
-+ #!/bin/sh
-+
++test_expect_success "setup proc-receive hook (unexpected ref)" '
++ write_script "$upstream/hooks/proc-receive" <<-EOF
+ printf >&2 "# proc-receive hook\n"
-+
+ test-tool proc-receive -v \
+ -r "$ZERO_OID $A refs/heads/master ok"
+ EOF
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push : (B) refs/for/master/topic
-+test_expect_success "proc-receive: warning on report for builtin command" '
++test_expect_success "proc-receive: report on unexpected ref" '
+ test_must_fail git -C workbench push origin \
+ $B:refs/heads/master \
+ HEAD:refs/for/master/topic \
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/heads/master ok
-+ remote: error: proc-receive reported status on ref of builtin command: refs/heads/master
++ remote: error: proc-receive reported status on unexpected ref: refs/heads/master
+ remote: # post-receive hook
+ remote: post-receive< <COMMIT-A> <COMMIT-B> refs/heads/master
+ To <URL/of/upstream.git>
+ <OID-A>..<OID-B> <COMMIT-B> -> master
-+ ! [remote rejected] HEAD -> refs/for/master/topic (no report from proc-receive)
++ ! [remote rejected] HEAD -> refs/for/master/topic (proc-receive failed to report status)
+ EOF
+ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+ test_cmp expect actual
+'
+
++# Refs of upstream : master(B)
++# Refs of workbench: master(A) tags/v123
+test_expect_success "cleanup" '
+ git -C "$upstream" update-ref refs/heads/master $A
+'
+
+test_expect_success "setup proc-receive hook (ok)" '
-+ cat >"$upstream/hooks/proc-receive" <<-EOF
-+ #!/bin/sh
-+
++ write_script "$upstream/hooks/proc-receive" <<-EOF
+ printf >&2 "# proc-receive hook\n"
-+
+ test-tool proc-receive -v \
+ -r "$ZERO_OID $A refs/for/master/topic ok"
+ EOF
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push : refs/for/a/b/c/my/topic
-+test_expect_success "proc-receive: no report from proc-receive" '
++test_expect_success "proc-receive: failed to report status" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/a/b/c/my/topic \
+ >out 2>&1 &&
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/master/topic ok
+ remote: error: proc-receive reported status on unknown ref: refs/for/master/topic
+ To <URL/of/upstream.git>
-+ ! [remote rejected] HEAD -> refs/for/a/b/c/my/topic (no report from proc-receive)
++ ! [remote rejected] HEAD -> refs/for/a/b/c/my/topic (proc-receive failed to report status)
+ EOF
+ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push -o ... : refs/for/master/topic
-+test_expect_success "not support push options" '
++test_expect_success "proc-receive: not support push options" '
+ test_must_fail git -C workbench push \
+ -o issue=123 \
+ -o reviewer=user1 \
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+# Refs of upstream : master(A)
+# Refs of workbench: master(A) tags/v123
+# git push -o ... : next(A) refs/for/master/topic
-+test_expect_success "push with options" '
++test_expect_success "proc-receive: push with options" '
+ git -C workbench push \
+ --atomic \
+ -o issue=123 \
@@ t/t5411/common-test-cases.sh: test_expect_success "normal git-push command" '
+ EOF
+ test_cmp expect actual
+'
++
++# Refs of upstream : master(A) next(A)
++# Refs of workbench: master(A) tags/v123
++test_expect_success "cleanup" '
++ git -C "$upstream" update-ref -d refs/heads/next
++'
++
++test_expect_success "setup proc-receive hook (ft)" '
++ write_script "$upstream/hooks/proc-receive" <<-EOF
++ printf >&2 "# proc-receive hook\n"
++ test-tool proc-receive -v \
++ -r "$ZERO_OID $B refs/for/master/topic ft"
++ EOF
++'
++
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push : refs/for/master/topic(B)
++test_expect_success "proc-receive: fall throught, let receive-pack to execute" '
++ git -C workbench push origin \
++ $B:refs/for/master/topic \
++ >out 2>&1 &&
++ make_user_friendly_and_stable_output <out >actual &&
++ cat >expect <<-EOF &&
++ remote: # pre-receive hook
++ remote: pre-receive< <ZERO-OID> <COMMIT-B> refs/for/master/topic
++ remote: # proc-receive hook
++ remote: proc-receive< <ZERO-OID> <COMMIT-B> refs/for/master/topic
++ remote: proc-receive> <ZERO-OID> <COMMIT-B> refs/for/master/topic ft
++ remote: # post-receive hook
++ remote: post-receive< <ZERO-OID> <COMMIT-B> refs/for/master/topic
++ To <URL/of/upstream.git>
++ * [new reference] <COMMIT-B> -> refs/for/master/topic
++ EOF
++ test_cmp expect actual &&
++ git -C "$upstream" show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
++ cat >expect <<-EOF &&
++ <COMMIT-B> refs/for/master/topic
++ <COMMIT-A> refs/heads/master
++ EOF
++ test_cmp expect actual
++'
++
++# Refs of upstream : master(A) refs/for/master/topic(A)
++# Refs of workbench: master(A) tags/v123
++test_expect_success "cleanup" '
++ git -C "$upstream" update-ref -d refs/for/master/topic
++'
++
++test_expect_success "setup proc-receive hook (with extended status)" '
++ write_script "$upstream/hooks/proc-receive" <<-EOF
++ printf >&2 "# proc-receive hook\n"
++ test-tool proc-receive -v \
++ -r "$ZERO_OID $A refs/for/a/b/c/topic ok" \
++ -r "$ZERO_OID $A refs/for/next/topic ok\0ref=refs/pull/123/head" \
++ -r "$B $A refs/for/master/topic ok\0ref=refs/pull/124/head forced-update"
++ EOF
++'
++
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push : refs/for/next/topic(A) refs/for/a/b/c/topic(A) refs/for/master/topic(A)
++test_expect_success "proc-receive: report with extended status" '
++ git -C workbench push origin \
++ HEAD:refs/for/next/topic \
++ HEAD:refs/for/a/b/c/topic \
++ HEAD:refs/for/master/topic \
++ >out 2>&1 &&
++ make_user_friendly_and_stable_output <out >actual &&
++ cat >expect <<-EOF &&
++ remote: # pre-receive hook
++ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
++ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/topic
++ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: # proc-receive hook
++ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
++ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/topic
++ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/a/b/c/topic ok
++ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/next/topic ok\0ref=refs/pull/123/head
++ remote: proc-receive> <COMMIT-B> <COMMIT-A> refs/for/master/topic ok\0ref=refs/pull/124/head forced-update
++ remote: # post-receive hook
++ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
++ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/topic
++ remote: post-receive< <COMMIT-B> <COMMIT-A> refs/for/master/topic
++ To <URL/of/upstream.git>
++ * [new reference] HEAD -> refs/for/next/topic
++ * [new reference] HEAD -> refs/for/a/b/c/topic
++ * [new reference] HEAD -> refs/for/master/topic
++ EOF
++ test_cmp expect actual &&
++ git -C "$upstream" show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
++ cat >expect <<-EOF &&
++ <COMMIT-A> refs/heads/master
++ EOF
++ test_cmp expect actual
++'
++
++test_expect_success "setup proc-receive hook" '
++ write_script "$upstream/hooks/proc-receive" <<-EOF
++ printf >&2 "# proc-receive hook\n"
++ test-tool proc-receive -v \
++ -r "$ZERO_OID $A refs/for/next/topic ok" \
++ -r "$A $B refs/for/master/topic ok"
++ EOF
++'
++
++# Refs of upstream : master(A)
++# Refs of workbench: master(A) tags/v123
++# git push : (B) bar(A) baz(A) refs/for/next/topic(A) foo(A) refs/for/master/topic(A)
++test_expect_success "proc-receive: report update of mixed refs" '
++ git -C workbench push origin \
++ $B:refs/heads/master \
++ HEAD:refs/heads/bar \
++ HEAD:refs/heads/baz \
++ HEAD:refs/for/next/topic \
++ HEAD:refs/heads/foo \
++ HEAD:refs/for/master/topic \
++ >out 2>&1 &&
++ make_user_friendly_and_stable_output <out >actual &&
++ cat >expect <<-EOF &&
++ remote: # pre-receive hook
++ remote: pre-receive< <COMMIT-A> <COMMIT-B> refs/heads/master
++ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/bar
++ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/baz
++ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
++ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/foo
++ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: # proc-receive hook
++ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
++ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/next/topic ok
++ remote: proc-receive> <COMMIT-A> <COMMIT-B> refs/for/master/topic ok
++ remote: # post-receive hook
++ remote: post-receive< <COMMIT-A> <COMMIT-B> refs/heads/master
++ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/bar
++ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/baz
++ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
++ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/foo
++ remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/master/topic
++ To <URL/of/upstream.git>
++ <OID-A>..<OID-B> <COMMIT-B> -> master
++ * [new branch] HEAD -> bar
++ * [new branch] HEAD -> baz
++ * [new reference] HEAD -> refs/for/next/topic
++ * [new branch] HEAD -> foo
++ * [new reference] HEAD -> refs/for/master/topic
++ EOF
++ test_cmp expect actual &&
++ git -C "$upstream" show-ref >out &&
++ make_user_friendly_and_stable_output <out >actual &&
++ cat >expect <<-EOF &&
++ <COMMIT-A> refs/heads/bar
++ <COMMIT-A> refs/heads/baz
++ <COMMIT-A> refs/heads/foo
++ <COMMIT-B> refs/heads/master
++ EOF
++ test_cmp expect actual
++'
3: 16257616ec < -: ---------- refs.c: refactor to reuse ref_is_hidden()
7: d1677b61d1 ! 4: a4a1cc1d14 send-pack: extension for client-side status report @@ send-pack.c: static int receive_status(struct packet_reader *reader, struct ref
}
## t/t5411/common-test-cases.sh ##
-@@ t/t5411/common-test-cases.sh: test_expect_success "report update of all special refs" '
- remote: post-receive< <ZERO-OID> <COMMIT-A> refs/review/a/b/c/topic
+@@ t/t5411/common-test-cases.sh: test_expect_success "proc-receive: report with extended status" '
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/topic
remote: post-receive< <COMMIT-B> <COMMIT-A> refs/for/master/topic
To <URL/of/upstream.git>
- * [new reference] HEAD -> refs/for/next/topic
+ * [new reference] HEAD -> refs/pull/123/head
- * [new reference] HEAD -> refs/review/a/b/c/topic
+ * [new reference] HEAD -> refs/for/a/b/c/topic
- * [new reference] HEAD -> refs/for/master/topic
+ + <OID-B>...<OID-A> HEAD -> refs/pull/124/head (forced update)
EOF
test_cmp expect actual &&
git -C "$upstream" show-ref >out &&
-@@ t/t5411/common-test-cases.sh: test_expect_success "report mixed refs update" ' +@@ t/t5411/common-test-cases.sh: test_expect_success "proc-receive: report update of mixed refs" '
* [new branch] HEAD -> baz
* [new reference] HEAD -> refs/for/next/topic
* [new branch] HEAD -> foo
@@ t/t5411/common-test-cases.sh: test_expect_success "report mixed refs update" '
EOF
test_cmp expect actual &&
git -C "$upstream" show-ref >out &&
-@@ t/t5411/common-test-cases.sh: test_expect_success "report test: fallthrough" '
- <OID-A>..<OID-B> <COMMIT-B> -> bar
- - [deleted] foo
- + <OID-B>...<OID-A> HEAD -> master (forced update)
-- * [new reference] HEAD -> refs/for/master/topic
-- * [new reference] HEAD -> refs/for/next/topic
-+ <OID-A>..<OID-B> HEAD -> refs/pull/123/head
-+ + <OID-B>...<OID-A> HEAD -> refs/pull/124/head (forced update)
- EOF
- test_cmp expect actual &&
- git -C "$upstream" show-ref >out &&
## transport-helper.c ##
@@ transport-helper.c: static int push_update_ref_status(struct strbuf *buf,
-: ---------- > 5: 4e3c383c12 refs.c: refactor to reuse ref_is_hidden()
4: cd49b57c17 ! 6: dd83569c4e receive-pack: new config receive.procReceiveRefs
@@ builtin/receive-pack.c: static int receive_pack_config(const char *var, const ch
+ len = strlen(prefix);
+ while (len && prefix[len - 1] == '/')
+ prefix[--len] = '\0';
-+ string_list_insert(&proc_receive_refs, prefix);
++ string_list_append(&proc_receive_refs, prefix);
+ return 0;
+ }
+
@@ builtin/receive-pack.c: static int receive_pack_config(const char *var, const ch
}
@@ builtin/receive-pack.c: static void execute_commands(struct command *commands,
- /* Try to find commands that have special prefix in their reference names,
+ * Try to find commands that have special prefix in their reference names,
* and mark them to run an external "proc-receive" hook later.
*/
- for (cmd = commands; cmd; cmd = cmd->next) {
@@ builtin/receive-pack.c: static void execute_commands(struct command *commands,
+ if (proc_receive_refs.nr > 0) {
+ struct strbuf refname_full = STRBUF_INIT;
+ size_t prefix_len;
-+
-+ strbuf_addstr(&refname_full, get_git_namespace());
-+ prefix_len = refname_full.len;
- /* TODO: replace the fixed prefix by looking up git config variables. */
- if (!strncmp(cmd->ref_name, "refs/for/", 9)) {
- cmd->run_proc_receive = RUN_PROC_RECEIVE_SCHEDULED;
- run_proc_receive = 1;
++ strbuf_addstr(&refname_full, get_git_namespace());
++ prefix_len = refname_full.len;
++
+ for (cmd = commands; cmd; cmd = cmd->next) {
+ if (!should_process_cmd(cmd))
+ continue;
+
+ strbuf_setlen(&refname_full, prefix_len);
+ strbuf_addstr(&refname_full, cmd->ref_name);
-+ if (ref_is_matched(&proc_receive_refs, cmd->ref_name, refname_full.buf)) {
++ if (ref_matches(&proc_receive_refs, cmd->ref_name, refname_full.buf)) {
+ cmd->run_proc_receive = RUN_PROC_RECEIVE_SCHEDULED;
+ run_proc_receive = 1;
+ }
@@ t/t5411/common-test-cases.sh: test_expect_success "cleanup" '
# Refs of upstream : master(A)
# Refs of workbench: master(A) tags/v123
# git push : next(A) refs/for/master/topic(A)
-@@ t/t5411/common-test-cases.sh: test_expect_success "push with options" '
- EOF
- test_cmp expect actual
- '
-+
-+# Refs of upstream : master(A) next(A)
-+# Refs of workbench: master(A) tags/v123
-+test_expect_success "cleanup" '
-+ git -C "$upstream" update-ref -d refs/heads/next
-+'
-+
-+test_expect_success "setup proc-receive hook" '
-+ cat >"$upstream/hooks/proc-receive" <<-EOF &&
-+ #!/bin/sh
-+
-+ printf >&2 "# proc-receive hook\n"
-+
-+ test-tool proc-receive -v \
+@@ t/t5411/common-test-cases.sh: test_expect_success "setup proc-receive hook (with extended status)" '
+ write_script "$upstream/hooks/proc-receive" <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+- -r "$ZERO_OID $A refs/for/a/b/c/topic ok" \
+ -r "$ZERO_OID $A refs/review/a/b/c/topic ok" \
-+ -r "$ZERO_OID $A refs/for/next/topic ok" \
-+ -r "$B $A refs/for/master/topic ok"
-+ EOF
-+ chmod a+x "$upstream/hooks/proc-receive"
-+'
-+
-+# Refs of upstream : master(A)
-+# Refs of workbench: master(A) tags/v123
+ -r "$ZERO_OID $A refs/for/next/topic ok\0ref=refs/pull/123/head" \
+ -r "$B $A refs/for/master/topic ok\0ref=refs/pull/124/head forced-update"
+ EOF
+@@ t/t5411/common-test-cases.sh: test_expect_success "setup proc-receive hook (with extended status)" '
+
+ # Refs of upstream : master(A)
+ # Refs of workbench: master(A) tags/v123
+-# git push : refs/for/next/topic(A) refs/for/a/b/c/topic(A) refs/for/master/topic(A)
+# git push : refs/for/next/topic(A) refs/review/a/b/c/topic(A) refs/for/master/topic(A)
-+test_expect_success "report update of all special refs" '
-+ git -C workbench push origin \
-+ HEAD:refs/for/next/topic \
+ test_expect_success "proc-receive: report with extended status" '
+ git -C workbench push origin \
+ HEAD:refs/for/next/topic \
+- HEAD:refs/for/a/b/c/topic \
+ HEAD:refs/review/a/b/c/topic \
-+ HEAD:refs/for/master/topic \
-+ >out 2>&1 &&
-+ make_user_friendly_and_stable_output <out >actual &&
-+ cat >expect <<-EOF &&
-+ remote: # pre-receive hook
-+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
+ HEAD:refs/for/master/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ remote: # pre-receive hook
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
+- remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/topic
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/review/a/b/c/topic
-+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
-+ remote: # proc-receive hook
-+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: # proc-receive hook
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
+- remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/topic
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/review/a/b/c/topic
-+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
+- remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/a/b/c/topic ok
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/review/a/b/c/topic ok
-+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/next/topic ok
-+ remote: proc-receive> <COMMIT-B> <COMMIT-A> refs/for/master/topic ok
-+ remote: # post-receive hook
-+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/next/topic ok\0ref=refs/pull/123/head
+ remote: proc-receive> <COMMIT-B> <COMMIT-A> refs/for/master/topic ok\0ref=refs/pull/124/head forced-update
+ remote: # post-receive hook
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
+- remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/topic
+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/review/a/b/c/topic
-+ remote: post-receive< <COMMIT-B> <COMMIT-A> refs/for/master/topic
-+ To <URL/of/upstream.git>
-+ * [new reference] HEAD -> refs/for/next/topic
+ remote: post-receive< <COMMIT-B> <COMMIT-A> refs/for/master/topic
+ To <URL/of/upstream.git>
+ * [new reference] HEAD -> refs/pull/123/head
+- * [new reference] HEAD -> refs/for/a/b/c/topic
+ * [new reference] HEAD -> refs/review/a/b/c/topic
-+ * [new reference] HEAD -> refs/for/master/topic
-+ EOF
-+ test_cmp expect actual &&
-+ git -C "$upstream" show-ref >out &&
-+ make_user_friendly_and_stable_output <out >actual &&
-+ cat >expect <<-EOF &&
-+ <COMMIT-A> refs/heads/master
-+ EOF
-+ test_cmp expect actual
+ + <OID-B>...<OID-A> HEAD -> refs/pull/124/head (forced update)
+ EOF
+ test_cmp expect actual &&
+@@ t/t5411/common-test-cases.sh: test_expect_success "proc-receive: report update of mixed refs" '
+ EOF
+ test_cmp expect actual
+ '
++
++test_expect_success "config add receive.procReceiveRefs = refs" '
++ git -C "$upstream" config --add receive.procReceiveRefs refs
+'
+
+test_expect_success "setup proc-receive hook" '
-+ cat >"$upstream/hooks/proc-receive" <<-EOF &&
-+ #!/bin/sh
-+
++ write_script "$upstream/hooks/proc-receive" <<-EOF
+ printf >&2 "# proc-receive hook\n"
-+
+ test-tool proc-receive -v \
-+ -r "$ZERO_OID $A refs/for/next/topic ok" \
-+ -r "$A $B refs/for/master/topic ok"
++ -r "$B $A refs/heads/master ft" \
++ -r "$A $ZERO_OID refs/heads/foo ft" \
++ -r "$A $B refs/heads/bar ft" \
++ -r "$A $B refs/for/master/topic ok\0ref=refs/pull/123/head" \
++ -r "$B $A refs/for/next/topic ok\0ref=refs/pull/124/head forced-update"
+ EOF
-+ chmod a+x "$upstream/hooks/proc-receive"
+'
+
-+# Refs of upstream : master(A)
++# Refs of upstream : master(B) foo(A) bar(A)) baz(A)
+# Refs of workbench: master(A) tags/v123
-+# git push : bar(A) baz(A) refs/for/next/topic(A) foo(A) refs/for/master/topic(A)
-+test_expect_success "report mixed refs update" '
-+ git -C workbench push origin \
-+ HEAD:refs/heads/bar \
-+ HEAD:refs/heads/baz \
-+ HEAD:refs/for/next/topic \
-+ HEAD:refs/heads/foo \
++# git push -f : master(A) (NULL) (B) refs/for/master/topic(A) refs/for/next/topic(A)
++test_expect_success "proc-receive: process all refs" '
++ git -C workbench push -f origin \
++ HEAD:refs/heads/master \
++ :refs/heads/foo \
++ $B:refs/heads/bar \
+ HEAD:refs/for/master/topic \
++ HEAD:refs/for/next/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ remote: # pre-receive hook
-+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/bar
-+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/baz
-+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
-+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/foo
++ remote: pre-receive< <COMMIT-A> <COMMIT-B> refs/heads/bar
++ remote: pre-receive< <COMMIT-A> <ZERO-OID> refs/heads/foo
++ remote: pre-receive< <COMMIT-B> <COMMIT-A> refs/heads/master
+ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
++ remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
+ remote: # proc-receive hook
-+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
++ remote: proc-receive< <COMMIT-A> <COMMIT-B> refs/heads/bar
++ remote: proc-receive< <COMMIT-A> <ZERO-OID> refs/heads/foo
++ remote: proc-receive< <COMMIT-B> <COMMIT-A> refs/heads/master
+ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
-+ remote: proc-receive> <ZERO-OID> <COMMIT-A> refs/for/next/topic ok
-+ remote: proc-receive> <COMMIT-A> <COMMIT-B> refs/for/master/topic ok
++ remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
++ remote: proc-receive> <COMMIT-B> <COMMIT-A> refs/heads/master ft
++ remote: proc-receive> <COMMIT-A> <ZERO-OID> refs/heads/foo ft
++ remote: proc-receive> <COMMIT-A> <COMMIT-B> refs/heads/bar ft
++ remote: proc-receive> <COMMIT-A> <COMMIT-B> refs/for/master/topic ok\0ref=refs/pull/123/head
++ remote: proc-receive> <COMMIT-B> <COMMIT-A> refs/for/next/topic ok\0ref=refs/pull/124/head forced-update
+ remote: # post-receive hook
-+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/bar
-+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/baz
-+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
-+ remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/foo
++ remote: post-receive< <COMMIT-A> <COMMIT-B> refs/heads/bar
++ remote: post-receive< <COMMIT-A> <ZERO-OID> refs/heads/foo
++ remote: post-receive< <COMMIT-B> <COMMIT-A> refs/heads/master
+ remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/master/topic
++ remote: post-receive< <COMMIT-B> <COMMIT-A> refs/for/next/topic
+ To <URL/of/upstream.git>
-+ * [new branch] HEAD -> bar
-+ * [new branch] HEAD -> baz
-+ * [new reference] HEAD -> refs/for/next/topic
-+ * [new branch] HEAD -> foo
-+ * [new reference] HEAD -> refs/for/master/topic
++ <OID-A>..<OID-B> <COMMIT-B> -> bar
++ - [deleted] foo
++ + <OID-B>...<OID-A> HEAD -> master (forced update)
++ <OID-A>..<OID-B> HEAD -> refs/pull/123/head
++ + <OID-B>...<OID-A> HEAD -> refs/pull/124/head (forced update)
+ EOF
+ test_cmp expect actual &&
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
-+ <COMMIT-A> refs/heads/bar
++ <COMMIT-B> refs/heads/bar
+ <COMMIT-A> refs/heads/baz
-+ <COMMIT-A> refs/heads/foo
+ <COMMIT-A> refs/heads/master
+ EOF
+ test_cmp expect actual
6: 860860536e < -: ---------- receive-pack: extension for server-side report
8: 41159c2ef2 = 7: 2d04f495a3 doc: add documentation for the proc-receive hook---
Jiang Xin (7):
transport: not report a non-head push as a branch
connect: export parse_feature_value()
receive-pack: add new proc-receive hook
send-pack: extension for client-side status report refs.c: refactor to reuse ref_is_hidden()
receive-pack: new config receive.procReceiveRefs
doc: add documentation for the proc-receive hook
Documentation/config/receive.txt | 14 + Documentation/githooks.txt | 58 ++ Makefile | 1 + builtin/receive-pack.c | 328 ++++++++- connect.c | 3 +- connect.h | 1 + refs.c | 13 +- refs.h | 1 + remote.c | 28 + remote.h | 3 + send-pack.c | 13 +- t/helper/test-proc-receive.c | 178 +++++ t/helper/test-tool.c | 1 + t/helper/test-tool.h | 1 + t/t5411-proc-receive-hook.sh | 67 ++ t/t5411/common-functions.sh | 54 ++ t/t5411/common-test-cases.sh | 794 +++++++++++++++++++++ t/t5412-proc-receive-hook-http-protocol.sh | 78 ++ t/t5516-fetch-push.sh | 2 +- transport-helper.c | 66 +- transport.c | 42 +- 21 files changed, 1691 insertions(+), 55 deletions(-) create mode 100644 t/helper/test-proc-receive.c create mode 100755 t/t5411-proc-receive-hook.sh create mode 100644 t/t5411/common-functions.sh create mode 100644 t/t5411/common-test-cases.sh create mode 100755 t/t5412-proc-receive-hook-http-protocol.sh
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v11 1/7 transport: not report a non-head push as a branch
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
When pushing a new reference (not a head or tag), report it as a new reference instead of a new branch.
--- t/t5411-proc-receive-hook.sh | 67 +++++++++++++++++++ t/t5411/common-functions.sh | 50 ++++++++++++++ t/t5411/common-test-cases.sh | 43 ++++++++++++ t/t5412-proc-receive-hook-http-protocol.sh | 78 ++++++++++++++++++++++ t/t5516-fetch-push.sh | 2 +- transport.c | 9 ++- 6 files changed, 245 insertions(+), 4 deletions(-) create mode 100755 t/t5411-proc-receive-hook.sh create mode 100644 t/t5411/common-functions.sh create mode 100644 t/t5411/common-test-cases.sh create mode 100755 t/t5412-proc-receive-hook-http-protocol.sh
t/t5411-proc-receive-hook.sh new file +67 -0
@@ -0,0 +1,67 @@ +#!/bin/sh +# +# Copyright (c) 2020 Jiang Xin +# + +test_description='Test proc-receive hook' + +. ./test-lib.sh + +. "$TEST_DIRECTORY"/t5411/common-functions.sh + +# Format the output of git-push, git-show-ref and other commands to make a +# user-friendly and stable text. In addition to the common format method, +# we also replace the URL of different protocol for the upstream repository +# with a fixed pattern. +make_user_friendly_and_stable_output () { + make_user_friendly_and_stable_output_common | + sed -e "s#To ../upstream.git#To <URL/of/upstream.git>#" +} + +# Refs of upstream : master(B) next(A) +# Refs of workbench: master(A) tags/v123 +test_expect_success "setup" ' + git init --bare upstream.git && + git init workbench && + create_commits_in workbench A B && + ( + cd workbench && + # Try to make a stable fixed width for abbreviated commit ID, + # this fixed-width oid will be replaced with "<OID>". + git config core.abbrev 7 && + git remote add origin ../upstream.git && + git update-ref refs/heads/master $A && + git tag -m "v123" v123 $A && + git push origin \ + $B:refs/heads/master \ + $A:refs/heads/next + ) && + TAG=$(git -C workbench rev-parse v123) && + + # setup pre-receive hook + write_script upstream.git/hooks/pre-receive <<-\EOF && + exec >&2 + echo "# pre-receive hook" + while read old new ref + do + echo "pre-receive< $old $new $ref" + done + EOF + + # setup post-receive hook + write_script upstream.git/hooks/post-receive <<-\EOF && + exec >&2 + echo "# post-receive hook" + while read old new ref + do + echo "post-receive< $old $new $ref" + done + EOF + + upstream=upstream.git +' + +# Include test cases for both file and HTTP protocol +. "$TEST_DIRECTORY"/t5411/common-test-cases.sh + +test_done
t/t5411/common-functions.sh new file +50 -0
50 lines not on this page · raw message
t/t5411/common-test-cases.sh new file +43 -0
43 lines not on this page · raw message
t/t5412-proc-receive-hook-http-protocol.sh new file +78 -0
78 lines not on this page · raw message
t/t5516-fetch-push.sh +1 -1
@@ -1039,7 +1039,7 @@ test_force_fetch_tag "annotated tag" "-f -a -m'tag message'" test_expect_success 'push --porcelain' ' mk_empty testrepo && echo >.git/foo "To testrepo" && - echo >>.git/foo "* refs/heads/master:refs/remotes/origin/master [new branch]" && + echo >>.git/foo "* refs/heads/master:refs/remotes/origin/master [new reference]" && echo >>.git/foo "Done" && git push >.git/bar --porcelain testrepo refs/heads/master:refs/remotes/origin/master && (
transport.c +6 -3
@@ -500,9 +500,12 @@ static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widt porcelain, summary_width); else if (is_null_oid(&ref->old_oid))print_ref_status ('*' , - (starts_with (ref->name,"refs/tags/" ) ?"[new tag]" : -"[new branch]" ), - ref, ref->peer_ref,NULL , porcelain, summary_width); + (starts_with (ref->name,"refs/tags/" ) + ?"[new tag]" + : (starts_with (ref->name,"refs/heads/" ) + ?"[new branch]" + :"[new reference]" )), + ref, ref->peer_ref,NULL , porcelain, summary_width); else {struct strbuf quickref =STRBUF_INIT ;char type;
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v11 2/7 connect: export parse_feature_value()
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Export parse_feature_value() to parse extended status of report for "proc-receive" and "receive-pack".
--- connect.c | 3 +-- connect.h | 1 + 2 files changed, 2 insertions(+), 2 deletions(-)
connect.c +1 -2
@@ -18,7 +18,6 @@static char *server_capabilities_v1;static struct argv_array server_capabilities_v2 =ARGV_ARRAY_INIT ; -static const char *parse_feature_value (const char *,const char *,int *);static int check_ref (const char *name,unsigned int flags) {
@@ -447,7 +446,7 @@ struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,return list; } -static const char *parse_feature_value (const char *feature_list,const char *feature,int *lenp) +const char *parse_feature_value (const char *feature_list,const char *feature,int *lenp) {int len;
connect.h +1 -0
@@ -12,6 +12,7 @@ int finish_connect(struct child_process *conn);int git_connection_is_socket (struct child_process *conn);int server_supports (const char *feature);int parse_feature_request (const char *features,const char *feature); +const char *parse_feature_value (const char *feature_list,const char *feature,int *len_ret);const char *server_feature_value (const char *feature,int *len_ret);int url_is_local_not_ssh (const char *url);
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v11 3/7 receive-pack: add new proc-receive hook
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Git calls an internal execute_commands function to handle commands sent from client to git-receive-pack. Regardless of what references the user pushes, git creates or updates the corresponding references if the user has write-permission. A contributor who has no write-permission, cannot push to the repository directly. So, the contributor has to write commits to an alternate location, and sends pull request by emails or by other ways. We call this workflow as a distributed workflow.
It would be more convenient to work in a centralized workflow like what Gerrit provided for some cases. For example, a read-only user who cannot push to a branch directly can run the following git push command to push commits to a pseudo reference (has a prefix "refs/for/", not "refs/heads/") to create a code review.
git push origin \
HEAD:refs/for/<branch-name>/<session>The <branch-name> in the above example can be as simple as "master", or a more complicated branch name like "foo/bar". The <session> in the above example command can be the local branch name of the client side, such as "my/topic".
We cannot implement a centralized workflow elegantly by using "pre-receive" + "post-receive", because Git will call the internal function "execute_commands" to create references (even the special pseudo reference) between these two hooks. Even though we can delete the temporarily created pseudo reference via the "post-receive" hook, having a temporary reference is not safe for concurrent pushes.
So, add a filter and a new handler to support this kind of workflow. The filter will check the prefix of the reference name, and if the command has a special reference name, the filter will turn a specific field (run_proc_receive) on for the command. Commands with this filed turned on will be executed by a new handler (an hook named "proc-receive") instead of the internal execute_commands function. We can use this "proc-receive" command to create pull requests or send emails for code review.
Suggested by Junio, this "proc-receive" hook reads the commands, push-options (optional), and send result using a protocol in pkt-line format. In the following example, The letter "S" stands for "receive-pack" and letter "H" stands for the hook.
# Version and features negotiation.
S: PKT-LINE(version=1\0push-options atomic...)
S: flush-pkt
H: PKT-LINE(version=1\0push-options...)
H: flush-pkt
# Send commands from server to the hook.
S: PKT-LINE(<old-oid> <new-oid> <ref>)
S: ... ...
S: flush-pkt
# Send push-options only if the 'push-options' feature is enabled.
S: PKT-LINE(push-option)
S: ... ...
S: flush-pkt
# Receive result from the hook.
# OK, run this command successfully.
H: PKT-LINE(<old-oid> <new-oid> <ref> ok)
# NO, I reject it.
H: PKT-LINE(<old-oid> <new-oid> <ref> ng <reason>)
# Fall through, let 'receive-pack' to execute it.
H: PKT-LINE(<old-oid> <new-oid> <ref> ft)
# OK, but has an alternate reference. The alternate reference name
# and other status are given in key=value pairs after the null
# character.
H: PKT-LINE(<old-oid> <new-oid> <ref> ok\0ref=refs/pull/123/head
forced-update)
H: ... ...
H: flush-pktAfter receiving a command, the hook will execute the command, and may create/update different reference. For example, a command for a pseudo reference "refs/for/master/topic" may create/update different reference such as "refs/pull/123/head". The alternate reference name and other status are given in key-value pairs as extended status of the report line.
The list of commands returned from "proc-receive" will replace the relevant commands that are sent from user to "receive-pack", and "receive-pack" will continue to run the "execute_commands" function and other routines. Finally, the result of the execution of these commands will be reported to end user.
The reporting function from "receive-pack" to "send-pack" is also extended using a backward compatible way by adding key-value pairs after an null character, like:
# OK, run this command successfully with optional extended-status. ok <reference>\0ref=refs/pull/123/head old-oid=...
# NO, I reject it.
ng <reference> <error message>
Parsing the extended status of the report lines for "send-pack" is not implemented in this commit, so we can make a compatible test on old version of a Git client.
--- Makefile | 1 + builtin/receive-pack.c | 299 +++++++++++++++- t/helper/test-proc-receive.c | 178 +++++++++ t/helper/test-tool.c | 1 + t/helper/test-tool.h | 1 + t/t5411/common-functions.sh | 10 +- t/t5411/common-test-cases.sh | 673 +++++++++++++++++++++++++++++++++++ 7 files changed, 1151 insertions(+), 12 deletions(-) create mode 100644 t/helper/test-proc-receive.c
Makefile +1 -0
@@ -725,6 +725,7 @@ TEST_BUILTINS_OBJS += test-parse-pathspec-file.o TEST_BUILTINS_OBJS += test-path-utils.o TEST_BUILTINS_OBJS += test-pkt-line.o TEST_BUILTINS_OBJS += test-prio-queue.o +TEST_BUILTINS_OBJS += test-proc-receive.o TEST_BUILTINS_OBJS += test-progress.o TEST_BUILTINS_OBJS += test-reach.o TEST_BUILTINS_OBJS += test-read-cache.o
builtin/receive-pack.c +290 -9
351 lines not on this page · raw message
t/helper/test-proc-receive.c new file +178 -0
178 lines not on this page · raw message
t/helper/test-tool.c +1 -0
@@ -44,6 +44,7 @@ static struct test_cmd cmds[] = { { "path-utils", cmd__path_utils }, { "pkt-line", cmd__pkt_line }, { "prio-queue", cmd__prio_queue }, + { "proc-receive", cmd__proc_receive}, { "progress", cmd__progress }, { "reach", cmd__reach }, { "read-cache", cmd__read_cache },
t/helper/test-tool.h +1 -0
@@ -34,6 +34,7 @@ int cmd__parse_pathspec_file(int argc, const char** argv);int cmd__path_utils (int argc,const char **argv);int cmd__pkt_line (int argc,const char **argv);int cmd__prio_queue (int argc,const char **argv); +int cmd__proc_receive (int argc,const char **argv);int cmd__progress (int argc,const char **argv);int cmd__reach (int argc,const char **argv);int cmd__read_cache (int argc,const char **argv);
t/t5411/common-functions.sh +7 -3
@@ -34,8 +34,11 @@ create_commits_in () { # Format the output of git-push, git-show-ref and other commands to make a # user-friendly and stable text. We can easily prepare the expect text # without having to worry about future changes of the commit ID and spaces -# of the output. We also replce single quotes with double quotes, because -# it is boring to prepare unquoted single quotes in expect text. +# of the output. Single quotes are replaced with double quotes, because +# it is boring to prepare unquoted single quotes in expect text. We also +# remove some locale error messages, which break test if we turn on +# `GIT_TEST_GETTEXT_POISON=true` in order to test unintentional translations +# on plumbing commands. make_user_friendly_and_stable_output_common () { sed \ -e "s/ *\$//" \
@@ -46,5 +49,6 @@ make_user_friendly_and_stable_output_common () { -e "s/$TAG/<TAG-v123>/g" \ -e "s/$ZERO_OID/<ZERO-OID>/g" \ -e "s/$(echo $A | cut -c1-7)[0-9a-f]*/<OID-A>/g" \ - -e "s/$(echo $B | cut -c1-7)[0-9a-f]*/<OID-B>/g" + -e "s/$(echo $B | cut -c1-7)[0-9a-f]*/<OID-B>/g" \ + -e "/^error: / d" }
t/t5411/common-test-cases.sh +673 -0
676 lines not on this page · raw message
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v11 4/7 send-pack: extension for client-side status report
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
This is the client-side extension for status report of git-push.
The "proc-receive" hook may receive a command for a pseudo-reference with a zero-old as its old-oid, while the result of the hook may point to an alternate reference and the reference may exist already with a non-zero old-oid. Git client may receive a report with extened status as the following example:
ok refs/for/master/topic\0ref=refs/pull/123/head old-oid=...
Parse the key-value pairs in the extended status after the null character of the report, and show the proper reference name, old-oid and new-oid to the user.
--- remote.c | 28 +++++++++++++++ remote.h | 3 ++ send-pack.c | 13 ++++++- t/t5411/common-test-cases.sh | 6 ++-- transport-helper.c | 66 +++++++++++++++++++----------------- transport.c | 33 +++++++++++++++--- 6 files changed, 110 insertions(+), 39 deletions(-)
remote.c +28 -0
@@ -1,5 +1,6 @@#include "cache.h" #include "config.h" +#include "connect.h" #include "remote.h" #include "refs.h" #include "refspec.h"
@@ -2311,3 +2312,30 @@ void apply_push_cas(struct push_cas_option *cas,for (ref = remote_refs; ref; ref = ref->next)apply_cas (cas, remote, ref); } + +void update_ref_from_remote_status (struct ref *ref) +{ +char *val; +char c; +int len; + +if (!ref->remote_status) +return ; + + val = (char *)parse_feature_value (ref->remote_status,"old-oid" , &len); +if (val && len) { + c = *(val+len); + *(val+len) ='\0' ; +get_oid_hex (val, &ref->old_oid); + *(val+len) = c; + } + val = (char *)parse_feature_value (ref->remote_status,"new-oid" , &len); +if (val && len) { + c = *(val+len); + *(val+len) ='\0' ; +get_oid_hex (val, &ref->new_oid); + *(val+len) = c; + } +if (parse_feature_request (ref->remote_status,"forced-update" )) + ref->forced_update =1 ; +}
remote.h +3 -0
@@ -345,4 +345,7 @@ int parseopt_push_cas_option(const struct option *, const char *arg, int unset);int is_empty_cas (const struct push_cas_option *);void apply_push_cas (struct push_cas_option *,struct remote *,struct ref *); +/* Parse key-value pairs of remote_status and update the reference accordingly */ +void update_ref_from_remote_status (struct ref *ref); +#endif
send-pack.c +12 -1
@@ -159,6 +159,9 @@ static int receive_status(struct packet_reader *reader, struct ref *refs)while (1 ) {const char *refname;char *msg; +char *extended_status =NULL ; +int len; +if (packet_reader_read (reader) !=PACKET_READ_NORMAL )break ;if (!starts_with (reader->line,"ok " ) && !starts_with (reader->line,"ng " )) {
@@ -167,10 +170,13 @@ static int receive_status(struct packet_reader *reader, struct ref *refs)break ; } + len =strlen (reader->line); refname = reader->line +3 ; msg =strchr (refname,' ' );if (msg) *msg++ ='\0' ; +if (reader->pktlen > len) + extended_status = (char *)reader->line + len +1 ;/* first try searching at our hint, falling back to all refs */ if (hint)
@@ -194,7 +200,12 @@ static int receive_status(struct packet_reader *reader, struct ref *refs) hint->status =REF_STATUS_REMOTE_REJECT ; ret =-1 ; } - hint->remote_status =xstrdup_or_null (msg); +if (msg) { + hint->remote_status =xstrdup (msg); + }else if (extended_status) { + hint->remote_status =xstrdup (extended_status); +update_ref_from_remote_status (hint); + }/* start our next search from the next ref */ hint = hint->next; }
t/t5411/common-test-cases.sh +3 -3
@@ -640,9 +640,9 @@ test_expect_success "proc-receive: report with extended status" ' remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/topic remote: post-receive< <COMMIT-B> <COMMIT-A> refs/for/master/topic To <URL/of/upstream.git> - * [new reference] HEAD -> refs/for/next/topic + * [new reference] HEAD -> refs/pull/123/head * [new reference] HEAD -> refs/for/a/b/c/topic - * [new reference] HEAD -> refs/for/master/topic + + <OID-B>...<OID-A> HEAD -> refs/pull/124/head (forced update) EOF test_cmp expect actual && git -C "$upstream" show-ref >out &&
@@ -701,7 +701,7 @@ test_expect_success "proc-receive: report update of mixed refs" ' * [new branch] HEAD -> baz * [new reference] HEAD -> refs/for/next/topic * [new branch] HEAD -> foo - * [new reference] HEAD -> refs/for/master/topic + <OID-A>..<OID-B> HEAD -> refs/for/master/topic EOF test_cmp expect actual && git -C "$upstream" show-ref >out &&
transport-helper.c +35 -31
78 lines not on this page · raw message
transport.c +29 -4
@@ -463,11 +463,30 @@ static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain, int summary_width) { + char *from_name = NULL; +char *to_name =NULL ; +const char *val; +int len; + +if (from && from->remote_status) { + val =parse_feature_value (from->remote_status,"ref" , &len); +if (val && len) + from_name =xmemdupz (val, len); + } +if (to && to->remote_status) { + val =parse_feature_value (to->remote_status,"ref" , &len); +if (val && len) + to_name =xmemdupz (val, len); + } +if (porcelain) {if (from) - fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name); +fprintf (stdout,"%c\t%s:%s\t" , flag, + from_name ? from_name : from->name, + to_name ? to_name: to->name);else -fprintf (stdout,"%c\t:%s\t" , flag, to->name); +fprintf (stdout,"%c\t:%s\t" , flag, + to_name ? to_name: to->name);if (msg)fprintf (stdout,"%s (%s)\n" , summary, msg);else
@@ -481,9 +500,11 @@ static void print_ref_status(char flag, const char *summary,fprintf (stderr," %s%c %-*s%s " , red, flag, summary_width, summary, reset);if (from) -fprintf (stderr,"%s -> %s" ,prettify_refname (from->name),prettify_refname (to->name)); +fprintf (stderr,"%s -> %s" , +prettify_refname (from_name ? from_name : from->name), +prettify_refname (to_name ? to_name : to->name));else -fputs (prettify_refname (to->name), stderr); +fputs (prettify_refname (to_name ? to_name : to->name), stderr);if (msg) {fputs (" (" , stderr);fputs (msg, stderr);
@@ -491,6 +512,10 @@ static void print_ref_status(char flag, const char *summary, }fputc ('\n' , stderr); } +if (from_name) +free (from_name); +if (to_name) +free (to_name); }static void print_ok_ref_status (struct ref *ref,int porcelain,int summary_width)
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v11 5/7 refs.c: refactor to reuse ref_is_hidden()
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Add new function ref_is_matched() to reuse ref_is_hidden(). Will use this function for receive-pack to check commands with specific prefixes.
Test case t5512 covered this change.
--- refs.c | 13 +++++++++---- refs.h | 1 + 2 files changed, 10 insertions(+), 4 deletions(-)
refs.c +9 -4
@@ -1388,14 +1388,14 @@ int parse_hide_refs_config(const char *var, const char *value, const char *sectireturn 0 ; } -int ref_is_hidden (const char *refname,const char *refname_full) +int ref_matches (struct string_list *match_refs,const char *refname,const char *refname_full) {int i; -if (!hide_refs) +if (!match_refs)return 0 ; -for (i = hide_refs->nr -1 ; i >=0 ; i--) { -const char *match = hide_refs->items[i].string; +for (i = match_refs->nr -1 ; i >=0 ; i--) { +const char *match = match_refs->items[i].string;const char *subject;int neg =0 ;const char *p;
@@ -1421,6 +1421,11 @@ int ref_is_hidden(const char *refname, const char *refname_full)return 0 ; } +int ref_is_hidden (const char *refname,const char *refname_full) +{ +return ref_matches (hide_refs, refname, refname_full); +} +const char *find_descendant_ref (const char *dirname,const struct string_list *extras,const struct string_list *skip)
refs.h +1 -0
@@ -739,6 +739,7 @@ int parse_hide_refs_config(const char *var, const char *value, const char *);* parameter always points to the full ref name. */ int ref_is_hidden (const char *,const char *); +int ref_matches (struct string_list *,const char *,const char *);enum ref_type {REF_TYPE_PER_WORKTREE ,/* refs inside refs/ but not shared */
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v11 6/7 receive-pack: new config receive.procReceiveRefs
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Add a new multi-valued config variable "receive.procReceiveRefs" for receive-pack command, like the follows:
git config --system --add receive.procReceiveRefs refs/for
git config --system --add receive.procReceiveRefs refs/draftsIf the specific prefix strings match the reference names of the commands which are sent from git client to receive-pack, these commands will be executed by an external hook (named "proc-receive"), instead of the internal execute_commands function.
For example, if it is set to "refs/for", pushing to a reference such as "refs/for/master" will not create or update reference "refs/for/master", but may create or update a pull request directly by running the hook "proc-receive".
--- Documentation/config/receive.txt | 14 +++++ builtin/receive-pack.c | 43 ++++++++++++--- t/t5411/common-test-cases.sh | 94 +++++++++++++++++++++++++++++--- 3 files changed, 136 insertions(+), 15 deletions(-)
Documentation/config/receive.txt +14 -0
@@ -114,6 +114,20 @@ receive.hideRefs:: An attempt to update or delete a hidden ref by `git push` is rejected. +receive.procReceiveRefs:: + This is a multi-valued variable that defines reference prefixes + to match the commands in `receive-pack`. Commands matching the + prefixes will be executed by an external hook "proc-receive", + instead of the internal `execute_commands` function. If this + variable is not defined, the "proc-receive" hook will never be + used, and all commands will be executed by the internal + `execute_commands` function. + + For example, if this variable is set to "refs/for", pushing to + reference such as "refs/for/master" will not create or update a + reference named "refs/for/master", but may create or update a + pull request directly by running the hook "proc-receive". + receive.updateServerInfo:: If set to true, git-receive-pack will run git-update-server-info after receiving data from git-push and updating refs.
builtin/receive-pack.c +36 -7
74 lines not on this page · raw message
t/t5411/common-test-cases.sh +86 -8
135 lines not on this page · raw message
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v11 7/7 doc: add documentation for the proc-receive hook
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Add documentation for the new "proc-receive" hook.
--- Documentation/githooks.txt | 58 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+)
Documentation/githooks.txt +58 -0
@@ -333,6 +333,64 @@ The default 'update' hook, when enabled--and with `hooks.allowunannotated` config option unset or set to false--prevents unannotated tags to be pushed. +proc-receive +~~~~~~~~~~~~ + +This hook is invoked by linkgit:git-receive-pack[1]. If the server has +set the multi-valued config variable `receive.procReceiveRefs`, and the +commands sent to 'receive-pack' have matching reference names, these +commands will be executed by this hook, instead of by the internal +`execute_commands()` function. This hook is responsible for updating +the relevant references and reporting the results back to 'receive-pack'. + +This hook executes once for the receive operation. It takes no +arguments, but uses a pkt-line format protocol to communicate with +'receive-pack' to read commands, push-options and send results. In the +following example for the protocol, the letter 'S' stands for +'receive-pack' and the letter 'H' stands for this hook. + + # Version and features negotiation. + S: PKT-LINE(version=1\0push-options atomic...) + S: flush-pkt + H: PKT-LINE(version=1\0push-options...) + H: flush-pkt + + # Send commands from server to the hook. + S: PKT-LINE(<old-oid> <new-oid> <ref>) + S: ... ... + S: flush-pkt + # Send push-options only if the 'push-options' feature is enabled. + S: PKT-LINE(push-option) + S: ... ... + S: flush-pkt + + # Receive result from the hook. + # OK, run this command successfully. + H: PKT-LINE(<old-oid> <new-oid> <ref> ok) + # NO, I reject it. + H: PKT-LINE(<old-oid> <new-oid> <ref> ng <reason>) + # Fall through, let 'receive-pack' to execute it. + H: PKT-LINE(<old-oid> <new-oid> <ref> ft) + # OK, but has an alternate reference. The alternate reference name + # and other status are given in key=value pairs after the null + # character. + H: PKT-LINE(<old-oid> <new-oid> <ref> ok\0ref=refs/pull/123/head + forced-update) + H: ... ... + H: flush-pkt + +Each command for the 'proc-receive' hook may point to a pseudo-reference +and always has a zero-old as its old-oid, while the 'proc-receive' hook +may update an alternate reference and the alternate reference may exist +already with a non-zero old-oid. For this case, this hook may return +different OID and different reference name as extended status of the +report line. + +The report of the commands of this hook should have the same order as +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. + [[post-receive]] post-receive ~~~~~~~~~~~~
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
PATCH v10 1/8 transport: not report a non-head push as a branch
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
When pushing a new reference (not a head or tag), report it as a new reference instead of a new branch.
--- t/t5411-proc-receive-hook.sh | 75 +++++++++++++++++++ t/t5411/common-functions.sh | 50 +++++++++++++ t/t5411/common-test-cases.sh | 43 +++++++++++ t/t5412-proc-receive-hook-http-protocol.sh | 86 ++++++++++++++++++++++ t/t5516-fetch-push.sh | 2 +- transport.c | 9 ++- 6 files changed, 261 insertions(+), 4 deletions(-) create mode 100755 t/t5411-proc-receive-hook.sh create mode 100644 t/t5411/common-functions.sh create mode 100644 t/t5411/common-test-cases.sh create mode 100755 t/t5412-proc-receive-hook-http-protocol.sh
t/t5411-proc-receive-hook.sh new file +75 -0
@@ -0,0 +1,75 @@ +#!/bin/sh +# +# Copyright (c) 2020 Jiang Xin +# + +test_description='Test proc-receive hook' + +. ./test-lib.sh + +. "$TEST_DIRECTORY"/t5411/common-functions.sh + +# Format the output of git-push, git-show-ref and other commands to make a +# user-friendly and stable text. In addition to the common format method, +# we also replace the URL of different protocol for the upstream repository +# with a fixed pattern. +make_user_friendly_and_stable_output () { + make_user_friendly_and_stable_output_common | sed \ + -e "s#To ../upstream.git#To <URL/of/upstream.git>#" +} + +# Refs of upstream : master(B) next(A) +# Refs of workbench: master(A) tags/v123 +test_expect_success "setup" ' + git init --bare upstream.git && + git init workbench && + create_commits_in workbench A B && + ( + cd workbench && + # Try to make a stable fixed width for abbreviated commit ID, + # this fixed-width oid will be replaced with "<OID>". + git config core.abbrev 7 && + git remote add origin ../upstream.git && + git update-ref refs/heads/master $A && + git tag -m "v123" v123 $A && + git push origin \ + $B:refs/heads/master \ + $A:refs/heads/next + ) && + TAG=$(git -C workbench rev-parse v123) && + + # setup pre-receive hook + cat >upstream.git/hooks/pre-receive <<-\EOF && + #!/bin/sh + + echo >&2 "# pre-receive hook" + + while read old new ref + do + echo >&2 "pre-receive< $old $new $ref" + done + EOF + + # setup post-receive hook + cat >upstream.git/hooks/post-receive <<-\EOF && + #!/bin/sh + + echo >&2 "# post-receive hook" + + while read old new ref + do + echo >&2 "post-receive< $old $new $ref" + done + EOF + + chmod a+x \ + upstream.git/hooks/pre-receive \ + upstream.git/hooks/post-receive && + + upstream=upstream.git +' + +# Include test cases for both file and HTTP protocol +. "$TEST_DIRECTORY"/t5411/common-test-cases.sh + +test_done
t/t5411/common-functions.sh new file +50 -0
50 lines not on this page · raw message
t/t5411/common-test-cases.sh new file +43 -0
43 lines not on this page · raw message
t/t5412-proc-receive-hook-http-protocol.sh new file +86 -0
86 lines not on this page · raw message
t/t5516-fetch-push.sh +1 -1
@@ -1039,7 +1039,7 @@ test_force_fetch_tag "annotated tag" "-f -a -m'tag message'" test_expect_success 'push --porcelain' ' mk_empty testrepo && echo >.git/foo "To testrepo" && - echo >>.git/foo "* refs/heads/master:refs/remotes/origin/master [new branch]" && + echo >>.git/foo "* refs/heads/master:refs/remotes/origin/master [new reference]" && echo >>.git/foo "Done" && git push >.git/bar --porcelain testrepo refs/heads/master:refs/remotes/origin/master && (
transport.c +6 -3
@@ -500,9 +500,12 @@ static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widt porcelain, summary_width); else if (is_null_oid(&ref->old_oid))print_ref_status ('*' , - (starts_with (ref->name,"refs/tags/" ) ?"[new tag]" : -"[new branch]" ), - ref, ref->peer_ref,NULL , porcelain, summary_width); + (starts_with (ref->name,"refs/tags/" ) + ?"[new tag]" + : (starts_with (ref->name,"refs/heads/" ) + ?"[new branch]" + :"[new reference]" )), + ref, ref->peer_ref,NULL , porcelain, summary_width); else {struct strbuf quickref =STRBUF_INIT ;char type;
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
Re: [PATCH v10 1/8] transport: not report a non-head push as a branch
Quoted 19 lines · Jiang Xin
create mode 100755 t/t5411-proc-receive-hook.sh create mode 100644 t/t5411/common-functions.sh create mode 100644 t/t5411/common-test-cases.sh create mode 100755 t/t5412-proc-receive-hook-http-protocol.sht/t5411-proc-receive-hook.sh new file +8
-0@@ -0,0 +1,75 @@ +#!/bin/sh +# +# Copyright (c) 2020 Jiang Xin +# + +test_description='Test proc-receive hook' + +. ./test-lib.sh
t/t5411-proc-receive-hook.sh line 18
Quoted 10 lines
t/t5411-proc-receive-hook.sh new file +10
-0+ +. "$TEST_DIRECTORY"/t5411/common-functions.sh + +# Format the output of git-push, git-show-ref and other commands to make a +# user-friendly and stable text. In addition to the common format method, +# we also replace the URL of different protocol for the upstream repository +# with a fixed pattern. +make_user_friendly_and_stable_output () { + make_user_friendly_and_stable_output_common | sed \ + -e "s#To ../upstream.git#To <URL/of/upstream.git>#"
Break the line immediately after the pipe, and begin the next line with the command. That way, the shell knows that you haven't finished giving the command at the end of the first line, and you do not have to use a backslash, i.e.
make_user_friendly |
sed -e 's/from/to/'Quoted 5 lines
t/t5411-proc-receive-hook.sh new file +5
-0+ cd workbench && + # Try to make a stable fixed width for abbreviated commit ID, + # this fixed-width oid will be replaced with "<OID>". + git config core.abbrev 7 && + git remote add origin ../upstream.git &&
t/t5411-proc-receive-hook.sh line 42
Quoted 10 lines
t/t5411-proc-receive-hook.sh new file +10
-0+ git update-ref refs/heads/master $A && + git tag -m "v123" v123 $A && + git push origin \ + $B:refs/heads/master \ + $A:refs/heads/next + ) && + TAG=$(git -C workbench rev-parse v123) && + + # setup pre-receive hook + cat >upstream.git/hooks/pre-receive <<-\EOF &&
Use write_script and you don't need the chmod at the end.
t/t5411-proc-receive-hook.sh line 51
Quoted 9 lines
t/t5411-proc-receive-hook.sh new file +9
-0+ #!/bin/sh + + echo >&2 "# pre-receive hook" + + while read old new ref + do + echo >&2 "pre-receive< $old $new $ref" + done + EOF
Perhaps "exec >&2" upfront?
Quoted 5 lines
t/t5411-proc-receive-hook.sh new file +5
-0+ # setup post-receive hook + cat >upstream.git/hooks/post-receive <<-\EOF && + #!/bin/sh + + echo >&2 "# post-receive hook"
t/t5411-proc-receive-hook.sh line 67
Quoted 10 lines
t/t5411-proc-receive-hook.sh new file +10
-0+ + while read old new ref + do + echo >&2 "post-receive< $old $new $ref" + done + EOF + + chmod a+x \ + upstream.git/hooks/pre-receive \ + upstream.git/hooks/post-receive &&
Ditto.
t/t5411/common-functions.sh
Quoted 15 lines
t/t5411-proc-receive-hook.sh new file +7
-0+ upstream=upstream.git +' + +# Include test cases for both file and HTTP protocol +. "$TEST_DIRECTORY"/t5411/common-test-cases.sh + +test_donet/t5411/common-functions.sh new file +0
-0@@ -0,0 +1,50 @@
It is good that this omits "#!" and leaves the file without +x bit.
t/t5411/common-functions.sh line 38
Quoted 5 lines
t/t5411/common-functions.sh new file +5
-0+# Format the output of git-push, git-show-ref and other commands to make a +# user-friendly and stable text. We can easily prepare the expect text +# without having to worry about future changes of the commit ID and spaces +# of the output. We also replce single quotes with double quotes, because +# it is boring to prepare unquoted single quotes in expect txt.
s/txt/text/
t/t5411/common-functions.sh line 50
Quoted 12 lines
t/t5411/common-functions.sh new file +12
-0+make_user_friendly_and_stable_output_common () { + sed \ + -e "s/ *\$//" \ + -e "s/ */ /g" \ + -e "s/'/\"/g" \ + -e "s/$A/<COMMIT-A>/g" \ + -e "s/$B/<COMMIT-B>/g" \ + -e "s/$TAG/<TAG-v123>/g" \ + -e "s/$ZERO_OID/<ZERO-OID>/g" \ + -e "s/$(echo $A | cut -c1-7)[0-9a-f]*/<OID-A>/g" \ + -e "s/$(echo $B | cut -c1-7)[0-9a-f]*/<OID-B>/g" +}
So a full object name for commit "A" becomes <COMMIT-A> while an abbreviated one becomes <OID-A>? OK. I wonder if we force the full length (i.e. no abbreviation), the tests become simpler, but it is OK. As long as A and B does not share the same 7 hexdigits in any future hash function we choose, the above will do the right thing ;-)
Quoted 21 lines
t/t5412-proc-receive-hook-http-protocol.sh new file +15
-0@@ -0,0 +1,86 @@ +#!/bin/sh +# +# Copyright (c) 2020 Jiang Xin +# + +test_description='Test proc-receive hook for HTTP protocol' + +. ./test-lib.sh + +ROOT_PATH="$PWD" +. "$TEST_DIRECTORY"/lib-gpg.sh +. "$TEST_DIRECTORY"/lib-httpd.sh +. "$TEST_DIRECTORY"/lib-terminal.sh +start_httpd +
t/t5412-proc-receive-hook-http-protocol.sh line 25
Quoted 10 lines
t/t5412-proc-receive-hook-http-protocol.sh new file +10
-0+. "$TEST_DIRECTORY"/t5411/common-functions.sh + +# Format the output of git-push, git-show-ref and other commands to make a +# user-friendly and stable text. In addition to the common format method, +# we also replace the URL of different protocol for the upstream repository +# with a fixed pattern. +make_user_friendly_and_stable_output () { + make_user_friendly_and_stable_output_common | sed \ + -e "s#To http:.*/upstream.git#To <URL/of/upstream.git>#" +}
Ditto.
Quoted 14 lines
t/t5412-proc-receive-hook-http-protocol.sh new file +14
-0+# Refs of upstream : master(B) next(A) +# Refs of workbench: master(A) tags/v123 +test_expect_success "setup" ' + git init --bare upstream.git && + git -C upstream.git config http.receivepack true && + git init workbench && + create_commits_in workbench A B && + ( + cd workbench && + # Try to make a stable fixed width for abbreviated commit ID, + # this fixed-width oid will be replaced with "<OID>". + git config core.abbrev 7 && + git remote add origin ../upstream.git && + git update-ref refs/heads/master $A &&
t/t5412-proc-receive-hook-http-protocol.sh line 50
Quoted 10 lines
t/t5412-proc-receive-hook-http-protocol.sh new file +10
-0+ git tag -m "v123" v123 $A && + git push origin \ + $B:refs/heads/master \ + $A:refs/heads/next + ) && + TAG=$(git -C workbench rev-parse v123) && + + # setup pre-receive hook + cat >upstream.git/hooks/pre-receive <<-\EOF && + #!/bin/sh
Ditto.
Quoted 54 lines
t/t5412-proc-receive-hook-http-protocol.sh new file +36
-0+ + echo >&2 "# pre-receive hook" + + while read old new ref + do + echo >&2 "pre-receive< $old $new $ref" + done + EOF + + # setup post-receive hook + cat >upstream.git/hooks/post-receive <<-\EOF && + #!/bin/sh + + echo >&2 "# post-receive hook" + + while read old new ref + do + echo >&2 "post-receive< $old $new $ref" + done + EOF + + chmod a+x \ + upstream.git/hooks/pre-receive \ + upstream.git/hooks/post-receive && + + upstream="$HTTPD_DOCUMENT_ROOT_PATH/upstream.git" && + mv upstream.git "$upstream" && + git -C workbench remote set-url origin $HTTPD_URL/smart/upstream.git +' + +setup_askpass_helper + +# Include test cases for both file and HTTP protocol +. "$TEST_DIRECTORY"/t5411/common-test-cases.sh + +test_donet/t5516-fetch-push.sh +1
-1@@ -1039,7 +1039,7 @@ test_force_fetch_tag "annotated tag" "-f -a -m'tag message'" test_expect_success 'push --porcelain' ' mk_empty testrepo && echo >.git/foo "To testrepo" && - echo >>.git/foo "* refs/heads/master:refs/remotes/origin/master [new branch]" && + echo >>.git/foo "* refs/heads/master:refs/remotes/origin/master [new reference]" && echo >>.git/foo "Done" && git push >.git/bar --porcelain testrepo refs/heads/master:refs/remotes/origin/master && (transport.c +0
-0
transport.c static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widt · line 501
Quoted 2 lines
transport.c +0
-0@@ -500,9 +500,12 @@ static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widt porcelain, summary_width); else if (is_null_oid(&ref->old_oid))
This block is about an update to missing "old", i.e. a creation.
transport.c static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widt · line 505
Quoted 4 lines
transport.c +0
-3print_ref_status('*', - (starts_with (ref->name,"refs/tags/" ) ?"[new tag]" : -"[new branch]" ), - ref, ref->peer_ref,NULL , porcelain, summary_width);
We used to say "if outside refs/tags/, let's call it branch".
transport.c static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widt · line 508
Quoted 6 lines
transport.c +6
-0+ (starts_with (ref->name,"refs/tags/" ) + ?"[new tag]" + : (starts_with (ref->name,"refs/heads/" ) + ?"[new branch]" + :"[new reference]" )), + ref, ref->peer_ref, NULL, porcelain, summary_width);
Now we say "outside refs/heads/ and refs/tags/, that's just a reference, different from branch". OK.
Do we ever update refs/remotes/ with this codepath? Would we want to label it differently, i.e. as "remote-tracking branch"?
transport.c static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widt · line 511
Quoted 3 lines
transport.c +0
-0else {struct strbuf quickref =STRBUF_INIT ;char type;
Thanks.
Re: [PATCH v10 1/8] transport: not report a non-head push as a branch
Quoted 11 lines · Junio C Hamano <[email protected]> 于2020年4月13日周一 上午4:26写道:
t/t5411-proc-receive-hook.sh line 18
Quoted 3 lines
t/t5411-proc-receive-hook.sh new file +3
-0+make_user_friendly_and_stable_output () { + make_user_friendly_and_stable_output_common | sed \ + -e "s#To ../upstream.git#To <URL/of/upstream.git>#"Break the line immediately after the pipe, and begin the next line with the command. That way, the shell knows that you haven't finished giving the command at the end of the first line, and you do not have to use a backslash, i.e.
make_user_friendly | sed -e 's/from/to/'
Will do.
Quoted 16 lines
Quoted 5 lines
t/t5411-proc-receive-hook.sh new file +5
-0+ cd workbench && + # Try to make a stable fixed width for abbreviated commit ID, + # this fixed-width oid will be replaced with "<OID>". + git config core.abbrev 7 && + git remote add origin ../upstream.git &&t/t5411-proc-receive-hook.sh line 42
Quoted 10 lines
t/t5411-proc-receive-hook.sh new file +10
-0+ git update-ref refs/heads/master $A && + git tag -m "v123" v123 $A && + git push origin \ + $B:refs/heads/master \ + $A:refs/heads/next + ) && + TAG=$(git -C workbench rev-parse v123) && + + # setup pre-receive hook + cat >upstream.git/hooks/pre-receive <<-\EOF &&
Quoted 1 line
Use
write_scriptand you don't need the chmod at the end.
Yes, it's tedious to write chmod again and again. Will replace all the places for writing script.
Quoted 11 lines
t/t5411-proc-receive-hook.sh line 51
Quoted 9 lines
t/t5411-proc-receive-hook.sh new file +9
-0+ #!/bin/sh + + echo >&2 "# pre-receive hook" + + while read old new ref + do + echo >&2 "pre-receive< $old $new $ref" + done + EOFPerhaps "exec >&2" upfront?
This works for "pre-receive" and "post-receive" script. But in "proc-receive" hook, stdout and stderr are different, and standard output is used for protocol output.
Quoted 9 lines
t/t5411/common-functions.sh
Quoted 6 lines
t/t5411/common-functions.sh new file +0
-0@@ -0,0 +1,50 @@It is good that this omits "#!" and leaves the file without +x bit.
Quoted 7 lines
t/t5411/common-functions.sh line 38
Quoted 5 lines
t/t5411/common-functions.sh new file +5
-0+# Format the output of git-push, git-show-ref and other commands to make a +# user-friendly and stable text. We can easily prepare the expect text +# without having to worry about future changes of the commit ID and spaces +# of the output. We also replce single quotes with double quotes, because +# it is boring to prepare unquoted single quotes in expect txt.s/txt/text/
Will do. Found it for a long time, but forgot to correct it.
Quoted 7 lines
t/t5411/common-functions.sh line 11
Quoted 6 lines
t/t5411/common-functions.sh new file +6
-0+ (starts_with(ref->name, "refs/tags/") + ? "[new tag]" + : (starts_with(ref->name, "refs/heads/") + ? "[new branch]" + : "[new reference]")), + ref, ref->peer_ref, NULL, porcelain, summary_width);
Quoted 5 lines
Now we say "outside refs/heads/ and refs/tags/, that's just a reference, different from branch". OK.
Do we ever update refs/remotes/ with this codepath? Would we want to label it differently, i.e. as "remote-tracking branch"?
Can we call a reference (starts with "refs/remotes/") as a remote-tracking branch, if it is on the remote side without a proper remote and branch settings in .git/config?
Thanks.
PATCH v10 2/8 receive-pack: add new proc-receive hook
In reply to Jiang XinFrom: Jiang Xin <[email protected]>
Git calls an internal execute_commands function to handle commands sent from client to git-receive-pack. Regardless of what references the user pushes, git creates or updates the corresponding references if the user has write-permission. A contributor who has no write-permission, cannot push to the repository directly. So, the contributor has to write commits to an alternate location, and sends pull request by emails or by other ways. We call this workflow as a distributed workflow.
It would be more convenient to work in a centralized workflow like what Gerrit provided for some cases. For example, a read-only user who cannot push to a branch directly can run the following git push command to push commits to a pseudo reference (has a prefix "refs/for/", not "refs/heads/") to create a code review.
git push origin \
HEAD:refs/for/<branch-name>/<session>The <branch-name> in the above example can be as simple as "master", or a more complicated branch name like "foo/bar". The <session> in the above example command can be the local branch name of the client side, such as "my/topic".
We cannot implement a centralized workflow elegantly by using "pre-receive" + "post-receive", because Git will call the internal function "execute_commands" to create references (even the special pseudo reference) between these two hooks. Even though we can delete the temporarily created pseudo reference via the "post-receive" hook, having a temporary reference is not safe for concurrent pushes.
So, add a filter and a new handler to support this kind of workflow. The filter will check the prefix of the reference name, and if the command has a special reference name, the filter will turn a specific field (run_proc_receive) on for the command. Commands with this filed turned on will be executed by a new handler (an hook named "proc-receive") instead of the internal execute_commands function. We can use this "proc-receive" command to create pull requests or send emails for code review.
Suggested by Junio, this "proc-receive" hook reads the commands, push-options (optional), and send result using a protocol in pkt-line format. In the following example, The letter "S" stands for "receive-pack" and letter "H" stands for the hook.
# Version and features negotiation.
S: PKT-LINE(version=1\0push-options atomic...)
S: flush-pkt
H: PKT-LINE(version=1\0push-options...)
H: flush-pkt
# Send commands from server to the hook.
S: PKT-LINE(<old-oid> <new-oid> <ref>)
S: ... ...
S: flush-pkt
# Send push-options only if the 'push-options' feature is enabled.
S: PKT-LINE(push-option)
S: ... ...
S: flush-pkt
# Receive result from the hook.
# OK, run this command successfully.
H: PKT-LINE(<old-oid> <new-oid> <ref> ok)
# NO, I reject it.
H: PKT-LINE(<old-oid> <new-oid> <ref> ng <reason>)
# Fall through, let 'receive-pack' to execute it.
H: PKT-LINE(<old-oid> <new-oid> <ref> ft)
# OK, but has an alternate reference. The alternate reference name
# and other status are given in key=value pairs after the null
# character.
H: PKT-LINE(<old-oid> <new-oid> <ref> ok\0ref=refs/pull/123/head
forced-update)
H: ... ...
H: flush-pktAfter receiving a command, the hook will execute the command, and may create/update different reference. For example, a command for a pseudo reference "refs/for/master/topic" may create/update different reference such as "refs/pull/123/head". The alternate reference name and other status are given in key-value pairs as extended status of the report line.
The list of commands returned from "proc-receive" will replace the relevant commands that are sent from user to "receive-pack", and "receive-pack" will continue to run the "execute_commands" function and other routines. Finally, the result of the execution of these commands will be reported to end user.
--- Makefile | 1 + builtin/receive-pack.c | 260 +++++++++++++++- t/helper/test-proc-receive.c | 168 ++++++++++ t/helper/test-tool.c | 1 + t/helper/test-tool.h | 1 + t/t5411/common-functions.sh | 10 +- t/t5411/common-test-cases.sh | 576 +++++++++++++++++++++++++++++++++++ 7 files changed, 1011 insertions(+), 6 deletions(-) create mode 100644 t/helper/test-proc-receive.c
Makefile +1 -0
@@ -725,6 +725,7 @@ TEST_BUILTINS_OBJS += test-parse-pathspec-file.o TEST_BUILTINS_OBJS += test-path-utils.o TEST_BUILTINS_OBJS += test-pkt-line.o TEST_BUILTINS_OBJS += test-prio-queue.o +TEST_BUILTINS_OBJS += test-proc-receive.o TEST_BUILTINS_OBJS += test-progress.o TEST_BUILTINS_OBJS += test-reach.o TEST_BUILTINS_OBJS += test-read-cache.o
builtin/receive-pack.c +257 -3
306 lines not on this page · raw message
t/helper/test-proc-receive.c new file +168 -0
168 lines not on this page · raw message
t/helper/test-tool.c +1 -0
@@ -44,6 +44,7 @@ static struct test_cmd cmds[] = { { "path-utils", cmd__path_utils }, { "pkt-line", cmd__pkt_line }, { "prio-queue", cmd__prio_queue }, + { "proc-receive", cmd__proc_receive}, { "progress", cmd__progress }, { "reach", cmd__reach }, { "read-cache", cmd__read_cache },
t/helper/test-tool.h +1 -0
@@ -34,6 +34,7 @@ int cmd__parse_pathspec_file(int argc, const char** argv);int cmd__path_utils (int argc,const char **argv);int cmd__pkt_line (int argc,const char **argv);int cmd__prio_queue (int argc,const char **argv); +int cmd__proc_receive (int argc,const char **argv);int cmd__progress (int argc,const char **argv);int cmd__reach (int argc,const char **argv);int cmd__read_cache (int argc,const char **argv);
t/t5411/common-functions.sh +7 -3
@@ -34,8 +34,11 @@ create_commits_in () { # Format the output of git-push, git-show-ref and other commands to make a # user-friendly and stable text. We can easily prepare the expect text # without having to worry about future changes of the commit ID and spaces -# of the output. We also replce single quotes with double quotes, because -# it is boring to prepare unquoted single quotes in expect txt. +# of the output. Single quotes are replaced with double quotes, because +# it is boring to prepare unquoted single quotes in expect txt. We also +# remove some locale error messages, which break test if we turn on +# `GIT_TEST_GETTEXT_POISON=true` in order to test unintentional translations +# on plumbing commands. make_user_friendly_and_stable_output_common () { sed \ -e "s/ *\$//" \
@@ -46,5 +49,6 @@ make_user_friendly_and_stable_output_common () { -e "s/$TAG/<TAG-v123>/g" \ -e "s/$ZERO_OID/<ZERO-OID>/g" \ -e "s/$(echo $A | cut -c1-7)[0-9a-f]*/<OID-A>/g" \ - -e "s/$(echo $B | cut -c1-7)[0-9a-f]*/<OID-B>/g" + -e "s/$(echo $B | cut -c1-7)[0-9a-f]*/<OID-B>/g" \ + -e "/^error: / d" }
t/t5411/common-test-cases.sh +576 -0
579 lines not on this page · raw message
Signature · 1 line
2.24.1.15.g448c31058d.agit.4.5
Re: [PATCH v10 2/8] receive-pack: add new proc-receive hook
Quoted 8 lines · Jiang Xin
The filter will check the prefix of the reference name, and if the command has a special reference name, the filter will turn a specific field (
run_proc_receive) on for the command. Commands with this filed turned on will be executed by a new handler (an hook named "proc-receive") instead of the internalexecute_commandsfunction. We can use this "proc-receive" command to create pull requests or send emails for code review.
Quoted 4 lines
Suggested by Junio, this "proc-receive" hook reads the commands, push-options (optional), and send result using a protocol in pkt-line format. In the following example, The letter "S" stands for "receive-pack" and letter "H" stands for the hook.
Heh, you're giving me too much credit here. The exchange illustrated below makes sense to me, ...
Quoted 15 lines
# Version and features negotiation.
S: PKT-LINE(version=1\0push-options atomic...)
S: flush-pkt
H: PKT-LINE(version=1\0push-options...)
H: flush-pkt# Send commands from server to the hook.
S: PKT-LINE(<old-oid> <new-oid> <ref>)
S: ... ...
S: flush-pkt
# Send push-options only if the 'push-options' feature is enabled.
S: PKT-LINE(push-option)
S: ... ...
S: flush-pkt
Quoted 12 lines
# Receive result from the hook.
# OK, run this command successfully.
H: PKT-LINE(<old-oid> <new-oid> <ref> ok)
# NO, I reject it.
H: PKT-LINE(<old-oid> <new-oid> <ref> ng <reason>)
# Fall through, let 'receive-pack' to execute it.
H: PKT-LINE(<old-oid> <new-oid> <ref> ft)
# OK, but has an alternate reference. The alternate reference name # and other status are given in key=value pairs after the null # character.
H: PKT-LINE(<old-oid> <new-oid> <ref> ok\0ref=refs/pull/123/head
forced-update)
The semantics of this one is fuzzy. We made an update to a ref that is different from the one that was requested (presumably that is what is reported after "\0ref="), OK, but did we update the ref in the request, too, or did we leave the ref in the request intact? Or, do we say "no\0ref=..." if we don't update the requested ref and instead update a different one? Let's hold the answer at this point and keep thinking...
Quoted 10 lines
H: ... ... H: flush-pktAfter receiving a command, the hook will execute the command, and may create/update different reference. For example, a command for a pseudo reference "refs/for/master/topic" may create/update different reference such as "refs/pull/123/head". The alternate reference name and other status are given in key-value pairs as extended status of the report line.
Quoted 5 lines
The list of commands returned from "proc-receive" will replace the relevant commands that are sent from user to "receive-pack", and "receive-pack" will continue to run the "
execute_commands" function and other routines. Finally, the result of the execution of these commands will be reported to end user.
It is not clear if there needs to be any correspondence between the sequence of input commands and the sequence of output commands. I am guessing that there is not any and the hook is allowed to do anything as it wants to. For example:
- it is OK to execute them all and report them, but in a totally random order.
- it is OK to ignore all input and report no update (not even "ng").
- it is OK to be asked to update one ref, but update and report updates to two refs that are not related to the requested ref.
Is my understanding correct?
Or does the design tie the set of input and output ref updates closely together, i.e. one input corresponds to one output and they are in order, so all the hook is allowed to do is
- to execute and update as it is told, and report "ok",
- to reject and report "ng", or
- to update *another* ref without updating the requested one, with "ok\0ref=..." mechanism.
I am not sure which one is more sensible, though.
If we choose to use the "anything goes" approach, I do not think there is no need for the "ok\nref=..." mechanism---we can just give two output records, one "ok" for the original request, and a new "ok" that reports the "we updated this one instead".
Quoted 55 lines
builtin/receive-pack.c +43
-1+#define RUN_PROC_RECEIVE_SCHEDULED 1 +#define RUN_PROC_RECEIVE_RETURNED 2struct command {struct command *next;const char *error_string;unsigned int skip_update:1 , - did_not_exist:1 ; + did_not_exist:1 , + run_proc_receive:2 ;int index;struct object_id old_oid;struct object_id new_oid;@@ -817,6 +820,236 @@ static int run_update_hook(struct command *cmd)return finish_command (&proc); } +static struct command *find_command_by_refname (const struct command *list, +const char *refname) +{ +for (; list; list = list->next) +if (!strcmp (list->ref_name, refname)) +return (struct command *)list; +return NULL ; +} + +static int read_proc_receive_report (struct packet_reader *reader, +struct command *commands, +struct strbuf *errmsg) +{ +struct command *cmd; +struct command *hint =NULL ; +int code =0 ; + +for (;;) { +struct object_id old_oid, new_oid; +const char *refname; +const char *p; +char *status; +char *msg =NULL ; + +if (packet_reader_read (reader) !=PACKET_READ_NORMAL ) +break ; +if (parse_oid_hex (reader->line, &old_oid, &p) || + *p++ !=' ' || +parse_oid_hex (p, &new_oid, &p) || + *p++ !=' ' ) { +strbuf_addf (errmsg,"proc-receive expected 'old new ref status [msg]', got '%s'\n" , + reader->line); + code =-1 ; +continue ; + } + + refname = p; + status =strchr (p,' ' ); +if (!status) {
builtin/receive-pack.c static int run_update_hook(struct command *cmd) · line 871
Quoted 10 lines
builtin/receive-pack.c +10
-0+strbuf_addf (errmsg,"proc-receive expected 'old new ref status [msg]', got '%s'\n" , + reader->line); + code =-1 ; +continue ; + } + *status++ ='\0' ; +if (strlen (status) >2 && *(status +2 ) ==' ' ) { + msg = status +2 ; + *msg++ ='\0' ; + }
So, here we parsed "<old> <new> <ref> (ok|ng)", with status pointing at ok/ng, or "<old> <new> <ref> (ok|ng) <why>", with status pointing at ok/ng and msg pointing at <why>. refname points at <ref>.
Most notably, "ok\0ref=..." is not handled any specially from a normal "ok" here.
Quoted 11 lines
builtin/receive-pack.c +11
-0+if (strlen (status) !=2 ) { +strbuf_addf (errmsg,"proc-receive has bad status '%s' for '%s'\n" , + status, reader->line); + code =-1 ; +continue ; + } + +/* first try searching at our hint, falling back to all refs */ +if (hint) + hint =find_command_by_refname (hint, refname); +if (!hint)
builtin/receive-pack.c static int run_update_hook(struct command *cmd) · line 892
Quoted 10 lines
builtin/receive-pack.c +10
-0+ hint =find_command_by_refname (commands, refname); +if (!hint) { +strbuf_addf (errmsg,"proc-receive reported status on unknown ref: %s\n" , + refname); +continue ; + } +if (!hint->run_proc_receive) { +strbuf_addf (errmsg,"proc-receive reported status on ref of builtin command: %s\n" , + refname); +continue ;
Updates against a request to update certain ref hierarchies by the hook is rejected here.
builtin/receive-pack.c static int run_update_hook(struct command *cmd) · line 899
Quoted 7 lines
builtin/receive-pack.c +7
-0+ } +if (!strcmp (status,"ng" )) { +if (msg) + hint->error_string =xstrdup (msg); +else + hint->error_string ="failed" ; + code =-1 ;
The <why> in "(ok|ng) <why>" message is only used for "ng", which is probably good, as the reason why something succeeded is not usually very interesting.
builtin/receive-pack.c static int run_update_hook(struct command *cmd) · line 904
Quoted 5 lines
builtin/receive-pack.c +5
-0+ }else if (strcmp ("ok" , status)) { +strbuf_addf (errmsg,"proc-receive has bad status '%s' for '%s'\n" , + status, reader->line); + code =-1 ; +/* Skip marking it as RUN_PROC_RECEIVE_RETURNED */
But then shouldn't we be complaining if msg is not NULL here, instead of silently ignoring? Also we didn't see what happened to the promised "ok\0ref=..." stuff here, or the passthru "ft". Puzzled...
builtin/receive-pack.c static int run_update_hook(struct command *cmd) · line 909
Quoted 5 lines
builtin/receive-pack.c +5
-0+continue ; + } +oidcpy (&hint->old_oid, &old_oid); +oidcpy (&hint->new_oid, &new_oid); + hint->run_proc_receive |=RUN_PROC_RECEIVE_RETURNED ;
Or, is this last one the catch all for "ft" (in other words, the hook does not have to say "ft", but as long as it says two non-SP letters that are not "ok" nor "ng", it is taken as "ft")?
builtin/receive-pack.c static int run_update_hook(struct command *cmd) · line 920
Quoted 11 lines
builtin/receive-pack.c +11
-0+ } + +for (cmd = commands; cmd; cmd = cmd->next) +if (cmd->run_proc_receive && !cmd->error_string && + !(cmd->run_proc_receive &RUN_PROC_RECEIVE_RETURNED )) { + cmd->error_string ="no report from proc-receive" ; + code =-1 ; + } + +return code; +}
OK, so this sort-of answers my earlier question. But not quite...
The output records and the input requests are tied by "<ref>" each input record wanted to udpate. The order does not matter, but not having a corresponding report in the hook's output is like the hook reporting a "ng" failure. It also means that the hook can update two refs in response to one request, but it is awkward.
Quoted 16 lines
builtin/receive-pack.c +16
-0+static int run_proc_receive_hook (struct command *commands, +const struct string_list *push_options) +{ +struct child_process proc =CHILD_PROCESS_INIT ; +struct async muxer; +struct command *cmd; +const char *argv[2 ]; +struct packet_reader reader; +struct strbuf cap =STRBUF_INIT ; +struct strbuf errmsg =STRBUF_INIT ; +int pr_use_push_options =0 ; +int version =0 ; +int code; + + argv[0 ] =find_hook ("proc-receive" ); +if (!argv[0 ]) {
builtin/receive-pack.c static int run_update_hook(struct command *cmd) · line 947
Quoted 10 lines
builtin/receive-pack.c +10
-0+rp_error ("cannot find hook 'proc-receive'" ); +return -1 ; + } + argv[1 ] =NULL ; + + proc.argv = argv; + proc.in =-1 ; + proc.out =-1 ; + proc.trace2_hook_name ="proc-receive" ; +
Isn't this a brand new protocol between receive-pack and a hook? I am puzzled why we want a choice between using and not using sideband. It's not like you are maintaining compatibility with ancient version of Git that talked with proc-receive hook but without sideband, because it did not know how to multiplex sideband communication. Shouldn't we just always assume that the sideband is available?
Or are we just letting the hook directly answer the push client and that is why this thing needs to know if we are using sideband between us and the client? I kind of expected that you'd keep the two communication channels on both sides isolated so that the side that talks with the hook does not need to know how we are talking with the client.
Quoted 27 lines
builtin/receive-pack.c +25
-0+if (use_sideband) { +memset (&muxer,0 ,sizeof (muxer)); + muxer.proc = copy_to_sideband; + muxer.in =-1 ; + code =start_async (&muxer); +if (code) +return code; + proc.err = muxer.in; + }else { + proc.err =0 ; + } + + code =start_command (&proc); +if (code) { +if (use_sideband) +finish_async (&muxer); +return code; + } + +sigchain_push (SIGPIPE ,SIG_IGN ); + +/* Version negotiaton */ +packet_reader_init (&reader, proc.out,NULL ,0 , +PACKET_READ_CHOMP_NEWLINE | +PACKET_READ_DIE_ON_ERR_PACKET );
builtin/receive-pack.c static void execute_commands(struct command *commands, · line 1734
Quoted 4 lines
builtin/receive-pack.c +1
-0@@ -1497,6 +1731,20 @@ static void execute_commands(struct command *commands,reject_updates_to_hidden (commands); +/* Try to find commands that have special prefix in their reference names,
Style?
Quoted 2 lines
builtin/receive-pack.c +2
-0+* and mark them to run an external "proc-receive" hook later. +*/