test-reach: test is_descendant_of
[git/raj.git] / t / helper / test-reach.c
blobdccbd48178416d2ecc5c2a3261821bc4670dad0f
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 "tag.h"
9 int cmd__reach(int ac, const char **av)
11 struct object_id oid_A, oid_B;
12 struct commit *A, *B;
13 struct commit_list *X;
14 struct strbuf buf = STRBUF_INIT;
15 struct repository *r = the_repository;
17 setup_git_directory();
19 if (ac < 2)
20 exit(1);
22 A = B = NULL;
23 X = NULL;
25 while (strbuf_getline(&buf, stdin) != EOF) {
26 struct object_id oid;
27 struct object *o;
28 struct commit *c;
29 if (buf.len < 3)
30 continue;
32 if (get_oid_committish(buf.buf + 2, &oid))
33 die("failed to resolve %s", buf.buf + 2);
35 o = parse_object(r, &oid);
36 o = deref_tag_noverify(o);
38 if (!o)
39 die("failed to load commit for input %s resulting in oid %s\n",
40 buf.buf, oid_to_hex(&oid));
42 c = object_as_type(r, o, OBJ_COMMIT, 0);
44 if (!c)
45 die("failed to load commit for input %s resulting in oid %s\n",
46 buf.buf, oid_to_hex(&oid));
48 switch (buf.buf[0]) {
49 case 'A':
50 oidcpy(&oid_A, &oid);
51 A = c;
52 break;
54 case 'B':
55 oidcpy(&oid_B, &oid);
56 B = c;
57 break;
59 case 'X':
60 commit_list_insert(c, &X);
61 break;
63 default:
64 die("unexpected start of line: %c", buf.buf[0]);
67 strbuf_release(&buf);
69 if (!strcmp(av[1], "ref_newer"))
70 printf("%s(A,B):%d\n", av[1], ref_newer(&oid_A, &oid_B));
71 else if (!strcmp(av[1], "in_merge_bases"))
72 printf("%s(A,B):%d\n", av[1], in_merge_bases(A, B));
73 else if (!strcmp(av[1], "is_descendant_of"))
74 printf("%s(A,X):%d\n", av[1], is_descendant_of(A, X));
76 exit(0);