@matheusbsbs / matheus-bagliano / commits / 8736a84890
Fork 0

send-pack: track errors for each ref

Instead of keeping the 'ret' variable, we instead have a status flag for each ref that tracks what happened to it. We then print the ref status after all of the refs have been examined. This paves the way for three improvements: - updating tracking refs only for non-error refs - incorporating remote rejection into the printed status - printing errors in a different order than we processed (e.g., consolidating non-ff errors near the end with a special message) Signed-off-by: Jeff King <peff@peff.net> Acked-by: Alex Riesen <raa.lkml@gmail.com> Acked-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Nov 17, 2007 at 07:54 UTC 8736a8489080509516f5f4cc1cc74de33150f397
3 files changed +156 -95
builtin-send-pack.c
+134 -90
@@ -207,8 +207,9 @@ static void update_tracking_ref(struct remote *remote, struct ref *ref)
207 }
208 }
209
210 -static const char *prettify_ref(const char *name)
210 +static const char *prettify_ref(const struct ref *ref)
211 {
212 + const char *name = ref->name;
213 return name + (
214 !prefixcmp(name, "refs/heads/") ? 11 :
215 !prefixcmp(name, "refs/tags/") ? 10 :
@@ -218,15 +219,104 @@ static const char *prettify_ref(const char *name)
219
220 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
221
222 +static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
223 +{
224 + fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
225 + if (from)
226 + fprintf(stderr, "%s -> %s", prettify_ref(from), prettify_ref(to));
227 + else
228 + fputs(prettify_ref(to), stderr);
229 + if (msg) {
230 + fputs(" (", stderr);
231 + fputs(msg, stderr);
232 + fputc(')', stderr);
233 + }
234 + fputc('\n', stderr);
235 +}
236 +
237 +static const char *status_abbrev(unsigned char sha1[20])
238 +{
239 + const char *abbrev;
240 + abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV);
241 + return abbrev ? abbrev : sha1_to_hex(sha1);
242 +}
243 +
244 +static void print_ok_ref_status(struct ref *ref)
245 +{
246 + if (ref->deletion)
247 + print_ref_status('-', "[deleted]", ref, NULL, NULL);
248 + else if (is_null_sha1(ref->old_sha1))
249 + print_ref_status('*',
250 + (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
251 + "[new branch]"),
252 + ref, ref->peer_ref, NULL);
253 + else {
254 + char quickref[84];
255 + char type;
256 + const char *msg;
257 +
258 + strcpy(quickref, status_abbrev(ref->old_sha1));
259 + if (ref->nonfastforward) {
260 + strcat(quickref, "...");
261 + type = '+';
262 + msg = "forced update";
263 + } else {
264 + strcat(quickref, "..");
265 + type = ' ';
266 + msg = NULL;
267 + }
268 + strcat(quickref, status_abbrev(ref->new_sha1));
269 +
270 + print_ref_status(type, quickref, ref, ref->peer_ref, msg);
271 + }
272 +}
273 +
274 +static void print_push_status(const char *dest, struct ref *refs)
275 +{
276 + struct ref *ref;
277 + int shown_dest = 0;
278 +
279 + for (ref = refs; ref; ref = ref->next) {
280 + if (!ref->status)
281 + continue;
282 + if (ref->status == REF_STATUS_UPTODATE && !args.verbose)
283 + continue;
284 +
285 + if (!shown_dest) {
286 + fprintf(stderr, "To %s\n", dest);
287 + shown_dest = 1;
288 + }
289 +
290 + switch(ref->status) {
291 + case REF_STATUS_NONE:
292 + print_ref_status('X', "[no match]", ref, NULL, NULL);
293 + break;
294 + case REF_STATUS_REJECT_NODELETE:
295 + print_ref_status('!', "[rejected]", ref, NULL,
296 + "remote does not support deleting refs");
297 + break;
298 + case REF_STATUS_UPTODATE:
299 + print_ref_status('=', "[up to date]", ref,
300 + ref->peer_ref, NULL);
301 + break;
302 + case REF_STATUS_REJECT_NONFASTFORWARD:
303 + print_ref_status('!', "[rejected]", ref, ref->peer_ref,
304 + "non-fast forward");
305 + break;
306 + case REF_STATUS_OK:
307 + print_ok_ref_status(ref);
308 + break;
309 + }
310 + }
311 +}
312 +
313 static int do_send_pack(int in, int out, struct remote *remote, const char *dest, int nr_refspec, const char **refspec)
314 {
315 struct ref *ref;
316 int new_refs;
225 - int ret = 0;
317 int ask_for_status_report = 0;
318 int allow_deleting_refs = 0;
319 int expect_status_report = 0;
229 - int shown_dest = 0;
320 int flags = MATCH_REFS_NONE;
321
322 if (args.send_all)
@@ -262,10 +352,6 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
352 */
353 new_refs = 0;
354 for (ref = remote_refs; ref; ref = ref->next) {
265 - char old_hex[60], *new_hex;
266 - int will_delete_ref;
267 - const char *pretty_ref;
268 - const char *pretty_peer = NULL; /* only used when not deleting */
355 const unsigned char *new_sha1;
356
357 if (!ref->peer_ref) {
@@ -276,29 +362,15 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
362 else
363 new_sha1 = ref->peer_ref->new_sha1;
364
279 - if (!shown_dest) {
280 - fprintf(stderr, "To %s\n", dest);
281 - shown_dest = 1;
282 - }
283 -
284 - will_delete_ref = is_null_sha1(new_sha1);
365
286 - pretty_ref = prettify_ref(ref->name);
287 - if (!will_delete_ref)
288 - pretty_peer = prettify_ref(ref->peer_ref->name);
289 -
290 - if (will_delete_ref && !allow_deleting_refs) {
291 - fprintf(stderr, " ! %-*s %s (remote does not support deleting refs)\n",
292 - SUMMARY_WIDTH, "[rejected]", pretty_ref);
293 - ret = -2;
366 + ref->deletion = is_null_sha1(new_sha1);
367 + if (ref->deletion && !allow_deleting_refs) {
368 + ref->status = REF_STATUS_REJECT_NODELETE;
369 continue;
370 }
296 - if (!will_delete_ref &&
371 + if (!ref->deletion &&
372 !hashcmp(ref->old_sha1, new_sha1)) {
298 - if (args.verbose)
299 - fprintf(stderr, " = %-*s %s -> %s\n",
300 - SUMMARY_WIDTH, "[up to date]",
301 - pretty_peer, pretty_ref);
373 + ref->status = REF_STATUS_UPTODATE;
374 continue;
375 }
376
@@ -321,33 +393,26 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
393 * always allowed.
394 */
395
324 - if (!args.force_update &&
325 - !will_delete_ref &&
396 + ref->nonfastforward =
397 + !ref->deletion &&
398 !is_null_sha1(ref->old_sha1) &&
327 - !ref->force) {
328 - if (!has_sha1_file(ref->old_sha1) ||
329 - !ref_newer(new_sha1, ref->old_sha1)) {
330 - /* We do not have the remote ref, or
331 - * we know that the remote ref is not
332 - * an ancestor of what we are trying to
333 - * push. Either way this can be losing
334 - * commits at the remote end and likely
335 - * we were not up to date to begin with.
336 - */
337 - fprintf(stderr, " ! %-*s %s -> %s (non-fast forward)\n",
338 - SUMMARY_WIDTH, "[rejected]",
339 - pretty_peer, pretty_ref);
340 - ret = -2;
341 - continue;
342 - }
399 + (!has_sha1_file(ref->old_sha1)
400 + || !ref_newer(new_sha1, ref->old_sha1));
401 +
402 + if (ref->nonfastforward && !ref->force && !args.force_update) {
403 + ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
404 + continue;
405 }
406 +
407 hashcpy(ref->new_sha1, new_sha1);
345 - if (!will_delete_ref)
408 + if (!ref->deletion)
409 new_refs++;
347 - strcpy(old_hex, sha1_to_hex(ref->old_sha1));
348 - new_hex = sha1_to_hex(ref->new_sha1);
410 + ref->status = REF_STATUS_OK;
411
412 if (!args.dry_run) {
413 + char *old_hex = sha1_to_hex(ref->old_sha1);
414 + char *new_hex = sha1_to_hex(ref->new_sha1);
415 +
416 if (ask_for_status_report) {
417 packet_write(out, "%s %s %s%c%s",
418 old_hex, new_hex, ref->name, 0,
@@ -359,64 +424,43 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
424 packet_write(out, "%s %s %s",
425 old_hex, new_hex, ref->name);
426 }
362 - if (will_delete_ref)
363 - fprintf(stderr, " - %-*s %s\n",
364 - SUMMARY_WIDTH, "[deleting]",
365 - pretty_ref);
366 - else if (is_null_sha1(ref->old_sha1)) {
367 - const char *msg;
368 -
369 - if (!prefixcmp(ref->name, "refs/tags/"))
370 - msg = "[new tag]";
371 - else
372 - msg = "[new branch]";
373 - fprintf(stderr, " * %-*s %s -> %s\n",
374 - SUMMARY_WIDTH, msg,
375 - pretty_peer, pretty_ref);
376 - }
377 - else {
378 - char quickref[83];
379 - char type = ' ';
380 - const char *msg = "";
381 - const char *old_abb;
382 - old_abb = find_unique_abbrev(ref->old_sha1, DEFAULT_ABBREV);
383 - strcpy(quickref, old_abb ? old_abb : old_hex);
384 - if (ref_newer(ref->peer_ref->new_sha1, ref->old_sha1))
385 - strcat(quickref, "..");
386 - else {
387 - strcat(quickref, "...");
388 - type = '+';
389 - msg = " (forced update)";
390 - }
391 - strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
392 -
393 - fprintf(stderr, " %c %-*s %s -> %s%s\n",
394 - type,
395 - SUMMARY_WIDTH, quickref,
396 - pretty_peer, pretty_ref,
397 - msg);
398 - }
427 }
428
429 packet_flush(out);
402 - if (new_refs && !args.dry_run)
403 - ret = pack_objects(out, remote_refs);
430 + if (new_refs && !args.dry_run) {
431 + if (pack_objects(out, remote_refs) < 0) {
432 + close(out);
433 + return -1;
434 + }
435 + }
436 close(out);
437
438 + print_push_status(dest, remote_refs);
439 +
440 if (expect_status_report) {
441 if (receive_status(in))
408 - ret = -4;
442 + return -1;
443 }
444
411 - if (!args.dry_run && remote && ret == 0) {
445 + if (!args.dry_run && remote) {
446 for (ref = remote_refs; ref; ref = ref->next)
447 if (!is_null_sha1(ref->new_sha1))
448 update_tracking_ref(remote, ref);
449 }
450
417 - if (!new_refs && ret == 0)
451 + if (!new_refs)
452 fprintf(stderr, "Everything up-to-date\n");
419 - return ret;
453 + for (ref = remote_refs; ref; ref = ref->next) {
454 + switch (ref->status) {
455 + case REF_STATUS_NONE:
456 + case REF_STATUS_UPTODATE:
457 + case REF_STATUS_OK:
458 + break;
459 + default:
460 + return -1;
461 + }
462 + }
463 + return 0;
464 }
465
466 static void verify_remote_names(int nr_heads, const char **heads)
cache.h
+11 -2
@@ -493,8 +493,17 @@ struct ref {
493 struct ref *next;
494 unsigned char old_sha1[20];
495 unsigned char new_sha1[20];
496 - unsigned char force;
497 - unsigned char merge;
496 + unsigned char force : 1;
497 + unsigned char merge : 1;
498 + unsigned char nonfastforward : 1;
499 + unsigned char deletion : 1;
500 + enum {
501 + REF_STATUS_NONE = 0,
502 + REF_STATUS_OK,
503 + REF_STATUS_REJECT_NONFASTFORWARD,
504 + REF_STATUS_REJECT_NODELETE,
505 + REF_STATUS_UPTODATE,
506 + } status;
507 struct ref *peer_ref; /* when renaming */
508 char name[FLEX_ARRAY]; /* more */
509 };
t/t5404-tracking-branches.sh
+11 -3
@@ -19,7 +19,7 @@ test_expect_success 'setup' '
19 git commit -a -m b2
20 '
21
22 -test_expect_success 'check tracking branches updated correctly after push' '
22 +test_expect_success 'prepare pushable branches' '
23 cd aa &&
24 b1=$(git rev-parse origin/b1) &&
25 b2=$(git rev-parse origin/b2) &&
@@ -31,8 +31,16 @@ test_expect_success 'check tracking branches updated correctly after push' '
31 git commit -a -m aa-b2 &&
32 git checkout master &&
33 echo aa-master >>file &&
34 - git commit -a -m aa-master &&
35 - git push &&
34 + git commit -a -m aa-master
35 +'
36 +
37 +test_expect_success 'mixed-success push returns error' '! git push'
38 +
39 +test_expect_success 'check tracking branches updated correctly after push' '
40 + test "$(git rev-parse origin/master)" = "$(git rev-parse master)"
41 +'
42 +
43 +test_expect_success 'check tracking branches not updated for failed refs' '
44 test "$(git rev-parse origin/b1)" = "$b1" &&
45 test "$(git rev-parse origin/b2)" = "$b2"
46 '