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

[PATCH v2 03/12] struct ref_update: move "have_old" into "flags"

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Feb 12, 2015, 11:12 UTC
Message-ID
<1423739543-1025-4-git-send-email-mhagger@alum.mit.edu>
In-Reply-To
<1423739543-1025-1-git-send-email-mhagger@alum.mit.edu>

Instead of having a separate have_old field, record this boolean value as a bit in the "flags" field.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 refs.c | 45 ++++++++++++++++++++++++++++-----------------
 1 file changed, 28 insertions(+), 17 deletions(-)
diff --git a/refs.c b/refs.c
index 4de1383..6cfc07f 100644
--- a/refs.c
+++ b/refs.c
@@ -41,12 +41,18 @@ static unsigned char refname_disposition[256] = {
 #define REF_DELETING	0x02
 
 /*
- * Used as a flag to ref_transaction_delete when a loose ref is being
+ * Used as a flag in ref_update::flags when a loose ref is being
  * pruned.
  */
 #define REF_ISPRUNING	0x04
 
 /*
+ * Used as a flag in ref_update::flags when old_sha1 should be
+ * checked.
+ */
+#define REF_HAVE_OLD	0x08
+
+/*
  * Try to read one refname component from the front of refname.
  * Return the length of the component found, or -1 if the component is
  * not legal.  It is legal if it is something reasonable to have under
@@ -3563,16 +3569,20 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)
 }
 
 /**
- * Information needed for a single ref update.  Set new_sha1 to the
- * new value or to zero to delete the ref.  To check the old value
- * while locking the ref, set have_old to 1 and set old_sha1 to the
- * value or to zero to ensure the ref does not exist before update.
+ * Information needed for a single ref update. Set new_sha1 to the new
+ * value or to null_sha1 to delete the ref. To check the old value
+ * while the ref is locked, set (flags & REF_HAVE_OLD) and set
+ * old_sha1 to the old value, or to null_sha1 to ensure the ref does
+ * not exist before update.
  */
 struct ref_update {
 	unsigned char new_sha1[20];
 	unsigned char old_sha1[20];
-	int flags; /* REF_NODEREF? */
-	int have_old; /* 1 if old_sha1 is valid, 0 otherwise */
+	/*
+	 * One or more of REF_HAVE_OLD, REF_NODEREF,
+	 * REF_DELETING, and REF_IS_PRUNING:
+	 */
+	int flags;
 	struct ref_lock *lock;
 	int type;
 	char *msg;
@@ -3666,10 +3676,11 @@ int ref_transaction_update(struct ref_transaction *transaction,
 
 	update = add_update(transaction, refname);
 	hashcpy(update->new_sha1, new_sha1);
-	update->flags = flags;
-	update->have_old = have_old;
-	if (have_old)
+	if (have_old) {
 		hashcpy(update->old_sha1, old_sha1);
+		flags |= REF_HAVE_OLD;
+	}
+	update->flags = flags;
 	if (msg)
 		update->msg = xstrdup(msg);
 	return 0;
@@ -3785,13 +3796,13 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 
 		if (is_null_sha1(update->new_sha1))
 			flags |= REF_DELETING;
-		update->lock = lock_ref_sha1_basic(update->refname,
-						   (update->have_old ?
-						    update->old_sha1 :
-						    NULL),
-						   NULL,
-						   flags,
-						   &update->type);
+		update->lock = lock_ref_sha1_basic(
+				update->refname,
+				((update->flags & REF_HAVE_OLD) ?
+				 update->old_sha1 : NULL),
+				NULL,
+				flags,
+				&update->type);
 		if (!update->lock) {
 			ret = (errno == ENOTDIR)
 				? TRANSACTION_NAME_CONFLICT
-- 
2.1.4
Previous: Michael HaggertyNext: Stefan Beller
Message 4 of 27 in “Allow reference values to be checked in a transaction”
  1. 00/12 Allow reference values to be checked in a transactionMichael Haggerty, Feb 12, 2015
  2. 01/12 refs: move REF_DELETING to refs.cMichael Haggerty, Feb 12, 2015
  3. 02/12 refs: remove the gap in the REF_* constant valuesMichael Haggerty, Feb 12, 2015
  4. 03/12 struct ref_update: move "have_old" into "flags"Michael Haggerty, Feb 12, 2015
  5. Stefan BellerFeb 12, 2015
  6. Junio C HamanoFeb 12, 2015
  7. Michael HaggertyFeb 17, 2015
  8. Junio C HamanoFeb 17, 2015
  9. 04/12 ref_transaction_update(): remove "have_old" parameterMichael Haggerty, Feb 12, 2015
  10. Junio C HamanoFeb 12, 2015
  11. Michael HaggertyFeb 13, 2015
  12. Junio C HamanoFeb 13, 2015
  13. 05/12 ref_transaction_delete(): remove "have_old" parameterMichael Haggerty, Feb 12, 2015
  14. 06/12 commit: add tests of commit racesMichael Haggerty, Feb 12, 2015
  15. Stefan BellerFeb 12, 2015
  16. Michael HaggertyFeb 17, 2015
  17. Junio C HamanoFeb 12, 2015
  18. Michael HaggertyFeb 17, 2015
  19. 07/12 commit: avoid race when creating orphan commitsMichael Haggerty, Feb 12, 2015
  20. Junio C HamanoFeb 12, 2015
  21. 08/12 ref_transaction_create(): check that new_sha1 is validMichael Haggerty, Feb 12, 2015
  22. 09/12 ref_transaction_delete(): check that old_sha1 is not null_sha1Michael Haggerty, Feb 12, 2015
  23. 10/12 ref_transaction_verify(): new function to check a reference's valueMichael Haggerty, Feb 12, 2015
  24. Stefan BellerFeb 12, 2015
  25. 11/12 update_ref(): improve documentationMichael Haggerty, Feb 12, 2015
  26. 12/12 refs.h: Remove duplication in function docstringsMichael Haggerty, Feb 12, 2015
  27. Junio C HamanoFeb 12, 2015

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.