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