Subject: [PATCH 2/8] reftable-backend: extract out `write_symref_with_log`

From: Karthik Nayak <[email-masked]>
Date: Sat, 30 Mar 2024 23:46:17 +0100

From: Karthik Nayak <[email-masked]>

The function `write_create_symref_table`, creates a
`reftable_ref_record` for a symref and adds it to the writer. Then it
also creates a log entry for the symref. It does all of this while also
obtaining and using a new update index.

We extract out `write_symref_with_log` from this to provide the
functionality of creating a symref without making changes to the update
index. This will be used to add `update-symref` option to the
`git-update-ref` command.

Rename the `create` field to `arg` while we're here, as `create` is a
bit misleading.

Signed-off-by: Karthik Nayak <[email-masked]>
---
 refs/reftable-backend.c | 41 +++++++++++++++++++++++++----------------
 1 file changed, 25 insertions(+), 16 deletions(-)

diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c
index e206d5a073..282a08e3cb 100644
--- a/refs/reftable-backend.c
+++ b/refs/reftable-backend.c
@@ -1222,23 +1222,22 @@ struct write_create_symref_arg {
 	const char *logmsg;
 };
 
-static int write_create_symref_table(struct reftable_writer *writer, void *cb_data)
+static int write_symref_with_log(struct reftable_writer *writer,
+				 struct write_create_symref_arg *arg,
+				 uint64_t update_index)
 {
-	struct write_create_symref_arg *create = cb_data;
-	uint64_t ts = reftable_stack_next_update_index(create->stack);
 	struct reftable_ref_record ref = {
-		.refname = (char *)create->refname,
+		.refname = (char *)arg->refname,
 		.value_type = REFTABLE_REF_SYMREF,
-		.value.symref = (char *)create->target,
-		.update_index = ts,
+		.value.symref = (char *)arg->target,
+		.update_index = update_index,
 	};
+
 	struct reftable_log_record log = {0};
 	struct object_id new_oid;
 	struct object_id old_oid;
 	int ret;
 
-	reftable_writer_set_limits(writer, ts, ts);
-
 	ret = reftable_writer_add_ref(writer, &ref);
 	if (ret)
 		return ret;
@@ -1251,25 +1250,35 @@ static int write_create_symref_table(struct reftable_writer *writer, void *cb_da
 	 * not resolve for new repositories this ordering will ensure that this
 	 * never happens.
 	 */
-	if (!create->logmsg ||
-	    !refs_resolve_ref_unsafe(&create->refs->base, create->target,
+	if (!arg->logmsg ||
+	    !refs_resolve_ref_unsafe(&arg->refs->base, arg->target,
 				     RESOLVE_REF_READING, &new_oid, NULL) ||
-	    !should_write_log(&create->refs->base, create->refname))
+	    !should_write_log(&arg->refs->base, arg->refname))
 		return 0;
 
 	fill_reftable_log_record(&log);
-	log.refname = xstrdup(create->refname);
-	log.update_index = ts;
-	log.value.update.message = xstrndup(create->logmsg,
-					    create->refs->write_options.block_size / 2);
+	log.refname = xstrdup(arg->refname);
+	log.update_index = update_index;
+	log.value.update.message = xstrndup(arg->logmsg,
+					    arg->refs->write_options.block_size / 2);
 	memcpy(log.value.update.new_hash, new_oid.hash, GIT_MAX_RAWSZ);
-	if (refs_resolve_ref_unsafe(&create->refs->base, create->refname,
+	if (refs_resolve_ref_unsafe(&arg->refs->base, arg->refname,
 				    RESOLVE_REF_READING, &old_oid, NULL))
 		memcpy(log.value.update.old_hash, old_oid.hash, GIT_MAX_RAWSZ);
 
 	ret = reftable_writer_add_log(writer, &log);
 	reftable_log_record_release(&log);
 	return ret;
+
+}
+
+static int write_create_symref_table(struct reftable_writer *writer, void *cb_data)
+{
+	struct write_create_symref_arg *arg = cb_data;
+	uint64_t ts = reftable_stack_next_update_index(arg->stack);
+	reftable_writer_set_limits(writer, ts, ts);
+
+	return write_symref_with_log(writer, arg, ts);
 }
 
 static int reftable_be_create_symref(struct ref_store *ref_store,
-- 
2.43.GIT