Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/t/helper/test-reach.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / t / helper / test-reach.c` 186 lines ยท 5021 bytes Raw Blame History 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(r, 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], 140 can_all_from_reach_with_flag(r, &X_obj, 2, 4, 0, 0)); 141 } else if (!strcmp(av[1], "commit_contains")) { 142 struct ref_filter filter = REF_FILTER_INIT; 143 struct contains_cache cache; 144 init_contains_cache(&cache); 145 146 if (ac > 2 && !strcmp(av[2], "--tag")) 147 filter.with_commit_tag_algo = 1; 148 else 149 filter.with_commit_tag_algo = 0; 150 151 printf("%s(_,A,X,_):%d\n", av[1], commit_contains(&filter, A, X, &cache)); 152 clear_contains_cache(&cache); 153 } else if (!strcmp(av[1], "get_reachable_subset")) { 154 const int reachable_flag = 1; 155 int count = 0; 156 struct commit_list *current; 157 struct commit_list *list = get_reachable_subset(X_stack.items, X_stack.nr, 158 Y_stack.items, Y_stack.nr, 159 reachable_flag); 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)); 165 count++; 166 } 167 for (size_t i = 0; i < Y_stack.nr; i++) { 168 if (Y_stack.items[i]->object.flags & reachable_flag) 169 count--; 170 } 171 172 if (count < 0) 173 die(_("too many commits marked reachable")); 174 175 print_sorted_commit_ids(list); 176 commit_list_free(list); 177 } 178 179 object_array_clear(&X_obj); 180 strbuf_release(&buf); 181 commit_list_free(X); 182 commit_list_free(Y); 183 commit_stack_clear(&X_stack); 184 commit_stack_clear(&Y_stack); 185 return 0; 186 }