Git fork

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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

Patrick Steinhardt and committed by
Junio C Hamano
046c6732 211fa8b2

+13 -19
+3 -5
refs.c
··· 1393 1393 update = ref_transaction_add_update(transaction, refname, flags, 1394 1394 new_oid, old_oid, NULL, NULL, 1395 1395 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; 1401 1396 update->index = index; 1402 1397 1403 1398 /* ··· 3318 3313 3319 3314 int ref_update_expects_existing_old_ref(struct ref_update *update) 3320 3315 { 3316 + if (update->flags & REF_LOG_ONLY) 3317 + return 0; 3318 + 3321 3319 return (update->flags & REF_HAVE_OLD) && 3322 3320 (!is_null_oid(&update->old_oid) || update->old_target); 3323 3321 }
+5 -4
refs/files-backend.c
··· 2500 2500 * done when new_update is processed. 2501 2501 */ 2502 2502 update->flags |= REF_LOG_ONLY | REF_NO_DEREF; 2503 - update->flags &= ~REF_HAVE_OLD; 2504 2503 2505 2504 return 0; 2506 2505 } ··· 2515 2514 struct object_id *oid, 2516 2515 struct strbuf *err) 2517 2516 { 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)) 2520 2520 return 0; 2521 2521 2522 2522 if (is_null_oid(&update->old_oid)) { ··· 3095 3095 for (i = 0; i < transaction->nr; i++) { 3096 3096 struct ref_update *update = transaction->updates[i]; 3097 3097 3098 - if ((update->flags & REF_HAVE_OLD) && 3098 + if (!(update->flags & REF_LOG_ONLY) && 3099 + (update->flags & REF_HAVE_OLD) && 3099 3100 !is_null_oid(&update->old_oid)) 3100 3101 BUG("initial ref transaction with old_sha1 set"); 3101 3102
+2 -1
refs/refs-internal.h
··· 802 802 803 803 /* 804 804 * 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. 806 807 */ 807 808 int ref_update_expects_existing_old_ref(struct ref_update *update); 808 809
+3 -9
refs/reftable-backend.c
··· 1180 1180 if (ret > 0) { 1181 1181 /* The reference does not exist, but we expected it to. */ 1182 1182 strbuf_addf(err, _("cannot lock ref '%s': " 1183 - 1184 - 1185 1183 "unable to resolve reference '%s'"), 1186 1184 ref_update_original_update_refname(u), u->refname); 1187 1185 return REF_TRANSACTION_ERROR_NONEXISTENT_REF; ··· 1235 1233 1236 1234 new_update->parent_update = u; 1237 1235 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. */ 1243 1237 u->flags |= REF_LOG_ONLY | REF_NO_DEREF; 1244 - u->flags &= ~REF_HAVE_OLD; 1245 1238 } 1246 1239 } 1247 1240 ··· 1265 1258 ret = ref_update_check_old_target(referent->buf, u, err); 1266 1259 if (ret) 1267 1260 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)) { 1269 1263 if (is_null_oid(&u->old_oid)) { 1270 1264 strbuf_addf(err, _("cannot lock ref '%s': " 1271 1265 "reference already exists"),