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 <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed May 22, 2017 at 16:17 UTC c0ca9357640ae5efbdbfed4c5b476c820a839e85
1 file changed +24 -9
refs/files-backend.c
+24 -9
@@ -2834,6 +2834,27 @@ static int lock_ref_for_update(struct files_ref_store *refs,
2834 return 0;
2835 }
2836
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 +
2858 static int files_transaction_commit(struct ref_store *ref_store,
2859 struct ref_transaction *transaction,
2860 struct strbuf *err)
@@ -2856,10 +2877,8 @@ static int files_transaction_commit(struct ref_store *ref_store,
2877 if (transaction->state != REF_TRANSACTION_OPEN)
2878 die("BUG: commit called for transaction that is not open");
2879
2859 - if (!transaction->nr) {
2860 - transaction->state = REF_TRANSACTION_CLOSED;
2861 - return 0;
2862 - }
2880 + if (!transaction->nr)
2881 + goto cleanup;
2882
2883 /*
2884 * Fail if a refname appears more than once in the
@@ -3005,15 +3024,11 @@ static int files_transaction_commit(struct ref_store *ref_store,
3024 clear_loose_ref_cache(refs);
3025
3026 cleanup:
3027 + files_transaction_cleanup(transaction);
3028 strbuf_release(&sb);
3009 - transaction->state = REF_TRANSACTION_CLOSED;
3029
3030 for (i = 0; i < transaction->nr; i++) {
3031 struct ref_update *update = transaction->updates[i];
3013 - struct ref_lock *lock = update->backend_data;
3014 -
3015 - if (lock)
3016 - unlock_ref(lock);
3032
3033 if (update->flags & REF_DELETED_LOOSE) {
3034 /*