files_pack_refs(): use a reference transaction to write packed refs

Now that the packed reference store supports transactions, we can use a transaction to write the packed versions of references that we want to pack. This decreases the coupling between `files_ref_store` and `packed_ref_store`. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Sep 8, 2017 at 15:51 UTC 27d03d04d5b7e86a260d7afa283c9905f64de350
1 file changed +17 -7
refs/files-backend.c
+17 -7
@@ -1100,6 +1100,11 @@ static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
1100 int ok;
1101 struct ref_to_prune *refs_to_prune = NULL;
1102 struct strbuf err = STRBUF_INIT;
1103 + struct ref_transaction *transaction;
1104 +
1105 + transaction = ref_store_transaction_begin(refs->packed_ref_store, &err);
1106 + if (!transaction)
1107 + return -1;
1108
1109 packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err);
1110
@@ -1115,12 +1120,14 @@ static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
1120 continue;
1121
1122 /*
1118 - * Create an entry in the packed-refs cache equivalent
1119 - * to the one from the loose ref cache, except that
1120 - * we don't copy the peeled status, because we want it
1121 - * to be re-peeled.
1123 + * Add a reference creation for this reference to the
1124 + * packed-refs transaction:
1125 */
1123 - add_packed_ref(refs->packed_ref_store, iter->refname, iter->oid);
1126 + if (ref_transaction_update(transaction, iter->refname,
1127 + iter->oid->hash, NULL,
1128 + REF_NODEREF, NULL, &err))
1129 + die("failure preparing to create packed reference %s: %s",
1130 + iter->refname, err.buf);
1131
1132 /* Schedule the loose reference for pruning if requested. */
1133 if ((flags & PACK_REFS_PRUNE)) {
@@ -1134,8 +1141,11 @@ static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
1141 if (ok != ITER_DONE)
1142 die("error while iterating over references");
1143
1137 - if (commit_packed_refs(refs->packed_ref_store, &err))
1138 - die("unable to overwrite old ref-pack file: %s", err.buf);
1144 + if (ref_transaction_commit(transaction, &err))
1145 + die("unable to write new packed-refs: %s", err.buf);
1146 +
1147 + ref_transaction_free(transaction);
1148 +
1149 packed_refs_unlock(refs->packed_ref_store);
1150
1151 prune_refs(refs, refs_to_prune);