@matheusbsbs / matheus-bagliano / commits / f43cd49fb8
Fork 0

Change {pre,post}-receive hooks to use stdin

Sergey Vlasov, Andy Parkins and Alex Riesen all pointed out that it is possible for a single invocation of receive-pack to be given more refs than the OS might allow us to pass as command line parameters to a single hook invocation. We don't want to break these up into multiple invocations (like xargs might do) as that makes it impossible for the pre-receive hook to verify multiple related ref updates occur at the same time, and it makes it harder for post-receive to send out a single batch notification. Instead we pass the reference data on a pipe connected to the hook's stdin, supplying one ref per line to the hook. This way a single hook invocation can obtain an infinite amount of ref data, without bumping into any operating system limits. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn O. Pearce committed Mar 10, 2007 at 03:28 UTC f43cd49fb82b0eee10b88833b58edd711fe8298d
3 files changed +62 -58
Documentation/git-receive-pack.txt
+16 -16
@@ -40,13 +40,13 @@ OPTIONS
40 pre-receive Hook
41 ----------------
42 Before any ref is updated, if $GIT_DIR/hooks/pre-receive file exists
43 -and is executable, it will be invoked once, with three parameters
44 -per ref to be updated:
43 +and is executable, it will be invoked once with no parameters. The
44 +standard input of the hook will be one line per ref to be updated:
45
46 - $GIT_DIR/hooks/pre-receive (refname sha1-old sha1-new)+
46 + sha1-old SP sha1-new SP refname LF
47
48 -The refname parameter is relative to $GIT_DIR; e.g. for the master
49 -head this is "refs/heads/master". The two sha1 arguments after
48 +The refname value is relative to $GIT_DIR; e.g. for the master
49 +head this is "refs/heads/master". The two sha1 values before
50 each refname are the object names for the refname before and after
51 the update. Refs to be created will have sha1-old equal to 0{40},
52 while refs to be deleted will have sha1-new equal to 0{40}, otherwise
@@ -86,13 +86,14 @@ post-receive Hook
86 -----------------
87 After all refs were updated (or attempted to be updated), if any
88 ref update was successful, and if $GIT_DIR/hooks/post-receive
89 -file exists and is executable, it will be invoke once with three
90 -parameters for each successfully updated ref:
89 +file exists and is executable, it will be invoke once with no
90 +parameters. The standard input of the hook will be one line
91 +for each successfully updated ref:
92
92 - $GIT_DIR/hooks/post-receive (refname sha1-old sha1-new)+
93 + sha1-old SP sha1-new SP refname LF
94
94 -The refname parameter is relative to $GIT_DIR; e.g. for the master
95 -head this is "refs/heads/master". The two sha1 arguments after
95 +The refname value is relative to $GIT_DIR; e.g. for the master
96 +head this is "refs/heads/master". The two sha1 values before
97 each refname are the object names for the refname before and after
98 the update. Refs that were created will have sha1-old equal to
99 0{40}, while refs that were deleted will have sha1-new equal to
@@ -105,18 +106,17 @@ ref listing the commits pushed to the repository:
106
107 #!/bin/sh
108 # mail out commit update information.
108 - while test $# -gt 0
109 + while read oval nval ref
110 do
110 - if expr "$2" : '0*$' >/dev/null
111 + if expr "$oval" : '0*$' >/dev/null
112 then
113 echo "Created a new ref, with the following commits:"
113 - git-rev-list --pretty "$2"
114 + git-rev-list --pretty "$nval"
115 else
116 echo "New commits:"
116 - git-rev-list --pretty "$3" "^$2"
117 + git-rev-list --pretty "$nval" "^$oval"
118 fi |
118 - mail -s "Changes to ref $1" commit-list@mydomain
119 - shift; shift; shift; # discard this ref's args
119 + mail -s "Changes to ref $ref" commit-list@mydomain
120 done
121 exit 0
122
receive-pack.c
+30 -28
@@ -79,6 +79,8 @@ static int hook_status(int code, const char *hook_name)
79 return error("hook fork failed");
80 case -ERR_RUN_COMMAND_EXEC:
81 return error("hook execute failed");
82 + case -ERR_RUN_COMMAND_PIPE:
83 + return error("hook pipe failed");
84 case -ERR_RUN_COMMAND_WAITPID:
85 return error("waitpid failed");
86 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
@@ -93,44 +95,44 @@ static int hook_status(int code, const char *hook_name)
95 }
96 }
97
96 -static int run_hook(const char *hook_name,
97 - struct command *first_cmd,
98 - int single)
98 +static int run_hook(const char *hook_name)
99 {
100 + static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
101 struct command *cmd;
101 - int argc, code;
102 - const char **argv;
102 + struct child_process proc;
103 + const char *argv[2];
104 + int have_input = 0, code;
105
104 - for (argc = 0, cmd = first_cmd; cmd; cmd = cmd->next) {
106 + for (cmd = commands; !have_input && cmd; cmd = cmd->next) {
107 if (!cmd->error_string)
106 - argc += 3;
107 - if (single)
108 - break;
108 + have_input = 1;
109 }
110
111 - if (!argc || access(hook_name, X_OK) < 0)
111 + if (!have_input || access(hook_name, X_OK) < 0)
112 return 0;
113
114 - argv = xmalloc(sizeof(*argv) * (2 + argc));
114 argv[0] = hook_name;
116 - for (argc = 1, cmd = first_cmd; cmd; cmd = cmd->next) {
115 + argv[1] = NULL;
116 +
117 + memset(&proc, 0, sizeof(proc));
118 + proc.argv = argv;
119 + proc.in = -1;
120 + proc.stdout_to_stderr = 1;
121 +
122 + code = start_command(&proc);
123 + if (code)
124 + return hook_status(code, hook_name);
125 + for (cmd = commands; cmd; cmd = cmd->next) {
126 if (!cmd->error_string) {
118 - argv[argc++] = xstrdup(cmd->ref_name);
119 - argv[argc++] = xstrdup(sha1_to_hex(cmd->old_sha1));
120 - argv[argc++] = xstrdup(sha1_to_hex(cmd->new_sha1));
127 + size_t n = snprintf(buf, sizeof(buf), "%s %s %s\n",
128 + sha1_to_hex(cmd->old_sha1),
129 + sha1_to_hex(cmd->new_sha1),
130 + cmd->ref_name);
131 + if (write_in_full(proc.in, buf, n) != n)
132 + break;
133 }
122 - if (single)
123 - break;
134 }
125 - argv[argc] = NULL;
126 -
127 - code = run_command_v_opt(argv,
128 - RUN_COMMAND_NO_STDIN | RUN_COMMAND_STDOUT_TO_STDERR);
129 - while (--argc > 0)
130 - free((char*)argv[argc]);
131 - free(argv);
132 -
133 - return hook_status(code, hook_name);
135 + return hook_status(finish_command(&proc), hook_name);
136 }
137
138 static int run_update_hook(struct command *cmd)
@@ -265,7 +267,7 @@ static void execute_commands(const char *unpacker_error)
267 return;
268 }
269
268 - if (run_hook(pre_receive_hook, commands, 0)) {
270 + if (run_hook(pre_receive_hook)) {
271 while (cmd) {
272 cmd->error_string = "pre-receive hook declined";
273 cmd = cmd->next;
@@ -520,7 +522,7 @@ int main(int argc, char **argv)
522 unlink(pack_lockfile);
523 if (report_status)
524 report(unpack_status);
523 - run_hook(post_receive_hook, commands, 0);
525 + run_hook(post_receive_hook);
526 run_update_post_hook(commands);
527 }
528 return 0;
t/t5401-update-hooks.sh
+16 -14
@@ -25,8 +25,8 @@ test_expect_success setup '
25
26 cat >victim/.git/hooks/pre-receive <<'EOF'
27 #!/bin/sh
28 -echo "$@" >>$GIT_DIR/pre-receive.args
29 -read x; printf "$x" >$GIT_DIR/pre-receive.stdin
28 +printf "$@" >>$GIT_DIR/pre-receive.args
29 +cat - >$GIT_DIR/pre-receive.stdin
30 echo STDOUT pre-receive
31 echo STDERR pre-receive >&2
32 EOF
@@ -44,8 +44,8 @@ chmod u+x victim/.git/hooks/update
44
45 cat >victim/.git/hooks/post-receive <<'EOF'
46 #!/bin/sh
47 -echo "$@" >>$GIT_DIR/post-receive.args
48 -read x; printf "$x" >$GIT_DIR/post-receive.stdin
47 +printf "$@" >>$GIT_DIR/post-receive.args
48 +cat - >$GIT_DIR/post-receive.stdin
49 echo STDOUT post-receive
50 echo STDERR post-receive >&2
51 EOF
@@ -80,11 +80,10 @@ test_expect_success 'hooks ran' '
80 test -f victim/.git/post-update.stdin
81 '
82
83 -test_expect_success 'pre-receive hook arguments' '
84 - echo \
85 - refs/heads/master $commit0 $commit1 \
86 - refs/heads/tofail $commit1 $commit0 \
87 - | git diff - victim/.git/pre-receive.args
83 +test_expect_success 'pre-receive hook input' '
84 + (echo $commit0 $commit1 refs/heads/master;
85 + echo $commit1 $commit0 refs/heads/tofail
86 + ) | git diff - victim/.git/pre-receive.stdin
87 '
88
89 test_expect_success 'update hook arguments' '
@@ -93,9 +92,9 @@ test_expect_success 'update hook arguments' '
92 ) | git diff - victim/.git/update.args
93 '
94
96 -test_expect_success 'post-receive hook arguments' '
97 - echo refs/heads/master $commit0 $commit1 |
98 - git diff - victim/.git/post-receive.args
95 +test_expect_success 'post-receive hook input' '
96 + echo $commit0 $commit1 refs/heads/master |
97 + git diff - victim/.git/post-receive.stdin
98 '
99
100 test_expect_success 'post-update hook arguments' '
@@ -104,12 +103,15 @@ test_expect_success 'post-update hook arguments' '
103 '
104
105 test_expect_success 'all hook stdin is /dev/null' '
107 - ! test -s victim/.git/pre-receive.stdin &&
106 ! test -s victim/.git/update.stdin &&
109 - ! test -s victim/.git/post-receive.stdin &&
107 ! test -s victim/.git/post-update.stdin
108 '
109
110 +test_expect_success 'all *-receive hook args are empty' '
111 + ! test -s victim/.git/pre-receive.args &&
112 + ! test -s victim/.git/post-receive.args
113 +'
114 +
115 test_expect_failure 'send-pack produced no output' '
116 test -s send.out
117 '