connect: tighten check for unexpected early hang up

A server hanging up immediately to mark access being denied does not send any .have refs, shallow lines, or anything else before hanging up. If the server has sent anything, then the hangup is unexpected. That is, if the server hangs up after a shallow line but before sending any refs, then git should tell me so: fatal: The remote end hung up upon initial contact instead of suggesting an access control problem: fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. Noticed while examining this code. This case isn't likely to come up in practice but tightening the check makes the code easier to read and manipulate. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed Sep 9, 2016 at 10:36 UTC 55e4f9365a405f5bffdc6b9babfb482b66d48809
1 file changed +12 -6
connect.c
+12 -6
@@ -43,9 +43,9 @@ int check_ref_type(const struct ref *ref, int flags)
43 return check_ref(ref->name, flags);
44 }
45
46 -static void die_initial_contact(int got_at_least_one_head)
46 +static void die_initial_contact(int unexpected)
47 {
48 - if (got_at_least_one_head)
48 + if (unexpected)
49 die("The remote end hung up upon initial contact");
50 else
51 die("Could not read from remote repository.\n\n"
@@ -115,10 +115,17 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
115 struct sha1_array *shallow_points)
116 {
117 struct ref **orig_list = list;
118 - int got_at_least_one_head = 0;
118 +
119 + /*
120 + * A hang-up after seeing some response from the other end
121 + * means that it is unexpected, as we know the other end is
122 + * willing to talk to us. A hang-up before seeing any
123 + * response does not necessarily mean an ACL problem, though.
124 + */
125 + int saw_response;
126
127 *list = NULL;
121 - for (;;) {
128 + for (saw_response = 0; ; saw_response = 1) {
129 struct ref *ref;
130 struct object_id old_oid;
131 char *name;
@@ -131,7 +138,7 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
138 PACKET_READ_GENTLE_ON_EOF |
139 PACKET_READ_CHOMP_NEWLINE);
140 if (len < 0)
134 - die_initial_contact(got_at_least_one_head);
141 + die_initial_contact(saw_response);
142
143 if (!len)
144 break;
@@ -171,7 +178,6 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
178 oidcpy(&ref->old_oid, &old_oid);
179 *list = ref;
180 list = &ref->next;
174 - got_at_least_one_head = 1;
181 }
182
183 annotate_refs_with_symref_info(*orig_list);