@matheusbsbs / matheus-bagliano / commits / 2a0fe89a97
Fork 0

send-pack: tighten remote error reporting

Previously, we set all ref pushes to 'OK', and then marked them as errors if the remote reported so. This has the problem that if the remote dies or fails to report a ref, we just assume it was OK. Instead, we use a new non-OK state to indicate that we are expecting status (if the remote doesn't support the report-status feature, we fall back on the old behavior). Thus we can flag refs for which we expected a status, but got none (conversely, we now also print a warning for refs for which we get a status, but weren't expecting one). This also allows us to simplify the receive_status exit code, since each ref is individually marked with failure until we get a success response. We can just print the usual status table, so the user still gets a sense of what we were trying to do when the failure happened. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Nov 18, 2007 at 02:16 UTC 2a0fe89a976331cb2163d4f299e38e2cb5010632
2 files changed +54 -43
builtin-send-pack.c
+52 -42
@@ -146,60 +146,67 @@ static void get_local_heads(void)
146 for_each_ref(one_local_ref, NULL);
147 }
148
149 -static struct ref *set_ref_error(struct ref *refs, const char *line)
150 -{
151 - struct ref *ref;
152 -
153 - for (ref = refs; ref; ref = ref->next) {
154 - const char *msg;
155 - if (prefixcmp(line, ref->name))
156 - continue;
157 - msg = line + strlen(ref->name);
158 - if (*msg++ != ' ')
159 - continue;
160 - ref->status = REF_STATUS_REMOTE_REJECT;
161 - ref->error = xstrdup(msg);
162 - ref->error[strlen(ref->error)-1] = '\0';
163 - return ref;
164 - }
165 - return NULL;
166 -}
167 -
168 -/* a return value of -1 indicates that an error occurred,
169 - * but we were able to set individual ref errors. A return
170 - * value of -2 means we couldn't even get that far. */
149 static int receive_status(int in, struct ref *refs)
150 {
151 struct ref *hint;
152 char line[1000];
153 int ret = 0;
154 int len = packet_read_line(in, line, sizeof(line));
177 - if (len < 10 || memcmp(line, "unpack ", 7)) {
178 - fprintf(stderr, "did not receive status back\n");
179 - return -2;
180 - }
155 + if (len < 10 || memcmp(line, "unpack ", 7))
156 + return error("did not receive remote status");
157 if (memcmp(line, "unpack ok\n", 10)) {
182 - fputs(line, stderr);
158 + char *p = line + strlen(line) - 1;
159 + if (*p == '\n')
160 + *p = '\0';
161 + error("unpack failed: %s", line + 7);
162 ret = -1;
163 }
164 hint = NULL;
165 while (1) {
166 + char *refname;
167 + char *msg;
168 len = packet_read_line(in, line, sizeof(line));
169 if (!len)
170 break;
171 if (len < 3 ||
191 - (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
172 + (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
173 fprintf(stderr, "protocol error: %s\n", line);
174 ret = -1;
175 break;
176 }
196 - if (!memcmp(line, "ok", 2))
197 - continue;
177 +
178 + line[strlen(line)-1] = '\0';
179 + refname = line + 3;
180 + msg = strchr(refname, ' ');
181 + if (msg)
182 + *msg++ = '\0';
183 +
184 + /* first try searching at our hint, falling back to all refs */
185 if (hint)
199 - hint = set_ref_error(hint, line + 3);
186 + hint = find_ref_by_name(hint, refname);
187 if (!hint)
201 - hint = set_ref_error(refs, line + 3);
202 - ret = -1;
188 + hint = find_ref_by_name(refs, refname);
189 + if (!hint) {
190 + warning("remote reported status on unknown ref: %s",
191 + refname);
192 + continue;
193 + }
194 + if (hint->status != REF_STATUS_EXPECTING_REPORT) {
195 + warning("remote reported status on unexpected ref: %s",
196 + refname);
197 + continue;
198 + }
199 +
200 + if (line[0] == 'o' && line[1] == 'k')
201 + hint->status = REF_STATUS_OK;
202 + else {
203 + hint->status = REF_STATUS_REMOTE_REJECT;
204 + ret = -1;
205 + }
206 + if (msg)
207 + hint->remote_status = xstrdup(msg);
208 + /* start our next search from the next ref */
209 + hint = hint->next;
210 }
211 return ret;
212 }
@@ -324,10 +331,14 @@ static void print_push_status(const char *dest, struct ref *refs)
331 "non-fast forward");
332 break;
333 case REF_STATUS_REMOTE_REJECT:
327 - if (ref->deletion)
328 - print_ref_status('!', "[remote rejected]", ref, NULL, ref->error);
329 - else
330 - print_ref_status('!', "[remote rejected]", ref, ref->peer_ref, ref->error);
334 + print_ref_status('!', "[remote rejected]", ref,
335 + ref->deletion ? NULL : ref->peer_ref,
336 + ref->remote_status);
337 + break;
338 + case REF_STATUS_EXPECTING_REPORT:
339 + print_ref_status('!', "[remote failure]", ref,
340 + ref->deletion ? NULL : ref->peer_ref,
341 + "remote failed to report status");
342 break;
343 case REF_STATUS_OK:
344 print_ok_ref_status(ref);
@@ -434,7 +445,6 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
445 hashcpy(ref->new_sha1, new_sha1);
446 if (!ref->deletion)
447 new_refs++;
437 - ref->status = REF_STATUS_OK;
448
449 if (!args.dry_run) {
450 char *old_hex = sha1_to_hex(ref->old_sha1);
@@ -451,6 +461,9 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
461 packet_write(out, "%s %s %s",
462 old_hex, new_hex, ref->name);
463 }
464 + ref->status = expect_status_report ?
465 + REF_STATUS_EXPECTING_REPORT :
466 + REF_STATUS_OK;
467 }
468
469 packet_flush(out);
@@ -462,11 +475,8 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
475 }
476 close(out);
477
465 - if (expect_status_report) {
478 + if (expect_status_report)
479 ret = receive_status(in, remote_refs);
467 - if (ret == -2)
468 - return -1;
469 - }
480 else
481 ret = 0;
482
cache.h
+2 -1
@@ -504,8 +504,9 @@ struct ref {
504 REF_STATUS_REJECT_NODELETE,
505 REF_STATUS_UPTODATE,
506 REF_STATUS_REMOTE_REJECT,
507 + REF_STATUS_EXPECTING_REPORT,
508 } status;
508 - char *error;
509 + char *remote_status;
510 struct ref *peer_ref; /* when renaming */
511 char name[FLEX_ARRAY]; /* more */
512 };