| 1115 | } |
| 1116 | |
| 1117 | void ahead_behind(struct repository *r, |
| 1118 | struct commit **commits, size_t commits_nr, |
| 1119 | struct ahead_behind_count *counts, size_t counts_nr) |
| 1120 | { |
| 1121 | struct nonstale_queue queue = { |
| 1122 | { .compare = compare_commits_by_gen_then_commit_date } |
| 1123 | }; |
| 1124 | size_t width = DIV_ROUND_UP(commits_nr, BITS_IN_EWORD); |
| 1125 | |
| 1126 | if (!commits_nr || !counts_nr) |
| 1127 | return; |
| 1128 | |
| 1129 | for (size_t i = 0; i < counts_nr; i++) { |
| 1130 | counts[i].ahead = 0; |
| 1131 | counts[i].behind = 0; |
| 1132 | } |
| 1133 | |
| 1134 | ensure_generations_valid(r, commits, commits_nr); |
| 1135 | |
| 1136 | init_bit_arrays(&bit_arrays); |
| 1137 | |
| 1138 | for (size_t i = 0; i < commits_nr; i++) { |
| 1139 | struct commit *c = commits[i]; |
| 1140 | struct bitmap *bitmap = get_bit_array(c, width); |
| 1141 | |
| 1142 | bitmap_set(bitmap, i); |
| 1143 | insert_no_dup(&queue, c); |
| 1144 | } |
| 1145 | |
| 1146 | while (queue.max_nonstale) { |
| 1147 | struct commit *c = nonstale_queue_get(&queue); |
| 1148 | struct commit_list *p; |
| 1149 | struct bitmap *bitmap_c = get_bit_array(c, width); |
| 1150 | |
| 1151 | for (size_t i = 0; i < counts_nr; i++) { |
| 1152 | int reach_from_tip = !!bitmap_get(bitmap_c, counts[i].tip_index); |
| 1153 | int reach_from_base = !!bitmap_get(bitmap_c, counts[i].base_index); |
| 1154 | |
| 1155 | if (reach_from_tip ^ reach_from_base) { |
| 1156 | if (reach_from_base) |
| 1157 | counts[i].behind++; |
| 1158 | else |
| 1159 | counts[i].ahead++; |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | for (p = c->parents; p; p = p->next) { |
| 1164 | struct bitmap *bitmap_p; |
| 1165 | |
| 1166 | repo_parse_commit(r, p->item); |
| 1167 | |
| 1168 | bitmap_p = get_bit_array(p->item, width); |
| 1169 | bitmap_or(bitmap_p, bitmap_c); |
| 1170 | |
| 1171 | /* |
| 1172 | * If this parent is reachable from every starting |
| 1173 | * commit, then none of its ancestors can contribute |
| 1174 | * to the ahead/behind count. Mark it as STALE, so |
no test coverage detected