refs.c: add transaction.status and track OPEN/CLOSED
Track the state of a transaction in a new state field. Check the field for sanity, i.e. that state must be OPEN when _commit/_create/_delete or _update is called or else die(BUG:...) Signed-off-by: Ronnie Sahlberg <sahlberg@google.com> Reviewed-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ronnie Sahlberg committed
Apr 29, 2014 at 12:06 UTC
2bdc785fd7f699669a582f7a83f5b81192d24d88
1 file changed
+33
-1
refs.c
+33
-1
@@ -3386,6 +3386,21 @@ struct ref_update {
3386
const char refname[FLEX_ARRAY];
3387
};
3388
3389
+/*
3390
+ * Transaction states.
3391
+ * OPEN: The transaction is in a valid state and can accept new updates.
3392
+ * An OPEN transaction can be committed.
3393
+ * CLOSED: A closed transaction is no longer active and no other operations
3394
+ * than free can be used on it in this state.
3395
+ * A transaction can either become closed by successfully committing
3396
+ * an active transaction or if there is a failure while building
3397
+ * the transaction thus rendering it failed/inactive.
3398
+ */
3399
+enum ref_transaction_state {
3400
+ REF_TRANSACTION_OPEN = 0,
3401
+ REF_TRANSACTION_CLOSED = 1
3402
+};
3403
+
3404
/*
3405
* Data structure for holding a reference transaction, which can
3406
* consist of checks and updates to multiple references, carried out
@@ -3395,6 +3410,7 @@ struct ref_transaction {
3410
struct ref_update **updates;
3411
size_t alloc;
3412
size_t nr;
3413
+ enum ref_transaction_state state;
3414
};
3415
3416
struct ref_transaction *ref_transaction_begin(struct strbuf *err)
@@ -3437,6 +3453,9 @@ int ref_transaction_update(struct ref_transaction *transaction,
3453
{
3454
struct ref_update *update;
3455
3456
+ if (transaction->state != REF_TRANSACTION_OPEN)
3457
+ die("BUG: update called for transaction that is not open");
3458
+
3459
if (have_old && !old_sha1)
3460
die("BUG: have_old is true but old_sha1 is NULL");
3461
@@ -3457,6 +3476,9 @@ int ref_transaction_create(struct ref_transaction *transaction,
3476
{
3477
struct ref_update *update;
3478
3479
+ if (transaction->state != REF_TRANSACTION_OPEN)
3480
+ die("BUG: create called for transaction that is not open");
3481
+
3482
if (!new_sha1 || is_null_sha1(new_sha1))
3483
die("BUG: create ref with null new_sha1");
3484
@@ -3477,6 +3499,9 @@ int ref_transaction_delete(struct ref_transaction *transaction,
3499
{
3500
struct ref_update *update;
3501
3502
+ if (transaction->state != REF_TRANSACTION_OPEN)
3503
+ die("BUG: delete called for transaction that is not open");
3504
+
3505
if (have_old && !old_sha1)
3506
die("BUG: have_old is true but old_sha1 is NULL");
3507
@@ -3532,8 +3557,13 @@ int ref_transaction_commit(struct ref_transaction *transaction,
3557
int n = transaction->nr;
3558
struct ref_update **updates = transaction->updates;
3559
3535
- if (!n)
3560
+ if (transaction->state != REF_TRANSACTION_OPEN)
3561
+ die("BUG: commit called for transaction that is not open");
3562
+
3563
+ if (!n) {
3564
+ transaction->state = REF_TRANSACTION_CLOSED;
3565
return 0;
3566
+ }
3567
3568
/* Allocate work space */
3569
delnames = xmalloc(sizeof(*delnames) * n);
@@ -3595,6 +3625,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,
3625
clear_loose_ref_cache(&ref_cache);
3626
3627
cleanup:
3628
+ transaction->state = REF_TRANSACTION_CLOSED;
3629
+
3630
for (i = 0; i < n; i++)
3631
if (updates[i]->lock)
3632
unlock_ref(updates[i]->lock);