THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

794 lines
21KB

  1. #include "cache.h"
  2. #include "repository.h"
  3. #include "tempfile.h"
  4. #include "lockfile.h"
  5. #include "object-store.h"
  6. #include "commit.h"
  7. #include "tag.h"
  8. #include "pkt-line.h"
  9. #include "remote.h"
  10. #include "refs.h"
  11. #include "sha1-array.h"
  12. #include "diff.h"
  13. #include "revision.h"
  14. #include "commit-slab.h"
  15. #include "list-objects.h"
  16. #include "commit-reach.h"
  17. void set_alternate_shallow_file(struct repository *r, const char *path, int override)
  18. {
  19. if (r->parsed_objects->is_shallow != -1)
  20. BUG("is_repository_shallow must not be called before set_alternate_shallow_file");
  21. if (r->parsed_objects->alternate_shallow_file && !override)
  22. return;
  23. free(r->parsed_objects->alternate_shallow_file);
  24. r->parsed_objects->alternate_shallow_file = xstrdup_or_null(path);
  25. }
  26. int register_shallow(struct repository *r, const struct object_id *oid)
  27. {
  28. struct commit_graft *graft =
  29. xmalloc(sizeof(struct commit_graft));
  30. struct commit *commit = lookup_commit(the_repository, oid);
  31. oidcpy(&graft->oid, oid);
  32. graft->nr_parent = -1;
  33. if (commit && commit->object.parsed)
  34. commit->parents = NULL;
  35. return register_commit_graft(r, graft, 0);
  36. }
  37. int is_repository_shallow(struct repository *r)
  38. {
  39. /*
  40. * NEEDSWORK: This function updates
  41. * r->parsed_objects->{is_shallow,shallow_stat} as a side effect but
  42. * there is no corresponding function to clear them when the shallow
  43. * file is updated.
  44. */
  45. FILE *fp;
  46. char buf[1024];
  47. const char *path = r->parsed_objects->alternate_shallow_file;
  48. if (r->parsed_objects->is_shallow >= 0)
  49. return r->parsed_objects->is_shallow;
  50. if (!path)
  51. path = git_path_shallow(r);
  52. /*
  53. * fetch-pack sets '--shallow-file ""' as an indicator that no
  54. * shallow file should be used. We could just open it and it
  55. * will likely fail. But let's do an explicit check instead.
  56. */
  57. if (!*path || (fp = fopen(path, "r")) == NULL) {
  58. stat_validity_clear(r->parsed_objects->shallow_stat);
  59. r->parsed_objects->is_shallow = 0;
  60. return r->parsed_objects->is_shallow;
  61. }
  62. stat_validity_update(r->parsed_objects->shallow_stat, fileno(fp));
  63. r->parsed_objects->is_shallow = 1;
  64. while (fgets(buf, sizeof(buf), fp)) {
  65. struct object_id oid;
  66. if (get_oid_hex(buf, &oid))
  67. die("bad shallow line: %s", buf);
  68. register_shallow(r, &oid);
  69. }
  70. fclose(fp);
  71. return r->parsed_objects->is_shallow;
  72. }
  73. /*
  74. * TODO: use "int" elemtype instead of "int *" when/if commit-slab
  75. * supports a "valid" flag.
  76. */
  77. define_commit_slab(commit_depth, int *);
  78. struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
  79. int shallow_flag, int not_shallow_flag)
  80. {
  81. int i = 0, cur_depth = 0;
  82. struct commit_list *result = NULL;
  83. struct object_array stack = OBJECT_ARRAY_INIT;
  84. struct commit *commit = NULL;
  85. struct commit_graft *graft;
  86. struct commit_depth depths;
  87. init_commit_depth(&depths);
  88. while (commit || i < heads->nr || stack.nr) {
  89. struct commit_list *p;
  90. if (!commit) {
  91. if (i < heads->nr) {
  92. int **depth_slot;
  93. commit = (struct commit *)
  94. deref_tag(the_repository,
  95. heads->objects[i++].item,
  96. NULL, 0);
  97. if (!commit || commit->object.type != OBJ_COMMIT) {
  98. commit = NULL;
  99. continue;
  100. }
  101. depth_slot = commit_depth_at(&depths, commit);
  102. if (!*depth_slot)
  103. *depth_slot = xmalloc(sizeof(int));
  104. **depth_slot = 0;
  105. cur_depth = 0;
  106. } else {
  107. commit = (struct commit *)
  108. object_array_pop(&stack);
  109. cur_depth = **commit_depth_at(&depths, commit);
  110. }
  111. }
  112. parse_commit_or_die(commit);
  113. cur_depth++;
  114. if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
  115. (is_repository_shallow(the_repository) && !commit->parents &&
  116. (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL &&
  117. graft->nr_parent < 0)) {
  118. commit_list_insert(commit, &result);
  119. commit->object.flags |= shallow_flag;
  120. commit = NULL;
  121. continue;
  122. }
  123. commit->object.flags |= not_shallow_flag;
  124. for (p = commit->parents, commit = NULL; p; p = p->next) {
  125. int **depth_slot = commit_depth_at(&depths, p->item);
  126. if (!*depth_slot) {
  127. *depth_slot = xmalloc(sizeof(int));
  128. **depth_slot = cur_depth;
  129. } else {
  130. if (cur_depth >= **depth_slot)
  131. continue;
  132. **depth_slot = cur_depth;
  133. }
  134. if (p->next)
  135. add_object_array(&p->item->object,
  136. NULL, &stack);
  137. else {
  138. commit = p->item;
  139. cur_depth = **commit_depth_at(&depths, commit);
  140. }
  141. }
  142. }
  143. for (i = 0; i < depths.slab_count; i++) {
  144. int j;
  145. if (!depths.slab[i])
  146. continue;
  147. for (j = 0; j < depths.slab_size; j++)
  148. free(depths.slab[i][j]);
  149. }
  150. clear_commit_depth(&depths);
  151. return result;
  152. }
  153. static void show_commit(struct commit *commit, void *data)
  154. {
  155. commit_list_insert(commit, data);
  156. }
  157. /*
  158. * Given rev-list arguments, run rev-list. All reachable commits
  159. * except border ones are marked with not_shallow_flag. Border commits
  160. * are marked with shallow_flag. The list of border/shallow commits
  161. * are also returned.
  162. */
  163. struct commit_list *get_shallow_commits_by_rev_list(int ac, const char **av,
  164. int shallow_flag,
  165. int not_shallow_flag)
  166. {
  167. struct commit_list *result = NULL, *p;
  168. struct commit_list *not_shallow_list = NULL;
  169. struct rev_info revs;
  170. int both_flags = shallow_flag | not_shallow_flag;
  171. /*
  172. * SHALLOW (excluded) and NOT_SHALLOW (included) should not be
  173. * set at this point. But better be safe than sorry.
  174. */
  175. clear_object_flags(both_flags);
  176. is_repository_shallow(the_repository); /* make sure shallows are read */
  177. repo_init_revisions(the_repository, &revs, NULL);
  178. save_commit_buffer = 0;
  179. setup_revisions(ac, av, &revs, NULL);
  180. if (prepare_revision_walk(&revs))
  181. die("revision walk setup failed");
  182. traverse_commit_list(&revs, show_commit, NULL, &not_shallow_list);
  183. if (!not_shallow_list)
  184. die("no commits selected for shallow requests");
  185. /* Mark all reachable commits as NOT_SHALLOW */
  186. for (p = not_shallow_list; p; p = p->next)
  187. p->item->object.flags |= not_shallow_flag;
  188. /*
  189. * mark border commits SHALLOW + NOT_SHALLOW.
  190. * We cannot clear NOT_SHALLOW right now. Imagine border
  191. * commit A is processed first, then commit B, whose parent is
  192. * A, later. If NOT_SHALLOW on A is cleared at step 1, B
  193. * itself is considered border at step 2, which is incorrect.
  194. */
  195. for (p = not_shallow_list; p; p = p->next) {
  196. struct commit *c = p->item;
  197. struct commit_list *parent;
  198. if (parse_commit(c))
  199. die("unable to parse commit %s",
  200. oid_to_hex(&c->object.oid));
  201. for (parent = c->parents; parent; parent = parent->next)
  202. if (!(parent->item->object.flags & not_shallow_flag)) {
  203. c->object.flags |= shallow_flag;
  204. commit_list_insert(c, &result);
  205. break;
  206. }
  207. }
  208. free_commit_list(not_shallow_list);
  209. /*
  210. * Now we can clean up NOT_SHALLOW on border commits. Having
  211. * both flags set can confuse the caller.
  212. */
  213. for (p = result; p; p = p->next) {
  214. struct object *o = &p->item->object;
  215. if ((o->flags & both_flags) == both_flags)
  216. o->flags &= ~not_shallow_flag;
  217. }
  218. return result;
  219. }
  220. static void check_shallow_file_for_update(struct repository *r)
  221. {
  222. if (r->parsed_objects->is_shallow == -1)
  223. BUG("shallow must be initialized by now");
  224. if (!stat_validity_check(r->parsed_objects->shallow_stat,
  225. git_path_shallow(r)))
  226. die("shallow file has changed since we read it");
  227. }
  228. #define SEEN_ONLY 1
  229. #define VERBOSE 2
  230. #define QUICK 4
  231. struct write_shallow_data {
  232. struct strbuf *out;
  233. int use_pack_protocol;
  234. int count;
  235. unsigned flags;
  236. };
  237. static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
  238. {
  239. struct write_shallow_data *data = cb_data;
  240. const char *hex = oid_to_hex(&graft->oid);
  241. if (graft->nr_parent != -1)
  242. return 0;
  243. if (data->flags & QUICK) {
  244. if (!has_object_file(&graft->oid))
  245. return 0;
  246. } else if (data->flags & SEEN_ONLY) {
  247. struct commit *c = lookup_commit(the_repository, &graft->oid);
  248. if (!c || !(c->object.flags & SEEN)) {
  249. if (data->flags & VERBOSE)
  250. printf("Removing %s from .git/shallow\n",
  251. oid_to_hex(&c->object.oid));
  252. return 0;
  253. }
  254. }
  255. data->count++;
  256. if (data->use_pack_protocol)
  257. packet_buf_write(data->out, "shallow %s", hex);
  258. else {
  259. strbuf_addstr(data->out, hex);
  260. strbuf_addch(data->out, '\n');
  261. }
  262. return 0;
  263. }
  264. static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
  265. const struct oid_array *extra,
  266. unsigned flags)
  267. {
  268. struct write_shallow_data data;
  269. int i;
  270. data.out = out;
  271. data.use_pack_protocol = use_pack_protocol;
  272. data.count = 0;
  273. data.flags = flags;
  274. for_each_commit_graft(write_one_shallow, &data);
  275. if (!extra)
  276. return data.count;
  277. for (i = 0; i < extra->nr; i++) {
  278. strbuf_addstr(out, oid_to_hex(extra->oid + i));
  279. strbuf_addch(out, '\n');
  280. data.count++;
  281. }
  282. return data.count;
  283. }
  284. int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
  285. const struct oid_array *extra)
  286. {
  287. return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
  288. }
  289. const char *setup_temporary_shallow(const struct oid_array *extra)
  290. {
  291. struct tempfile *temp;
  292. struct strbuf sb = STRBUF_INIT;
  293. if (write_shallow_commits(&sb, 0, extra)) {
  294. temp = xmks_tempfile(git_path("shallow_XXXXXX"));
  295. if (write_in_full(temp->fd, sb.buf, sb.len) < 0 ||
  296. close_tempfile_gently(temp) < 0)
  297. die_errno("failed to write to %s",
  298. get_tempfile_path(temp));
  299. strbuf_release(&sb);
  300. return get_tempfile_path(temp);
  301. }
  302. /*
  303. * is_repository_shallow() sees empty string as "no shallow
  304. * file".
  305. */
  306. return "";
  307. }
  308. void setup_alternate_shallow(struct lock_file *shallow_lock,
  309. const char **alternate_shallow_file,
  310. const struct oid_array *extra)
  311. {
  312. struct strbuf sb = STRBUF_INIT;
  313. int fd;
  314. fd = hold_lock_file_for_update(shallow_lock,
  315. git_path_shallow(the_repository),
  316. LOCK_DIE_ON_ERROR);
  317. check_shallow_file_for_update(the_repository);
  318. if (write_shallow_commits(&sb, 0, extra)) {
  319. if (write_in_full(fd, sb.buf, sb.len) < 0)
  320. die_errno("failed to write to %s",
  321. get_lock_file_path(shallow_lock));
  322. *alternate_shallow_file = get_lock_file_path(shallow_lock);
  323. } else
  324. /*
  325. * is_repository_shallow() sees empty string as "no
  326. * shallow file".
  327. */
  328. *alternate_shallow_file = "";
  329. strbuf_release(&sb);
  330. }
  331. static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
  332. {
  333. int fd = *(int *)cb;
  334. if (graft->nr_parent == -1)
  335. packet_write_fmt(fd, "shallow %s\n", oid_to_hex(&graft->oid));
  336. return 0;
  337. }
  338. void advertise_shallow_grafts(int fd)
  339. {
  340. if (!is_repository_shallow(the_repository))
  341. return;
  342. for_each_commit_graft(advertise_shallow_grafts_cb, &fd);
  343. }
  344. /*
  345. * mark_reachable_objects() should have been run prior to this and all
  346. * reachable commits marked as "SEEN", except when quick_prune is non-zero,
  347. * in which case lines are excised from the shallow file if they refer to
  348. * commits that do not exist (any longer).
  349. */
  350. void prune_shallow(unsigned options)
  351. {
  352. struct lock_file shallow_lock = LOCK_INIT;
  353. struct strbuf sb = STRBUF_INIT;
  354. unsigned flags = SEEN_ONLY;
  355. int fd;
  356. if (options & PRUNE_QUICK)
  357. flags |= QUICK;
  358. if (options & PRUNE_SHOW_ONLY) {
  359. flags |= VERBOSE;
  360. write_shallow_commits_1(&sb, 0, NULL, flags);
  361. strbuf_release(&sb);
  362. return;
  363. }
  364. fd = hold_lock_file_for_update(&shallow_lock,
  365. git_path_shallow(the_repository),
  366. LOCK_DIE_ON_ERROR);
  367. check_shallow_file_for_update(the_repository);
  368. if (write_shallow_commits_1(&sb, 0, NULL, flags)) {
  369. if (write_in_full(fd, sb.buf, sb.len) < 0)
  370. die_errno("failed to write to %s",
  371. get_lock_file_path(&shallow_lock));
  372. commit_lock_file(&shallow_lock);
  373. } else {
  374. unlink(git_path_shallow(the_repository));
  375. rollback_lock_file(&shallow_lock);
  376. }
  377. strbuf_release(&sb);
  378. }
  379. struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
  380. /*
  381. * Step 1, split sender shallow commits into "ours" and "theirs"
  382. * Step 2, clean "ours" based on .git/shallow
  383. */
  384. void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
  385. {
  386. int i;
  387. trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
  388. memset(info, 0, sizeof(*info));
  389. info->shallow = sa;
  390. if (!sa)
  391. return;
  392. ALLOC_ARRAY(info->ours, sa->nr);
  393. ALLOC_ARRAY(info->theirs, sa->nr);
  394. for (i = 0; i < sa->nr; i++) {
  395. if (has_object_file(sa->oid + i)) {
  396. struct commit_graft *graft;
  397. graft = lookup_commit_graft(the_repository,
  398. &sa->oid[i]);
  399. if (graft && graft->nr_parent < 0)
  400. continue;
  401. info->ours[info->nr_ours++] = i;
  402. } else
  403. info->theirs[info->nr_theirs++] = i;
  404. }
  405. }
  406. void clear_shallow_info(struct shallow_info *info)
  407. {
  408. free(info->ours);
  409. free(info->theirs);
  410. }
  411. /* Step 4, remove non-existent ones in "theirs" after getting the pack */
  412. void remove_nonexistent_theirs_shallow(struct shallow_info *info)
  413. {
  414. struct object_id *oid = info->shallow->oid;
  415. int i, dst;
  416. trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
  417. for (i = dst = 0; i < info->nr_theirs; i++) {
  418. if (i != dst)
  419. info->theirs[dst] = info->theirs[i];
  420. if (has_object_file(oid + info->theirs[i]))
  421. dst++;
  422. }
  423. info->nr_theirs = dst;
  424. }
  425. define_commit_slab(ref_bitmap, uint32_t *);
  426. #define POOL_SIZE (512 * 1024)
  427. struct paint_info {
  428. struct ref_bitmap ref_bitmap;
  429. unsigned nr_bits;
  430. char **pools;
  431. char *free, *end;
  432. unsigned pool_count;
  433. };
  434. static uint32_t *paint_alloc(struct paint_info *info)
  435. {
  436. unsigned nr = DIV_ROUND_UP(info->nr_bits, 32);
  437. unsigned size = nr * sizeof(uint32_t);
  438. void *p;
  439. if (!info->pool_count || size > info->end - info->free) {
  440. if (size > POOL_SIZE)
  441. BUG("pool size too small for %d in paint_alloc()",
  442. size);
  443. info->pool_count++;
  444. REALLOC_ARRAY(info->pools, info->pool_count);
  445. info->free = xmalloc(POOL_SIZE);
  446. info->pools[info->pool_count - 1] = info->free;
  447. info->end = info->free + POOL_SIZE;
  448. }
  449. p = info->free;
  450. info->free += size;
  451. return p;
  452. }
  453. /*
  454. * Given a commit SHA-1, walk down to parents until either SEEN,
  455. * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
  456. * all walked commits.
  457. */
  458. static void paint_down(struct paint_info *info, const struct object_id *oid,
  459. unsigned int id)
  460. {
  461. unsigned int i, nr;
  462. struct commit_list *head = NULL;
  463. int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
  464. size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
  465. struct commit *c = lookup_commit_reference_gently(the_repository, oid,
  466. 1);
  467. uint32_t *tmp; /* to be freed before return */
  468. uint32_t *bitmap;
  469. if (!c)
  470. return;
  471. tmp = xmalloc(bitmap_size);
  472. bitmap = paint_alloc(info);
  473. memset(bitmap, 0, bitmap_size);
  474. bitmap[id / 32] |= (1U << (id % 32));
  475. commit_list_insert(c, &head);
  476. while (head) {
  477. struct commit_list *p;
  478. struct commit *c = pop_commit(&head);
  479. uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
  480. /* XXX check "UNINTERESTING" from pack bitmaps if available */
  481. if (c->object.flags & (SEEN | UNINTERESTING))
  482. continue;
  483. else
  484. c->object.flags |= SEEN;
  485. if (*refs == NULL)
  486. *refs = bitmap;
  487. else {
  488. memcpy(tmp, *refs, bitmap_size);
  489. for (i = 0; i < bitmap_nr; i++)
  490. tmp[i] |= bitmap[i];
  491. if (memcmp(tmp, *refs, bitmap_size)) {
  492. *refs = paint_alloc(info);
  493. memcpy(*refs, tmp, bitmap_size);
  494. }
  495. }
  496. if (c->object.flags & BOTTOM)
  497. continue;
  498. if (parse_commit(c))
  499. die("unable to parse commit %s",
  500. oid_to_hex(&c->object.oid));
  501. for (p = c->parents; p; p = p->next) {
  502. if (p->item->object.flags & SEEN)
  503. continue;
  504. commit_list_insert(p->item, &head);
  505. }
  506. }
  507. nr = get_max_object_index();
  508. for (i = 0; i < nr; i++) {
  509. struct object *o = get_indexed_object(i);
  510. if (o && o->type == OBJ_COMMIT)
  511. o->flags &= ~SEEN;
  512. }
  513. free(tmp);
  514. }
  515. static int mark_uninteresting(const char *refname, const struct object_id *oid,
  516. int flags, void *cb_data)
  517. {
  518. struct commit *commit = lookup_commit_reference_gently(the_repository,
  519. oid, 1);
  520. if (!commit)
  521. return 0;
  522. commit->object.flags |= UNINTERESTING;
  523. mark_parents_uninteresting(commit);
  524. return 0;
  525. }
  526. static void post_assign_shallow(struct shallow_info *info,
  527. struct ref_bitmap *ref_bitmap,
  528. int *ref_status);
  529. /*
  530. * Step 6(+7), associate shallow commits with new refs
  531. *
  532. * info->ref must be initialized before calling this function.
  533. *
  534. * If used is not NULL, it's an array of info->shallow->nr
  535. * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
  536. * m-th shallow commit from info->shallow.
  537. *
  538. * If used is NULL, "ours" and "theirs" are updated. And if ref_status
  539. * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
  540. * the ref needs some shallow commits from either info->ours or
  541. * info->theirs.
  542. */
  543. void assign_shallow_commits_to_refs(struct shallow_info *info,
  544. uint32_t **used, int *ref_status)
  545. {
  546. struct object_id *oid = info->shallow->oid;
  547. struct oid_array *ref = info->ref;
  548. unsigned int i, nr;
  549. int *shallow, nr_shallow = 0;
  550. struct paint_info pi;
  551. trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
  552. ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs);
  553. for (i = 0; i < info->nr_ours; i++)
  554. shallow[nr_shallow++] = info->ours[i];
  555. for (i = 0; i < info->nr_theirs; i++)
  556. shallow[nr_shallow++] = info->theirs[i];
  557. /*
  558. * Prepare the commit graph to track what refs can reach what
  559. * (new) shallow commits.
  560. */
  561. nr = get_max_object_index();
  562. for (i = 0; i < nr; i++) {
  563. struct object *o = get_indexed_object(i);
  564. if (!o || o->type != OBJ_COMMIT)
  565. continue;
  566. o->flags &= ~(UNINTERESTING | BOTTOM | SEEN);
  567. }
  568. memset(&pi, 0, sizeof(pi));
  569. init_ref_bitmap(&pi.ref_bitmap);
  570. pi.nr_bits = ref->nr;
  571. /*
  572. * "--not --all" to cut short the traversal if new refs
  573. * connect to old refs. If not (e.g. force ref updates) it'll
  574. * have to go down to the current shallow commits.
  575. */
  576. head_ref(mark_uninteresting, NULL);
  577. for_each_ref(mark_uninteresting, NULL);
  578. /* Mark potential bottoms so we won't go out of bound */
  579. for (i = 0; i < nr_shallow; i++) {
  580. struct commit *c = lookup_commit(the_repository,
  581. &oid[shallow[i]]);
  582. c->object.flags |= BOTTOM;
  583. }
  584. for (i = 0; i < ref->nr; i++)
  585. paint_down(&pi, ref->oid + i, i);
  586. if (used) {
  587. int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t);
  588. memset(used, 0, sizeof(*used) * info->shallow->nr);
  589. for (i = 0; i < nr_shallow; i++) {
  590. const struct commit *c = lookup_commit(the_repository,
  591. &oid[shallow[i]]);
  592. uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
  593. if (*map)
  594. used[shallow[i]] = xmemdupz(*map, bitmap_size);
  595. }
  596. /*
  597. * unreachable shallow commits are not removed from
  598. * "ours" and "theirs". The user is supposed to run
  599. * step 7 on every ref separately and not trust "ours"
  600. * and "theirs" any more.
  601. */
  602. } else
  603. post_assign_shallow(info, &pi.ref_bitmap, ref_status);
  604. clear_ref_bitmap(&pi.ref_bitmap);
  605. for (i = 0; i < pi.pool_count; i++)
  606. free(pi.pools[i]);
  607. free(pi.pools);
  608. free(shallow);
  609. }
  610. struct commit_array {
  611. struct commit **commits;
  612. int nr, alloc;
  613. };
  614. static int add_ref(const char *refname, const struct object_id *oid,
  615. int flags, void *cb_data)
  616. {
  617. struct commit_array *ca = cb_data;
  618. ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
  619. ca->commits[ca->nr] = lookup_commit_reference_gently(the_repository,
  620. oid, 1);
  621. if (ca->commits[ca->nr])
  622. ca->nr++;
  623. return 0;
  624. }
  625. static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
  626. {
  627. unsigned int i;
  628. if (!ref_status)
  629. return;
  630. for (i = 0; i < nr; i++)
  631. if (bitmap[i / 32] & (1U << (i % 32)))
  632. ref_status[i]++;
  633. }
  634. /*
  635. * Step 7, reachability test on "ours" at commit level
  636. */
  637. static void post_assign_shallow(struct shallow_info *info,
  638. struct ref_bitmap *ref_bitmap,
  639. int *ref_status)
  640. {
  641. struct object_id *oid = info->shallow->oid;
  642. struct commit *c;
  643. uint32_t **bitmap;
  644. int dst, i, j;
  645. int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
  646. struct commit_array ca;
  647. trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
  648. if (ref_status)
  649. memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr);
  650. /* Remove unreachable shallow commits from "theirs" */
  651. for (i = dst = 0; i < info->nr_theirs; i++) {
  652. if (i != dst)
  653. info->theirs[dst] = info->theirs[i];
  654. c = lookup_commit(the_repository, &oid[info->theirs[i]]);
  655. bitmap = ref_bitmap_at(ref_bitmap, c);
  656. if (!*bitmap)
  657. continue;
  658. for (j = 0; j < bitmap_nr; j++)
  659. if (bitmap[0][j]) {
  660. update_refstatus(ref_status, info->ref->nr, *bitmap);
  661. dst++;
  662. break;
  663. }
  664. }
  665. info->nr_theirs = dst;
  666. memset(&ca, 0, sizeof(ca));
  667. head_ref(add_ref, &ca);
  668. for_each_ref(add_ref, &ca);
  669. /* Remove unreachable shallow commits from "ours" */
  670. for (i = dst = 0; i < info->nr_ours; i++) {
  671. if (i != dst)
  672. info->ours[dst] = info->ours[i];
  673. c = lookup_commit(the_repository, &oid[info->ours[i]]);
  674. bitmap = ref_bitmap_at(ref_bitmap, c);
  675. if (!*bitmap)
  676. continue;
  677. for (j = 0; j < bitmap_nr; j++)
  678. if (bitmap[0][j] &&
  679. /* Step 7, reachability test at commit level */
  680. !in_merge_bases_many(c, ca.nr, ca.commits)) {
  681. update_refstatus(ref_status, info->ref->nr, *bitmap);
  682. dst++;
  683. break;
  684. }
  685. }
  686. info->nr_ours = dst;
  687. free(ca.commits);
  688. }
  689. /* (Delayed) step 7, reachability test at commit level */
  690. int delayed_reachability_test(struct shallow_info *si, int c)
  691. {
  692. if (si->need_reachability_test[c]) {
  693. struct commit *commit = lookup_commit(the_repository,
  694. &si->shallow->oid[c]);
  695. if (!si->commits) {
  696. struct commit_array ca;
  697. memset(&ca, 0, sizeof(ca));
  698. head_ref(add_ref, &ca);
  699. for_each_ref(add_ref, &ca);
  700. si->commits = ca.commits;
  701. si->nr_commits = ca.nr;
  702. }
  703. si->reachable[c] = in_merge_bases_many(commit,
  704. si->nr_commits,
  705. si->commits);
  706. si->need_reachability_test[c] = 0;
  707. }
  708. return si->reachable[c];
  709. }