5 #include "commit-reach.h"
9 #include "object-name.h"
10 #include "parse-options.h"
11 #include "ref-filter.h"
13 #include "string-list.h"
16 static void print_sorted_commit_ids(struct commit_list
*list
)
19 struct string_list s
= STRING_LIST_INIT_DUP
;
22 string_list_append(&s
, oid_to_hex(&list
->item
->object
.oid
));
28 for (i
= 0; i
< s
.nr
; i
++)
29 printf("%s\n", s
.items
[i
].string
);
31 string_list_clear(&s
, 0);
34 int cmd__reach(int ac
, const char **av
)
36 struct object_id oid_A
, oid_B
;
38 struct commit_list
*X
, *Y
;
39 struct object_array X_obj
= OBJECT_ARRAY_INIT
;
40 struct commit
**X_array
, **Y_array
;
41 int X_nr
, X_alloc
, Y_nr
, Y_alloc
;
42 struct strbuf buf
= STRBUF_INIT
;
43 struct repository
*r
= the_repository
;
45 setup_git_directory();
53 X_alloc
= Y_alloc
= 16;
54 ALLOC_ARRAY(X_array
, X_alloc
);
55 ALLOC_ARRAY(Y_array
, Y_alloc
);
57 while (strbuf_getline(&buf
, stdin
) != EOF
) {
60 struct object
*peeled
;
65 if (repo_get_oid_committish(the_repository
, buf
.buf
+ 2, &oid
))
66 die("failed to resolve %s", buf
.buf
+ 2);
68 orig
= parse_object(r
, &oid
);
69 peeled
= deref_tag_noverify(orig
);
72 die("failed to load commit for input %s resulting in oid %s\n",
73 buf
.buf
, oid_to_hex(&oid
));
75 c
= object_as_type(peeled
, OBJ_COMMIT
, 0);
78 die("failed to load commit for input %s resulting in oid %s\n",
79 buf
.buf
, oid_to_hex(&oid
));
93 commit_list_insert(c
, &X
);
94 ALLOC_GROW(X_array
, X_nr
+ 1, X_alloc
);
96 add_object_array(orig
, NULL
, &X_obj
);
100 commit_list_insert(c
, &Y
);
101 ALLOC_GROW(Y_array
, Y_nr
+ 1, Y_alloc
);
106 die("unexpected start of line: %c", buf
.buf
[0]);
109 strbuf_release(&buf
);
111 if (!strcmp(av
[1], "ref_newer"))
112 printf("%s(A,B):%d\n", av
[1], ref_newer(&oid_A
, &oid_B
));
113 else if (!strcmp(av
[1], "in_merge_bases"))
114 printf("%s(A,B):%d\n", av
[1],
115 repo_in_merge_bases(the_repository
, A
, B
));
116 else if (!strcmp(av
[1], "in_merge_bases_many"))
117 printf("%s(A,X):%d\n", av
[1],
118 repo_in_merge_bases_many(the_repository
, A
, X_nr
, X_array
));
119 else if (!strcmp(av
[1], "is_descendant_of"))
120 printf("%s(A,X):%d\n", av
[1], repo_is_descendant_of(r
, A
, X
));
121 else if (!strcmp(av
[1], "get_merge_bases_many")) {
122 struct commit_list
*list
= repo_get_merge_bases_many(the_repository
,
125 printf("%s(A,X):\n", av
[1]);
126 print_sorted_commit_ids(list
);
127 } else if (!strcmp(av
[1], "reduce_heads")) {
128 struct commit_list
*list
= reduce_heads(X
);
129 printf("%s(X):\n", av
[1]);
130 print_sorted_commit_ids(list
);
131 } else if (!strcmp(av
[1], "can_all_from_reach")) {
132 printf("%s(X,Y):%d\n", av
[1], can_all_from_reach(X
, Y
, 1));
133 } else if (!strcmp(av
[1], "can_all_from_reach_with_flag")) {
134 struct commit_list
*iter
= Y
;
137 iter
->item
->object
.flags
|= 2;
141 printf("%s(X,_,_,0,0):%d\n", av
[1], can_all_from_reach_with_flag(&X_obj
, 2, 4, 0, 0));
142 } else if (!strcmp(av
[1], "commit_contains")) {
143 struct ref_filter filter
;
144 struct contains_cache cache
;
145 init_contains_cache(&cache
);
147 if (ac
> 2 && !strcmp(av
[2], "--tag"))
148 filter
.with_commit_tag_algo
= 1;
150 filter
.with_commit_tag_algo
= 0;
152 printf("%s(_,A,X,_):%d\n", av
[1], commit_contains(&filter
, A
, X
, &cache
));
153 } else if (!strcmp(av
[1], "get_reachable_subset")) {
154 const int reachable_flag
= 1;
156 struct commit_list
*current
;
157 struct commit_list
*list
= get_reachable_subset(X_array
, X_nr
,
160 printf("get_reachable_subset(X,Y)\n");
161 for (current
= list
; current
; current
= current
->next
) {
162 if (!(list
->item
->object
.flags
& reachable_flag
))
163 die(_("commit %s is not marked reachable"),
164 oid_to_hex(&list
->item
->object
.oid
));
167 for (i
= 0; i
< Y_nr
; i
++) {
168 if (Y_array
[i
]->object
.flags
& reachable_flag
)
173 die(_("too many commits marked reachable"));
175 print_sorted_commit_ids(list
);