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