Merge branch 'jk/reopen-tempfile-truncate'
[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 commit **X_array;
35         int X_nr, X_alloc;
36         struct strbuf buf = STRBUF_INIT;
37         struct repository *r = the_repository;
38
39         setup_git_directory();
40
41         if (ac < 2)
42                 exit(1);
43
44         A = B = NULL;
45         X = Y = NULL;
46         X_nr = 0;
47         X_alloc = 16;
48         ALLOC_ARRAY(X_array, X_alloc);
49
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;
56
57                 if (get_oid_committish(buf.buf + 2, &oid))
58                         die("failed to resolve %s", buf.buf + 2);
59
60                 o = parse_object(r, &oid);
61                 o = deref_tag_noverify(o);
62
63                 if (!o)
64                         die("failed to load commit for input %s resulting in oid %s\n",
65                             buf.buf, oid_to_hex(&oid));
66
67                 c = object_as_type(r, o, OBJ_COMMIT, 0);
68
69                 if (!c)
70                         die("failed to load commit for input %s resulting in oid %s\n",
71                             buf.buf, oid_to_hex(&oid));
72
73                 switch (buf.buf[0]) {
74                         case 'A':
75                                 oidcpy(&oid_A, &oid);
76                                 A = c;
77                                 break;
78
79                         case 'B':
80                                 oidcpy(&oid_B, &oid);
81                                 B = c;
82                                 break;
83
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;
89
90                         case 'Y':
91                                 commit_list_insert(c, &Y);
92                                 break;
93
94                         default:
95                                 die("unexpected start of line: %c", buf.buf[0]);
96                 }
97         }
98         strbuf_release(&buf);
99
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);
120
121                 if (ac > 2 && !strcmp(av[2], "--tag"))
122                         filter.with_commit_tag_algo = 1;
123                 else
124                         filter.with_commit_tag_algo = 0;
125
126                 printf("%s(_,A,X,_):%d\n", av[1], commit_contains(&filter, A, X, &cache));
127         }
128
129         exit(0);
130 }