Skip to content

Commit ec4d8b7

Browse files
pks-tgitster
authored andcommitted
refs: stop unsetting REF_HAVE_OLD for log-only updates
The `REF_HAVE_OLD` flag indicates whether a given ref update has its old object ID set. If so, the value of that field is used to verify whether the current state of the reference matches this expected state. It is thus an important part of mitigating races with a concurrent process that updates the same set of references. When writing reflogs though we explicitly unset that flag. This is a sensible thing to do: the old state of reflog entry updates may not necessarily match the current on-disk state of its accompanying ref, but it's only intended to signal what old object ID we want to write into the new reflog entry. For example when migrating refs we end up writing many reflog entries for a single reference, and most likely those reflog entries will have many different old object IDs. But unsetting this flag also removes a useful signal, namely that the caller _did_ provide an old object ID for a given reflog entry. This signal will become useful in a subsequent commit, where we add a new flag that tells the transaction to use the provided old and new object IDs to write a reflog entry. The `REF_HAVE_OLD` flag is then used as a signal to verify that the caller really did provide an old object ID. Stop unsetting the flag so that we can use it as this described signal in a subsequent commit. Skip checking the old object ID for log-only updates so that we don't expect it to match the current on-disk state. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 988cfcf commit ec4d8b7

File tree

4 files changed

+13
-19
lines changed

4 files changed

+13
-19
lines changed

refs.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,11 +1393,6 @@ int ref_transaction_update_reflog(struct ref_transaction *transaction,
13931393
update = ref_transaction_add_update(transaction, refname, flags,
13941394
new_oid, old_oid, NULL, NULL,
13951395
committer_info, msg);
1396-
/*
1397-
* While we do set the old_oid value, we unset the flag to skip
1398-
* old_oid verification which only makes sense for refs.
1399-
*/
1400-
update->flags &= ~REF_HAVE_OLD;
14011396
update->index = index;
14021397

14031398
/*
@@ -3318,6 +3313,9 @@ int repo_migrate_ref_storage_format(struct repository *repo,
33183313

33193314
int ref_update_expects_existing_old_ref(struct ref_update *update)
33203315
{
3316+
if (update->flags & REF_LOG_ONLY)
3317+
return 0;
3318+
33213319
return (update->flags & REF_HAVE_OLD) &&
33223320
(!is_null_oid(&update->old_oid) || update->old_target);
33233321
}

refs/files-backend.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2500,7 +2500,6 @@ static enum ref_transaction_error split_symref_update(struct ref_update *update,
25002500
* done when new_update is processed.
25012501
*/
25022502
update->flags |= REF_LOG_ONLY | REF_NO_DEREF;
2503-
update->flags &= ~REF_HAVE_OLD;
25042503

25052504
return 0;
25062505
}
@@ -2515,8 +2514,9 @@ static enum ref_transaction_error check_old_oid(struct ref_update *update,
25152514
struct object_id *oid,
25162515
struct strbuf *err)
25172516
{
2518-
if (!(update->flags & REF_HAVE_OLD) ||
2519-
oideq(oid, &update->old_oid))
2517+
if (update->flags & REF_LOG_ONLY ||
2518+
!(update->flags & REF_HAVE_OLD) ||
2519+
oideq(oid, &update->old_oid))
25202520
return 0;
25212521

25222522
if (is_null_oid(&update->old_oid)) {
@@ -3095,7 +3095,8 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,
30953095
for (i = 0; i < transaction->nr; i++) {
30963096
struct ref_update *update = transaction->updates[i];
30973097

3098-
if ((update->flags & REF_HAVE_OLD) &&
3098+
if (!(update->flags & REF_LOG_ONLY) &&
3099+
(update->flags & REF_HAVE_OLD) &&
30993100
!is_null_oid(&update->old_oid))
31003101
BUG("initial ref transaction with old_sha1 set");
31013102

refs/refs-internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,8 @@ enum ref_transaction_error ref_update_check_old_target(const char *referent,
802802

803803
/*
804804
* Check if the ref must exist, this means that the old_oid or
805-
* old_target is non NULL.
805+
* old_target is non NULL. Log-only updates never require the old state to
806+
* match.
806807
*/
807808
int ref_update_expects_existing_old_ref(struct ref_update *update);
808809

refs/reftable-backend.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,8 +1180,6 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor
11801180
if (ret > 0) {
11811181
/* The reference does not exist, but we expected it to. */
11821182
strbuf_addf(err, _("cannot lock ref '%s': "
1183-
1184-
11851183
"unable to resolve reference '%s'"),
11861184
ref_update_original_update_refname(u), u->refname);
11871185
return REF_TRANSACTION_ERROR_NONEXISTENT_REF;
@@ -1235,13 +1233,8 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor
12351233

12361234
new_update->parent_update = u;
12371235

1238-
/*
1239-
* Change the symbolic ref update to log only. Also, it
1240-
* doesn't need to check its old OID value, as that will be
1241-
* done when new_update is processed.
1242-
*/
1236+
/* Change the symbolic ref update to log only. */
12431237
u->flags |= REF_LOG_ONLY | REF_NO_DEREF;
1244-
u->flags &= ~REF_HAVE_OLD;
12451238
}
12461239
}
12471240

@@ -1265,7 +1258,8 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor
12651258
ret = ref_update_check_old_target(referent->buf, u, err);
12661259
if (ret)
12671260
return ret;
1268-
} else if ((u->flags & REF_HAVE_OLD) && !oideq(&current_oid, &u->old_oid)) {
1261+
} else if ((u->flags & (REF_LOG_ONLY | REF_HAVE_OLD)) == REF_HAVE_OLD &&
1262+
!oideq(&current_oid, &u->old_oid)) {
12691263
if (is_null_oid(&u->old_oid)) {
12701264
strbuf_addf(err, _("cannot lock ref '%s': "
12711265
"reference already exists"),

0 commit comments

Comments
 (0)