]> git.ipfire.org Git - thirdparty/git.git/commit - t/t5411/test-0027-push-options--porcelain.sh
receive-pack: add new proc-receive hook
authorJiang Xin <zhiyou.jx@alibaba-inc.com>
Thu, 27 Aug 2020 15:45:44 +0000 (11:45 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 27 Aug 2020 19:47:47 +0000 (12:47 -0700)
commit15d3af5e22eceb9cd6cd341703d612a62194451e
tree0c1752351db2b4bd7c4e1e1e55276bc0af044551
parent38b9197a76a77671a8f48e423236739e7556c20d
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>
28 files changed:
Makefile
builtin/receive-pack.c
remote.h
t/helper/test-proc-receive.c [new file with mode: 0644]
t/helper/test-tool.c
t/helper/test-tool.h
t/t5411/test-0011-no-hook-error.sh [new file with mode: 0644]
t/t5411/test-0012-no-hook-error--porcelain.sh [new file with mode: 0644]
t/t5411/test-0013-bad-protocol.sh [new file with mode: 0644]
t/t5411/test-0014-bad-protocol--porcelain.sh [new file with mode: 0644]
t/t5411/test-0020-report-ng.sh [new file with mode: 0644]
t/t5411/test-0021-report-ng--porcelain.sh [new file with mode: 0644]
t/t5411/test-0022-report-unexpect-ref.sh [new file with mode: 0644]
t/t5411/test-0023-report-unexpect-ref--porcelain.sh [new file with mode: 0644]
t/t5411/test-0024-report-unknown-ref.sh [new file with mode: 0644]
t/t5411/test-0025-report-unknown-ref--porcelain.sh [new file with mode: 0644]
t/t5411/test-0026-push-options.sh [new file with mode: 0644]
t/t5411/test-0027-push-options--porcelain.sh [new file with mode: 0644]
t/t5411/test-0030-report-ok.sh [new file with mode: 0644]
t/t5411/test-0031-report-ok--porcelain.sh [new file with mode: 0644]
t/t5411/test-0032-report-with-options.sh [new file with mode: 0644]
t/t5411/test-0033-report-with-options--porcelain.sh [new file with mode: 0644]
t/t5411/test-0034-report-ft.sh [new file with mode: 0644]
t/t5411/test-0035-report-ft--porcelain.sh [new file with mode: 0644]
t/t5411/test-0036-report-multi-rewrite-for-one-ref.sh [new file with mode: 0644]
t/t5411/test-0037-report-multi-rewrite-for-one-ref--porcelain.sh [new file with mode: 0644]
t/t5411/test-0038-report-mixed-refs.sh [new file with mode: 0644]
t/t5411/test-0039-report-mixed-refs--porcelain.sh [new file with mode: 0644]