Skip to content

Commit 95b11ce

Browse files
committed
test-reach: test can_all_from_reach_with_flags
The can_all_from_reach_with_flags method is used by ok_to_give_up in upload-pack.c to see if we have done enough negotiation during a fetch. This method is intentionally created to preserve state between calls to assist with stateful negotiation, such as over SSH. To make this method testable, add a new can_all_from_reach method that does the initial setup and final tear-down. Call the method from 'test-tool reach'. Since this is a many-to-many reachability query, add a new type of input to the 'test-tool reach' input format. Lines "Y:<committish>" create a list of commits to be the reachability targets from the commits in the 'X' list. In the context of fetch negotiation, the 'X' commits are the 'want' commits and the 'Y' commits are the 'have' commits. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 3b0379c commit 95b11ce

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

commit-reach.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,3 +593,50 @@ int can_all_from_reach_with_flag(struct object_array *from,
593593
}
594594
return 1;
595595
}
596+
597+
int can_all_from_reach(struct commit_list *from, struct commit_list *to,
598+
int cutoff_by_min_date)
599+
{
600+
struct object_array from_objs = OBJECT_ARRAY_INIT;
601+
time_t min_commit_date = cutoff_by_min_date ? from->item->date : 0;
602+
struct commit_list *from_iter = from, *to_iter = to;
603+
int result;
604+
605+
while (from_iter) {
606+
add_object_array(&from_iter->item->object, NULL, &from_objs);
607+
608+
if (!parse_commit(from_iter->item)) {
609+
if (from_iter->item->date < min_commit_date)
610+
min_commit_date = from_iter->item->date;
611+
}
612+
613+
from_iter = from_iter->next;
614+
}
615+
616+
while (to_iter) {
617+
if (!parse_commit(to_iter->item)) {
618+
if (to_iter->item->date < min_commit_date)
619+
min_commit_date = to_iter->item->date;
620+
}
621+
622+
to_iter->item->object.flags |= PARENT2;
623+
624+
to_iter = to_iter->next;
625+
}
626+
627+
result = can_all_from_reach_with_flag(&from_objs, PARENT2, PARENT1,
628+
min_commit_date);
629+
630+
while (from) {
631+
clear_commit_marks(from->item, PARENT1);
632+
from = from->next;
633+
}
634+
635+
while (to) {
636+
clear_commit_marks(to->item, PARENT2);
637+
to = to->next;
638+
}
639+
640+
object_array_clear(&from_objs);
641+
return result;
642+
}

commit-reach.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,7 @@ int reachable(struct commit *from, int with_flag, int assign_flag,
6868
int can_all_from_reach_with_flag(struct object_array *from,
6969
int with_flag, int assign_flag,
7070
time_t min_commit_date);
71+
int can_all_from_reach(struct commit_list *from, struct commit_list *to,
72+
int commit_date_cutoff);
7173

7274
#endif

t/helper/test-reach.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ int cmd__reach(int ac, const char **av)
99
{
1010
struct object_id oid_A, oid_B;
1111
struct commit *A, *B;
12-
struct commit_list *X;
12+
struct commit_list *X, *Y;
1313
struct commit **X_array;
1414
int X_nr, X_alloc;
1515
struct strbuf buf = STRBUF_INIT;
@@ -21,7 +21,7 @@ int cmd__reach(int ac, const char **av)
2121
exit(1);
2222

2323
A = B = NULL;
24-
X = NULL;
24+
X = Y = NULL;
2525
X_nr = 0;
2626
X_alloc = 16;
2727
ALLOC_ARRAY(X_array, X_alloc);
@@ -66,6 +66,10 @@ int cmd__reach(int ac, const char **av)
6666
X_array[X_nr++] = c;
6767
break;
6868

69+
case 'Y':
70+
commit_list_insert(c, &Y);
71+
break;
72+
6973
default:
7074
die("unexpected start of line: %c", buf.buf[0]);
7175
}
@@ -92,6 +96,8 @@ int cmd__reach(int ac, const char **av)
9296
printf("%s\n", oid_to_hex(&list->item->object.oid));
9397
list = list->next;
9498
}
99+
} else if (!strcmp(av[1], "can_all_from_reach")) {
100+
printf("%s(X,Y):%d\n", av[1], can_all_from_reach(X, Y, 1));
95101
}
96102

97103
exit(0);

t/t6600-test-reach.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,49 @@ test_expect_success 'reduce_heads' '
160160
test_three_modes reduce_heads
161161
'
162162

163+
test_expect_success 'can_all_from_reach:hit' '
164+
cat >input <<-\EOF &&
165+
X:commit-2-10
166+
X:commit-3-9
167+
X:commit-4-8
168+
X:commit-5-7
169+
X:commit-6-6
170+
X:commit-7-5
171+
X:commit-8-4
172+
X:commit-9-3
173+
Y:commit-1-9
174+
Y:commit-2-8
175+
Y:commit-3-7
176+
Y:commit-4-6
177+
Y:commit-5-5
178+
Y:commit-6-4
179+
Y:commit-7-3
180+
Y:commit-8-1
181+
EOF
182+
printf "can_all_from_reach(X,Y):1\n" >expect &&
183+
test_three_modes can_all_from_reach
184+
'
185+
186+
test_expect_success 'can_all_from_reach:miss' '
187+
cat >input <<-\EOF &&
188+
X:commit-2-10
189+
X:commit-3-9
190+
X:commit-4-8
191+
X:commit-5-7
192+
X:commit-6-6
193+
X:commit-7-5
194+
X:commit-8-4
195+
X:commit-9-3
196+
Y:commit-1-9
197+
Y:commit-2-8
198+
Y:commit-3-7
199+
Y:commit-4-6
200+
Y:commit-5-5
201+
Y:commit-6-4
202+
Y:commit-8-5
203+
EOF
204+
printf "can_all_from_reach(X,Y):0\n" >expect &&
205+
test_three_modes can_all_from_reach
206+
'
207+
163208
test_done

0 commit comments

Comments
 (0)