]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refs: add interface to iterate over queued transactional updates
authorPatrick Steinhardt <ps@pks.im>
Thu, 17 Feb 2022 13:04:32 +0000 (14:04 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 17 Feb 2022 19:19:44 +0000 (11:19 -0800)
There is no way for a caller to see whether a reference update has
already been queued up for a given reference transaction. There are
multiple alternatives to provide this functionality:

    - We may add a function that simply tells us whether a specific
      reference has already been queued. If implemented naively then
      this would potentially be quadratic in runtime behaviour if this
      question is asked repeatedly because we have to iterate over all
      references every time. The alternative would be to add a hashmap
      of all queued reference updates to speed up the lookup, but this
      adds overhead to all callers.

    - We may add a flag to `ref_transaction_add_update()` that causes it
      to skip duplicates, but this has the same runtime concerns as the
      first alternative.

    - We may add an interface which lets callers collect all updates
      which have already been queued such that he can avoid re-adding
      them. This is the most flexible approach and puts the burden on
      the caller, but also allows us to not impact any of the existing
      callsites which don't need this information.

This commit implements the last approach: it allows us to compute the
map of already-queued updates once up front such that we can then skip
all subsequent references which are already part of this map.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refs.c
refs.h

diff --git a/refs.c b/refs.c
index 7017ae598041bb1eda4ef5f95a6880be7c54ff71..7dc6c35ebc8dc59fd6f7f0fb4ed23f12ca46d207 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -2412,6 +2412,22 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,
        return refs->be->initial_transaction_commit(refs, transaction, err);
 }
 
+void ref_transaction_for_each_queued_update(struct ref_transaction *transaction,
+                                           ref_transaction_for_each_queued_update_fn cb,
+                                           void *cb_data)
+{
+       int i;
+
+       for (i = 0; i < transaction->nr; i++) {
+               struct ref_update *update = transaction->updates[i];
+
+               cb(update->refname,
+                  (update->flags & REF_HAVE_OLD) ? &update->old_oid : NULL,
+                  (update->flags & REF_HAVE_NEW) ? &update->new_oid : NULL,
+                  cb_data);
+       }
+}
+
 int refs_delete_refs(struct ref_store *refs, const char *logmsg,
                     struct string_list *refnames, unsigned int flags)
 {
diff --git a/refs.h b/refs.h
index cd2d0c1ac09001b904070d3938d317c157c4d4b6..991ed6268d1087e84ee03fd7517ae632c31500ec 100644 (file)
--- a/refs.h
+++ b/refs.h
@@ -770,6 +770,20 @@ int ref_transaction_abort(struct ref_transaction *transaction,
 int initial_ref_transaction_commit(struct ref_transaction *transaction,
                                   struct strbuf *err);
 
+/*
+ * Execute the given callback function for each of the reference updates which
+ * have been queued in the given transaction. `old_oid` and `new_oid` may be
+ * `NULL` pointers depending on whether the update has these object IDs set or
+ * not.
+ */
+typedef void ref_transaction_for_each_queued_update_fn(const char *refname,
+                                                      const struct object_id *old_oid,
+                                                      const struct object_id *new_oid,
+                                                      void *cb_data);
+void ref_transaction_for_each_queued_update(struct ref_transaction *transaction,
+                                           ref_transaction_for_each_queued_update_fn cb,
+                                           void *cb_data);
+
 /*
  * Free `*transaction` and all associated data.
  */