test-reach: test get_merge_bases_many

The get_merge_bases_many method returns a list of merge bases for a single commit (A) against a list of commits (X). Some care is needed in constructing the expected behavior because the result is not the expected merge-base for an octopus merge with those parents but instead the set of maximal commits that are reachable from A and at least one of the commits in X. Add get_merge_bases_many to 'test-tool reach' and create a test that demonstrates that this output returns multiple results. Specifically, we select a list of three commits such that we output two commits that are reachable from one of the first two, respectively, and none are reachable from the third. 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 324dec0191e7579e3990e707d334652c0fe4a786
2 files changed +46
t/helper/test-reach.c
+31
@@ -4,13 +4,34 @@
4 #include "commit-reach.h"
5 #include "config.h"
6 #include "parse-options.h"
7 +#include "string-list.h"
8 #include "tag.h"
9
10 +static void print_sorted_commit_ids(struct commit_list *list)
11 +{
12 + int i;
13 + struct string_list s = STRING_LIST_INIT_DUP;
14 +
15 + while (list) {
16 + string_list_append(&s, oid_to_hex(&list->item->object.oid));
17 + list = list->next;
18 + }
19 +
20 + string_list_sort(&s);
21 +
22 + for (i = 0; i < s.nr; i++)
23 + printf("%s\n", s.items[i].string);
24 +
25 + string_list_clear(&s, 0);
26 +}
27 +
28 int cmd__reach(int ac, const char **av)
29 {
30 struct object_id oid_A, oid_B;
31 struct commit *A, *B;
32 struct commit_list *X;
33 + struct commit **X_array;
34 + int X_nr, X_alloc;
35 struct strbuf buf = STRBUF_INIT;
36 struct repository *r = the_repository;
37
@@ -21,6 +42,9 @@ int cmd__reach(int ac, const char **av)
42
43 A = B = NULL;
44 X = NULL;
45 + X_nr = 0;
46 + X_alloc = 16;
47 + ALLOC_ARRAY(X_array, X_alloc);
48
49 while (strbuf_getline(&buf, stdin) != EOF) {
50 struct object_id oid;
@@ -58,6 +82,8 @@ int cmd__reach(int ac, const char **av)
82
83 case 'X':
84 commit_list_insert(c, &X);
85 + ALLOC_GROW(X_array, X_nr + 1, X_alloc);
86 + X_array[X_nr++] = c;
87 break;
88
89 default:
@@ -72,6 +98,11 @@ int cmd__reach(int ac, const char **av)
98 printf("%s(A,B):%d\n", av[1], in_merge_bases(A, B));
99 else if (!strcmp(av[1], "is_descendant_of"))
100 printf("%s(A,X):%d\n", av[1], is_descendant_of(A, X));
101 + else if (!strcmp(av[1], "get_merge_bases_many")) {
102 + struct commit_list *list = get_merge_bases_many(A, X_nr, X_array);
103 + printf("%s(A,X):\n", av[1]);
104 + print_sorted_commit_ids(list);
105 + }
106
107 exit(0);
108 }
t/t6600-test-reach.sh
+15
@@ -123,4 +123,19 @@ test_expect_success 'is_descendant_of:miss' '
123 test_three_modes is_descendant_of
124 '
125
126 +test_expect_success 'get_merge_bases_many' '
127 + cat >input <<-\EOF &&
128 + A:commit-5-7
129 + X:commit-4-8
130 + X:commit-6-6
131 + X:commit-8-3
132 + EOF
133 + {
134 + echo "get_merge_bases_many(A,X):" &&
135 + git rev-parse commit-5-6 \
136 + commit-4-7 | sort
137 + } >expect &&
138 + test_three_modes get_merge_bases_many
139 +'
140 +
141 test_done