struct ref_update: add a lock field
Now that we manage ref_update objects internally, we can use them to hold some of the scratch space we need when actually carrying out the updates. Store the (struct ref_lock *) there. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michael Haggerty committed
Apr 7, 2014 at 15:48 UTC
81c960e4dcbd69e28b031cbe370100cb28acb911
1 file changed
+19
-17
refs.c
+19
-17
@@ -3278,6 +3278,7 @@ struct ref_update {
3278
unsigned char old_sha1[20];
3279
int flags; /* REF_NODEREF? */
3280
int have_old; /* 1 if old_sha1 is valid, 0 otherwise */
3281
+ struct ref_lock *lock;
3282
const char refname[FLEX_ARRAY];
3283
};
3284
@@ -3413,7 +3414,6 @@ int ref_transaction_commit(struct ref_transaction *transaction,
3414
int ret = 0, delnum = 0, i;
3415
struct ref_update **updates;
3416
int *types;
3416
- struct ref_lock **locks;
3417
const char **delnames;
3418
int n = transaction->nr;
3419
@@ -3423,7 +3423,6 @@ int ref_transaction_commit(struct ref_transaction *transaction,
3423
/* Allocate work space */
3424
updates = xmalloc(sizeof(*updates) * n);
3425
types = xmalloc(sizeof(*types) * n);
3426
- locks = xcalloc(n, sizeof(*locks));
3426
delnames = xmalloc(sizeof(*delnames) * n);
3427
3428
/* Copy, sort, and reject duplicate refs */
@@ -3437,12 +3436,12 @@ int ref_transaction_commit(struct ref_transaction *transaction,
3436
for (i = 0; i < n; i++) {
3437
struct ref_update *update = updates[i];
3438
3440
- locks[i] = update_ref_lock(update->refname,
3441
- (update->have_old ?
3442
- update->old_sha1 : NULL),
3443
- update->flags,
3444
- &types[i], onerr);
3445
- if (!locks[i]) {
3439
+ update->lock = update_ref_lock(update->refname,
3440
+ (update->have_old ?
3441
+ update->old_sha1 : NULL),
3442
+ update->flags,
3443
+ &types[i], onerr);
3444
+ if (!update->lock) {
3445
ret = 1;
3446
goto cleanup;
3447
}
@@ -3456,19 +3455,23 @@ int ref_transaction_commit(struct ref_transaction *transaction,
3455
ret = update_ref_write(msg,
3456
update->refname,
3457
update->new_sha1,
3459
- locks[i], onerr);
3460
- locks[i] = NULL; /* freed by update_ref_write */
3458
+ update->lock, onerr);
3459
+ update->lock = NULL; /* freed by update_ref_write */
3460
if (ret)
3461
goto cleanup;
3462
}
3463
}
3464
3465
/* Perform deletes now that updates are safely completed */
3467
- for (i = 0; i < n; i++)
3468
- if (locks[i]) {
3469
- delnames[delnum++] = locks[i]->ref_name;
3470
- ret |= delete_ref_loose(locks[i], types[i]);
3466
+ for (i = 0; i < n; i++) {
3467
+ struct ref_update *update = updates[i];
3468
+
3469
+ if (update->lock) {
3470
+ delnames[delnum++] = update->lock->ref_name;
3471
+ ret |= delete_ref_loose(update->lock, types[i]);
3472
}
3473
+ }
3474
+
3475
ret |= repack_without_refs(delnames, delnum);
3476
for (i = 0; i < delnum; i++)
3477
unlink_or_warn(git_path("logs/%s", delnames[i]));
@@ -3476,11 +3479,10 @@ int ref_transaction_commit(struct ref_transaction *transaction,
3479
3480
cleanup:
3481
for (i = 0; i < n; i++)
3479
- if (locks[i])
3480
- unlock_ref(locks[i]);
3482
+ if (updates[i]->lock)
3483
+ unlock_ref(updates[i]->lock);
3484
free(updates);
3485
free(types);
3483
- free(locks);
3486
free(delnames);
3487
ref_transaction_free(transaction);
3488
return ret;