blob: 4df01187c9172088810fa8e7880a94429d956a42 [file] [log] [blame]
Derrick Stoleeab176ac2018-07-20 16:33:15 +00001#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"
Derrick Stolee324dec02018-07-20 16:33:20 +00007#include "string-list.h"
Derrick Stoleeab176ac2018-07-20 16:33:15 +00008#include "tag.h"
9
Derrick Stolee324dec02018-07-20 16:33:20 +000010static void print_sorted_commit_ids(struct commit_list *list)
11{
12 int i;
13 struct string_list s = STRING_LIST_INIT_DUP;
14
15 while (list) {
16 string_list_append(&s, oid_to_hex(&list->item->object.oid));
17 list = list->next;
18 }
19
20 string_list_sort(&s);
21
22 for (i = 0; i < s.nr; i++)
23 printf("%s\n", s.items[i].string);
24
25 string_list_clear(&s, 0);
26}
27
Derrick Stoleeab176ac2018-07-20 16:33:15 +000028int cmd__reach(int ac, const char **av)
29{
30 struct object_id oid_A, oid_B;
Derrick Stolee5cd52de2018-07-20 16:33:17 +000031 struct commit *A, *B;
Derrick Stolee62552322018-07-20 16:33:18 +000032 struct commit_list *X;
Derrick Stolee324dec02018-07-20 16:33:20 +000033 struct commit **X_array;
34 int X_nr, X_alloc;
Derrick Stoleeab176ac2018-07-20 16:33:15 +000035 struct strbuf buf = STRBUF_INIT;
36 struct repository *r = the_repository;
37
38 setup_git_directory();
39
40 if (ac < 2)
41 exit(1);
42
Derrick Stolee5cd52de2018-07-20 16:33:17 +000043 A = B = NULL;
Derrick Stolee62552322018-07-20 16:33:18 +000044 X = NULL;
Derrick Stolee324dec02018-07-20 16:33:20 +000045 X_nr = 0;
46 X_alloc = 16;
47 ALLOC_ARRAY(X_array, X_alloc);
Derrick Stoleeab176ac2018-07-20 16:33:15 +000048
49 while (strbuf_getline(&buf, stdin) != EOF) {
50 struct object_id oid;
51 struct object *o;
52 struct commit *c;
53 if (buf.len < 3)
54 continue;
55
56 if (get_oid_committish(buf.buf + 2, &oid))
57 die("failed to resolve %s", buf.buf + 2);
58
59 o = parse_object(r, &oid);
60 o = deref_tag_noverify(o);
61
62 if (!o)
63 die("failed to load commit for input %s resulting in oid %s\n",
64 buf.buf, oid_to_hex(&oid));
65
66 c = object_as_type(r, o, OBJ_COMMIT, 0);
67
68 if (!c)
69 die("failed to load commit for input %s resulting in oid %s\n",
70 buf.buf, oid_to_hex(&oid));
71
72 switch (buf.buf[0]) {
73 case 'A':
74 oidcpy(&oid_A, &oid);
Derrick Stolee5cd52de2018-07-20 16:33:17 +000075 A = c;
Derrick Stoleeab176ac2018-07-20 16:33:15 +000076 break;
77
78 case 'B':
79 oidcpy(&oid_B, &oid);
Derrick Stolee5cd52de2018-07-20 16:33:17 +000080 B = c;
Derrick Stoleeab176ac2018-07-20 16:33:15 +000081 break;
82
Derrick Stolee62552322018-07-20 16:33:18 +000083 case 'X':
84 commit_list_insert(c, &X);
Derrick Stolee324dec02018-07-20 16:33:20 +000085 ALLOC_GROW(X_array, X_nr + 1, X_alloc);
86 X_array[X_nr++] = c;
Derrick Stolee62552322018-07-20 16:33:18 +000087 break;
88
Derrick Stoleeab176ac2018-07-20 16:33:15 +000089 default:
90 die("unexpected start of line: %c", buf.buf[0]);
91 }
92 }
93 strbuf_release(&buf);
94
95 if (!strcmp(av[1], "ref_newer"))
96 printf("%s(A,B):%d\n", av[1], ref_newer(&oid_A, &oid_B));
Derrick Stolee5cd52de2018-07-20 16:33:17 +000097 else if (!strcmp(av[1], "in_merge_bases"))
98 printf("%s(A,B):%d\n", av[1], in_merge_bases(A, B));
Derrick Stolee62552322018-07-20 16:33:18 +000099 else if (!strcmp(av[1], "is_descendant_of"))
100 printf("%s(A,X):%d\n", av[1], is_descendant_of(A, X));
Derrick Stolee324dec02018-07-20 16:33:20 +0000101 else if (!strcmp(av[1], "get_merge_bases_many")) {
102 struct commit_list *list = get_merge_bases_many(A, X_nr, X_array);
103 printf("%s(A,X):\n", av[1]);
104 print_sorted_commit_ids(list);
105 }
Derrick Stoleeab176ac2018-07-20 16:33:15 +0000106
107 exit(0);
108}