Skip to content

Commit ed2f6f8

Browse files
ferdinandybgitster
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: Junio C Hamano <[email protected]>
1 parent dfe86fa commit ed2f6f8

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
@@ -773,8 +773,10 @@ int ref_transaction_verify(struct ref_transaction *transaction,
773773

774774
/* Naming conflict (for example, the ref names A and A/B conflict). */
775775
#define TRANSACTION_NAME_CONFLICT -1
776+
/* When only creation was requested, but the ref already exists. */
777+
#define TRANSACTION_CREATE_EXISTS -2
776778
/* All other errors. */
777-
#define TRANSACTION_GENERIC_ERROR -2
779+
#define TRANSACTION_GENERIC_ERROR -3
778780

779781
/*
780782
* 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
@@ -2501,14 +2501,18 @@ static int split_symref_update(struct ref_update *update,
25012501
static int check_old_oid(struct ref_update *update, struct object_id *oid,
25022502
struct strbuf *err)
25032503
{
2504+
int ret = TRANSACTION_GENERIC_ERROR;
2505+
25042506
if (!(update->flags & REF_HAVE_OLD) ||
25052507
oideq(oid, &update->old_oid))
25062508
return 0;
25072509

2508-
if (is_null_oid(&update->old_oid))
2510+
if (is_null_oid(&update->old_oid)) {
25092511
strbuf_addf(err, "cannot lock ref '%s': "
25102512
"reference already exists",
25112513
ref_update_original_update_refname(update));
2514+
ret = TRANSACTION_CREATE_EXISTS;
2515+
}
25122516
else if (is_null_oid(oid))
25132517
strbuf_addf(err, "cannot lock ref '%s': "
25142518
"reference is missing but expected %s",
@@ -2521,7 +2525,7 @@ static int check_old_oid(struct ref_update *update, struct object_id *oid,
25212525
oid_to_hex(oid),
25222526
oid_to_hex(&update->old_oid));
25232527

2524-
return -1;
2528+
return ret;
25252529
}
25262530

25272531
/*
@@ -2601,9 +2605,11 @@ static int lock_ref_for_update(struct files_ref_store *refs,
26012605
ret = TRANSACTION_GENERIC_ERROR;
26022606
goto out;
26032607
}
2604-
} else if (check_old_oid(update, &lock->old_oid, err)) {
2605-
ret = TRANSACTION_GENERIC_ERROR;
2606-
goto out;
2608+
} else {
2609+
ret = check_old_oid(update, &lock->old_oid, err);
2610+
if (ret) {
2611+
goto out;
2612+
}
26072613
}
26082614
} else {
26092615
/*
@@ -2634,9 +2640,11 @@ static int lock_ref_for_update(struct files_ref_store *refs,
26342640
update->old_target);
26352641
ret = TRANSACTION_GENERIC_ERROR;
26362642
goto out;
2637-
} else if (check_old_oid(update, &lock->old_oid, err)) {
2638-
ret = TRANSACTION_GENERIC_ERROR;
2639-
goto out;
2643+
} else {
2644+
ret = check_old_oid(update, &lock->old_oid, err);
2645+
if (ret) {
2646+
goto out;
2647+
}
26402648
}
26412649

26422650
/*

refs/reftable-backend.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,10 +1208,13 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
12081208
goto done;
12091209
}
12101210
} else if ((u->flags & REF_HAVE_OLD) && !oideq(&current_oid, &u->old_oid)) {
1211-
if (is_null_oid(&u->old_oid))
1211+
ret = TRANSACTION_NAME_CONFLICT;
1212+
if (is_null_oid(&u->old_oid)) {
12121213
strbuf_addf(err, _("cannot lock ref '%s': "
12131214
"reference already exists"),
12141215
ref_update_original_update_refname(u));
1216+
ret = TRANSACTION_CREATE_EXISTS;
1217+
}
12151218
else if (is_null_oid(&current_oid))
12161219
strbuf_addf(err, _("cannot lock ref '%s': "
12171220
"reference is missing but expected %s"),
@@ -1223,7 +1226,6 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
12231226
ref_update_original_update_refname(u),
12241227
oid_to_hex(&current_oid),
12251228
oid_to_hex(&u->old_oid));
1226-
ret = -1;
12271229
goto done;
12281230
}
12291231

0 commit comments

Comments
 (0)