git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 2/6] refs: simplify logic when migrating reflog entries

From
Patrick Steinhardt <ps@pks.im>
Date
Jul 31, 2025, 14:56 UTC
Message-ID
<20250731-pks-remote-rename-improvements-v2-2-dda6f083674d@pks.im>
In-Reply-To
<20250731-pks-remote-rename-improvements-v2-0-dda6f083674d@pks.im>

When migrating reflog entries between two storage formats we have to do so via two callback-driven functions:

  - `migrate_one_reflog()` gets invoked via `refs_for_each_reflog()` to
    first list all available reflogs.
  - `migrate_one_reflog_entry()` gets invoked via
    `refs_for_each_reflog_ent()` in `migrate_one_reflog()`.

Before the preceding commit we didn't have the refname available in `migrate_one_reflog_entry()`, which made it necessary to have a separate structure that we pass to the second callback so that we can propagate the refname. Now that `refs_for_each_reflog_ent()` knows to pass the refname to the callback though that indirection isn't necessary anymore.

There's one catch though: we do have an update index that is also stored in the entry-specific callback data. This update index is required so that we can tell the ref backend in which order it should persist the reflog entries to disk.

But that purpose can be trivially achieved by just converting it into a global counter that is used for all reflog entries, regardless of which reference they are for. The ordering will remain the same as both the update index and the refname is considered when sorting the entries.

Move the index into `struct migration_data` and drop the now-unused `struct reflog_migration_data` to simplify the code a bit.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 refs.c | 36 ++++++++++--------------------------
 1 file changed, 10 insertions(+), 26 deletions(-)
diff --git a/refs.c b/refs.c
index 6ed0cd6ddc..04c9ace793 100644
--- a/refs.c
+++ b/refs.c
@@ -2941,6 +2941,7 @@ struct migration_data {
 	struct ref_transaction *transaction;
 	struct strbuf *errbuf;
 	struct strbuf sb, name, mail;
+	uint64_t index;
 };
 
 static int migrate_one_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
@@ -2973,14 +2974,6 @@ static int migrate_one_ref(const char *refname, const char *referent UNUSED, con
 	return ret;
 }
 
-struct reflog_migration_data {
-	uint64_t index;
-	struct ref_store *old_refs;
-	struct ref_transaction *transaction;
-	struct strbuf *errbuf;
-	struct strbuf *sb, *name, *mail;
-};
-
 static int migrate_one_reflog_entry(const char *refname,
 				    struct object_id *old_oid,
 				    struct object_id *new_oid,
@@ -2988,7 +2981,7 @@ static int migrate_one_reflog_entry(const char *refname,
 				    timestamp_t timestamp, int tz,
 				    const char *msg, void *cb_data)
 {
-	struct reflog_migration_data *data = cb_data;
+	struct migration_data *data = cb_data;
 	struct ident_split ident;
 	const char *date;
 	int ret;
@@ -2996,17 +2989,17 @@ static int migrate_one_reflog_entry(const char *refname,
 	if (split_ident_line(&ident, committer, strlen(committer)) < 0)
 		return -1;
 
-	strbuf_reset(data->name);
-	strbuf_add(data->name, ident.name_begin, ident.name_end - ident.name_begin);
-	strbuf_reset(data->mail);
-	strbuf_add(data->mail, ident.mail_begin, ident.mail_end - ident.mail_begin);
+	strbuf_reset(&data->name);
+	strbuf_add(&data->name, ident.name_begin, ident.name_end - ident.name_begin);
+	strbuf_reset(&data->mail);
+	strbuf_add(&data->mail, ident.mail_begin, ident.mail_end - ident.mail_begin);
 
 	date = show_date(timestamp, tz, DATE_MODE(NORMAL));
-	strbuf_reset(data->sb);
-	strbuf_addstr(data->sb, fmt_ident(data->name->buf, data->mail->buf, WANT_BLANK_IDENT, date, 0));
+	strbuf_reset(&data->sb);
+	strbuf_addstr(&data->sb, fmt_ident(data->name.buf, data->mail.buf, WANT_BLANK_IDENT, date, 0));
 
 	ret = ref_transaction_update_reflog(data->transaction, refname,
-					    new_oid, old_oid, data->sb->buf,
+					    new_oid, old_oid, data->sb.buf,
 					    msg, data->index++, data->errbuf);
 	return ret;
 }
@@ -3014,17 +3007,8 @@ static int migrate_one_reflog_entry(const char *refname,
 static int migrate_one_reflog(const char *refname, void *cb_data)
 {
 	struct migration_data *migration_data = cb_data;
-	struct reflog_migration_data data = {
-		.old_refs = migration_data->old_refs,
-		.transaction = migration_data->transaction,
-		.errbuf = migration_data->errbuf,
-		.sb = &migration_data->sb,
-		.name = &migration_data->name,
-		.mail = &migration_data->mail,
-	};
-
 	return refs_for_each_reflog_ent(migration_data->old_refs, refname,
-					migrate_one_reflog_entry, &data);
+					migrate_one_reflog_entry, migration_data);
 }
 
 static int move_files(const char *from_path, const char *to_path, struct strbuf *errbuf)
-- 
2.50.1.619.g074bbf1d35.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 26 of 37 in “builtin/remote: rework how remote refs get renamed”
  1. 0/4 builtin/remote: rework how remote refs get renamedPatrick Steinhardt, Jul 28, 2025
  2. 1/4 refs: pass refname when invoking reflog entry callbackPatrick Steinhardt, Jul 28, 2025
  3. Justin ToblerJul 28, 2025
  4. Junio C HamanoJul 28, 2025
  5. Karthik NayakJul 29, 2025
  6. Patrick SteinhardtJul 31, 2025
  7. 2/4 refs: simplify logic when migrating reflog entriesPatrick Steinhardt, Jul 28, 2025
  8. Justin ToblerJul 28, 2025
  9. Junio C HamanoJul 28, 2025
  10. 3/4 builtin/remote: rework how remote refs get renamedPatrick Steinhardt, Jul 28, 2025
  11. Junio C HamanoJul 28, 2025
  12. Patrick SteinhardtJul 29, 2025
  13. Justin ToblerJul 28, 2025
  14. Junio C HamanoJul 28, 2025
  15. Patrick SteinhardtJul 29, 2025
  16. Jeff KingJul 29, 2025
  17. Patrick SteinhardtJul 29, 2025
  18. Jeff KingAug 2, 2025
  19. 4/4 builtin/remote: only iterate through refs that are to be renamedPatrick Steinhardt, Jul 28, 2025
  20. Junio C HamanoJul 28, 2025
  21. Karthik NayakJul 30, 2025
  22. Patrick SteinhardtJul 31, 2025
  23. Junio C HamanoJul 28, 2025
  24. 0/6 builtin/remote: rework how remote refs get renamedPatrick Steinhardt, Jul 31, 2025
  25. 1/6 refs: pass refname when invoking reflog entry callbackPatrick Steinhardt, Jul 31, 2025
  26. 2/6 refs: simplify logic when migrating reflog entriesPatrick Steinhardt, Jul 31, 2025
  27. 3/6 builtin/remote: fix sign comparison warningsPatrick Steinhardt, Jul 31, 2025
  28. 4/6 builtin/remote: determine whether refs need renaming early onPatrick Steinhardt, Jul 31, 2025
  29. 5/6 builtin/remote: rework how remote refs get renamedPatrick Steinhardt, Jul 31, 2025
  30. Jeff KingAug 2, 2025
  31. Patrick SteinhardtAug 4, 2025
  32. 6/6 builtin/remote: only iterate through refs that are to be renamedPatrick Steinhardt, Jul 31, 2025
  33. Junio C HamanoJul 31, 2025
  34. Patrick SteinhardtAug 1, 2025
  35. Junio C HamanoAug 1, 2025
  36. Patrick SteinhardtAug 4, 2025
  37. Junio C HamanoAug 4, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.