Skip to content

Commit 8f8f10a

Browse files
committed
Merge branch 'jx/t5411-flake-fix'
The exchange between receive-pack and proc-receive hook did not carefully check for errors. * jx/t5411-flake-fix: receive-pack: use default version 0 for proc-receive receive-pack: gently write messages to proc-receive t5411: new helper filter_out_user_friendly_and_stable_output
2 parents 455e8d1 + 80ffeb9 commit 8f8f10a

9 files changed

+545
-67
lines changed

builtin/receive-pack.c

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -977,15 +977,25 @@ static int read_proc_receive_report(struct packet_reader *reader,
977977
int new_report = 0;
978978
int code = 0;
979979
int once = 0;
980+
int response = 0;
980981

981982
for (;;) {
982983
struct object_id old_oid, new_oid;
983984
const char *head;
984985
const char *refname;
985986
char *p;
986-
987-
if (packet_reader_read(reader) != PACKET_READ_NORMAL)
987+
enum packet_read_status status;
988+
989+
status = packet_reader_read(reader);
990+
if (status != PACKET_READ_NORMAL) {
991+
/* Check whether proc-receive exited abnormally */
992+
if (status == PACKET_READ_EOF && !response) {
993+
strbuf_addstr(errmsg, "proc-receive exited abnormally");
994+
return -1;
995+
}
988996
break;
997+
}
998+
response++;
989999

9901000
head = reader->line;
9911001
p = strchr(head, ' ');
@@ -1145,31 +1155,49 @@ static int run_proc_receive_hook(struct command *commands,
11451155
if (use_push_options)
11461156
strbuf_addstr(&cap, " push-options");
11471157
if (cap.len) {
1148-
packet_write_fmt(proc.in, "version=1%c%s\n", '\0', cap.buf + 1);
1158+
code = packet_write_fmt_gently(proc.in, "version=1%c%s\n", '\0', cap.buf + 1);
11491159
strbuf_release(&cap);
11501160
} else {
1151-
packet_write_fmt(proc.in, "version=1\n");
1161+
code = packet_write_fmt_gently(proc.in, "version=1\n");
11521162
}
1153-
packet_flush(proc.in);
1163+
if (!code)
1164+
code = packet_flush_gently(proc.in);
11541165

1155-
for (;;) {
1156-
int linelen;
1166+
if (!code)
1167+
for (;;) {
1168+
int linelen;
1169+
enum packet_read_status status;
11571170

1158-
if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
1159-
break;
1171+
status = packet_reader_read(&reader);
1172+
if (status != PACKET_READ_NORMAL) {
1173+
/* Check whether proc-receive exited abnormally */
1174+
if (status == PACKET_READ_EOF)
1175+
code = -1;
1176+
break;
1177+
}
11601178

1161-
if (reader.pktlen > 8 && starts_with(reader.line, "version=")) {
1162-
version = atoi(reader.line + 8);
1163-
linelen = strlen(reader.line);
1164-
if (linelen < reader.pktlen) {
1165-
const char *feature_list = reader.line + linelen + 1;
1166-
if (parse_feature_request(feature_list, "push-options"))
1167-
hook_use_push_options = 1;
1179+
if (reader.pktlen > 8 && starts_with(reader.line, "version=")) {
1180+
version = atoi(reader.line + 8);
1181+
linelen = strlen(reader.line);
1182+
if (linelen < reader.pktlen) {
1183+
const char *feature_list = reader.line + linelen + 1;
1184+
if (parse_feature_request(feature_list, "push-options"))
1185+
hook_use_push_options = 1;
1186+
}
11681187
}
11691188
}
1189+
1190+
if (code) {
1191+
strbuf_addstr(&errmsg, "fail to negotiate version with proc-receive hook");
1192+
goto cleanup;
11701193
}
11711194

1172-
if (version != 1) {
1195+
switch (version) {
1196+
case 0:
1197+
/* fallthrough */
1198+
case 1:
1199+
break;
1200+
default:
11731201
strbuf_addf(&errmsg, "proc-receive version '%d' is not supported",
11741202
version);
11751203
code = -1;
@@ -1180,20 +1208,36 @@ static int run_proc_receive_hook(struct command *commands,
11801208
for (cmd = commands; cmd; cmd = cmd->next) {
11811209
if (!cmd->run_proc_receive || cmd->skip_update || cmd->error_string)
11821210
continue;
1183-
packet_write_fmt(proc.in, "%s %s %s",
1184-
oid_to_hex(&cmd->old_oid),
1185-
oid_to_hex(&cmd->new_oid),
1186-
cmd->ref_name);
1211+
code = packet_write_fmt_gently(proc.in, "%s %s %s",
1212+
oid_to_hex(&cmd->old_oid),
1213+
oid_to_hex(&cmd->new_oid),
1214+
cmd->ref_name);
1215+
if (code)
1216+
break;
1217+
}
1218+
if (!code)
1219+
code = packet_flush_gently(proc.in);
1220+
if (code) {
1221+
strbuf_addstr(&errmsg, "fail to write commands to proc-receive hook");
1222+
goto cleanup;
11871223
}
1188-
packet_flush(proc.in);
11891224

11901225
/* Send push options */
11911226
if (hook_use_push_options) {
11921227
struct string_list_item *item;
11931228

1194-
for_each_string_list_item(item, push_options)
1195-
packet_write_fmt(proc.in, "%s", item->string);
1196-
packet_flush(proc.in);
1229+
for_each_string_list_item(item, push_options) {
1230+
code = packet_write_fmt_gently(proc.in, "%s", item->string);
1231+
if (code)
1232+
break;
1233+
}
1234+
if (!code)
1235+
code = packet_flush_gently(proc.in);
1236+
if (code) {
1237+
strbuf_addstr(&errmsg,
1238+
"fail to write push-options to proc-receive hook");
1239+
goto cleanup;
1240+
}
11971241
}
11981242

11991243
/* Read result from proc-receive */

t/helper/test-proc-receive.c

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ static const char *proc_receive_usage[] = {
1010
NULL
1111
};
1212

13-
static int die_version;
14-
static int die_readline;
13+
static int die_read_version;
14+
static int die_write_version;
15+
static int die_read_commands;
16+
static int die_read_push_options;
17+
static int die_write_report;
1518
static int no_push_options;
1619
static int use_atomic;
1720
static int use_push_options;
@@ -33,14 +36,23 @@ struct command {
3336
static void proc_receive_verison(struct packet_reader *reader) {
3437
int server_version = 0;
3538

39+
if (die_read_version)
40+
die("die with the --die-read-version option");
41+
3642
for (;;) {
3743
int linelen;
3844

3945
if (packet_reader_read(reader) != PACKET_READ_NORMAL)
4046
break;
4147

48+
/* Ignore version negotiation for version 0 */
49+
if (version == 0)
50+
continue;
51+
4252
if (reader->pktlen > 8 && starts_with(reader->line, "version=")) {
4353
server_version = atoi(reader->line+8);
54+
if (server_version != 1)
55+
die("bad protocol version: %d", server_version);
4456
linelen = strlen(reader->line);
4557
if (linelen < reader->pktlen) {
4658
const char *feature_list = reader->line + linelen + 1;
@@ -52,12 +64,13 @@ static void proc_receive_verison(struct packet_reader *reader) {
5264
}
5365
}
5466

55-
if (server_version != 1 || die_version)
56-
die("bad protocol version: %d", server_version);
67+
if (die_write_version)
68+
die("die with the --die-write-version option");
5769

58-
packet_write_fmt(1, "version=%d%c%s\n",
59-
version, '\0',
60-
use_push_options && !no_push_options ? "push-options": "");
70+
if (version != 0)
71+
packet_write_fmt(1, "version=%d%c%s\n",
72+
version, '\0',
73+
use_push_options && !no_push_options ? "push-options": "");
6174
packet_flush(1);
6275
}
6376

@@ -75,11 +88,13 @@ static void proc_receive_read_commands(struct packet_reader *reader,
7588
if (packet_reader_read(reader) != PACKET_READ_NORMAL)
7689
break;
7790

91+
if (die_read_commands)
92+
die("die with the --die-read-commands option");
93+
7894
if (parse_oid_hex(reader->line, &old_oid, &p) ||
7995
*p++ != ' ' ||
8096
parse_oid_hex(p, &new_oid, &p) ||
81-
*p++ != ' ' ||
82-
die_readline)
97+
*p++ != ' ')
8398
die("protocol error: expected 'old new ref', got '%s'",
8499
reader->line);
85100
refname = p;
@@ -99,6 +114,9 @@ static void proc_receive_read_push_options(struct packet_reader *reader,
99114
if (no_push_options || !use_push_options)
100115
return;
101116

117+
if (die_read_push_options)
118+
die("die with the --die-read-push-options option");
119+
102120
while (1) {
103121
if (packet_reader_read(reader) != PACKET_READ_NORMAL)
104122
break;
@@ -117,10 +135,16 @@ int cmd__proc_receive(int argc, const char **argv)
117135
struct option options[] = {
118136
OPT_BOOL(0, "no-push-options", &no_push_options,
119137
"disable push options"),
120-
OPT_BOOL(0, "die-version", &die_version,
121-
"die during version negotiation"),
122-
OPT_BOOL(0, "die-readline", &die_readline,
123-
"die when readline"),
138+
OPT_BOOL(0, "die-read-version", &die_read_version,
139+
"die when reading version"),
140+
OPT_BOOL(0, "die-write-version", &die_write_version,
141+
"die when writing version"),
142+
OPT_BOOL(0, "die-read-commands", &die_read_commands,
143+
"die when reading commands"),
144+
OPT_BOOL(0, "die-read-push-options", &die_read_push_options,
145+
"die when reading push-options"),
146+
OPT_BOOL(0, "die-write-report", &die_write_report,
147+
"die when writing report"),
124148
OPT_STRING_LIST('r', "return", &returns, "old/new/ref/status/msg",
125149
"return of results"),
126150
OPT__VERBOSE(&verbose, "be verbose"),
@@ -136,7 +160,7 @@ int cmd__proc_receive(int argc, const char **argv)
136160
usage_msg_opt("Too many arguments.", proc_receive_usage, options);
137161
packet_reader_init(&reader, 0, NULL, 0,
138162
PACKET_READ_CHOMP_NEWLINE |
139-
PACKET_READ_DIE_ON_ERR_PACKET);
163+
PACKET_READ_GENTLE_ON_EOF);
140164

141165
sigchain_push(SIGPIPE, SIG_IGN);
142166
proc_receive_verison(&reader);
@@ -166,6 +190,8 @@ int cmd__proc_receive(int argc, const char **argv)
166190
fprintf(stderr, "proc-receive> %s\n", item->string);
167191
}
168192

193+
if (die_write_report)
194+
die("die with the --die-write-report option");
169195
if (returns.nr)
170196
for_each_string_list_item(item, &returns)
171197
packet_write_fmt(1, "%s\n", item->string);

t/t5411/common-functions.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ create_commits_in () {
4242
make_user_friendly_and_stable_output () {
4343
sed \
4444
-e "s/ *\$//" \
45-
-e "s/ */ /g" \
45+
-e "s/ */ /g" \
4646
-e "s/'/\"/g" \
4747
-e "s/ / /g" \
4848
-e "s/$A/<COMMIT-A>/g" \
@@ -54,3 +54,8 @@ make_user_friendly_and_stable_output () {
5454
-e "s#To $URL_PREFIX/upstream.git#To <URL/of/upstream.git>#" \
5555
-e "/^error: / d"
5656
}
57+
58+
filter_out_user_friendly_and_stable_output () {
59+
make_user_friendly_and_stable_output |
60+
sed -n ${1+"$@"}
61+
}

t/t5411/test-0000-standard-git-push.sh

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@ test_expect_success "git-push --atomic ($PROTOCOL)" '
3636
main \
3737
$B:refs/heads/next \
3838
>out 2>&1 &&
39-
make_user_friendly_and_stable_output <out |
40-
sed -n \
41-
-e "/^To / { s/ */ /g; p; }" \
42-
-e "/^ ! / { s/ */ /g; p; }" \
43-
>actual &&
39+
filter_out_user_friendly_and_stable_output \
40+
-e "/^To / { p; }" \
41+
-e "/^ ! / { p; }" \
42+
<out >actual &&
4443
cat >expect <<-EOF &&
4544
To <URL/of/upstream.git>
4645
! [rejected] main -> main (non-fast-forward)

t/t5411/test-0001-standard-git-push--porcelain.sh

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,15 @@ test_expect_success "git-push --atomic ($PROTOCOL/porcelain)" '
3737
main \
3838
$B:refs/heads/next \
3939
>out 2>&1 &&
40-
make_user_friendly_and_stable_output <out |
41-
sed -n \
42-
-e "s/^# GETTEXT POISON #//" \
43-
-e "/^To / { s/ */ /g; p; }" \
44-
-e "/^! / { s/ */ /g; p; }" \
45-
>actual &&
40+
filter_out_user_friendly_and_stable_output \
41+
-e "s/^# GETTEXT POISON #//" \
42+
-e "/^To / { p; }" \
43+
-e "/^! / { p; }" \
44+
<out >actual &&
4645
cat >expect <<-EOF &&
4746
To <URL/of/upstream.git>
48-
! refs/heads/main:refs/heads/main [rejected] (non-fast-forward)
49-
! <COMMIT-B>:refs/heads/next [rejected] (atomic push failed)
47+
! refs/heads/main:refs/heads/main [rejected] (non-fast-forward)
48+
! <COMMIT-B>:refs/heads/next [rejected] (atomic push failed)
5049
EOF
5150
test_cmp expect actual &&
5251
git -C "$upstream" show-ref >out &&

0 commit comments

Comments
 (0)