@matheusbsbs / matheus-bagliano / commits / 937a515a15
Fork 0

upload-pack: stop the other side when they have more roots than we do.

When the downloader has more roots than we do, we will see many "have" that leads to the root we do not have and let the other side walk all the way to that root. Tell them to stop sending the branch'es ancestors by sending a fake "ACK" when we already have common ancestor for the wanted refs. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Jul 5, 2006 at 21:28 UTC 937a515a15f776aa84430574f71367292a52b978
1 file changed +92 -9
upload-pack.c
+92 -9
@@ -11,9 +11,15 @@
11
12 static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
13
14 -#define THEY_HAVE (1U << 0)
15 -#define OUR_REF (1U << 1)
16 -#define WANTED (1U << 2)
14 +/* bits #0..7 in revision.h, #8..10 in commit.c */
15 +#define THEY_HAVE (1u << 11)
16 +#define OUR_REF (1u << 12)
17 +#define WANTED (1u << 13)
18 +#define COMMON_KNOWN (1u << 14)
19 +#define REACHABLE (1u << 15)
20 +
21 +static unsigned long oldest_have = 0;
22 +
23 static int multi_ack = 0, nr_our_refs = 0;
24 static int use_thin_pack = 0;
25 static struct object_array have_obj;
@@ -323,11 +329,12 @@ static void create_pack_file(void)
329 static int got_sha1(char *hex, unsigned char *sha1)
330 {
331 struct object *o;
332 + int we_knew_they_have = 0;
333
334 if (get_sha1_hex(hex, sha1))
335 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
336 if (!has_sha1_file(sha1))
330 - return 0;
337 + return -1;
338
339 o = lookup_object(sha1);
340 if (!(o && o->parsed))
@@ -336,15 +343,84 @@ static int got_sha1(char *hex, unsigned char *sha1)
343 die("oops (%s)", sha1_to_hex(sha1));
344 if (o->type == OBJ_COMMIT) {
345 struct commit_list *parents;
346 + struct commit *commit = (struct commit *)o;
347 if (o->flags & THEY_HAVE)
340 - return 0;
341 - o->flags |= THEY_HAVE;
342 - for (parents = ((struct commit*)o)->parents;
348 + we_knew_they_have = 1;
349 + else
350 + o->flags |= THEY_HAVE;
351 + if (!oldest_have || (commit->date < oldest_have))
352 + oldest_have = commit->date;
353 + for (parents = commit->parents;
354 parents;
355 parents = parents->next)
356 parents->item->object.flags |= THEY_HAVE;
357 }
347 - add_object_array(o, NULL, &have_obj);
358 + if (!we_knew_they_have) {
359 + add_object_array(o, NULL, &have_obj);
360 + return 1;
361 + }
362 + return 0;
363 +}
364 +
365 +static int reachable(struct commit *want)
366 +{
367 + struct commit_list *work = NULL;
368 +
369 + insert_by_date(want, &work);
370 + while (work) {
371 + struct commit_list *list = work->next;
372 + struct commit *commit = work->item;
373 + free(work);
374 + work = list;
375 +
376 + if (commit->object.flags & THEY_HAVE) {
377 + want->object.flags |= COMMON_KNOWN;
378 + break;
379 + }
380 + if (!commit->object.parsed)
381 + parse_object(commit->object.sha1);
382 + if (commit->object.flags & REACHABLE)
383 + continue;
384 + commit->object.flags |= REACHABLE;
385 + if (commit->date < oldest_have)
386 + continue;
387 + for (list = commit->parents; list; list = list->next) {
388 + struct commit *parent = list->item;
389 + if (!(parent->object.flags & REACHABLE))
390 + insert_by_date(parent, &work);
391 + }
392 + }
393 + want->object.flags |= REACHABLE;
394 + clear_commit_marks(want, REACHABLE);
395 + free_commit_list(work);
396 + return (want->object.flags & COMMON_KNOWN);
397 +}
398 +
399 +static int ok_to_give_up(void)
400 +{
401 + int i;
402 +
403 + if (!have_obj.nr)
404 + return 0;
405 +
406 + for (i = 0; i < want_obj.nr; i++) {
407 + struct object *want = want_obj.objects[i].item;
408 +
409 + if (want->flags & COMMON_KNOWN)
410 + continue;
411 + want = deref_tag(want, "a want line", 0);
412 + if (!want || want->type != OBJ_COMMIT) {
413 + /* no way to tell if this is reachable by
414 + * looking at the ancestry chain alone, so
415 + * leave a note to ourselves not to worry about
416 + * this object anymore.
417 + */
418 + want_obj.objects[i].item->flags |= COMMON_KNOWN;
419 + continue;
420 + }
421 + if (!reachable((struct commit *)want))
422 + return 0;
423 + }
424 return 1;
425 }
426
@@ -369,7 +445,13 @@ static int get_common_commits(void)
445 }
446 len = strip(line, len);
447 if (!strncmp(line, "have ", 5)) {
372 - if (got_sha1(line+5, sha1)) {
448 + switch (got_sha1(line+5, sha1)) {
449 + case -1: /* they have what we do not */
450 + if (multi_ack && ok_to_give_up())
451 + packet_write(1, "ACK %s continue\n",
452 + sha1_to_hex(sha1));
453 + break;
454 + default:
455 memcpy(hex, sha1_to_hex(sha1), 41);
456 if (multi_ack) {
457 const char *msg = "ACK %s continue\n";
@@ -378,6 +460,7 @@ static int get_common_commits(void)
460 }
461 else if (have_obj.nr == 1)
462 packet_write(1, "ACK %s\n", hex);
463 + break;
464 }
465 continue;
466 }