-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreftable-backend.c
2333 lines (2009 loc) · 64.7 KB
/
reftable-backend.c
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
#define USE_THE_REPOSITORY_VARIABLE
#include "../git-compat-util.h"
#include "../abspath.h"
#include "../chdir-notify.h"
#include "../config.h"
#include "../dir.h"
#include "../environment.h"
#include "../gettext.h"
#include "../hash.h"
#include "../hex.h"
#include "../iterator.h"
#include "../ident.h"
#include "../lockfile.h"
#include "../object.h"
#include "../path.h"
#include "../refs.h"
#include "../reftable/reftable-stack.h"
#include "../reftable/reftable-record.h"
#include "../reftable/reftable-error.h"
#include "../reftable/reftable-iterator.h"
#include "../setup.h"
#include "../strmap.h"
#include "parse.h"
#include "refs-internal.h"
/*
* Used as a flag in ref_update::flags when the ref_update was via an
* update to HEAD.
*/
#define REF_UPDATE_VIA_HEAD (1 << 8)
struct reftable_ref_store {
struct ref_store base;
/*
* The main stack refers to the common dir and thus contains common
* refs as well as refs of the main repository.
*/
struct reftable_stack *main_stack;
/*
* The worktree stack refers to the gitdir in case the refdb is opened
* via a worktree. It thus contains the per-worktree refs.
*/
struct reftable_stack *worktree_stack;
/*
* Map of worktree stacks by their respective worktree names. The map
* is populated lazily when we try to resolve `worktrees/$worktree` refs.
*/
struct strmap worktree_stacks;
struct reftable_write_options write_options;
unsigned int store_flags;
int err;
};
/*
* Downcast ref_store to reftable_ref_store. Die if ref_store is not a
* reftable_ref_store. required_flags is compared with ref_store's store_flags
* to ensure the ref_store has all required capabilities. "caller" is used in
* any necessary error messages.
*/
static struct reftable_ref_store *reftable_be_downcast(struct ref_store *ref_store,
unsigned int required_flags,
const char *caller)
{
struct reftable_ref_store *refs;
if (ref_store->be != &refs_be_reftable)
BUG("ref_store is type \"%s\" not \"reftables\" in %s",
ref_store->be->name, caller);
refs = (struct reftable_ref_store *)ref_store;
if ((refs->store_flags & required_flags) != required_flags)
BUG("operation %s requires abilities 0x%x, but only have 0x%x",
caller, required_flags, refs->store_flags);
return refs;
}
/*
* Some refs are global to the repository (refs/heads/{*}), while others are
* local to the worktree (eg. HEAD, refs/bisect/{*}). We solve this by having
* multiple separate databases (ie. multiple reftable/ directories), one for
* the shared refs, one for the current worktree refs, and one for each
* additional worktree. For reading, we merge the view of both the shared and
* the current worktree's refs, when necessary.
*
* This function also optionally assigns the rewritten reference name that is
* local to the stack. This translation is required when using worktree refs
* like `worktrees/$worktree/refs/heads/foo` as worktree stacks will store
* those references in their normalized form.
*/
static struct reftable_stack *stack_for(struct reftable_ref_store *store,
const char *refname,
const char **rewritten_ref)
{
const char *wtname;
int wtname_len;
if (!refname)
return store->main_stack;
switch (parse_worktree_ref(refname, &wtname, &wtname_len, rewritten_ref)) {
case REF_WORKTREE_OTHER: {
static struct strbuf wtname_buf = STRBUF_INIT;
struct strbuf wt_dir = STRBUF_INIT;
struct reftable_stack *stack;
/*
* We're using a static buffer here so that we don't need to
* allocate the worktree name whenever we look up a reference.
* This could be avoided if the strmap interface knew how to
* handle keys with a length.
*/
strbuf_reset(&wtname_buf);
strbuf_add(&wtname_buf, wtname, wtname_len);
/*
* There is an edge case here: when the worktree references the
* current worktree, then we set up the stack once via
* `worktree_stacks` and once via `worktree_stack`. This is
* wasteful, but in the reading case it shouldn't matter. And
* in the writing case we would notice that the stack is locked
* already and error out when trying to write a reference via
* both stacks.
*/
stack = strmap_get(&store->worktree_stacks, wtname_buf.buf);
if (!stack) {
strbuf_addf(&wt_dir, "%s/worktrees/%s/reftable",
store->base.repo->commondir, wtname_buf.buf);
store->err = reftable_new_stack(&stack, wt_dir.buf,
&store->write_options);
assert(store->err != REFTABLE_API_ERROR);
strmap_put(&store->worktree_stacks, wtname_buf.buf, stack);
}
strbuf_release(&wt_dir);
return stack;
}
case REF_WORKTREE_CURRENT:
/*
* If there is no worktree stack then we're currently in the
* main worktree. We thus return the main stack in that case.
*/
if (!store->worktree_stack)
return store->main_stack;
return store->worktree_stack;
case REF_WORKTREE_MAIN:
case REF_WORKTREE_SHARED:
return store->main_stack;
default:
BUG("unhandled worktree reference type");
}
}
static int should_write_log(struct ref_store *refs, const char *refname)
{
if (log_all_ref_updates == LOG_REFS_UNSET)
log_all_ref_updates = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
switch (log_all_ref_updates) {
case LOG_REFS_NONE:
return refs_reflog_exists(refs, refname);
case LOG_REFS_ALWAYS:
return 1;
case LOG_REFS_NORMAL:
if (should_autocreate_reflog(refname))
return 1;
return refs_reflog_exists(refs, refname);
default:
BUG("unhandled core.logAllRefUpdates value %d", log_all_ref_updates);
}
}
static void fill_reftable_log_record(struct reftable_log_record *log, const struct ident_split *split)
{
const char *tz_begin;
int sign = 1;
reftable_log_record_release(log);
log->value_type = REFTABLE_LOG_UPDATE;
log->value.update.name =
xstrndup(split->name_begin, split->name_end - split->name_begin);
log->value.update.email =
xstrndup(split->mail_begin, split->mail_end - split->mail_begin);
log->value.update.time = atol(split->date_begin);
tz_begin = split->tz_begin;
if (*tz_begin == '-') {
sign = -1;
tz_begin++;
}
if (*tz_begin == '+') {
sign = 1;
tz_begin++;
}
log->value.update.tz_offset = sign * atoi(tz_begin);
}
static int read_ref_without_reload(struct reftable_stack *stack,
const char *refname,
struct object_id *oid,
struct strbuf *referent,
unsigned int *type)
{
struct reftable_ref_record ref = {0};
int ret;
ret = reftable_stack_read_ref(stack, refname, &ref);
if (ret)
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)) {
oidread(oid, reftable_ref_record_val1(&ref),
the_repository->hash_algo);
} else {
/* We got a tombstone, which should not happen. */
BUG("unhandled reference value type %d", ref.value_type);
}
done:
assert(ret != REFTABLE_API_ERROR);
reftable_ref_record_release(&ref);
return ret;
}
static int reftable_be_config(const char *var, const char *value,
const struct config_context *ctx,
void *_opts)
{
struct reftable_write_options *opts = _opts;
if (!strcmp(var, "reftable.blocksize")) {
unsigned long block_size = git_config_ulong(var, value, ctx->kvi);
if (block_size > 16777215)
die("reftable block size cannot exceed 16MB");
opts->block_size = block_size;
} else if (!strcmp(var, "reftable.restartinterval")) {
unsigned long restart_interval = git_config_ulong(var, value, ctx->kvi);
if (restart_interval > UINT16_MAX)
die("reftable block size cannot exceed %u", (unsigned)UINT16_MAX);
opts->restart_interval = restart_interval;
} else if (!strcmp(var, "reftable.indexobjects")) {
opts->skip_index_objects = !git_config_bool(var, value);
} else if (!strcmp(var, "reftable.geometricfactor")) {
unsigned long factor = git_config_ulong(var, value, ctx->kvi);
if (factor > UINT8_MAX)
die("reftable geometric factor cannot exceed %u", (unsigned)UINT8_MAX);
opts->auto_compaction_factor = factor;
}
return 0;
}
static struct ref_store *reftable_be_init(struct repository *repo,
const char *gitdir,
unsigned int store_flags)
{
struct reftable_ref_store *refs = xcalloc(1, sizeof(*refs));
struct strbuf path = STRBUF_INIT;
int is_worktree;
mode_t mask;
mask = umask(0);
umask(mask);
base_ref_store_init(&refs->base, repo, gitdir, &refs_be_reftable);
strmap_init(&refs->worktree_stacks);
refs->store_flags = store_flags;
refs->write_options.hash_id = repo->hash_algo->format_id;
refs->write_options.default_permissions = calc_shared_perm(0666 & ~mask);
refs->write_options.disable_auto_compact =
!git_env_bool("GIT_TEST_REFTABLE_AUTOCOMPACTION", 1);
git_config(reftable_be_config, &refs->write_options);
/*
* It is somewhat unfortunate that we have to mirror the default block
* size of the reftable library here. But given that the write options
* wouldn't be updated by the library here, and given that we require
* the proper block size to trim reflog message so that they fit, we
* must set up a proper value here.
*/
if (!refs->write_options.block_size)
refs->write_options.block_size = 4096;
/*
* Set up the main reftable stack that is hosted in GIT_COMMON_DIR.
* This stack contains both the shared and the main worktree refs.
*
* Note that we don't try to resolve the path in case we have a
* worktree because `get_common_dir_noenv()` already does it for us.
*/
is_worktree = get_common_dir_noenv(&path, gitdir);
if (!is_worktree) {
strbuf_reset(&path);
strbuf_realpath(&path, gitdir, 0);
}
strbuf_addstr(&path, "/reftable");
refs->err = reftable_new_stack(&refs->main_stack, path.buf,
&refs->write_options);
if (refs->err)
goto done;
/*
* If we're in a worktree we also need to set up the worktree reftable
* stack that is contained in the per-worktree GIT_DIR.
*
* Ideally, we would also add the stack to our worktree stack map. But
* we have no way to figure out the worktree name here and thus can't
* do it efficiently.
*/
if (is_worktree) {
strbuf_reset(&path);
strbuf_addf(&path, "%s/reftable", gitdir);
refs->err = reftable_new_stack(&refs->worktree_stack, path.buf,
&refs->write_options);
if (refs->err)
goto done;
}
chdir_notify_reparent("reftables-backend $GIT_DIR", &refs->base.gitdir);
done:
assert(refs->err != REFTABLE_API_ERROR);
strbuf_release(&path);
return &refs->base;
}
static void reftable_be_release(struct ref_store *ref_store)
{
struct reftable_ref_store *refs = reftable_be_downcast(ref_store, 0, "release");
struct strmap_entry *entry;
struct hashmap_iter iter;
if (refs->main_stack) {
reftable_stack_destroy(refs->main_stack);
refs->main_stack = NULL;
}
if (refs->worktree_stack) {
reftable_stack_destroy(refs->worktree_stack);
refs->worktree_stack = NULL;
}
strmap_for_each_entry(&refs->worktree_stacks, &iter, entry)
reftable_stack_destroy(entry->value);
strmap_clear(&refs->worktree_stacks, 0);
}
static int reftable_be_create_on_disk(struct ref_store *ref_store,
int flags UNUSED,
struct strbuf *err UNUSED)
{
struct reftable_ref_store *refs =
reftable_be_downcast(ref_store, REF_STORE_WRITE, "create");
struct strbuf sb = STRBUF_INIT;
strbuf_addf(&sb, "%s/reftable", refs->base.gitdir);
safe_create_dir(sb.buf, 1);
strbuf_reset(&sb);
strbuf_addf(&sb, "%s/HEAD", refs->base.gitdir);
write_file(sb.buf, "ref: refs/heads/.invalid");
adjust_shared_perm(sb.buf);
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");
adjust_shared_perm(sb.buf);
strbuf_release(&sb);
return 0;
}
static int reftable_be_remove_on_disk(struct ref_store *ref_store,
struct strbuf *err)
{
struct reftable_ref_store *refs =
reftable_be_downcast(ref_store, REF_STORE_WRITE, "remove");
struct strbuf sb = STRBUF_INIT;
int ret = 0;
/*
* Release the ref store such that all stacks are closed. This is
* required so that the "tables.list" file is not open anymore, which
* would otherwise make it impossible to remove the file on Windows.
*/
reftable_be_release(ref_store);
strbuf_addf(&sb, "%s/reftable", refs->base.gitdir);
if (remove_dir_recursively(&sb, 0) < 0) {
strbuf_addf(err, "could not delete reftables: %s",
strerror(errno));
ret = -1;
}
strbuf_reset(&sb);
strbuf_addf(&sb, "%s/HEAD", refs->base.gitdir);
if (unlink(sb.buf) < 0) {
strbuf_addf(err, "could not delete stub HEAD: %s",
strerror(errno));
ret = -1;
}
strbuf_reset(&sb);
strbuf_addf(&sb, "%s/refs/heads", refs->base.gitdir);
if (unlink(sb.buf) < 0) {
strbuf_addf(err, "could not delete stub heads: %s",
strerror(errno));
ret = -1;
}
strbuf_reset(&sb);
strbuf_addf(&sb, "%s/refs", refs->base.gitdir);
if (rmdir(sb.buf) < 0) {
strbuf_addf(err, "could not delete refs directory: %s",
strerror(errno));
ret = -1;
}
strbuf_release(&sb);
return ret;
}
struct reftable_ref_iterator {
struct ref_iterator base;
struct reftable_ref_store *refs;
struct reftable_iterator iter;
struct reftable_ref_record ref;
struct object_id oid;
const char *prefix;
size_t prefix_len;
unsigned int flags;
int err;
};
static int reftable_ref_iterator_advance(struct ref_iterator *ref_iterator)
{
struct reftable_ref_iterator *iter =
(struct reftable_ref_iterator *)ref_iterator;
struct reftable_ref_store *refs = iter->refs;
while (!iter->err) {
int flags = 0;
iter->err = reftable_iterator_next_ref(&iter->iter, &iter->ref);
if (iter->err)
break;
/*
* The files backend only lists references contained in "refs/" unless
* the root refs are to be included. We emulate the same behaviour here.
*/
if (!starts_with(iter->ref.refname, "refs/") &&
!(iter->flags & DO_FOR_EACH_INCLUDE_ROOT_REFS &&
is_root_ref(iter->ref.refname))) {
continue;
}
if (iter->prefix_len &&
strncmp(iter->prefix, iter->ref.refname, iter->prefix_len)) {
iter->err = 1;
break;
}
if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
parse_worktree_ref(iter->ref.refname, NULL, NULL, NULL) !=
REF_WORKTREE_CURRENT)
continue;
switch (iter->ref.value_type) {
case REFTABLE_REF_VAL1:
oidread(&iter->oid, iter->ref.value.val1,
the_repository->hash_algo);
break;
case REFTABLE_REF_VAL2:
oidread(&iter->oid, iter->ref.value.val2.value,
the_repository->hash_algo);
break;
case REFTABLE_REF_SYMREF:
if (!refs_resolve_ref_unsafe(&iter->refs->base, iter->ref.refname,
RESOLVE_REF_READING, &iter->oid, &flags))
oidclr(&iter->oid, the_repository->hash_algo);
break;
default:
BUG("unhandled reference value type %d", iter->ref.value_type);
}
if (is_null_oid(&iter->oid))
flags |= REF_ISBROKEN;
if (check_refname_format(iter->ref.refname, REFNAME_ALLOW_ONELEVEL)) {
if (!refname_is_safe(iter->ref.refname))
die(_("refname is dangerous: %s"), iter->ref.refname);
oidclr(&iter->oid, the_repository->hash_algo);
flags |= REF_BAD_NAME | REF_ISBROKEN;
}
if (iter->flags & DO_FOR_EACH_OMIT_DANGLING_SYMREFS &&
flags & REF_ISSYMREF &&
flags & REF_ISBROKEN)
continue;
if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
!ref_resolves_to_object(iter->ref.refname, refs->base.repo,
&iter->oid, flags))
continue;
iter->base.refname = iter->ref.refname;
iter->base.oid = &iter->oid;
iter->base.flags = flags;
break;
}
if (iter->err > 0) {
if (ref_iterator_abort(ref_iterator) != ITER_DONE)
return ITER_ERROR;
return ITER_DONE;
}
if (iter->err < 0) {
ref_iterator_abort(ref_iterator);
return ITER_ERROR;
}
return ITER_OK;
}
static int reftable_ref_iterator_peel(struct ref_iterator *ref_iterator,
struct object_id *peeled)
{
struct reftable_ref_iterator *iter =
(struct reftable_ref_iterator *)ref_iterator;
if (iter->ref.value_type == REFTABLE_REF_VAL2) {
oidread(peeled, iter->ref.value.val2.target_value,
the_repository->hash_algo);
return 0;
}
return -1;
}
static int reftable_ref_iterator_abort(struct ref_iterator *ref_iterator)
{
struct reftable_ref_iterator *iter =
(struct reftable_ref_iterator *)ref_iterator;
reftable_ref_record_release(&iter->ref);
reftable_iterator_destroy(&iter->iter);
free(iter);
return ITER_DONE;
}
static struct ref_iterator_vtable reftable_ref_iterator_vtable = {
.advance = reftable_ref_iterator_advance,
.peel = reftable_ref_iterator_peel,
.abort = reftable_ref_iterator_abort
};
static struct reftable_ref_iterator *ref_iterator_for_stack(struct reftable_ref_store *refs,
struct reftable_stack *stack,
const char *prefix,
int flags)
{
struct reftable_ref_iterator *iter;
int ret;
iter = xcalloc(1, sizeof(*iter));
base_ref_iterator_init(&iter->base, &reftable_ref_iterator_vtable);
iter->prefix = prefix;
iter->prefix_len = prefix ? strlen(prefix) : 0;
iter->base.oid = &iter->oid;
iter->flags = flags;
iter->refs = refs;
ret = refs->err;
if (ret)
goto done;
ret = reftable_stack_reload(stack);
if (ret)
goto done;
reftable_stack_init_ref_iterator(stack, &iter->iter);
ret = reftable_iterator_seek_ref(&iter->iter, prefix);
if (ret)
goto done;
done:
iter->err = ret;
return iter;
}
static struct ref_iterator *reftable_be_iterator_begin(struct ref_store *ref_store,
const char *prefix,
const char **exclude_patterns,
unsigned int flags)
{
struct reftable_ref_iterator *main_iter, *worktree_iter;
struct reftable_ref_store *refs;
unsigned int required_flags = REF_STORE_READ;
if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
required_flags |= REF_STORE_ODB;
refs = reftable_be_downcast(ref_store, required_flags, "ref_iterator_begin");
main_iter = ref_iterator_for_stack(refs, refs->main_stack, prefix, flags);
/*
* The worktree stack is only set when we're in an actual worktree
* right now. If we aren't, then we return the common reftable
* iterator, only.
*/
if (!refs->worktree_stack)
return &main_iter->base;
/*
* Otherwise we merge both the common and the per-worktree refs into a
* single iterator.
*/
worktree_iter = ref_iterator_for_stack(refs, refs->worktree_stack, prefix, flags);
return merge_ref_iterator_begin(&worktree_iter->base, &main_iter->base,
ref_iterator_select, NULL);
}
static int reftable_be_read_raw_ref(struct ref_store *ref_store,
const char *refname,
struct object_id *oid,
struct strbuf *referent,
unsigned int *type,
int *failure_errno)
{
struct reftable_ref_store *refs =
reftable_be_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
struct reftable_stack *stack = stack_for(refs, refname, &refname);
int ret;
if (refs->err < 0)
return refs->err;
ret = reftable_stack_reload(stack);
if (ret)
return ret;
ret = read_ref_without_reload(stack, refname, oid, referent, type);
if (ret < 0)
return ret;
if (ret > 0) {
*failure_errno = ENOENT;
return -1;
}
return 0;
}
static int reftable_be_read_symbolic_ref(struct ref_store *ref_store,
const char *refname,
struct strbuf *referent)
{
struct reftable_ref_store *refs =
reftable_be_downcast(ref_store, REF_STORE_READ, "read_symbolic_ref");
struct reftable_stack *stack = stack_for(refs, refname, &refname);
struct reftable_ref_record ref = {0};
int ret;
ret = reftable_stack_reload(stack);
if (ret)
return ret;
ret = reftable_stack_read_ref(stack, refname, &ref);
if (ret == 0 && ref.value_type == REFTABLE_REF_SYMREF)
strbuf_addstr(referent, ref.value.symref);
else
ret = -1;
reftable_ref_record_release(&ref);
return ret;
}
struct reftable_transaction_update {
struct ref_update *update;
struct object_id current_oid;
};
struct write_transaction_table_arg {
struct reftable_ref_store *refs;
struct reftable_stack *stack;
struct reftable_addition *addition;
struct reftable_transaction_update *updates;
size_t updates_nr;
size_t updates_alloc;
size_t updates_expected;
};
struct reftable_transaction_data {
struct write_transaction_table_arg *args;
size_t args_nr, args_alloc;
};
static void free_transaction_data(struct reftable_transaction_data *tx_data)
{
if (!tx_data)
return;
for (size_t i = 0; i < tx_data->args_nr; i++) {
reftable_addition_destroy(tx_data->args[i].addition);
free(tx_data->args[i].updates);
}
free(tx_data->args);
free(tx_data);
}
/*
* Prepare transaction update for the given reference update. This will cause
* us to lock the corresponding reftable stack for concurrent modification.
*/
static int prepare_transaction_update(struct write_transaction_table_arg **out,
struct reftable_ref_store *refs,
struct reftable_transaction_data *tx_data,
struct ref_update *update,
struct strbuf *err)
{
struct reftable_stack *stack = stack_for(refs, update->refname, NULL);
struct write_transaction_table_arg *arg = NULL;
size_t i;
int ret;
/*
* Search for a preexisting stack update. If there is one then we add
* the update to it, otherwise we set up a new stack update.
*/
for (i = 0; !arg && i < tx_data->args_nr; i++)
if (tx_data->args[i].stack == stack)
arg = &tx_data->args[i];
if (!arg) {
struct reftable_addition *addition;
ret = reftable_stack_reload(stack);
if (ret)
return ret;
ret = reftable_stack_new_addition(&addition, stack);
if (ret) {
if (ret == REFTABLE_LOCK_ERROR)
strbuf_addstr(err, "cannot lock references");
return ret;
}
ALLOC_GROW(tx_data->args, tx_data->args_nr + 1,
tx_data->args_alloc);
arg = &tx_data->args[tx_data->args_nr++];
arg->refs = refs;
arg->stack = stack;
arg->addition = addition;
arg->updates = NULL;
arg->updates_nr = 0;
arg->updates_alloc = 0;
arg->updates_expected = 0;
}
arg->updates_expected++;
if (out)
*out = arg;
return 0;
}
/*
* Queue a reference update for the correct stack. We potentially need to
* handle multiple stack updates in a single transaction when it spans across
* multiple worktrees.
*/
static int queue_transaction_update(struct reftable_ref_store *refs,
struct reftable_transaction_data *tx_data,
struct ref_update *update,
struct object_id *current_oid,
struct strbuf *err)
{
struct write_transaction_table_arg *arg = NULL;
int ret;
if (update->backend_data)
BUG("reference update queued more than once");
ret = prepare_transaction_update(&arg, refs, tx_data, update, err);
if (ret < 0)
return ret;
ALLOC_GROW(arg->updates, arg->updates_nr + 1,
arg->updates_alloc);
arg->updates[arg->updates_nr].update = update;
oidcpy(&arg->updates[arg->updates_nr].current_oid, current_oid);
update->backend_data = &arg->updates[arg->updates_nr++];
return 0;
}
static int reftable_be_transaction_prepare(struct ref_store *ref_store,
struct ref_transaction *transaction,
struct strbuf *err)
{
struct reftable_ref_store *refs =
reftable_be_downcast(ref_store, REF_STORE_WRITE|REF_STORE_MAIN, "ref_transaction_prepare");
struct strbuf referent = STRBUF_INIT, head_referent = STRBUF_INIT;
struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
struct reftable_transaction_data *tx_data = NULL;
struct object_id head_oid;
unsigned int head_type = 0;
size_t i;
int ret;
ret = refs->err;
if (ret < 0)
goto done;
tx_data = xcalloc(1, sizeof(*tx_data));
/*
* Preprocess all updates. For one we check that there are no duplicate
* reference updates in this transaction. Second, we lock all stacks
* that will be modified during the transaction.
*/
for (i = 0; i < transaction->nr; i++) {
ret = prepare_transaction_update(NULL, refs, tx_data,
transaction->updates[i], err);
if (ret)
goto done;
string_list_append(&affected_refnames,
transaction->updates[i]->refname);
}
/*
* Now that we have counted updates per stack we can preallocate their
* arrays. This avoids having to reallocate many times.
*/
for (i = 0; i < tx_data->args_nr; i++) {
CALLOC_ARRAY(tx_data->args[i].updates, tx_data->args[i].updates_expected);
tx_data->args[i].updates_alloc = tx_data->args[i].updates_expected;
}
/*
* Fail if a refname appears more than once in the transaction.
* This code is taken from the files backend and is a good candidate to
* be moved into the generic layer.
*/
string_list_sort(&affected_refnames);
if (ref_update_reject_duplicates(&affected_refnames, err)) {
ret = TRANSACTION_GENERIC_ERROR;
goto done;
}
ret = read_ref_without_reload(stack_for(refs, "HEAD", NULL), "HEAD", &head_oid,
&head_referent, &head_type);
if (ret < 0)
goto done;
ret = 0;
for (i = 0; i < transaction->nr; i++) {
struct ref_update *u = transaction->updates[i];
struct object_id current_oid = {0};
struct reftable_stack *stack;
const char *rewritten_ref;
stack = stack_for(refs, u->refname, &rewritten_ref);
/* Verify that the new object ID is valid. */
if ((u->flags & REF_HAVE_NEW) && !is_null_oid(&u->new_oid) &&
!(u->flags & REF_SKIP_OID_VERIFICATION) &&
!(u->flags & REF_LOG_ONLY)) {
struct object *o = parse_object(refs->base.repo, &u->new_oid);
if (!o) {
strbuf_addf(err,
_("trying to write ref '%s' with nonexistent object %s"),
u->refname, oid_to_hex(&u->new_oid));
ret = -1;
goto done;
}
if (o->type != OBJ_COMMIT && is_branch(u->refname)) {
strbuf_addf(err, _("trying to write non-commit object %s to branch '%s'"),
oid_to_hex(&u->new_oid), u->refname);
ret = -1;
goto done;
}
}
/*
* When we update the reference that HEAD points to we enqueue
* a second log-only update for HEAD so that its reflog is
* updated accordingly.
*/
if (head_type == REF_ISSYMREF &&
!(u->flags & REF_LOG_ONLY) &&
!(u->flags & REF_UPDATE_VIA_HEAD) &&
!strcmp(rewritten_ref, head_referent.buf)) {
struct ref_update *new_update;
/*
* First make sure that HEAD is not already in the
* transaction. This check is O(lg N) in the transaction
* size, but it happens at most once per transaction.
*/
if (string_list_has_string(&affected_refnames, "HEAD")) {
/* An entry already existed */
strbuf_addf(err,
_("multiple updates for 'HEAD' (including one "
"via its referent '%s') are not allowed"),
u->refname);
ret = TRANSACTION_NAME_CONFLICT;
goto done;
}
new_update = ref_transaction_add_update(
transaction, "HEAD",
u->flags | REF_LOG_ONLY | REF_NO_DEREF,
&u->new_oid, &u->old_oid, NULL, NULL, u->msg);
string_list_insert(&affected_refnames, new_update->refname);
}
ret = read_ref_without_reload(stack, rewritten_ref,
¤t_oid, &referent, &u->type);
if (ret < 0)
goto done;
if (ret > 0 && !ref_update_expects_existing_old_ref(u)) {
/*
* The reference does not exist, and we either have no
* old object ID or expect the reference to not exist.
* We can thus skip below safety checks as well as the
* symref splitting. But we do want to verify that
* there is no conflicting reference here so that we
* can output a proper error message instead of failing
* at a later point.
*/
ret = refs_verify_refname_available(ref_store, u->refname,
&affected_refnames, NULL, err);
if (ret < 0)
goto done;
/*
* There is no need to write the reference deletion
* when the reference in question doesn't exist.
*/
if ((u->flags & REF_HAVE_NEW) && !ref_update_has_null_new_value(u)) {
ret = queue_transaction_update(refs, tx_data, u,
¤t_oid, err);
if (ret)
goto done;
}
continue;
}
if (ret > 0) {
/* The reference does not exist, but we expected it to. */
strbuf_addf(err, _("cannot lock ref '%s': "
"unable to resolve reference '%s'"),
ref_update_original_update_refname(u), u->refname);
ret = -1;
goto done;
}
if (u->type & REF_ISSYMREF) {
/*
* The reftable stack is locked at this point already,
* so it is safe to call `refs_resolve_ref_unsafe()`
* here without causing races.
*/
const char *resolved = refs_resolve_ref_unsafe(&refs->base, u->refname, 0,
¤t_oid, NULL);
if (u->flags & REF_NO_DEREF) {
if (u->flags & REF_HAVE_OLD && !resolved) {
strbuf_addf(err, _("cannot lock ref '%s': "
"error reading reference"), u->refname);
ret = -1;
goto done;
}
} else {
struct ref_update *new_update;
int new_flags;
new_flags = u->flags;