shallow.c 19.5 KB
Newer Older
1
#include "cache.h"
2
#include "tempfile.h"
3
#include "lockfile.h"
4
#include "commit.h"
5
#include "tag.h"
6
#include "pkt-line.h"
7 8 9 10 11 12
#include "remote.h"
#include "refs.h"
#include "sha1-array.h"
#include "diff.h"
#include "revision.h"
#include "commit-slab.h"
13 14
#include "revision.h"
#include "list-objects.h"
15 16

static int is_shallow = -1;
17
static struct stat_validity shallow_stat;
18 19
static char *alternate_shallow_file;

20
void set_alternate_shallow_file(const char *path, int override)
21 22 23
{
	if (is_shallow != -1)
		die("BUG: is_repository_shallow must not be called before set_alternate_shallow_file");
24 25
	if (alternate_shallow_file && !override)
		return;
26
	free(alternate_shallow_file);
27
	alternate_shallow_file = xstrdup_or_null(path);
28
}
29 30 31 32 33 34 35

int register_shallow(const unsigned char *sha1)
{
	struct commit_graft *graft =
		xmalloc(sizeof(struct commit_graft));
	struct commit *commit = lookup_commit(sha1);

36
	hashcpy(graft->oid.hash, sha1);
37 38 39 40 41 42
	graft->nr_parent = -1;
	if (commit && commit->object.parsed)
		commit->parents = NULL;
	return register_commit_graft(graft, 0);
}

43
int is_repository_shallow(void)
44 45 46
{
	FILE *fp;
	char buf[1024];
47
	const char *path = alternate_shallow_file;
48 49 50 51

	if (is_shallow >= 0)
		return is_shallow;

52
	if (!path)
53
		path = git_path_shallow();
54 55 56 57 58
	/*
	 * fetch-pack sets '--shallow-file ""' as an indicator that no
	 * shallow file should be used. We could just open it and it
	 * will likely fail. But let's do an explicit check instead.
	 */
59 60
	if (!*path || (fp = fopen(path, "r")) == NULL) {
		stat_validity_clear(&shallow_stat);
61 62 63
		is_shallow = 0;
		return is_shallow;
	}
64
	stat_validity_update(&shallow_stat, fileno(fp));
65 66 67 68 69 70 71 72 73 74 75 76
	is_shallow = 1;

	while (fgets(buf, sizeof(buf), fp)) {
		unsigned char sha1[20];
		if (get_sha1_hex(buf, sha1))
			die("bad shallow line: %s", buf);
		register_shallow(sha1);
	}
	fclose(fp);
	return is_shallow;
}

77 78
struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
		int shallow_flag, int not_shallow_flag)
79 80 81
{
	int i = 0, cur_depth = 0;
	struct commit_list *result = NULL;
82
	struct object_array stack = OBJECT_ARRAY_INIT;
83
	struct commit *commit = NULL;
84
	struct commit_graft *graft;
85 86 87 88 89 90

	while (commit || i < heads->nr || stack.nr) {
		struct commit_list *p;
		if (!commit) {
			if (i < heads->nr) {
				commit = (struct commit *)
91
					deref_tag(heads->objects[i++].item, NULL, 0);
92
				if (!commit || commit->object.type != OBJ_COMMIT) {
93 94 95
					commit = NULL;
					continue;
				}
96 97 98
				if (!commit->util)
					commit->util = xmalloc(sizeof(int));
				*(int *)commit->util = 0;
99 100 101 102 103 104 105
				cur_depth = 0;
			} else {
				commit = (struct commit *)
					stack.objects[--stack.nr].item;
				cur_depth = *(int *)commit->util;
			}
		}
106
		parse_commit_or_die(commit);
107
		cur_depth++;
108 109
		if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
		    (is_repository_shallow() && !commit->parents &&
B
brian m. carlson 已提交
110
		     (graft = lookup_commit_graft(commit->object.oid.hash)) != NULL &&
111
		     graft->nr_parent < 0)) {
112 113 114 115 116 117
			commit_list_insert(commit, &result);
			commit->object.flags |= shallow_flag;
			commit = NULL;
			continue;
		}
		commit->object.flags |= not_shallow_flag;
118 119 120 121 122 123 124 125 126 127 128
		for (p = commit->parents, commit = NULL; p; p = p->next) {
			if (!p->item->util) {
				int *pointer = xmalloc(sizeof(int));
				p->item->util = pointer;
				*pointer =  cur_depth;
			} else {
				int *pointer = p->item->util;
				if (cur_depth >= *pointer)
					continue;
				*pointer = cur_depth;
			}
129 130 131 132 133 134
			if (p->next)
				add_object_array(&p->item->object,
						NULL, &stack);
			else {
				commit = p->item;
				cur_depth = *(int *)commit->util;
135
			}
136 137 138 139 140
		}
	}

	return result;
}
141

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
static void show_commit(struct commit *commit, void *data)
{
	commit_list_insert(commit, data);
}

/*
 * Given rev-list arguments, run rev-list. All reachable commits
 * except border ones are marked with not_shallow_flag. Border commits
 * are marked with shallow_flag. The list of border/shallow commits
 * are also returned.
 */
struct commit_list *get_shallow_commits_by_rev_list(int ac, const char **av,
						    int shallow_flag,
						    int not_shallow_flag)
{
	struct commit_list *result = NULL, *p;
	struct commit_list *not_shallow_list = NULL;
	struct rev_info revs;
	int both_flags = shallow_flag | not_shallow_flag;

	/*
	 * SHALLOW (excluded) and NOT_SHALLOW (included) should not be
	 * set at this point. But better be safe than sorry.
	 */
	clear_object_flags(both_flags);

	is_repository_shallow(); /* make sure shallows are read */

	init_revisions(&revs, NULL);
	save_commit_buffer = 0;
	setup_revisions(ac, av, &revs, NULL);

	if (prepare_revision_walk(&revs))
		die("revision walk setup failed");
	traverse_commit_list(&revs, show_commit, NULL, &not_shallow_list);

	/* Mark all reachable commits as NOT_SHALLOW */
	for (p = not_shallow_list; p; p = p->next)
		p->item->object.flags |= not_shallow_flag;

	/*
	 * mark border commits SHALLOW + NOT_SHALLOW.
	 * We cannot clear NOT_SHALLOW right now. Imagine border
	 * commit A is processed first, then commit B, whose parent is
	 * A, later. If NOT_SHALLOW on A is cleared at step 1, B
	 * itself is considered border at step 2, which is incorrect.
	 */
	for (p = not_shallow_list; p; p = p->next) {
		struct commit *c = p->item;
		struct commit_list *parent;

		if (parse_commit(c))
			die("unable to parse commit %s",
			    oid_to_hex(&c->object.oid));

		for (parent = c->parents; parent; parent = parent->next)
			if (!(parent->item->object.flags & not_shallow_flag)) {
				c->object.flags |= shallow_flag;
				commit_list_insert(c, &result);
				break;
			}
	}
	free_commit_list(not_shallow_list);

	/*
	 * Now we can clean up NOT_SHALLOW on border commits. Having
	 * both flags set can confuse the caller.
	 */
	for (p = result; p; p = p->next) {
		struct object *o = &p->item->object;
		if ((o->flags & both_flags) == both_flags)
			o->flags &= ~not_shallow_flag;
	}
	return result;
}

218
static void check_shallow_file_for_update(void)
219
{
220
	if (is_shallow == -1)
221 222
		die("BUG: shallow must be initialized by now");

223
	if (!stat_validity_check(&shallow_stat, git_path_shallow()))
224
		die("shallow file has changed since we read it");
225
}
226

227 228 229
#define SEEN_ONLY 1
#define VERBOSE   2

230 231 232 233
struct write_shallow_data {
	struct strbuf *out;
	int use_pack_protocol;
	int count;
234
	unsigned flags;
235 236 237 238 239
};

static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
{
	struct write_shallow_data *data = cb_data;
240
	const char *hex = oid_to_hex(&graft->oid);
241 242
	if (graft->nr_parent != -1)
		return 0;
243
	if (data->flags & SEEN_ONLY) {
244
		struct commit *c = lookup_commit(graft->oid.hash);
245 246 247
		if (!c || !(c->object.flags & SEEN)) {
			if (data->flags & VERBOSE)
				printf("Removing %s from .git/shallow\n",
248
				       oid_to_hex(&c->object.oid));
249 250 251
			return 0;
		}
	}
252 253 254 255 256 257 258 259 260 261
	data->count++;
	if (data->use_pack_protocol)
		packet_buf_write(data->out, "shallow %s", hex);
	else {
		strbuf_addstr(data->out, hex);
		strbuf_addch(data->out, '\n');
	}
	return 0;
}

262
static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
263
				   const struct oid_array *extra,
264
				   unsigned flags)
265 266
{
	struct write_shallow_data data;
267
	int i;
268 269 270
	data.out = out;
	data.use_pack_protocol = use_pack_protocol;
	data.count = 0;
271
	data.flags = flags;
272
	for_each_commit_graft(write_one_shallow, &data);
273 274 275
	if (!extra)
		return data.count;
	for (i = 0; i < extra->nr; i++) {
276
		strbuf_addstr(out, oid_to_hex(extra->oid + i));
277 278 279
		strbuf_addch(out, '\n');
		data.count++;
	}
280 281 282
	return data.count;
}

283
int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
284
			  const struct oid_array *extra)
285 286 287 288
{
	return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
}

289
static struct tempfile temporary_shallow;
290

291
const char *setup_temporary_shallow(const struct oid_array *extra)
292 293 294 295
{
	struct strbuf sb = STRBUF_INIT;
	int fd;

296
	if (write_shallow_commits(&sb, 0, extra)) {
297
		fd = xmks_tempfile(&temporary_shallow, git_path("shallow_XXXXXX"));
298

299 300
		if (write_in_full(fd, sb.buf, sb.len) != sb.len)
			die_errno("failed to write to %s",
301 302
				  get_tempfile_path(&temporary_shallow));
		close_tempfile(&temporary_shallow);
303
		strbuf_release(&sb);
304
		return get_tempfile_path(&temporary_shallow);
305 306 307 308 309
	}
	/*
	 * is_repository_shallow() sees empty string as "no shallow
	 * file".
	 */
310
	return get_tempfile_path(&temporary_shallow);
311 312
}

313
void setup_alternate_shallow(struct lock_file *shallow_lock,
314
			     const char **alternate_shallow_file,
315
			     const struct oid_array *extra)
316 317 318 319
{
	struct strbuf sb = STRBUF_INIT;
	int fd;

320
	fd = hold_lock_file_for_update(shallow_lock, git_path_shallow(),
321
				       LOCK_DIE_ON_ERROR);
322
	check_shallow_file_for_update();
323
	if (write_shallow_commits(&sb, 0, extra)) {
324 325
		if (write_in_full(fd, sb.buf, sb.len) != sb.len)
			die_errno("failed to write to %s",
326 327
				  get_lock_file_path(shallow_lock));
		*alternate_shallow_file = get_lock_file_path(shallow_lock);
328 329 330 331 332 333 334 335
	} else
		/*
		 * is_repository_shallow() sees empty string as "no
		 * shallow file".
		 */
		*alternate_shallow_file = "";
	strbuf_release(&sb);
}
336 337 338 339 340

static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
{
	int fd = *(int *)cb;
	if (graft->nr_parent == -1)
341
		packet_write_fmt(fd, "shallow %s\n", oid_to_hex(&graft->oid));
342 343 344 345 346 347 348 349 350
	return 0;
}

void advertise_shallow_grafts(int fd)
{
	if (!is_repository_shallow())
		return;
	for_each_commit_graft(advertise_shallow_grafts_cb, &fd);
}
351

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
/*
 * mark_reachable_objects() should have been run prior to this and all
 * reachable commits marked as "SEEN".
 */
void prune_shallow(int show_only)
{
	static struct lock_file shallow_lock;
	struct strbuf sb = STRBUF_INIT;
	int fd;

	if (show_only) {
		write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY | VERBOSE);
		strbuf_release(&sb);
		return;
	}
367
	fd = hold_lock_file_for_update(&shallow_lock, git_path_shallow(),
368
				       LOCK_DIE_ON_ERROR);
369
	check_shallow_file_for_update();
370 371 372
	if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {
		if (write_in_full(fd, sb.buf, sb.len) != sb.len)
			die_errno("failed to write to %s",
373
				  get_lock_file_path(&shallow_lock));
374 375
		commit_lock_file(&shallow_lock);
	} else {
376
		unlink(git_path_shallow());
377 378 379 380 381
		rollback_lock_file(&shallow_lock);
	}
	strbuf_release(&sb);
}

K
Karsten Blees 已提交
382
struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
383 384 385 386 387

/*
 * Step 1, split sender shallow commits into "ours" and "theirs"
 * Step 2, clean "ours" based on .git/shallow
 */
388
void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
389 390
{
	int i;
K
Karsten Blees 已提交
391
	trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
392 393 394 395
	memset(info, 0, sizeof(*info));
	info->shallow = sa;
	if (!sa)
		return;
J
Jeff King 已提交
396 397
	ALLOC_ARRAY(info->ours, sa->nr);
	ALLOC_ARRAY(info->theirs, sa->nr);
398
	for (i = 0; i < sa->nr; i++) {
399
		if (has_object_file(sa->oid + i)) {
400
			struct commit_graft *graft;
401
			graft = lookup_commit_graft(sa->oid[i].hash);
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
			if (graft && graft->nr_parent < 0)
				continue;
			info->ours[info->nr_ours++] = i;
		} else
			info->theirs[info->nr_theirs++] = i;
	}
}

void clear_shallow_info(struct shallow_info *info)
{
	free(info->ours);
	free(info->theirs);
}

/* Step 4, remove non-existent ones in "theirs" after getting the pack */

void remove_nonexistent_theirs_shallow(struct shallow_info *info)
{
420
	struct object_id *oid = info->shallow->oid;
421
	int i, dst;
K
Karsten Blees 已提交
422
	trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
423 424 425
	for (i = dst = 0; i < info->nr_theirs; i++) {
		if (i != dst)
			info->theirs[dst] = info->theirs[i];
426
		if (has_object_file(oid + info->theirs[i]))
427 428 429 430 431
			dst++;
	}
	info->nr_theirs = dst;
}

432 433
define_commit_slab(ref_bitmap, uint32_t *);

434 435
#define POOL_SIZE (512 * 1024)

436 437 438
struct paint_info {
	struct ref_bitmap ref_bitmap;
	unsigned nr_bits;
439
	char **pools;
440
	char *free, *end;
441
	unsigned pool_count;
442 443 444 445
};

static uint32_t *paint_alloc(struct paint_info *info)
{
R
René Scharfe 已提交
446
	unsigned nr = DIV_ROUND_UP(info->nr_bits, 32);
447 448
	unsigned size = nr * sizeof(uint32_t);
	void *p;
449
	if (!info->pool_count || size > info->end - info->free) {
450 451 452
		if (size > POOL_SIZE)
			die("BUG: pool size too small for %d in paint_alloc()",
			    size);
453 454
		info->pool_count++;
		REALLOC_ARRAY(info->pools, info->pool_count);
455
		info->free = xmalloc(POOL_SIZE);
456
		info->pools[info->pool_count - 1] = info->free;
457
		info->end = info->free + POOL_SIZE;
458 459 460 461 462 463 464 465 466 467 468 469
	}
	p = info->free;
	info->free += size;
	return p;
}

/*
 * Given a commit SHA-1, walk down to parents until either SEEN,
 * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
 * all walked commits.
 */
static void paint_down(struct paint_info *info, const unsigned char *sha1,
470
		       unsigned int id)
471 472 473
{
	unsigned int i, nr;
	struct commit_list *head = NULL;
R
René Scharfe 已提交
474
	int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
475
	size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
476
	struct commit *c = lookup_commit_reference_gently(sha1, 1);
J
Johannes Schindelin 已提交
477 478 479
	uint32_t *tmp; /* to be freed before return */
	uint32_t *bitmap;

480 481
	if (!c)
		return;
J
Johannes Schindelin 已提交
482 483 484

	tmp = xmalloc(bitmap_size);
	bitmap = paint_alloc(info);
485
	memset(bitmap, 0, bitmap_size);
486
	bitmap[id / 32] |= (1U << (id % 32));
487 488 489
	commit_list_insert(c, &head);
	while (head) {
		struct commit_list *p;
490
		struct commit *c = pop_commit(&head);
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
		uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);

		/* XXX check "UNINTERESTING" from pack bitmaps if available */
		if (c->object.flags & (SEEN | UNINTERESTING))
			continue;
		else
			c->object.flags |= SEEN;

		if (*refs == NULL)
			*refs = bitmap;
		else {
			memcpy(tmp, *refs, bitmap_size);
			for (i = 0; i < bitmap_nr; i++)
				tmp[i] |= bitmap[i];
			if (memcmp(tmp, *refs, bitmap_size)) {
				*refs = paint_alloc(info);
				memcpy(*refs, tmp, bitmap_size);
			}
		}

		if (c->object.flags & BOTTOM)
			continue;

		if (parse_commit(c))
			die("unable to parse commit %s",
516
			    oid_to_hex(&c->object.oid));
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534

		for (p = c->parents; p; p = p->next) {
			if (p->item->object.flags & SEEN)
				continue;
			commit_list_insert(p->item, &head);
		}
	}

	nr = get_max_object_index();
	for (i = 0; i < nr; i++) {
		struct object *o = get_indexed_object(i);
		if (o && o->type == OBJ_COMMIT)
			o->flags &= ~SEEN;
	}

	free(tmp);
}

535
static int mark_uninteresting(const char *refname, const struct object_id *oid,
536 537
			      int flags, void *cb_data)
{
538
	struct commit *commit = lookup_commit_reference_gently(oid->hash, 1);
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
	if (!commit)
		return 0;
	commit->object.flags |= UNINTERESTING;
	mark_parents_uninteresting(commit);
	return 0;
}

static void post_assign_shallow(struct shallow_info *info,
				struct ref_bitmap *ref_bitmap,
				int *ref_status);
/*
 * Step 6(+7), associate shallow commits with new refs
 *
 * info->ref must be initialized before calling this function.
 *
 * If used is not NULL, it's an array of info->shallow->nr
 * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
 * m-th shallow commit from info->shallow.
 *
 * If used is NULL, "ours" and "theirs" are updated. And if ref_status
 * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
 * the ref needs some shallow commits from either info->ours or
 * info->theirs.
 */
void assign_shallow_commits_to_refs(struct shallow_info *info,
				    uint32_t **used, int *ref_status)
{
566
	struct object_id *oid = info->shallow->oid;
567
	struct oid_array *ref = info->ref;
568 569 570 571
	unsigned int i, nr;
	int *shallow, nr_shallow = 0;
	struct paint_info pi;

K
Karsten Blees 已提交
572
	trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
J
Jeff King 已提交
573
	ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs);
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
	for (i = 0; i < info->nr_ours; i++)
		shallow[nr_shallow++] = info->ours[i];
	for (i = 0; i < info->nr_theirs; i++)
		shallow[nr_shallow++] = info->theirs[i];

	/*
	 * Prepare the commit graph to track what refs can reach what
	 * (new) shallow commits.
	 */
	nr = get_max_object_index();
	for (i = 0; i < nr; i++) {
		struct object *o = get_indexed_object(i);
		if (!o || o->type != OBJ_COMMIT)
			continue;

		o->flags &= ~(UNINTERESTING | BOTTOM | SEEN);
	}

	memset(&pi, 0, sizeof(pi));
	init_ref_bitmap(&pi.ref_bitmap);
	pi.nr_bits = ref->nr;

	/*
	 * "--not --all" to cut short the traversal if new refs
	 * connect to old refs. If not (e.g. force ref updates) it'll
	 * have to go down to the current shallow commits.
	 */
601 602
	head_ref(mark_uninteresting, NULL);
	for_each_ref(mark_uninteresting, NULL);
603 604 605

	/* Mark potential bottoms so we won't go out of bound */
	for (i = 0; i < nr_shallow; i++) {
606
		struct commit *c = lookup_commit(oid[shallow[i]].hash);
607 608 609 610
		c->object.flags |= BOTTOM;
	}

	for (i = 0; i < ref->nr; i++)
611
		paint_down(&pi, ref->oid[i].hash, i);
612 613

	if (used) {
R
René Scharfe 已提交
614
		int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t);
615 616
		memset(used, 0, sizeof(*used) * info->shallow->nr);
		for (i = 0; i < nr_shallow; i++) {
617
			const struct commit *c = lookup_commit(oid[shallow[i]].hash);
618 619 620 621 622 623 624 625 626 627 628 629 630 631
			uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
			if (*map)
				used[shallow[i]] = xmemdupz(*map, bitmap_size);
		}
		/*
		 * unreachable shallow commits are not removed from
		 * "ours" and "theirs". The user is supposed to run
		 * step 7 on every ref separately and not trust "ours"
		 * and "theirs" any more.
		 */
	} else
		post_assign_shallow(info, &pi.ref_bitmap, ref_status);

	clear_ref_bitmap(&pi.ref_bitmap);
632 633 634
	for (i = 0; i < pi.pool_count; i++)
		free(pi.pools[i]);
	free(pi.pools);
635 636 637 638 639 640 641 642
	free(shallow);
}

struct commit_array {
	struct commit **commits;
	int nr, alloc;
};

643 644
static int add_ref(const char *refname, const struct object_id *oid,
		   int flags, void *cb_data)
645 646 647
{
	struct commit_array *ca = cb_data;
	ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
648
	ca->commits[ca->nr] = lookup_commit_reference_gently(oid->hash, 1);
649 650 651 652 653 654 655
	if (ca->commits[ca->nr])
		ca->nr++;
	return 0;
}

static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
{
656
	unsigned int i;
657 658 659
	if (!ref_status)
		return;
	for (i = 0; i < nr; i++)
660
		if (bitmap[i / 32] & (1U << (i % 32)))
661 662 663 664 665 666 667 668 669 670
			ref_status[i]++;
}

/*
 * Step 7, reachability test on "ours" at commit level
 */
static void post_assign_shallow(struct shallow_info *info,
				struct ref_bitmap *ref_bitmap,
				int *ref_status)
{
671
	struct object_id *oid = info->shallow->oid;
672 673 674
	struct commit *c;
	uint32_t **bitmap;
	int dst, i, j;
R
René Scharfe 已提交
675
	int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
676 677
	struct commit_array ca;

K
Karsten Blees 已提交
678
	trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
679 680 681 682 683 684 685
	if (ref_status)
		memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr);

	/* Remove unreachable shallow commits from "theirs" */
	for (i = dst = 0; i < info->nr_theirs; i++) {
		if (i != dst)
			info->theirs[dst] = info->theirs[i];
686
		c = lookup_commit(oid[info->theirs[i]].hash);
687 688 689 690 691 692 693 694 695 696 697 698 699
		bitmap = ref_bitmap_at(ref_bitmap, c);
		if (!*bitmap)
			continue;
		for (j = 0; j < bitmap_nr; j++)
			if (bitmap[0][j]) {
				update_refstatus(ref_status, info->ref->nr, *bitmap);
				dst++;
				break;
			}
	}
	info->nr_theirs = dst;

	memset(&ca, 0, sizeof(ca));
700 701
	head_ref(add_ref, &ca);
	for_each_ref(add_ref, &ca);
702 703 704 705 706

	/* Remove unreachable shallow commits from "ours" */
	for (i = dst = 0; i < info->nr_ours; i++) {
		if (i != dst)
			info->ours[dst] = info->ours[i];
707
		c = lookup_commit(oid[info->ours[i]].hash);
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
		bitmap = ref_bitmap_at(ref_bitmap, c);
		if (!*bitmap)
			continue;
		for (j = 0; j < bitmap_nr; j++)
			if (bitmap[0][j] &&
			    /* Step 7, reachability test at commit level */
			    !in_merge_bases_many(c, ca.nr, ca.commits)) {
				update_refstatus(ref_status, info->ref->nr, *bitmap);
				dst++;
				break;
			}
	}
	info->nr_ours = dst;

	free(ca.commits);
}
724 725 726 727 728

/* (Delayed) step 7, reachability test at commit level */
int delayed_reachability_test(struct shallow_info *si, int c)
{
	if (si->need_reachability_test[c]) {
729
		struct commit *commit = lookup_commit(si->shallow->oid[c].hash);
730 731 732

		if (!si->commits) {
			struct commit_array ca;
733

734
			memset(&ca, 0, sizeof(ca));
735 736
			head_ref(add_ref, &ca);
			for_each_ref(add_ref, &ca);
737 738 739 740 741 742 743 744 745 746 747
			si->commits = ca.commits;
			si->nr_commits = ca.nr;
		}

		si->reachable[c] = in_merge_bases_many(commit,
						       si->nr_commits,
						       si->commits);
		si->need_reachability_test[c] = 0;
	}
	return si->reachable[c];
}