git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 5/7] refs: add interface to iterate over queued transactional updates

From
Patrick Steinhardt <ps@pks.im>
Date
Feb 21, 2022, 08:02 UTC
Message-ID
<0316d770e9590641a942370c84e4d8a42df2ffcd.1645430423.git.ps@pks.im>
In-Reply-To
<cover.1645430423.git.ps@pks.im>

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>
---
 refs.c | 16 ++++++++++++++++
 refs.h | 14 ++++++++++++++
 2 files changed, 30 insertions(+)
diff --git a/refs.c b/refs.c
index d680de3bc0..35d4e69687 100644
--- a/refs.c
+++ b/refs.c
@@ -2417,6 +2417,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 ff859d5951..1ae12c410a 100644
--- a/refs.h
+++ b/refs.h
@@ -776,6 +776,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.
  */
-- 
2.35.1
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 33 of 35 in “fetch: improve atomicity of `--atomic` flag”
  1. 0/7 fetch: improve atomicity of `--atomic` flagPatrick Steinhardt, Feb 17, 2022
  2. 1/7 fetch: increase test coverage of fetchesPatrick Steinhardt, Feb 17, 2022
  3. Christian CouderFeb 17, 2022
  4. Patrick SteinhardtFeb 21, 2022
  5. Junio C HamanoFeb 17, 2022
  6. Junio C HamanoFeb 17, 2022
  7. Patrick SteinhardtFeb 18, 2022
  8. Junio C HamanoFeb 18, 2022
  9. Junio C HamanoMar 3, 2022
  10. Patrick SteinhardtMar 3, 2022
  11. 2/7 fetch: backfill tags before setting upstreamPatrick Steinhardt, Feb 17, 2022
  12. Junio C HamanoFeb 17, 2022
  13. 3/7 fetch: control lifecycle of FETCH_HEAD in a single placePatrick Steinhardt, Feb 17, 2022
  14. Junio C HamanoFeb 17, 2022
  15. 4/7 fetch: report errors when backfilling tags failsPatrick Steinhardt, Feb 17, 2022
  16. Junio C HamanoFeb 17, 2022
  17. 6/7 fetch: make `--atomic` flag cover backfilling of tagsPatrick Steinhardt, Feb 17, 2022
  18. Junio C HamanoFeb 17, 2022
  19. 5/7 refs: add interface to iterate over queued transactional updatesPatrick Steinhardt, Feb 17, 2022
  20. 7/7 fetch: make `--atomic` flag cover pruning of refsPatrick Steinhardt, Feb 17, 2022
  21. Christian CouderFeb 17, 2022
  22. Junio C HamanoFeb 17, 2022
  23. 0/7 fetch: improve atomicity of `--atomic` flagPatrick Steinhardt, Feb 21, 2022
  24. 1/7 fetch: increase test coverage of fetchesPatrick Steinhardt, Feb 21, 2022
  25. Junio C HamanoMar 3, 2022
  26. Junio C HamanoMar 3, 2022
  27. Patrick SteinhardtMar 3, 2022
  28. Junio C HamanoMar 3, 2022
  29. Patrick SteinhardtMar 3, 2022
  30. 2/7 fetch: backfill tags before setting upstreamPatrick Steinhardt, Feb 21, 2022
  31. 3/7 fetch: control lifecycle of FETCH_HEAD in a single placePatrick Steinhardt, Feb 21, 2022
  32. 4/7 fetch: report errors when backfilling tags failsPatrick Steinhardt, Feb 21, 2022
  33. 5/7 refs: add interface to iterate over queued transactional updatesPatrick Steinhardt, Feb 21, 2022
  34. 6/7 fetch: make `--atomic` flag cover backfilling of tagsPatrick Steinhardt, Feb 21, 2022
  35. 7/7 fetch: make `--atomic` flag cover pruning of refsPatrick Steinhardt, Feb 21, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.