Skip to content

Commit 72be623

Browse files
KarthikNayakgitster
authored andcommitted
refs: support partial update rejections 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 partial transactions, 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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b019de6 commit 72be623

File tree

5 files changed

+70
-25
lines changed

5 files changed

+70
-25
lines changed

refs.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2540,6 +2540,7 @@ 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
{
@@ -2559,6 +2560,7 @@ enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs
25592560
strset_init(&dirnames);
25602561

25612562
for (size_t i = 0; i < refnames->nr; i++) {
2563+
const size_t *update_idx = (size_t *)refnames->items[i].util;
25622564
const char *refname = refnames->items[i].string;
25632565
const char *extra_refname;
25642566
struct object_id oid;
@@ -2598,12 +2600,26 @@ enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs
25982600
if (!initial_transaction &&
25992601
!refs_read_raw_ref(refs, dirname.buf, &oid, &referent,
26002602
&type, &ignore_errno)) {
2603+
if (transaction && ref_transaction_maybe_set_rejected(
2604+
transaction, *update_idx,
2605+
REF_TRANSACTION_ERROR_NAME_CONFLICT)) {
2606+
strset_remove(&dirnames, dirname.buf);
2607+
continue;
2608+
}
2609+
26012610
strbuf_addf(err, _("'%s' exists; cannot create '%s'"),
26022611
dirname.buf, refname);
26032612
goto cleanup;
26042613
}
26052614

26062615
if (extras && string_list_has_string(extras, dirname.buf)) {
2616+
if (transaction && ref_transaction_maybe_set_rejected(
2617+
transaction, *update_idx,
2618+
REF_TRANSACTION_ERROR_NAME_CONFLICT)) {
2619+
strset_remove(&dirnames, dirname.buf);
2620+
continue;
2621+
}
2622+
26072623
strbuf_addf(err, _("cannot process '%s' and '%s' at the same time"),
26082624
refname, dirname.buf);
26092625
goto cleanup;
@@ -2636,6 +2652,11 @@ enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs
26362652
string_list_has_string(skip, iter->refname))
26372653
continue;
26382654

2655+
if (transaction && ref_transaction_maybe_set_rejected(
2656+
transaction, *update_idx,
2657+
REF_TRANSACTION_ERROR_NAME_CONFLICT))
2658+
continue;
2659+
26392660
strbuf_addf(err, _("'%s' exists; cannot create '%s'"),
26402661
iter->refname, refname);
26412662
goto cleanup;
@@ -2647,6 +2668,11 @@ enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs
26472668

26482669
extra_refname = find_descendant_ref(dirname.buf, extras, skip);
26492670
if (extra_refname) {
2671+
if (transaction && ref_transaction_maybe_set_rejected(
2672+
transaction, *update_idx,
2673+
REF_TRANSACTION_ERROR_NAME_CONFLICT))
2674+
continue;
2675+
26502676
strbuf_addf(err, _("cannot process '%s' and '%s' at the same time"),
26512677
refname, extra_refname);
26522678
goto cleanup;
@@ -2678,7 +2704,7 @@ enum ref_transaction_error refs_verify_refname_available(
26782704
};
26792705

26802706
return refs_verify_refnames_available(refs, &refnames, extras, skip,
2681-
initial_transaction, err);
2707+
NULL, initial_transaction, err);
26822708
}
26832709

26842710
struct do_for_each_reflog_help {

refs.h

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

150-
/*
151-
* Same as `refs_verify_refname_available()`, but checking for a list of
152-
* refnames instead of only a single item. This is more efficient in the case
153-
* where one needs to check multiple refnames.
154-
*/
155-
enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs,
156-
const struct string_list *refnames,
157-
const struct string_list *extras,
158-
const struct string_list *skip,
159-
unsigned int initial_transaction,
160-
struct strbuf *err);
161-
162150
int refs_ref_exists(struct ref_store *refs, const char *refname);
163151

164152
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_insert(refnames_to_check, refname);
871+
item = string_list_insert(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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,4 +805,21 @@ enum ref_transaction_error ref_update_check_old_target(const char *referent,
805805
*/
806806
int ref_update_expects_existing_old_ref(struct ref_update *update);
807807

808+
/*
809+
* Same as `refs_verify_refname_available()`, but checking for a list of
810+
* refnames instead of only a single item. This is more efficient in the case
811+
* where one needs to check multiple refnames.
812+
*
813+
* If a transaction is provided with partial support, then individual updates
814+
* are marked rejected, reference backends are then in charge of not committing
815+
* 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+
808825
#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) {
@@ -1385,6 +1389,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
13851389
string_list_sort(&refnames_to_check);
13861390
ret = refs_verify_refnames_available(ref_store, &refnames_to_check,
13871391
&transaction->refnames, NULL,
1392+
transaction,
13881393
transaction->flags & REF_TRANSACTION_FLAG_INITIAL,
13891394
err);
13901395
if (ret < 0)
@@ -1403,7 +1408,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
14031408
}
14041409
strbuf_release(&referent);
14051410
strbuf_release(&head_referent);
1406-
string_list_clear(&refnames_to_check, 0);
1411+
string_list_clear(&refnames_to_check, 1);
14071412

14081413
return ret;
14091414
}

0 commit comments

Comments
 (0)