Skip to content

Commit c0ca935

Browse files
mhaggergitster
authored andcommitted
files_transaction_cleanup(): new helper function
Extract the cleanup functionality from `files_transaction_commit()` into a new function. It will soon have another caller. Use the common cleanup code even on early exit if the transaction is empty, to reduce code duplication. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 00d1744 commit c0ca935

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

refs/files-backend.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2834,6 +2834,27 @@ static int lock_ref_for_update(struct files_ref_store *refs,
28342834
return 0;
28352835
}
28362836

2837+
/*
2838+
* Unlock any references in `transaction` that are still locked, and
2839+
* mark the transaction closed.
2840+
*/
2841+
static void files_transaction_cleanup(struct ref_transaction *transaction)
2842+
{
2843+
size_t i;
2844+
2845+
for (i = 0; i < transaction->nr; i++) {
2846+
struct ref_update *update = transaction->updates[i];
2847+
struct ref_lock *lock = update->backend_data;
2848+
2849+
if (lock) {
2850+
unlock_ref(lock);
2851+
update->backend_data = NULL;
2852+
}
2853+
}
2854+
2855+
transaction->state = REF_TRANSACTION_CLOSED;
2856+
}
2857+
28372858
static int files_transaction_commit(struct ref_store *ref_store,
28382859
struct ref_transaction *transaction,
28392860
struct strbuf *err)
@@ -2856,10 +2877,8 @@ static int files_transaction_commit(struct ref_store *ref_store,
28562877
if (transaction->state != REF_TRANSACTION_OPEN)
28572878
die("BUG: commit called for transaction that is not open");
28582879

2859-
if (!transaction->nr) {
2860-
transaction->state = REF_TRANSACTION_CLOSED;
2861-
return 0;
2862-
}
2880+
if (!transaction->nr)
2881+
goto cleanup;
28632882

28642883
/*
28652884
* Fail if a refname appears more than once in the
@@ -3005,15 +3024,11 @@ static int files_transaction_commit(struct ref_store *ref_store,
30053024
clear_loose_ref_cache(refs);
30063025

30073026
cleanup:
3027+
files_transaction_cleanup(transaction);
30083028
strbuf_release(&sb);
3009-
transaction->state = REF_TRANSACTION_CLOSED;
30103029

30113030
for (i = 0; i < transaction->nr; i++) {
30123031
struct ref_update *update = transaction->updates[i];
3013-
struct ref_lock *lock = update->backend_data;
3014-
3015-
if (lock)
3016-
unlock_ref(lock);
30173032

30183033
if (update->flags & REF_DELETED_LOOSE) {
30193034
/*

0 commit comments

Comments
 (0)