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

[PATCH 03/24] refs: convert delete_ref and refs_delete_ref to struct object_id

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Oct 1, 2017, 22:08 UTC
Message-ID
<20171001220831.214705-4-sandals@crustytoothpaste.net>
In-Reply-To
<20171001220831.214705-1-sandals@crustytoothpaste.net>

Convert delete_ref and refs_delete_ref to take a pointer to struct object_id. Update the documentation accordingly, including referring to null_oid in lowercase, as it is not a #define constant.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 builtin/branch.c          |  2 +-
 builtin/replace.c         |  2 +-
 builtin/reset.c           |  2 +-
 builtin/tag.c             |  2 +-
 builtin/update-ref.c      |  2 +-
 refs.c                    | 21 +++++++++++----------
 refs.h                    | 12 ++++++------
 refs/files-backend.c      |  2 +-
 t/helper/test-ref-store.c |  6 +++---
 9 files changed, 26 insertions(+), 25 deletions(-)
diff --git a/builtin/branch.c b/builtin/branch.c
index 355f9ef5da..6031b74d68 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -256,7 +256,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 			goto next;
 		}
 
-		if (delete_ref(NULL, name, is_null_oid(&oid) ? NULL : oid.hash,
+		if (delete_ref(NULL, name, is_null_oid(&oid) ? NULL : &oid,
 			       REF_NODEREF)) {
 			error(remote_branch
 			      ? _("Error deleting remote-tracking branch '%s'")
diff --git a/builtin/replace.c b/builtin/replace.c
index 3e71a77152..2854eaa0f3 100644
--- a/builtin/replace.c
+++ b/builtin/replace.c
@@ -128,7 +128,7 @@ static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
 static int delete_replace_ref(const char *name, const char *ref,
 			      const struct object_id *oid)
 {
-	if (delete_ref(NULL, ref, oid->hash, 0))
+	if (delete_ref(NULL, ref, oid, 0))
 		return 1;
 	printf("Deleted replace ref '%s'\n", name);
 	return 0;
diff --git a/builtin/reset.c b/builtin/reset.c
index 9cd89b2305..5f3632e05b 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -269,7 +269,7 @@ static int reset_refs(const char *rev, const struct object_id *oid)
 		update_ref_oid(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
 			   UPDATE_REFS_MSG_ON_ERR);
 	} else if (old_orig)
-		delete_ref(NULL, "ORIG_HEAD", old_orig->hash, 0);
+		delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
 	set_reflog_message(&msg, "updating HEAD", rev);
 	update_ref_status = update_ref_oid(msg.buf, "HEAD", oid, orig, 0,
 				       UPDATE_REFS_MSG_ON_ERR);
diff --git a/builtin/tag.c b/builtin/tag.c
index c627794181..46ff4ca736 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -97,7 +97,7 @@ static int for_each_tag_name(const char **argv, each_tag_name_fn fn,
 static int delete_tag(const char *name, const char *ref,
 		      const struct object_id *oid, const void *cb_data)
 {
-	if (delete_ref(NULL, ref, oid->hash, 0))
+	if (delete_ref(NULL, ref, oid, 0))
 		return 1;
 	printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(oid->hash, DEFAULT_ABBREV));
 	return 0;
diff --git a/builtin/update-ref.c b/builtin/update-ref.c
index 6b90c5dead..bf0f80ebae 100644
--- a/builtin/update-ref.c
+++ b/builtin/update-ref.c
@@ -434,7 +434,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 		 * NULL_SHA1 as "don't care" here:
 		 */
 		return delete_ref(msg, refname,
-				  (oldval && !is_null_oid(&oldoid)) ? oldoid.hash : NULL,
+				  (oldval && !is_null_oid(&oldoid)) ? &oldoid : NULL,
 				  flags);
 	else
 		return update_ref(msg, refname, oid.hash, oldval ? oldoid.hash : NULL,
diff --git a/refs.c b/refs.c
index 6042645c40..0a5b68d6fb 100644
--- a/refs.c
+++ b/refs.c
@@ -620,25 +620,25 @@ static int write_pseudoref(const char *pseudoref, const unsigned char *sha1,
 	return ret;
 }
 
-static int delete_pseudoref(const char *pseudoref, const unsigned char *old_sha1)
+static int delete_pseudoref(const char *pseudoref, const struct object_id *old_oid)
 {
 	static struct lock_file lock;
 	const char *filename;
 
 	filename = git_path("%s", pseudoref);
 
-	if (old_sha1 && !is_null_sha1(old_sha1)) {
+	if (old_oid && !is_null_oid(old_oid)) {
 		int fd;
-		unsigned char actual_old_sha1[20];
+		struct object_id actual_old_oid;
 
 		fd = hold_lock_file_for_update_timeout(
 				&lock, filename, LOCK_DIE_ON_ERROR,
 				get_files_ref_lock_timeout_ms());
 		if (fd < 0)
 			die_errno(_("Could not open '%s' for writing"), filename);
-		if (read_ref(pseudoref, actual_old_sha1))
+		if (read_ref(pseudoref, actual_old_oid.hash))
 			die("could not read ref '%s'", pseudoref);
-		if (hashcmp(actual_old_sha1, old_sha1)) {
+		if (oidcmp(&actual_old_oid, old_oid)) {
 			warning("Unexpected sha1 when deleting %s", pseudoref);
 			rollback_lock_file(&lock);
 			return -1;
@@ -655,7 +655,7 @@ static int delete_pseudoref(const char *pseudoref, const unsigned char *old_sha1
 
 int refs_delete_ref(struct ref_store *refs, const char *msg,
 		    const char *refname,
-		    const unsigned char *old_sha1,
+		    const struct object_id *old_oid,
 		    unsigned int flags)
 {
 	struct ref_transaction *transaction;
@@ -663,12 +663,13 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,
 
 	if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
 		assert(refs == get_main_ref_store());
-		return delete_pseudoref(refname, old_sha1);
+		return delete_pseudoref(refname, old_oid);
 	}
 
 	transaction = ref_store_transaction_begin(refs, &err);
 	if (!transaction ||
-	    ref_transaction_delete(transaction, refname, old_sha1,
+	    ref_transaction_delete(transaction, refname,
+				   old_oid ? old_oid->hash : NULL,
 				   flags, msg, &err) ||
 	    ref_transaction_commit(transaction, &err)) {
 		error("%s", err.buf);
@@ -682,10 +683,10 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,
 }
 
 int delete_ref(const char *msg, const char *refname,
-	       const unsigned char *old_sha1, unsigned int flags)
+	       const struct object_id *old_oid, unsigned int flags)
 {
 	return refs_delete_ref(get_main_ref_store(), msg, refname,
-			       old_sha1, flags);
+			       old_oid, flags);
 }
 
 int copy_reflog_msg(char *buf, const char *msg)
diff --git a/refs.h b/refs.h
index e1c5803f9a..07e9907b31 100644
--- a/refs.h
+++ b/refs.h
@@ -371,19 +371,19 @@ int refs_reflog_exists(struct ref_store *refs, const char *refname);
 int reflog_exists(const char *refname);
 
 /*
- * Delete the specified reference. If old_sha1 is non-NULL, then
+ * Delete the specified reference. If old_oid is non-NULL, then
  * verify that the current value of the reference is old_sha1 before
- * deleting it. If old_sha1 is NULL, delete the reference if it
- * exists, regardless of its old value. It is an error for old_sha1 to
- * be NULL_SHA1. msg and flags are passed through to
+ * deleting it. If old_oid is NULL, delete the reference if it
+ * exists, regardless of its old value. It is an error for old_oid to
+ * be null_oid. msg and flags are passed through to
  * ref_transaction_delete().
  */
 int refs_delete_ref(struct ref_store *refs, const char *msg,
 		    const char *refname,
-		    const unsigned char *old_sha1,
+		    const struct object_id *old_oid,
 		    unsigned int flags);
 int delete_ref(const char *msg, const char *refname,
-	       const unsigned char *old_sha1, unsigned int flags);
+	       const struct object_id *old_oid, unsigned int flags);
 
 /*
  * Delete the specified references. If there are any problems, emit
diff --git a/refs/files-backend.c b/refs/files-backend.c
index e3968d4f7c..0c641f0ad0 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -1308,7 +1308,7 @@ static int files_rename_ref(struct ref_store *ref_store,
 	}
 
 	if (refs_delete_ref(&refs->base, logmsg, oldrefname,
-			    orig_oid.hash, REF_NODEREF)) {
+			    &orig_oid, REF_NODEREF)) {
 		error("unable to delete old %s", oldrefname);
 		goto rollback;
 	}
diff --git a/t/helper/test-ref-store.c b/t/helper/test-ref-store.c
index 05d8c4d8af..4624238641 100644
--- a/t/helper/test-ref-store.c
+++ b/t/helper/test-ref-store.c
@@ -218,12 +218,12 @@ static int cmd_delete_ref(struct ref_store *refs, const char **argv)
 	const char *refname = notnull(*argv++, "refname");
 	const char *sha1_buf = notnull(*argv++, "old-sha1");
 	unsigned int flags = arg_flags(*argv++, "flags");
-	unsigned char old_sha1[20];
+	struct object_id old_oid;
 
-	if (get_sha1_hex(sha1_buf, old_sha1))
+	if (get_oid_hex(sha1_buf, &old_oid))
 		die("not sha-1");
 
-	return refs_delete_ref(refs, msg, refname, old_sha1, flags);
+	return refs_delete_ref(refs, msg, refname, &old_oid, flags);
 }
 
 static int cmd_update_ref(struct ref_store *refs, const char **argv)
-- 
2.14.2.822.g60be5d43e6
Previous: brian m. carlsonNext: brian m. carlson
Message 6 of 34 in “object_id part 10”
  1. 00/24 object_id part 10brian m. carlson, Oct 1, 2017
  2. 01/24 walker: convert to struct object_idbrian m. carlson, Oct 1, 2017
  3. 02/24 refs/files-backend: convert struct ref_to_prune to object_idbrian m. carlson, Oct 1, 2017
  4. 05/24 refs: update ref transactions to use struct object_idbrian m. carlson, Oct 1, 2017
  5. 07/24 refs: convert resolve_refdup and refs_resolve_refdup to struct object_idbrian m. carlson, Oct 1, 2017
  6. 03/24 refs: convert delete_ref and refs_delete_ref to struct object_idbrian m. carlson, Oct 1, 2017
  7. 08/24 refs: convert read_ref and read_ref_full to object_idbrian m. carlson, Oct 1, 2017
  8. 06/24 Convert check_connected to use struct object_idbrian m. carlson, Oct 1, 2017
  9. Stefan BellerOct 2, 2017
  10. 11/24 refs: convert dwim_log to struct object_idbrian m. carlson, Oct 1, 2017
  11. 15/24 refs: convert read_ref_at to struct object_idbrian m. carlson, Oct 1, 2017
  12. Brandon WilliamsOct 2, 2017
  13. 17/24 sha1_file: convert index_path and index_fd to struct object_idbrian m. carlson, Oct 1, 2017
  14. 24/24 refs/files-backend: convert static functions to object_idbrian m. carlson, Oct 1, 2017
  15. 23/24 refs: convert read_raw_ref backends to struct object_idbrian m. carlson, Oct 1, 2017
  16. 22/24 refs: convert peel_object to struct object_idbrian m. carlson, Oct 1, 2017
  17. 16/24 refs: convert reflog_expire parameter to struct object_idbrian m. carlson, Oct 1, 2017
  18. 20/24 worktree: convert struct worktree to object_idbrian m. carlson, Oct 1, 2017
  19. 21/24 refs: convert resolve_ref_unsafe to struct object_idbrian m. carlson, Oct 1, 2017
  20. 14/24 refs: convert peel_ref to struct object_idbrian m. carlson, Oct 1, 2017
  21. 18/24 Convert remaining callers of resolve_gitlink_ref to object_idbrian m. carlson, Oct 1, 2017
  22. 19/24 refs: convert resolve_gitlink_ref to struct object_idbrian m. carlson, Oct 1, 2017
  23. 09/24 refs: convert dwim_ref and expand_ref to struct object_idbrian m. carlson, Oct 1, 2017
  24. 13/24 builtin/pack-objects: convert to struct object_idbrian m. carlson, Oct 1, 2017
  25. Stefan BellerOct 2, 2017
  26. 10/24 builtin/reflog: convert remaining unsigned char uses to object_idbrian m. carlson, Oct 1, 2017
  27. 12/24 pack-bitmap: convert traverse_bitmap_commit_list to object_idbrian m. carlson, Oct 1, 2017
  28. 04/24 refs: convert update_ref and refs_update_ref to use struct object_idbrian m. carlson, Oct 1, 2017
  29. Stefan BellerOct 2, 2017
  30. brian m. carlsonOct 3, 2017
  31. Brandon WilliamsOct 2, 2017
  32. Brandon WilliamsOct 2, 2017
  33. Stefan BellerOct 2, 2017
  34. brian m. carlsonOct 3, 2017

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.