Subject: [PATCH 3/8] reftable-backend: move `write_symref_with_log` up

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

From: Karthik Nayak <[email-masked]>

The `write_symref_with_log` code will be consequently used to provide
symref creation functionality in transactions. To do this, we move the
declaration up so it can be used accordingly.

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

diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c
index 282a08e3cb..9b53d42541 100644
--- a/refs/reftable-backend.c
+++ b/refs/reftable-backend.c
@@ -588,6 +588,64 @@ static const char *original_update_refname(struct ref_update *update)
 	return update->refname;
 }
 
+struct write_create_symref_arg {
+	struct reftable_ref_store *refs;
+	struct reftable_stack *stack;
+	const char *refname;
+	const char *target;
+	const char *logmsg;
+};
+
+static int write_symref_with_log(struct reftable_writer *writer,
+				 struct write_create_symref_arg *arg,
+				 uint64_t update_index)
+{
+	struct reftable_ref_record ref = {
+		.refname = (char *)arg->refname,
+		.value_type = REFTABLE_REF_SYMREF,
+		.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;
+
+	ret = reftable_writer_add_ref(writer, &ref);
+	if (ret)
+		return ret;
+
+	/*
+	 * Note that it is important to try and resolve the reference before we
+	 * write the log entry. This is because `should_write_log()` will munge
+	 * `core.logAllRefUpdates`, which is undesirable when we create a new
+	 * repository because it would be written into the config. As HEAD will
+	 * not resolve for new repositories this ordering will ensure that this
+	 * never happens.
+	 */
+	if (!arg->logmsg ||
+	    !refs_resolve_ref_unsafe(&arg->refs->base, arg->target,
+				     RESOLVE_REF_READING, &new_oid, NULL) ||
+	    !should_write_log(&arg->refs->base, arg->refname))
+		return 0;
+
+	fill_reftable_log_record(&log);
+	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(&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;
+
+}
+
 struct reftable_transaction_update {
 	struct ref_update *update;
 	struct object_id current_oid;
@@ -1214,64 +1272,6 @@ static int reftable_be_pack_refs(struct ref_store *ref_store,
 	return ret;
 }
 
-struct write_create_symref_arg {
-	struct reftable_ref_store *refs;
-	struct reftable_stack *stack;
-	const char *refname;
-	const char *target;
-	const char *logmsg;
-};
-
-static int write_symref_with_log(struct reftable_writer *writer,
-				 struct write_create_symref_arg *arg,
-				 uint64_t update_index)
-{
-	struct reftable_ref_record ref = {
-		.refname = (char *)arg->refname,
-		.value_type = REFTABLE_REF_SYMREF,
-		.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;
-
-	ret = reftable_writer_add_ref(writer, &ref);
-	if (ret)
-		return ret;
-
-	/*
-	 * Note that it is important to try and resolve the reference before we
-	 * write the log entry. This is because `should_write_log()` will munge
-	 * `core.logAllRefUpdates`, which is undesirable when we create a new
-	 * repository because it would be written into the config. As HEAD will
-	 * not resolve for new repositories this ordering will ensure that this
-	 * never happens.
-	 */
-	if (!arg->logmsg ||
-	    !refs_resolve_ref_unsafe(&arg->refs->base, arg->target,
-				     RESOLVE_REF_READING, &new_oid, NULL) ||
-	    !should_write_log(&arg->refs->base, arg->refname))
-		return 0;
-
-	fill_reftable_log_record(&log);
-	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(&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;
-- 
2.43.GIT