| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | |
| 3 | #include "test-tool.h" |
| 4 | #include "commit.h" |
| 5 | #include "commit-reach.h" |
| 6 | #include "gettext.h" |
| 7 | #include "hex.h" |
| 8 | #include "object-name.h" |
| 9 | #include "ref-filter.h" |
| 10 | #include "setup.h" |
| 11 | #include "string-list.h" |
| 12 | #include "tag.h" |
| 13 | |
| 14 | static void print_sorted_commit_ids(struct commit_list *list) |
| 15 | { |
| 16 | struct string_list s = STRING_LIST_INIT_DUP; |
| 17 | |
| 18 | while (list) { |
| 19 | string_list_append(&s, oid_to_hex(&list->item->object.oid)); |
| 20 | list = list->next; |
| 21 | } |
| 22 | |
| 23 | string_list_sort(&s); |
| 24 | |
| 25 | for (size_t i = 0; i < s.nr; i++) |
| 26 | printf("%s\n", s.items[i].string); |
| 27 | |
| 28 | string_list_clear(&s, 0); |
| 29 | } |
| 30 | |
| 31 | int cmd__reach(int ac, const char **av) |
| 32 | { |
| 33 | struct object_id oid_A, oid_B; |
| 34 | struct commit *A, *B; |
| 35 | struct commit_list *X, *Y; |
| 36 | struct object_array X_obj = OBJECT_ARRAY_INIT; |
| 37 | struct commit_stack X_stack = COMMIT_STACK_INIT; |
| 38 | struct commit_stack Y_stack = COMMIT_STACK_INIT; |
| 39 | struct strbuf buf = STRBUF_INIT; |
| 40 | struct repository *r = the_repository; |
| 41 | |
| 42 | setup_git_directory(the_repository); |
| 43 | |
| 44 | if (ac < 2) |
| 45 | exit(1); |
| 46 | |
| 47 | A = B = NULL; |
| 48 | X = Y = NULL; |
| 49 | |
| 50 | while (strbuf_getline(&buf, stdin) != EOF) { |
| 51 | struct object_id oid; |
| 52 | struct object *orig; |
| 53 | struct object *peeled; |
| 54 | struct commit *c; |
| 55 | if (buf.len < 3) |
| 56 | continue; |
| 57 | |
| 58 | if (repo_get_oid_committish(the_repository, buf.buf + 2, &oid)) |
| 59 | die("failed to resolve %s", buf.buf + 2); |
| 60 | |
| 61 | orig = parse_object(r, &oid); |
| 62 | peeled = deref_tag(the_repository, orig, NULL, 0); |
| 63 | |
| 64 | if (!peeled) |
| 65 | die("failed to load commit for input %s resulting in oid %s", |
| 66 | buf.buf, oid_to_hex(&oid)); |
| 67 | |
| 68 | c = object_as_type(peeled, OBJ_COMMIT, 0); |
| 69 | |
| 70 | if (!c) |
| 71 | die("failed to load commit for input %s resulting in oid %s", |
| 72 | buf.buf, oid_to_hex(&oid)); |
| 73 | |
| 74 | switch (buf.buf[0]) { |
| 75 | case 'A': |
| 76 | oidcpy(&oid_A, &oid); |
| 77 | A = c; |
| 78 | break; |
| 79 | |
| 80 | case 'B': |
| 81 | oidcpy(&oid_B, &oid); |
| 82 | B = c; |
| 83 | break; |
| 84 | |
| 85 | case 'X': |
| 86 | commit_list_insert(c, &X); |
| 87 | commit_stack_push(&X_stack, c); |
| 88 | add_object_array(orig, NULL, &X_obj); |
| 89 | break; |
| 90 | |
| 91 | case 'Y': |
| 92 | commit_list_insert(c, &Y); |
| 93 | commit_stack_push(&Y_stack, c); |
| 94 | break; |
| 95 | |
| 96 | default: |
| 97 | die("unexpected start of line: %c", buf.buf[0]); |
| 98 | } |
| 99 | } |
| 100 | strbuf_release(&buf); |
| 101 | |
| 102 | if (!strcmp(av[1], "ref_newer")) |
| 103 | printf("%s(A,B):%d\n", av[1], ref_newer(&oid_A, &oid_B)); |
| 104 | else if (!strcmp(av[1], "in_merge_bases")) |
| 105 | printf("%s(A,B):%d\n", av[1], |
| 106 | repo_in_merge_bases(the_repository, A, B)); |
| 107 | else if (!strcmp(av[1], "in_merge_bases_many")) |
| 108 | printf("%s(A,X):%d\n", av[1], |
| 109 | repo_in_merge_bases_many(the_repository, A, X_stack.nr, X_stack.items, 0)); |
| 110 | else if (!strcmp(av[1], "is_descendant_of")) |
| 111 | printf("%s(A,X):%d\n", av[1], repo_is_descendant_of(r, A, X)); |
| 112 | else if (!strcmp(av[1], "get_branch_base_for_tip")) |
| 113 | printf("%s(A,X):%d\n", av[1], get_branch_base_for_tip(r, A, X_stack.items, X_stack.nr)); |
| 114 | else if (!strcmp(av[1], "get_merge_bases_many")) { |
| 115 | struct commit_list *list = NULL; |
| 116 | if (repo_get_merge_bases_many(the_repository, |
| 117 | A, X_stack.nr, |
| 118 | X_stack.items, |
| 119 | &list) < 0) |
| 120 | exit(128); |
| 121 | printf("%s(A,X):\n", av[1]); |
| 122 | print_sorted_commit_ids(list); |
| 123 | commit_list_free(list); |
| 124 | } else if (!strcmp(av[1], "reduce_heads")) { |
| 125 | struct commit_list *list = reduce_heads(X); |
| 126 | printf("%s(X):\n", av[1]); |
| 127 | print_sorted_commit_ids(list); |
| 128 | commit_list_free(list); |
| 129 | } else if (!strcmp(av[1], "can_all_from_reach")) { |
| 130 | printf("%s(X,Y):%d\n", av[1], can_all_from_reach(X, Y, 1)); |
| 131 | } else if (!strcmp(av[1], "can_all_from_reach_with_flag")) { |
| 132 | struct commit_list *iter = Y; |
| 133 | |
| 134 | while (iter) { |
| 135 | iter->item->object.flags |= 2; |
| 136 | iter = iter->next; |
| 137 | } |
| 138 | |
| 139 | printf("%s(X,_,_,0,0):%d\n", av[1], can_all_from_reach_with_flag(&X_obj, 2, 4, 0, 0)); |
| 140 | } else if (!strcmp(av[1], "commit_contains")) { |
| 141 | struct ref_filter filter = REF_FILTER_INIT; |
| 142 | struct contains_cache cache; |
| 143 | init_contains_cache(&cache); |
| 144 | |
| 145 | if (ac > 2 && !strcmp(av[2], "--tag")) |
| 146 | filter.with_commit_tag_algo = 1; |
| 147 | else |
| 148 | filter.with_commit_tag_algo = 0; |
| 149 | |
| 150 | printf("%s(_,A,X,_):%d\n", av[1], commit_contains(&filter, A, X, &cache)); |
| 151 | clear_contains_cache(&cache); |
| 152 | } else if (!strcmp(av[1], "get_reachable_subset")) { |
| 153 | const int reachable_flag = 1; |
| 154 | int count = 0; |
| 155 | struct commit_list *current; |
| 156 | struct commit_list *list = get_reachable_subset(X_stack.items, X_stack.nr, |
| 157 | Y_stack.items, Y_stack.nr, |
| 158 | reachable_flag); |
| 159 | printf("get_reachable_subset(X,Y)\n"); |
| 160 | for (current = list; current; current = current->next) { |
| 161 | if (!(list->item->object.flags & reachable_flag)) |
| 162 | die(_("commit %s is not marked reachable"), |
| 163 | oid_to_hex(&list->item->object.oid)); |
| 164 | count++; |
| 165 | } |
| 166 | for (size_t i = 0; i < Y_stack.nr; i++) { |
| 167 | if (Y_stack.items[i]->object.flags & reachable_flag) |
| 168 | count--; |
| 169 | } |
| 170 | |
| 171 | if (count < 0) |
| 172 | die(_("too many commits marked reachable")); |
| 173 | |
| 174 | print_sorted_commit_ids(list); |
| 175 | commit_list_free(list); |
| 176 | } |
| 177 | |
| 178 | object_array_clear(&X_obj); |
| 179 | strbuf_release(&buf); |
| 180 | commit_list_free(X); |
| 181 | commit_list_free(Y); |
| 182 | commit_stack_clear(&X_stack); |
| 183 | commit_stack_clear(&Y_stack); |
| 184 | return 0; |
| 185 | } |