Skip to content

Commit ee480aa

Browse files
ferdinandybgitster
authored andcommitted
transaction: 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: Junio C Hamano <[email protected]>
1 parent 074b0aa commit ee480aa

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

refs.h

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

760760
/* Naming conflict (for example, the ref names A and A/B conflict). */
761761
#define TRANSACTION_NAME_CONFLICT -1
762+
/* When only creation was requested, but the ref already exists. */
763+
#define TRANSACTION_CREATE_EXISTS -2
762764
/* All other errors. */
763-
#define TRANSACTION_GENERIC_ERROR -2
765+
#define TRANSACTION_GENERIC_ERROR -3
764766

765767
/*
766768
* Perform the preparatory stages of committing `transaction`. Acquire

refs/files-backend.c

Lines changed: 20 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
/*
@@ -2603,9 +2607,13 @@ static int lock_ref_for_update(struct files_ref_store *refs,
26032607
ret = TRANSACTION_GENERIC_ERROR;
26042608
goto out;
26052609
}
2606-
} else if (check_old_oid(update, &lock->old_oid, err)) {
2607-
ret = TRANSACTION_GENERIC_ERROR;
2608-
goto out;
2610+
} else {
2611+
int checkret;
2612+
checkret = check_old_oid(update, &lock->old_oid, err);
2613+
if (checkret) {
2614+
ret = checkret;
2615+
goto out;
2616+
}
26092617
}
26102618
} else {
26112619
/*
@@ -2636,9 +2644,13 @@ static int lock_ref_for_update(struct files_ref_store *refs,
26362644
update->old_target);
26372645
ret = TRANSACTION_GENERIC_ERROR;
26382646
goto out;
2639-
} else if (check_old_oid(update, &lock->old_oid, err)) {
2640-
ret = TRANSACTION_GENERIC_ERROR;
2641-
goto out;
2647+
} else {
2648+
int checkret;
2649+
checkret = check_old_oid(update, &lock->old_oid, err);
2650+
if (checkret) {
2651+
ret = checkret;
2652+
goto out;
2653+
}
26422654
}
26432655

26442656
/*

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)