Track the status of a transaction in a new status field. Check the field for
sanity, i.e. that status must be OPEN when _commit/_create/_delete or
_update is called or else die(BUG:...)

Signed-off-by: Ronnie Sahlberg <sahlb...@google.com>
---
 refs.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/refs.c b/refs.c
index 733dfb3..3fb5cb8 100644
--- a/refs.c
+++ b/refs.c
@@ -3336,6 +3336,12 @@ struct ref_update {
        const char refname[FLEX_ARRAY];
 };
 
+enum ref_transaction_status {
+       REF_TRANSACTION_OPEN   = 0,
+       REF_TRANSACTION_CLOSED = 1,
+       REF_TRANSACTION_ERROR  = 2,
+};
+
 /*
  * Data structure for holding a reference transaction, which can
  * consist of checks and updates to multiple references, carried out
@@ -3345,6 +3351,7 @@ struct ref_transaction {
        struct ref_update **updates;
        size_t alloc;
        size_t nr;
+       enum ref_transaction_status status;
 };
 
 struct ref_transaction *ref_transaction_begin(void)
@@ -3368,6 +3375,11 @@ void ref_transaction_free(struct ref_transaction 
*transaction)
 
 void ref_transaction_rollback(struct ref_transaction *transaction)
 {
+       if (!transaction)
+               return;
+
+       transaction->status = REF_TRANSACTION_ERROR;
+
        ref_transaction_free(transaction);
 }
 
@@ -3395,6 +3407,9 @@ int ref_transaction_update(struct ref_transaction 
*transaction,
        if (have_old && !old_sha1)
                die("BUG: have_old is true but old_sha1 is NULL");
 
+       if (transaction->status != REF_TRANSACTION_OPEN)
+               die("BUG: update on transaction that is not open");
+
        update = add_update(transaction, refname);
        hashcpy(update->new_sha1, new_sha1);
        update->flags = flags;
@@ -3415,6 +3430,9 @@ int ref_transaction_create(struct ref_transaction 
*transaction,
        if (!new_sha1 || is_null_sha1(new_sha1))
                die("BUG: create ref with null new_sha1");
 
+       if (transaction->status != REF_TRANSACTION_OPEN)
+               die("BUG: create on transaction that is not open");
+
        update = add_update(transaction, refname);
 
        hashcpy(update->new_sha1, new_sha1);
@@ -3435,6 +3453,9 @@ int ref_transaction_delete(struct ref_transaction 
*transaction,
        if (have_old && !old_sha1)
                die("BUG: have_old is true but old_sha1 is NULL");
 
+       if (transaction->status != REF_TRANSACTION_OPEN)
+               die("BUG: delete on transaction that is not open");
+
        update = add_update(transaction, refname);
        update->flags = flags;
        update->have_old = have_old;
@@ -3505,8 +3526,13 @@ int ref_transaction_commit(struct ref_transaction 
*transaction,
        int n = transaction->nr;
        struct ref_update **updates = transaction->updates;
 
-       if (!n)
+       if (transaction->status != REF_TRANSACTION_OPEN)
+               die("BUG: commit on transaction that is not open");
+
+       if (!n) {
+               transaction->status = REF_TRANSACTION_CLOSED;
                return 0;
+       }
 
        /* Allocate work space */
        delnames = xmalloc(sizeof(*delnames) * n);
@@ -3569,6 +3595,9 @@ int ref_transaction_commit(struct ref_transaction 
*transaction,
        clear_loose_ref_cache(&ref_cache);
 
 cleanup:
+       transaction->status = ret ? REF_TRANSACTION_ERROR
+         : REF_TRANSACTION_CLOSED;
+
        for (i = 0; i < n; i++)
                if (updates[i]->lock)
                        unlock_ref(updates[i]->lock);
-- 
2.0.0.rc3.477.gffe78a2

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to