test-reach: test commit_contains
[git.git] / t / helper / test-reach.c
blobeb2110399829d3b6cfb0c5a20eec7d279e45d0a9
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"
7 #include "ref-filter.h"
8 #include "string-list.h"
9 #include "tag.h"
11 static void print_sorted_commit_ids(struct commit_list *list)
13 int i;
14 struct string_list s = STRING_LIST_INIT_DUP;
16 while (list) {
17 string_list_append(&s, oid_to_hex(&list->item->object.oid));
18 list = list->next;
21 string_list_sort(&s);
23 for (i = 0; i < s.nr; i++)
24 printf("%s\n", s.items[i].string);
26 string_list_clear(&s, 0);
29 int cmd__reach(int ac, const char **av)
31 struct object_id oid_A, oid_B;
32 struct commit *A, *B;
33 struct commit_list *X, *Y;
34 struct commit **X_array;
35 int X_nr, X_alloc;
36 struct strbuf buf = STRBUF_INIT;
37 struct repository *r = the_repository;
39 setup_git_directory();
41 if (ac < 2)
42 exit(1);
44 A = B = NULL;
45 X = Y = NULL;
46 X_nr = 0;
47 X_alloc = 16;
48 ALLOC_ARRAY(X_array, X_alloc);
50 while (strbuf_getline(&buf, stdin) != EOF) {
51 struct object_id oid;
52 struct object *o;
53 struct commit *c;
54 if (buf.len < 3)
55 continue;
57 if (get_oid_committish(buf.buf + 2, &oid))
58 die("failed to resolve %s", buf.buf + 2);
60 o = parse_object(r, &oid);
61 o = deref_tag_noverify(o);
63 if (!o)
64 die("failed to load commit for input %s resulting in oid %s\n",
65 buf.buf, oid_to_hex(&oid));
67 c = object_as_type(r, o, OBJ_COMMIT, 0);
69 if (!c)
70 die("failed to load commit for input %s resulting in oid %s\n",
71 buf.buf, oid_to_hex(&oid));
73 switch (buf.buf[0]) {
74 case 'A':
75 oidcpy(&oid_A, &oid);
76 A = c;
77 break;
79 case 'B':
80 oidcpy(&oid_B, &oid);
81 B = c;
82 break;
84 case 'X':
85 commit_list_insert(c, &X);
86 ALLOC_GROW(X_array, X_nr + 1, X_alloc);
87 X_array[X_nr++] = c;
88 break;
90 case 'Y':
91 commit_list_insert(c, &Y);
92 break;
94 default:
95 die("unexpected start of line: %c", buf.buf[0]);
98 strbuf_release(&buf);
100 if (!strcmp(av[1], "ref_newer"))
101 printf("%s(A,B):%d\n", av[1], ref_newer(&oid_A, &oid_B));
102 else if (!strcmp(av[1], "in_merge_bases"))
103 printf("%s(A,B):%d\n", av[1], in_merge_bases(A, B));
104 else if (!strcmp(av[1], "is_descendant_of"))
105 printf("%s(A,X):%d\n", av[1], is_descendant_of(A, X));
106 else if (!strcmp(av[1], "get_merge_bases_many")) {
107 struct commit_list *list = get_merge_bases_many(A, X_nr, X_array);
108 printf("%s(A,X):\n", av[1]);
109 print_sorted_commit_ids(list);
110 } else if (!strcmp(av[1], "reduce_heads")) {
111 struct commit_list *list = reduce_heads(X);
112 printf("%s(X):\n", av[1]);
113 print_sorted_commit_ids(list);
114 } else if (!strcmp(av[1], "can_all_from_reach")) {
115 printf("%s(X,Y):%d\n", av[1], can_all_from_reach(X, Y, 1));
116 } else if (!strcmp(av[1], "commit_contains")) {
117 struct ref_filter filter;
118 struct contains_cache cache;
119 init_contains_cache(&cache);
121 if (ac > 2 && !strcmp(av[2], "--tag"))
122 filter.with_commit_tag_algo = 1;
123 else
124 filter.with_commit_tag_algo = 0;
126 printf("%s(_,A,X,_):%d\n", av[1], commit_contains(&filter, A, X, &cache));
129 exit(0);