Skip to content

Commit cc8eb64

Browse files
committed
Merge branch 'sp/maint-push-sideband' into sp/push-sideband
* sp/maint-push-sideband: receive-pack: Send internal errors over side-band #2 t5401: Use a bare repository for the remote peer Conflicts: builtin-receive-pack.c t/t5401-update-hooks.sh
2 parents 76d44c8 + 466dbc4 commit cc8eb64

File tree

2 files changed

+81
-45
lines changed

2 files changed

+81
-45
lines changed

builtin-receive-pack.c

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,42 @@ static struct command *commands;
139139
static const char pre_receive_hook[] = "hooks/pre-receive";
140140
static const char post_receive_hook[] = "hooks/post-receive";
141141

142+
static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
143+
static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
144+
145+
static void report_message(const char *prefix, const char *err, va_list params)
146+
{
147+
int sz = strlen(prefix);
148+
char msg[4096];
149+
150+
strncpy(msg, prefix, sz);
151+
sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
152+
if (sz > (sizeof(msg) - 1))
153+
sz = sizeof(msg) - 1;
154+
msg[sz++] = '\n';
155+
156+
if (use_sideband)
157+
send_sideband(1, 2, msg, sz, use_sideband);
158+
else
159+
xwrite(2, msg, sz);
160+
}
161+
162+
static void rp_warning(const char *err, ...)
163+
{
164+
va_list params;
165+
va_start(params, err);
166+
report_message("warning: ", err, params);
167+
va_end(params);
168+
}
169+
170+
static void rp_error(const char *err, ...)
171+
{
172+
va_list params;
173+
va_start(params, err);
174+
report_message("error: ", err, params);
175+
va_end(params);
176+
}
177+
142178
static int copy_to_sideband(int in, int out, void *arg)
143179
{
144180
char data[128];
@@ -270,7 +306,7 @@ static void refuse_unconfigured_deny(void)
270306
{
271307
int i;
272308
for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
273-
error("%s", refuse_unconfigured_deny_msg[i]);
309+
rp_error("%s", refuse_unconfigured_deny_msg[i]);
274310
}
275311

276312
static char *refuse_unconfigured_deny_delete_current_msg[] = {
@@ -290,7 +326,7 @@ static void refuse_unconfigured_deny_delete_current(void)
290326
for (i = 0;
291327
i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg);
292328
i++)
293-
error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
329+
rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
294330
}
295331

296332
static const char *update(struct command *cmd)
@@ -302,7 +338,7 @@ static const char *update(struct command *cmd)
302338

303339
/* only refs/... are allowed */
304340
if (prefixcmp(name, "refs/") || check_ref_format(name + 5)) {
305-
error("refusing to create funny ref '%s' remotely", name);
341+
rp_error("refusing to create funny ref '%s' remotely", name);
306342
return "funny refname";
307343
}
308344

@@ -311,11 +347,11 @@ static const char *update(struct command *cmd)
311347
case DENY_IGNORE:
312348
break;
313349
case DENY_WARN:
314-
warning("updating the current branch");
350+
rp_warning("updating the current branch");
315351
break;
316352
case DENY_REFUSE:
317353
case DENY_UNCONFIGURED:
318-
error("refusing to update checked out branch: %s", name);
354+
rp_error("refusing to update checked out branch: %s", name);
319355
if (deny_current_branch == DENY_UNCONFIGURED)
320356
refuse_unconfigured_deny();
321357
return "branch is currently checked out";
@@ -330,7 +366,7 @@ static const char *update(struct command *cmd)
330366

331367
if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
332368
if (deny_deletes && !prefixcmp(name, "refs/heads/")) {
333-
error("denying ref deletion for %s", name);
369+
rp_error("denying ref deletion for %s", name);
334370
return "deletion prohibited";
335371
}
336372

@@ -339,13 +375,13 @@ static const char *update(struct command *cmd)
339375
case DENY_IGNORE:
340376
break;
341377
case DENY_WARN:
342-
warning("deleting the current branch");
378+
rp_warning("deleting the current branch");
343379
break;
344380
case DENY_REFUSE:
345381
case DENY_UNCONFIGURED:
346382
if (deny_delete_current == DENY_UNCONFIGURED)
347383
refuse_unconfigured_deny_delete_current();
348-
error("refusing to delete the current branch: %s", name);
384+
rp_error("refusing to delete the current branch: %s", name);
349385
return "deletion of the current branch prohibited";
350386
}
351387
}
@@ -375,31 +411,31 @@ static const char *update(struct command *cmd)
375411
break;
376412
free_commit_list(bases);
377413
if (!ent) {
378-
error("denying non-fast-forward %s"
379-
" (you should pull first)", name);
414+
rp_error("denying non-fast-forward %s"
415+
" (you should pull first)", name);
380416
return "non-fast-forward";
381417
}
382418
}
383419
if (run_update_hook(cmd)) {
384-
error("hook declined to update %s", name);
420+
rp_error("hook declined to update %s", name);
385421
return "hook declined";
386422
}
387423

388424
if (is_null_sha1(new_sha1)) {
389425
if (!parse_object(old_sha1)) {
390-
warning ("Allowing deletion of corrupt ref.");
426+
rp_warning("Allowing deletion of corrupt ref.");
391427
old_sha1 = NULL;
392428
}
393429
if (delete_ref(name, old_sha1, 0)) {
394-
error("failed to delete %s", name);
430+
rp_error("failed to delete %s", name);
395431
return "failed to delete";
396432
}
397433
return NULL; /* good */
398434
}
399435
else {
400436
lock = lock_any_ref_for_update(name, old_sha1, 0);
401437
if (!lock) {
402-
error("failed to lock %s", name);
438+
rp_error("failed to lock %s", name);
403439
return "failed to lock";
404440
}
405441
if (write_ref_sha1(lock, new_sha1, "push")) {

t/t5401-update-hooks.sh

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,101 +17,100 @@ test_expect_success setup '
1717
commit1=$(echo modify | git commit-tree $tree1 -p $commit0) &&
1818
git update-ref refs/heads/master $commit0 &&
1919
git update-ref refs/heads/tofail $commit1 &&
20-
git clone ./. victim &&
21-
GIT_DIR=victim/.git git config receive.denyCurrentBranch warn &&
22-
GIT_DIR=victim/.git git update-ref refs/heads/tofail $commit1 &&
20+
git clone --bare ./. victim.git &&
21+
GIT_DIR=victim.git git update-ref refs/heads/tofail $commit1 &&
2322
git update-ref refs/heads/master $commit1 &&
2423
git update-ref refs/heads/tofail $commit0
2524
'
2625

27-
cat >victim/.git/hooks/pre-receive <<'EOF'
26+
cat >victim.git/hooks/pre-receive <<'EOF'
2827
#!/bin/sh
2928
printf %s "$@" >>$GIT_DIR/pre-receive.args
3029
cat - >$GIT_DIR/pre-receive.stdin
3130
echo STDOUT pre-receive
3231
echo STDERR pre-receive >&2
3332
EOF
34-
chmod u+x victim/.git/hooks/pre-receive
33+
chmod u+x victim.git/hooks/pre-receive
3534

36-
cat >victim/.git/hooks/update <<'EOF'
35+
cat >victim.git/hooks/update <<'EOF'
3736
#!/bin/sh
3837
echo "$@" >>$GIT_DIR/update.args
3938
read x; printf %s "$x" >$GIT_DIR/update.stdin
4039
echo STDOUT update $1
4140
echo STDERR update $1 >&2
4241
test "$1" = refs/heads/master || exit
4342
EOF
44-
chmod u+x victim/.git/hooks/update
43+
chmod u+x victim.git/hooks/update
4544

46-
cat >victim/.git/hooks/post-receive <<'EOF'
45+
cat >victim.git/hooks/post-receive <<'EOF'
4746
#!/bin/sh
4847
printf %s "$@" >>$GIT_DIR/post-receive.args
4948
cat - >$GIT_DIR/post-receive.stdin
5049
echo STDOUT post-receive
5150
echo STDERR post-receive >&2
5251
EOF
53-
chmod u+x victim/.git/hooks/post-receive
52+
chmod u+x victim.git/hooks/post-receive
5453

55-
cat >victim/.git/hooks/post-update <<'EOF'
54+
cat >victim.git/hooks/post-update <<'EOF'
5655
#!/bin/sh
5756
echo "$@" >>$GIT_DIR/post-update.args
5857
read x; printf %s "$x" >$GIT_DIR/post-update.stdin
5958
echo STDOUT post-update
6059
echo STDERR post-update >&2
6160
EOF
62-
chmod u+x victim/.git/hooks/post-update
61+
chmod u+x victim.git/hooks/post-update
6362

6463
test_expect_success push '
65-
test_must_fail git send-pack --force ./victim/.git \
64+
test_must_fail git send-pack --force ./victim.git \
6665
master tofail >send.out 2>send.err
6766
'
6867

6968
test_expect_success 'updated as expected' '
70-
test $(GIT_DIR=victim/.git git rev-parse master) = $commit1 &&
71-
test $(GIT_DIR=victim/.git git rev-parse tofail) = $commit1
69+
test $(GIT_DIR=victim.git git rev-parse master) = $commit1 &&
70+
test $(GIT_DIR=victim.git git rev-parse tofail) = $commit1
7271
'
7372

7473
test_expect_success 'hooks ran' '
75-
test -f victim/.git/pre-receive.args &&
76-
test -f victim/.git/pre-receive.stdin &&
77-
test -f victim/.git/update.args &&
78-
test -f victim/.git/update.stdin &&
79-
test -f victim/.git/post-receive.args &&
80-
test -f victim/.git/post-receive.stdin &&
81-
test -f victim/.git/post-update.args &&
82-
test -f victim/.git/post-update.stdin
74+
test -f victim.git/pre-receive.args &&
75+
test -f victim.git/pre-receive.stdin &&
76+
test -f victim.git/update.args &&
77+
test -f victim.git/update.stdin &&
78+
test -f victim.git/post-receive.args &&
79+
test -f victim.git/post-receive.stdin &&
80+
test -f victim.git/post-update.args &&
81+
test -f victim.git/post-update.stdin
8382
'
8483

8584
test_expect_success 'pre-receive hook input' '
8685
(echo $commit0 $commit1 refs/heads/master;
8786
echo $commit1 $commit0 refs/heads/tofail
88-
) | test_cmp - victim/.git/pre-receive.stdin
87+
) | test_cmp - victim.git/pre-receive.stdin
8988
'
9089

9190
test_expect_success 'update hook arguments' '
9291
(echo refs/heads/master $commit0 $commit1;
9392
echo refs/heads/tofail $commit1 $commit0
94-
) | test_cmp - victim/.git/update.args
93+
) | test_cmp - victim.git/update.args
9594
'
9695

9796
test_expect_success 'post-receive hook input' '
9897
echo $commit0 $commit1 refs/heads/master |
99-
test_cmp - victim/.git/post-receive.stdin
98+
test_cmp - victim.git/post-receive.stdin
10099
'
101100

102101
test_expect_success 'post-update hook arguments' '
103102
echo refs/heads/master |
104-
test_cmp - victim/.git/post-update.args
103+
test_cmp - victim.git/post-update.args
105104
'
106105

107106
test_expect_success 'all hook stdin is /dev/null' '
108-
! test -s victim/.git/update.stdin &&
109-
! test -s victim/.git/post-update.stdin
107+
! test -s victim.git/update.stdin &&
108+
! test -s victim.git/post-update.stdin
110109
'
111110

112111
test_expect_success 'all *-receive hook args are empty' '
113-
! test -s victim/.git/pre-receive.args &&
114-
! test -s victim/.git/post-receive.args
112+
! test -s victim.git/pre-receive.args &&
113+
! test -s victim.git/post-receive.args
115114
'
116115

117116
test_expect_success 'send-pack produced no output' '
@@ -125,14 +124,15 @@ remote: STDOUT update refs/heads/master
125124
remote: STDERR update refs/heads/master
126125
remote: STDOUT update refs/heads/tofail
127126
remote: STDERR update refs/heads/tofail
127+
remote: error: hook declined to update refs/heads/tofail
128128
remote: STDOUT post-receive
129129
remote: STDERR post-receive
130130
remote: STDOUT post-update
131131
remote: STDERR post-update
132132
EOF
133133
test_expect_success 'send-pack stderr contains hook messages' '
134134
grep ^remote: send.err | sed "s/ *\$//" >actual &&
135-
test_cmp - actual <expect
135+
test_cmp expect actual
136136
'
137137

138138
test_done

0 commit comments

Comments
 (0)