Merge branch 'tq/branch-create-wo-branch-get'
[git] / t / helper / test-reach.c
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"
10
11 static 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
29 int cmd__reach(int ac, const char **av)
30 {
31         struct object_id oid_A, oid_B;
32         struct commit *A, *B;
33         struct commit_list *X, *Y;
34         struct object_array X_obj = OBJECT_ARRAY_INIT;
35         struct commit **X_array;
36         int X_nr, X_alloc;
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
45         A = B = NULL;
46         X = Y = NULL;
47         X_nr = 0;
48         X_alloc = 16;
49         ALLOC_ARRAY(X_array, X_alloc);
50
51         while (strbuf_getline(&buf, stdin) != EOF) {
52                 struct object_id oid;
53                 struct object *orig;
54                 struct object *peeled;
55                 struct commit *c;
56                 if (buf.len < 3)
57                         continue;
58
59                 if (get_oid_committish(buf.buf + 2, &oid))
60                         die("failed to resolve %s", buf.buf + 2);
61
62                 orig = parse_object(r, &oid);
63                 peeled = deref_tag_noverify(orig);
64
65                 if (!peeled)
66                         die("failed to load commit for input %s resulting in oid %s\n",
67                             buf.buf, oid_to_hex(&oid));
68
69                 c = object_as_type(r, peeled, OBJ_COMMIT, 0);
70
71                 if (!c)
72                         die("failed to load commit for input %s resulting in oid %s\n",
73                             buf.buf, oid_to_hex(&oid));
74
75                 switch (buf.buf[0]) {
76                         case 'A':
77                                 oidcpy(&oid_A, &oid);
78                                 A = c;
79                                 break;
80
81                         case 'B':
82                                 oidcpy(&oid_B, &oid);
83                                 B = c;
84                                 break;
85
86                         case 'X':
87                                 commit_list_insert(c, &X);
88                                 ALLOC_GROW(X_array, X_nr + 1, X_alloc);
89                                 X_array[X_nr++] = c;
90                                 add_object_array(orig, NULL, &X_obj);
91                                 break;
92
93                         case 'Y':
94                                 commit_list_insert(c, &Y);
95                                 break;
96
97                         default:
98                                 die("unexpected start of line: %c", buf.buf[0]);
99                 }
100         }
101         strbuf_release(&buf);
102
103         if (!strcmp(av[1], "ref_newer"))
104                 printf("%s(A,B):%d\n", av[1], ref_newer(&oid_A, &oid_B));
105         else if (!strcmp(av[1], "in_merge_bases"))
106                 printf("%s(A,B):%d\n", av[1], in_merge_bases(A, B));
107         else if (!strcmp(av[1], "is_descendant_of"))
108                 printf("%s(A,X):%d\n", av[1], is_descendant_of(A, X));
109         else if (!strcmp(av[1], "get_merge_bases_many")) {
110                 struct commit_list *list = get_merge_bases_many(A, X_nr, X_array);
111                 printf("%s(A,X):\n", av[1]);
112                 print_sorted_commit_ids(list);
113         } else if (!strcmp(av[1], "reduce_heads")) {
114                 struct commit_list *list = reduce_heads(X);
115                 printf("%s(X):\n", av[1]);
116                 print_sorted_commit_ids(list);
117         } else if (!strcmp(av[1], "can_all_from_reach")) {
118                 printf("%s(X,Y):%d\n", av[1], can_all_from_reach(X, Y, 1));
119         } else if (!strcmp(av[1], "can_all_from_reach_with_flag")) {
120                 struct commit_list *iter = Y;
121
122                 while (iter) {
123                         iter->item->object.flags |= 2;
124                         iter = iter->next;
125                 }
126
127                 printf("%s(X,_,_,0,0):%d\n", av[1], can_all_from_reach_with_flag(&X_obj, 2, 4, 0, 0));
128         } else if (!strcmp(av[1], "commit_contains")) {
129                 struct ref_filter filter;
130                 struct contains_cache cache;
131                 init_contains_cache(&cache);
132
133                 if (ac > 2 && !strcmp(av[2], "--tag"))
134                         filter.with_commit_tag_algo = 1;
135                 else
136                         filter.with_commit_tag_algo = 0;
137
138                 printf("%s(_,A,X,_):%d\n", av[1], commit_contains(&filter, A, X, &cache));
139         }
140
141         exit(0);
142 }