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