git/t/helper/test-proc-receive.c
Jiang Xin 15d3af5e22 receive-pack: add new proc-receive 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 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 (a 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(ok <ref>)
    # NO, I reject it.
    H: PKT-LINE(ng <ref> <reason>)
    # Fall through, let 'receive-pack' to execute it.
    H: PKT-LINE(ok <ref>)
    H: PKT-LINE(option fall-through)
    # OK, but has an alternate reference.  The alternate reference name
    # and other status can be given in options
    H: PKT-LINE(ok <ref>)
    H: PKT-LINE(option refname <refname>)
    H: PKT-LINE(option old-oid <old-oid>)
    H: PKT-LINE(option new-oid <new-oid>)
    H: PKT-LINE(option forced-update)
    H: ... ...
    H: flush-pkt

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

The reporting function from "receive-pack" to "send-pack" will be
extended in latter commit just like what the "proc-receive" hook reports
to "receive-pack".

Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-08-27 12:47:47 -07:00

177 lines
4.5 KiB
C

#include "cache.h"
#include "connect.h"
#include "parse-options.h"
#include "pkt-line.h"
#include "sigchain.h"
#include "test-tool.h"
static const char *proc_receive_usage[] = {
"test-tool proc-receive [<options>...]",
NULL
};
static int die_version;
static int die_readline;
static int no_push_options;
static int use_atomic;
static int use_push_options;
static int verbose;
static int version = 1;
static struct string_list returns = STRING_LIST_INIT_NODUP;
struct command {
struct command *next;
const char *error_string;
unsigned int skip_update:1,
did_not_exist:1;
int index;
struct object_id old_oid;
struct object_id new_oid;
char ref_name[FLEX_ARRAY]; /* more */
};
static void proc_receive_verison(struct packet_reader *reader) {
int server_version = 0;
for (;;) {
int linelen;
if (packet_reader_read(reader) != PACKET_READ_NORMAL)
break;
if (reader->pktlen > 8 && starts_with(reader->line, "version=")) {
server_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, "atomic"))
use_atomic= 1;
if (parse_feature_request(feature_list, "push-options"))
use_push_options = 1;
}
}
}
if (server_version != 1 || die_version)
die("bad protocol version: %d", server_version);
packet_write_fmt(1, "version=%d%c%s\n",
version, '\0',
use_push_options && !no_push_options ? "push-options": "");
packet_flush(1);
}
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;
if (parse_oid_hex(reader->line, &old_oid, &p) ||
*p++ != ' ' ||
parse_oid_hex(p, &new_oid, &p) ||
*p++ != ' ' ||
die_readline)
die("protocol error: expected 'old new ref', got '%s'",
reader->line);
refname = p;
FLEX_ALLOC_STR(cmd, ref_name, refname);
oidcpy(&cmd->old_oid, &old_oid);
oidcpy(&cmd->new_oid, &new_oid);
*tail = cmd;
tail = &cmd->next;
}
}
static void proc_receive_read_push_options(struct packet_reader *reader,
struct string_list *options)
{
if (no_push_options || !use_push_options)
return;
while (1) {
if (packet_reader_read(reader) != PACKET_READ_NORMAL)
break;
string_list_append(options, reader->line);
}
}
int cmd__proc_receive(int argc, const char **argv)
{
int nongit_ok = 0;
struct packet_reader reader;
struct command *commands = NULL;
struct string_list push_options = STRING_LIST_INIT_DUP;
struct string_list_item *item;
struct option options[] = {
OPT_BOOL(0, "no-push-options", &no_push_options,
"disable push options"),
OPT_BOOL(0, "die-version", &die_version,
"die during version negotiation"),
OPT_BOOL(0, "die-readline", &die_readline,
"die when readline"),
OPT_STRING_LIST('r', "return", &returns, "old/new/ref/status/msg",
"return of results"),
OPT__VERBOSE(&verbose, "be verbose"),
OPT_INTEGER('V', "version", &version,
"use this protocol version number"),
OPT_END()
};
setup_git_directory_gently(&nongit_ok);
argc = parse_options(argc, argv, "test-tools", options, proc_receive_usage, 0);
if (argc > 0)
usage_msg_opt("Too many arguments.", proc_receive_usage, options);
packet_reader_init(&reader, 0, NULL, 0,
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);
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)
fprintf(stderr, "proc-receive< %s %s %s\n",
oid_to_hex(&cmd->old_oid),
oid_to_hex(&cmd->new_oid),
cmd->ref_name);
if (push_options.nr > 0)
for_each_string_list_item(item, &push_options)
fprintf(stderr, "proc-receive< %s\n", item->string);
if (returns.nr)
for_each_string_list_item(item, &returns)
fprintf(stderr, "proc-receive> %s\n", item->string);
}
if (returns.nr)
for_each_string_list_item(item, &returns)
packet_write_fmt(1, "%s\n", item->string);
packet_flush(1);
sigchain_pop(SIGPIPE);
return 0;
}