git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Cc: Karthik Nayak <karthik.188@gmail.com>
Subject: [PATCH 7/8] refs: stop unsetting REF_HAVE_OLD for log-only updates
Date: Tue, 22 Jul 2025 13:20:56 +0200	[thread overview]
Message-ID: <20250722-pks-reflog-append-v1-7-183e5949de16@pks.im> (raw)
In-Reply-To: <20250722-pks-reflog-append-v1-0-183e5949de16@pks.im>

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 whteher
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 is useful to determine whether we have to resolve the refname
manually to figure out the current state, or whether we should just go
with what the caller has provided.

This actually causes real issues when migrating reflogs, as we don't
know to actually use the caller-provided old object ID when writing
those entries. Instead, reflog entries simply end up with the all-zero
object ID.

Stop unsetting the flag so that we can use it as this described signal,
which we'll do 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>
---
 refs.c                  |  7 +------
 refs/files-backend.c    |  9 +++++----
 refs/reftable-backend.c | 12 +++---------
 3 files changed, 9 insertions(+), 19 deletions(-)

diff --git a/refs.c b/refs.c
index 64544300dc3..c78d5be6e20 100644
--- a/refs.c
+++ b/refs.c
@@ -1384,11 +1384,6 @@ int ref_transaction_update_reflog(struct ref_transaction *transaction,
 	update = ref_transaction_add_update(transaction, refname, flags,
 					    new_oid, old_oid, NULL, NULL,
 					    committer_info, msg);
-	/*
-	 * While we do set the old_oid value, we unset the flag to skip
-	 * old_oid verification which only makes sense for refs.
-	 */
-	update->flags &= ~REF_HAVE_OLD;
 	update->index = index;
 
 	/*
@@ -3310,7 +3305,7 @@ int repo_migrate_ref_storage_format(struct repository *repo,
 
 int ref_update_expects_existing_old_ref(struct ref_update *update)
 {
-	return (update->flags & REF_HAVE_OLD) &&
+	return (update->flags & (REF_HAVE_OLD | REF_LOG_ONLY)) == REF_HAVE_OLD &&
 		(!is_null_oid(&update->old_oid) || update->old_target);
 }
 
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 89ae4517a97..d519bb615fa 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -2493,7 +2493,6 @@ static enum ref_transaction_error split_symref_update(struct ref_update *update,
 	 * done when new_update is processed.
 	 */
 	update->flags |= REF_LOG_ONLY | REF_NO_DEREF;
-	update->flags &= ~REF_HAVE_OLD;
 
 	return 0;
 }
@@ -2508,8 +2507,9 @@ static enum ref_transaction_error check_old_oid(struct ref_update *update,
 						struct object_id *oid,
 						struct strbuf *err)
 {
-	if (!(update->flags & REF_HAVE_OLD) ||
-		   oideq(oid, &update->old_oid))
+	if (update->flags & REF_LOG_ONLY ||
+	    !(update->flags & REF_HAVE_OLD) ||
+	    oideq(oid, &update->old_oid))
 		return 0;
 
 	if (is_null_oid(&update->old_oid)) {
@@ -3061,7 +3061,8 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,
 	for (i = 0; i < transaction->nr; i++) {
 		struct ref_update *update = transaction->updates[i];
 
-		if ((update->flags & REF_HAVE_OLD) &&
+		if (!(update->flags & REF_LOG_ONLY) &&
+		    (update->flags & REF_HAVE_OLD) &&
 		    !is_null_oid(&update->old_oid))
 			BUG("initial ref transaction with old_sha1 set");
 
diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c
index 4c3817f4ec1..44af58ac50b 100644
--- a/refs/reftable-backend.c
+++ b/refs/reftable-backend.c
@@ -1180,8 +1180,6 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor
 	if (ret > 0) {
 		/* The reference does not exist, but we expected it to. */
 		strbuf_addf(err, _("cannot lock ref '%s': "
-
-
 				   "unable to resolve reference '%s'"),
 			    ref_update_original_update_refname(u), u->refname);
 		return REF_TRANSACTION_ERROR_NONEXISTENT_REF;
@@ -1235,13 +1233,8 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor
 
 			new_update->parent_update = u;
 
-			/*
-			 * Change the symbolic ref update to log only. Also, it
-			 * doesn't need to check its old OID value, as that will be
-			 * done when new_update is processed.
-			 */
+			/* Change the symbolic ref update to log only. */
 			u->flags |= REF_LOG_ONLY | REF_NO_DEREF;
-			u->flags &= ~REF_HAVE_OLD;
 		}
 	}
 
@@ -1265,7 +1258,8 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor
 		ret = ref_update_check_old_target(referent->buf, u, err);
 		if (ret)
 			return ret;
-	} else if ((u->flags & REF_HAVE_OLD) && !oideq(&current_oid, &u->old_oid)) {
+	} else if ((u->flags & (REF_LOG_ONLY | REF_HAVE_OLD)) == REF_HAVE_OLD &&
+		   !oideq(&current_oid, &u->old_oid)) {
 		if (is_null_oid(&u->old_oid)) {
 			strbuf_addf(err, _("cannot lock ref '%s': "
 					   "reference already exists"),

-- 
2.50.1.465.gcb3da1c9e6.dirty


  parent reply	other threads:[~2025-07-22 11:21 UTC|newest]

Thread overview: 114+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-22 11:20 [PATCH 0/8] refs: fix migration of reflog entries Patrick Steinhardt
2025-07-22 11:20 ` [PATCH 1/8] Documentation/git-reflog: convert to use synopsis type Patrick Steinhardt
2025-07-22 22:04   ` Junio C Hamano
2025-07-22 11:20 ` [PATCH 2/8] builtin/reflog: improve grouping of subcommands Patrick Steinhardt
2025-07-23 18:14   ` Justin Tobler
2025-07-24  7:42     ` Patrick Steinhardt
2025-07-24 16:45       ` Junio C Hamano
2025-07-22 11:20 ` [PATCH 3/8] refs: export `ref_transaction_update_reflog()` Patrick Steinhardt
2025-07-23 18:25   ` Justin Tobler
2025-07-24  8:36   ` Karthik Nayak
2025-07-24 12:55   ` Toon Claes
2025-07-22 11:20 ` [PATCH 4/8] builtin/reflog: implement subcommand to write new entries Patrick Steinhardt
2025-07-23 19:00   ` Justin Tobler
2025-07-24  7:42     ` Patrick Steinhardt
2025-07-24 12:54   ` Toon Claes
2025-07-25  5:36     ` Patrick Steinhardt
2025-07-24 16:20   ` SZEDER Gábor
2025-07-24 21:10     ` Junio C Hamano
2025-07-25  5:36       ` Patrick Steinhardt
2025-07-25 14:35         ` Junio C Hamano
2025-07-22 11:20 ` [PATCH 5/8] ident: fix type of string length parameter Patrick Steinhardt
2025-07-22 11:20 ` [PATCH 6/8] refs: fix identity for migrated reflogs Patrick Steinhardt
2025-07-23 19:41   ` Justin Tobler
2025-07-24  7:42     ` Patrick Steinhardt
2025-07-24  9:41   ` Karthik Nayak
2025-07-24 12:56   ` Toon Claes
2025-07-22 11:20 ` Patrick Steinhardt [this message]
2025-07-23 20:31   ` [PATCH 7/8] refs: stop unsetting REF_HAVE_OLD for log-only updates Justin Tobler
2025-07-24  7:42     ` Patrick Steinhardt
2025-07-24 10:21   ` Karthik Nayak
2025-07-24 11:35     ` Patrick Steinhardt
2025-07-22 11:20 ` [PATCH 8/8] refs: fix invalid old object IDs when migrating reflogs Patrick Steinhardt
2025-07-22 22:09   ` Junio C Hamano
2025-07-23  4:04     ` Patrick Steinhardt
2025-07-25  6:58 ` [PATCH v2 0/8] refs: fix migration of reflog entries Patrick Steinhardt
2025-07-25  6:58   ` [PATCH v2 1/8] Documentation/git-reflog: convert to use synopsis type Patrick Steinhardt
2025-07-25  6:58   ` [PATCH v2 2/8] builtin/reflog: improve grouping of subcommands Patrick Steinhardt
2025-07-25  6:58   ` [PATCH v2 3/8] refs: export `ref_transaction_update_reflog()` Patrick Steinhardt
2025-07-25  6:58   ` [PATCH v2 4/8] builtin/reflog: implement subcommand to write new entries Patrick Steinhardt
2025-07-28 15:33     ` Kristoffer Haugsbakk
2025-07-28 18:49       ` Junio C Hamano
2025-07-28 20:39         ` Karthik Nayak
2025-07-28 20:59           ` Junio C Hamano
2025-07-30  7:55             ` Karthik Nayak
2025-07-29  0:25       ` Ben Knoble
2025-07-29  6:14         ` Kristoffer Haugsbakk
2025-07-29  6:51         ` Patrick Steinhardt
2025-07-29 15:00           ` Junio C Hamano
2025-07-30  5:33             ` Patrick Steinhardt
2025-07-30 10:33               ` Patrick Steinhardt
2025-07-25  6:58   ` [PATCH v2 5/8] ident: fix type of string length parameter Patrick Steinhardt
2025-07-25  6:58   ` [PATCH v2 6/8] refs: fix identity for migrated reflogs Patrick Steinhardt
2025-07-25  6:58   ` [PATCH v2 7/8] refs: stop unsetting REF_HAVE_OLD for log-only updates Patrick Steinhardt
2025-07-25 11:36     ` Jeff King
2025-07-28 14:43       ` Patrick Steinhardt
2025-07-29  7:14         ` Jeff King
2025-07-29  7:54           ` Patrick Steinhardt
2025-07-25  6:58   ` [PATCH v2 8/8] refs: fix invalid old object IDs when migrating reflogs Patrick Steinhardt
2025-07-29  8:55 ` [PATCH v3 0/9] refs: fix migration of reflog entries Patrick Steinhardt
2025-07-29  8:55   ` [PATCH v3 1/9] Documentation/git-reflog: convert to use synopsis type Patrick Steinhardt
2025-07-29  8:55   ` [PATCH v3 2/9] builtin/reflog: improve grouping of subcommands Patrick Steinhardt
2025-07-29  8:55   ` [PATCH v3 3/9] refs: export `ref_transaction_update_reflog()` Patrick Steinhardt
2025-08-01 11:38     ` Toon Claes
2025-08-04  7:37       ` Patrick Steinhardt
2025-07-29  8:55   ` [PATCH v3 4/9] builtin/reflog: implement subcommand to write new entries Patrick Steinhardt
2025-07-29 16:07     ` Junio C Hamano
2025-08-01 11:37     ` Toon Claes
2025-08-04  7:38       ` Patrick Steinhardt
2025-07-29  8:55   ` [PATCH v3 5/9] ident: fix type of string length parameter Patrick Steinhardt
2025-07-29  8:55   ` [PATCH v3 6/9] refs: fix identity for migrated reflogs Patrick Steinhardt
2025-07-29  8:55   ` [PATCH v3 7/9] refs/files: detect race when generating reflog entry for HEAD Patrick Steinhardt
2025-07-29 16:16     ` Junio C Hamano
2025-08-01 11:55     ` Toon Claes
2025-08-02 11:11     ` Jeff King
2025-08-04  7:38       ` Patrick Steinhardt
2025-08-04 14:47         ` Jeff King
2025-07-29  8:55   ` [PATCH v3 8/9] refs: stop unsetting REF_HAVE_OLD for log-only updates Patrick Steinhardt
2025-07-29  8:55   ` [PATCH v3 9/9] refs: fix invalid old object IDs when migrating reflogs Patrick Steinhardt
2025-08-04  9:46 ` [PATCH v4 0/9] refs: fix migration of reflog entries Patrick Steinhardt
2025-08-04  9:46   ` [PATCH v4 1/9] Documentation/git-reflog: convert to use synopsis type Patrick Steinhardt
2025-08-04  9:46   ` [PATCH v4 2/9] builtin/reflog: improve grouping of subcommands Patrick Steinhardt
2025-08-04  9:46   ` [PATCH v4 3/9] refs: export `ref_transaction_update_reflog()` Patrick Steinhardt
2025-08-04  9:46   ` [PATCH v4 4/9] builtin/reflog: implement subcommand to write new entries Patrick Steinhardt
2025-08-04  9:46   ` [PATCH v4 5/9] ident: fix type of string length parameter Patrick Steinhardt
2025-08-04  9:46   ` [PATCH v4 6/9] refs: fix identity for migrated reflogs Patrick Steinhardt
2025-08-04  9:46   ` [PATCH v4 7/9] refs/files: detect race when generating reflog entry for HEAD Patrick Steinhardt
2025-08-04 15:38     ` Jeff King
2025-08-04  9:46   ` [PATCH v4 8/9] refs: stop unsetting REF_HAVE_OLD for log-only updates Patrick Steinhardt
2025-08-04  9:46   ` [PATCH v4 9/9] refs: fix invalid old object IDs when migrating reflogs Patrick Steinhardt
2025-08-05 15:11 ` [PATCH v5 0/9] refs: fix migration of reflog entries Patrick Steinhardt
2025-08-05 15:11   ` [PATCH v5 1/9] Documentation/git-reflog: convert to use synopsis type Patrick Steinhardt
2025-08-05 17:04     ` Jean-Noël AVILA
2025-08-05 21:47       ` Junio C Hamano
2025-08-06  5:53         ` Patrick Steinhardt
2025-08-05 15:11   ` [PATCH v5 2/9] builtin/reflog: improve grouping of subcommands Patrick Steinhardt
2025-08-05 15:11   ` [PATCH v5 3/9] refs: export `ref_transaction_update_reflog()` Patrick Steinhardt
2025-08-05 15:11   ` [PATCH v5 4/9] builtin/reflog: implement subcommand to write new entries Patrick Steinhardt
2025-08-05 15:11   ` [PATCH v5 5/9] ident: fix type of string length parameter Patrick Steinhardt
2025-08-05 15:11   ` [PATCH v5 6/9] refs: fix identity for migrated reflogs Patrick Steinhardt
2025-08-05 15:11   ` [PATCH v5 7/9] refs/files: detect race when generating reflog entry for HEAD Patrick Steinhardt
2025-08-05 15:11   ` [PATCH v5 8/9] refs: stop unsetting REF_HAVE_OLD for log-only updates Patrick Steinhardt
2025-08-05 15:11   ` [PATCH v5 9/9] refs: fix invalid old object IDs when migrating reflogs Patrick Steinhardt
2025-08-05 18:47   ` [PATCH v5 0/9] refs: fix migration of reflog entries Jeff King
2025-08-06  5:53     ` Patrick Steinhardt
2025-08-06  5:54 ` [PATCH v6 " Patrick Steinhardt
2025-08-06  5:54   ` [PATCH v6 1/9] Documentation/git-reflog: convert to use synopsis type Patrick Steinhardt
2025-08-06  5:54   ` [PATCH v6 2/9] builtin/reflog: improve grouping of subcommands Patrick Steinhardt
2025-08-06  5:54   ` [PATCH v6 3/9] refs: export `ref_transaction_update_reflog()` Patrick Steinhardt
2025-08-06  5:54   ` [PATCH v6 4/9] builtin/reflog: implement subcommand to write new entries Patrick Steinhardt
2025-08-06  5:54   ` [PATCH v6 5/9] ident: fix type of string length parameter Patrick Steinhardt
2025-08-06  5:54   ` [PATCH v6 6/9] refs: fix identity for migrated reflogs Patrick Steinhardt
2025-08-06  5:54   ` [PATCH v6 7/9] refs/files: detect race when generating reflog entry for HEAD Patrick Steinhardt
2025-08-06  5:54   ` [PATCH v6 8/9] refs: stop unsetting REF_HAVE_OLD for log-only updates Patrick Steinhardt
2025-08-06  5:54   ` [PATCH v6 9/9] refs: fix invalid old object IDs when migrating reflogs Patrick Steinhardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250722-pks-reflog-append-v1-7-183e5949de16@pks.im \
    --to=ps@pks.im \
    --cc=git@vger.kernel.org \
    --cc=karthik.188@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).