Skip to content

Commit 31726bb

Browse files
KarthikNayakgitster
authored andcommitted
refs: support rejection in batch updates during F/D checks
The `refs_verify_refnames_available()` is used to batch check refnames for F/D conflicts. While this is the more performant alternative than its individual version, it does not provide rejection capabilities on a single update level. For batched updates, this would mean a rejection of the entire transaction whenever one reference has a F/D conflict. Modify the function to call `ref_transaction_maybe_set_rejected()` to check if a single update can be rejected. Since this function is only internally used within 'refs/' and we want to pass in a `struct ref_transaction *` as a variable. We also move and mark `refs_verify_refnames_available()` to 'refs-internal.h' to be an internal function. Signed-off-by: Karthik Nayak <[email protected]> Acked-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 23fc8e4 commit 31726bb

File tree

5 files changed

+76
-27
lines changed

5 files changed

+76
-27
lines changed

refs.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,13 +2540,15 @@ enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs
25402540
const struct string_list *refnames,
25412541
const struct string_list *extras,
25422542
const struct string_list *skip,
2543+
struct ref_transaction *transaction,
25432544
unsigned int initial_transaction,
25442545
struct strbuf *err)
25452546
{
25462547
struct strbuf dirname = STRBUF_INIT;
25472548
struct strbuf referent = STRBUF_INIT;
25482549
struct string_list_item *item;
25492550
struct ref_iterator *iter = NULL;
2551+
struct strset conflicting_dirnames;
25502552
struct strset dirnames;
25512553
int ret = REF_TRANSACTION_ERROR_NAME_CONFLICT;
25522554

@@ -2557,9 +2559,11 @@ enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs
25572559

25582560
assert(err);
25592561

2562+
strset_init(&conflicting_dirnames);
25602563
strset_init(&dirnames);
25612564

25622565
for_each_string_list_item(item, refnames) {
2566+
const size_t *update_idx = (size_t *)item->util;
25632567
const char *refname = item->string;
25642568
const char *extra_refname;
25652569
struct object_id oid;
@@ -2597,14 +2601,30 @@ enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs
25972601
continue;
25982602

25992603
if (!initial_transaction &&
2600-
!refs_read_raw_ref(refs, dirname.buf, &oid, &referent,
2601-
&type, &ignore_errno)) {
2604+
(strset_contains(&conflicting_dirnames, dirname.buf) ||
2605+
!refs_read_raw_ref(refs, dirname.buf, &oid, &referent,
2606+
&type, &ignore_errno))) {
2607+
if (transaction && ref_transaction_maybe_set_rejected(
2608+
transaction, *update_idx,
2609+
REF_TRANSACTION_ERROR_NAME_CONFLICT)) {
2610+
strset_remove(&dirnames, dirname.buf);
2611+
strset_add(&conflicting_dirnames, dirname.buf);
2612+
continue;
2613+
}
2614+
26022615
strbuf_addf(err, _("'%s' exists; cannot create '%s'"),
26032616
dirname.buf, refname);
26042617
goto cleanup;
26052618
}
26062619

26072620
if (extras && string_list_has_string(extras, dirname.buf)) {
2621+
if (transaction && ref_transaction_maybe_set_rejected(
2622+
transaction, *update_idx,
2623+
REF_TRANSACTION_ERROR_NAME_CONFLICT)) {
2624+
strset_remove(&dirnames, dirname.buf);
2625+
continue;
2626+
}
2627+
26082628
strbuf_addf(err, _("cannot process '%s' and '%s' at the same time"),
26092629
refname, dirname.buf);
26102630
goto cleanup;
@@ -2637,6 +2657,11 @@ enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs
26372657
string_list_has_string(skip, iter->refname))
26382658
continue;
26392659

2660+
if (transaction && ref_transaction_maybe_set_rejected(
2661+
transaction, *update_idx,
2662+
REF_TRANSACTION_ERROR_NAME_CONFLICT))
2663+
continue;
2664+
26402665
strbuf_addf(err, _("'%s' exists; cannot create '%s'"),
26412666
iter->refname, refname);
26422667
goto cleanup;
@@ -2648,6 +2673,11 @@ enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs
26482673

26492674
extra_refname = find_descendant_ref(dirname.buf, extras, skip);
26502675
if (extra_refname) {
2676+
if (transaction && ref_transaction_maybe_set_rejected(
2677+
transaction, *update_idx,
2678+
REF_TRANSACTION_ERROR_NAME_CONFLICT))
2679+
continue;
2680+
26512681
strbuf_addf(err, _("cannot process '%s' and '%s' at the same time"),
26522682
refname, extra_refname);
26532683
goto cleanup;
@@ -2659,6 +2689,7 @@ enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs
26592689
cleanup:
26602690
strbuf_release(&referent);
26612691
strbuf_release(&dirname);
2692+
strset_clear(&conflicting_dirnames);
26622693
strset_clear(&dirnames);
26632694
ref_iterator_free(iter);
26642695
return ret;
@@ -2679,7 +2710,7 @@ enum ref_transaction_error refs_verify_refname_available(
26792710
};
26802711

26812712
return refs_verify_refnames_available(refs, &refnames, extras, skip,
2682-
initial_transaction, err);
2713+
NULL, initial_transaction, err);
26832714
}
26842715

26852716
struct do_for_each_reflog_help {

refs.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,6 @@ enum ref_transaction_error refs_verify_refname_available(struct ref_store *refs,
141141
unsigned int initial_transaction,
142142
struct strbuf *err);
143143

144-
/*
145-
* Same as `refs_verify_refname_available()`, but checking for a list of
146-
* refnames instead of only a single item. This is more efficient in the case
147-
* where one needs to check multiple refnames.
148-
*/
149-
enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs,
150-
const struct string_list *refnames,
151-
const struct string_list *extras,
152-
const struct string_list *skip,
153-
unsigned int initial_transaction,
154-
struct strbuf *err);
155-
156144
int refs_ref_exists(struct ref_store *refs, const char *refname);
157145

158146
int should_autocreate_reflog(enum log_refs_config log_all_ref_updates,

refs/files-backend.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -677,16 +677,18 @@ static void unlock_ref(struct ref_lock *lock)
677677
* - Generate informative error messages in the case of failure
678678
*/
679679
static enum ref_transaction_error lock_raw_ref(struct files_ref_store *refs,
680-
const char *refname,
680+
struct ref_update *update,
681+
size_t update_idx,
681682
int mustexist,
682683
struct string_list *refnames_to_check,
683684
const struct string_list *extras,
684685
struct ref_lock **lock_p,
685686
struct strbuf *referent,
686-
unsigned int *type,
687687
struct strbuf *err)
688688
{
689689
enum ref_transaction_error ret = REF_TRANSACTION_ERROR_GENERIC;
690+
const char *refname = update->refname;
691+
unsigned int *type = &update->type;
690692
struct ref_lock *lock;
691693
struct strbuf ref_file = STRBUF_INIT;
692694
int attempts_remaining = 3;
@@ -785,6 +787,8 @@ static enum ref_transaction_error lock_raw_ref(struct files_ref_store *refs,
785787

786788
if (files_read_raw_ref(&refs->base, refname, &lock->old_oid, referent,
787789
type, &failure_errno)) {
790+
struct string_list_item *item;
791+
788792
if (failure_errno == ENOENT) {
789793
if (mustexist) {
790794
/* Garden variety missing reference. */
@@ -864,7 +868,9 @@ static enum ref_transaction_error lock_raw_ref(struct files_ref_store *refs,
864868
* make sure there is no existing packed ref that conflicts
865869
* with refname. This check is deferred so that we can batch it.
866870
*/
867-
string_list_append(refnames_to_check, refname);
871+
item = string_list_append(refnames_to_check, refname);
872+
item->util = xmalloc(sizeof(update_idx));
873+
memcpy(item->util, &update_idx, sizeof(update_idx));
868874
}
869875

870876
ret = 0;
@@ -2547,6 +2553,7 @@ struct files_transaction_backend_data {
25472553
*/
25482554
static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *refs,
25492555
struct ref_update *update,
2556+
size_t update_idx,
25502557
struct ref_transaction *transaction,
25512558
const char *head_ref,
25522559
struct string_list *refnames_to_check,
@@ -2575,9 +2582,9 @@ static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *re
25752582
if (lock) {
25762583
lock->count++;
25772584
} else {
2578-
ret = lock_raw_ref(refs, update->refname, mustexist,
2585+
ret = lock_raw_ref(refs, update, update_idx, mustexist,
25792586
refnames_to_check, &transaction->refnames,
2580-
&lock, &referent, &update->type, err);
2587+
&lock, &referent, err);
25812588
if (ret) {
25822589
char *reason;
25832590

@@ -2849,7 +2856,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,
28492856
for (i = 0; i < transaction->nr; i++) {
28502857
struct ref_update *update = transaction->updates[i];
28512858

2852-
ret = lock_ref_for_update(refs, update, transaction,
2859+
ret = lock_ref_for_update(refs, update, i, transaction,
28532860
head_ref, &refnames_to_check,
28542861
err);
28552862
if (ret) {
@@ -2905,7 +2912,8 @@ static int files_transaction_prepare(struct ref_store *ref_store,
29052912
* So instead, we accept the race for now.
29062913
*/
29072914
if (refs_verify_refnames_available(refs->packed_ref_store, &refnames_to_check,
2908-
&transaction->refnames, NULL, 0, err)) {
2915+
&transaction->refnames, NULL, transaction,
2916+
0, err)) {
29092917
ret = REF_TRANSACTION_ERROR_NAME_CONFLICT;
29102918
goto cleanup;
29112919
}
@@ -2951,7 +2959,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,
29512959

29522960
cleanup:
29532961
free(head_ref);
2954-
string_list_clear(&refnames_to_check, 0);
2962+
string_list_clear(&refnames_to_check, 1);
29552963

29562964
if (ret)
29572965
files_transaction_cleanup(refs, transaction);
@@ -3097,7 +3105,8 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,
30973105
}
30983106

30993107
if (refs_verify_refnames_available(&refs->base, &refnames_to_check,
3100-
&affected_refnames, NULL, 1, err)) {
3108+
&affected_refnames, NULL, transaction,
3109+
1, err)) {
31013110
packed_refs_unlock(refs->packed_ref_store);
31023111
ret = REF_TRANSACTION_ERROR_NAME_CONFLICT;
31033112
goto cleanup;

refs/refs-internal.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,4 +806,20 @@ enum ref_transaction_error ref_update_check_old_target(const char *referent,
806806
*/
807807
int ref_update_expects_existing_old_ref(struct ref_update *update);
808808

809+
/*
810+
* Same as `refs_verify_refname_available()`, but checking for a list of
811+
* refnames instead of only a single item. This is more efficient in the case
812+
* where one needs to check multiple refnames.
813+
*
814+
* If using batched updates, then individual updates are marked rejected,
815+
* reference backends are then in charge of not committing those updates.
816+
*/
817+
enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs,
818+
const struct string_list *refnames,
819+
const struct string_list *extras,
820+
const struct string_list *skip,
821+
struct ref_transaction *transaction,
822+
unsigned int initial_transaction,
823+
struct strbuf *err);
824+
809825
#endif /* REFS_REFS_INTERNAL_H */

refs/reftable-backend.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,7 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor
10741074
struct ref_transaction *transaction,
10751075
struct reftable_backend *be,
10761076
struct ref_update *u,
1077+
size_t update_idx,
10771078
struct string_list *refnames_to_check,
10781079
unsigned int head_type,
10791080
struct strbuf *head_referent,
@@ -1149,6 +1150,7 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor
11491150
if (ret < 0)
11501151
return REF_TRANSACTION_ERROR_GENERIC;
11511152
if (ret > 0 && !ref_update_expects_existing_old_ref(u)) {
1153+
struct string_list_item *item;
11521154
/*
11531155
* The reference does not exist, and we either have no
11541156
* old object ID or expect the reference to not exist.
@@ -1158,7 +1160,9 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor
11581160
* can output a proper error message instead of failing
11591161
* at a later point.
11601162
*/
1161-
string_list_append(refnames_to_check, u->refname);
1163+
item = string_list_append(refnames_to_check, u->refname);
1164+
item->util = xmalloc(sizeof(update_idx));
1165+
memcpy(item->util, &update_idx, sizeof(update_idx));
11621166

11631167
/*
11641168
* There is no need to write the reference deletion
@@ -1368,7 +1372,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
13681372

13691373
for (i = 0; i < transaction->nr; i++) {
13701374
ret = prepare_single_update(refs, tx_data, transaction, be,
1371-
transaction->updates[i],
1375+
transaction->updates[i], i,
13721376
&refnames_to_check, head_type,
13731377
&head_referent, &referent, err);
13741378
if (ret) {
@@ -1384,6 +1388,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
13841388

13851389
ret = refs_verify_refnames_available(ref_store, &refnames_to_check,
13861390
&transaction->refnames, NULL,
1391+
transaction,
13871392
transaction->flags & REF_TRANSACTION_FLAG_INITIAL,
13881393
err);
13891394
if (ret < 0)
@@ -1402,7 +1407,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
14021407
}
14031408
strbuf_release(&referent);
14041409
strbuf_release(&head_referent);
1405-
string_list_clear(&refnames_to_check, 0);
1410+
string_list_clear(&refnames_to_check, 1);
14061411

14071412
return ret;
14081413
}

0 commit comments

Comments
 (0)