receive-pack: new config receive.procReceiveRefs

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 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".

Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jiang Xin
2020-05-18 05:40:36 -04:00
committed by Junio C Hamano
parent fc2cadab10
commit 2dd1150a0a
6 changed files with 288 additions and 7 deletions

View File

@ -77,6 +77,7 @@ static struct object_id push_cert_oid;
static struct signature_check sigcheck;
static const char *push_cert_nonce;
static const char *cert_nonce_seed;
static struct string_list proc_receive_refs;
static const char *NONCE_UNSOLICITED = "UNSOLICITED";
static const char *NONCE_BAD = "BAD";
@ -229,6 +230,20 @@ static int receive_pack_config(const char *var, const char *value, void *cb)
return 0;
}
if (strcmp(var, "receive.procreceiverefs") == 0) {
char *prefix;
int len;
if (!value)
return config_error_nonbool(var);
prefix = xstrdup(value);
len = strlen(prefix);
while (len && prefix[len - 1] == '/')
prefix[--len] = '\0';
string_list_append(&proc_receive_refs, prefix);
return 0;
}
return git_default_config(var, value, cb);
}
@ -1810,15 +1825,26 @@ 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.
*/
for (cmd = commands; cmd; cmd = cmd->next) {
if (!should_process_cmd(cmd))
continue;
if (proc_receive_refs.nr > 0) {
struct strbuf refname_full = STRBUF_INIT;
size_t prefix_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_matches(&proc_receive_refs, cmd->ref_name, refname_full.buf)) {
cmd->run_proc_receive = RUN_PROC_RECEIVE_SCHEDULED;
run_proc_receive = 1;
}
}
strbuf_release(&refname_full);
}
if (run_receive_hook(commands, "pre-receive", 0, push_options)) {
@ -2319,6 +2345,8 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
OPT_END()
};
string_list_init(&proc_receive_refs, 0);
packet_trace_identity("receive-pack");
argc = parse_options(argc, argv, prefix, options, receive_pack_usage, 0);
@ -2436,5 +2464,6 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
oid_array_clear(&shallow);
oid_array_clear(&ref);
free((void *)push_cert_nonce);
string_list_clear(&proc_receive_refs, 0);
return 0;
}