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