upload-pack: refactor ok_to_give_up()

In anticipation of consolidating all commit reachability algorithms, refactor ok_to_give_up() in order to allow splitting its logic into an external method. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jul 20, 2018 at 16:33 UTC 921bf7734f11da75516bfd12da5e4fa203cc6d19
1 file changed +23 -11
upload-pack.c
+23 -11
@@ -369,34 +369,46 @@ static int reachable(struct commit *from, unsigned int with_flag,
369 return (from->object.flags & assign_flag);
370 }
371
372 -static int ok_to_give_up(void)
372 +/*
373 + * Determine if every commit in 'from' can reach at least one commit
374 + * that is marked with 'with_flag'. As we traverse, use 'assign_flag'
375 + * as a marker for commits that are already visited.
376 + */
377 +static int can_all_from_reach_with_flag(struct object_array *from,
378 + unsigned int with_flag,
379 + unsigned int assign_flag)
380 {
381 int i;
382
376 - if (!have_obj.nr)
377 - return 0;
378 -
379 - for (i = 0; i < want_obj.nr; i++) {
380 - struct object *want = want_obj.objects[i].item;
383 + for (i = 0; i < from->nr; i++) {
384 + struct object *from_one = from->objects[i].item;
385
382 - if (want->flags & COMMON_KNOWN)
386 + if (from_one->flags & assign_flag)
387 continue;
384 - want = deref_tag(the_repository, want, "a want line", 0);
385 - if (!want || want->type != OBJ_COMMIT) {
388 + from_one = deref_tag(the_repository, from_one, "a from object", 0);
389 + if (!from_one || from_one->type != OBJ_COMMIT) {
390 /* no way to tell if this is reachable by
391 * looking at the ancestry chain alone, so
392 * leave a note to ourselves not to worry about
393 * this object anymore.
394 */
391 - want_obj.objects[i].item->flags |= COMMON_KNOWN;
395 + from->objects[i].item->flags |= assign_flag;
396 continue;
397 }
394 - if (!reachable((struct commit *)want, THEY_HAVE, COMMON_KNOWN))
398 + if (!reachable((struct commit *)from_one, with_flag, assign_flag))
399 return 0;
400 }
401 return 1;
402 }
403
404 +static int ok_to_give_up(void)
405 +{
406 + if (!have_obj.nr)
407 + return 0;
408 +
409 + return can_all_from_reach_with_flag(&want_obj, THEY_HAVE, COMMON_KNOWN);
410 +}
411 +
412 static int get_common_commits(void)
413 {
414 struct object_id oid;