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 commit
**X_array
;
36 struct strbuf buf
= STRBUF_INIT
;
37 struct repository
*r
= the_repository
;
39 setup_git_directory();
48 ALLOC_ARRAY(X_array
, X_alloc
);
50 while (strbuf_getline(&buf
, stdin
) != EOF
) {
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
);
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);
70 die("failed to load commit for input %s resulting in oid %s\n",
71 buf
.buf
, oid_to_hex(&oid
));
85 commit_list_insert(c
, &X
);
86 ALLOC_GROW(X_array
, X_nr
+ 1, X_alloc
);
91 commit_list_insert(c
, &Y
);
95 die("unexpected start of line: %c", buf
.buf
[0]);
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;
124 filter
.with_commit_tag_algo
= 0;
126 printf("%s(_,A,X,_):%d\n", av
[1], commit_contains(&filter
, A
, X
, &cache
));