[PATCH v3 0/8] refs: add reflog support to `git refs migrate`

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The `git refs migrate` command was introduced in
25a0023f28 (builtin/refs: new command to migrate ref storage formats,
2024-06-06) to support migrating from one reference backend to another.

One limitation of the feature was that it didn't support migrating
repositories which contained reflogs. This isn't a requirement on the
server side as repositories are stored as bare repositories (which do
not contain any reflogs). Clients however generally use reflogs and
until now couldn't use the `git refs migrate` command to migrate their
repositories to the new reftable format.

One of the issues for adding reflog support is that the ref transactions
don't support reflogs additions:
  1. While there is REF_LOG_ONLY flag, there is no function to utilize
  the flag and add reflogs.
  2. reference backends generally sort the updates by the refname. This
  wouldn't work for reflogs which need to ensure that they maintain the
  order of creation.
  3. In the files backend, reflog entries are added by obtaining locks
  on the refs themselves. This means each update in the transaction, will
  obtain a ref_lock. This paradigm fails to accompany the fact that there
  could be multiple reflog updates for a refname in a single transaction.
  4. The backends check for duplicate entries, which doesn't make sense
  in the context of adding multiple reflogs for a given refname.

We overcome these issue we make the following changes:
  - Update the ref_update structure to also include the committer
  information. Using this, we can add a new function which only adds
  reflog updates to the transaction.
  - Add an index field to the ref_update structure, this will help order
  updates in pre-defined order, this fixes #2.
  - While the ideal fix for #3 would be to actually introduce reflog
  locks, this wouldn't be possible without breaking backward
  compatibility. So we add a count field to the existing ref_lock. With
  this, multiple reflog updates can share a single ref_lock.

Overall, this series is a bit more involved, and I would appreciate it
if it receives a bit more scrutiny.

The series is based on top of e66fd72e97 (The fourteenth batch,
2024-12-06) with `kn/reftable-writer-log-write-verify` merged in.

Signed-off-by: Karthik Nayak <karthik.188@xxxxxxxxx>
---
Changes in v3:
- patch 5: Use `xstrdup_or_null` unconditionally. 
- patch 6: In `transaction_refname_valid()` use the transaction flags
  to identify reflogs. Update the documentation to also mention the
  purpose of the `index` field.
- patch 8: Instead of allocating an strbuf for each reflog entry, we 
  store and re-use one in the migration callback data.
- patch 8: Don't use a global index increment for all reflogs entries,
  instead create and use one per reflog.
- patch 8: Avoid setting the first reflog index to `1`. This would default
  to `0` as the first index, which is okay, since the index is incremented
  for consequtive reflog entries.
- Small typo fixes. 
- Thanks to Christian and Patrick for the review!  
- Link to v2: https://lore.kernel.org/all/20241213-320-git-refs-migrate-reflogs-v2-0-f28312cdb6c0@xxxxxxxxx/

Changes in v2:
- Split patch 5 into two separate patches. This should make it easier to
  review and reduce cognitive load in a single patch.
- In reftable backend, instead of using `strmapint` to ensure we have
  new update_indexes for reflogs with the same refname, we now use the
  already available `update->index` field to increment the update_index.
- Cleanup the code and follow some of the better practices.
- Add some clarity to the commit messages.
- Link to v1: https://lore.kernel.org/r/20241209-320-git-refs-migrate-reflogs-v1-0-d4bc37ee860f@xxxxxxxxx

---
Karthik Nayak (8):
      refs: include committer info in `ref_update` struct
      refs: add `index` field to `struct ref_udpate`
      refs/files: add count field to ref_lock
      refs: extract out refname verification in transactions
      refs: add `committer_info` to `ref_transaction_add_update()`
      refs: introduce the `ref_transaction_update_reflog` function
      refs: allow multiple reflog entries for the same refname
      refs: add support for migrating reflogs

 Documentation/git-refs.txt |   2 -
 refs.c                     | 165 +++++++++++++++++++++++++++++++++------------
 refs.h                     |  14 ++++
 refs/files-backend.c       | 131 ++++++++++++++++++++++-------------
 refs/refs-internal.h       |   9 +++
 refs/reftable-backend.c    |  53 ++++++++++++---
 t/t1460-refs-migrate.sh    |  73 ++++++++++++++------
 7 files changed, 327 insertions(+), 120 deletions(-)
---

Range-diff versus v2:

1:  d9bd20468c = 1:  d1c06e34bf refs: include committer info in `ref_update` struct
2:  8478eeac95 = 2:  33e65a965a refs: add `index` field to `struct ref_udpate`
3:  913fd320f0 = 3:  34c5beccb6 refs/files: add count field to ref_lock
4:  66b86b5807 = 4:  4fceb64954 refs: extract out refname verification in transactions
5:  33ad1774d4 ! 5:  3c5abd0047 refs: add `committer_info` to `ref_transaction_add_update()`
    @@ refs.c: struct ref_update *ref_transaction_add_update(
      		oidcpy(&update->old_oid, old_oid);
     -	if (!(flags & REF_SKIP_CREATE_REFLOG))
     +	if (!(flags & REF_SKIP_CREATE_REFLOG)) {
    -+		if (committer_info)
    -+			update->committer_info = xstrdup(committer_info);
    -+
    ++		update->committer_info = xstrdup_or_null(committer_info);
      		update->msg = normalize_reflog_message(msg);
     +	}
      
6:  cdbb15b11a ! 6:  9e12f16b96 refs: introduce the `ref_transaction_update_reflog` function
    @@ Commit message
           means clients can add reflog entries with custom committer
           information.
     
    +    The `transaction_refname_valid()` function also modifies the error
    +    message selectively based on the type of the update. This change also
    +    affects reflog updates which go through `ref_transaction_update()`.
    +
         A follow up commit will utilize this function to add reflog support to
         `git refs migrate`.
     
         Signed-off-by: Karthik Nayak <karthik.188@xxxxxxxxx>
     
      ## refs.c ##
    -@@ refs.c: struct ref_update *ref_transaction_add_update(
    - 
    - static int transaction_refname_valid(const char *refname,
    - 				     const struct object_id *new_oid,
    --				     unsigned int flags, struct strbuf *err)
    -+				     unsigned int flags, unsigned int reflog,
    -+				     struct strbuf *err)
    - {
    - 	if (flags & REF_SKIP_REFNAME_VERIFICATION)
    +@@ refs.c: static int transaction_refname_valid(const char *refname,
      		return 1;
      
      	if (is_pseudo_ref(refname)) {
     -		strbuf_addf(err, _("refusing to update pseudoref '%s'"),
     -			    refname);
    -+		const char *what = reflog ? "reflog for pseudoref" : "pseudoref";
    ++		const char *what = flags & REF_LOG_ONLY ? "reflog for pseudoref" : "pseudoref";
     +		strbuf_addf(err, _("refusing to update %s '%s'"), what, refname);
      		return 0;
      	} else if ((new_oid && !is_null_oid(new_oid)) ?
    @@ refs.c: struct ref_update *ref_transaction_add_update(
      		 !refname_is_safe(refname)) {
     -		strbuf_addf(err, _("refusing to update ref with bad name '%s'"),
     -			    refname);
    -+		const char *what = reflog ? "reflog with bad name" : "ref with bad name";
    ++		const char *what = flags & REF_LOG_ONLY ? "reflog with bad name" : "ref with bad name";
     +		strbuf_addf(err, _("refusing to update %s '%s'"), what, refname);
      		return 0;
      	}
      
    -@@ refs.c: int ref_transaction_update(struct ref_transaction *transaction,
    - 		return -1;
    - 	}
    - 
    --	if (!transaction_refname_valid(refname, new_oid, flags, err))
    -+	if (!transaction_refname_valid(refname, new_oid, flags, 0, err))
    - 		return -1;
    - 
    - 	if (flags & ~REF_TRANSACTION_UPDATE_ALLOWED_FLAGS)
     @@ refs.c: int ref_transaction_update(struct ref_transaction *transaction,
      	ref_transaction_add_update(transaction, refname, flags,
      				   new_oid, old_oid, new_target,
    @@ refs.c: int ref_transaction_update(struct ref_transaction *transaction,
     +
     +	assert(err);
     +
    -+	if (!transaction_refname_valid(refname, new_oid, flags, 1, err))
    -+		return -1;
    -+
     +	flags |= REF_LOG_ONLY | REF_NO_DEREF;
     +
    ++	if (!transaction_refname_valid(refname, new_oid, flags, err))
    ++		return -1;
    ++
     +	update = ref_transaction_add_update(transaction, refname, flags,
     +					    new_oid, old_oid, NULL, NULL,
     +					    committer_info, msg);
    @@ refs.h: int ref_transaction_update(struct ref_transaction *transaction,
      
     +/*
     + * Similar to`ref_transaction_update`, but this function is only for adding
    -+ * a reflog update. Supports providing custom committer information.
    ++ * a reflog update. Supports providing custom committer information. The index
    ++ * field can be utiltized to order updates as desired. When not used, the
    ++ * updates default to being ordered by refname.
     + */
     +int ref_transaction_update_reflog(struct ref_transaction *transaction,
     +				  const char *refname,
7:  dffc14e1a3 ! 7:  4d76cf4773 refs: allow multiple reflog entries for the same refname
    @@ Commit message
         update_index before writing to the block. When there are multiple
         reflogs for a given refname, it is essential that the order of the
         reflogs is maintained. So add the `index` value to the `update_index`.
    -    The `index` field is only be set when multiple reflog entries for a
    -    given refname are added and as such in most scenarios the old behavior
    +    The `index` field is only set when multiple reflog entries for a given
    +    refname are added and as such in most scenarios the old behavior
         remains.
     
         This is required to add reflog migration support to `git refs migrate`.
8:  481d185e6e ! 8:  31cc392d8d refs: add support for migrating reflogs
    @@ refs.c
      
      /*
       * List of all available backends
    -@@ refs.c: int ref_update_check_old_target(const char *referent, struct ref_update *update,
    - }
    - 
    - struct migration_data {
    -+	unsigned int reflog_index;
    +@@ refs.c: struct migration_data {
      	struct ref_store *old_refs;
      	struct ref_transaction *transaction;
      	struct strbuf *errbuf;
    ++	struct strbuf sb;
    + };
    + 
    + static int migrate_one_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
     @@ refs.c: static int migrate_one_ref(const char *refname, const char *referent UNUSED, con
      	return ret;
      }
      
     +struct reflog_migration_data {
    -+	unsigned int *index;
    ++	unsigned int index;
     +	const char *refname;
     +	struct ref_store *old_refs;
     +	struct ref_transaction *transaction;
     +	struct strbuf *errbuf;
    ++	struct strbuf *sb;
     +};
     +
     +static int migrate_one_reflog_entry(struct object_id *old_oid,
    @@ refs.c: static int migrate_one_ref(const char *refname, const char *referent UNU
     +				    const char *msg, void *cb_data)
     +{
     +	struct reflog_migration_data *data = cb_data;
    -+	struct strbuf sb = STRBUF_INIT;
     +	const char *date;
     +	int ret;
     +
     +	date = show_date(timestamp, tz, DATE_MODE(NORMAL));
    ++	strbuf_reset(data->sb);
     +	/* committer contains name and email */
    -+	strbuf_addstr(&sb, fmt_ident("", committer, WANT_BLANK_IDENT, date, 0));
    ++	strbuf_addstr(data->sb, fmt_ident("", committer, WANT_BLANK_IDENT, date, 0));
     +
     +	ret = ref_transaction_update_reflog(data->transaction, data->refname,
    -+					    new_oid, old_oid, sb.buf,
    ++					    new_oid, old_oid, data->sb->buf,
     +					    REF_HAVE_NEW | REF_HAVE_OLD, msg,
    -+					    (*data->index)++, data->errbuf);
    -+	strbuf_release(&sb);
    -+
    ++					    data->index++, data->errbuf);
     +	return ret;
     +}
     +
    @@ refs.c: static int migrate_one_ref(const char *refname, const char *referent UNU
     +	data.old_refs = migration_data->old_refs;
     +	data.transaction = migration_data->transaction;
     +	data.errbuf = migration_data->errbuf;
    -+	data.index = &migration_data->reflog_index;
    ++	data.sb = &migration_data->sb;
     +
     +	return refs_for_each_reflog_ent(migration_data->old_refs, refname,
     +					migrate_one_reflog_entry, &data);
    @@ refs.c: int repo_migrate_ref_storage_format(struct repository *repo,
      	 */
      	strbuf_addf(&new_gitdir, "%s/%s", old_refs->gitdir, "ref_migration.XXXXXX");
      	if (!mkdtemp(new_gitdir.buf)) {
    +@@ refs.c: int repo_migrate_ref_storage_format(struct repository *repo,
    + 	data.old_refs = old_refs;
    + 	data.transaction = transaction;
    + 	data.errbuf = errbuf;
    ++	strbuf_init(&data.sb, 0);
    + 
    + 	/*
    + 	 * We need to use the internal `do_for_each_ref()` here so that we can
     @@ refs.c: int repo_migrate_ref_storage_format(struct repository *repo,
      	if (ret < 0)
      		goto done;
      
    -+	data.reflog_index = 1;
     +	ret = refs_for_each_reflog(old_refs, migrate_one_reflog, &data);
     +	if (ret < 0)
     +		goto done;
    @@ refs.c: int repo_migrate_ref_storage_format(struct repository *repo,
      	ret = ref_transaction_commit(transaction, errbuf);
      	if (ret < 0)
      		goto done;
    +@@ refs.c: int repo_migrate_ref_storage_format(struct repository *repo,
    + 	}
    + 	ref_transaction_free(transaction);
    + 	strbuf_release(&new_gitdir);
    ++	strbuf_release(&data.sb);
    + 	return ret;
    + }
    + 
     
      ## t/t1460-refs-migrate.sh ##
     @@ t/t1460-refs-migrate.sh: export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME


--- 

base-commit: 09245f4b75863f4e94dac7feebaafce53a26965f
change-id: 20241111-320-git-refs-migrate-reflogs-a53e3a6cffc9

Thanks
- Karthik





[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux