Skip to content

Commit 31d287c

Browse files
ferdinandybttaylorr
authored andcommitted
refs: add TRANSACTION_CREATE_EXISTS error
Currently there is only one special error for transaction, for when there is a naming conflict, all other errors are dumped under a generic error. Add a new special error case for when the caller requests the reference to be updated only when it does not yet exist and the reference actually does exist. Signed-off-by: Bence Ferdinandy <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent c72cc52 commit 31d287c

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

refs.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,8 +762,10 @@ int ref_transaction_verify(struct ref_transaction *transaction,
762762

763763
/* Naming conflict (for example, the ref names A and A/B conflict). */
764764
#define TRANSACTION_NAME_CONFLICT -1
765+
/* When only creation was requested, but the ref already exists. */
766+
#define TRANSACTION_CREATE_EXISTS -2
765767
/* All other errors. */
766-
#define TRANSACTION_GENERIC_ERROR -2
768+
#define TRANSACTION_GENERIC_ERROR -3
767769

768770
/*
769771
* Perform the preparatory stages of committing `transaction`. Acquire

refs/files-backend.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,14 +2502,18 @@ static int split_symref_update(struct ref_update *update,
25022502
static int check_old_oid(struct ref_update *update, struct object_id *oid,
25032503
struct strbuf *err)
25042504
{
2505+
int ret = TRANSACTION_GENERIC_ERROR;
2506+
25052507
if (!(update->flags & REF_HAVE_OLD) ||
25062508
oideq(oid, &update->old_oid))
25072509
return 0;
25082510

2509-
if (is_null_oid(&update->old_oid))
2511+
if (is_null_oid(&update->old_oid)) {
25102512
strbuf_addf(err, "cannot lock ref '%s': "
25112513
"reference already exists",
25122514
ref_update_original_update_refname(update));
2515+
ret = TRANSACTION_CREATE_EXISTS;
2516+
}
25132517
else if (is_null_oid(oid))
25142518
strbuf_addf(err, "cannot lock ref '%s': "
25152519
"reference is missing but expected %s",
@@ -2522,7 +2526,7 @@ static int check_old_oid(struct ref_update *update, struct object_id *oid,
25222526
oid_to_hex(oid),
25232527
oid_to_hex(&update->old_oid));
25242528

2525-
return -1;
2529+
return ret;
25262530
}
25272531

25282532
/*
@@ -2602,9 +2606,11 @@ static int lock_ref_for_update(struct files_ref_store *refs,
26022606
ret = TRANSACTION_GENERIC_ERROR;
26032607
goto out;
26042608
}
2605-
} else if (check_old_oid(update, &lock->old_oid, err)) {
2606-
ret = TRANSACTION_GENERIC_ERROR;
2607-
goto out;
2609+
} else {
2610+
ret = check_old_oid(update, &lock->old_oid, err);
2611+
if (ret) {
2612+
goto out;
2613+
}
26082614
}
26092615
} else {
26102616
/*
@@ -2635,9 +2641,11 @@ static int lock_ref_for_update(struct files_ref_store *refs,
26352641
update->old_target);
26362642
ret = TRANSACTION_GENERIC_ERROR;
26372643
goto out;
2638-
} else if (check_old_oid(update, &lock->old_oid, err)) {
2639-
ret = TRANSACTION_GENERIC_ERROR;
2640-
goto out;
2644+
} else {
2645+
ret = check_old_oid(update, &lock->old_oid, err);
2646+
if (ret) {
2647+
goto out;
2648+
}
26412649
}
26422650

26432651
/*

refs/reftable-backend.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,10 +1206,13 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
12061206
goto done;
12071207
}
12081208
} else if ((u->flags & REF_HAVE_OLD) && !oideq(&current_oid, &u->old_oid)) {
1209-
if (is_null_oid(&u->old_oid))
1209+
ret = TRANSACTION_NAME_CONFLICT;
1210+
if (is_null_oid(&u->old_oid)) {
12101211
strbuf_addf(err, _("cannot lock ref '%s': "
12111212
"reference already exists"),
12121213
ref_update_original_update_refname(u));
1214+
ret = TRANSACTION_CREATE_EXISTS;
1215+
}
12131216
else if (is_null_oid(&current_oid))
12141217
strbuf_addf(err, _("cannot lock ref '%s': "
12151218
"reference is missing but expected %s"),
@@ -1221,7 +1224,6 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
12211224
ref_update_original_update_refname(u),
12221225
oid_to_hex(&current_oid),
12231226
oid_to_hex(&u->old_oid));
1224-
ret = -1;
12251227
goto done;
12261228
}
12271229

0 commit comments

Comments
 (0)