aboutsummaryrefslogtreecommitdiffstats
path: root/pseudo-merge.c
blob: f85629b02e48b0c6aba1480a6b037a536ec98a4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
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
566
567
568
569
570
571
572
573
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
#include "git-compat-util.h"
#include "pseudo-merge.h"
#include "date.h"
#include "oid-array.h"
#include "strbuf.h"
#include "config.h"
#include "string-list.h"
#include "refs.h"
#include "pack-bitmap.h"
#include "commit.h"
#include "alloc.h"
#include "progress.h"
#include "hex.h"

#define DEFAULT_PSEUDO_MERGE_DECAY 1.0f
#define DEFAULT_PSEUDO_MERGE_MAX_MERGES 64
#define DEFAULT_PSEUDO_MERGE_SAMPLE_RATE 100
#define DEFAULT_PSEUDO_MERGE_THRESHOLD approxidate("1.week.ago")
#define DEFAULT_PSEUDO_MERGE_STABLE_THRESHOLD approxidate("1.month.ago")
#define DEFAULT_PSEUDO_MERGE_STABLE_SIZE 512

static float gitexp(float base, int exp)
{
	float result = 1;
	while (1) {
		if (exp % 2)
			result *= base;
		exp >>= 1;
		if (!exp)
			break;
		base *= base;
	}
	return result;
}

static uint32_t pseudo_merge_group_size(const struct pseudo_merge_group *group,
					const struct pseudo_merge_matches *matches,
					uint32_t i)
{
	float C = 0.0f;
	uint32_t n;

	/*
	 * The size of pseudo-merge groups decays according to a power series,
	 * which looks like:
	 *
	 *   f(n) = C * n^-k
	 *
	 * , where 'n' is the n-th pseudo-merge group, 'f(n)' is its size, 'k'
	 * is the decay rate, and 'C' is a scaling value.
	 *
	 * The value of C depends on the number of groups, decay rate, and total
	 * number of commits. It is computed such that if there are M and N
	 * total groups and commits, respectively, that:
	 *
	 *   N = f(0) + f(1) + ... f(M-1)
	 *
	 * Rearranging to isolate C, we get:
	 *
	 *   N = \sum_{n=1}^M C / n^k
	 *
	 *   N / C = \sum_{n=1}^M n^-k
	 *
	 *   C = N / \sum_{n=1}^M n^-k
	 *
	 * For example, if we have a decay rate of 'k' being equal to 1.5, 'N'
	 * total commits equal to 10,000, and 'M' being equal to 6 groups, then
	 * the (rounded) group sizes are:
	 *
	 *   { 5469, 1934, 1053, 684, 489, 372 }
	 *
	 * increasing the number of total groups, say to 10, scales the group
	 * sizes appropriately:
	 *
	 *   { 5012, 1772, 964, 626, 448, 341, 271, 221, 186, 158 }
	 */
	for (n = 0; n < group->max_merges; n++)
		C += 1.0f / gitexp(n + 1, group->decay);
	C = matches->unstable_nr / C;

	return (int)((C / gitexp(i + 1, group->decay)) + 0.5);
}

static void init_pseudo_merge_group(struct pseudo_merge_group *group)
{
	memset(group, 0, sizeof(struct pseudo_merge_group));

	strmap_init_with_options(&group->matches, NULL, 0);

	group->decay = DEFAULT_PSEUDO_MERGE_DECAY;
	group->max_merges = DEFAULT_PSEUDO_MERGE_MAX_MERGES;
	group->sample_rate = DEFAULT_PSEUDO_MERGE_SAMPLE_RATE;
	group->threshold = DEFAULT_PSEUDO_MERGE_THRESHOLD;
	group->stable_threshold = DEFAULT_PSEUDO_MERGE_STABLE_THRESHOLD;
	group->stable_size = DEFAULT_PSEUDO_MERGE_STABLE_SIZE;
}

static int pseudo_merge_config(const char *var, const char *value,
			       const struct config_context *ctx,
			       void *cb_data)
{
	struct string_list *list = cb_data;
	struct string_list_item *item;
	struct pseudo_merge_group *group;
	struct strbuf buf = STRBUF_INIT;
	const char *sub, *key;
	size_t sub_len;

	if (parse_config_key(var, "bitmappseudomerge", &sub, &sub_len, &key))
		return 0;

	if (!sub_len)
		return 0;

	strbuf_add(&buf, sub, sub_len);

	item = string_list_lookup(list, buf.buf);
	if (!item) {
		item = string_list_insert(list, buf.buf);

		item->util = xmalloc(sizeof(struct pseudo_merge_group));
		init_pseudo_merge_group(item->util);
	}

	group = item->util;

	if (!strcmp(key, "pattern")) {
		struct strbuf re = STRBUF_INIT;

		free(group->pattern);
		if (*value != '^')
			strbuf_addch(&re, '^');
		strbuf_addstr(&re, value);

		group->pattern = xcalloc(1, sizeof(regex_t));
		if (regcomp(group->pattern, re.buf, REG_EXTENDED))
			die(_("failed to load pseudo-merge regex for %s: '%s'"),
			    sub, re.buf);

		strbuf_release(&re);
	} else if (!strcmp(key, "decay")) {
		group->decay = git_config_int(var, value, ctx->kvi);
		if (group->decay < 0) {
			warning(_("%s must be non-negative, using default"), var);
			group->decay = DEFAULT_PSEUDO_MERGE_DECAY;
		}
	} else if (!strcmp(key, "samplerate")) {
		group->sample_rate = git_config_int(var, value, ctx->kvi);
		if (!(0 <= group->sample_rate && group->sample_rate <= 100)) {
			warning(_("%s must be between 0 and 100, using default"), var);
			group->sample_rate = DEFAULT_PSEUDO_MERGE_SAMPLE_RATE;
		}
	} else if (!strcmp(key, "threshold")) {
		if (git_config_expiry_date(&group->threshold, var, value)) {
			strbuf_release(&buf);
			return -1;
		}
	} else if (!strcmp(key, "maxmerges")) {
		group->max_merges = git_config_int(var, value, ctx->kvi);
		if (group->max_merges < 0) {
			warning(_("%s must be non-negative, using default"), var);
			group->max_merges = DEFAULT_PSEUDO_MERGE_MAX_MERGES;
		}
	} else if (!strcmp(key, "stablethreshold")) {
		if (git_config_expiry_date(&group->stable_threshold, var, value)) {
			strbuf_release(&buf);
			return -1;
		}
	} else if (!strcmp(key, "stablesize")) {
		group->stable_size = git_config_int(var, value, ctx->kvi);
		if (group->stable_size <= 0) {
			warning(_("%s must be positive, using default"), var);
			group->stable_size = DEFAULT_PSEUDO_MERGE_STABLE_SIZE;
		}
	}

	strbuf_release(&buf);

	return 0;
}

void load_pseudo_merges_from_config(struct string_list *list)
{
	struct string_list_item *item;

	git_config(pseudo_merge_config, list);

	for_each_string_list_item(item, list) {
		struct pseudo_merge_group *group = item->util;
		if (!group->pattern)
			die(_("pseudo-merge group '%s' missing required pattern"),
			    item->string);
		if (group->threshold < group->stable_threshold)
			die(_("pseudo-merge group '%s' has unstable threshold "
			      "before stable one"), item->string);
	}
}

static int find_pseudo_merge_group_for_ref(const char *refname,
					   const struct object_id *oid,
					   int flags UNUSED,
					   void *_data)
{
	struct string_list *list = _data;
	struct object_id peeled;
	struct commit *c;
	uint32_t i;
	int has_bitmap;

	if (!peel_iterated_oid(oid, &peeled))
		oid = &peeled;

	c = lookup_commit(the_repository, oid);
	if (!c)
		return 0;

	has_bitmap = bitmap_writer_has_bitmapped_object_id(oid);

	for (i = 0; i < list->nr; i++) {
		struct pseudo_merge_group *group;
		struct pseudo_merge_matches *matches;
		struct strbuf group_name = STRBUF_INIT;
		regmatch_t captures[16];
		size_t j;

		group = list->items[i].util;
		if (regexec(group->pattern, refname, ARRAY_SIZE(captures),
			    captures, 0))
			continue;

		if (captures[ARRAY_SIZE(captures) - 1].rm_so != -1)
			warning(_("pseudo-merge regex from config has too many capture "
				  "groups (max=%"PRIuMAX")"),
				(uintmax_t)ARRAY_SIZE(captures) - 2);

		for (j = !!group->pattern->re_nsub; j < ARRAY_SIZE(captures); j++) {
			regmatch_t *match = &captures[j];
			if (match->rm_so == -1)
				continue;

			if (group_name.len)
				strbuf_addch(&group_name, '-');

			strbuf_add(&group_name, refname + match->rm_so,
				   match->rm_eo - match->rm_so);
		}

		matches = strmap_get(&group->matches, group_name.buf);
		if (!matches) {
			matches = xcalloc(1, sizeof(*matches));
			strmap_put(&group->matches, strbuf_detach(&group_name, NULL),
				   matches);
		}

		if (c->date <= group->stable_threshold) {
			ALLOC_GROW(matches->stable, matches->stable_nr + 1,
				   matches->stable_alloc);
			matches->stable[matches->stable_nr++] = c;
		} else if (c->date <= group->threshold && !has_bitmap) {
			ALLOC_GROW(matches->unstable, matches->unstable_nr + 1,
				   matches->unstable_alloc);
			matches->unstable[matches->unstable_nr++] = c;
		}

		strbuf_release(&group_name);
	}

	return 0;
}

static struct commit *push_pseudo_merge(struct pseudo_merge_group *group)
{
	struct commit *merge;

	ALLOC_GROW(group->merges, group->merges_nr + 1, group->merges_alloc);

	merge = alloc_commit_node(the_repository);
	merge->object.parsed = 1;
	merge->object.flags |= BITMAP_PSEUDO_MERGE;

	group->merges[group->merges_nr++] = merge;

	return merge;
}

static struct pseudo_merge_commit_idx *pseudo_merge_idx(kh_oid_map_t *pseudo_merge_commits,
							const struct object_id *oid)

{
	struct pseudo_merge_commit_idx *pmc;
	khiter_t hash_pos;

	hash_pos = kh_get_oid_map(pseudo_merge_commits, *oid);
	if (hash_pos == kh_end(pseudo_merge_commits)) {
		int hash_ret;
		hash_pos = kh_put_oid_map(pseudo_merge_commits, *oid, &hash_ret);

		CALLOC_ARRAY(pmc, 1);

		kh_value(pseudo_merge_commits, hash_pos) = pmc;
	} else {
		pmc = kh_value(pseudo_merge_commits, hash_pos);
	}

	return pmc;
}

#define MIN_PSEUDO_MERGE_SIZE 8

static void select_pseudo_merges_1(struct pseudo_merge_group *group,
				   struct pseudo_merge_matches *matches,
				   kh_oid_map_t *pseudo_merge_commits,
				   uint32_t *pseudo_merges_nr)
{
	uint32_t i, j;
	uint32_t stable_merges_nr;

	if (!matches->stable_nr && !matches->unstable_nr)
		return; /* all tips in this group already have bitmaps */

	stable_merges_nr = matches->stable_nr / group->stable_size;
	if (matches->stable_nr % group->stable_size)
		stable_merges_nr++;

	/* make stable_merges_nr pseudo merges for stable commits */
	for (i = 0, j = 0; i < stable_merges_nr; i++) {
		struct commit *merge;
		struct commit_list **p;

		merge = push_pseudo_merge(group);
		p = &merge->parents;

		do {
			struct commit *c;
			struct pseudo_merge_commit_idx *pmc;

			if (j >= matches->stable_nr)
				break;

			c = matches->stable[j++];
			pmc = pseudo_merge_idx(pseudo_merge_commits,
					       &c->object.oid);

			ALLOC_GROW(pmc->pseudo_merge, pmc->nr + 1, pmc->alloc);

			pmc->pseudo_merge[pmc->nr++] = *pseudo_merges_nr;
			p = commit_list_append(c, p);
		} while (j % group->stable_size);

		bitmap_writer_push_bitmapped_commit(merge, 1);
		(*pseudo_merges_nr)++;
	}

	/* make up to group->max_merges pseudo merges for unstable commits */
	for (i = 0, j = 0; i < group->max_merges; i++) {
		struct commit *merge;
		struct commit_list **p;
		uint32_t size, end;

		merge = push_pseudo_merge(group);
		p = &merge->parents;

		size = pseudo_merge_group_size(group, matches, i);
		end = size < MIN_PSEUDO_MERGE_SIZE ? matches->unstable_nr : j + size;

		for (; j < end && j < matches->unstable_nr; j++) {
			struct commit *c = matches->unstable[j];
			struct pseudo_merge_commit_idx *pmc;

			if (j % (100 / group->sample_rate))
				continue;

			pmc = pseudo_merge_idx(pseudo_merge_commits,
					       &c->object.oid);

			ALLOC_GROW(pmc->pseudo_merge, pmc->nr + 1, pmc->alloc);

			pmc->pseudo_merge[pmc->nr++] = *pseudo_merges_nr;
			p = commit_list_append(c, p);
		}

		bitmap_writer_push_bitmapped_commit(merge, 1);
		(*pseudo_merges_nr)++;
		if (end >= matches->unstable_nr)
			break;
	}
}

static int commit_date_cmp(const void *va, const void *vb)
{
	timestamp_t a = (*(const struct commit **)va)->date;
	timestamp_t b = (*(const struct commit **)vb)->date;

	if (a < b)
		return -1;
	else if (a > b)
		return 1;
	return 0;
}

static void sort_pseudo_merge_matches(struct pseudo_merge_matches *matches)
{
	QSORT(matches->stable, matches->stable_nr, commit_date_cmp);
	QSORT(matches->unstable, matches->unstable_nr, commit_date_cmp);
}

void select_pseudo_merges(struct string_list *list,
			  struct commit **commits, size_t commits_nr,
			  kh_oid_map_t *pseudo_merge_commits,
			  uint32_t *pseudo_merges_nr,
			  unsigned show_progress)
{
	struct progress *progress = NULL;
	uint32_t i;

	if (!list->nr)
		return;

	if (show_progress)
		progress = start_progress("Selecting pseudo-merge commits", list->nr);

	refs_for_each_ref(get_main_ref_store(the_repository),
			  find_pseudo_merge_group_for_ref, list);

	for (i = 0; i < list->nr; i++) {
		struct pseudo_merge_group *group;
		struct hashmap_iter iter;
		struct strmap_entry *e;

		group = list->items[i].util;
		strmap_for_each_entry(&group->matches, &iter, e) {
			struct pseudo_merge_matches *matches = e->value;

			sort_pseudo_merge_matches(matches);

			select_pseudo_merges_1(group, matches,
					       pseudo_merge_commits,
					       pseudo_merges_nr);
		}

		display_progress(progress, i + 1);
	}

	stop_progress(&progress);
}

void free_pseudo_merge_map(struct pseudo_merge_map *pm)
{
	uint32_t i;
	for (i = 0; i < pm->nr; i++) {
		ewah_pool_free(pm->v[i].commits);
		ewah_pool_free(pm->v[i].bitmap);
	}
	free(pm->v);
}

struct pseudo_merge_commit_ext {
	uint32_t nr;
	const unsigned char *ptr;
};

static int pseudo_merge_ext_at(const struct pseudo_merge_map *pm,
			       struct pseudo_merge_commit_ext *ext, size_t at)
{
	if (at >= pm->map_size)
		return error(_("extended pseudo-merge read out-of-bounds "
			       "(%"PRIuMAX" >= %"PRIuMAX")"),
			     (uintmax_t)at, (uintmax_t)pm->map_size);

	ext->nr = get_be32(pm->map + at);
	ext->ptr = pm->map + at + sizeof(uint32_t);

	return 0;
}

struct ewah_bitmap *pseudo_merge_bitmap(const struct pseudo_merge_map *pm,
					struct pseudo_merge *merge)
{
	if (!merge->loaded_commits)
		BUG("cannot use unloaded pseudo-merge bitmap");

	if (!merge->loaded_bitmap) {
		size_t at = merge->bitmap_at;

		merge->bitmap = read_bitmap(pm->map, pm->map_size, &at);
		merge->loaded_bitmap = 1;
	}

	return merge->bitmap;
}

struct pseudo_merge *use_pseudo_merge(const struct pseudo_merge_map *pm,
				      struct pseudo_merge *merge)
{
	if (!merge->loaded_commits) {
		size_t pos = merge->at;

		merge->commits = read_bitmap(pm->map, pm->map_size, &pos);
		merge->bitmap_at = pos;
		merge->loaded_commits = 1;
	}
	return merge;
}

static struct pseudo_merge *pseudo_merge_at(const struct pseudo_merge_map *pm,
					    struct object_id *oid,
					    size_t want)
{
	size_t lo = 0;
	size_t hi = pm->nr;

	while (lo < hi) {
		size_t mi = lo + (hi - lo) / 2;
		size_t got = pm->v[mi].at;

		if (got == want)
			return use_pseudo_merge(pm, &pm->v[mi]);
		else if (got < want)
			hi = mi;
		else
			lo = mi + 1;
	}

	warning(_("could not find pseudo-merge for commit %s at offset %"PRIuMAX),
		oid_to_hex(oid), (uintmax_t)want);

	return NULL;
}

struct pseudo_merge_commit {
	uint32_t commit_pos;
	uint64_t pseudo_merge_ofs;
};

#define PSEUDO_MERGE_COMMIT_RAWSZ (sizeof(uint32_t)+sizeof(uint64_t))

static void read_pseudo_merge_commit_at(struct pseudo_merge_commit *merge,
					const unsigned char *at)
{
	merge->commit_pos = get_be32(at);
	merge->pseudo_merge_ofs = get_be64(at + sizeof(uint32_t));
}

static int nth_pseudo_merge_ext(const struct pseudo_merge_map *pm,
				struct pseudo_merge_commit_ext *ext,
				struct pseudo_merge_commit *merge,
				uint32_t n)
{
	size_t ofs;

	if (n >= ext->nr)
		return error(_("extended pseudo-merge lookup out-of-bounds "
			       "(%"PRIu32" >= %"PRIu32")"), n, ext->nr);

	ofs = get_be64(ext->ptr + st_mult(n, sizeof(uint64_t)));
	if (ofs >= pm->map_size)
		return error(_("out-of-bounds read: (%"PRIuMAX" >= %"PRIuMAX")"),
			     (uintmax_t)ofs, (uintmax_t)pm->map_size);

	read_pseudo_merge_commit_at(merge, pm->map + ofs);

	return 0;
}

static unsigned apply_pseudo_merge(const struct pseudo_merge_map *pm,
				   struct pseudo_merge *merge,
				   struct bitmap *result,
				   struct bitmap *roots)
{
	if (merge->satisfied)
		return 0;

	if (!ewah_bitmap_is_subset(merge->commits, roots ? roots : result))
		return 0;

	bitmap_or_ewah(result, pseudo_merge_bitmap(pm, merge));
	if (roots)
		bitmap_or_ewah(roots, pseudo_merge_bitmap(pm, merge));
	merge->satisfied = 1;

	return 1;
}

static int pseudo_merge_commit_cmp(const void *va, const void *vb)
{
	struct pseudo_merge_commit merge;
	uint32_t key = *(uint32_t*)va;

	read_pseudo_merge_commit_at(&merge, vb);

	if (key < merge.commit_pos)
		return -1;
	if (key > merge.commit_pos)
		return 1;
	return 0;
}

static struct pseudo_merge_commit *find_pseudo_merge(const struct pseudo_merge_map *pm,
						     uint32_t pos)
{
	if (!pm->commits_nr)
		return NULL;

	return bsearch(&pos, pm->commits, pm->commits_nr,
		       PSEUDO_MERGE_COMMIT_RAWSZ, pseudo_merge_commit_cmp);
}

int apply_pseudo_merges_for_commit(const struct pseudo_merge_map *pm,
				   struct bitmap *result,
				   struct commit *commit, uint32_t commit_pos)
{
	struct pseudo_merge *merge;
	struct pseudo_merge_commit *merge_commit;
	int ret = 0;

	merge_commit = find_pseudo_merge(pm, commit_pos);
	if (!merge_commit)
		return 0;

	if (merge_commit->pseudo_merge_ofs & ((uint64_t)1<<63)) {
		struct pseudo_merge_commit_ext ext = { 0 };
		off_t ofs = merge_commit->pseudo_merge_ofs & ~((uint64_t)1<<63);
		uint32_t i;

		if (pseudo_merge_ext_at(pm, &ext, ofs) < -1) {
			warning(_("could not read extended pseudo-merge table "
				  "for commit %s"),
				oid_to_hex(&commit->object.oid));
			return ret;
		}

		for (i = 0; i < ext.nr; i++) {
			if (nth_pseudo_merge_ext(pm, &ext, merge_commit, i) < 0)
				return ret;

			merge = pseudo_merge_at(pm, &commit->object.oid,
						merge_commit->pseudo_merge_ofs);

			if (!merge)
				return ret;

			if (apply_pseudo_merge(pm, merge, result, NULL))
				ret++;
		}
	} else {
		merge = pseudo_merge_at(pm, &commit->object.oid,
					merge_commit->pseudo_merge_ofs);

		if (!merge)
			return ret;

		if (apply_pseudo_merge(pm, merge, result, NULL))
			ret++;
	}

	if (ret)
		cascade_pseudo_merges(pm, result, NULL);

	return ret;
}

int cascade_pseudo_merges(const struct pseudo_merge_map *pm,
			  struct bitmap *result,
			  struct bitmap *roots)
{
	unsigned any_satisfied;
	int ret = 0;

	do {
		struct pseudo_merge *merge;
		uint32_t i;

		any_satisfied = 0;

		for (i = 0; i < pm->nr; i++) {
			merge = use_pseudo_merge(pm, &pm->v[i]);
			if (apply_pseudo_merge(pm, merge, result, roots)) {
				any_satisfied |= 1;
				ret++;
			}
		}
	} while (any_satisfied);

	return ret;
}

struct pseudo_merge *pseudo_merge_for_parents(const struct pseudo_merge_map *pm,
					      struct bitmap *parents)
{
	struct pseudo_merge *match = NULL;
	size_t i;

	if (!pm->nr)
		return NULL;

	/*
	 * NOTE: this loop is quadratic in the worst-case (where no
	 * matching pseudo-merge bitmaps are found), but in practice
	 * this is OK for a few reasons:
	 *
	 *   - Rejecting pseudo-merge bitmaps that do not match the
	 *     given commit is done quickly (i.e. `bitmap_equals_ewah()`
	 *     returns early when we know the two bitmaps aren't equal.
	 *
	 *   - Already matched pseudo-merge bitmaps (which we track with
	 *     the `->satisfied` bit here) are skipped as potential
	 *     candidates.
	 *
	 *   - The number of pseudo-merges should be small (in the
	 *     hundreds for most repositories).
	 *
	 * If in the future this semi-quadratic behavior does become a
	 * problem, another approach would be to keep track of which
	 * pseudo-merges are still "viable" after enumerating the
	 * pseudo-merge commit's parents:
	 *
	 *   - A pseudo-merge bitmap becomes non-viable when the bit(s)
	 *     corresponding to one or more parent(s) of the given
	 *     commit are not set in a candidate pseudo-merge's commits
	 *     bitmap.
	 *
	 *   - After processing all bits, enumerate the remaining set of
	 *     viable pseudo-merge bitmaps, and check that their
	 *     popcount() matches the number of parents in the given
	 *     commit.
	 */
	for (i = 0; i < pm->nr; i++) {
		struct pseudo_merge *candidate = use_pseudo_merge(pm, &pm->v[i]);
		if (!candidate || candidate->satisfied)
			continue;
		if (!bitmap_equals_ewah(parents, candidate->commits))
			continue;

		match = candidate;
		match->satisfied = 1;
		break;
	}

	return match;
}