]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-reach.c
ref-filter.h: provide `REF_FILTER_INIT`
[thirdparty/git.git] / t / helper / test-reach.c
CommitLineData
ab176ac4 1#include "test-tool.h"
36bf1958 2#include "alloc.h"
ab176ac4
DS
3#include "commit.h"
4#include "commit-reach.h"
5#include "config.h"
f394e093 6#include "gettext.h"
41771fa4 7#include "hex.h"
dabab1d6 8#include "object-name.h"
ab176ac4 9#include "parse-options.h"
1fee1242 10#include "ref-filter.h"
e38da487 11#include "setup.h"
324dec01 12#include "string-list.h"
ab176ac4
DS
13#include "tag.h"
14
324dec01
DS
15static void print_sorted_commit_ids(struct commit_list *list)
16{
17 int i;
18 struct string_list s = STRING_LIST_INIT_DUP;
19
20 while (list) {
21 string_list_append(&s, oid_to_hex(&list->item->object.oid));
22 list = list->next;
23 }
24
25 string_list_sort(&s);
26
27 for (i = 0; i < s.nr; i++)
28 printf("%s\n", s.items[i].string);
29
30 string_list_clear(&s, 0);
31}
32
ab176ac4
DS
33int cmd__reach(int ac, const char **av)
34{
35 struct object_id oid_A, oid_B;
5cd52de3 36 struct commit *A, *B;
1792bc12 37 struct commit_list *X, *Y;
b67f6b26 38 struct object_array X_obj = OBJECT_ARRAY_INIT;
4c7bb452
DS
39 struct commit **X_array, **Y_array;
40 int X_nr, X_alloc, Y_nr, Y_alloc;
ab176ac4
DS
41 struct strbuf buf = STRBUF_INIT;
42 struct repository *r = the_repository;
43
44 setup_git_directory();
45
46 if (ac < 2)
47 exit(1);
48
5cd52de3 49 A = B = NULL;
1792bc12 50 X = Y = NULL;
4c7bb452
DS
51 X_nr = Y_nr = 0;
52 X_alloc = Y_alloc = 16;
324dec01 53 ALLOC_ARRAY(X_array, X_alloc);
4c7bb452 54 ALLOC_ARRAY(Y_array, Y_alloc);
ab176ac4
DS
55
56 while (strbuf_getline(&buf, stdin) != EOF) {
57 struct object_id oid;
b67f6b26
DS
58 struct object *orig;
59 struct object *peeled;
ab176ac4
DS
60 struct commit *c;
61 if (buf.len < 3)
62 continue;
63
d850b7a5 64 if (repo_get_oid_committish(the_repository, buf.buf + 2, &oid))
ab176ac4
DS
65 die("failed to resolve %s", buf.buf + 2);
66
b67f6b26
DS
67 orig = parse_object(r, &oid);
68 peeled = deref_tag_noverify(orig);
ab176ac4 69
b67f6b26 70 if (!peeled)
ab176ac4
DS
71 die("failed to load commit for input %s resulting in oid %s\n",
72 buf.buf, oid_to_hex(&oid));
73
6da43d93 74 c = object_as_type(peeled, OBJ_COMMIT, 0);
ab176ac4
DS
75
76 if (!c)
77 die("failed to load commit for input %s resulting in oid %s\n",
78 buf.buf, oid_to_hex(&oid));
79
80 switch (buf.buf[0]) {
81 case 'A':
82 oidcpy(&oid_A, &oid);
5cd52de3 83 A = c;
ab176ac4
DS
84 break;
85
86 case 'B':
87 oidcpy(&oid_B, &oid);
5cd52de3 88 B = c;
ab176ac4
DS
89 break;
90
6255232e
DS
91 case 'X':
92 commit_list_insert(c, &X);
324dec01
DS
93 ALLOC_GROW(X_array, X_nr + 1, X_alloc);
94 X_array[X_nr++] = c;
b67f6b26 95 add_object_array(orig, NULL, &X_obj);
6255232e
DS
96 break;
97
1792bc12
DS
98 case 'Y':
99 commit_list_insert(c, &Y);
4c7bb452
DS
100 ALLOC_GROW(Y_array, Y_nr + 1, Y_alloc);
101 Y_array[Y_nr++] = c;
1792bc12
DS
102 break;
103
ab176ac4
DS
104 default:
105 die("unexpected start of line: %c", buf.buf[0]);
106 }
107 }
108 strbuf_release(&buf);
109
110 if (!strcmp(av[1], "ref_newer"))
111 printf("%s(A,B):%d\n", av[1], ref_newer(&oid_A, &oid_B));
5cd52de3 112 else if (!strcmp(av[1], "in_merge_bases"))
cb338c23
ÆAB
113 printf("%s(A,B):%d\n", av[1],
114 repo_in_merge_bases(the_repository, A, B));
8791bf18 115 else if (!strcmp(av[1], "in_merge_bases_many"))
cb338c23
ÆAB
116 printf("%s(A,X):%d\n", av[1],
117 repo_in_merge_bases_many(the_repository, A, X_nr, X_array));
6255232e 118 else if (!strcmp(av[1], "is_descendant_of"))
c1ea625f 119 printf("%s(A,X):%d\n", av[1], repo_is_descendant_of(r, A, X));
324dec01 120 else if (!strcmp(av[1], "get_merge_bases_many")) {
cb338c23
ÆAB
121 struct commit_list *list = repo_get_merge_bases_many(the_repository,
122 A, X_nr,
123 X_array);
324dec01
DS
124 printf("%s(A,X):\n", av[1]);
125 print_sorted_commit_ids(list);
0c89f715
DS
126 } else if (!strcmp(av[1], "reduce_heads")) {
127 struct commit_list *list = reduce_heads(X);
128 printf("%s(X):\n", av[1]);
129 print_sorted_commit_ids(list);
1792bc12
DS
130 } else if (!strcmp(av[1], "can_all_from_reach")) {
131 printf("%s(X,Y):%d\n", av[1], can_all_from_reach(X, Y, 1));
b67f6b26
DS
132 } else if (!strcmp(av[1], "can_all_from_reach_with_flag")) {
133 struct commit_list *iter = Y;
134
135 while (iter) {
136 iter->item->object.flags |= 2;
137 iter = iter->next;
138 }
139
140 printf("%s(X,_,_,0,0):%d\n", av[1], can_all_from_reach_with_flag(&X_obj, 2, 4, 0, 0));
1fee1242 141 } else if (!strcmp(av[1], "commit_contains")) {
b9f7daa6 142 struct ref_filter filter = REF_FILTER_INIT;
1fee1242
DS
143 struct contains_cache cache;
144 init_contains_cache(&cache);
145
146 if (ac > 2 && !strcmp(av[2], "--tag"))
147 filter.with_commit_tag_algo = 1;
148 else
149 filter.with_commit_tag_algo = 0;
150
151 printf("%s(_,A,X,_):%d\n", av[1], commit_contains(&filter, A, X, &cache));
4c7bb452
DS
152 } else if (!strcmp(av[1], "get_reachable_subset")) {
153 const int reachable_flag = 1;
154 int i, count = 0;
155 struct commit_list *current;
156 struct commit_list *list = get_reachable_subset(X_array, X_nr,
157 Y_array, Y_nr,
158 reachable_flag);
159 printf("get_reachable_subset(X,Y)\n");
160 for (current = list; current; current = current->next) {
161 if (!(list->item->object.flags & reachable_flag))
162 die(_("commit %s is not marked reachable"),
163 oid_to_hex(&list->item->object.oid));
164 count++;
165 }
166 for (i = 0; i < Y_nr; i++) {
167 if (Y_array[i]->object.flags & reachable_flag)
168 count--;
169 }
170
171 if (count < 0)
172 die(_("too many commits marked reachable"));
173
174 print_sorted_commit_ids(list);
324dec01 175 }
ab176ac4 176
338abb0f 177 return 0;
ab176ac4 178}