git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
130fd90e45b89c1adc38a6cce3c1c1a470fcdd0f blob 37017 bytes (raw)

   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
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
 
#include "../cache.h"
#include "../chdir-notify.h"
#include "../config.h"
#include "../iterator.h"
#include "../lockfile.h"
#include "../refs.h"
#include "../reftable/reftable-stack.h"
#include "../reftable/reftable-record.h"
#include "../reftable/reftable-error.h"
#include "../reftable/reftable-blocksource.h"
#include "../reftable/reftable-reader.h"
#include "../reftable/reftable-iterator.h"
#include "../reftable/reftable-merged.h"
#include "../reftable/reftable-generic.h"
#include "../worktree.h"
#include "refs-internal.h"

extern struct ref_storage_be refs_be_reftable;

struct git_reftable_ref_store {
	struct ref_store base;
	unsigned int store_flags;

	int err;
	char *repo_dir;

	char *reftable_dir;
	char *worktree_reftable_dir;

	struct reftable_stack *main_stack;
	struct reftable_stack *worktree_stack;
};

static struct reftable_stack *stack_for(struct git_reftable_ref_store *store,
					const char *refname)
{
	if (store->worktree_stack == NULL)
		return store->main_stack;

	switch (ref_type(refname)) {
	case REF_TYPE_PER_WORKTREE:
	case REF_TYPE_PSEUDOREF:
	case REF_TYPE_OTHER_PSEUDOREF:
		return store->worktree_stack;
	default:
	case REF_TYPE_MAIN_PSEUDOREF:
	case REF_TYPE_NORMAL:
		return store->main_stack;
	}
}

static int git_reftable_read_raw_ref(struct ref_store *ref_store,
				     const char *refname, struct object_id *oid,
				     struct strbuf *referent,
				     unsigned int *type);

static void clear_reftable_log_record(struct reftable_log_record *log)
{
	log->refname = NULL;
	switch (log->value_type) {
	case REFTABLE_LOG_UPDATE:
		log->update.old_hash = NULL;
		log->update.new_hash = NULL;
		log->update.message = NULL;
		break;
	case REFTABLE_LOG_DELETION:
		break;
	}
	reftable_log_record_release(log);
}

static void fill_reftable_log_record(struct reftable_log_record *log)
{
	const char *info = git_committer_info(0);
	struct ident_split split = { NULL };
	int result = split_ident_line(&split, info, strlen(info));
	int sign = 1;
	assert(0 == result);

	reftable_log_record_release(log);
	log->value_type = REFTABLE_LOG_UPDATE;
	log->update.name =
		xstrndup(split.name_begin, split.name_end - split.name_begin);
	log->update.email =
		xstrndup(split.mail_begin, split.mail_end - split.mail_begin);
	log->update.time = atol(split.date_begin);
	if (*split.tz_begin == '-') {
		sign = -1;
		split.tz_begin++;
	}
	if (*split.tz_begin == '+') {
		sign = 1;
		split.tz_begin++;
	}

	log->update.tz_offset = sign * atoi(split.tz_begin);
}

static int has_suffix(struct strbuf *b, const char *suffix)
{
	size_t len = strlen(suffix);

	if (len > b->len) {
		return 0;
	}

	return 0 == strncmp(b->buf + b->len - len, suffix, len);
}

/* trims the last path component of b. Returns -1 if it is not
 * present, or 0 on success
 */
static int trim_component(struct strbuf *b)
{
	char *last;
	last = strrchr(b->buf, '/');
	if (!last)
		return -1;
	strbuf_setlen(b, last - b->buf);
	return 0;
}

/* Returns whether `b` is a worktree path, trimming it to the gitdir
 */
static int is_worktree(struct strbuf *b)
{
	if (trim_component(b) < 0) {
		return 0;
	}
	if (!has_suffix(b, "/worktrees")) {
		return 0;
	}
	trim_component(b);
	return 1;
}

static struct ref_store *git_reftable_ref_store_create(const char *path,
						       unsigned int store_flags)
{
	struct git_reftable_ref_store *refs = xcalloc(1, sizeof(*refs));
	struct ref_store *ref_store = (struct ref_store *)refs;
	struct reftable_write_options cfg = {
		.block_size = 4096,
		.hash_id = the_hash_algo->format_id,
	};
	struct strbuf sb = STRBUF_INIT;
	const char *gitdir = path;
	struct strbuf wt_buf = STRBUF_INIT;
	int wt = 0;

	strbuf_addstr(&wt_buf, path);

	/* this is clumsy, but the official worktree functions (eg.
	 * get_worktrees()) function will try to initialize a ref storage
	 * backend, leading to infinite recursion.  */
	wt = is_worktree(&wt_buf);
	if (wt) {
		gitdir = wt_buf.buf;
	}

	base_ref_store_init(ref_store, &refs_be_reftable);
	ref_store->gitdir = xstrdup(gitdir);
	refs->store_flags = store_flags;
	strbuf_addf(&sb, "%s/reftable", gitdir);
	refs->reftable_dir = xstrdup(sb.buf);
	strbuf_reset(&sb);

	refs->err =
		reftable_new_stack(&refs->main_stack, refs->reftable_dir, cfg);
	assert(refs->err != REFTABLE_API_ERROR);

	if (refs->err == 0 && wt) {
		strbuf_addf(&sb, "%s/reftable", path);
		refs->worktree_reftable_dir = xstrdup(sb.buf);

		refs->err = reftable_new_stack(&refs->worktree_stack,
					       refs->worktree_reftable_dir,
					       cfg);
		assert(refs->err != REFTABLE_API_ERROR);
	}

	strbuf_release(&sb);
	strbuf_release(&wt_buf);
	return ref_store;
}

static int git_reftable_init_db(struct ref_store *ref_store, struct strbuf *err)
{
	struct git_reftable_ref_store *refs =
		(struct git_reftable_ref_store *)ref_store;
	struct strbuf sb = STRBUF_INIT;

	safe_create_dir(refs->reftable_dir, 1);
	assert(refs->worktree_reftable_dir == NULL);

	strbuf_addf(&sb, "%s/HEAD", refs->base.gitdir);
	write_file(sb.buf, "ref: refs/heads/.invalid");
	strbuf_reset(&sb);

	strbuf_addf(&sb, "%s/refs", refs->base.gitdir);
	safe_create_dir(sb.buf, 1);
	strbuf_reset(&sb);

	strbuf_addf(&sb, "%s/refs/heads", refs->base.gitdir);
	write_file(sb.buf, "this repository uses the reftable format");

	return 0;
}

struct git_reftable_iterator {
	struct ref_iterator base;
	struct reftable_iterator iter;
	struct reftable_ref_record ref;
	struct object_id oid;
	struct ref_store *ref_store;

	/* In case we must iterate over 2 stacks, this is non-null. */
	struct reftable_merged_table *merged;
	unsigned int flags;
	int err;
	const char *prefix;
};

static int reftable_ref_iterator_advance(struct ref_iterator *ref_iterator)
{
	struct git_reftable_iterator *ri =
		(struct git_reftable_iterator *)ref_iterator;
	while (ri->err == 0) {
		ri->err = reftable_iterator_next_ref(&ri->iter, &ri->ref);
		if (ri->err) {
			break;
		}

		if (ref_type(ri->ref.refname) == REF_TYPE_PSEUDOREF) {
			/*
			  pseudorefs, eg. HEAD, FETCH_HEAD should not be
			  produced, by default.
			 */
			continue;
		}
		ri->base.refname = ri->ref.refname;
		if (ri->prefix != NULL &&
		    strncmp(ri->prefix, ri->ref.refname, strlen(ri->prefix))) {
			ri->err = 1;
			break;
		}
		if (ri->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
		    ref_type(ri->base.refname) != REF_TYPE_PER_WORKTREE)
			continue;

		ri->base.flags = 0;
		switch (ri->ref.value_type) {
		case REFTABLE_REF_VAL1:
			hashcpy(ri->oid.hash, ri->ref.value.val1);
			break;
		case REFTABLE_REF_VAL2:
			hashcpy(ri->oid.hash, ri->ref.value.val2.value);
			break;
		case REFTABLE_REF_SYMREF: {
			int out_flags = 0;
			const char *resolved = refs_resolve_ref_unsafe(
				ri->ref_store, ri->ref.refname,
				RESOLVE_REF_READING, &ri->oid, &out_flags);
			ri->base.flags = out_flags;
			if (resolved == NULL &&
			    !(ri->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
			    (ri->base.flags & REF_ISBROKEN)) {
				continue;
			}
			break;
		}
		default:
			abort();
		}

		ri->base.oid = &ri->oid;
		if (!(ri->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
		    !ref_resolves_to_object(ri->base.refname, ri->base.oid,
					    ri->base.flags)) {
			continue;
		}

		break;
	}

	if (ri->err > 0) {
		return ITER_DONE;
	}
	if (ri->err < 0) {
		return ITER_ERROR;
	}

	return ITER_OK;
}

static int reftable_ref_iterator_peel(struct ref_iterator *ref_iterator,
				      struct object_id *peeled)
{
	struct git_reftable_iterator *ri =
		(struct git_reftable_iterator *)ref_iterator;
	if (ri->ref.value_type == REFTABLE_REF_VAL2) {
		hashcpy(peeled->hash, ri->ref.value.val2.target_value);
		return 0;
	}

	return -1;
}

static int reftable_ref_iterator_abort(struct ref_iterator *ref_iterator)
{
	struct git_reftable_iterator *ri =
		(struct git_reftable_iterator *)ref_iterator;
	reftable_ref_record_release(&ri->ref);
	reftable_iterator_destroy(&ri->iter);
	if (ri->merged) {
		reftable_merged_table_free(ri->merged);
	}
	return 0;
}

static struct ref_iterator_vtable reftable_ref_iterator_vtable = {
	reftable_ref_iterator_advance, reftable_ref_iterator_peel,
	reftable_ref_iterator_abort
};

static struct ref_iterator *
git_reftable_ref_iterator_begin(struct ref_store *ref_store, const char *prefix,
				unsigned int flags)
{
	struct git_reftable_ref_store *refs =
		(struct git_reftable_ref_store *)ref_store;
	struct git_reftable_iterator *ri = xcalloc(1, sizeof(*ri));

	if (refs->err < 0) {
		ri->err = refs->err;
	} else if (refs->worktree_stack == NULL) {
		struct reftable_merged_table *mt =
			reftable_stack_merged_table(refs->main_stack);
		ri->err = reftable_merged_table_seek_ref(mt, &ri->iter, prefix);
	} else {
		struct reftable_merged_table *mt1 =
			reftable_stack_merged_table(refs->main_stack);
		struct reftable_merged_table *mt2 =
			reftable_stack_merged_table(refs->worktree_stack);
		struct reftable_table *tabs =
			xcalloc(2, sizeof(struct reftable_table));
		reftable_table_from_merged_table(&tabs[0], mt1);
		reftable_table_from_merged_table(&tabs[1], mt2);
		ri->err = reftable_new_merged_table(&ri->merged, tabs, 2,
						    the_hash_algo->format_id);
		if (ri->err == 0)
			ri->err = reftable_merged_table_seek_ref(
				ri->merged, &ri->iter, prefix);
	}

	base_ref_iterator_init(&ri->base, &reftable_ref_iterator_vtable, 1);
	ri->prefix = prefix;
	ri->base.oid = &ri->oid;
	ri->flags = flags;
	ri->ref_store = ref_store;
	return &ri->base;
}

static int fixup_symrefs(struct ref_store *ref_store,
			 struct ref_transaction *transaction)
{
	struct strbuf referent = STRBUF_INIT;
	int i = 0;
	int err = 0;

	for (i = 0; i < transaction->nr; i++) {
		struct ref_update *update = transaction->updates[i];
		struct object_id old_oid;

		err = git_reftable_read_raw_ref(ref_store, update->refname,
						&old_oid, &referent,
						/* mutate input, like
						   files-backend.c */
						&update->type);
		if (err < 0 && errno == ENOENT &&
		    is_null_oid(&update->old_oid)) {
			err = 0;
		}
		if (err < 0)
			goto done;

		if (!(update->type & REF_ISSYMREF))
			continue;

		if (update->flags & REF_NO_DEREF) {
			/* what should happen here? See files-backend.c
			 * lock_ref_for_update. */
		} else {
			/*
			  If we are updating a symref (eg. HEAD), we should also
			  update the branch that the symref points to.

			  This is generic functionality, and would be better
			  done in refs.c, but the current implementation is
			  intertwined with the locking in files-backend.c.
			*/
			int new_flags = update->flags;
			struct ref_update *new_update = NULL;

			/* if this is an update for HEAD, should also record a
			   log entry for HEAD? See files-backend.c,
			   split_head_update()
			*/
			new_update = ref_transaction_add_update(
				transaction, referent.buf, new_flags,
				&update->new_oid, &update->old_oid,
				update->msg);
			new_update->parent_update = update;

			/* files-backend sets REF_LOG_ONLY here. */
			update->flags |= REF_NO_DEREF | REF_LOG_ONLY;
			update->flags &= ~REF_HAVE_OLD;
		}
	}

done:
	assert(err != REFTABLE_API_ERROR);
	strbuf_release(&referent);
	return err;
}

static int git_reftable_transaction_prepare(struct ref_store *ref_store,
					    struct ref_transaction *transaction,
					    struct strbuf *errbuf)
{
	struct git_reftable_ref_store *refs =
		(struct git_reftable_ref_store *)ref_store;
	struct reftable_addition *add = NULL;
	struct reftable_stack *stack =
		transaction->nr ?
			      stack_for(refs, transaction->updates[0]->refname) :
			      refs->main_stack;
	int err = refs->err;
	if (err < 0) {
		goto done;
	}

	err = reftable_stack_reload(stack);
	if (err) {
		goto done;
	}

	err = reftable_stack_new_addition(&add, stack);
	if (err) {
		goto done;
	}

	err = fixup_symrefs(ref_store, transaction);
	if (err) {
		goto done;
	}

	transaction->backend_data = add;
	transaction->state = REF_TRANSACTION_PREPARED;

done:
	assert(err != REFTABLE_API_ERROR);
	if (err < 0) {
		transaction->state = REF_TRANSACTION_CLOSED;
		strbuf_addf(errbuf, "reftable: transaction prepare: %s",
			    reftable_error_str(err));
	}

	return err;
}

static int git_reftable_transaction_abort(struct ref_store *ref_store,
					  struct ref_transaction *transaction,
					  struct strbuf *err)
{
	struct reftable_addition *add =
		(struct reftable_addition *)transaction->backend_data;
	reftable_addition_destroy(add);
	transaction->backend_data = NULL;
	return 0;
}

static int reftable_check_old_oid(struct ref_store *refs, const char *refname,
				  struct object_id *want_oid)
{
	struct object_id out_oid;
	int out_flags = 0;
	const char *resolved = refs_resolve_ref_unsafe(
		refs, refname, RESOLVE_REF_READING, &out_oid, &out_flags);
	if (is_null_oid(want_oid) != (resolved == NULL)) {
		return REFTABLE_LOCK_ERROR;
	}

	if (resolved != NULL && !oideq(&out_oid, want_oid)) {
		return REFTABLE_LOCK_ERROR;
	}

	return 0;
}

static int ref_update_cmp(const void *a, const void *b)
{
	return strcmp((*(struct ref_update **)a)->refname,
		      (*(struct ref_update **)b)->refname);
}

static int write_transaction_table(struct reftable_writer *writer, void *arg)
{
	struct ref_transaction *transaction = (struct ref_transaction *)arg;
	struct git_reftable_ref_store *refs =
		(struct git_reftable_ref_store *)transaction->ref_store;
	struct reftable_stack *stack =
		stack_for(refs, transaction->updates[0]->refname);
	uint64_t ts = reftable_stack_next_update_index(stack);
	int err = 0;
	int i = 0;
	struct reftable_log_record *logs =
		calloc(transaction->nr, sizeof(*logs));
	struct ref_update **sorted =
		malloc(transaction->nr * sizeof(struct ref_update *));
	COPY_ARRAY(sorted, transaction->updates, transaction->nr);
	QSORT(sorted, transaction->nr, ref_update_cmp);
	reftable_writer_set_limits(writer, ts, ts);

	for (i = 0; i < transaction->nr; i++) {
		struct ref_update *u = sorted[i];
		struct reftable_log_record *log = &logs[i];
		fill_reftable_log_record(log);
		log->update_index = ts;
		log->value_type = REFTABLE_LOG_UPDATE;
		log->refname = (char *)u->refname;
		log->update.old_hash = u->old_oid.hash;
		log->update.new_hash = u->new_oid.hash;
		log->update.message = u->msg;

		if (u->flags & REF_LOG_ONLY) {
			continue;
		}

		if (u->flags & REF_HAVE_NEW) {
			struct reftable_ref_record ref = { NULL };
			struct object_id peeled;

			int peel_error = peel_object(&u->new_oid, &peeled);
			ref.refname = (char *)u->refname;
			ref.update_index = ts;

			if (!peel_error) {
				ref.value_type = REFTABLE_REF_VAL2;
				ref.value.val2.target_value = peeled.hash;
				ref.value.val2.value = u->new_oid.hash;
			} else if (!is_null_oid(&u->new_oid)) {
				ref.value_type = REFTABLE_REF_VAL1;
				ref.value.val1 = u->new_oid.hash;
			}

			err = reftable_writer_add_ref(writer, &ref);
			if (err < 0) {
				goto done;
			}
		}
	}

	for (i = 0; i < transaction->nr; i++) {
		err = reftable_writer_add_log(writer, &logs[i]);
		clear_reftable_log_record(&logs[i]);
		if (err < 0) {
			goto done;
		}
	}

done:
	assert(err != REFTABLE_API_ERROR);
	free(logs);
	free(sorted);
	return err;
}

static int git_reftable_transaction_finish(struct ref_store *ref_store,
					   struct ref_transaction *transaction,
					   struct strbuf *errmsg)
{
	struct reftable_addition *add =
		(struct reftable_addition *)transaction->backend_data;
	int err = 0;
	int i;

	for (i = 0; i < transaction->nr; i++) {
		struct ref_update *u = transaction->updates[i];
		if (u->flags & REF_HAVE_OLD) {
			err = reftable_check_old_oid(transaction->ref_store,
						     u->refname, &u->old_oid);
			if (err < 0) {
				goto done;
			}
		}
	}
	if (transaction->nr) {
		err = reftable_addition_add(add, &write_transaction_table,
					    transaction);
		if (err < 0) {
			goto done;
		}
	}

	err = reftable_addition_commit(add);

done:
	assert(err != REFTABLE_API_ERROR);
	reftable_addition_destroy(add);
	transaction->state = REF_TRANSACTION_CLOSED;
	transaction->backend_data = NULL;
	if (err) {
		strbuf_addf(errmsg, "reftable: transaction failure: %s",
			    reftable_error_str(err));
		return -1;
	}
	return err;
}

static int
git_reftable_transaction_initial_commit(struct ref_store *ref_store,
					struct ref_transaction *transaction,
					struct strbuf *errmsg)
{
	int err = git_reftable_transaction_prepare(ref_store, transaction,
						   errmsg);
	if (err)
		return err;

	return git_reftable_transaction_finish(ref_store, transaction, errmsg);
}

struct write_delete_refs_arg {
	struct reftable_stack *stack;
	struct string_list *refnames;
	const char *logmsg;
	unsigned int flags;
};

static int write_delete_refs_table(struct reftable_writer *writer, void *argv)
{
	struct write_delete_refs_arg *arg =
		(struct write_delete_refs_arg *)argv;
	uint64_t ts = reftable_stack_next_update_index(arg->stack);
	int err = 0;
	int i = 0;

	reftable_writer_set_limits(writer, ts, ts);
	for (i = 0; i < arg->refnames->nr; i++) {
		struct reftable_ref_record ref = {
			.refname = (char *)arg->refnames->items[i].string,
			.value_type = REFTABLE_REF_DELETION,
			.update_index = ts,
		};
		err = reftable_writer_add_ref(writer, &ref);
		if (err < 0) {
			return err;
		}
	}

	for (i = 0; i < arg->refnames->nr; i++) {
		struct reftable_log_record log = {
			.update_index = ts,
		};
		struct reftable_ref_record current = { NULL };
		fill_reftable_log_record(&log);
		log.update_index = ts;
		log.refname = (char *)arg->refnames->items[i].string;

		log.update.message = xstrdup(arg->logmsg);
		log.update.new_hash = NULL;
		log.update.old_hash = NULL;
		if (reftable_stack_read_ref(arg->stack, log.refname,
					    &current) == 0) {
			log.update.old_hash =
				reftable_ref_record_val1(&current);
		}
		err = reftable_writer_add_log(writer, &log);
		log.update.old_hash = NULL;
		reftable_ref_record_release(&current);

		clear_reftable_log_record(&log);
		if (err < 0) {
			return err;
		}
	}
	return 0;
}

static int git_reftable_delete_refs(struct ref_store *ref_store,
				    const char *msg,
				    struct string_list *refnames,
				    unsigned int flags)
{
	struct git_reftable_ref_store *refs =
		(struct git_reftable_ref_store *)ref_store;
	struct reftable_stack *stack =
		stack_for(refs, refnames->items[0].string);
	struct write_delete_refs_arg arg = {
		.stack = stack,
		.refnames = refnames,
		.logmsg = msg,
		.flags = flags,
	};
	int err = refs->err;
	if (err < 0) {
		goto done;
	}

	string_list_sort(refnames);
	err = reftable_stack_reload(stack);
	if (err) {
		goto done;
	}
	err = reftable_stack_add(stack, &write_delete_refs_table, &arg);
done:
	assert(err != REFTABLE_API_ERROR);
	return err;
}

static int git_reftable_pack_refs(struct ref_store *ref_store,
				  unsigned int flags)
{
	struct git_reftable_ref_store *refs =
		(struct git_reftable_ref_store *)ref_store;
	int err = refs->err;
	if (err < 0) {
		return err;
	}
	err = reftable_stack_compact_all(refs->main_stack, NULL);
	if (err == 0 && refs->worktree_stack != NULL)
		err = reftable_stack_compact_all(refs->worktree_stack, NULL);
	if (err == 0)
		err = reftable_stack_clean(refs->main_stack);
	if (err == 0 && refs->worktree_stack != NULL)
		err = reftable_stack_clean(refs->worktree_stack);

	return err;
}

struct write_create_symref_arg {
	struct git_reftable_ref_store *refs;
	struct reftable_stack *stack;
	const char *refname;
	const char *target;
	const char *logmsg;
};

static int write_create_symref_table(struct reftable_writer *writer, void *arg)
{
	struct write_create_symref_arg *create =
		(struct write_create_symref_arg *)arg;
	uint64_t ts = reftable_stack_next_update_index(create->stack);
	int err = 0;

	struct reftable_ref_record ref = {
		.refname = (char *)create->refname,
		.value_type = REFTABLE_REF_SYMREF,
		.value.symref = (char *)create->target,
		.update_index = ts,
	};
	reftable_writer_set_limits(writer, ts, ts);
	err = reftable_writer_add_ref(writer, &ref);
	if (err == 0) {
		struct reftable_log_record log = { NULL };
		struct object_id new_oid;
		struct object_id old_oid;

		fill_reftable_log_record(&log);
		log.refname = (char *)create->refname;
		log.update_index = ts;
		log.update.message = (char *)create->logmsg;
		if (refs_resolve_ref_unsafe(
			    (struct ref_store *)create->refs, create->refname,
			    RESOLVE_REF_READING, &old_oid, NULL) != NULL) {
			log.update.old_hash = old_oid.hash;
		}

		if (refs_resolve_ref_unsafe((struct ref_store *)create->refs,
					    create->target, RESOLVE_REF_READING,
					    &new_oid, NULL) != NULL) {
			log.update.new_hash = new_oid.hash;
		}

		if (log.update.old_hash != NULL ||
		    log.update.new_hash != NULL) {
			err = reftable_writer_add_log(writer, &log);
		}
		log.refname = NULL;
		log.update.message = NULL;
		log.update.old_hash = NULL;
		log.update.new_hash = NULL;
		clear_reftable_log_record(&log);
	}
	return err;
}

static int git_reftable_create_symref(struct ref_store *ref_store,
				      const char *refname, const char *target,
				      const char *logmsg)
{
	struct git_reftable_ref_store *refs =
		(struct git_reftable_ref_store *)ref_store;
	struct reftable_stack *stack = stack_for(refs, refname);
	struct write_create_symref_arg arg = { .refs = refs,
					       .stack = stack,
					       .refname = refname,
					       .target = target,
					       .logmsg = logmsg };
	int err = refs->err;
	if (err < 0) {
		goto done;
	}
	err = reftable_stack_reload(stack);
	if (err) {
		goto done;
	}
	err = reftable_stack_add(stack, &write_create_symref_table, &arg);
done:
	assert(err != REFTABLE_API_ERROR);
	return err;
}

struct write_rename_arg {
	struct reftable_stack *stack;
	const char *oldname;
	const char *newname;
	const char *logmsg;
};

static int write_rename_table(struct reftable_writer *writer, void *argv)
{
	struct write_rename_arg *arg = (struct write_rename_arg *)argv;
	uint64_t ts = reftable_stack_next_update_index(arg->stack);
	struct reftable_ref_record ref = { NULL };
	int err = reftable_stack_read_ref(arg->stack, arg->oldname, &ref);

	if (err) {
		goto done;
	}

	/* XXX do ref renames overwrite the target? */
	if (reftable_stack_read_ref(arg->stack, arg->newname, &ref) == 0) {
		goto done;
	}

	reftable_writer_set_limits(writer, ts, ts);

	{
		struct reftable_ref_record todo[2] = {
			{
				.refname = (char *)arg->oldname,
				.update_index = ts,
				.value_type = REFTABLE_REF_DELETION,
			},
			ref,
		};
		todo[1].update_index = ts;
		free(todo[1].refname);
		todo[1].refname = strdup(arg->newname);

		err = reftable_writer_add_refs(writer, todo, 2);
		if (err < 0) {
			goto done;
		}
	}

	if (reftable_ref_record_val1(&ref)) {
		uint8_t *val1 = reftable_ref_record_val1(&ref);
		struct reftable_log_record todo[2] = { { NULL } };
		fill_reftable_log_record(&todo[0]);
		fill_reftable_log_record(&todo[1]);

		todo[0].refname = (char *)arg->oldname;
		todo[0].update_index = ts;
		todo[0].update.message = (char *)arg->logmsg;
		todo[0].update.old_hash = val1;
		todo[0].update.new_hash = NULL;

		todo[1].refname = (char *)arg->newname;
		todo[1].update_index = ts;
		todo[1].update.old_hash = NULL;
		todo[1].update.new_hash = val1;
		todo[1].update.message = (char *)arg->logmsg;

		err = reftable_writer_add_logs(writer, todo, 2);

		clear_reftable_log_record(&todo[0]);
		clear_reftable_log_record(&todo[1]);

		if (err < 0) {
			goto done;
		}

	} else {
		/* XXX symrefs? */
	}

done:
	assert(err != REFTABLE_API_ERROR);
	reftable_ref_record_release(&ref);
	return err;
}

static int git_reftable_rename_ref(struct ref_store *ref_store,
				   const char *oldrefname,
				   const char *newrefname, const char *logmsg)
{
	struct git_reftable_ref_store *refs =
		(struct git_reftable_ref_store *)ref_store;
	struct reftable_stack *stack = stack_for(refs, newrefname);
	struct write_rename_arg arg = {
		.stack = stack,
		.oldname = oldrefname,
		.newname = newrefname,
		.logmsg = logmsg,
	};
	int err = refs->err;
	if (err < 0) {
		goto done;
	}
	err = reftable_stack_reload(stack);
	if (err) {
		goto done;
	}

	err = reftable_stack_add(stack, &write_rename_table, &arg);
done:
	assert(err != REFTABLE_API_ERROR);
	return err;
}

static int git_reftable_copy_ref(struct ref_store *ref_store,
				 const char *oldrefname, const char *newrefname,
				 const char *logmsg)
{
	BUG("reftable reference store does not support copying references");
}

struct git_reftable_reflog_ref_iterator {
	struct ref_iterator base;
	struct reftable_iterator iter;
	struct reftable_log_record log;
	struct object_id oid;

	/* Used when iterating over worktree & main */
	struct reftable_merged_table *merged;
	char *last_name;
};

static int
git_reftable_reflog_ref_iterator_advance(struct ref_iterator *ref_iterator)
{
	struct git_reftable_reflog_ref_iterator *ri =
		(struct git_reftable_reflog_ref_iterator *)ref_iterator;

	while (1) {
		int err = reftable_iterator_next_log(&ri->iter, &ri->log);
		if (err > 0) {
			return ITER_DONE;
		}
		if (err < 0) {
			return ITER_ERROR;
		}

		ri->base.refname = ri->log.refname;
		if (ri->last_name != NULL &&
		    !strcmp(ri->log.refname, ri->last_name)) {
			/* we want the refnames that we have reflogs for, so we
			 * skip if we've already produced this name. This could
			 * be faster by seeking directly to
			 * reflog@update_index==0.
			 */
			continue;
		}

		free(ri->last_name);
		ri->last_name = xstrdup(ri->log.refname);
		hashcpy(ri->oid.hash, ri->log.update.new_hash);
		return ITER_OK;
	}
}

static int
git_reftable_reflog_ref_iterator_peel(struct ref_iterator *ref_iterator,
				      struct object_id *peeled)
{
	BUG("not supported.");
	return -1;
}

static int
git_reftable_reflog_ref_iterator_abort(struct ref_iterator *ref_iterator)
{
	struct git_reftable_reflog_ref_iterator *ri =
		(struct git_reftable_reflog_ref_iterator *)ref_iterator;
	reftable_log_record_release(&ri->log);
	reftable_iterator_destroy(&ri->iter);
	if (ri->merged)
		reftable_merged_table_free(ri->merged);
	return 0;
}

static struct ref_iterator_vtable git_reftable_reflog_ref_iterator_vtable = {
	git_reftable_reflog_ref_iterator_advance,
	git_reftable_reflog_ref_iterator_peel,
	git_reftable_reflog_ref_iterator_abort
};

static struct ref_iterator *
git_reftable_reflog_iterator_begin(struct ref_store *ref_store)
{
	struct git_reftable_reflog_ref_iterator *ri = xcalloc(1, sizeof(*ri));
	struct git_reftable_ref_store *refs =
		(struct git_reftable_ref_store *)ref_store;

	if (refs->worktree_stack == NULL) {
		struct reftable_stack *stack = refs->main_stack;
		struct reftable_merged_table *mt =
			reftable_stack_merged_table(stack);
		int err = reftable_merged_table_seek_log(mt, &ri->iter, "");
		if (err < 0) {
			free(ri);
			/* XXX is this allowed? */
			return NULL;
		}
	} else {
		struct reftable_merged_table *mt1 =
			reftable_stack_merged_table(refs->main_stack);
		struct reftable_merged_table *mt2 =
			reftable_stack_merged_table(refs->worktree_stack);
		struct reftable_table *tabs =
			xcalloc(2, sizeof(struct reftable_table));
		int err = 0;
		reftable_table_from_merged_table(&tabs[0], mt1);
		reftable_table_from_merged_table(&tabs[1], mt2);
		err = reftable_new_merged_table(&ri->merged, tabs, 2,
						the_hash_algo->format_id);
		if (err < 0) {
			free(tabs);
			/* XXX see above */
			return NULL;
		}
		err = reftable_merged_table_seek_ref(ri->merged, &ri->iter, "");
		if (err < 0) {
			return NULL;
		}
	}
	base_ref_iterator_init(&ri->base,
			       &git_reftable_reflog_ref_iterator_vtable, 1);
	ri->base.oid = &ri->oid;

	return (struct ref_iterator *)ri;
}

static int git_reftable_for_each_reflog_ent_newest_first(
	struct ref_store *ref_store, const char *refname, each_reflog_ent_fn fn,
	void *cb_data)
{
	struct reftable_iterator it = { NULL };
	struct git_reftable_ref_store *refs =
		(struct git_reftable_ref_store *)ref_store;
	struct reftable_stack *stack = stack_for(refs, refname);
	struct reftable_merged_table *mt = NULL;
	int err = 0;
	struct reftable_log_record log = { NULL };

	if (refs->err < 0) {
		return refs->err;
	}

	mt = reftable_stack_merged_table(stack);
	err = reftable_merged_table_seek_log(mt, &it, refname);
	while (err == 0) {
		struct object_id old_oid;
		struct object_id new_oid;
		const char *full_committer = "";

		err = reftable_iterator_next_log(&it, &log);
		if (err > 0) {
			err = 0;
			break;
		}
		if (err < 0) {
			break;
		}

		if (strcmp(log.refname, refname)) {
			break;
		}

		hashcpy(old_oid.hash, log.update.old_hash);
		hashcpy(new_oid.hash, log.update.new_hash);

		full_committer = fmt_ident(log.update.name, log.update.email,
					   WANT_COMMITTER_IDENT,
					   /*date*/ NULL, IDENT_NO_DATE);
		err = fn(&old_oid, &new_oid, full_committer, log.update.time,
			 log.update.tz_offset, log.update.message, cb_data);
		if (err)
			break;
	}

	reftable_log_record_release(&log);
	reftable_iterator_destroy(&it);
	return err;
}

static int git_reftable_for_each_reflog_ent_oldest_first(
	struct ref_store *ref_store, const char *refname, each_reflog_ent_fn fn,
	void *cb_data)
{
	struct reftable_iterator it = { NULL };
	struct git_reftable_ref_store *refs =
		(struct git_reftable_ref_store *)ref_store;
	struct reftable_stack *stack = stack_for(refs, refname);
	struct reftable_merged_table *mt = NULL;
	struct reftable_log_record *logs = NULL;
	int cap = 0;
	int len = 0;
	int err = 0;
	int i = 0;

	if (refs->err < 0) {
		return refs->err;
	}
	mt = reftable_stack_merged_table(stack);
	err = reftable_merged_table_seek_log(mt, &it, refname);

	while (err == 0) {
		struct reftable_log_record log = { NULL };
		err = reftable_iterator_next_log(&it, &log);
		if (err > 0) {
			err = 0;
			break;
		}
		if (err < 0) {
			break;
		}

		if (strcmp(log.refname, refname)) {
			break;
		}

		if (len == cap) {
			cap = 2 * cap + 1;
			logs = realloc(logs, cap * sizeof(*logs));
		}

		logs[len++] = log;
	}

	for (i = len; i--;) {
		struct reftable_log_record *log = &logs[i];
		struct object_id old_oid;
		struct object_id new_oid;
		const char *full_committer = "";

		hashcpy(old_oid.hash, log->update.old_hash);
		hashcpy(new_oid.hash, log->update.new_hash);

		full_committer = fmt_ident(log->update.name, log->update.email,
					   WANT_COMMITTER_IDENT, NULL,
					   IDENT_NO_DATE);
		err = fn(&old_oid, &new_oid, full_committer, log->update.time,
			 log->update.tz_offset, log->update.message, cb_data);
		if (err) {
			break;
		}
	}

	for (i = 0; i < len; i++) {
		reftable_log_record_release(&logs[i]);
	}
	free(logs);

	reftable_iterator_destroy(&it);
	return err;
}

static int git_reftable_reflog_exists(struct ref_store *ref_store,
				      const char *refname)
{
	struct reftable_iterator it = { NULL };
	struct git_reftable_ref_store *refs =
		(struct git_reftable_ref_store *)ref_store;
	struct reftable_stack *stack = stack_for(refs, refname);
	struct reftable_merged_table *mt = reftable_stack_merged_table(stack);
	struct reftable_log_record log = { NULL };
	int err = refs->err;

	if (err < 0) {
		goto done;
	}
	err = reftable_merged_table_seek_log(mt, &it, refname);
	if (err) {
		goto done;
	}
	err = reftable_iterator_next_log(&it, &log);
	if (err) {
		goto done;
	}

	if (strcmp(log.refname, refname)) {
		err = 1;
	}

done:
	reftable_iterator_destroy(&it);
	reftable_log_record_release(&log);
	return !err;
}

static int git_reftable_create_reflog(struct ref_store *ref_store,
				      const char *refname, int force_create,
				      struct strbuf *err)
{
	return 0;
}

static int git_reftable_delete_reflog(struct ref_store *ref_store,
				      const char *refname)
{
	return 0;
}

struct reflog_expiry_arg {
	struct git_reftable_ref_store *refs;
	struct reftable_stack *stack;
	struct reftable_log_record *tombstones;
	int len;
	int cap;
};

static void clear_log_tombstones(struct reflog_expiry_arg *arg)
{
	int i = 0;
	for (; i < arg->len; i++) {
		reftable_log_record_release(&arg->tombstones[i]);
	}

	FREE_AND_NULL(arg->tombstones);
}

static void add_log_tombstone(struct reflog_expiry_arg *arg,
			      const char *refname, uint64_t ts)
{
	struct reftable_log_record tombstone = {
		.refname = xstrdup(refname),
		.update_index = ts,
	};
	if (arg->len == arg->cap) {
		arg->cap = 2 * arg->cap + 1;
		arg->tombstones =
			realloc(arg->tombstones, arg->cap * sizeof(tombstone));
	}
	arg->tombstones[arg->len++] = tombstone;
}

static int write_reflog_expiry_table(struct reftable_writer *writer, void *argv)
{
	struct reflog_expiry_arg *arg = (struct reflog_expiry_arg *)argv;
	uint64_t ts = reftable_stack_next_update_index(arg->stack);
	int i = 0;
	reftable_writer_set_limits(writer, ts, ts);
	for (i = 0; i < arg->len; i++) {
		int err = reftable_writer_add_log(writer, &arg->tombstones[i]);
		if (err) {
			return err;
		}
	}
	return 0;
}

static int
git_reftable_reflog_expire(struct ref_store *ref_store, const char *refname,
			   const struct object_id *oid, unsigned int flags,
			   reflog_expiry_prepare_fn prepare_fn,
			   reflog_expiry_should_prune_fn should_prune_fn,
			   reflog_expiry_cleanup_fn cleanup_fn,
			   void *policy_cb_data)
{
	/*
	  For log expiry, we write tombstones in place of the expired entries,
	  This means that the entries are still retrievable by delving into the
	  stack, and expiring entries paradoxically takes extra memory.

	  This memory is only reclaimed when some operation issues a
	  git_reftable_pack_refs(), which will compact the entire stack and get
	  rid of deletion entries.

	  It would be better if the refs backend supported an API that sets a
	  criterion for all refs, passing the criterion to pack_refs().
	*/
	struct git_reftable_ref_store *refs =
		(struct git_reftable_ref_store *)ref_store;
	struct reftable_stack *stack = stack_for(refs, refname);
	struct reftable_merged_table *mt = NULL;
	struct reflog_expiry_arg arg = {
		.stack = stack,
		.refs = refs,
	};
	struct reftable_log_record log = { NULL };
	struct reftable_iterator it = { NULL };
	int err = 0;
	if (refs->err < 0) {
		return refs->err;
	}
	err = reftable_stack_reload(stack);
	if (err) {
		goto done;
	}

	mt = reftable_stack_merged_table(stack);
	err = reftable_merged_table_seek_log(mt, &it, refname);
	if (err < 0) {
		goto done;
	}

	while (1) {
		struct object_id ooid;
		struct object_id noid;

		int err = reftable_iterator_next_log(&it, &log);
		if (err < 0) {
			goto done;
		}

		if (err > 0 || strcmp(log.refname, refname)) {
			break;
		}
		hashcpy(ooid.hash, log.update.old_hash);
		hashcpy(noid.hash, log.update.new_hash);

		if (should_prune_fn(&ooid, &noid, log.update.email,
				    (timestamp_t)log.update.time,
				    log.update.tz_offset, log.update.message,
				    policy_cb_data)) {
			add_log_tombstone(&arg, refname, log.update_index);
		}
	}
	err = reftable_stack_add(stack, &write_reflog_expiry_table, &arg);

done:
	assert(err != REFTABLE_API_ERROR);
	reftable_log_record_release(&log);
	reftable_iterator_destroy(&it);
	clear_log_tombstones(&arg);
	return err;
}

static int reftable_error_to_errno(int err)
{
	switch (err) {
	case REFTABLE_IO_ERROR:
		return EIO;
	case REFTABLE_FORMAT_ERROR:
		return EFAULT;
	case REFTABLE_NOT_EXIST_ERROR:
		return ENOENT;
	case REFTABLE_LOCK_ERROR:
		return EBUSY;
	case REFTABLE_API_ERROR:
		return EINVAL;
	case REFTABLE_ZLIB_ERROR:
		return EDOM;
	default:
		return ERANGE;
	}
}

static int git_reftable_read_raw_ref(struct ref_store *ref_store,
				     const char *refname, struct object_id *oid,
				     struct strbuf *referent,
				     unsigned int *type)
{
	struct git_reftable_ref_store *refs =
		(struct git_reftable_ref_store *)ref_store;
	struct reftable_stack *stack = stack_for(refs, refname);

	struct reftable_ref_record ref = { NULL };
	int err = 0;
	if (refs->err < 0) {
		return refs->err;
	}

	/* This is usually not needed, but Git doesn't signal to ref backend if
	   a subprocess updated the ref DB.  So we always check.
	*/
	err = reftable_stack_reload(stack);
	if (err) {
		goto done;
	}

	err = reftable_stack_read_ref(stack, refname, &ref);
	if (err > 0) {
		errno = ENOENT;
		err = -1;
		goto done;
	}
	if (err < 0) {
		errno = reftable_error_to_errno(err);
		err = -1;
		goto done;
	}

	if (ref.value_type == REFTABLE_REF_SYMREF) {
		strbuf_reset(referent);
		strbuf_addstr(referent, ref.value.symref);
		*type |= REF_ISSYMREF;
	} else if (reftable_ref_record_val1(&ref) != NULL) {
		hashcpy(oid->hash, reftable_ref_record_val1(&ref));
	} else {
		*type |= REF_ISBROKEN;
		errno = EINVAL;
		err = -1;
	}
done:
	assert(err != REFTABLE_API_ERROR);
	reftable_ref_record_release(&ref);
	return err;
}

struct ref_storage_be refs_be_reftable = {
	&refs_be_files,
	"reftable",
	git_reftable_ref_store_create,
	git_reftable_init_db,
	git_reftable_transaction_prepare,
	git_reftable_transaction_finish,
	git_reftable_transaction_abort,
	git_reftable_transaction_initial_commit,

	git_reftable_pack_refs,
	git_reftable_create_symref,
	git_reftable_delete_refs,
	git_reftable_rename_ref,
	git_reftable_copy_ref,

	git_reftable_ref_iterator_begin,
	git_reftable_read_raw_ref,

	git_reftable_reflog_iterator_begin,
	git_reftable_for_each_reflog_ent_oldest_first,
	git_reftable_for_each_reflog_ent_newest_first,
	git_reftable_reflog_exists,
	git_reftable_create_reflog,
	git_reftable_delete_reflog,
	git_reftable_reflog_expire,
};
debug log:

solving 130fd90e45b8 ...
found 130fd90e45b8 in https://public-inbox.org/git/2dc73bf2ec96acae12a17c719083d11401775bc3.1618255553.git.gitgitgadget@gmail.com/

applying [1/1] https://public-inbox.org/git/2dc73bf2ec96acae12a17c719083d11401775bc3.1618255553.git.gitgitgadget@gmail.com/
diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c
new file mode 100644
index 000000000000..130fd90e45b8

Checking patch refs/reftable-backend.c...
Applied patch refs/reftable-backend.c cleanly.

index at:
100644 130fd90e45b89c1adc38a6cce3c1c1a470fcdd0f	refs/reftable-backend.c

Code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).