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

send-pack: assign remote errors to each ref

This lets us show remote errors (e.g., a denied hook) along with the usual push output. There is a slightly clever optimization in receive_status that bears explanation. We need to correlate the returned status and our ref objects, which naively could be an O(m*n) operation. However, since the current implementation of receive-pack returns the errors to us in the same order that we sent them, we optimistically look for the next ref to be looked up to come after the last one we have found. So it should be an O(m+n) merge if the receive-pack behavior holds, but we fall back to a correct but slower behavior if it should change. Signed-off-by: Jeff King <peff@peff.net> Acked-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Nov 17, 2007 at 07:56 UTC ca74c458a3908314cf29b96f1c43fe2b2597de76
3 files changed +71 -6
builtin-send-pack.c
+45 -6
@@ -146,19 +146,43 @@ static void get_local_heads(void)
146 for_each_ref(one_local_ref, NULL);
147 }
148
149 -static int receive_status(int in)
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. */
171 +static int receive_status(int in, struct ref *refs)
172 +{
173 + struct ref *hint;
174 char line[1000];
175 int ret = 0;
176 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");
156 - return -1;
179 + return -2;
180 }
181 if (memcmp(line, "unpack ok\n", 10)) {
182 fputs(line, stderr);
183 ret = -1;
184 }
185 + hint = NULL;
186 while (1) {
187 len = packet_read_line(in, line, sizeof(line));
188 if (!len)
@@ -171,7 +195,10 @@ static int receive_status(int in)
195 }
196 if (!memcmp(line, "ok", 2))
197 continue;
174 - fputs(line, stderr);
198 + if (hint)
199 + hint = set_ref_error(hint, line + 3);
200 + if (!hint)
201 + hint = set_ref_error(refs, line + 3);
202 ret = -1;
203 }
204 return ret;
@@ -296,6 +323,12 @@ static void print_push_status(const char *dest, struct ref *refs)
323 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
324 "non-fast forward");
325 break;
326 + 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);
331 + break;
332 case REF_STATUS_OK:
333 print_ok_ref_status(ref);
334 break;
@@ -311,6 +344,7 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
344 int allow_deleting_refs = 0;
345 int expect_status_report = 0;
346 int flags = MATCH_REFS_NONE;
347 + int ret;
348
349 if (args.send_all)
350 flags |= MATCH_REFS_ALL;
@@ -428,12 +462,15 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
462 }
463 close(out);
464
431 - print_push_status(dest, remote_refs);
432 -
465 if (expect_status_report) {
434 - if (receive_status(in))
466 + ret = receive_status(in, remote_refs);
467 + if (ret == -2)
468 return -1;
469 }
470 + else
471 + ret = 0;
472 +
473 + print_push_status(dest, remote_refs);
474
475 if (!args.dry_run && remote) {
476 for (ref = remote_refs; ref; ref = ref->next)
@@ -442,6 +479,8 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
479
480 if (!new_refs)
481 fprintf(stderr, "Everything up-to-date\n");
482 + if (ret < 0)
483 + return ret;
484 for (ref = remote_refs; ref; ref = ref->next) {
485 switch (ref->status) {
486 case REF_STATUS_NONE:
cache.h
+2
@@ -503,7 +503,9 @@ struct ref {
503 REF_STATUS_REJECT_NONFASTFORWARD,
504 REF_STATUS_REJECT_NODELETE,
505 REF_STATUS_UPTODATE,
506 + REF_STATUS_REMOTE_REJECT,
507 } status;
508 + char *error;
509 struct ref *peer_ref; /* when renaming */
510 char name[FLEX_ARRAY]; /* more */
511 };
t/t5406-remote-rejects.sh new
+24
@@ -0,0 +1,24 @@
1 +#!/bin/sh
2 +
3 +test_description='remote push rejects are reported by client'
4 +
5 +. ./test-lib.sh
6 +
7 +test_expect_success 'setup' '
8 + mkdir .git/hooks &&
9 + (echo "#!/bin/sh" ; echo "exit 1") >.git/hooks/update &&
10 + chmod +x .git/hooks/update &&
11 + echo 1 >file &&
12 + git add file &&
13 + git commit -m 1 &&
14 + git clone . child &&
15 + cd child &&
16 + echo 2 >file &&
17 + git commit -a -m 2
18 +'
19 +
20 +test_expect_success 'push reports error' '! git push 2>stderr'
21 +
22 +test_expect_success 'individual ref reports error' 'grep rejected stderr'
23 +
24 +test_done