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