4 #include "commit-reach.h"
6 #include "parse-options.h"
7 #include "ref-filter.h"
8 #include "string-list.h"
11 static void print_sorted_commit_ids(struct commit_list *list)
14 struct string_list s = STRING_LIST_INIT_DUP;
17 string_list_append(&s, oid_to_hex(&list->item->object.oid));
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;
33 struct commit_list *X, *Y;
34 struct object_array X_obj = OBJECT_ARRAY_INIT;
35 struct commit **X_array, **Y_array;
36 int X_nr, X_alloc, Y_nr, Y_alloc;
37 struct strbuf buf = STRBUF_INIT;
38 struct repository *r = the_repository;
40 setup_git_directory();
48 X_alloc = Y_alloc = 16;
49 ALLOC_ARRAY(X_array, X_alloc);
50 ALLOC_ARRAY(Y_array, Y_alloc);
52 while (strbuf_getline(&buf, stdin) != EOF) {
55 struct object *peeled;
60 if (get_oid_committish(buf.buf + 2, &oid))
61 die("failed to resolve %s", buf.buf + 2);
63 orig = parse_object(r, &oid);
64 peeled = deref_tag_noverify(orig);
67 die("failed to load commit for input %s resulting in oid %s\n",
68 buf.buf, oid_to_hex(&oid));
70 c = object_as_type(r, peeled, OBJ_COMMIT, 0);
73 die("failed to load commit for input %s resulting in oid %s\n",
74 buf.buf, oid_to_hex(&oid));
88 commit_list_insert(c, &X);
89 ALLOC_GROW(X_array, X_nr + 1, X_alloc);
91 add_object_array(orig, NULL, &X_obj);
95 commit_list_insert(c, &Y);
96 ALLOC_GROW(Y_array, Y_nr + 1, Y_alloc);
101 die("unexpected start of line: %c", buf.buf[0]);
104 strbuf_release(&buf);
106 if (!strcmp(av[1], "ref_newer"))
107 printf("%s(A,B):%d\n", av[1], ref_newer(&oid_A, &oid_B));
108 else if (!strcmp(av[1], "in_merge_bases"))
109 printf("%s(A,B):%d\n", av[1], in_merge_bases(A, B));
110 else if (!strcmp(av[1], "is_descendant_of"))
111 printf("%s(A,X):%d\n", av[1], is_descendant_of(A, X));
112 else if (!strcmp(av[1], "get_merge_bases_many")) {
113 struct commit_list *list = get_merge_bases_many(A, X_nr, X_array);
114 printf("%s(A,X):\n", av[1]);
115 print_sorted_commit_ids(list);
116 } else if (!strcmp(av[1], "reduce_heads")) {
117 struct commit_list *list = reduce_heads(X);
118 printf("%s(X):\n", av[1]);
119 print_sorted_commit_ids(list);
120 } else if (!strcmp(av[1], "can_all_from_reach")) {
121 printf("%s(X,Y):%d\n", av[1], can_all_from_reach(X, Y, 1));
122 } else if (!strcmp(av[1], "can_all_from_reach_with_flag")) {
123 struct commit_list *iter = Y;
126 iter->item->object.flags |= 2;
130 printf("%s(X,_,_,0,0):%d\n", av[1], can_all_from_reach_with_flag(&X_obj, 2, 4, 0, 0));
131 } else if (!strcmp(av[1], "commit_contains")) {
132 struct ref_filter filter;
133 struct contains_cache cache;
134 init_contains_cache(&cache);
136 if (ac > 2 && !strcmp(av[2], "--tag"))
137 filter.with_commit_tag_algo = 1;
139 filter.with_commit_tag_algo = 0;
141 printf("%s(_,A,X,_):%d\n", av[1], commit_contains(&filter, A, X, &cache));
142 } else if (!strcmp(av[1], "get_reachable_subset")) {
143 const int reachable_flag = 1;
145 struct commit_list *current;
146 struct commit_list *list = get_reachable_subset(X_array, X_nr,
149 printf("get_reachable_subset(X,Y)\n");
150 for (current = list; current; current = current->next) {
151 if (!(list->item->object.flags & reachable_flag))
152 die(_("commit %s is not marked reachable"),
153 oid_to_hex(&list->item->object.oid));
156 for (i = 0; i < Y_nr; i++) {
157 if (Y_array[i]->object.flags & reachable_flag)
162 die(_("too many commits marked reachable"));
164 print_sorted_commit_ids(list);