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