{"thread":{"id":"63170","subject":"[PATCH v4 1/8] refs/files: remove redundant check in split_symref_update()","startedAt":"2025-03-20T11:44:11Z","lastAt":"2025-03-25T12:31:20Z","messageCount":19,"participants":["Karthik Nayak","Patrick Steinhardt"],"isPatch":true,"patchVersion":4,"patchTotal":8},"messages":[{"id":"514759","messageId":"20250320-245-partially-atomic-ref-updates-v4-1-3dcc1b311dc9@gmail.com","threadId":"63170","inReplyTo":"20250320-245-partially-atomic-ref-updates-v4-0-3dcc1b311dc9@gmail.com","subject":"[PATCH v4 1/8] refs/files: remove redundant check in split_symref_update()","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2025-03-20T11:43:56Z","receivedAt":"2025-03-20T11:44:11Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"In `split_symref_update()`, there were two checks for duplicate\nrefnames:\n\n  - At the start, `string_list_has_string()` ensures the refname is not\n    already in `affected_refnames`, preventing duplicates from being\n    added.\n\n  - After adding the refname, another check verifies whether the newly\n    inserted item has a `util` value.\n\nThe second check is unnecessary because the first one guarantees that\n`string_list_insert()` will never encounter a preexisting entry.\n\nThe `item->util` field is assigned to validate that a rename doesn't\nalready exist in the list. The validation is done after the first check.\nAs this check is removed, clean up the validation and the assignment of\nthis field in `split_head_update()` and `files_transaction_prepare()`.\n\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n refs/files-backend.c | 20 +++-----------------\n 1 file changed, 3 insertions(+), 17 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex ff54a4bb7e..15559a09c5 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2382,7 +2382,6 @@ static int split_head_update(struct ref_update *update,\n \t\t\t     struct string_list *affected_refnames,\n \t\t\t     struct strbuf *err)\n {\n-\tstruct string_list_item *item;\n \tstruct ref_update *new_update;\n \n \tif ((update->flags & REF_LOG_ONLY) ||\n@@ -2421,8 +2420,7 @@ static int split_head_update(struct ref_update *update,\n \t */\n \tif (strcmp(new_update->refname, \"HEAD\"))\n \t\tBUG(\"%s unexpectedly not 'HEAD'\", new_update->refname);\n-\titem = string_list_insert(affected_refnames, new_update->refname);\n-\titem->util = new_update;\n+\tstring_list_insert(affected_refnames, new_update->refname);\n \n \treturn 0;\n }\n@@ -2441,7 +2439,6 @@ static int split_symref_update(struct ref_update *update,\n \t\t\t       struct string_list *affected_refnames,\n \t\t\t       struct strbuf *err)\n {\n-\tstruct string_list_item *item;\n \tstruct ref_update *new_update;\n \tunsigned int new_flags;\n \n@@ -2496,11 +2493,7 @@ static int split_symref_update(struct ref_update *update,\n \t * be valid as long as affected_refnames is in use, and NOT\n \t * referent, which might soon be freed by our caller.\n \t */\n-\titem = string_list_insert(affected_refnames, new_update->refname);\n-\tif (item->util)\n-\t\tBUG(\"%s unexpectedly found in affected_refnames\",\n-\t\t    new_update->refname);\n-\titem->util = new_update;\n+\tstring_list_insert(affected_refnames, new_update->refname);\n \n \treturn 0;\n }\n@@ -2834,7 +2827,6 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \t */\n \tfor (i = 0; i < transaction->nr; i++) {\n \t\tstruct ref_update *update = transaction->updates[i];\n-\t\tstruct string_list_item *item;\n \n \t\tif ((update->flags & REF_IS_PRUNING) &&\n \t\t    !(update->flags & REF_NO_DEREF))\n@@ -2843,13 +2835,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \t\tif (update->flags & REF_LOG_ONLY)\n \t\t\tcontinue;\n \n-\t\titem = string_list_append(&affected_refnames, update->refname);\n-\t\t/*\n-\t\t * We store a pointer to update in item->util, but at\n-\t\t * the moment we never use the value of this field\n-\t\t * except to check whether it is non-NULL.\n-\t\t */\n-\t\titem->util = update;\n+\t\tstring_list_append(&affected_refnames, update->refname);\n \t}\n \tstring_list_sort(&affected_refnames);\n \tif (ref_update_reject_duplicates(&affected_refnames, err)) {\n\n-- \n2.48.1\n\n"},{"id":"514760","messageId":"20250320-245-partially-atomic-ref-updates-v4-0-3dcc1b311dc9@gmail.com","threadId":"63170","inReplyTo":"20250207-245-partially-atomic-ref-updates-v1-0-e6a3690ff23a@gmail.com","subject":"[PATCH v4 0/8] refs: introduce support for batched reference updates","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2025-03-20T11:43:55Z","receivedAt":"2025-03-20T11:44:11Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Git supports making reference updates with or without transactions.\nUpdates with transactions are generally better optimized. But\ntransactions are all or nothing. This means, if a user wants to batch\nupdates to take advantage of the optimizations without the hard\nrequirement that all updates must succeed, there is no way currently to\ndo so. Particularly with the reftable backend where batching multiple\nreference updates is more efficient than performing them sequentially.\n\nThis series introduces support for batched reference updates without\ntransactions allowing individual reference updates to fail while letting\nothers proceed. This capability is exposed through git-update-ref's\n`--allow-partial` flag, which can be used in `--stdin` mode to batch\nupdates and handle failures gracefully. Under the hood, these batched\nupdates still use the transactions infrastructure, while modifying\nsections to allow partial failures.\n\nThe changes are structured to carefully build up this functionality:\n\nFirst, we clean up and consolidate the reference update checking logic.\nThis includes removing duplicate checks in the files backend and moving\nrefname tracking to the generic layer, which simplifies the codebase and\nprepares it for the new feature.\n\nWe then restructure the reftable backend's transaction preparation code,\nextracting the update validation logic into a dedicated function. This\nnot only improves code organization but sets the stage for implementing\npartial transaction support.\n\nTo ensure we only skip errors which are user-oriented, we introduce\ntyped errors for transactions with 'enum ref_transaction_error'. We\nextend the existing errors to include other scenarios and use this new\nerrors throughout the refs code.\n\nWith this groundwork in place, we implement the core batch update\nsupport in the refs subsystem. This adds the necessary infrastructure to\ntrack and report rejected updates while allowing transactions to\nproceed. All reference backends are modified to support this behavior\nwhen enabled.\n\nFinally, we expose this functionality to users through\ngit-update-ref(1)'s `--allow-partial` flag, complete with test coverage\nand documentation. The flag is specifically limited to `--stdin` mode\nwhere batching multiple updates is most relevant.\n\nThis enhancement improves Git's flexibility in handling reference\nupdates while maintaining the safety of atomic transactions by default.\nIt's particularly valuable for tools and workflows that need to handle\nreference update failures gracefully without abandoning the entire batch\nof updates.\n\nThis series is based on top of 683c54c999 (Git 2.49, 2025-03-14) with\nPatrick's series 'refs: batch refname availability checks' [1] merged\nin.\n\n[1]: https://lore.kernel.org/all/20250217-pks-update-ref-optimization-v1-0-a2b6d87a24af@pks.im/\n\n---\nChanges in v4:\n- Rebased on top of 2.49 since there was a long time between the\n  previous iteration and we have a new release.\n- Changed the naming to say 'batched' updates instead of 'partial\n  transactions'. While we still use the transaction infrastructure\n  underneath, the new naming causes less ambiguity.\n- Clean up some of the commit messages.\n- Raise BUG for invalid update index while setting rejections.\n- Fix an incorrect early return.\n- Link to v3: https://lore.kernel.org/r/20250305-245-partially-atomic-ref-updates-v3-0-0c64e3052354@gmail.com\n\nChanges in v3:\n- Changed 'transaction_error' to 'ref_transaction_error' along with the\n  error names. Removed 'TRANSACTION_OK' since it can potentially be\n  missed instead of simply 'return 0'.\n- Rename 'ref_transaction_set_rejected' to\n  'ref_transaction_maybe_set_rejected' and move logic around error\n  checks to within this function.\n- Add a new struct 'ref_transaction_rejections' to track the rejections\n  within a transaction. This allows us to only iterate over rejected\n  updates.\n- Add a new commit to also support partial transactions within the\n  batched F/D checks.\n- Remove NUL delimited outputs in 'git-update-ref(1)'.\n- Remove translations for plumbing outputs.\n- Other small cleanups in the commit message and code.\n\nChanges in v2:\n- Introduce and use structured errors. This consolidates the errors\n  and their handling between the ref backends.\n- In the previous version, we skipped over all failures. This include\n  system failures such as low memory or IO problems. Let's instead, only\n  skip user-oriented failures, such as invalid old OID and so on.\n- Change the rejection function name to `ref_transaction_set_rejected()`.\n- Modify the commit messages and documentation to be a little more\n  verbose.\n- Link to v1: https://lore.kernel.org/r/20250207-245-partially-atomic-ref-updates-v1-0-e6a3690ff23a@gmail.com\n\n Documentation/git-update-ref.adoc |  14 +-\n builtin/fetch.c                   |   2 +-\n builtin/update-ref.c              |  67 ++++-\n refs.c                            | 162 ++++++++++--\n refs.h                            |  76 ++++--\n refs/files-backend.c              | 314 +++++++++++-------------\n refs/packed-backend.c             |  69 +++---\n refs/refs-internal.h              |  51 +++-\n refs/reftable-backend.c           | 502 +++++++++++++++++++-------------------\n t/t1400-update-ref.sh             | 233 ++++++++++++++++++\n 10 files changed, 968 insertions(+), 522 deletions(-)\n\nKarthik Nayak (8):\n      refs/files: remove redundant check in split_symref_update()\n      refs: move duplicate refname update check to generic layer\n      refs/files: remove duplicate duplicates check\n      refs/reftable: extract code from the transaction preparation\n      refs: introduce enum-based transaction error types\n      refs: implement batch reference update support\n      refs: support rejection in batch updates during F/D checks\n      update-ref: add --batch-updates flag for stdin mode\n\nRange-diff versus v3:\n\n1:  c54471c108 ! 1:  0ddde0cb83 refs/files: remove redundant check in split_symref_update()\n    @@ Commit message\n         The second check is unnecessary because the first one guarantees that\n         `string_list_insert()` will never encounter a preexisting entry.\n     \n    -    Since `item->util` is only used in this context, remove the assignment and\n    -    simplify the surrounding code.\n    +    The `item->util` field is assigned to validate that a rename doesn't\n    +    already exist in the list. The validation is done after the first check.\n    +    As this check is removed, clean up the validation and the assignment of\n    +    this field in `split_head_update()` and `files_transaction_prepare()`.\n     \n         Signed-off-by: Karthik Nayak <karthik.188@gmail.com>\n     \n2:  438ed0b49a ! 2:  4eba9153ff refs: move duplicate refname update check to generic layer\n    @@ refs/reftable-backend.c: static int reftable_be_transaction_prepare(struct ref_s\n      \t\t}\n      \n     @@ refs/reftable-backend.c: static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n    + \t\t}\n      \t}\n      \n    - \tstring_list_sort(&refnames_to_check);\n     -\tret = refs_verify_refnames_available(ref_store, &refnames_to_check, &affected_refnames, NULL,\n     +\tret = refs_verify_refnames_available(ref_store, &refnames_to_check,\n     +\t\t\t\t\t     &transaction->refnames, NULL,\n3:  acdaa636d9 = 3:  a06733298d refs/files: remove duplicate duplicates check\n4:  fc61deb9c6 ! 4:  707afdb039 refs/reftable: extract code from the transaction preparation\n    @@ Commit message\n         The refactoring consolidates all reference update validation into a\n         single logical block, which improves code maintainability and\n         readability. More importantly, this restructuring lays the groundwork\n    -    for implementing partial transaction support in the reftable backend,\n    -    which will be introduced in the following commit.\n    +    for implementing batched reference update support in the reftable\n    +    backend, which will be introduced in a followup commit.\n     \n         No functional changes are included in this commit - it is purely a code\n         reorganization to support future enhancements.\n    @@ refs/reftable-backend.c: static int reftable_be_transaction_prepare(struct ref_s\n     -\t\t}\n      \t}\n      \n    - \tstring_list_sort(&refnames_to_check);\n    + \tret = refs_verify_refnames_available(ref_store, &refnames_to_check,\n5:  c494ff7e99 ! 5:  b93c3f3f9f refs: introduce enum-based transaction error types\n    @@ Commit message\n             common scenarios.\n     \n         This refactoring paves the way for more comprehensive error handling\n    -    which we will utilize in the upcoming commits to add partial transaction\n    -    support.\n    +    which we will utilize in the upcoming commits to add batch reference\n    +    update support.\n     \n         Signed-off-by: Karthik Nayak <karthik.188@gmail.com>\n     \n    @@ refs.c: int ref_transaction_commit(struct ref_transaction *transaction,\n      {\n      \tstruct strbuf dirname = STRBUF_INIT;\n      \tstruct strbuf referent = STRBUF_INIT;\n    + \tstruct string_list_item *item;\n      \tstruct ref_iterator *iter = NULL;\n      \tstruct strset dirnames;\n     -\tint ret = -1;\n    @@ refs/packed-backend.c: static int write_with_updates(struct packed_ref_store *re\n      \t\t\t\t\t    \"reference is missing but expected %s\",\n      \t\t\t\t\t    update->refname,\n      \t\t\t\t\t    oid_to_hex(&update->old_oid));\n    -+\t\t\t\treturn REF_TRANSACTION_ERROR_NONEXISTENT_REF;\n    ++\t\t\t\tret = REF_TRANSACTION_ERROR_NONEXISTENT_REF;\n      \t\t\t\tgoto error;\n      \t\t\t}\n      \t\t}\n6:  b051e51162 ! 6:  feacd50666 refs: implement partial reference transaction support\n    @@ Metadata\n     Author: Karthik Nayak <karthik.188@gmail.com>\n     \n      ## Commit message ##\n    -    refs: implement partial reference transaction support\n    +    refs: implement batch reference update support\n     \n    -    Git's reference transactions are all-or-nothing: either all updates\n    -    succeed, or none do. While this atomic behavior is generally desirable,\n    -    it can be suboptimal especially when using the reftable backend, where\n    -    batching multiple reference updates into a single transaction is more\n    -    efficient than performing them sequentially.\n    +    Git supports making reference updates with or without transactions.\n    +    Updates with transactions are generally better optimized. But\n    +    transactions are all or nothing. This means, if a user wants to batch\n    +    updates to take advantage of the optimizations without the hard\n    +    requirement that all updates must succeed, there is no way currently to\n    +    do so. Particularly with the reftable backend where batching multiple\n    +    reference updates is more efficient than performing them sequentially.\n     \n    -    Introduce partial transaction support with a new flag,\n    -    'REF_TRANSACTION_ALLOW_PARTIAL'. When enabled, this flag allows\n    -    individual reference updates that would typically cause the entire\n    -    transaction to fail due to non-system-related errors to be marked as\n    -    rejected while permitting other updates to proceed. System errors\n    -    referred by 'REF_TRANSACTION_ERROR_GENERIC' continue to result in the\n    -    entire transaction failing. This approach enhances flexibility while\n    -    preserving transactional integrity where necessary.\n    +    Introduce batched update support with a new flag,\n    +    'REF_TRANSACTION_ALLOW_FAILURE'. Batched updates while different from\n    +    transactions, use the transaction infrastructure under the hood. When\n    +    enabled, this flag allows individual reference updates that would\n    +    typically cause the entire transaction to fail due to non-system-related\n    +    errors to be marked as rejected while permitting other updates to\n    +    proceed. System errors referred by 'REF_TRANSACTION_ERROR_GENERIC'\n    +    continue to result in the entire transaction failing. This approach\n    +    enhances flexibility while preserving transactional integrity where\n    +    necessary.\n     \n         The implementation introduces several key components:\n     \n    @@ Commit message\n           - Modify reference backends (files, packed, reftable) to handle\n             partial transactions by using `ref_transaction_set_rejected()`\n             instead of failing the entire transaction when\n    -        `REF_TRANSACTION_ALLOW_PARTIAL` is set.\n    +        `REF_TRANSACTION_ALLOW_FAILURE` is set.\n     \n           - Add `ref_transaction_for_each_rejected_update()` to let callers\n             examine which updates were rejected and why.\n     \n    -    This foundational change enables partial transaction support throughout\n    -    the reference subsystem. A following commit will expose this capability\n    -    to users by adding a `--allow-partial` flag to 'git-update-ref(1)',\n    +    This foundational change enables batched update support throughout the\n    +    reference subsystem. A following commit will expose this capability to\n    +    users by adding a `--batch-updates` flag to 'git-update-ref(1)',\n         providing both a user-facing feature and a testable implementation.\n     \n         Signed-off-by: Karthik Nayak <karthik.188@gmail.com>\n    @@ refs.c: struct ref_transaction *ref_store_transaction_begin(struct ref_store *re\n      \ttr->flags = flags;\n      \tstring_list_init_dup(&tr->refnames);\n     +\n    -+\tif (flags & REF_TRANSACTION_ALLOW_PARTIAL)\n    ++\tif (flags & REF_TRANSACTION_ALLOW_FAILURE)\n     +\t\tCALLOC_ARRAY(tr->rejections, 1);\n     +\n      \treturn tr;\n    @@ refs.c: void ref_transaction_free(struct ref_transaction *transaction)\n     +\tif (update_idx >= transaction->nr)\n     +\t\tBUG(\"trying to set rejection on invalid update index\");\n     +\n    -+\tif (!(transaction->flags & REF_TRANSACTION_ALLOW_PARTIAL))\n    ++\tif (!(transaction->flags & REF_TRANSACTION_ALLOW_FAILURE))\n     +\t\treturn 0;\n     +\n     +\tif (!transaction->rejections)\n    -+\t\tBUG(\"transaction not inititalized with partial support\");\n    ++\t\tBUG(\"transaction not inititalized with failure support\");\n     +\n     +\t/*\n     +\t * Don't accept generic errors, since these errors are not user\n    @@ refs.h: enum ref_transaction_flag {\n     +\t * is detected. This flag allows transactions to partially apply updates\n     +\t * while rejecting updates which do not match the expected state.\n     +\t */\n    -+\tREF_TRANSACTION_ALLOW_PARTIAL = (1 << 1),\n    ++\tREF_TRANSACTION_ALLOW_FAILURE = (1 << 1),\n      };\n      \n      /*\n    @@ refs/packed-backend.c: static enum ref_transaction_error write_with_updates(stru\n     @@ refs/packed-backend.c: static enum ref_transaction_error write_with_updates(struct packed_ref_store *re\n      \t\t\t\t\t    update->refname,\n      \t\t\t\t\t    oid_to_hex(&update->old_oid));\n    - \t\t\t\treturn REF_TRANSACTION_ERROR_NONEXISTENT_REF;\n    + \t\t\t\tret = REF_TRANSACTION_ERROR_NONEXISTENT_REF;\n     +\n     +\t\t\t\tif (ref_transaction_maybe_set_rejected(transaction, i, ret)) {\n     +\t\t\t\t\tstrbuf_setlen(err, 0);\n    @@ refs/refs-internal.h: struct ref_update {\n      \tuint64_t index;\n      \n     +\t/*\n    -+\t * Used in partial transactions to mark if a given update was rejected.\n    ++\t * Used in batched reference updates to mark if a given update\n    ++\t * was rejected.\n     +\t */\n     +\tenum ref_transaction_error rejection_err;\n     +\n    @@ refs/refs-internal.h: enum ref_transaction_state {\n      };\n      \n     +/*\n    -+ * Data structure to hold indices of updates which were rejected, when\n    -+ * partial transactions where enabled. While the updates themselves hold\n    -+ * the rejection error, this structure allows a transaction to iterate\n    -+ * only over the rejected updates.\n    ++ * Data structure to hold indices of updates which were rejected, for batched\n    ++ * reference updates. While the updates themselves hold the rejection error,\n    ++ * this structure allows a transaction to iterate only over the rejected\n    ++ * updates.\n     + */\n     +struct ref_transaction_rejections {\n     +\tsize_t *update_indices;\n    @@ refs/reftable-backend.c: static int reftable_be_transaction_prepare(struct ref_s\n     +\t\t}\n      \t}\n      \n    - \tstring_list_sort(&refnames_to_check);\n    + \tret = refs_verify_refnames_available(ref_store, &refnames_to_check,\n     @@ refs/reftable-backend.c: static int write_transaction_table(struct reftable_writer *writer, void *cb_data\n      \t\tstruct reftable_transaction_update *tx_update = &arg->updates[i];\n      \t\tstruct ref_update *u = tx_update->update;\n7:  47d7a64cb4 ! 7:  f20877267d refs: support partial update rejections during F/D checks\n    @@ Metadata\n     Author: Karthik Nayak <karthik.188@gmail.com>\n     \n      ## Commit message ##\n    -    refs: support partial update rejections during F/D checks\n    +    refs: support rejection in batch updates during F/D checks\n     \n         The `refs_verify_refnames_available()` is used to batch check refnames\n         for F/D conflicts. While this is the more performant alternative than\n         its individual version, it does not provide rejection capabilities on a\n    -    single update level. For partial transactions, this would mean a\n    -    rejection of the entire transaction whenever one reference has a F/D\n    -    conflict.\n    +    single update level. For batched updates, this would mean a rejection of\n    +    the entire transaction whenever one reference has a F/D conflict.\n     \n         Modify the function to call `ref_transaction_maybe_set_rejected()` to\n         check if a single update can be rejected. Since this function is only\n    @@ refs.c: enum ref_transaction_error refs_verify_refnames_available(struct ref_sto\n     @@ refs.c: enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs\n      \tstrset_init(&dirnames);\n      \n    - \tfor (size_t i = 0; i < refnames->nr; i++) {\n    -+\t\tconst size_t *update_idx = (size_t *)refnames->items[i].util;\n    - \t\tconst char *refname = refnames->items[i].string;\n    + \tfor_each_string_list_item(item, refnames) {\n    ++\t\tconst size_t *update_idx = (size_t *)item->util;\n    + \t\tconst char *refname = item->string;\n      \t\tconst char *extra_refname;\n      \t\tstruct object_id oid;\n     @@ refs.c: enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs\n    @@ refs/files-backend.c: static enum ref_transaction_error lock_raw_ref(struct file\n      \t\t * make sure there is no existing packed ref that conflicts\n      \t\t * with refname. This check is deferred so that we can batch it.\n      \t\t */\n    --\t\tstring_list_insert(refnames_to_check, refname);\n    -+\t\titem = string_list_insert(refnames_to_check, refname);\n    +-\t\tstring_list_append(refnames_to_check, refname);\n    ++\t\titem = string_list_append(refnames_to_check, refname);\n     +\t\titem->util = xmalloc(sizeof(update_idx));\n     +\t\tmemcpy(item->util, &update_idx, sizeof(update_idx));\n      \t}\n    @@ refs/refs-internal.h: enum ref_transaction_error ref_update_check_old_target(con\n     + * refnames instead of only a single item. This is more efficient in the case\n     + * where one needs to check multiple refnames.\n     + *\n    -+ * If a transaction is provided with partial support, then individual updates\n    -+ * are marked rejected, reference backends are then in charge of not committing\n    -+ * those updates.\n    ++ * If using batched updates, then individual updates are marked rejected,\n    ++ * reference backends are then in charge of not committing those updates.\n     + */\n     +enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs,\n     +\t\t\t\t\t  const struct string_list *refnames,\n    @@ refs/reftable-backend.c: static int reftable_be_transaction_prepare(struct ref_s\n      \t\t\t\t\t    &head_referent, &referent, err);\n      \t\tif (ret) {\n     @@ refs/reftable-backend.c: static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n    - \tstring_list_sort(&refnames_to_check);\n    + \n      \tret = refs_verify_refnames_available(ref_store, &refnames_to_check,\n      \t\t\t\t\t     &transaction->refnames, NULL,\n     +\t\t\t\t\t     transaction,\n8:  2237086c65 ! 8:  d6f43d5dce update-ref: add --allow-partial flag for stdin mode\n    @@ Metadata\n     Author: Karthik Nayak <karthik.188@gmail.com>\n     \n      ## Commit message ##\n    -    update-ref: add --allow-partial flag for stdin mode\n    +    update-ref: add --batch-updates flag for stdin mode\n     \n         When updating multiple references through stdin, Git's update-ref\n         command normally aborts the entire transaction if any single update\n    -    fails. While this atomic behavior prevents partial updates by default,\n    -    there are cases where applying successful updates while reporting\n    -    failures is desirable.\n    +    fails. This atomic behavior prevents partial updates. Introduce a new\n    +    batch update system, where the updates the performed together similar\n    +    but individual updates are allowed to fail.\n     \n    -    Add a new `--allow-partial` flag that allows the transaction to continue\n    +    Add a new `--batch-updates` flag that allows the transaction to continue\n         even when individual reference updates fail. This flag can only be used\n    -    in `--stdin` mode and builds upon the partial transaction support added\n    -    to the refs subsystem. When enabled, failed updates are reported in the\n    -    following format:\n    +    in `--stdin` mode and builds upon the batch update support added to the\n    +    refs subsystem in the previous commits. When enabled, failed updates are\n    +    reported in the following format:\n     \n           rejected SP (<old-oid> | <old-target>) SP (<new-oid> | <new-target>) SP <rejection-reason> LF\n     \n    @@ Documentation/git-update-ref.adoc: git-update-ref - Update the object name store\n     +[synopsis]\n     +git update-ref [-m <reason>] [--no-deref] -d <ref> [<old-oid>]\n     +\t       [-m <reason>] [--no-deref] [--create-reflog] <ref> <new-oid> [<old-oid>]\n    -+               [-m <reason>] [--no-deref] --stdin [-z] [--allow-partial]\n    ++               [-m <reason>] [--no-deref] --stdin [-z] [--batch-updates]\n      \n      DESCRIPTION\n      -----------\n    @@ Documentation/git-update-ref.adoc: performs all modifications together.  Specify\n      With `--create-reflog`, update-ref will create a reflog for each ref\n      even if one would not ordinarily be created.\n      \n    -+With `--allow-partial`, update-ref continues executing the transaction even if\n    -+some updates fail due to invalid or incorrect user input, applying only the\n    -+successful updates. Errors resulting from user-provided input are treated as\n    -+non-system-related and do not cause the entire transaction to be aborted.\n    -+However, system-related errors—such as I/O failures or memory issues—will still\n    -+result in a full failure. Additionally, errors like F/D conflicts are batched\n    -+for performance optimization and will also cause a full failure. Any failed\n    ++With `--batch-updates`, update-ref executes the updates in a batch but allows\n    ++individual updates to fail due to invalid or incorrect user input, applying only\n    ++the successful updates. However, system-related errors—such as I/O failures or\n    ++memory issues—will result in a full failure of all batched updates. Any failed\n     +updates will be reported in the following format:\n     +\n     +\trejected SP (<old-oid> | <old-target>) SP (<new-oid> | <new-target>) SP <rejection-reason> LF\n    @@ builtin/update-ref.c\n      \tN_(\"git update-ref [<options>] -d <refname> [<old-oid>]\"),\n      \tN_(\"git update-ref [<options>]    <refname> <new-oid> [<old-oid>]\"),\n     -\tN_(\"git update-ref [<options>] --stdin [-z]\"),\n    -+\tN_(\"git update-ref [<options>] --stdin [-z] [--allow-partial]\"),\n    ++\tN_(\"git update-ref [<options>] --stdin [-z] [--batch-updates]\"),\n      \tNULL\n      };\n      \n    @@ builtin/update-ref.c: int cmd_update_ref(int argc,\n      \t\tOPT_BOOL('z', NULL, &end_null, N_(\"stdin has NUL-terminated arguments\")),\n      \t\tOPT_BOOL( 0 , \"stdin\", &read_stdin, N_(\"read updates from stdin\")),\n      \t\tOPT_BOOL( 0 , \"create-reflog\", &create_reflog, N_(\"create a reflog\")),\n    -+\t\tOPT_BIT('0', \"allow-partial\", &flags, N_(\"allow partial transactions\"),\n    -+\t\t\tREF_TRANSACTION_ALLOW_PARTIAL),\n    ++\t\tOPT_BIT('0', \"batch-updates\", &flags, N_(\"batch reference updates\"),\n    ++\t\t\tREF_TRANSACTION_ALLOW_FAILURE),\n      \t\tOPT_END(),\n      \t};\n      \n    @@ builtin/update-ref.c: int cmd_update_ref(int argc,\n     +\t\tupdate_refs_stdin(flags);\n      \t\treturn 0;\n     -\t}\n    -+\t} else if (flags & REF_TRANSACTION_ALLOW_PARTIAL)\n    -+\t\tdie(\"--allow-partial can only be used with --stdin\");\n    ++\t} else if (flags & REF_TRANSACTION_ALLOW_FAILURE)\n    ++\t\tdie(\"--batch-updates can only be used with --stdin\");\n      \n      \tif (end_null)\n      \t\tusage_with_options(git_update_ref_usage, options);\n    @@ t/t1400-update-ref.sh: do\n      \t\tgrep \"$(git rev-parse $a) $(git rev-parse $a)\" actual\n      \t'\n      \n    -+\ttest_expect_success \"stdin $type allow-partial\" '\n    ++\ttest_expect_success \"stdin $type batch-updates\" '\n     +\t\tgit init repo &&\n     +\t\ttest_when_finished \"rm -fr repo\" &&\n     +\t\t(\n    @@ t/t1400-update-ref.sh: do\n     +\n     +\t\t\tformat_command $type \"update refs/heads/ref1\" \"$head\" \"$Z\" >stdin &&\n     +\t\t\tformat_command $type \"update refs/heads/ref2\" \"$head\" \"$Z\" >>stdin &&\n    -+\t\t\tgit update-ref $type --stdin --allow-partial <stdin &&\n    ++\t\t\tgit update-ref $type --stdin --batch-updates <stdin &&\n     +\t\t\techo $head >expect &&\n     +\t\t\tgit rev-parse refs/heads/ref1 >actual &&\n     +\t\t\ttest_cmp expect actual &&\n    @@ t/t1400-update-ref.sh: do\n     +\t\t)\n     +\t'\n     +\n    -+\ttest_expect_success \"stdin $type allow-partial with invalid new_oid\" '\n    ++\ttest_expect_success \"stdin $type batch-updates with invalid new_oid\" '\n     +\t\tgit init repo &&\n     +\t\ttest_when_finished \"rm -fr repo\" &&\n     +\t\t(\n    @@ t/t1400-update-ref.sh: do\n     +\n     +\t\t\tformat_command $type \"update refs/heads/ref1\" \"$old_head\" \"$head\" >stdin &&\n     +\t\t\tformat_command $type \"update refs/heads/ref2\" \"$(test_oid 001)\" \"$head\" >>stdin &&\n    -+\t\t\tgit update-ref $type --stdin --allow-partial <stdin >stdout &&\n    ++\t\t\tgit update-ref $type --stdin --batch-updates <stdin >stdout &&\n     +\t\t\techo $old_head >expect &&\n     +\t\t\tgit rev-parse refs/heads/ref1 >actual &&\n     +\t\t\ttest_cmp expect actual &&\n    @@ t/t1400-update-ref.sh: do\n     +\t\t)\n     +\t'\n     +\n    -+\ttest_expect_success \"stdin $type allow-partial with non-commit new_oid\" '\n    ++\ttest_expect_success \"stdin $type batch-updates with non-commit new_oid\" '\n     +\t\tgit init repo &&\n     +\t\ttest_when_finished \"rm -fr repo\" &&\n     +\t\t(\n    @@ t/t1400-update-ref.sh: do\n     +\n     +\t\t\tformat_command $type \"update refs/heads/ref1\" \"$old_head\" \"$head\" >stdin &&\n     +\t\t\tformat_command $type \"update refs/heads/ref2\" \"$head_tree\" \"$head\" >>stdin &&\n    -+\t\t\tgit update-ref $type --stdin --allow-partial <stdin >stdout &&\n    ++\t\t\tgit update-ref $type --stdin --batch-updates <stdin >stdout &&\n     +\t\t\techo $old_head >expect &&\n     +\t\t\tgit rev-parse refs/heads/ref1 >actual &&\n     +\t\t\ttest_cmp expect actual &&\n    @@ t/t1400-update-ref.sh: do\n     +\t\t)\n     +\t'\n     +\n    -+\ttest_expect_success \"stdin $type allow-partial with non-existent ref\" '\n    ++\ttest_expect_success \"stdin $type batch-updates with non-existent ref\" '\n     +\t\tgit init repo &&\n     +\t\ttest_when_finished \"rm -fr repo\" &&\n     +\t\t(\n    @@ t/t1400-update-ref.sh: do\n     +\n     +\t\t\tformat_command $type \"update refs/heads/ref1\" \"$old_head\" \"$head\" >stdin &&\n     +\t\t\tformat_command $type \"update refs/heads/ref2\" \"$old_head\" \"$head\" >>stdin &&\n    -+\t\t\tgit update-ref $type --stdin --allow-partial <stdin >stdout &&\n    ++\t\t\tgit update-ref $type --stdin --batch-updates <stdin >stdout &&\n     +\t\t\techo $old_head >expect &&\n     +\t\t\tgit rev-parse refs/heads/ref1 >actual &&\n     +\t\t\ttest_cmp expect actual &&\n    @@ t/t1400-update-ref.sh: do\n     +\t\t)\n     +\t'\n     +\n    -+\ttest_expect_success \"stdin $type allow-partial with dangling symref\" '\n    ++\ttest_expect_success \"stdin $type batch-updates with dangling symref\" '\n     +\t\tgit init repo &&\n     +\t\ttest_when_finished \"rm -fr repo\" &&\n     +\t\t(\n    @@ t/t1400-update-ref.sh: do\n     +\n     +\t\t\tformat_command $type \"update refs/heads/ref1\" \"$old_head\" \"$head\" >stdin &&\n     +\t\t\tformat_command $type \"update refs/heads/ref2\" \"$old_head\" \"$head\" >>stdin &&\n    -+\t\t\tgit update-ref $type --no-deref --stdin --allow-partial <stdin >stdout &&\n    ++\t\t\tgit update-ref $type --no-deref --stdin --batch-updates <stdin >stdout &&\n     +\t\t\techo $old_head >expect &&\n     +\t\t\tgit rev-parse refs/heads/ref1 >actual &&\n     +\t\t\ttest_cmp expect actual &&\n    @@ t/t1400-update-ref.sh: do\n     +\t\t)\n     +\t'\n     +\n    -+\ttest_expect_success \"stdin $type allow-partial with regular ref as symref\" '\n    ++\ttest_expect_success \"stdin $type batch-updates with regular ref as symref\" '\n     +\t\tgit init repo &&\n     +\t\ttest_when_finished \"rm -fr repo\" &&\n     +\t\t(\n    @@ t/t1400-update-ref.sh: do\n     +\n     +\t\t\tformat_command $type \"update refs/heads/ref1\" \"$old_head\" \"$head\" >stdin &&\n     +\t\t\tformat_command $type \"symref-update refs/heads/ref2\" \"$old_head\" \"ref\" \"refs/heads/nonexistent\" >>stdin &&\n    -+\t\t\tgit update-ref $type --no-deref --stdin --allow-partial <stdin >stdout &&\n    ++\t\t\tgit update-ref $type --no-deref --stdin --batch-updates <stdin >stdout &&\n     +\t\t\techo $old_head >expect &&\n     +\t\t\tgit rev-parse refs/heads/ref1 >actual &&\n     +\t\t\ttest_cmp expect actual &&\n    @@ t/t1400-update-ref.sh: do\n     +\t\t)\n     +\t'\n     +\n    -+\ttest_expect_success \"stdin $type allow-partial with invalid old_oid\" '\n    ++\ttest_expect_success \"stdin $type batch-updates with invalid old_oid\" '\n     +\t\tgit init repo &&\n     +\t\ttest_when_finished \"rm -fr repo\" &&\n     +\t\t(\n    @@ t/t1400-update-ref.sh: do\n     +\n     +\t\t\tformat_command $type \"update refs/heads/ref1\" \"$old_head\" \"$head\" >stdin &&\n     +\t\t\tformat_command $type \"update refs/heads/ref2\" \"$old_head\" \"$Z\" >>stdin &&\n    -+\t\t\tgit update-ref $type --stdin --allow-partial <stdin >stdout &&\n    ++\t\t\tgit update-ref $type --stdin --batch-updates <stdin >stdout &&\n     +\t\t\techo $old_head >expect &&\n     +\t\t\tgit rev-parse refs/heads/ref1 >actual &&\n     +\t\t\ttest_cmp expect actual &&\n    @@ t/t1400-update-ref.sh: do\n     +\t\t)\n     +\t'\n     +\n    -+\ttest_expect_success \"stdin $type allow-partial with incorrect old oid\" '\n    ++\ttest_expect_success \"stdin $type batch-updates with incorrect old oid\" '\n     +\t\tgit init repo &&\n     +\t\ttest_when_finished \"rm -fr repo\" &&\n     +\t\t(\n    @@ t/t1400-update-ref.sh: do\n     +\n     +\t\t\tformat_command $type \"update refs/heads/ref1\" \"$old_head\" \"$head\" >stdin &&\n     +\t\t\tformat_command $type \"update refs/heads/ref2\" \"$head\" \"$old_head\" >>stdin &&\n    -+\t\t\tgit update-ref $type --stdin --allow-partial <stdin >stdout &&\n    ++\t\t\tgit update-ref $type --stdin --batch-updates <stdin >stdout &&\n     +\t\t\techo $old_head >expect &&\n     +\t\t\tgit rev-parse refs/heads/ref1 >actual &&\n     +\t\t\ttest_cmp expect actual &&\n    @@ t/t1400-update-ref.sh: do\n     +\t\t)\n     +\t'\n     +\n    -+\ttest_expect_success \"stdin $type allow-partial refname conflict\" '\n    ++\ttest_expect_success \"stdin $type batch-updates refname conflict\" '\n     +\t\tgit init repo &&\n     +\t\ttest_when_finished \"rm -fr repo\" &&\n     +\t\t(\n    @@ t/t1400-update-ref.sh: do\n     +\n     +\t\t\tformat_command $type \"update refs/heads/ref/foo\" \"$old_head\" \"$head\" >stdin &&\n     +\t\t\tformat_command $type \"update refs/heads/ref\" \"$old_head\" \"\" >>stdin &&\n    -+\t\t\tgit update-ref $type --stdin --allow-partial <stdin >stdout &&\n    ++\t\t\tgit update-ref $type --stdin --batch-updates <stdin >stdout &&\n     +\t\t\techo $old_head >expect &&\n     +\t\t\tgit rev-parse refs/heads/ref/foo >actual &&\n     +\t\t\ttest_cmp expect actual &&\n    @@ t/t1400-update-ref.sh: do\n     +\t\t)\n     +\t'\n     +\n    -+\ttest_expect_success \"stdin $type allow-partial refname conflict new ref\" '\n    ++\ttest_expect_success \"stdin $type batch-updates refname conflict new ref\" '\n     +\t\tgit init repo &&\n     +\t\ttest_when_finished \"rm -fr repo\" &&\n     +\t\t(\n    @@ t/t1400-update-ref.sh: do\n     +\n     +\t\t\tformat_command $type \"update refs/heads/foo\" \"$old_head\" \"\" >stdin &&\n     +\t\t\tformat_command $type \"update refs/heads/ref\" \"$old_head\" \"\" >>stdin &&\n    -+\t\t\tgit update-ref $type --stdin --allow-partial <stdin >stdout &&\n    ++\t\t\tgit update-ref $type --stdin --batch-updates <stdin >stdout &&\n     +\t\t\techo $old_head >expect &&\n     +\t\t\tgit rev-parse refs/heads/foo >actual &&\n     +\t\t\ttest_cmp expect actual &&\n\n\nbase-commit: 679c868f5fffadd1f7e8e49d4d87d745ee36ffb7\nchange-id: 20241206-245-partially-atomic-ref-updates-9fe8b080345c\n\nThanks\n- Karthik\n\n"},{"id":"514762","messageId":"20250320-245-partially-atomic-ref-updates-v4-2-3dcc1b311dc9@gmail.com","threadId":"63170","inReplyTo":"20250320-245-partially-atomic-ref-updates-v4-0-3dcc1b311dc9@gmail.com","subject":"[PATCH v4 2/8] refs: move duplicate refname update check to generic layer","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2025-03-20T11:43:57Z","receivedAt":"2025-03-20T11:44:12Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Move the tracking of refnames in `affected_refnames` from individual\nbackends into the generic layer in 'refs.c'. This centralizes the\nduplicate refname detection that was previously handled separately by\neach backend.\n\nMake some changes to accommodate this move:\n\n  - Add a `string_list` field `refnames` to `ref_transaction` to contain\n    all the references in a transaction. This field is updated whenever\n    a new update is added via `ref_transaction_add_update`, so manual\n    additions in reference backends are dropped.\n\n  - Modify the backends to use this field internally as needed. The\n    backends need to check if an update for refname already exists when\n    splitting symrefs or adding an update for 'HEAD'.\n\n  - In the reftable backend, within `reftable_be_transaction_prepare()`,\n    move the `string_list_has_string()` check above\n    `ref_transaction_add_update()`. Since `ref_transaction_add_update()`\n    automatically adds the refname to `transaction->refnames`,\n    performing the check after will always return true, so we perform\n    the check before adding the update.\n\nThis helps reduce duplication of functionality between the backends and\nmakes it easier to make changes in a more centralized manner.\n\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n refs.c                  | 17 +++++++++++++\n refs/files-backend.c    | 67 +++++++++++--------------------------------------\n refs/packed-backend.c   | 25 +-----------------\n refs/refs-internal.h    |  2 ++\n refs/reftable-backend.c | 54 +++++++++++++--------------------------\n 5 files changed, 51 insertions(+), 114 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 2ac9d8ebd0..504bf2063e 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1175,6 +1175,7 @@ struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,\n \tCALLOC_ARRAY(tr, 1);\n \ttr->ref_store = refs;\n \ttr->flags = flags;\n+\tstring_list_init_dup(&tr->refnames);\n \treturn tr;\n }\n \n@@ -1205,6 +1206,7 @@ void ref_transaction_free(struct ref_transaction *transaction)\n \t\tfree((char *)transaction->updates[i]->old_target);\n \t\tfree(transaction->updates[i]);\n \t}\n+\tstring_list_clear(&transaction->refnames, 0);\n \tfree(transaction->updates);\n \tfree(transaction);\n }\n@@ -1218,6 +1220,7 @@ struct ref_update *ref_transaction_add_update(\n \t\tconst char *committer_info,\n \t\tconst char *msg)\n {\n+\tstruct string_list_item *item;\n \tstruct ref_update *update;\n \n \tif (transaction->state != REF_TRANSACTION_OPEN)\n@@ -1245,6 +1248,16 @@ struct ref_update *ref_transaction_add_update(\n \t\tupdate->msg = normalize_reflog_message(msg);\n \t}\n \n+\t/*\n+\t * This list is generally used by the backends to avoid duplicates.\n+\t * But we do support multiple log updates for a given refname within\n+\t * a single transaction.\n+\t */\n+\tif (!(update->flags & REF_LOG_ONLY)) {\n+\t\titem = string_list_append(&transaction->refnames, refname);\n+\t\titem->util = update;\n+\t}\n+\n \treturn update;\n }\n \n@@ -2405,6 +2418,10 @@ int ref_transaction_prepare(struct ref_transaction *transaction,\n \t\treturn -1;\n \t}\n \n+\tstring_list_sort(&transaction->refnames);\n+\tif (ref_update_reject_duplicates(&transaction->refnames, err))\n+\t\treturn TRANSACTION_GENERIC_ERROR;\n+\n \tret = refs->be->transaction_prepare(refs, transaction, err);\n \tif (ret)\n \t\treturn ret;\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 15559a09c5..58f62ea8a3 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2378,9 +2378,7 @@ static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_st\n  */\n static int split_head_update(struct ref_update *update,\n \t\t\t     struct ref_transaction *transaction,\n-\t\t\t     const char *head_ref,\n-\t\t\t     struct string_list *affected_refnames,\n-\t\t\t     struct strbuf *err)\n+\t\t\t     const char *head_ref, struct strbuf *err)\n {\n \tstruct ref_update *new_update;\n \n@@ -2398,7 +2396,7 @@ static int split_head_update(struct ref_update *update,\n \t * transaction. This check is O(lg N) in the transaction\n \t * size, but it happens at most once per transaction.\n \t */\n-\tif (string_list_has_string(affected_refnames, \"HEAD\")) {\n+\tif (string_list_has_string(&transaction->refnames, \"HEAD\")) {\n \t\t/* An entry already existed */\n \t\tstrbuf_addf(err,\n \t\t\t    \"multiple updates for 'HEAD' (including one \"\n@@ -2420,7 +2418,6 @@ static int split_head_update(struct ref_update *update,\n \t */\n \tif (strcmp(new_update->refname, \"HEAD\"))\n \t\tBUG(\"%s unexpectedly not 'HEAD'\", new_update->refname);\n-\tstring_list_insert(affected_refnames, new_update->refname);\n \n \treturn 0;\n }\n@@ -2436,7 +2433,6 @@ static int split_head_update(struct ref_update *update,\n static int split_symref_update(struct ref_update *update,\n \t\t\t       const char *referent,\n \t\t\t       struct ref_transaction *transaction,\n-\t\t\t       struct string_list *affected_refnames,\n \t\t\t       struct strbuf *err)\n {\n \tstruct ref_update *new_update;\n@@ -2448,7 +2444,7 @@ static int split_symref_update(struct ref_update *update,\n \t * size, but it happens at most once per symref in a\n \t * transaction.\n \t */\n-\tif (string_list_has_string(affected_refnames, referent)) {\n+\tif (string_list_has_string(&transaction->refnames, referent)) {\n \t\t/* An entry already exists */\n \t\tstrbuf_addf(err,\n \t\t\t    \"multiple updates for '%s' (including one \"\n@@ -2486,15 +2482,6 @@ static int split_symref_update(struct ref_update *update,\n \tupdate->flags |= REF_LOG_ONLY | REF_NO_DEREF;\n \tupdate->flags &= ~REF_HAVE_OLD;\n \n-\t/*\n-\t * Add the referent. This insertion is O(N) in the transaction\n-\t * size, but it happens at most once per symref in a\n-\t * transaction. Make sure to add new_update->refname, which will\n-\t * be valid as long as affected_refnames is in use, and NOT\n-\t * referent, which might soon be freed by our caller.\n-\t */\n-\tstring_list_insert(affected_refnames, new_update->refname);\n-\n \treturn 0;\n }\n \n@@ -2558,7 +2545,6 @@ static int lock_ref_for_update(struct files_ref_store *refs,\n \t\t\t       struct ref_transaction *transaction,\n \t\t\t       const char *head_ref,\n \t\t\t       struct string_list *refnames_to_check,\n-\t\t\t       struct string_list *affected_refnames,\n \t\t\t       struct strbuf *err)\n {\n \tstruct strbuf referent = STRBUF_INIT;\n@@ -2575,8 +2561,7 @@ static int lock_ref_for_update(struct files_ref_store *refs,\n \t\tupdate->flags |= REF_DELETING;\n \n \tif (head_ref) {\n-\t\tret = split_head_update(update, transaction, head_ref,\n-\t\t\t\t\taffected_refnames, err);\n+\t\tret = split_head_update(update, transaction, head_ref, err);\n \t\tif (ret)\n \t\t\tgoto out;\n \t}\n@@ -2586,9 +2571,8 @@ static int lock_ref_for_update(struct files_ref_store *refs,\n \t\tlock->count++;\n \t} else {\n \t\tret = lock_raw_ref(refs, update->refname, mustexist,\n-\t\t\t\t   refnames_to_check, affected_refnames,\n-\t\t\t\t   &lock, &referent,\n-\t\t\t\t   &update->type, err);\n+\t\t\t\t   refnames_to_check, &transaction->refnames,\n+\t\t\t\t   &lock, &referent, &update->type, err);\n \t\tif (ret) {\n \t\t\tchar *reason;\n \n@@ -2642,9 +2626,8 @@ static int lock_ref_for_update(struct files_ref_store *refs,\n \t\t\t * of processing the split-off update, so we\n \t\t\t * don't have to do it here.\n \t\t\t */\n-\t\t\tret = split_symref_update(update,\n-\t\t\t\t\t\t  referent.buf, transaction,\n-\t\t\t\t\t\t  affected_refnames, err);\n+\t\t\tret = split_symref_update(update, referent.buf,\n+\t\t\t\t\t\t  transaction, err);\n \t\t\tif (ret)\n \t\t\t\tgoto out;\n \t\t}\n@@ -2799,7 +2782,6 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \t\t\t       \"ref_transaction_prepare\");\n \tsize_t i;\n \tint ret = 0;\n-\tstruct string_list affected_refnames = STRING_LIST_INIT_NODUP;\n \tstruct string_list refnames_to_check = STRING_LIST_INIT_NODUP;\n \tchar *head_ref = NULL;\n \tint head_type;\n@@ -2818,12 +2800,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \ttransaction->backend_data = backend_data;\n \n \t/*\n-\t * Fail if a refname appears more than once in the\n-\t * transaction. (If we end up splitting up any updates using\n-\t * split_symref_update() or split_head_update(), those\n-\t * functions will check that the new updates don't have the\n-\t * same refname as any existing ones.) Also fail if any of the\n-\t * updates use REF_IS_PRUNING without REF_NO_DEREF.\n+\t * Fail if any of the updates use REF_IS_PRUNING without REF_NO_DEREF.\n \t */\n \tfor (i = 0; i < transaction->nr; i++) {\n \t\tstruct ref_update *update = transaction->updates[i];\n@@ -2831,16 +2808,6 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \t\tif ((update->flags & REF_IS_PRUNING) &&\n \t\t    !(update->flags & REF_NO_DEREF))\n \t\t\tBUG(\"REF_IS_PRUNING set without REF_NO_DEREF\");\n-\n-\t\tif (update->flags & REF_LOG_ONLY)\n-\t\t\tcontinue;\n-\n-\t\tstring_list_append(&affected_refnames, update->refname);\n-\t}\n-\tstring_list_sort(&affected_refnames);\n-\tif (ref_update_reject_duplicates(&affected_refnames, err)) {\n-\t\tret = TRANSACTION_GENERIC_ERROR;\n-\t\tgoto cleanup;\n \t}\n \n \t/*\n@@ -2882,7 +2849,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \n \t\tret = lock_ref_for_update(refs, update, transaction,\n \t\t\t\t\t  head_ref, &refnames_to_check,\n-\t\t\t\t\t  &affected_refnames, err);\n+\t\t\t\t\t  err);\n \t\tif (ret)\n \t\t\tgoto cleanup;\n \n@@ -2929,7 +2896,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \t * So instead, we accept the race for now.\n \t */\n \tif (refs_verify_refnames_available(refs->packed_ref_store, &refnames_to_check,\n-\t\t\t\t\t   &affected_refnames, NULL, 0, err)) {\n+\t\t\t\t\t   &transaction->refnames, NULL, 0, err)) {\n \t\tret = TRANSACTION_NAME_CONFLICT;\n \t\tgoto cleanup;\n \t}\n@@ -2975,7 +2942,6 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \n cleanup:\n \tfree(head_ref);\n-\tstring_list_clear(&affected_refnames, 0);\n \tstring_list_clear(&refnames_to_check, 0);\n \n \tif (ret)\n@@ -3050,13 +3016,8 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,\n \tif (transaction->state != REF_TRANSACTION_PREPARED)\n \t\tBUG(\"commit called for transaction that is not prepared\");\n \n-\t/* Fail if a refname appears more than once in the transaction: */\n-\tfor (i = 0; i < transaction->nr; i++)\n-\t\tif (!(transaction->updates[i]->flags & REF_LOG_ONLY))\n-\t\t\tstring_list_append(&affected_refnames,\n-\t\t\t\t\t   transaction->updates[i]->refname);\n-\tstring_list_sort(&affected_refnames);\n-\tif (ref_update_reject_duplicates(&affected_refnames, err)) {\n+\tstring_list_sort(&transaction->refnames);\n+\tif (ref_update_reject_duplicates(&transaction->refnames, err)) {\n \t\tret = TRANSACTION_GENERIC_ERROR;\n \t\tgoto cleanup;\n \t}\n@@ -3074,7 +3035,7 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,\n \t * that we are creating already exists.\n \t */\n \tif (refs_for_each_rawref(&refs->base, ref_present,\n-\t\t\t\t &affected_refnames))\n+\t\t\t\t &transaction->refnames))\n \t\tBUG(\"initial ref transaction called with existing refs\");\n \n \tpacked_transaction = ref_store_transaction_begin(refs->packed_ref_store,\ndiff --git a/refs/packed-backend.c b/refs/packed-backend.c\nindex f4c82ba2c7..19220d2e99 100644\n--- a/refs/packed-backend.c\n+++ b/refs/packed-backend.c\n@@ -1622,8 +1622,6 @@ int is_packed_transaction_needed(struct ref_store *ref_store,\n struct packed_transaction_backend_data {\n \t/* True iff the transaction owns the packed-refs lock. */\n \tint own_lock;\n-\n-\tstruct string_list updates;\n };\n \n static void packed_transaction_cleanup(struct packed_ref_store *refs,\n@@ -1632,8 +1630,6 @@ static void packed_transaction_cleanup(struct packed_ref_store *refs,\n \tstruct packed_transaction_backend_data *data = transaction->backend_data;\n \n \tif (data) {\n-\t\tstring_list_clear(&data->updates, 0);\n-\n \t\tif (is_tempfile_active(refs->tempfile))\n \t\t\tdelete_tempfile(&refs->tempfile);\n \n@@ -1658,7 +1654,6 @@ static int packed_transaction_prepare(struct ref_store *ref_store,\n \t\t\tREF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,\n \t\t\t\"ref_transaction_prepare\");\n \tstruct packed_transaction_backend_data *data;\n-\tsize_t i;\n \tint ret = TRANSACTION_GENERIC_ERROR;\n \n \t/*\n@@ -1671,34 +1666,16 @@ static int packed_transaction_prepare(struct ref_store *ref_store,\n \t */\n \n \tCALLOC_ARRAY(data, 1);\n-\tstring_list_init_nodup(&data->updates);\n \n \ttransaction->backend_data = data;\n \n-\t/*\n-\t * Stick the updates in a string list by refname so that we\n-\t * can sort them:\n-\t */\n-\tfor (i = 0; i < transaction->nr; i++) {\n-\t\tstruct ref_update *update = transaction->updates[i];\n-\t\tstruct string_list_item *item =\n-\t\t\tstring_list_append(&data->updates, update->refname);\n-\n-\t\t/* Store a pointer to update in item->util: */\n-\t\titem->util = update;\n-\t}\n-\tstring_list_sort(&data->updates);\n-\n-\tif (ref_update_reject_duplicates(&data->updates, err))\n-\t\tgoto failure;\n-\n \tif (!is_lock_file_locked(&refs->lock)) {\n \t\tif (packed_refs_lock(ref_store, 0, err))\n \t\t\tgoto failure;\n \t\tdata->own_lock = 1;\n \t}\n \n-\tif (write_with_updates(refs, &data->updates, err))\n+\tif (write_with_updates(refs, &transaction->refnames, err))\n \t\tgoto failure;\n \n \ttransaction->state = REF_TRANSACTION_PREPARED;\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex e5862757a7..92db793026 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -3,6 +3,7 @@\n \n #include \"refs.h\"\n #include \"iterator.h\"\n+#include \"string-list.h\"\n \n struct fsck_options;\n struct ref_transaction;\n@@ -198,6 +199,7 @@ enum ref_transaction_state {\n struct ref_transaction {\n \tstruct ref_store *ref_store;\n \tstruct ref_update **updates;\n+\tstruct string_list refnames;\n \tsize_t alloc;\n \tsize_t nr;\n \tenum ref_transaction_state state;\ndiff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\nindex ae434cd248..a92c9a2f4f 100644\n--- a/refs/reftable-backend.c\n+++ b/refs/reftable-backend.c\n@@ -1076,7 +1076,6 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \tstruct reftable_ref_store *refs =\n \t\treftable_be_downcast(ref_store, REF_STORE_WRITE|REF_STORE_MAIN, \"ref_transaction_prepare\");\n \tstruct strbuf referent = STRBUF_INIT, head_referent = STRBUF_INIT;\n-\tstruct string_list affected_refnames = STRING_LIST_INIT_NODUP;\n \tstruct string_list refnames_to_check = STRING_LIST_INIT_NODUP;\n \tstruct reftable_transaction_data *tx_data = NULL;\n \tstruct reftable_backend *be;\n@@ -1101,10 +1100,6 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t\t\t\t\t\t transaction->updates[i], err);\n \t\tif (ret)\n \t\t\tgoto done;\n-\n-\t\tif (!(transaction->updates[i]->flags & REF_LOG_ONLY))\n-\t\t\tstring_list_append(&affected_refnames,\n-\t\t\t\t\t   transaction->updates[i]->refname);\n \t}\n \n \t/*\n@@ -1116,17 +1111,6 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t\ttx_data->args[i].updates_alloc = tx_data->args[i].updates_expected;\n \t}\n \n-\t/*\n-\t * Fail if a refname appears more than once in the transaction.\n-\t * This code is taken from the files backend and is a good candidate to\n-\t * be moved into the generic layer.\n-\t */\n-\tstring_list_sort(&affected_refnames);\n-\tif (ref_update_reject_duplicates(&affected_refnames, err)) {\n-\t\tret = TRANSACTION_GENERIC_ERROR;\n-\t\tgoto done;\n-\t}\n-\n \t/*\n \t * TODO: it's dubious whether we should reload the stack that \"HEAD\"\n \t * belongs to or not. In theory, it may happen that we only modify\n@@ -1194,14 +1178,12 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t\t    !(u->flags & REF_LOG_ONLY) &&\n \t\t    !(u->flags & REF_UPDATE_VIA_HEAD) &&\n \t\t    !strcmp(rewritten_ref, head_referent.buf)) {\n-\t\t\tstruct ref_update *new_update;\n-\n \t\t\t/*\n \t\t\t * First make sure that HEAD is not already in the\n \t\t\t * transaction. This check is O(lg N) in the transaction\n \t\t\t * size, but it happens at most once per transaction.\n \t\t\t */\n-\t\t\tif (string_list_has_string(&affected_refnames, \"HEAD\")) {\n+\t\t\tif (string_list_has_string(&transaction->refnames, \"HEAD\")) {\n \t\t\t\t/* An entry already existed */\n \t\t\t\tstrbuf_addf(err,\n \t\t\t\t\t    _(\"multiple updates for 'HEAD' (including one \"\n@@ -1211,12 +1193,11 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t\t\t\tgoto done;\n \t\t\t}\n \n-\t\t\tnew_update = ref_transaction_add_update(\n-\t\t\t\t\ttransaction, \"HEAD\",\n-\t\t\t\t\tu->flags | REF_LOG_ONLY | REF_NO_DEREF,\n-\t\t\t\t\t&u->new_oid, &u->old_oid, NULL, NULL, NULL,\n-\t\t\t\t\tu->msg);\n-\t\t\tstring_list_insert(&affected_refnames, new_update->refname);\n+\t\t\tref_transaction_add_update(\n+\t\t\t\ttransaction, \"HEAD\",\n+\t\t\t\tu->flags | REF_LOG_ONLY | REF_NO_DEREF,\n+\t\t\t\t&u->new_oid, &u->old_oid, NULL, NULL, NULL,\n+\t\t\t\tu->msg);\n \t\t}\n \n \t\tret = reftable_backend_read_ref(be, rewritten_ref,\n@@ -1281,6 +1262,15 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t\t\t\tif (!strcmp(rewritten_ref, \"HEAD\"))\n \t\t\t\t\tnew_flags |= REF_UPDATE_VIA_HEAD;\n \n+\t\t\t\tif (string_list_has_string(&transaction->refnames, referent.buf)) {\n+\t\t\t\t\tstrbuf_addf(err,\n+\t\t\t\t\t\t    _(\"multiple updates for '%s' (including one \"\n+\t\t\t\t\t\t    \"via symref '%s') are not allowed\"),\n+\t\t\t\t\t\t    referent.buf, u->refname);\n+\t\t\t\t\tret = TRANSACTION_NAME_CONFLICT;\n+\t\t\t\t\tgoto done;\n+\t\t\t\t}\n+\n \t\t\t\t/*\n \t\t\t\t * If we are updating a symref (eg. HEAD), we should also\n \t\t\t\t * update the branch that the symref points to.\n@@ -1305,16 +1295,6 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t\t\t\t */\n \t\t\t\tu->flags |= REF_LOG_ONLY | REF_NO_DEREF;\n \t\t\t\tu->flags &= ~REF_HAVE_OLD;\n-\n-\t\t\t\tif (string_list_has_string(&affected_refnames, new_update->refname)) {\n-\t\t\t\t\tstrbuf_addf(err,\n-\t\t\t\t\t\t    _(\"multiple updates for '%s' (including one \"\n-\t\t\t\t\t\t    \"via symref '%s') are not allowed\"),\n-\t\t\t\t\t\t    referent.buf, u->refname);\n-\t\t\t\t\tret = TRANSACTION_NAME_CONFLICT;\n-\t\t\t\t\tgoto done;\n-\t\t\t\t}\n-\t\t\t\tstring_list_insert(&affected_refnames, new_update->refname);\n \t\t\t}\n \t\t}\n \n@@ -1383,7 +1363,8 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t\t}\n \t}\n \n-\tret = refs_verify_refnames_available(ref_store, &refnames_to_check, &affected_refnames, NULL,\n+\tret = refs_verify_refnames_available(ref_store, &refnames_to_check,\n+\t\t\t\t\t     &transaction->refnames, NULL,\n \t\t\t\t\t     transaction->flags & REF_TRANSACTION_FLAG_INITIAL,\n \t\t\t\t\t     err);\n \tif (ret < 0)\n@@ -1401,7 +1382,6 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t\t\tstrbuf_addf(err, _(\"reftable: transaction prepare: %s\"),\n \t\t\t\t    reftable_error_str(ret));\n \t}\n-\tstring_list_clear(&affected_refnames, 0);\n \tstrbuf_release(&referent);\n \tstrbuf_release(&head_referent);\n \tstring_list_clear(&refnames_to_check, 0);\n\n-- \n2.48.1\n\n"},{"id":"514761","messageId":"20250320-245-partially-atomic-ref-updates-v4-3-3dcc1b311dc9@gmail.com","threadId":"63170","inReplyTo":"20250320-245-partially-atomic-ref-updates-v4-0-3dcc1b311dc9@gmail.com","subject":"[PATCH v4 3/8] refs/files: remove duplicate duplicates check","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2025-03-20T11:43:58Z","receivedAt":"2025-03-20T11:44:13Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Within the files reference backend's transaction's 'finish' phase, a\nverification step is currently performed wherein the refnames list is\nsorted and examined for multiple updates targeting the same refname.\n\nIt has been observed that this verification is redundant, as an\nidentical check is already executed during the transaction's 'prepare'\nstage. Since the refnames list remains unmodified following the\n'prepare' stage, this secondary verification can be safely eliminated.\n\nThe duplicate check has been removed accordingly, and the\n`ref_update_reject_duplicates()` function has been marked as static, as\nits usage is now confined to 'refs.c'.\n\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n refs.c               | 9 +++++++--\n refs/files-backend.c | 6 ------\n refs/refs-internal.h | 8 --------\n 3 files changed, 7 insertions(+), 16 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 504bf2063e..61bed9672a 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2303,8 +2303,13 @@ int refs_update_symref_extended(struct ref_store *refs, const char *ref,\n \treturn ret;\n }\n \n-int ref_update_reject_duplicates(struct string_list *refnames,\n-\t\t\t\t struct strbuf *err)\n+/*\n+ * Write an error to `err` and return a nonzero value iff the same\n+ * refname appears multiple times in `refnames`. `refnames` must be\n+ * sorted on entry to this function.\n+ */\n+static int ref_update_reject_duplicates(struct string_list *refnames,\n+\t\t\t\t\tstruct strbuf *err)\n {\n \tsize_t i, n = refnames->nr;\n \ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 58f62ea8a3..ea023a59fc 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -3016,12 +3016,6 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,\n \tif (transaction->state != REF_TRANSACTION_PREPARED)\n \t\tBUG(\"commit called for transaction that is not prepared\");\n \n-\tstring_list_sort(&transaction->refnames);\n-\tif (ref_update_reject_duplicates(&transaction->refnames, err)) {\n-\t\tret = TRANSACTION_GENERIC_ERROR;\n-\t\tgoto cleanup;\n-\t}\n-\n \t/*\n \t * It's really undefined to call this function in an active\n \t * repository or when there are existing references: we are\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 92db793026..6d3770d0cc 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -142,14 +142,6 @@ int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,\n \t\t      struct object_id *oid, struct strbuf *referent,\n \t\t      unsigned int *type, int *failure_errno);\n \n-/*\n- * Write an error to `err` and return a nonzero value iff the same\n- * refname appears multiple times in `refnames`. `refnames` must be\n- * sorted on entry to this function.\n- */\n-int ref_update_reject_duplicates(struct string_list *refnames,\n-\t\t\t\t struct strbuf *err);\n-\n /*\n  * Add a ref_update with the specified properties to transaction, and\n  * return a pointer to the new object. This function does not verify\n\n-- \n2.48.1\n\n"},{"id":"514763","messageId":"20250320-245-partially-atomic-ref-updates-v4-4-3dcc1b311dc9@gmail.com","threadId":"63170","inReplyTo":"20250320-245-partially-atomic-ref-updates-v4-0-3dcc1b311dc9@gmail.com","subject":"[PATCH v4 4/8] refs/reftable: extract code from the transaction preparation","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2025-03-20T11:43:59Z","receivedAt":"2025-03-20T11:44:14Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Extract the core logic for preparing individual reference updates from\n`reftable_be_transaction_prepare()` into `prepare_single_update()`. This\ndedicated function now handles all validation and preparation steps for\neach reference update in the transaction, including object ID\nverification, HEAD reference handling, and symref processing.\n\nThe refactoring consolidates all reference update validation into a\nsingle logical block, which improves code maintainability and\nreadability. More importantly, this restructuring lays the groundwork\nfor implementing batched reference update support in the reftable\nbackend, which will be introduced in a followup commit.\n\nNo functional changes are included in this commit - it is purely a code\nreorganization to support future enhancements.\n\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n refs/reftable-backend.c | 463 +++++++++++++++++++++++++-----------------------\n 1 file changed, 237 insertions(+), 226 deletions(-)\n\ndiff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\nindex a92c9a2f4f..786df11a03 100644\n--- a/refs/reftable-backend.c\n+++ b/refs/reftable-backend.c\n@@ -1069,6 +1069,239 @@ static int queue_transaction_update(struct reftable_ref_store *refs,\n \treturn 0;\n }\n \n+static int prepare_single_update(struct reftable_ref_store *refs,\n+\t\t\t\t struct reftable_transaction_data *tx_data,\n+\t\t\t\t struct ref_transaction *transaction,\n+\t\t\t\t struct reftable_backend *be,\n+\t\t\t\t struct ref_update *u,\n+\t\t\t\t struct string_list *refnames_to_check,\n+\t\t\t\t unsigned int head_type,\n+\t\t\t\t struct strbuf *head_referent,\n+\t\t\t\t struct strbuf *referent,\n+\t\t\t\t struct strbuf *err)\n+{\n+\tstruct object_id current_oid = {0};\n+\tconst char *rewritten_ref;\n+\tint ret = 0;\n+\n+\t/*\n+\t * There is no need to reload the respective backends here as\n+\t * we have already reloaded them when preparing the transaction\n+\t * update. And given that the stacks have been locked there\n+\t * shouldn't have been any concurrent modifications of the\n+\t * stack.\n+\t */\n+\tret = backend_for(&be, refs, u->refname, &rewritten_ref, 0);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\t/* Verify that the new object ID is valid. */\n+\tif ((u->flags & REF_HAVE_NEW) && !is_null_oid(&u->new_oid) &&\n+\t    !(u->flags & REF_SKIP_OID_VERIFICATION) &&\n+\t    !(u->flags & REF_LOG_ONLY)) {\n+\t\tstruct object *o = parse_object(refs->base.repo, &u->new_oid);\n+\t\tif (!o) {\n+\t\t\tstrbuf_addf(err,\n+\t\t\t\t    _(\"trying to write ref '%s' with nonexistent object %s\"),\n+\t\t\t\t    u->refname, oid_to_hex(&u->new_oid));\n+\t\t\treturn -1;\n+\t\t}\n+\n+\t\tif (o->type != OBJ_COMMIT && is_branch(u->refname)) {\n+\t\t\tstrbuf_addf(err, _(\"trying to write non-commit object %s to branch '%s'\"),\n+\t\t\t\t    oid_to_hex(&u->new_oid), u->refname);\n+\t\t\treturn -1;\n+\t\t}\n+\t}\n+\n+\t/*\n+\t * When we update the reference that HEAD points to we enqueue\n+\t * a second log-only update for HEAD so that its reflog is\n+\t * updated accordingly.\n+\t */\n+\tif (head_type == REF_ISSYMREF &&\n+\t    !(u->flags & REF_LOG_ONLY) &&\n+\t    !(u->flags & REF_UPDATE_VIA_HEAD) &&\n+\t    !strcmp(rewritten_ref, head_referent->buf)) {\n+\t\t/*\n+\t\t * First make sure that HEAD is not already in the\n+\t\t * transaction. This check is O(lg N) in the transaction\n+\t\t * size, but it happens at most once per transaction.\n+\t\t */\n+\t\tif (string_list_has_string(&transaction->refnames, \"HEAD\")) {\n+\t\t\t/* An entry already existed */\n+\t\t\tstrbuf_addf(err,\n+\t\t\t\t    _(\"multiple updates for 'HEAD' (including one \"\n+\t\t\t\t      \"via its referent '%s') are not allowed\"),\n+\t\t\t\t    u->refname);\n+\t\t\treturn TRANSACTION_NAME_CONFLICT;\n+\t\t}\n+\n+\t\tref_transaction_add_update(\n+\t\t\ttransaction, \"HEAD\",\n+\t\t\tu->flags | REF_LOG_ONLY | REF_NO_DEREF,\n+\t\t\t&u->new_oid, &u->old_oid, NULL, NULL, NULL,\n+\t\t\tu->msg);\n+\t}\n+\n+\tret = reftable_backend_read_ref(be, rewritten_ref,\n+\t\t\t\t\t&current_oid, referent, &u->type);\n+\tif (ret < 0)\n+\t\treturn ret;\n+\tif (ret > 0 && !ref_update_expects_existing_old_ref(u)) {\n+\t\t/*\n+\t\t * The reference does not exist, and we either have no\n+\t\t * old object ID or expect the reference to not exist.\n+\t\t * We can thus skip below safety checks as well as the\n+\t\t * symref splitting. But we do want to verify that\n+\t\t * there is no conflicting reference here so that we\n+\t\t * can output a proper error message instead of failing\n+\t\t * at a later point.\n+\t\t */\n+\t\tstring_list_append(refnames_to_check, u->refname);\n+\n+\t\t/*\n+\t\t * There is no need to write the reference deletion\n+\t\t * when the reference in question doesn't exist.\n+\t\t */\n+\t\tif ((u->flags & REF_HAVE_NEW) && !ref_update_has_null_new_value(u)) {\n+\t\t\tret = queue_transaction_update(refs, tx_data, u,\n+\t\t\t\t\t\t       &current_oid, err);\n+\t\t\tif (ret)\n+\t\t\t\treturn ret;\n+\t\t}\n+\n+\t\treturn 0;\n+\t}\n+\tif (ret > 0) {\n+\t\t/* The reference does not exist, but we expected it to. */\n+\t\tstrbuf_addf(err, _(\"cannot lock ref '%s': \"\n+\n+\n+\t\t\t\t   \"unable to resolve reference '%s'\"),\n+\t\t\t    ref_update_original_update_refname(u), u->refname);\n+\t\treturn -1;\n+\t}\n+\n+\tif (u->type & REF_ISSYMREF) {\n+\t\t/*\n+\t\t * The reftable stack is locked at this point already,\n+\t\t * so it is safe to call `refs_resolve_ref_unsafe()`\n+\t\t * here without causing races.\n+\t\t */\n+\t\tconst char *resolved = refs_resolve_ref_unsafe(&refs->base, u->refname, 0,\n+\t\t\t\t\t\t\t       &current_oid, NULL);\n+\n+\t\tif (u->flags & REF_NO_DEREF) {\n+\t\t\tif (u->flags & REF_HAVE_OLD && !resolved) {\n+\t\t\t\tstrbuf_addf(err, _(\"cannot lock ref '%s': \"\n+\t\t\t\t\t\t   \"error reading reference\"), u->refname);\n+\t\t\t\treturn -1;\n+\t\t\t}\n+\t\t} else {\n+\t\t\tstruct ref_update *new_update;\n+\t\t\tint new_flags;\n+\n+\t\t\tnew_flags = u->flags;\n+\t\t\tif (!strcmp(rewritten_ref, \"HEAD\"))\n+\t\t\t\tnew_flags |= REF_UPDATE_VIA_HEAD;\n+\n+\t\t\tif (string_list_has_string(&transaction->refnames, referent->buf)) {\n+\t\t\t\tstrbuf_addf(err,\n+\t\t\t\t\t    _(\"multiple updates for '%s' (including one \"\n+\t\t\t\t\t      \"via symref '%s') are not allowed\"),\n+\t\t\t\t\t    referent->buf, u->refname);\n+\t\t\t\treturn TRANSACTION_NAME_CONFLICT;\n+\t\t\t}\n+\n+\t\t\t/*\n+\t\t\t * If we are updating a symref (eg. HEAD), we should also\n+\t\t\t * update the branch that the symref points to.\n+\t\t\t *\n+\t\t\t * This is generic functionality, and would be better\n+\t\t\t * done in refs.c, but the current implementation is\n+\t\t\t * intertwined with the locking in files-backend.c.\n+\t\t\t */\n+\t\t\tnew_update = ref_transaction_add_update(\n+\t\t\t\ttransaction, referent->buf, new_flags,\n+\t\t\t\tu->new_target ? NULL : &u->new_oid,\n+\t\t\t\tu->old_target ? NULL : &u->old_oid,\n+\t\t\t\tu->new_target, u->old_target,\n+\t\t\t\tu->committer_info, u->msg);\n+\n+\t\t\tnew_update->parent_update = u;\n+\n+\t\t\t/*\n+\t\t\t * Change the symbolic ref update to log only. Also, it\n+\t\t\t * doesn't need to check its old OID value, as that will be\n+\t\t\t * done when new_update is processed.\n+\t\t\t */\n+\t\t\tu->flags |= REF_LOG_ONLY | REF_NO_DEREF;\n+\t\t\tu->flags &= ~REF_HAVE_OLD;\n+\t\t}\n+\t}\n+\n+\t/*\n+\t * Verify that the old object matches our expectations. Note\n+\t * that the error messages here do not make a lot of sense in\n+\t * the context of the reftable backend as we never lock\n+\t * individual refs. But the error messages match what the files\n+\t * backend returns, which keeps our tests happy.\n+\t */\n+\tif (u->old_target) {\n+\t\tif (!(u->type & REF_ISSYMREF)) {\n+\t\t\tstrbuf_addf(err, _(\"cannot lock ref '%s': \"\n+\t\t\t\t\t   \"expected symref with target '%s': \"\n+\t\t\t\t\t   \"but is a regular ref\"),\n+\t\t\t\t    ref_update_original_update_refname(u),\n+\t\t\t\t    u->old_target);\n+\t\t\treturn -1;\n+\t\t}\n+\n+\t\tif (ref_update_check_old_target(referent->buf, u, err)) {\n+\t\t\treturn -1;\n+\t\t}\n+\t} else if ((u->flags & REF_HAVE_OLD) && !oideq(&current_oid, &u->old_oid)) {\n+\t\tif (is_null_oid(&u->old_oid)) {\n+\t\t\tstrbuf_addf(err, _(\"cannot lock ref '%s': \"\n+\t\t\t\t\t   \"reference already exists\"),\n+\t\t\t\t    ref_update_original_update_refname(u));\n+\t\t\treturn TRANSACTION_CREATE_EXISTS;\n+\t\t}\n+\t\telse if (is_null_oid(&current_oid))\n+\t\t\tstrbuf_addf(err, _(\"cannot lock ref '%s': \"\n+\t\t\t\t\t   \"reference is missing but expected %s\"),\n+\t\t\t\t    ref_update_original_update_refname(u),\n+\t\t\t\t    oid_to_hex(&u->old_oid));\n+\t\telse\n+\t\t\tstrbuf_addf(err, _(\"cannot lock ref '%s': \"\n+\t\t\t\t\t   \"is at %s but expected %s\"),\n+\t\t\t\t    ref_update_original_update_refname(u),\n+\t\t\t\t    oid_to_hex(&current_oid),\n+\t\t\t\t    oid_to_hex(&u->old_oid));\n+\t\treturn TRANSACTION_NAME_CONFLICT;\n+\t}\n+\n+\t/*\n+\t * If all of the following conditions are true:\n+\t *\n+\t *   - We're not about to write a symref.\n+\t *   - We're not about to write a log-only entry.\n+\t *   - Old and new object ID are different.\n+\t *\n+\t * Then we're essentially doing a no-op update that can be\n+\t * skipped. This is not only for the sake of efficiency, but\n+\t * also skips writing unneeded reflog entries.\n+\t */\n+\tif ((u->type & REF_ISSYMREF) ||\n+\t    (u->flags & REF_LOG_ONLY) ||\n+\t    (u->flags & REF_HAVE_NEW && !oideq(&current_oid, &u->new_oid)))\n+\t\treturn queue_transaction_update(refs, tx_data, u,\n+\t\t\t\t\t       &current_oid, err);\n+\n+\treturn 0;\n+}\n+\n static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t\t\t\t\t   struct ref_transaction *transaction,\n \t\t\t\t\t   struct strbuf *err)\n@@ -1133,234 +1366,12 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \tret = 0;\n \n \tfor (i = 0; i < transaction->nr; i++) {\n-\t\tstruct ref_update *u = transaction->updates[i];\n-\t\tstruct object_id current_oid = {0};\n-\t\tconst char *rewritten_ref;\n-\n-\t\t/*\n-\t\t * There is no need to reload the respective backends here as\n-\t\t * we have already reloaded them when preparing the transaction\n-\t\t * update. And given that the stacks have been locked there\n-\t\t * shouldn't have been any concurrent modifications of the\n-\t\t * stack.\n-\t\t */\n-\t\tret = backend_for(&be, refs, u->refname, &rewritten_ref, 0);\n+\t\tret = prepare_single_update(refs, tx_data, transaction, be,\n+\t\t\t\t\t    transaction->updates[i],\n+\t\t\t\t\t    &refnames_to_check, head_type,\n+\t\t\t\t\t    &head_referent, &referent, err);\n \t\tif (ret)\n \t\t\tgoto done;\n-\n-\t\t/* Verify that the new object ID is valid. */\n-\t\tif ((u->flags & REF_HAVE_NEW) && !is_null_oid(&u->new_oid) &&\n-\t\t    !(u->flags & REF_SKIP_OID_VERIFICATION) &&\n-\t\t    !(u->flags & REF_LOG_ONLY)) {\n-\t\t\tstruct object *o = parse_object(refs->base.repo, &u->new_oid);\n-\t\t\tif (!o) {\n-\t\t\t\tstrbuf_addf(err,\n-\t\t\t\t\t    _(\"trying to write ref '%s' with nonexistent object %s\"),\n-\t\t\t\t\t    u->refname, oid_to_hex(&u->new_oid));\n-\t\t\t\tret = -1;\n-\t\t\t\tgoto done;\n-\t\t\t}\n-\n-\t\t\tif (o->type != OBJ_COMMIT && is_branch(u->refname)) {\n-\t\t\t\tstrbuf_addf(err, _(\"trying to write non-commit object %s to branch '%s'\"),\n-\t\t\t\t\t    oid_to_hex(&u->new_oid), u->refname);\n-\t\t\t\tret = -1;\n-\t\t\t\tgoto done;\n-\t\t\t}\n-\t\t}\n-\n-\t\t/*\n-\t\t * When we update the reference that HEAD points to we enqueue\n-\t\t * a second log-only update for HEAD so that its reflog is\n-\t\t * updated accordingly.\n-\t\t */\n-\t\tif (head_type == REF_ISSYMREF &&\n-\t\t    !(u->flags & REF_LOG_ONLY) &&\n-\t\t    !(u->flags & REF_UPDATE_VIA_HEAD) &&\n-\t\t    !strcmp(rewritten_ref, head_referent.buf)) {\n-\t\t\t/*\n-\t\t\t * First make sure that HEAD is not already in the\n-\t\t\t * transaction. This check is O(lg N) in the transaction\n-\t\t\t * size, but it happens at most once per transaction.\n-\t\t\t */\n-\t\t\tif (string_list_has_string(&transaction->refnames, \"HEAD\")) {\n-\t\t\t\t/* An entry already existed */\n-\t\t\t\tstrbuf_addf(err,\n-\t\t\t\t\t    _(\"multiple updates for 'HEAD' (including one \"\n-\t\t\t\t\t    \"via its referent '%s') are not allowed\"),\n-\t\t\t\t\t    u->refname);\n-\t\t\t\tret = TRANSACTION_NAME_CONFLICT;\n-\t\t\t\tgoto done;\n-\t\t\t}\n-\n-\t\t\tref_transaction_add_update(\n-\t\t\t\ttransaction, \"HEAD\",\n-\t\t\t\tu->flags | REF_LOG_ONLY | REF_NO_DEREF,\n-\t\t\t\t&u->new_oid, &u->old_oid, NULL, NULL, NULL,\n-\t\t\t\tu->msg);\n-\t\t}\n-\n-\t\tret = reftable_backend_read_ref(be, rewritten_ref,\n-\t\t\t\t\t\t&current_oid, &referent, &u->type);\n-\t\tif (ret < 0)\n-\t\t\tgoto done;\n-\t\tif (ret > 0 && !ref_update_expects_existing_old_ref(u)) {\n-\t\t\t/*\n-\t\t\t * The reference does not exist, and we either have no\n-\t\t\t * old object ID or expect the reference to not exist.\n-\t\t\t * We can thus skip below safety checks as well as the\n-\t\t\t * symref splitting. But we do want to verify that\n-\t\t\t * there is no conflicting reference here so that we\n-\t\t\t * can output a proper error message instead of failing\n-\t\t\t * at a later point.\n-\t\t\t */\n-\t\t\tstring_list_append(&refnames_to_check, u->refname);\n-\n-\t\t\t/*\n-\t\t\t * There is no need to write the reference deletion\n-\t\t\t * when the reference in question doesn't exist.\n-\t\t\t */\n-\t\t\t if ((u->flags & REF_HAVE_NEW) && !ref_update_has_null_new_value(u)) {\n-\t\t\t\t ret = queue_transaction_update(refs, tx_data, u,\n-\t\t\t\t\t\t\t\t&current_oid, err);\n-\t\t\t\t if (ret)\n-\t\t\t\t\t goto done;\n-\t\t\t }\n-\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (ret > 0) {\n-\t\t\t/* The reference does not exist, but we expected it to. */\n-\t\t\tstrbuf_addf(err, _(\"cannot lock ref '%s': \"\n-\t\t\t\t    \"unable to resolve reference '%s'\"),\n-\t\t\t\t    ref_update_original_update_refname(u), u->refname);\n-\t\t\tret = -1;\n-\t\t\tgoto done;\n-\t\t}\n-\n-\t\tif (u->type & REF_ISSYMREF) {\n-\t\t\t/*\n-\t\t\t * The reftable stack is locked at this point already,\n-\t\t\t * so it is safe to call `refs_resolve_ref_unsafe()`\n-\t\t\t * here without causing races.\n-\t\t\t */\n-\t\t\tconst char *resolved = refs_resolve_ref_unsafe(&refs->base, u->refname, 0,\n-\t\t\t\t\t\t\t\t       &current_oid, NULL);\n-\n-\t\t\tif (u->flags & REF_NO_DEREF) {\n-\t\t\t\tif (u->flags & REF_HAVE_OLD && !resolved) {\n-\t\t\t\t\tstrbuf_addf(err, _(\"cannot lock ref '%s': \"\n-\t\t\t\t\t\t    \"error reading reference\"), u->refname);\n-\t\t\t\t\tret = -1;\n-\t\t\t\t\tgoto done;\n-\t\t\t\t}\n-\t\t\t} else {\n-\t\t\t\tstruct ref_update *new_update;\n-\t\t\t\tint new_flags;\n-\n-\t\t\t\tnew_flags = u->flags;\n-\t\t\t\tif (!strcmp(rewritten_ref, \"HEAD\"))\n-\t\t\t\t\tnew_flags |= REF_UPDATE_VIA_HEAD;\n-\n-\t\t\t\tif (string_list_has_string(&transaction->refnames, referent.buf)) {\n-\t\t\t\t\tstrbuf_addf(err,\n-\t\t\t\t\t\t    _(\"multiple updates for '%s' (including one \"\n-\t\t\t\t\t\t    \"via symref '%s') are not allowed\"),\n-\t\t\t\t\t\t    referent.buf, u->refname);\n-\t\t\t\t\tret = TRANSACTION_NAME_CONFLICT;\n-\t\t\t\t\tgoto done;\n-\t\t\t\t}\n-\n-\t\t\t\t/*\n-\t\t\t\t * If we are updating a symref (eg. HEAD), we should also\n-\t\t\t\t * update the branch that the symref points to.\n-\t\t\t\t *\n-\t\t\t\t * This is generic functionality, and would be better\n-\t\t\t\t * done in refs.c, but the current implementation is\n-\t\t\t\t * intertwined with the locking in files-backend.c.\n-\t\t\t\t */\n-\t\t\t\tnew_update = ref_transaction_add_update(\n-\t\t\t\t\ttransaction, referent.buf, new_flags,\n-\t\t\t\t\tu->new_target ? NULL : &u->new_oid,\n-\t\t\t\t\tu->old_target ? NULL : &u->old_oid,\n-\t\t\t\t\tu->new_target, u->old_target,\n-\t\t\t\t\tu->committer_info, u->msg);\n-\n-\t\t\t\tnew_update->parent_update = u;\n-\n-\t\t\t\t/*\n-\t\t\t\t * Change the symbolic ref update to log only. Also, it\n-\t\t\t\t * doesn't need to check its old OID value, as that will be\n-\t\t\t\t * done when new_update is processed.\n-\t\t\t\t */\n-\t\t\t\tu->flags |= REF_LOG_ONLY | REF_NO_DEREF;\n-\t\t\t\tu->flags &= ~REF_HAVE_OLD;\n-\t\t\t}\n-\t\t}\n-\n-\t\t/*\n-\t\t * Verify that the old object matches our expectations. Note\n-\t\t * that the error messages here do not make a lot of sense in\n-\t\t * the context of the reftable backend as we never lock\n-\t\t * individual refs. But the error messages match what the files\n-\t\t * backend returns, which keeps our tests happy.\n-\t\t */\n-\t\tif (u->old_target) {\n-\t\t\tif (!(u->type & REF_ISSYMREF)) {\n-\t\t\t\tstrbuf_addf(err, _(\"cannot lock ref '%s': \"\n-\t\t\t\t\t   \"expected symref with target '%s': \"\n-\t\t\t\t\t   \"but is a regular ref\"),\n-\t\t\t\t\t    ref_update_original_update_refname(u),\n-\t\t\t\t\t    u->old_target);\n-\t\t\t\tret = -1;\n-\t\t\t\tgoto done;\n-\t\t\t}\n-\n-\t\t\tif (ref_update_check_old_target(referent.buf, u, err)) {\n-\t\t\t\tret = -1;\n-\t\t\t\tgoto done;\n-\t\t\t}\n-\t\t} else if ((u->flags & REF_HAVE_OLD) && !oideq(&current_oid, &u->old_oid)) {\n-\t\t\tret = TRANSACTION_NAME_CONFLICT;\n-\t\t\tif (is_null_oid(&u->old_oid)) {\n-\t\t\t\tstrbuf_addf(err, _(\"cannot lock ref '%s': \"\n-\t\t\t\t\t\t   \"reference already exists\"),\n-\t\t\t\t\t    ref_update_original_update_refname(u));\n-\t\t\t\tret = TRANSACTION_CREATE_EXISTS;\n-\t\t\t}\n-\t\t\telse if (is_null_oid(&current_oid))\n-\t\t\t\tstrbuf_addf(err, _(\"cannot lock ref '%s': \"\n-\t\t\t\t\t\t   \"reference is missing but expected %s\"),\n-\t\t\t\t\t    ref_update_original_update_refname(u),\n-\t\t\t\t\t    oid_to_hex(&u->old_oid));\n-\t\t\telse\n-\t\t\t\tstrbuf_addf(err, _(\"cannot lock ref '%s': \"\n-\t\t\t\t\t\t   \"is at %s but expected %s\"),\n-\t\t\t\t\t    ref_update_original_update_refname(u),\n-\t\t\t\t\t    oid_to_hex(&current_oid),\n-\t\t\t\t\t    oid_to_hex(&u->old_oid));\n-\t\t\tgoto done;\n-\t\t}\n-\n-\t\t/*\n-\t\t * If all of the following conditions are true:\n-\t\t *\n-\t\t *   - We're not about to write a symref.\n-\t\t *   - We're not about to write a log-only entry.\n-\t\t *   - Old and new object ID are different.\n-\t\t *\n-\t\t * Then we're essentially doing a no-op update that can be\n-\t\t * skipped. This is not only for the sake of efficiency, but\n-\t\t * also skips writing unneeded reflog entries.\n-\t\t */\n-\t\tif ((u->type & REF_ISSYMREF) ||\n-\t\t    (u->flags & REF_LOG_ONLY) ||\n-\t\t    (u->flags & REF_HAVE_NEW && !oideq(&current_oid, &u->new_oid))) {\n-\t\t\tret = queue_transaction_update(refs, tx_data, u,\n-\t\t\t\t\t\t       &current_oid, err);\n-\t\t\tif (ret)\n-\t\t\t\tgoto done;\n-\t\t}\n \t}\n \n \tret = refs_verify_refnames_available(ref_store, &refnames_to_check,\n\n-- \n2.48.1\n\n"},{"id":"514764","messageId":"20250320-245-partially-atomic-ref-updates-v4-6-3dcc1b311dc9@gmail.com","threadId":"63170","inReplyTo":"20250320-245-partially-atomic-ref-updates-v4-0-3dcc1b311dc9@gmail.com","subject":"[PATCH v4 6/8] refs: implement batch reference update support","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2025-03-20T11:44:01Z","receivedAt":"2025-03-20T11:44:15Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Git supports making reference updates with or without transactions.\nUpdates with transactions are generally better optimized. But\ntransactions are all or nothing. This means, if a user wants to batch\nupdates to take advantage of the optimizations without the hard\nrequirement that all updates must succeed, there is no way currently to\ndo so. Particularly with the reftable backend where batching multiple\nreference updates is more efficient than performing them sequentially.\n\nIntroduce batched update support with a new flag,\n'REF_TRANSACTION_ALLOW_FAILURE'. Batched updates while different from\ntransactions, use the transaction infrastructure under the hood. When\nenabled, this flag allows individual reference updates that would\ntypically cause the entire transaction to fail due to non-system-related\nerrors to be marked as rejected while permitting other updates to\nproceed. System errors referred by 'REF_TRANSACTION_ERROR_GENERIC'\ncontinue to result in the entire transaction failing. This approach\nenhances flexibility while preserving transactional integrity where\nnecessary.\n\nThe implementation introduces several key components:\n\n  - Add 'rejection_err' field to struct `ref_update` to track failed\n    updates with failure reason.\n\n  - Add a new struct `ref_transaction_rejections` and a field within\n    `ref_transaction` to this struct to allow quick iteration over\n    rejected updates.\n\n  - Modify reference backends (files, packed, reftable) to handle\n    partial transactions by using `ref_transaction_set_rejected()`\n    instead of failing the entire transaction when\n    `REF_TRANSACTION_ALLOW_FAILURE` is set.\n\n  - Add `ref_transaction_for_each_rejected_update()` to let callers\n    examine which updates were rejected and why.\n\nThis foundational change enables batched update support throughout the\nreference subsystem. A following commit will expose this capability to\nusers by adding a `--batch-updates` flag to 'git-update-ref(1)',\nproviding both a user-facing feature and a testable implementation.\n\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n refs.c                  | 61 +++++++++++++++++++++++++++++++++++++++++++++++++\n refs.h                  | 22 ++++++++++++++++++\n refs/files-backend.c    | 12 +++++++++-\n refs/packed-backend.c   | 27 ++++++++++++++++++++--\n refs/refs-internal.h    | 26 +++++++++++++++++++++\n refs/reftable-backend.c | 12 +++++++++-\n 6 files changed, 156 insertions(+), 4 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 3d0b53d56e..b34ec198f5 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1176,6 +1176,10 @@ struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,\n \ttr->ref_store = refs;\n \ttr->flags = flags;\n \tstring_list_init_dup(&tr->refnames);\n+\n+\tif (flags & REF_TRANSACTION_ALLOW_FAILURE)\n+\t\tCALLOC_ARRAY(tr->rejections, 1);\n+\n \treturn tr;\n }\n \n@@ -1206,11 +1210,45 @@ void ref_transaction_free(struct ref_transaction *transaction)\n \t\tfree((char *)transaction->updates[i]->old_target);\n \t\tfree(transaction->updates[i]);\n \t}\n+\n+\tif (transaction->rejections)\n+\t\tfree(transaction->rejections->update_indices);\n+\tfree(transaction->rejections);\n+\n \tstring_list_clear(&transaction->refnames, 0);\n \tfree(transaction->updates);\n \tfree(transaction);\n }\n \n+int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction,\n+\t\t\t\t       size_t update_idx,\n+\t\t\t\t       enum ref_transaction_error err)\n+{\n+\tif (update_idx >= transaction->nr)\n+\t\tBUG(\"trying to set rejection on invalid update index\");\n+\n+\tif (!(transaction->flags & REF_TRANSACTION_ALLOW_FAILURE))\n+\t\treturn 0;\n+\n+\tif (!transaction->rejections)\n+\t\tBUG(\"transaction not inititalized with failure support\");\n+\n+\t/*\n+\t * Don't accept generic errors, since these errors are not user\n+\t * input related.\n+\t */\n+\tif (err == REF_TRANSACTION_ERROR_GENERIC)\n+\t\treturn 0;\n+\n+\ttransaction->updates[update_idx]->rejection_err = err;\n+\tALLOC_GROW(transaction->rejections->update_indices,\n+\t\t   transaction->rejections->nr + 1,\n+\t\t   transaction->rejections->alloc);\n+\ttransaction->rejections->update_indices[transaction->rejections->nr++] = update_idx;\n+\n+\treturn 1;\n+}\n+\n struct ref_update *ref_transaction_add_update(\n \t\tstruct ref_transaction *transaction,\n \t\tconst char *refname, unsigned int flags,\n@@ -1236,6 +1274,7 @@ struct ref_update *ref_transaction_add_update(\n \ttransaction->updates[transaction->nr++] = update;\n \n \tupdate->flags = flags;\n+\tupdate->rejection_err = 0;\n \n \tupdate->new_target = xstrdup_or_null(new_target);\n \tupdate->old_target = xstrdup_or_null(old_target);\n@@ -2728,6 +2767,28 @@ void ref_transaction_for_each_queued_update(struct ref_transaction *transaction,\n \t}\n }\n \n+void ref_transaction_for_each_rejected_update(struct ref_transaction *transaction,\n+\t\t\t\t\t      ref_transaction_for_each_rejected_update_fn cb,\n+\t\t\t\t\t      void *cb_data)\n+{\n+\tif (!transaction->rejections)\n+\t\treturn;\n+\n+\tfor (size_t i = 0; i < transaction->rejections->nr; i++) {\n+\t\tsize_t update_index = transaction->rejections->update_indices[i];\n+\t\tstruct ref_update *update = transaction->updates[update_index];\n+\n+\t\tif (!update->rejection_err)\n+\t\t\tcontinue;\n+\n+\t\tcb(update->refname,\n+\t\t   (update->flags & REF_HAVE_OLD) ? &update->old_oid : NULL,\n+\t\t   (update->flags & REF_HAVE_NEW) ? &update->new_oid : NULL,\n+\t\t   update->old_target, update->new_target,\n+\t\t   update->rejection_err, cb_data);\n+\t}\n+}\n+\n int refs_delete_refs(struct ref_store *refs, const char *logmsg,\n \t\t     struct string_list *refnames, unsigned int flags)\n {\ndiff --git a/refs.h b/refs.h\nindex dcd83e81e2..591b703d59 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -673,6 +673,13 @@ enum ref_transaction_flag {\n \t * either be absent or null_oid.\n \t */\n \tREF_TRANSACTION_FLAG_INITIAL = (1 << 0),\n+\n+\t/*\n+\t * The transaction mechanism by default fails all updates if any conflict\n+\t * is detected. This flag allows transactions to partially apply updates\n+\t * while rejecting updates which do not match the expected state.\n+\t */\n+\tREF_TRANSACTION_ALLOW_FAILURE = (1 << 1),\n };\n \n /*\n@@ -903,6 +910,21 @@ void ref_transaction_for_each_queued_update(struct ref_transaction *transaction,\n \t\t\t\t\t    ref_transaction_for_each_queued_update_fn cb,\n \t\t\t\t\t    void *cb_data);\n \n+/*\n+ * Execute the given callback function for each of the reference updates which\n+ * have been rejected in the given transaction.\n+ */\n+typedef void ref_transaction_for_each_rejected_update_fn(const char *refname,\n+\t\t\t\t\t\t\t const struct object_id *old_oid,\n+\t\t\t\t\t\t\t const struct object_id *new_oid,\n+\t\t\t\t\t\t\t const char *old_target,\n+\t\t\t\t\t\t\t const char *new_target,\n+\t\t\t\t\t\t\t enum ref_transaction_error err,\n+\t\t\t\t\t\t\t void *cb_data);\n+void ref_transaction_for_each_rejected_update(struct ref_transaction *transaction,\n+\t\t\t\t\t      ref_transaction_for_each_rejected_update_fn cb,\n+\t\t\t\t\t      void *cb_data);\n+\n /*\n  * Free `*transaction` and all associated data.\n  */\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 4f27f7652c..be758ffff5 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2852,8 +2852,15 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \t\tret = lock_ref_for_update(refs, update, transaction,\n \t\t\t\t\t  head_ref, &refnames_to_check,\n \t\t\t\t\t  err);\n-\t\tif (ret)\n+\t\tif (ret) {\n+\t\t\tif (ref_transaction_maybe_set_rejected(transaction, i, ret)) {\n+\t\t\t\tstrbuf_setlen(err, 0);\n+\t\t\t\tret = 0;\n+\n+\t\t\t\tcontinue;\n+\t\t\t}\n \t\t\tgoto cleanup;\n+\t\t}\n \n \t\tif (update->flags & REF_DELETING &&\n \t\t    !(update->flags & REF_LOG_ONLY) &&\n@@ -3151,6 +3158,9 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \t\tstruct ref_update *update = transaction->updates[i];\n \t\tstruct ref_lock *lock = update->backend_data;\n \n+\t\tif (update->rejection_err)\n+\t\t\tcontinue;\n+\n \t\tif (update->flags & REF_NEEDS_COMMIT ||\n \t\t    update->flags & REF_LOG_ONLY) {\n \t\t\tif (parse_and_write_reflog(refs, update, lock, err)) {\ndiff --git a/refs/packed-backend.c b/refs/packed-backend.c\nindex d90bd815a3..7bf57ca948 100644\n--- a/refs/packed-backend.c\n+++ b/refs/packed-backend.c\n@@ -1327,10 +1327,11 @@ static int packed_ref_store_remove_on_disk(struct ref_store *ref_store,\n  * remain locked when it is done.\n  */\n static enum ref_transaction_error write_with_updates(struct packed_ref_store *refs,\n-\t\t\t\t\t\t     struct string_list *updates,\n+\t\t\t\t\t\t     struct ref_transaction *transaction,\n \t\t\t\t\t\t     struct strbuf *err)\n {\n \tenum ref_transaction_error ret = REF_TRANSACTION_ERROR_GENERIC;\n+\tstruct string_list *updates = &transaction->refnames;\n \tstruct ref_iterator *iter = NULL;\n \tsize_t i;\n \tint ok;\n@@ -1411,6 +1412,13 @@ static enum ref_transaction_error write_with_updates(struct packed_ref_store *re\n \t\t\t\t\t\t    \"reference already exists\",\n \t\t\t\t\t\t    update->refname);\n \t\t\t\t\tret = REF_TRANSACTION_ERROR_CREATE_EXISTS;\n+\n+\t\t\t\t\tif (ref_transaction_maybe_set_rejected(transaction, i, ret)) {\n+\t\t\t\t\t\tstrbuf_setlen(err, 0);\n+\t\t\t\t\t\tret = 0;\n+\t\t\t\t\t\tcontinue;\n+\t\t\t\t\t}\n+\n \t\t\t\t\tgoto error;\n \t\t\t\t} else if (!oideq(&update->old_oid, iter->oid)) {\n \t\t\t\t\tstrbuf_addf(err, \"cannot update ref '%s': \"\n@@ -1419,6 +1427,13 @@ static enum ref_transaction_error write_with_updates(struct packed_ref_store *re\n \t\t\t\t\t\t    oid_to_hex(iter->oid),\n \t\t\t\t\t\t    oid_to_hex(&update->old_oid));\n \t\t\t\t\tret = REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE;\n+\n+\t\t\t\t\tif (ref_transaction_maybe_set_rejected(transaction, i, ret)) {\n+\t\t\t\t\t\tstrbuf_setlen(err, 0);\n+\t\t\t\t\t\tret = 0;\n+\t\t\t\t\t\tcontinue;\n+\t\t\t\t\t}\n+\n \t\t\t\t\tgoto error;\n \t\t\t\t}\n \t\t\t}\n@@ -1456,6 +1471,13 @@ static enum ref_transaction_error write_with_updates(struct packed_ref_store *re\n \t\t\t\t\t    update->refname,\n \t\t\t\t\t    oid_to_hex(&update->old_oid));\n \t\t\t\tret = REF_TRANSACTION_ERROR_NONEXISTENT_REF;\n+\n+\t\t\t\tif (ref_transaction_maybe_set_rejected(transaction, i, ret)) {\n+\t\t\t\t\tstrbuf_setlen(err, 0);\n+\t\t\t\t\tret = 0;\n+\t\t\t\t\tcontinue;\n+\t\t\t\t}\n+\n \t\t\t\tgoto error;\n \t\t\t}\n \t\t}\n@@ -1521,6 +1543,7 @@ static enum ref_transaction_error write_with_updates(struct packed_ref_store *re\n write_error:\n \tstrbuf_addf(err, \"error writing to %s: %s\",\n \t\t    get_tempfile_path(refs->tempfile), strerror(errno));\n+\tret = REF_TRANSACTION_ERROR_GENERIC;\n \n error:\n \tref_iterator_free(iter);\n@@ -1679,7 +1702,7 @@ static int packed_transaction_prepare(struct ref_store *ref_store,\n \t\tdata->own_lock = 1;\n \t}\n \n-\tret = write_with_updates(refs, &transaction->refnames, err);\n+\tret = write_with_updates(refs, transaction, err);\n \tif (ret)\n \t\tgoto failure;\n \ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 3f1d19abd9..73a5379b73 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -123,6 +123,12 @@ struct ref_update {\n \t */\n \tuint64_t index;\n \n+\t/*\n+\t * Used in batched reference updates to mark if a given update\n+\t * was rejected.\n+\t */\n+\tenum ref_transaction_error rejection_err;\n+\n \t/*\n \t * If this ref_update was split off of a symref update via\n \t * split_symref_update(), then this member points at that\n@@ -142,6 +148,13 @@ int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,\n \t\t      struct object_id *oid, struct strbuf *referent,\n \t\t      unsigned int *type, int *failure_errno);\n \n+/*\n+ * Mark a given update as rejected with a given reason.\n+ */\n+int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction,\n+\t\t\t\t       size_t update_idx,\n+\t\t\t\t       enum ref_transaction_error err);\n+\n /*\n  * Add a ref_update with the specified properties to transaction, and\n  * return a pointer to the new object. This function does not verify\n@@ -183,6 +196,18 @@ enum ref_transaction_state {\n \tREF_TRANSACTION_CLOSED   = 2\n };\n \n+/*\n+ * Data structure to hold indices of updates which were rejected, for batched\n+ * reference updates. While the updates themselves hold the rejection error,\n+ * this structure allows a transaction to iterate only over the rejected\n+ * updates.\n+ */\n+struct ref_transaction_rejections {\n+\tsize_t *update_indices;\n+\tsize_t alloc;\n+\tsize_t nr;\n+};\n+\n /*\n  * Data structure for holding a reference transaction, which can\n  * consist of checks and updates to multiple references, carried out\n@@ -195,6 +220,7 @@ struct ref_transaction {\n \tsize_t alloc;\n \tsize_t nr;\n \tenum ref_transaction_state state;\n+\tstruct ref_transaction_rejections *rejections;\n \tvoid *backend_data;\n \tunsigned int flags;\n \tuint64_t max_index;\ndiff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\nindex bd6b042103..ed65225763 100644\n--- a/refs/reftable-backend.c\n+++ b/refs/reftable-backend.c\n@@ -1371,8 +1371,15 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t\t\t\t\t    transaction->updates[i],\n \t\t\t\t\t    &refnames_to_check, head_type,\n \t\t\t\t\t    &head_referent, &referent, err);\n-\t\tif (ret)\n+\t\tif (ret) {\n+\t\t\tif (ref_transaction_maybe_set_rejected(transaction, i, ret)) {\n+\t\t\t\tstrbuf_setlen(err, 0);\n+\t\t\t\tret = 0;\n+\n+\t\t\t\tcontinue;\n+\t\t\t}\n \t\t\tgoto done;\n+\t\t}\n \t}\n \n \tret = refs_verify_refnames_available(ref_store, &refnames_to_check,\n@@ -1454,6 +1461,9 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data\n \t\tstruct reftable_transaction_update *tx_update = &arg->updates[i];\n \t\tstruct ref_update *u = tx_update->update;\n \n+\t\tif (u->rejection_err)\n+\t\t\tcontinue;\n+\n \t\t/*\n \t\t * Write a reflog entry when updating a ref to point to\n \t\t * something new in either of the following cases:\n\n-- \n2.48.1\n\n"},{"id":"514765","messageId":"20250320-245-partially-atomic-ref-updates-v4-5-3dcc1b311dc9@gmail.com","threadId":"63170","inReplyTo":"20250320-245-partially-atomic-ref-updates-v4-0-3dcc1b311dc9@gmail.com","subject":"[PATCH v4 5/8] refs: introduce enum-based transaction error types","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2025-03-20T11:44:00Z","receivedAt":"2025-03-20T11:44:15Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Replace preprocessor-defined transaction errors with a strongly-typed\nenum `ref_transaction_error`. This change:\n\n  - Improves type safety and function signature clarity.\n  - Makes error handling more explicit and discoverable.\n  - Maintains existing error cases, while adding new error cases for\n    common scenarios.\n\nThis refactoring paves the way for more comprehensive error handling\nwhich we will utilize in the upcoming commits to add batch reference\nupdate support.\n\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n builtin/fetch.c         |   2 +-\n refs.c                  |  49 ++++++------\n refs.h                  |  54 ++++++++-----\n refs/files-backend.c    | 202 ++++++++++++++++++++++++------------------------\n refs/packed-backend.c   |  23 +++---\n refs/refs-internal.h    |   5 +-\n refs/reftable-backend.c |  64 +++++++--------\n 7 files changed, 213 insertions(+), 186 deletions(-)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 95fd0018b9..7615c17faf 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -687,7 +687,7 @@ static int s_update_ref(const char *action,\n \t\tswitch (ref_transaction_commit(our_transaction, &err)) {\n \t\tcase 0:\n \t\t\tbreak;\n-\t\tcase TRANSACTION_NAME_CONFLICT:\n+\t\tcase REF_TRANSACTION_ERROR_NAME_CONFLICT:\n \t\t\tret = STORE_REF_ERROR_DF_CONFLICT;\n \t\t\tgoto out;\n \t\tdefault:\ndiff --git a/refs.c b/refs.c\nindex 61bed9672a..3d0b53d56e 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2271,7 +2271,7 @@ int refs_update_symref_extended(struct ref_store *refs, const char *ref,\n \t\t\t\t\t   REF_NO_DEREF, logmsg, &err))\n \t\t\tgoto error_return;\n \t\tprepret = ref_transaction_prepare(transaction, &err);\n-\t\tif (prepret && prepret != TRANSACTION_CREATE_EXISTS)\n+\t\tif (prepret && prepret != REF_TRANSACTION_ERROR_CREATE_EXISTS)\n \t\t\tgoto error_return;\n \t} else {\n \t\tif (ref_transaction_update(transaction, ref, NULL, NULL,\n@@ -2289,7 +2289,7 @@ int refs_update_symref_extended(struct ref_store *refs, const char *ref,\n \t\t}\n \t}\n \n-\tif (prepret == TRANSACTION_CREATE_EXISTS)\n+\tif (prepret == REF_TRANSACTION_ERROR_CREATE_EXISTS)\n \t\tgoto cleanup;\n \n \tif (ref_transaction_commit(transaction, &err))\n@@ -2425,7 +2425,7 @@ int ref_transaction_prepare(struct ref_transaction *transaction,\n \n \tstring_list_sort(&transaction->refnames);\n \tif (ref_update_reject_duplicates(&transaction->refnames, err))\n-\t\treturn TRANSACTION_GENERIC_ERROR;\n+\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \n \tret = refs->be->transaction_prepare(refs, transaction, err);\n \tif (ret)\n@@ -2497,19 +2497,19 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \treturn ret;\n }\n \n-int refs_verify_refnames_available(struct ref_store *refs,\n-\t\t\t\t   const struct string_list *refnames,\n-\t\t\t\t   const struct string_list *extras,\n-\t\t\t\t   const struct string_list *skip,\n-\t\t\t\t   unsigned int initial_transaction,\n-\t\t\t\t   struct strbuf *err)\n+enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs,\n+\t\t\t\t\t  const struct string_list *refnames,\n+\t\t\t\t\t  const struct string_list *extras,\n+\t\t\t\t\t  const struct string_list *skip,\n+\t\t\t\t\t  unsigned int initial_transaction,\n+\t\t\t\t\t  struct strbuf *err)\n {\n \tstruct strbuf dirname = STRBUF_INIT;\n \tstruct strbuf referent = STRBUF_INIT;\n \tstruct string_list_item *item;\n \tstruct ref_iterator *iter = NULL;\n \tstruct strset dirnames;\n-\tint ret = -1;\n+\tint ret = REF_TRANSACTION_ERROR_NAME_CONFLICT;\n \n \t/*\n \t * For the sake of comments in this function, suppose that\n@@ -2625,12 +2625,13 @@ int refs_verify_refnames_available(struct ref_store *refs,\n \treturn ret;\n }\n \n-int refs_verify_refname_available(struct ref_store *refs,\n-\t\t\t\t  const char *refname,\n-\t\t\t\t  const struct string_list *extras,\n-\t\t\t\t  const struct string_list *skip,\n-\t\t\t\t  unsigned int initial_transaction,\n-\t\t\t\t  struct strbuf *err)\n+enum ref_transaction_error refs_verify_refname_available(\n+\tstruct ref_store *refs,\n+\tconst char *refname,\n+\tconst struct string_list *extras,\n+\tconst struct string_list *skip,\n+\tunsigned int initial_transaction,\n+\tstruct strbuf *err)\n {\n \tstruct string_list_item item = { .string = (char *) refname };\n \tstruct string_list refnames = {\n@@ -2818,8 +2819,9 @@ int ref_update_has_null_new_value(struct ref_update *update)\n \treturn !update->new_target && is_null_oid(&update->new_oid);\n }\n \n-int ref_update_check_old_target(const char *referent, struct ref_update *update,\n-\t\t\t\tstruct strbuf *err)\n+enum ref_transaction_error ref_update_check_old_target(const char *referent,\n+\t\t\t\t\t\t       struct ref_update *update,\n+\t\t\t\t\t\t       struct strbuf *err)\n {\n \tif (!update->old_target)\n \t\tBUG(\"called without old_target set\");\n@@ -2827,17 +2829,18 @@ int ref_update_check_old_target(const char *referent, struct ref_update *update,\n \tif (!strcmp(referent, update->old_target))\n \t\treturn 0;\n \n-\tif (!strcmp(referent, \"\"))\n+\tif (!strcmp(referent, \"\")) {\n \t\tstrbuf_addf(err, \"verifying symref target: '%s': \"\n \t\t\t    \"reference is missing but expected %s\",\n \t\t\t    ref_update_original_update_refname(update),\n \t\t\t    update->old_target);\n-\telse\n-\t\tstrbuf_addf(err, \"verifying symref target: '%s': \"\n-\t\t\t    \"is at %s but expected %s\",\n+\t\treturn REF_TRANSACTION_ERROR_NONEXISTENT_REF;\n+\t}\n+\n+\tstrbuf_addf(err, \"verifying symref target: '%s': is at %s but expected %s\",\n \t\t\t    ref_update_original_update_refname(update),\n \t\t\t    referent, update->old_target);\n-\treturn -1;\n+\treturn REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE;\n }\n \n struct migration_data {\ndiff --git a/refs.h b/refs.h\nindex 240e2d8537..dcd83e81e2 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -16,6 +16,29 @@ struct worktree;\n enum ref_storage_format ref_storage_format_by_name(const char *name);\n const char *ref_storage_format_to_name(enum ref_storage_format ref_storage_format);\n \n+/*\n+ * enum ref_transaction_error represents the following return codes:\n+ * REF_TRANSACTION_ERROR_GENERIC error_code: default error code.\n+ * REF_TRANSACTION_ERROR_NAME_CONFLICT error_code: ref name conflict like A vs A/B.\n+ * REF_TRANSACTION_ERROR_CREATE_EXISTS error_code: ref to be created already exists.\n+ * REF_TRANSACTION_ERROR_NONEXISTENT_REF error_code: ref expected but doesn't exist.\n+ * REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE error_code: provided old_oid or old_target of\n+ * reference doesn't match actual.\n+ * REF_TRANSACTION_ERROR_INVALID_NEW_VALUE error_code: provided new_oid or new_target is\n+ * invalid.\n+ * REF_TRANSACTION_ERROR_EXPECTED_SYMREF error_code: expected ref to be symref, but is a\n+ * regular ref.\n+ */\n+enum ref_transaction_error {\n+\tREF_TRANSACTION_ERROR_GENERIC = -1,\n+\tREF_TRANSACTION_ERROR_NAME_CONFLICT = -2,\n+\tREF_TRANSACTION_ERROR_CREATE_EXISTS = -3,\n+\tREF_TRANSACTION_ERROR_NONEXISTENT_REF = -4,\n+\tREF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE = -5,\n+\tREF_TRANSACTION_ERROR_INVALID_NEW_VALUE = -6,\n+\tREF_TRANSACTION_ERROR_EXPECTED_SYMREF = -7,\n+};\n+\n /*\n  * Resolve a reference, recursively following symbolic references.\n  *\n@@ -117,24 +140,24 @@ int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,\n  *\n  * extras and skip must be sorted.\n  */\n-int refs_verify_refname_available(struct ref_store *refs,\n-\t\t\t\t  const char *refname,\n-\t\t\t\t  const struct string_list *extras,\n-\t\t\t\t  const struct string_list *skip,\n-\t\t\t\t  unsigned int initial_transaction,\n-\t\t\t\t  struct strbuf *err);\n+enum ref_transaction_error refs_verify_refname_available(struct ref_store *refs,\n+\t\t\t\t\t\t const char *refname,\n+\t\t\t\t\t\t const struct string_list *extras,\n+\t\t\t\t\t\t const struct string_list *skip,\n+\t\t\t\t\t\t unsigned int initial_transaction,\n+\t\t\t\t\t\t struct strbuf *err);\n \n /*\n  * Same as `refs_verify_refname_available()`, but checking for a list of\n  * refnames instead of only a single item. This is more efficient in the case\n  * where one needs to check multiple refnames.\n  */\n-int refs_verify_refnames_available(struct ref_store *refs,\n-\t\t\t\t   const struct string_list *refnames,\n-\t\t\t\t   const struct string_list *extras,\n-\t\t\t\t   const struct string_list *skip,\n-\t\t\t\t   unsigned int initial_transaction,\n-\t\t\t\t   struct strbuf *err);\n+enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs,\n+\t\t\t\t\t  const struct string_list *refnames,\n+\t\t\t\t\t  const struct string_list *extras,\n+\t\t\t\t\t  const struct string_list *skip,\n+\t\t\t\t\t  unsigned int initial_transaction,\n+\t\t\t\t\t  struct strbuf *err);\n \n int refs_ref_exists(struct ref_store *refs, const char *refname);\n \n@@ -830,13 +853,6 @@ int ref_transaction_verify(struct ref_transaction *transaction,\n \t\t\t   unsigned int flags,\n \t\t\t   struct strbuf *err);\n \n-/* Naming conflict (for example, the ref names A and A/B conflict). */\n-#define TRANSACTION_NAME_CONFLICT -1\n-/* When only creation was requested, but the ref already exists. */\n-#define TRANSACTION_CREATE_EXISTS -2\n-/* All other errors. */\n-#define TRANSACTION_GENERIC_ERROR -3\n-\n /*\n  * Perform the preparatory stages of committing `transaction`. Acquire\n  * any needed locks, check preconditions, etc.; basically, do as much\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex ea023a59fc..4f27f7652c 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -663,7 +663,7 @@ static void unlock_ref(struct ref_lock *lock)\n  * broken, lock the reference anyway but clear old_oid.\n  *\n  * Return 0 on success. On failure, write an error message to err and\n- * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR.\n+ * return REF_TRANSACTION_ERROR_NAME_CONFLICT or REF_TRANSACTION_ERROR_GENERIC.\n  *\n  * Implementation note: This function is basically\n  *\n@@ -676,19 +676,20 @@ static void unlock_ref(struct ref_lock *lock)\n  *   avoided, namely if we were successfully able to read the ref\n  * - Generate informative error messages in the case of failure\n  */\n-static int lock_raw_ref(struct files_ref_store *refs,\n-\t\t\tconst char *refname, int mustexist,\n-\t\t\tstruct string_list *refnames_to_check,\n-\t\t\tconst struct string_list *extras,\n-\t\t\tstruct ref_lock **lock_p,\n-\t\t\tstruct strbuf *referent,\n-\t\t\tunsigned int *type,\n-\t\t\tstruct strbuf *err)\n-{\n+static enum ref_transaction_error lock_raw_ref(struct files_ref_store *refs,\n+\t\t\t\t\t       const char *refname,\n+\t\t\t\t\t       int mustexist,\n+\t\t\t\t\t       struct string_list *refnames_to_check,\n+\t\t\t\t\t       const struct string_list *extras,\n+\t\t\t\t\t       struct ref_lock **lock_p,\n+\t\t\t\t\t       struct strbuf *referent,\n+\t\t\t\t\t       unsigned int *type,\n+\t\t\t\t\t       struct strbuf *err)\n+{\n+\tenum ref_transaction_error ret = REF_TRANSACTION_ERROR_GENERIC;\n \tstruct ref_lock *lock;\n \tstruct strbuf ref_file = STRBUF_INIT;\n \tint attempts_remaining = 3;\n-\tint ret = TRANSACTION_GENERIC_ERROR;\n \tint failure_errno;\n \n \tassert(err);\n@@ -728,13 +729,14 @@ static int lock_raw_ref(struct files_ref_store *refs,\n \t\t\t\tstrbuf_reset(err);\n \t\t\t\tstrbuf_addf(err, \"unable to resolve reference '%s'\",\n \t\t\t\t\t    refname);\n+\t\t\t\tret = REF_TRANSACTION_ERROR_NONEXISTENT_REF;\n \t\t\t} else {\n \t\t\t\t/*\n \t\t\t\t * The error message set by\n \t\t\t\t * refs_verify_refname_available() is\n \t\t\t\t * OK.\n \t\t\t\t */\n-\t\t\t\tret = TRANSACTION_NAME_CONFLICT;\n+\t\t\t\tret = REF_TRANSACTION_ERROR_NAME_CONFLICT;\n \t\t\t}\n \t\t} else {\n \t\t\t/*\n@@ -788,6 +790,7 @@ static int lock_raw_ref(struct files_ref_store *refs,\n \t\t\t\t/* Garden variety missing reference. */\n \t\t\t\tstrbuf_addf(err, \"unable to resolve reference '%s'\",\n \t\t\t\t\t    refname);\n+\t\t\t\tret = REF_TRANSACTION_ERROR_NONEXISTENT_REF;\n \t\t\t\tgoto error_return;\n \t\t\t} else {\n \t\t\t\t/*\n@@ -820,6 +823,7 @@ static int lock_raw_ref(struct files_ref_store *refs,\n \t\t\t\t/* Garden variety missing reference. */\n \t\t\t\tstrbuf_addf(err, \"unable to resolve reference '%s'\",\n \t\t\t\t\t    refname);\n+\t\t\t\tret = REF_TRANSACTION_ERROR_NONEXISTENT_REF;\n \t\t\t\tgoto error_return;\n \t\t\t} else if (remove_dir_recursively(&ref_file,\n \t\t\t\t\t\t\t  REMOVE_DIR_EMPTY_ONLY)) {\n@@ -830,7 +834,7 @@ static int lock_raw_ref(struct files_ref_store *refs,\n \t\t\t\t\t * The error message set by\n \t\t\t\t\t * verify_refname_available() is OK.\n \t\t\t\t\t */\n-\t\t\t\t\tret = TRANSACTION_NAME_CONFLICT;\n+\t\t\t\t\tret = REF_TRANSACTION_ERROR_NAME_CONFLICT;\n \t\t\t\t\tgoto error_return;\n \t\t\t\t} else {\n \t\t\t\t\t/*\n@@ -1517,10 +1521,11 @@ static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)\n \treturn ret;\n }\n \n-static int write_ref_to_lockfile(struct files_ref_store *refs,\n-\t\t\t\t struct ref_lock *lock,\n-\t\t\t\t const struct object_id *oid,\n-\t\t\t\t int skip_oid_verification, struct strbuf *err);\n+static enum ref_transaction_error write_ref_to_lockfile(struct files_ref_store *refs,\n+\t\t\t\t\t\t\tstruct ref_lock *lock,\n+\t\t\t\t\t\t\tconst struct object_id *oid,\n+\t\t\t\t\t\t\tint skip_oid_verification,\n+\t\t\t\t\t\t\tstruct strbuf *err);\n static int commit_ref_update(struct files_ref_store *refs,\n \t\t\t     struct ref_lock *lock,\n \t\t\t     const struct object_id *oid, const char *logmsg,\n@@ -1926,10 +1931,11 @@ static int files_log_ref_write(struct files_ref_store *refs,\n  * Write oid into the open lockfile, then close the lockfile. On\n  * errors, rollback the lockfile, fill in *err and return -1.\n  */\n-static int write_ref_to_lockfile(struct files_ref_store *refs,\n-\t\t\t\t struct ref_lock *lock,\n-\t\t\t\t const struct object_id *oid,\n-\t\t\t\t int skip_oid_verification, struct strbuf *err)\n+static enum ref_transaction_error write_ref_to_lockfile(struct files_ref_store *refs,\n+\t\t\t\t\t\t\tstruct ref_lock *lock,\n+\t\t\t\t\t\t\tconst struct object_id *oid,\n+\t\t\t\t\t\t\tint skip_oid_verification,\n+\t\t\t\t\t\t\tstruct strbuf *err)\n {\n \tstatic char term = '\\n';\n \tstruct object *o;\n@@ -1943,7 +1949,7 @@ static int write_ref_to_lockfile(struct files_ref_store *refs,\n \t\t\t\t\"trying to write ref '%s' with nonexistent object %s\",\n \t\t\t\tlock->ref_name, oid_to_hex(oid));\n \t\t\tunlock_ref(lock);\n-\t\t\treturn -1;\n+\t\t\treturn REF_TRANSACTION_ERROR_INVALID_NEW_VALUE;\n \t\t}\n \t\tif (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {\n \t\t\tstrbuf_addf(\n@@ -1951,7 +1957,7 @@ static int write_ref_to_lockfile(struct files_ref_store *refs,\n \t\t\t\t\"trying to write non-commit object %s to branch '%s'\",\n \t\t\t\toid_to_hex(oid), lock->ref_name);\n \t\t\tunlock_ref(lock);\n-\t\t\treturn -1;\n+\t\t\treturn REF_TRANSACTION_ERROR_INVALID_NEW_VALUE;\n \t\t}\n \t}\n \tfd = get_lock_file_fd(&lock->lk);\n@@ -1962,7 +1968,7 @@ static int write_ref_to_lockfile(struct files_ref_store *refs,\n \t\tstrbuf_addf(err,\n \t\t\t    \"couldn't write '%s'\", get_lock_file_path(&lock->lk));\n \t\tunlock_ref(lock);\n-\t\treturn -1;\n+\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \t}\n \treturn 0;\n }\n@@ -2376,9 +2382,10 @@ static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_st\n  * If update is a direct update of head_ref (the reference pointed to\n  * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.\n  */\n-static int split_head_update(struct ref_update *update,\n-\t\t\t     struct ref_transaction *transaction,\n-\t\t\t     const char *head_ref, struct strbuf *err)\n+static enum ref_transaction_error split_head_update(struct ref_update *update,\n+\t\t\t\t\t\t    struct ref_transaction *transaction,\n+\t\t\t\t\t\t    const char *head_ref,\n+\t\t\t\t\t\t    struct strbuf *err)\n {\n \tstruct ref_update *new_update;\n \n@@ -2402,7 +2409,7 @@ static int split_head_update(struct ref_update *update,\n \t\t\t    \"multiple updates for 'HEAD' (including one \"\n \t\t\t    \"via its referent '%s') are not allowed\",\n \t\t\t    update->refname);\n-\t\treturn TRANSACTION_NAME_CONFLICT;\n+\t\treturn REF_TRANSACTION_ERROR_NAME_CONFLICT;\n \t}\n \n \tnew_update = ref_transaction_add_update(\n@@ -2430,10 +2437,10 @@ static int split_head_update(struct ref_update *update,\n  * Note that the new update will itself be subject to splitting when\n  * the iteration gets to it.\n  */\n-static int split_symref_update(struct ref_update *update,\n-\t\t\t       const char *referent,\n-\t\t\t       struct ref_transaction *transaction,\n-\t\t\t       struct strbuf *err)\n+static enum ref_transaction_error split_symref_update(struct ref_update *update,\n+\t\t\t\t\t\t      const char *referent,\n+\t\t\t\t\t\t      struct ref_transaction *transaction,\n+\t\t\t\t\t\t      struct strbuf *err)\n {\n \tstruct ref_update *new_update;\n \tunsigned int new_flags;\n@@ -2450,7 +2457,7 @@ static int split_symref_update(struct ref_update *update,\n \t\t\t    \"multiple updates for '%s' (including one \"\n \t\t\t    \"via symref '%s') are not allowed\",\n \t\t\t    referent, update->refname);\n-\t\treturn TRANSACTION_NAME_CONFLICT;\n+\t\treturn REF_TRANSACTION_ERROR_NAME_CONFLICT;\n \t}\n \n \tnew_flags = update->flags;\n@@ -2491,11 +2498,10 @@ static int split_symref_update(struct ref_update *update,\n  * everything is OK, return 0; otherwise, write an error message to\n  * err and return -1.\n  */\n-static int check_old_oid(struct ref_update *update, struct object_id *oid,\n-\t\t\t struct strbuf *err)\n+static enum ref_transaction_error check_old_oid(struct ref_update *update,\n+\t\t\t\t\t\tstruct object_id *oid,\n+\t\t\t\t\t\tstruct strbuf *err)\n {\n-\tint ret = TRANSACTION_GENERIC_ERROR;\n-\n \tif (!(update->flags & REF_HAVE_OLD) ||\n \t\t   oideq(oid, &update->old_oid))\n \t\treturn 0;\n@@ -2504,21 +2510,20 @@ static int check_old_oid(struct ref_update *update, struct object_id *oid,\n \t\tstrbuf_addf(err, \"cannot lock ref '%s': \"\n \t\t\t    \"reference already exists\",\n \t\t\t    ref_update_original_update_refname(update));\n-\t\tret = TRANSACTION_CREATE_EXISTS;\n-\t}\n-\telse if (is_null_oid(oid))\n+\t\treturn REF_TRANSACTION_ERROR_CREATE_EXISTS;\n+\t} else if (is_null_oid(oid)) {\n \t\tstrbuf_addf(err, \"cannot lock ref '%s': \"\n \t\t\t    \"reference is missing but expected %s\",\n \t\t\t    ref_update_original_update_refname(update),\n \t\t\t    oid_to_hex(&update->old_oid));\n-\telse\n-\t\tstrbuf_addf(err, \"cannot lock ref '%s': \"\n-\t\t\t    \"is at %s but expected %s\",\n-\t\t\t    ref_update_original_update_refname(update),\n-\t\t\t    oid_to_hex(oid),\n-\t\t\t    oid_to_hex(&update->old_oid));\n+\t\treturn REF_TRANSACTION_ERROR_NONEXISTENT_REF;\n+\t}\n \n-\treturn ret;\n+\tstrbuf_addf(err, \"cannot lock ref '%s': is at %s but expected %s\",\n+\t\t    ref_update_original_update_refname(update), oid_to_hex(oid),\n+\t\t    oid_to_hex(&update->old_oid));\n+\n+\treturn REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE;\n }\n \n struct files_transaction_backend_data {\n@@ -2540,17 +2545,17 @@ struct files_transaction_backend_data {\n  * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY\n  *   update of HEAD.\n  */\n-static int lock_ref_for_update(struct files_ref_store *refs,\n-\t\t\t       struct ref_update *update,\n-\t\t\t       struct ref_transaction *transaction,\n-\t\t\t       const char *head_ref,\n-\t\t\t       struct string_list *refnames_to_check,\n-\t\t\t       struct strbuf *err)\n+static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *refs,\n+\t\t\t\t\t\t      struct ref_update *update,\n+\t\t\t\t\t\t      struct ref_transaction *transaction,\n+\t\t\t\t\t\t      const char *head_ref,\n+\t\t\t\t\t\t      struct string_list *refnames_to_check,\n+\t\t\t\t\t\t      struct strbuf *err)\n {\n \tstruct strbuf referent = STRBUF_INIT;\n \tint mustexist = ref_update_expects_existing_old_ref(update);\n \tstruct files_transaction_backend_data *backend_data;\n-\tint ret = 0;\n+\tenum ref_transaction_error ret = 0;\n \tstruct ref_lock *lock;\n \n \tfiles_assert_main_repository(refs, \"lock_ref_for_update\");\n@@ -2602,22 +2607,17 @@ static int lock_ref_for_update(struct files_ref_store *refs,\n \t\t\t\t\tstrbuf_addf(err, \"cannot lock ref '%s': \"\n \t\t\t\t\t\t    \"error reading reference\",\n \t\t\t\t\t\t    ref_update_original_update_refname(update));\n-\t\t\t\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\t\t\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n \t\t\t\t\tgoto out;\n \t\t\t\t}\n \t\t\t}\n \n-\t\t\tif (update->old_target) {\n-\t\t\t\tif (ref_update_check_old_target(referent.buf, update, err)) {\n-\t\t\t\t\tret = TRANSACTION_GENERIC_ERROR;\n-\t\t\t\t\tgoto out;\n-\t\t\t\t}\n-\t\t\t} else {\n+\t\t\tif (update->old_target)\n+\t\t\t\tret = ref_update_check_old_target(referent.buf, update, err);\n+\t\t\telse\n \t\t\t\tret = check_old_oid(update, &lock->old_oid, err);\n-\t\t\t\tif  (ret) {\n-\t\t\t\t\tgoto out;\n-\t\t\t\t}\n-\t\t\t}\n+\t\t\tif (ret)\n+\t\t\t\tgoto out;\n \t\t} else {\n \t\t\t/*\n \t\t\t * Create a new update for the reference this\n@@ -2644,7 +2644,7 @@ static int lock_ref_for_update(struct files_ref_store *refs,\n \t\t\t\t\t   \"but is a regular ref\"),\n \t\t\t\t    ref_update_original_update_refname(update),\n \t\t\t\t    update->old_target);\n-\t\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\t\tret = REF_TRANSACTION_ERROR_EXPECTED_SYMREF;\n \t\t\tgoto out;\n \t\t} else {\n \t\t\tret = check_old_oid(update, &lock->old_oid, err);\n@@ -2668,14 +2668,14 @@ static int lock_ref_for_update(struct files_ref_store *refs,\n \n \tif (update->new_target && !(update->flags & REF_LOG_ONLY)) {\n \t\tif (create_symref_lock(lock, update->new_target, err)) {\n-\t\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n \t\t\tgoto out;\n \t\t}\n \n \t\tif (close_ref_gently(lock)) {\n \t\t\tstrbuf_addf(err, \"couldn't close '%s.lock'\",\n \t\t\t\t    update->refname);\n-\t\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n \t\t\tgoto out;\n \t\t}\n \n@@ -2693,25 +2693,27 @@ static int lock_ref_for_update(struct files_ref_store *refs,\n \t\t\t * The reference already has the desired\n \t\t\t * value, so we don't need to write it.\n \t\t\t */\n-\t\t} else if (write_ref_to_lockfile(\n-\t\t\t\t   refs, lock, &update->new_oid,\n-\t\t\t\t   update->flags & REF_SKIP_OID_VERIFICATION,\n-\t\t\t\t   err)) {\n-\t\t\tchar *write_err = strbuf_detach(err, NULL);\n-\n-\t\t\t/*\n-\t\t\t * The lock was freed upon failure of\n-\t\t\t * write_ref_to_lockfile():\n-\t\t\t */\n-\t\t\tupdate->backend_data = NULL;\n-\t\t\tstrbuf_addf(err,\n-\t\t\t\t    \"cannot update ref '%s': %s\",\n-\t\t\t\t    update->refname, write_err);\n-\t\t\tfree(write_err);\n-\t\t\tret = TRANSACTION_GENERIC_ERROR;\n-\t\t\tgoto out;\n \t\t} else {\n-\t\t\tupdate->flags |= REF_NEEDS_COMMIT;\n+\t\t\tret = write_ref_to_lockfile(\n+\t\t\t\trefs, lock, &update->new_oid,\n+\t\t\t\tupdate->flags & REF_SKIP_OID_VERIFICATION,\n+\t\t\t\terr);\n+\t\t\tif (ret) {\n+\t\t\t\tchar *write_err = strbuf_detach(err, NULL);\n+\n+\t\t\t\t/*\n+\t\t\t\t * The lock was freed upon failure of\n+\t\t\t\t * write_ref_to_lockfile():\n+\t\t\t\t */\n+\t\t\t\tupdate->backend_data = NULL;\n+\t\t\t\tstrbuf_addf(err,\n+\t\t\t\t\t    \"cannot update ref '%s': %s\",\n+\t\t\t\t\t    update->refname, write_err);\n+\t\t\t\tfree(write_err);\n+\t\t\t\tgoto out;\n+\t\t\t} else {\n+\t\t\t\tupdate->flags |= REF_NEEDS_COMMIT;\n+\t\t\t}\n \t\t}\n \t}\n \tif (!(update->flags & REF_NEEDS_COMMIT)) {\n@@ -2723,7 +2725,7 @@ static int lock_ref_for_update(struct files_ref_store *refs,\n \t\tif (close_ref_gently(lock)) {\n \t\t\tstrbuf_addf(err, \"couldn't close '%s.lock'\",\n \t\t\t\t    update->refname);\n-\t\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n \t\t\tgoto out;\n \t\t}\n \t}\n@@ -2865,7 +2867,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \t\t\t\t\t\trefs->packed_ref_store,\n \t\t\t\t\t\ttransaction->flags, err);\n \t\t\t\tif (!packed_transaction) {\n-\t\t\t\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\t\t\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n \t\t\t\t\tgoto cleanup;\n \t\t\t\t}\n \n@@ -2897,13 +2899,13 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \t */\n \tif (refs_verify_refnames_available(refs->packed_ref_store, &refnames_to_check,\n \t\t\t\t\t   &transaction->refnames, NULL, 0, err)) {\n-\t\tret = TRANSACTION_NAME_CONFLICT;\n+\t\tret = REF_TRANSACTION_ERROR_NAME_CONFLICT;\n \t\tgoto cleanup;\n \t}\n \n \tif (packed_transaction) {\n \t\tif (packed_refs_lock(refs->packed_ref_store, 0, err)) {\n-\t\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n \t\t\tgoto cleanup;\n \t\t}\n \t\tbackend_data->packed_refs_locked = 1;\n@@ -2934,7 +2936,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \t\t\t */\n \t\t\tbackend_data->packed_transaction = NULL;\n \t\t\tif (ref_transaction_abort(packed_transaction, err)) {\n-\t\t\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\t\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n \t\t\t\tgoto cleanup;\n \t\t\t}\n \t\t}\n@@ -3035,7 +3037,7 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,\n \tpacked_transaction = ref_store_transaction_begin(refs->packed_ref_store,\n \t\t\t\t\t\t\t transaction->flags, err);\n \tif (!packed_transaction) {\n-\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n \t\tgoto cleanup;\n \t}\n \n@@ -3058,7 +3060,7 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,\n \t\t\tif (!loose_transaction) {\n \t\t\t\tloose_transaction = ref_store_transaction_begin(&refs->base, 0, err);\n \t\t\t\tif (!loose_transaction) {\n-\t\t\t\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\t\t\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n \t\t\t\t\tgoto cleanup;\n \t\t\t\t}\n \t\t\t}\n@@ -3083,19 +3085,19 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,\n \t}\n \n \tif (packed_refs_lock(refs->packed_ref_store, 0, err)) {\n-\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n \t\tgoto cleanup;\n \t}\n \n \tif (refs_verify_refnames_available(&refs->base, &refnames_to_check,\n \t\t\t\t\t   &affected_refnames, NULL, 1, err)) {\n \t\tpacked_refs_unlock(refs->packed_ref_store);\n-\t\tret = TRANSACTION_NAME_CONFLICT;\n+\t\tret = REF_TRANSACTION_ERROR_NAME_CONFLICT;\n \t\tgoto cleanup;\n \t}\n \n \tif (ref_transaction_commit(packed_transaction, err)) {\n-\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n \t\tgoto cleanup;\n \t}\n \tpacked_refs_unlock(refs->packed_ref_store);\n@@ -3103,7 +3105,7 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,\n \tif (loose_transaction) {\n \t\tif (ref_transaction_prepare(loose_transaction, err) ||\n \t\t    ref_transaction_commit(loose_transaction, err)) {\n-\t\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n \t\t\tgoto cleanup;\n \t\t}\n \t}\n@@ -3152,7 +3154,7 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \t\tif (update->flags & REF_NEEDS_COMMIT ||\n \t\t    update->flags & REF_LOG_ONLY) {\n \t\t\tif (parse_and_write_reflog(refs, update, lock, err)) {\n-\t\t\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\t\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n \t\t\t\tgoto cleanup;\n \t\t\t}\n \t\t}\n@@ -3171,7 +3173,7 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \t\t\t\tstrbuf_addf(err, \"couldn't set '%s'\", lock->ref_name);\n \t\t\t\tunlock_ref(lock);\n \t\t\t\tupdate->backend_data = NULL;\n-\t\t\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\t\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n \t\t\t\tgoto cleanup;\n \t\t\t}\n \t\t}\n@@ -3227,7 +3229,7 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \t\t\t\tstrbuf_reset(&sb);\n \t\t\t\tfiles_ref_path(refs, &sb, lock->ref_name);\n \t\t\t\tif (unlink_or_msg(sb.buf, err)) {\n-\t\t\t\t\tret = TRANSACTION_GENERIC_ERROR;\n+\t\t\t\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n \t\t\t\t\tgoto cleanup;\n \t\t\t\t}\n \t\t\t}\ndiff --git a/refs/packed-backend.c b/refs/packed-backend.c\nindex 19220d2e99..d90bd815a3 100644\n--- a/refs/packed-backend.c\n+++ b/refs/packed-backend.c\n@@ -1326,10 +1326,11 @@ static int packed_ref_store_remove_on_disk(struct ref_store *ref_store,\n  * The packfile must be locked before calling this function and will\n  * remain locked when it is done.\n  */\n-static int write_with_updates(struct packed_ref_store *refs,\n-\t\t\t      struct string_list *updates,\n-\t\t\t      struct strbuf *err)\n+static enum ref_transaction_error write_with_updates(struct packed_ref_store *refs,\n+\t\t\t\t\t\t     struct string_list *updates,\n+\t\t\t\t\t\t     struct strbuf *err)\n {\n+\tenum ref_transaction_error ret = REF_TRANSACTION_ERROR_GENERIC;\n \tstruct ref_iterator *iter = NULL;\n \tsize_t i;\n \tint ok;\n@@ -1353,7 +1354,7 @@ static int write_with_updates(struct packed_ref_store *refs,\n \t\tstrbuf_addf(err, \"unable to create file %s: %s\",\n \t\t\t    sb.buf, strerror(errno));\n \t\tstrbuf_release(&sb);\n-\t\treturn -1;\n+\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \t}\n \tstrbuf_release(&sb);\n \n@@ -1409,6 +1410,7 @@ static int write_with_updates(struct packed_ref_store *refs,\n \t\t\t\t\tstrbuf_addf(err, \"cannot update ref '%s': \"\n \t\t\t\t\t\t    \"reference already exists\",\n \t\t\t\t\t\t    update->refname);\n+\t\t\t\t\tret = REF_TRANSACTION_ERROR_CREATE_EXISTS;\n \t\t\t\t\tgoto error;\n \t\t\t\t} else if (!oideq(&update->old_oid, iter->oid)) {\n \t\t\t\t\tstrbuf_addf(err, \"cannot update ref '%s': \"\n@@ -1416,6 +1418,7 @@ static int write_with_updates(struct packed_ref_store *refs,\n \t\t\t\t\t\t    update->refname,\n \t\t\t\t\t\t    oid_to_hex(iter->oid),\n \t\t\t\t\t\t    oid_to_hex(&update->old_oid));\n+\t\t\t\t\tret = REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE;\n \t\t\t\t\tgoto error;\n \t\t\t\t}\n \t\t\t}\n@@ -1452,6 +1455,7 @@ static int write_with_updates(struct packed_ref_store *refs,\n \t\t\t\t\t    \"reference is missing but expected %s\",\n \t\t\t\t\t    update->refname,\n \t\t\t\t\t    oid_to_hex(&update->old_oid));\n+\t\t\t\tret = REF_TRANSACTION_ERROR_NONEXISTENT_REF;\n \t\t\t\tgoto error;\n \t\t\t}\n \t\t}\n@@ -1509,7 +1513,7 @@ static int write_with_updates(struct packed_ref_store *refs,\n \t\t\t    strerror(errno));\n \t\tstrbuf_release(&sb);\n \t\tdelete_tempfile(&refs->tempfile);\n-\t\treturn -1;\n+\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \t}\n \n \treturn 0;\n@@ -1521,7 +1525,7 @@ static int write_with_updates(struct packed_ref_store *refs,\n error:\n \tref_iterator_free(iter);\n \tdelete_tempfile(&refs->tempfile);\n-\treturn -1;\n+\treturn ret;\n }\n \n int is_packed_transaction_needed(struct ref_store *ref_store,\n@@ -1654,7 +1658,7 @@ static int packed_transaction_prepare(struct ref_store *ref_store,\n \t\t\tREF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,\n \t\t\t\"ref_transaction_prepare\");\n \tstruct packed_transaction_backend_data *data;\n-\tint ret = TRANSACTION_GENERIC_ERROR;\n+\tenum ref_transaction_error ret = REF_TRANSACTION_ERROR_GENERIC;\n \n \t/*\n \t * Note that we *don't* skip transactions with zero updates,\n@@ -1675,7 +1679,8 @@ static int packed_transaction_prepare(struct ref_store *ref_store,\n \t\tdata->own_lock = 1;\n \t}\n \n-\tif (write_with_updates(refs, &transaction->refnames, err))\n+\tret = write_with_updates(refs, &transaction->refnames, err);\n+\tif (ret)\n \t\tgoto failure;\n \n \ttransaction->state = REF_TRANSACTION_PREPARED;\n@@ -1707,7 +1712,7 @@ static int packed_transaction_finish(struct ref_store *ref_store,\n \t\t\tref_store,\n \t\t\tREF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,\n \t\t\t\"ref_transaction_finish\");\n-\tint ret = TRANSACTION_GENERIC_ERROR;\n+\tint ret = REF_TRANSACTION_ERROR_GENERIC;\n \tchar *packed_refs_path;\n \n \tclear_snapshot(refs);\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 6d3770d0cc..3f1d19abd9 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -770,8 +770,9 @@ int ref_update_has_null_new_value(struct ref_update *update);\n  * If everything is OK, return 0; otherwise, write an error message to\n  * err and return -1.\n  */\n-int ref_update_check_old_target(const char *referent, struct ref_update *update,\n-\t\t\t\tstruct strbuf *err);\n+enum ref_transaction_error ref_update_check_old_target(const char *referent,\n+\t\t\t\t\t\t       struct ref_update *update,\n+\t\t\t\t\t\t       struct strbuf *err);\n \n /*\n  * Check if the ref must exist, this means that the old_oid or\ndiff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\nindex 786df11a03..bd6b042103 100644\n--- a/refs/reftable-backend.c\n+++ b/refs/reftable-backend.c\n@@ -1069,20 +1069,20 @@ static int queue_transaction_update(struct reftable_ref_store *refs,\n \treturn 0;\n }\n \n-static int prepare_single_update(struct reftable_ref_store *refs,\n-\t\t\t\t struct reftable_transaction_data *tx_data,\n-\t\t\t\t struct ref_transaction *transaction,\n-\t\t\t\t struct reftable_backend *be,\n-\t\t\t\t struct ref_update *u,\n-\t\t\t\t struct string_list *refnames_to_check,\n-\t\t\t\t unsigned int head_type,\n-\t\t\t\t struct strbuf *head_referent,\n-\t\t\t\t struct strbuf *referent,\n-\t\t\t\t struct strbuf *err)\n+static enum ref_transaction_error prepare_single_update(struct reftable_ref_store *refs,\n+\t\t\t\t\t\t\tstruct reftable_transaction_data *tx_data,\n+\t\t\t\t\t\t\tstruct ref_transaction *transaction,\n+\t\t\t\t\t\t\tstruct reftable_backend *be,\n+\t\t\t\t\t\t\tstruct ref_update *u,\n+\t\t\t\t\t\t\tstruct string_list *refnames_to_check,\n+\t\t\t\t\t\t\tunsigned int head_type,\n+\t\t\t\t\t\t\tstruct strbuf *head_referent,\n+\t\t\t\t\t\t\tstruct strbuf *referent,\n+\t\t\t\t\t\t\tstruct strbuf *err)\n {\n+\tenum ref_transaction_error ret = 0;\n \tstruct object_id current_oid = {0};\n \tconst char *rewritten_ref;\n-\tint ret = 0;\n \n \t/*\n \t * There is no need to reload the respective backends here as\n@@ -1093,7 +1093,7 @@ static int prepare_single_update(struct reftable_ref_store *refs,\n \t */\n \tret = backend_for(&be, refs, u->refname, &rewritten_ref, 0);\n \tif (ret)\n-\t\treturn ret;\n+\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \n \t/* Verify that the new object ID is valid. */\n \tif ((u->flags & REF_HAVE_NEW) && !is_null_oid(&u->new_oid) &&\n@@ -1104,13 +1104,13 @@ static int prepare_single_update(struct reftable_ref_store *refs,\n \t\t\tstrbuf_addf(err,\n \t\t\t\t    _(\"trying to write ref '%s' with nonexistent object %s\"),\n \t\t\t\t    u->refname, oid_to_hex(&u->new_oid));\n-\t\t\treturn -1;\n+\t\t\treturn REF_TRANSACTION_ERROR_INVALID_NEW_VALUE;\n \t\t}\n \n \t\tif (o->type != OBJ_COMMIT && is_branch(u->refname)) {\n \t\t\tstrbuf_addf(err, _(\"trying to write non-commit object %s to branch '%s'\"),\n \t\t\t\t    oid_to_hex(&u->new_oid), u->refname);\n-\t\t\treturn -1;\n+\t\t\treturn REF_TRANSACTION_ERROR_INVALID_NEW_VALUE;\n \t\t}\n \t}\n \n@@ -1134,7 +1134,7 @@ static int prepare_single_update(struct reftable_ref_store *refs,\n \t\t\t\t    _(\"multiple updates for 'HEAD' (including one \"\n \t\t\t\t      \"via its referent '%s') are not allowed\"),\n \t\t\t\t    u->refname);\n-\t\t\treturn TRANSACTION_NAME_CONFLICT;\n+\t\t\treturn REF_TRANSACTION_ERROR_NAME_CONFLICT;\n \t\t}\n \n \t\tref_transaction_add_update(\n@@ -1147,7 +1147,7 @@ static int prepare_single_update(struct reftable_ref_store *refs,\n \tret = reftable_backend_read_ref(be, rewritten_ref,\n \t\t\t\t\t&current_oid, referent, &u->type);\n \tif (ret < 0)\n-\t\treturn ret;\n+\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \tif (ret > 0 && !ref_update_expects_existing_old_ref(u)) {\n \t\t/*\n \t\t * The reference does not exist, and we either have no\n@@ -1168,7 +1168,7 @@ static int prepare_single_update(struct reftable_ref_store *refs,\n \t\t\tret = queue_transaction_update(refs, tx_data, u,\n \t\t\t\t\t\t       &current_oid, err);\n \t\t\tif (ret)\n-\t\t\t\treturn ret;\n+\t\t\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \t\t}\n \n \t\treturn 0;\n@@ -1180,7 +1180,7 @@ static int prepare_single_update(struct reftable_ref_store *refs,\n \n \t\t\t\t   \"unable to resolve reference '%s'\"),\n \t\t\t    ref_update_original_update_refname(u), u->refname);\n-\t\treturn -1;\n+\t\treturn REF_TRANSACTION_ERROR_NONEXISTENT_REF;\n \t}\n \n \tif (u->type & REF_ISSYMREF) {\n@@ -1196,7 +1196,7 @@ static int prepare_single_update(struct reftable_ref_store *refs,\n \t\t\tif (u->flags & REF_HAVE_OLD && !resolved) {\n \t\t\t\tstrbuf_addf(err, _(\"cannot lock ref '%s': \"\n \t\t\t\t\t\t   \"error reading reference\"), u->refname);\n-\t\t\t\treturn -1;\n+\t\t\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \t\t\t}\n \t\t} else {\n \t\t\tstruct ref_update *new_update;\n@@ -1211,7 +1211,7 @@ static int prepare_single_update(struct reftable_ref_store *refs,\n \t\t\t\t\t    _(\"multiple updates for '%s' (including one \"\n \t\t\t\t\t      \"via symref '%s') are not allowed\"),\n \t\t\t\t\t    referent->buf, u->refname);\n-\t\t\t\treturn TRANSACTION_NAME_CONFLICT;\n+\t\t\t\treturn REF_TRANSACTION_ERROR_NAME_CONFLICT;\n \t\t\t}\n \n \t\t\t/*\n@@ -1255,31 +1255,32 @@ static int prepare_single_update(struct reftable_ref_store *refs,\n \t\t\t\t\t   \"but is a regular ref\"),\n \t\t\t\t    ref_update_original_update_refname(u),\n \t\t\t\t    u->old_target);\n-\t\t\treturn -1;\n+\t\t\treturn REF_TRANSACTION_ERROR_EXPECTED_SYMREF;\n \t\t}\n \n-\t\tif (ref_update_check_old_target(referent->buf, u, err)) {\n-\t\t\treturn -1;\n-\t\t}\n+\t\tret = ref_update_check_old_target(referent->buf, u, err);\n+\t\tif (ret)\n+\t\t\treturn ret;\n \t} else if ((u->flags & REF_HAVE_OLD) && !oideq(&current_oid, &u->old_oid)) {\n \t\tif (is_null_oid(&u->old_oid)) {\n \t\t\tstrbuf_addf(err, _(\"cannot lock ref '%s': \"\n \t\t\t\t\t   \"reference already exists\"),\n \t\t\t\t    ref_update_original_update_refname(u));\n-\t\t\treturn TRANSACTION_CREATE_EXISTS;\n-\t\t}\n-\t\telse if (is_null_oid(&current_oid))\n+\t\t\treturn REF_TRANSACTION_ERROR_CREATE_EXISTS;\n+\t\t} else if (is_null_oid(&current_oid)) {\n \t\t\tstrbuf_addf(err, _(\"cannot lock ref '%s': \"\n \t\t\t\t\t   \"reference is missing but expected %s\"),\n \t\t\t\t    ref_update_original_update_refname(u),\n \t\t\t\t    oid_to_hex(&u->old_oid));\n-\t\telse\n+\t\t\treturn REF_TRANSACTION_ERROR_NONEXISTENT_REF;\n+\t\t} else {\n \t\t\tstrbuf_addf(err, _(\"cannot lock ref '%s': \"\n \t\t\t\t\t   \"is at %s but expected %s\"),\n \t\t\t\t    ref_update_original_update_refname(u),\n \t\t\t\t    oid_to_hex(&current_oid),\n \t\t\t\t    oid_to_hex(&u->old_oid));\n-\t\treturn TRANSACTION_NAME_CONFLICT;\n+\t\t\treturn REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE;\n+\t\t}\n \t}\n \n \t/*\n@@ -1296,8 +1297,8 @@ static int prepare_single_update(struct reftable_ref_store *refs,\n \tif ((u->type & REF_ISSYMREF) ||\n \t    (u->flags & REF_LOG_ONLY) ||\n \t    (u->flags & REF_HAVE_NEW && !oideq(&current_oid, &u->new_oid)))\n-\t\treturn queue_transaction_update(refs, tx_data, u,\n-\t\t\t\t\t       &current_oid, err);\n+\t\tif (queue_transaction_update(refs, tx_data, u, &current_oid, err))\n+\t\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \n \treturn 0;\n }\n@@ -1385,7 +1386,6 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \ttransaction->state = REF_TRANSACTION_PREPARED;\n \n done:\n-\tassert(ret != REFTABLE_API_ERROR);\n \tif (ret < 0) {\n \t\tfree_transaction_data(tx_data);\n \t\ttransaction->state = REF_TRANSACTION_CLOSED;\n\n-- \n2.48.1\n\n"},{"id":"514766","messageId":"20250320-245-partially-atomic-ref-updates-v4-7-3dcc1b311dc9@gmail.com","threadId":"63170","inReplyTo":"20250320-245-partially-atomic-ref-updates-v4-0-3dcc1b311dc9@gmail.com","subject":"[PATCH v4 7/8] refs: support rejection in batch updates during F/D checks","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2025-03-20T11:44:02Z","receivedAt":"2025-03-20T11:44:17Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"The `refs_verify_refnames_available()` is used to batch check refnames\nfor F/D conflicts. While this is the more performant alternative than\nits individual version, it does not provide rejection capabilities on a\nsingle update level. For batched updates, this would mean a rejection of\nthe entire transaction whenever one reference has a F/D conflict.\n\nModify the function to call `ref_transaction_maybe_set_rejected()` to\ncheck if a single update can be rejected. Since this function is only\ninternally used within 'refs/' and we want to pass in a `struct\nref_transaction *` as a variable. We also move and mark\n`refs_verify_refnames_available()` to 'refs-internal.h' to be an\ninternal function.\n\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n refs.c                  | 28 +++++++++++++++++++++++++++-\n refs.h                  | 12 ------------\n refs/files-backend.c    | 27 ++++++++++++++++++---------\n refs/refs-internal.h    | 16 ++++++++++++++++\n refs/reftable-backend.c | 11 ++++++++---\n 5 files changed, 69 insertions(+), 25 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex b34ec198f5..f719046f47 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2540,6 +2540,7 @@ enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs\n \t\t\t\t\t  const struct string_list *refnames,\n \t\t\t\t\t  const struct string_list *extras,\n \t\t\t\t\t  const struct string_list *skip,\n+\t\t\t\t\t  struct ref_transaction *transaction,\n \t\t\t\t\t  unsigned int initial_transaction,\n \t\t\t\t\t  struct strbuf *err)\n {\n@@ -2560,6 +2561,7 @@ enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs\n \tstrset_init(&dirnames);\n \n \tfor_each_string_list_item(item, refnames) {\n+\t\tconst size_t *update_idx = (size_t *)item->util;\n \t\tconst char *refname = item->string;\n \t\tconst char *extra_refname;\n \t\tstruct object_id oid;\n@@ -2599,12 +2601,26 @@ enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs\n \t\t\tif (!initial_transaction &&\n \t\t\t    !refs_read_raw_ref(refs, dirname.buf, &oid, &referent,\n \t\t\t\t\t       &type, &ignore_errno)) {\n+\t\t\t\tif (transaction && ref_transaction_maybe_set_rejected(\n+\t\t\t\t\t    transaction, *update_idx,\n+\t\t\t\t\t    REF_TRANSACTION_ERROR_NAME_CONFLICT)) {\n+\t\t\t\t\tstrset_remove(&dirnames, dirname.buf);\n+\t\t\t\t\tcontinue;\n+\t\t\t\t}\n+\n \t\t\t\tstrbuf_addf(err, _(\"'%s' exists; cannot create '%s'\"),\n \t\t\t\t\t    dirname.buf, refname);\n \t\t\t\tgoto cleanup;\n \t\t\t}\n \n \t\t\tif (extras && string_list_has_string(extras, dirname.buf)) {\n+\t\t\t\tif (transaction && ref_transaction_maybe_set_rejected(\n+\t\t\t\t\t    transaction, *update_idx,\n+\t\t\t\t\t    REF_TRANSACTION_ERROR_NAME_CONFLICT)) {\n+\t\t\t\t\tstrset_remove(&dirnames, dirname.buf);\n+\t\t\t\t\tcontinue;\n+\t\t\t\t}\n+\n \t\t\t\tstrbuf_addf(err, _(\"cannot process '%s' and '%s' at the same time\"),\n \t\t\t\t\t    refname, dirname.buf);\n \t\t\t\tgoto cleanup;\n@@ -2637,6 +2653,11 @@ enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs\n \t\t\t\t    string_list_has_string(skip, iter->refname))\n \t\t\t\t\tcontinue;\n \n+\t\t\t\tif (transaction && ref_transaction_maybe_set_rejected(\n+\t\t\t\t\t    transaction, *update_idx,\n+\t\t\t\t\t    REF_TRANSACTION_ERROR_NAME_CONFLICT))\n+\t\t\t\t\tcontinue;\n+\n \t\t\t\tstrbuf_addf(err, _(\"'%s' exists; cannot create '%s'\"),\n \t\t\t\t\t    iter->refname, refname);\n \t\t\t\tgoto cleanup;\n@@ -2648,6 +2669,11 @@ enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs\n \n \t\textra_refname = find_descendant_ref(dirname.buf, extras, skip);\n \t\tif (extra_refname) {\n+\t\t\tif (transaction && ref_transaction_maybe_set_rejected(\n+\t\t\t\t    transaction, *update_idx,\n+\t\t\t\t    REF_TRANSACTION_ERROR_NAME_CONFLICT))\n+\t\t\t\tcontinue;\n+\n \t\t\tstrbuf_addf(err, _(\"cannot process '%s' and '%s' at the same time\"),\n \t\t\t\t    refname, extra_refname);\n \t\t\tgoto cleanup;\n@@ -2679,7 +2705,7 @@ enum ref_transaction_error refs_verify_refname_available(\n \t};\n \n \treturn refs_verify_refnames_available(refs, &refnames, extras, skip,\n-\t\t\t\t\t      initial_transaction, err);\n+\t\t\t\t\t      NULL, initial_transaction, err);\n }\n \n struct do_for_each_reflog_help {\ndiff --git a/refs.h b/refs.h\nindex 591b703d59..1e7ab2532f 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -147,18 +147,6 @@ enum ref_transaction_error refs_verify_refname_available(struct ref_store *refs,\n \t\t\t\t\t\t unsigned int initial_transaction,\n \t\t\t\t\t\t struct strbuf *err);\n \n-/*\n- * Same as `refs_verify_refname_available()`, but checking for a list of\n- * refnames instead of only a single item. This is more efficient in the case\n- * where one needs to check multiple refnames.\n- */\n-enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs,\n-\t\t\t\t\t  const struct string_list *refnames,\n-\t\t\t\t\t  const struct string_list *extras,\n-\t\t\t\t\t  const struct string_list *skip,\n-\t\t\t\t\t  unsigned int initial_transaction,\n-\t\t\t\t\t  struct strbuf *err);\n-\n int refs_ref_exists(struct ref_store *refs, const char *refname);\n \n int should_autocreate_reflog(enum log_refs_config log_all_ref_updates,\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex be758ffff5..1d50d4013c 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -677,16 +677,18 @@ static void unlock_ref(struct ref_lock *lock)\n  * - Generate informative error messages in the case of failure\n  */\n static enum ref_transaction_error lock_raw_ref(struct files_ref_store *refs,\n-\t\t\t\t\t       const char *refname,\n+\t\t\t\t\t       struct ref_update *update,\n+\t\t\t\t\t       size_t update_idx,\n \t\t\t\t\t       int mustexist,\n \t\t\t\t\t       struct string_list *refnames_to_check,\n \t\t\t\t\t       const struct string_list *extras,\n \t\t\t\t\t       struct ref_lock **lock_p,\n \t\t\t\t\t       struct strbuf *referent,\n-\t\t\t\t\t       unsigned int *type,\n \t\t\t\t\t       struct strbuf *err)\n {\n \tenum ref_transaction_error ret = REF_TRANSACTION_ERROR_GENERIC;\n+\tconst char *refname = update->refname;\n+\tunsigned int *type = &update->type;\n \tstruct ref_lock *lock;\n \tstruct strbuf ref_file = STRBUF_INIT;\n \tint attempts_remaining = 3;\n@@ -785,6 +787,8 @@ static enum ref_transaction_error lock_raw_ref(struct files_ref_store *refs,\n \n \tif (files_read_raw_ref(&refs->base, refname, &lock->old_oid, referent,\n \t\t\t       type, &failure_errno)) {\n+\t\tstruct string_list_item *item;\n+\n \t\tif (failure_errno == ENOENT) {\n \t\t\tif (mustexist) {\n \t\t\t\t/* Garden variety missing reference. */\n@@ -864,7 +868,9 @@ static enum ref_transaction_error lock_raw_ref(struct files_ref_store *refs,\n \t\t * make sure there is no existing packed ref that conflicts\n \t\t * with refname. This check is deferred so that we can batch it.\n \t\t */\n-\t\tstring_list_append(refnames_to_check, refname);\n+\t\titem = string_list_append(refnames_to_check, refname);\n+\t\titem->util = xmalloc(sizeof(update_idx));\n+\t\tmemcpy(item->util, &update_idx, sizeof(update_idx));\n \t}\n \n \tret = 0;\n@@ -2547,6 +2553,7 @@ struct files_transaction_backend_data {\n  */\n static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *refs,\n \t\t\t\t\t\t      struct ref_update *update,\n+\t\t\t\t\t\t      size_t update_idx,\n \t\t\t\t\t\t      struct ref_transaction *transaction,\n \t\t\t\t\t\t      const char *head_ref,\n \t\t\t\t\t\t      struct string_list *refnames_to_check,\n@@ -2575,9 +2582,9 @@ static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *re\n \tif (lock) {\n \t\tlock->count++;\n \t} else {\n-\t\tret = lock_raw_ref(refs, update->refname, mustexist,\n+\t\tret = lock_raw_ref(refs, update, update_idx, mustexist,\n \t\t\t\t   refnames_to_check, &transaction->refnames,\n-\t\t\t\t   &lock, &referent, &update->type, err);\n+\t\t\t\t   &lock, &referent, err);\n \t\tif (ret) {\n \t\t\tchar *reason;\n \n@@ -2849,7 +2856,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \tfor (i = 0; i < transaction->nr; i++) {\n \t\tstruct ref_update *update = transaction->updates[i];\n \n-\t\tret = lock_ref_for_update(refs, update, transaction,\n+\t\tret = lock_ref_for_update(refs, update, i, transaction,\n \t\t\t\t\t  head_ref, &refnames_to_check,\n \t\t\t\t\t  err);\n \t\tif (ret) {\n@@ -2905,7 +2912,8 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \t * So instead, we accept the race for now.\n \t */\n \tif (refs_verify_refnames_available(refs->packed_ref_store, &refnames_to_check,\n-\t\t\t\t\t   &transaction->refnames, NULL, 0, err)) {\n+\t\t\t\t\t   &transaction->refnames, NULL, transaction,\n+\t\t\t\t\t   0, err)) {\n \t\tret = REF_TRANSACTION_ERROR_NAME_CONFLICT;\n \t\tgoto cleanup;\n \t}\n@@ -2951,7 +2959,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \n cleanup:\n \tfree(head_ref);\n-\tstring_list_clear(&refnames_to_check, 0);\n+\tstring_list_clear(&refnames_to_check, 1);\n \n \tif (ret)\n \t\tfiles_transaction_cleanup(refs, transaction);\n@@ -3097,7 +3105,8 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,\n \t}\n \n \tif (refs_verify_refnames_available(&refs->base, &refnames_to_check,\n-\t\t\t\t\t   &affected_refnames, NULL, 1, err)) {\n+\t\t\t\t\t   &affected_refnames, NULL, transaction,\n+\t\t\t\t\t   1, err)) {\n \t\tpacked_refs_unlock(refs->packed_ref_store);\n \t\tret = REF_TRANSACTION_ERROR_NAME_CONFLICT;\n \t\tgoto cleanup;\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 73a5379b73..f868870851 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -806,4 +806,20 @@ enum ref_transaction_error ref_update_check_old_target(const char *referent,\n  */\n int ref_update_expects_existing_old_ref(struct ref_update *update);\n \n+/*\n+ * Same as `refs_verify_refname_available()`, but checking for a list of\n+ * refnames instead of only a single item. This is more efficient in the case\n+ * where one needs to check multiple refnames.\n+ *\n+ * If using batched updates, then individual updates are marked rejected,\n+ * reference backends are then in charge of not committing those updates.\n+ */\n+enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs,\n+\t\t\t\t\t  const struct string_list *refnames,\n+\t\t\t\t\t  const struct string_list *extras,\n+\t\t\t\t\t  const struct string_list *skip,\n+\t\t\t\t\t  struct ref_transaction *transaction,\n+\t\t\t\t\t  unsigned int initial_transaction,\n+\t\t\t\t\t  struct strbuf *err);\n+\n #endif /* REFS_REFS_INTERNAL_H */\ndiff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\nindex ed65225763..f5429c918b 100644\n--- a/refs/reftable-backend.c\n+++ b/refs/reftable-backend.c\n@@ -1074,6 +1074,7 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor\n \t\t\t\t\t\t\tstruct ref_transaction *transaction,\n \t\t\t\t\t\t\tstruct reftable_backend *be,\n \t\t\t\t\t\t\tstruct ref_update *u,\n+\t\t\t\t\t\t\tsize_t update_idx,\n \t\t\t\t\t\t\tstruct string_list *refnames_to_check,\n \t\t\t\t\t\t\tunsigned int head_type,\n \t\t\t\t\t\t\tstruct strbuf *head_referent,\n@@ -1149,6 +1150,7 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor\n \tif (ret < 0)\n \t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \tif (ret > 0 && !ref_update_expects_existing_old_ref(u)) {\n+\t\tstruct string_list_item *item;\n \t\t/*\n \t\t * The reference does not exist, and we either have no\n \t\t * old object ID or expect the reference to not exist.\n@@ -1158,7 +1160,9 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor\n \t\t * can output a proper error message instead of failing\n \t\t * at a later point.\n \t\t */\n-\t\tstring_list_append(refnames_to_check, u->refname);\n+\t\titem = string_list_append(refnames_to_check, u->refname);\n+\t\titem->util = xmalloc(sizeof(update_idx));\n+\t\tmemcpy(item->util, &update_idx, sizeof(update_idx));\n \n \t\t/*\n \t\t * There is no need to write the reference deletion\n@@ -1368,7 +1372,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \n \tfor (i = 0; i < transaction->nr; i++) {\n \t\tret = prepare_single_update(refs, tx_data, transaction, be,\n-\t\t\t\t\t    transaction->updates[i],\n+\t\t\t\t\t    transaction->updates[i], i,\n \t\t\t\t\t    &refnames_to_check, head_type,\n \t\t\t\t\t    &head_referent, &referent, err);\n \t\tif (ret) {\n@@ -1384,6 +1388,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \n \tret = refs_verify_refnames_available(ref_store, &refnames_to_check,\n \t\t\t\t\t     &transaction->refnames, NULL,\n+\t\t\t\t\t     transaction,\n \t\t\t\t\t     transaction->flags & REF_TRANSACTION_FLAG_INITIAL,\n \t\t\t\t\t     err);\n \tif (ret < 0)\n@@ -1402,7 +1407,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t}\n \tstrbuf_release(&referent);\n \tstrbuf_release(&head_referent);\n-\tstring_list_clear(&refnames_to_check, 0);\n+\tstring_list_clear(&refnames_to_check, 1);\n \n \treturn ret;\n }\n\n-- \n2.48.1\n\n"},{"id":"514767","messageId":"20250320-245-partially-atomic-ref-updates-v4-8-3dcc1b311dc9@gmail.com","threadId":"63170","inReplyTo":"20250320-245-partially-atomic-ref-updates-v4-0-3dcc1b311dc9@gmail.com","subject":"[PATCH v4 8/8] update-ref: add --batch-updates flag for stdin mode","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2025-03-20T11:44:03Z","receivedAt":"2025-03-20T11:44:18Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"When updating multiple references through stdin, Git's update-ref\ncommand normally aborts the entire transaction if any single update\nfails. This atomic behavior prevents partial updates. Introduce a new\nbatch update system, where the updates the performed together similar\nbut individual updates are allowed to fail.\n\nAdd a new `--batch-updates` flag that allows the transaction to continue\neven when individual reference updates fail. This flag can only be used\nin `--stdin` mode and builds upon the batch update support added to the\nrefs subsystem in the previous commits. When enabled, failed updates are\nreported in the following format:\n\n  rejected SP (<old-oid> | <old-target>) SP (<new-oid> | <new-target>) SP <rejection-reason> LF\n\nUpdate the documentation to reflect this change and also tests to cover\ndifferent scenarios where an update could be rejected.\n\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n Documentation/git-update-ref.adoc |  14 ++-\n builtin/update-ref.c              |  67 ++++++++++-\n t/t1400-update-ref.sh             | 233 ++++++++++++++++++++++++++++++++++++++\n 3 files changed, 306 insertions(+), 8 deletions(-)\n\ndiff --git a/Documentation/git-update-ref.adoc b/Documentation/git-update-ref.adoc\nindex 9e6935d38d..5be2c16776 100644\n--- a/Documentation/git-update-ref.adoc\n+++ b/Documentation/git-update-ref.adoc\n@@ -7,8 +7,10 @@ git-update-ref - Update the object name stored in a ref safely\n \n SYNOPSIS\n --------\n-[verse]\n-'git update-ref' [-m <reason>] [--no-deref] (-d <ref> [<old-oid>] | [--create-reflog] <ref> <new-oid> [<old-oid>] | --stdin [-z])\n+[synopsis]\n+git update-ref [-m <reason>] [--no-deref] -d <ref> [<old-oid>]\n+\t       [-m <reason>] [--no-deref] [--create-reflog] <ref> <new-oid> [<old-oid>]\n+               [-m <reason>] [--no-deref] --stdin [-z] [--batch-updates]\n \n DESCRIPTION\n -----------\n@@ -57,6 +59,14 @@ performs all modifications together.  Specify commands of the form:\n With `--create-reflog`, update-ref will create a reflog for each ref\n even if one would not ordinarily be created.\n \n+With `--batch-updates`, update-ref executes the updates in a batch but allows\n+individual updates to fail due to invalid or incorrect user input, applying only\n+the successful updates. However, system-related errors—such as I/O failures or\n+memory issues—will result in a full failure of all batched updates. Any failed\n+updates will be reported in the following format:\n+\n+\trejected SP (<old-oid> | <old-target>) SP (<new-oid> | <new-target>) SP <rejection-reason> LF\n+\n Quote fields containing whitespace as if they were strings in C source\n code; i.e., surrounded by double-quotes and with backslash escapes.\n Use 40 \"0\" characters or the empty string to specify a zero value.  To\ndiff --git a/builtin/update-ref.c b/builtin/update-ref.c\nindex 1d541e13ad..97e14b279e 100644\n--- a/builtin/update-ref.c\n+++ b/builtin/update-ref.c\n@@ -5,6 +5,7 @@\n #include \"config.h\"\n #include \"gettext.h\"\n #include \"hash.h\"\n+#include \"hex.h\"\n #include \"refs.h\"\n #include \"object-name.h\"\n #include \"parse-options.h\"\n@@ -13,7 +14,7 @@\n static const char * const git_update_ref_usage[] = {\n \tN_(\"git update-ref [<options>] -d <refname> [<old-oid>]\"),\n \tN_(\"git update-ref [<options>]    <refname> <new-oid> [<old-oid>]\"),\n-\tN_(\"git update-ref [<options>] --stdin [-z]\"),\n+\tN_(\"git update-ref [<options>] --stdin [-z] [--batch-updates]\"),\n \tNULL\n };\n \n@@ -565,6 +566,49 @@ static void parse_cmd_abort(struct ref_transaction *transaction,\n \treport_ok(\"abort\");\n }\n \n+static void print_rejected_refs(const char *refname,\n+\t\t\t\tconst struct object_id *old_oid,\n+\t\t\t\tconst struct object_id *new_oid,\n+\t\t\t\tconst char *old_target,\n+\t\t\t\tconst char *new_target,\n+\t\t\t\tenum ref_transaction_error err,\n+\t\t\t\tvoid *cb_data UNUSED)\n+{\n+\tstruct strbuf sb = STRBUF_INIT;\n+\tconst char *reason = \"\";\n+\n+\tswitch (err) {\n+\tcase REF_TRANSACTION_ERROR_NAME_CONFLICT:\n+\t\treason = \"refname conflict\";\n+\t\tbreak;\n+\tcase REF_TRANSACTION_ERROR_CREATE_EXISTS:\n+\t\treason = \"reference already exists\";\n+\t\tbreak;\n+\tcase REF_TRANSACTION_ERROR_NONEXISTENT_REF:\n+\t\treason = \"reference does not exist\";\n+\t\tbreak;\n+\tcase REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE:\n+\t\treason = \"incorrect old value provided\";\n+\t\tbreak;\n+\tcase REF_TRANSACTION_ERROR_INVALID_NEW_VALUE:\n+\t\treason = \"invalid new value provided\";\n+\t\tbreak;\n+\tcase REF_TRANSACTION_ERROR_EXPECTED_SYMREF:\n+\t\treason = \"expected symref but found regular ref\";\n+\t\tbreak;\n+\tdefault:\n+\t\treason = \"unkown failure\";\n+\t}\n+\n+\tstrbuf_addf(&sb, \"rejected %s %s %s %s\\n\", refname,\n+\t\t    new_oid ? oid_to_hex(new_oid) : new_target,\n+\t\t    old_oid ? oid_to_hex(old_oid) : old_target,\n+\t\t    reason);\n+\n+\tfwrite(sb.buf, sb.len, 1, stdout);\n+\tstrbuf_release(&sb);\n+}\n+\n static void parse_cmd_commit(struct ref_transaction *transaction,\n \t\t\t     const char *next, const char *end UNUSED)\n {\n@@ -573,6 +617,10 @@ static void parse_cmd_commit(struct ref_transaction *transaction,\n \t\tdie(\"commit: extra input: %s\", next);\n \tif (ref_transaction_commit(transaction, &error))\n \t\tdie(\"commit: %s\", error.buf);\n+\n+\tref_transaction_for_each_rejected_update(transaction,\n+\t\t\t\t\t\t print_rejected_refs, NULL);\n+\n \treport_ok(\"commit\");\n \tref_transaction_free(transaction);\n }\n@@ -609,7 +657,7 @@ static const struct parse_cmd {\n \t{ \"commit\",        parse_cmd_commit,        0, UPDATE_REFS_CLOSED },\n };\n \n-static void update_refs_stdin(void)\n+static void update_refs_stdin(unsigned int flags)\n {\n \tstruct strbuf input = STRBUF_INIT, err = STRBUF_INIT;\n \tenum update_refs_state state = UPDATE_REFS_OPEN;\n@@ -617,7 +665,7 @@ static void update_refs_stdin(void)\n \tint i, j;\n \n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t  0, &err);\n+\t\t\t\t\t\t  flags, &err);\n \tif (!transaction)\n \t\tdie(\"%s\", err.buf);\n \n@@ -685,7 +733,7 @@ static void update_refs_stdin(void)\n \t\t\t */\n \t\t\tstate = cmd->state;\n \t\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t\t\t  0, &err);\n+\t\t\t\t\t\t\t\t  flags, &err);\n \t\t\tif (!transaction)\n \t\t\t\tdie(\"%s\", err.buf);\n \n@@ -701,6 +749,8 @@ static void update_refs_stdin(void)\n \t\t/* Commit by default if no transaction was requested. */\n \t\tif (ref_transaction_commit(transaction, &err))\n \t\t\tdie(\"%s\", err.buf);\n+\t\tref_transaction_for_each_rejected_update(transaction,\n+\t\t\t\t\t\t print_rejected_refs, NULL);\n \t\tref_transaction_free(transaction);\n \t\tbreak;\n \tcase UPDATE_REFS_STARTED:\n@@ -727,6 +777,8 @@ int cmd_update_ref(int argc,\n \tstruct object_id oid, oldoid;\n \tint delete = 0, no_deref = 0, read_stdin = 0, end_null = 0;\n \tint create_reflog = 0;\n+\tunsigned int flags = 0;\n+\n \tstruct option options[] = {\n \t\tOPT_STRING( 'm', NULL, &msg, N_(\"reason\"), N_(\"reason of the update\")),\n \t\tOPT_BOOL('d', NULL, &delete, N_(\"delete the reference\")),\n@@ -735,6 +787,8 @@ int cmd_update_ref(int argc,\n \t\tOPT_BOOL('z', NULL, &end_null, N_(\"stdin has NUL-terminated arguments\")),\n \t\tOPT_BOOL( 0 , \"stdin\", &read_stdin, N_(\"read updates from stdin\")),\n \t\tOPT_BOOL( 0 , \"create-reflog\", &create_reflog, N_(\"create a reflog\")),\n+\t\tOPT_BIT('0', \"batch-updates\", &flags, N_(\"batch reference updates\"),\n+\t\t\tREF_TRANSACTION_ALLOW_FAILURE),\n \t\tOPT_END(),\n \t};\n \n@@ -756,9 +810,10 @@ int cmd_update_ref(int argc,\n \t\t\tusage_with_options(git_update_ref_usage, options);\n \t\tif (end_null)\n \t\t\tline_termination = '\\0';\n-\t\tupdate_refs_stdin();\n+\t\tupdate_refs_stdin(flags);\n \t\treturn 0;\n-\t}\n+\t} else if (flags & REF_TRANSACTION_ALLOW_FAILURE)\n+\t\tdie(\"--batch-updates can only be used with --stdin\");\n \n \tif (end_null)\n \t\tusage_with_options(git_update_ref_usage, options);\ndiff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh\nindex 29045aad43..d29d23cb89 100755\n--- a/t/t1400-update-ref.sh\n+++ b/t/t1400-update-ref.sh\n@@ -2066,6 +2066,239 @@ do\n \t\tgrep \"$(git rev-parse $a) $(git rev-parse $a)\" actual\n \t'\n \n+\ttest_expect_success \"stdin $type batch-updates\" '\n+\t\tgit init repo &&\n+\t\ttest_when_finished \"rm -fr repo\" &&\n+\t\t(\n+\t\t\tcd repo &&\n+\t\t\ttest_commit commit &&\n+\t\t\thead=$(git rev-parse HEAD) &&\n+\n+\t\t\tformat_command $type \"update refs/heads/ref1\" \"$head\" \"$Z\" >stdin &&\n+\t\t\tformat_command $type \"update refs/heads/ref2\" \"$head\" \"$Z\" >>stdin &&\n+\t\t\tgit update-ref $type --stdin --batch-updates <stdin &&\n+\t\t\techo $head >expect &&\n+\t\t\tgit rev-parse refs/heads/ref1 >actual &&\n+\t\t\ttest_cmp expect actual &&\n+\t\t\tgit rev-parse refs/heads/ref2 >actual &&\n+\t\t\ttest_cmp expect actual\n+\t\t)\n+\t'\n+\n+\ttest_expect_success \"stdin $type batch-updates with invalid new_oid\" '\n+\t\tgit init repo &&\n+\t\ttest_when_finished \"rm -fr repo\" &&\n+\t\t(\n+\t\t\tcd repo &&\n+\t\t\ttest_commit one &&\n+\t\t\told_head=$(git rev-parse HEAD) &&\n+\t\t\ttest_commit two &&\n+\t\t\thead=$(git rev-parse HEAD) &&\n+\t\t\tgit update-ref refs/heads/ref1 $head &&\n+\t\t\tgit update-ref refs/heads/ref2 $head &&\n+\n+\t\t\tformat_command $type \"update refs/heads/ref1\" \"$old_head\" \"$head\" >stdin &&\n+\t\t\tformat_command $type \"update refs/heads/ref2\" \"$(test_oid 001)\" \"$head\" >>stdin &&\n+\t\t\tgit update-ref $type --stdin --batch-updates <stdin >stdout &&\n+\t\t\techo $old_head >expect &&\n+\t\t\tgit rev-parse refs/heads/ref1 >actual &&\n+\t\t\ttest_cmp expect actual &&\n+\t\t\techo $head >expect &&\n+\t\t\tgit rev-parse refs/heads/ref2 >actual &&\n+\t\t\ttest_cmp expect actual &&\n+\t\t\ttest_grep -q \"invalid new value provided\" stdout\n+\t\t)\n+\t'\n+\n+\ttest_expect_success \"stdin $type batch-updates with non-commit new_oid\" '\n+\t\tgit init repo &&\n+\t\ttest_when_finished \"rm -fr repo\" &&\n+\t\t(\n+\t\t\tcd repo &&\n+\t\t\ttest_commit one &&\n+\t\t\told_head=$(git rev-parse HEAD) &&\n+\t\t\ttest_commit two &&\n+\t\t\thead=$(git rev-parse HEAD) &&\n+\t\t\thead_tree=$(git rev-parse HEAD^{tree}) &&\n+\t\t\tgit update-ref refs/heads/ref1 $head &&\n+\t\t\tgit update-ref refs/heads/ref2 $head &&\n+\n+\t\t\tformat_command $type \"update refs/heads/ref1\" \"$old_head\" \"$head\" >stdin &&\n+\t\t\tformat_command $type \"update refs/heads/ref2\" \"$head_tree\" \"$head\" >>stdin &&\n+\t\t\tgit update-ref $type --stdin --batch-updates <stdin >stdout &&\n+\t\t\techo $old_head >expect &&\n+\t\t\tgit rev-parse refs/heads/ref1 >actual &&\n+\t\t\ttest_cmp expect actual &&\n+\t\t\techo $head >expect &&\n+\t\t\tgit rev-parse refs/heads/ref2 >actual &&\n+\t\t\ttest_cmp expect actual &&\n+\t\t\ttest_grep -q \"invalid new value provided\" stdout\n+\t\t)\n+\t'\n+\n+\ttest_expect_success \"stdin $type batch-updates with non-existent ref\" '\n+\t\tgit init repo &&\n+\t\ttest_when_finished \"rm -fr repo\" &&\n+\t\t(\n+\t\t\tcd repo &&\n+\t\t\ttest_commit one &&\n+\t\t\told_head=$(git rev-parse HEAD) &&\n+\t\t\ttest_commit two &&\n+\t\t\thead=$(git rev-parse HEAD) &&\n+\t\t\tgit update-ref refs/heads/ref1 $head &&\n+\n+\t\t\tformat_command $type \"update refs/heads/ref1\" \"$old_head\" \"$head\" >stdin &&\n+\t\t\tformat_command $type \"update refs/heads/ref2\" \"$old_head\" \"$head\" >>stdin &&\n+\t\t\tgit update-ref $type --stdin --batch-updates <stdin >stdout &&\n+\t\t\techo $old_head >expect &&\n+\t\t\tgit rev-parse refs/heads/ref1 >actual &&\n+\t\t\ttest_cmp expect actual &&\n+\t\t\ttest_must_fail git rev-parse refs/heads/ref2 &&\n+\t\t\ttest_grep -q \"reference does not exist\" stdout\n+\t\t)\n+\t'\n+\n+\ttest_expect_success \"stdin $type batch-updates with dangling symref\" '\n+\t\tgit init repo &&\n+\t\ttest_when_finished \"rm -fr repo\" &&\n+\t\t(\n+\t\t\tcd repo &&\n+\t\t\ttest_commit one &&\n+\t\t\told_head=$(git rev-parse HEAD) &&\n+\t\t\ttest_commit two &&\n+\t\t\thead=$(git rev-parse HEAD) &&\n+\t\t\tgit update-ref refs/heads/ref1 $head &&\n+\t\t\tgit symbolic-ref refs/heads/ref2 refs/heads/nonexistent &&\n+\n+\t\t\tformat_command $type \"update refs/heads/ref1\" \"$old_head\" \"$head\" >stdin &&\n+\t\t\tformat_command $type \"update refs/heads/ref2\" \"$old_head\" \"$head\" >>stdin &&\n+\t\t\tgit update-ref $type --no-deref --stdin --batch-updates <stdin >stdout &&\n+\t\t\techo $old_head >expect &&\n+\t\t\tgit rev-parse refs/heads/ref1 >actual &&\n+\t\t\ttest_cmp expect actual &&\n+\t\t\techo $head >expect &&\n+\t\t\ttest_must_fail git rev-parse refs/heads/ref2 &&\n+\t\t\ttest_grep -q \"reference does not exist\" stdout\n+\t\t)\n+\t'\n+\n+\ttest_expect_success \"stdin $type batch-updates with regular ref as symref\" '\n+\t\tgit init repo &&\n+\t\ttest_when_finished \"rm -fr repo\" &&\n+\t\t(\n+\t\t\tcd repo &&\n+\t\t\ttest_commit one &&\n+\t\t\told_head=$(git rev-parse HEAD) &&\n+\t\t\ttest_commit two &&\n+\t\t\thead=$(git rev-parse HEAD) &&\n+\t\t\tgit update-ref refs/heads/ref1 $head &&\n+\t\t\tgit update-ref refs/heads/ref2 $head &&\n+\n+\t\t\tformat_command $type \"update refs/heads/ref1\" \"$old_head\" \"$head\" >stdin &&\n+\t\t\tformat_command $type \"symref-update refs/heads/ref2\" \"$old_head\" \"ref\" \"refs/heads/nonexistent\" >>stdin &&\n+\t\t\tgit update-ref $type --no-deref --stdin --batch-updates <stdin >stdout &&\n+\t\t\techo $old_head >expect &&\n+\t\t\tgit rev-parse refs/heads/ref1 >actual &&\n+\t\t\ttest_cmp expect actual &&\n+\t\t\techo $head >expect &&\n+\t\t\techo $head >expect &&\n+\t\t\tgit rev-parse refs/heads/ref2 >actual &&\n+\t\t\ttest_cmp expect actual &&\n+\t\t\ttest_grep -q \"expected symref but found regular ref\" stdout\n+\t\t)\n+\t'\n+\n+\ttest_expect_success \"stdin $type batch-updates with invalid old_oid\" '\n+\t\tgit init repo &&\n+\t\ttest_when_finished \"rm -fr repo\" &&\n+\t\t(\n+\t\t\tcd repo &&\n+\t\t\ttest_commit one &&\n+\t\t\told_head=$(git rev-parse HEAD) &&\n+\t\t\ttest_commit two &&\n+\t\t\thead=$(git rev-parse HEAD) &&\n+\t\t\tgit update-ref refs/heads/ref1 $head &&\n+\t\t\tgit update-ref refs/heads/ref2 $head &&\n+\n+\t\t\tformat_command $type \"update refs/heads/ref1\" \"$old_head\" \"$head\" >stdin &&\n+\t\t\tformat_command $type \"update refs/heads/ref2\" \"$old_head\" \"$Z\" >>stdin &&\n+\t\t\tgit update-ref $type --stdin --batch-updates <stdin >stdout &&\n+\t\t\techo $old_head >expect &&\n+\t\t\tgit rev-parse refs/heads/ref1 >actual &&\n+\t\t\ttest_cmp expect actual &&\n+\t\t\techo $head >expect &&\n+\t\t\tgit rev-parse refs/heads/ref2 >actual &&\n+\t\t\ttest_cmp expect actual &&\n+\t\t\ttest_grep -q \"reference already exists\" stdout\n+\t\t)\n+\t'\n+\n+\ttest_expect_success \"stdin $type batch-updates with incorrect old oid\" '\n+\t\tgit init repo &&\n+\t\ttest_when_finished \"rm -fr repo\" &&\n+\t\t(\n+\t\t\tcd repo &&\n+\t\t\ttest_commit one &&\n+\t\t\told_head=$(git rev-parse HEAD) &&\n+\t\t\ttest_commit two &&\n+\t\t\thead=$(git rev-parse HEAD) &&\n+\t\t\tgit update-ref refs/heads/ref1 $head &&\n+\t\t\tgit update-ref refs/heads/ref2 $head &&\n+\n+\t\t\tformat_command $type \"update refs/heads/ref1\" \"$old_head\" \"$head\" >stdin &&\n+\t\t\tformat_command $type \"update refs/heads/ref2\" \"$head\" \"$old_head\" >>stdin &&\n+\t\t\tgit update-ref $type --stdin --batch-updates <stdin >stdout &&\n+\t\t\techo $old_head >expect &&\n+\t\t\tgit rev-parse refs/heads/ref1 >actual &&\n+\t\t\ttest_cmp expect actual &&\n+\t\t\techo $head >expect &&\n+\t\t\tgit rev-parse refs/heads/ref2 >actual &&\n+\t\t\ttest_cmp expect actual &&\n+\t\t\ttest_grep -q \"incorrect old value provided\" stdout\n+\t\t)\n+\t'\n+\n+\ttest_expect_success \"stdin $type batch-updates refname conflict\" '\n+\t\tgit init repo &&\n+\t\ttest_when_finished \"rm -fr repo\" &&\n+\t\t(\n+\t\t\tcd repo &&\n+\t\t\ttest_commit one &&\n+\t\t\told_head=$(git rev-parse HEAD) &&\n+\t\t\ttest_commit two &&\n+\t\t\thead=$(git rev-parse HEAD) &&\n+\t\t\tgit update-ref refs/heads/ref/foo $head &&\n+\n+\t\t\tformat_command $type \"update refs/heads/ref/foo\" \"$old_head\" \"$head\" >stdin &&\n+\t\t\tformat_command $type \"update refs/heads/ref\" \"$old_head\" \"\" >>stdin &&\n+\t\t\tgit update-ref $type --stdin --batch-updates <stdin >stdout &&\n+\t\t\techo $old_head >expect &&\n+\t\t\tgit rev-parse refs/heads/ref/foo >actual &&\n+\t\t\ttest_cmp expect actual &&\n+\t\t\ttest_grep -q \"refname conflict\" stdout\n+\t\t)\n+\t'\n+\n+\ttest_expect_success \"stdin $type batch-updates refname conflict new ref\" '\n+\t\tgit init repo &&\n+\t\ttest_when_finished \"rm -fr repo\" &&\n+\t\t(\n+\t\t\tcd repo &&\n+\t\t\ttest_commit one &&\n+\t\t\told_head=$(git rev-parse HEAD) &&\n+\t\t\ttest_commit two &&\n+\t\t\thead=$(git rev-parse HEAD) &&\n+\t\t\tgit update-ref refs/heads/ref/foo $head &&\n+\n+\t\t\tformat_command $type \"update refs/heads/foo\" \"$old_head\" \"\" >stdin &&\n+\t\t\tformat_command $type \"update refs/heads/ref\" \"$old_head\" \"\" >>stdin &&\n+\t\t\tgit update-ref $type --stdin --batch-updates <stdin >stdout &&\n+\t\t\techo $old_head >expect &&\n+\t\t\tgit rev-parse refs/heads/foo >actual &&\n+\t\t\ttest_cmp expect actual &&\n+\t\t\ttest_grep -q \"refname conflict\" stdout\n+\t\t)\n+\t'\n done\n \n test_expect_success 'update-ref should also create reflog for HEAD' '\n\n-- \n2.48.1\n\n"},{"id":"514808","messageId":"Z9x578gHJQPCYU4v@pks.im","threadId":"63170","inReplyTo":"20250320-245-partially-atomic-ref-updates-v4-5-3dcc1b311dc9@gmail.com","subject":"Re: [PATCH v4 5/8] refs: introduce enum-based transaction error types","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-03-20T20:26:23Z","receivedAt":"2025-03-20T20:26:33Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Thu, Mar 20, 2025 at 12:44:00PM +0100, Karthik Nayak wrote:\n> diff --git a/refs.h b/refs.h\n> index 240e2d8537..dcd83e81e2 100644\n> --- a/refs.h\n> +++ b/refs.h\n> @@ -16,6 +16,29 @@ struct worktree;\n>  enum ref_storage_format ref_storage_format_by_name(const char *name);\n>  const char *ref_storage_format_to_name(enum ref_storage_format ref_storage_format);\n>  \n> +/*\n> + * enum ref_transaction_error represents the following return codes:\n> + * REF_TRANSACTION_ERROR_GENERIC error_code: default error code.\n> + * REF_TRANSACTION_ERROR_NAME_CONFLICT error_code: ref name conflict like A vs A/B.\n> + * REF_TRANSACTION_ERROR_CREATE_EXISTS error_code: ref to be created already exists.\n> + * REF_TRANSACTION_ERROR_NONEXISTENT_REF error_code: ref expected but doesn't exist.\n> + * REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE error_code: provided old_oid or old_target of\n> + * reference doesn't match actual.\n> + * REF_TRANSACTION_ERROR_INVALID_NEW_VALUE error_code: provided new_oid or new_target is\n> + * invalid.\n> + * REF_TRANSACTION_ERROR_EXPECTED_SYMREF error_code: expected ref to be symref, but is a\n> + * regular ref.\n> + */\n> +enum ref_transaction_error {\n> +\tREF_TRANSACTION_ERROR_GENERIC = -1,\n> +\tREF_TRANSACTION_ERROR_NAME_CONFLICT = -2,\n> +\tREF_TRANSACTION_ERROR_CREATE_EXISTS = -3,\n> +\tREF_TRANSACTION_ERROR_NONEXISTENT_REF = -4,\n> +\tREF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE = -5,\n> +\tREF_TRANSACTION_ERROR_INVALID_NEW_VALUE = -6,\n> +\tREF_TRANSACTION_ERROR_EXPECTED_SYMREF = -7,\n> +};\n> +\n\nTiny nit: I think it's generally preferable to document each specific\nright next to its definition.\n\nPatrick\n"},{"id":"514809","messageId":"Z9x590KcwP8WGGG-@pks.im","threadId":"63170","inReplyTo":"20250320-245-partially-atomic-ref-updates-v4-6-3dcc1b311dc9@gmail.com","subject":"Re: [PATCH v4 6/8] refs: implement batch reference update support","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-03-20T20:26:31Z","receivedAt":"2025-03-20T20:26:34Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Thu, Mar 20, 2025 at 12:44:01PM +0100, Karthik Nayak wrote:\n> diff --git a/refs.c b/refs.c\n> index 3d0b53d56e..b34ec198f5 100644\n> --- a/refs.c\n> +++ b/refs.c\n> @@ -1206,11 +1210,45 @@ void ref_transaction_free(struct ref_transaction *transaction)\n>  \t\tfree((char *)transaction->updates[i]->old_target);\n>  \t\tfree(transaction->updates[i]);\n>  \t}\n> +\n> +\tif (transaction->rejections)\n> +\t\tfree(transaction->rejections->update_indices);\n> +\tfree(transaction->rejections);\n> +\n>  \tstring_list_clear(&transaction->refnames, 0);\n>  \tfree(transaction->updates);\n>  \tfree(transaction);\n>  }\n>  \n> +int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction,\n> +\t\t\t\t       size_t update_idx,\n> +\t\t\t\t       enum ref_transaction_error err)\n> +{\n> +\tif (update_idx >= transaction->nr)\n> +\t\tBUG(\"trying to set rejection on invalid update index\");\n> +\n> +\tif (!(transaction->flags & REF_TRANSACTION_ALLOW_FAILURE))\n> +\t\treturn 0;\n> +\n> +\tif (!transaction->rejections)\n> +\t\tBUG(\"transaction not inititalized with failure support\");\n> +\n> +\t/*\n> +\t * Don't accept generic errors, since these errors are not user\n> +\t * input related.\n> +\t */\n> +\tif (err == REF_TRANSACTION_ERROR_GENERIC)\n> +\t\treturn 0;\n> +\n> +\ttransaction->updates[update_idx]->rejection_err = err;\n> +\tALLOC_GROW(transaction->rejections->update_indices,\n> +\t\t   transaction->rejections->nr + 1,\n> +\t\t   transaction->rejections->alloc);\n> +\ttransaction->rejections->update_indices[transaction->rejections->nr++] = update_idx;\n> +\n> +\treturn 1;\n> +}\n\nIf we had a `struct ref_update_rejection` we could store the update\nindex and rejection errors in the same location, which might be a bit\neasier to reason about.\n\n> diff --git a/refs/packed-backend.c b/refs/packed-backend.c\n> index d90bd815a3..7bf57ca948 100644\n> --- a/refs/packed-backend.c\n> +++ b/refs/packed-backend.c\n> @@ -1327,10 +1327,11 @@ static int packed_ref_store_remove_on_disk(struct ref_store *ref_store,\n>   * remain locked when it is done.\n>   */\n>  static enum ref_transaction_error write_with_updates(struct packed_ref_store *refs,\n> -\t\t\t\t\t\t     struct string_list *updates,\n> +\t\t\t\t\t\t     struct ref_transaction *transaction,\n>  \t\t\t\t\t\t     struct strbuf *err)\n>  {\n>  \tenum ref_transaction_error ret = REF_TRANSACTION_ERROR_GENERIC;\n> +\tstruct string_list *updates = &transaction->refnames;\n>  \tstruct ref_iterator *iter = NULL;\n>  \tsize_t i;\n>  \tint ok;\n> @@ -1411,6 +1412,13 @@ static enum ref_transaction_error write_with_updates(struct packed_ref_store *re\n>  \t\t\t\t\t\t    \"reference already exists\",\n>  \t\t\t\t\t\t    update->refname);\n>  \t\t\t\t\tret = REF_TRANSACTION_ERROR_CREATE_EXISTS;\n> +\n> +\t\t\t\t\tif (ref_transaction_maybe_set_rejected(transaction, i, ret)) {\n> +\t\t\t\t\t\tstrbuf_setlen(err, 0);\n\nNit: you can use `strbuf_reset()` for this and other instances.\n\nPatrick\n"},{"id":"514925","messageId":"Z-FZObjvIN-qzvlj@pks.im","threadId":"63170","inReplyTo":"20250320-245-partially-atomic-ref-updates-v4-7-3dcc1b311dc9@gmail.com","subject":"Re: [PATCH v4 7/8] refs: support rejection in batch updates during F/D checks","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-03-24T13:08:09Z","receivedAt":"2025-03-24T13:08:19Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Thu, Mar 20, 2025 at 12:44:02PM +0100, Karthik Nayak wrote:\n> diff --git a/refs.c b/refs.c\n> index b34ec198f5..f719046f47 100644\n> --- a/refs.c\n> +++ b/refs.c\n> @@ -2540,6 +2540,7 @@ enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs\n>  \t\t\t\t\t  const struct string_list *refnames,\n>  \t\t\t\t\t  const struct string_list *extras,\n>  \t\t\t\t\t  const struct string_list *skip,\n> +\t\t\t\t\t  struct ref_transaction *transaction,\n>  \t\t\t\t\t  unsigned int initial_transaction,\n>  \t\t\t\t\t  struct strbuf *err)\n>  {\n> @@ -2599,12 +2601,26 @@ enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs\n>  \t\t\tif (!initial_transaction &&\n>  \t\t\t    !refs_read_raw_ref(refs, dirname.buf, &oid, &referent,\n>  \t\t\t\t\t       &type, &ignore_errno)) {\n> +\t\t\t\tif (transaction && ref_transaction_maybe_set_rejected(\n> +\t\t\t\t\t    transaction, *update_idx,\n> +\t\t\t\t\t    REF_TRANSACTION_ERROR_NAME_CONFLICT)) {\n> +\t\t\t\t\tstrset_remove(&dirnames, dirname.buf);\n> +\t\t\t\t\tcontinue;\n> +\t\t\t\t}\n> +\n\nOkay. We have to remove the dirname from `dirnames` again so that the\nnext reference that creates a reference in the same directory would also\nbe marked as conflicting. It does have the consequence that we now have\nto read the dirname N times again, where N is the number of refs that\nare created below that directory.\n\nWe could probably improve this by using another map that contains the\nconflicting names, right?\n\n> diff --git a/refs/files-backend.c b/refs/files-backend.c\n> index be758ffff5..1d50d4013c 100644\n> --- a/refs/files-backend.c\n> +++ b/refs/files-backend.c\n> @@ -864,7 +868,9 @@ static enum ref_transaction_error lock_raw_ref(struct files_ref_store *refs,\n>  \t\t * make sure there is no existing packed ref that conflicts\n>  \t\t * with refname. This check is deferred so that we can batch it.\n>  \t\t */\n> -\t\tstring_list_append(refnames_to_check, refname);\n> +\t\titem = string_list_append(refnames_to_check, refname);\n> +\t\titem->util = xmalloc(sizeof(update_idx));\n> +\t\tmemcpy(item->util, &update_idx, sizeof(update_idx));\n>  \t}\n>  \n>  \tret = 0;\n\nHm, so we have to allocate the `util` field now to store the update\nindex, which is a bit unfortunate because all of this is part of the hot\nloop. We cannot store a direct pointer though because the array of\nupdates may be reallocated, which would invalidate any pointers pointing\ninto the array.\n\nI was wondering whether we could abuse an `uintptr_t` and use it to\nstore the update index as a pointer. It does feel somewhat dirty though.\n\nPatrick\n"},{"id":"514926","messageId":"Z-FZQOYGPIuB2h-t@pks.im","threadId":"63170","inReplyTo":"20250320-245-partially-atomic-ref-updates-v4-8-3dcc1b311dc9@gmail.com","subject":"Re: [PATCH v4 8/8] update-ref: add --batch-updates flag for stdin mode","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-03-24T13:08:16Z","receivedAt":"2025-03-24T13:08:20Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Thu, Mar 20, 2025 at 12:44:03PM +0100, Karthik Nayak wrote:\n> diff --git a/Documentation/git-update-ref.adoc b/Documentation/git-update-ref.adoc\n> index 9e6935d38d..5be2c16776 100644\n> --- a/Documentation/git-update-ref.adoc\n> +++ b/Documentation/git-update-ref.adoc\n> @@ -57,6 +59,14 @@ performs all modifications together.  Specify commands of the form:\n>  With `--create-reflog`, update-ref will create a reflog for each ref\n>  even if one would not ordinarily be created.\n>  \n> +With `--batch-updates`, update-ref executes the updates in a batch but allows\n> +individual updates to fail due to invalid or incorrect user input, applying only\n> +the successful updates. However, system-related errors—such as I/O failures or\n> +memory issues—will result in a full failure of all batched updates. Any failed\n> +updates will be reported in the following format:\n> +\n> +\trejected SP (<old-oid> | <old-target>) SP (<new-oid> | <new-target>) SP <rejection-reason> LF\n> +\n\nDoes this support NUL-terminated mode? It probably should, and if it\ndoes we should also document the format.\n\n> diff --git a/builtin/update-ref.c b/builtin/update-ref.c\n> index 1d541e13ad..97e14b279e 100644\n> --- a/builtin/update-ref.c\n> +++ b/builtin/update-ref.c\n> @@ -735,6 +787,8 @@ int cmd_update_ref(int argc,\n>  \t\tOPT_BOOL('z', NULL, &end_null, N_(\"stdin has NUL-terminated arguments\")),\n>  \t\tOPT_BOOL( 0 , \"stdin\", &read_stdin, N_(\"read updates from stdin\")),\n>  \t\tOPT_BOOL( 0 , \"create-reflog\", &create_reflog, N_(\"create a reflog\")),\n> +\t\tOPT_BIT('0', \"batch-updates\", &flags, N_(\"batch reference updates\"),\n> +\t\t\tREF_TRANSACTION_ALLOW_FAILURE),\n>  \t\tOPT_END(),\n>  \t};\n>  \n> @@ -756,9 +810,10 @@ int cmd_update_ref(int argc,\n>  \t\t\tusage_with_options(git_update_ref_usage, options);\n>  \t\tif (end_null)\n>  \t\t\tline_termination = '\\0';\n> -\t\tupdate_refs_stdin();\n> +\t\tupdate_refs_stdin(flags);\n>  \t\treturn 0;\n> -\t}\n> +\t} else if (flags & REF_TRANSACTION_ALLOW_FAILURE)\n> +\t\tdie(\"--batch-updates can only be used with --stdin\");\n\nNit: formatting, the `else if` branch should have curly braces.\n\nPatrick\n"},{"id":"514931","messageId":"CAOLa=ZRM8EeeVXpZFpLU7Wg8k98_U+0hw1y63gQDjPEfR2P2cw@mail.gmail.com","threadId":"63170","inReplyTo":"Z9x578gHJQPCYU4v@pks.im","subject":"Re: [PATCH v4 5/8] refs: introduce enum-based transaction error types","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2025-03-24T14:50:56Z","receivedAt":"2025-03-24T14:50:58Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> On Thu, Mar 20, 2025 at 12:44:00PM +0100, Karthik Nayak wrote:\n>> diff --git a/refs.h b/refs.h\n>> index 240e2d8537..dcd83e81e2 100644\n>> --- a/refs.h\n>> +++ b/refs.h\n>> @@ -16,6 +16,29 @@ struct worktree;\n>>  enum ref_storage_format ref_storage_format_by_name(const char *name);\n>>  const char *ref_storage_format_to_name(enum ref_storage_format ref_storage_format);\n>>\n>> +/*\n>> + * enum ref_transaction_error represents the following return codes:\n>> + * REF_TRANSACTION_ERROR_GENERIC error_code: default error code.\n>> + * REF_TRANSACTION_ERROR_NAME_CONFLICT error_code: ref name conflict like A vs A/B.\n>> + * REF_TRANSACTION_ERROR_CREATE_EXISTS error_code: ref to be created already exists.\n>> + * REF_TRANSACTION_ERROR_NONEXISTENT_REF error_code: ref expected but doesn't exist.\n>> + * REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE error_code: provided old_oid or old_target of\n>> + * reference doesn't match actual.\n>> + * REF_TRANSACTION_ERROR_INVALID_NEW_VALUE error_code: provided new_oid or new_target is\n>> + * invalid.\n>> + * REF_TRANSACTION_ERROR_EXPECTED_SYMREF error_code: expected ref to be symref, but is a\n>> + * regular ref.\n>> + */\n>> +enum ref_transaction_error {\n>> +\tREF_TRANSACTION_ERROR_GENERIC = -1,\n>> +\tREF_TRANSACTION_ERROR_NAME_CONFLICT = -2,\n>> +\tREF_TRANSACTION_ERROR_CREATE_EXISTS = -3,\n>> +\tREF_TRANSACTION_ERROR_NONEXISTENT_REF = -4,\n>> +\tREF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE = -5,\n>> +\tREF_TRANSACTION_ERROR_INVALID_NEW_VALUE = -6,\n>> +\tREF_TRANSACTION_ERROR_EXPECTED_SYMREF = -7,\n>> +};\n>> +\n>\n> Tiny nit: I think it's generally preferable to document each specific\n> right next to its definition.\n>\n\nIdk about this, I based it off on `enum bisect_error` which is similar,\nbut I also see the same in `enum scld_error`.\n\nI'm okay to change this though, perhaps makes sense to document the\npreferred style however.\n\n> Patrick\n"},{"id":"514936","messageId":"CAOLa=ZQ7nYNMZ2rHBao=vf-HxpWOy6g_jywi0bU5nTf6Tx=F+g@mail.gmail.com","threadId":"63170","inReplyTo":"Z9x590KcwP8WGGG-@pks.im","subject":"Re: [PATCH v4 6/8] refs: implement batch reference update support","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2025-03-24T14:54:59Z","receivedAt":"2025-03-24T14:55:01Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> On Thu, Mar 20, 2025 at 12:44:01PM +0100, Karthik Nayak wrote:\n>> diff --git a/refs.c b/refs.c\n>> index 3d0b53d56e..b34ec198f5 100644\n>> --- a/refs.c\n>> +++ b/refs.c\n>> @@ -1206,11 +1210,45 @@ void ref_transaction_free(struct ref_transaction *transaction)\n>>  \t\tfree((char *)transaction->updates[i]->old_target);\n>>  \t\tfree(transaction->updates[i]);\n>>  \t}\n>> +\n>> +\tif (transaction->rejections)\n>> +\t\tfree(transaction->rejections->update_indices);\n>> +\tfree(transaction->rejections);\n>> +\n>>  \tstring_list_clear(&transaction->refnames, 0);\n>>  \tfree(transaction->updates);\n>>  \tfree(transaction);\n>>  }\n>>\n>> +int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction,\n>> +\t\t\t\t       size_t update_idx,\n>> +\t\t\t\t       enum ref_transaction_error err)\n>> +{\n>> +\tif (update_idx >= transaction->nr)\n>> +\t\tBUG(\"trying to set rejection on invalid update index\");\n>> +\n>> +\tif (!(transaction->flags & REF_TRANSACTION_ALLOW_FAILURE))\n>> +\t\treturn 0;\n>> +\n>> +\tif (!transaction->rejections)\n>> +\t\tBUG(\"transaction not inititalized with failure support\");\n>> +\n>> +\t/*\n>> +\t * Don't accept generic errors, since these errors are not user\n>> +\t * input related.\n>> +\t */\n>> +\tif (err == REF_TRANSACTION_ERROR_GENERIC)\n>> +\t\treturn 0;\n>> +\n>> +\ttransaction->updates[update_idx]->rejection_err = err;\n>> +\tALLOC_GROW(transaction->rejections->update_indices,\n>> +\t\t   transaction->rejections->nr + 1,\n>> +\t\t   transaction->rejections->alloc);\n>> +\ttransaction->rejections->update_indices[transaction->rejections->nr++] = update_idx;\n>> +\n>> +\treturn 1;\n>> +}\n>\n> If we had a `struct ref_update_rejection` we could store the update\n> index and rejection errors in the same location, which might be a bit\n> easier to reason about.\n>\n\nI struggled with this a bit, I was thinking the same at the start. But\nthen I was also thinking that a rejection is specific to an update so it\nshould lie in `ref_update`, however tracking failed updates in a\ntransaction is specific to a transaction and should lie in\n`ref_transaction`. It probably would be easier code-wise to have them\nboth in the same place, but it didn't feel like the correct place.\n\n>> diff --git a/refs/packed-backend.c b/refs/packed-backend.c\n>> index d90bd815a3..7bf57ca948 100644\n>> --- a/refs/packed-backend.c\n>> +++ b/refs/packed-backend.c\n>> @@ -1327,10 +1327,11 @@ static int packed_ref_store_remove_on_disk(struct ref_store *ref_store,\n>>   * remain locked when it is done.\n>>   */\n>>  static enum ref_transaction_error write_with_updates(struct packed_ref_store *refs,\n>> -\t\t\t\t\t\t     struct string_list *updates,\n>> +\t\t\t\t\t\t     struct ref_transaction *transaction,\n>>  \t\t\t\t\t\t     struct strbuf *err)\n>>  {\n>>  \tenum ref_transaction_error ret = REF_TRANSACTION_ERROR_GENERIC;\n>> +\tstruct string_list *updates = &transaction->refnames;\n>>  \tstruct ref_iterator *iter = NULL;\n>>  \tsize_t i;\n>>  \tint ok;\n>> @@ -1411,6 +1412,13 @@ static enum ref_transaction_error write_with_updates(struct packed_ref_store *re\n>>  \t\t\t\t\t\t    \"reference already exists\",\n>>  \t\t\t\t\t\t    update->refname);\n>>  \t\t\t\t\tret = REF_TRANSACTION_ERROR_CREATE_EXISTS;\n>> +\n>> +\t\t\t\t\tif (ref_transaction_maybe_set_rejected(transaction, i, ret)) {\n>> +\t\t\t\t\t\tstrbuf_setlen(err, 0);\n>\n> Nit: you can use `strbuf_reset()` for this and other instances.\n>\n\nYes, indeed, will change!\n\n> Patrick\n"},{"id":"514959","messageId":"CAOLa=ZRx8gt=_J2cRFEsUEi31ia7qNL7SZ-WBWuWSyU=8SRt+g@mail.gmail.com","threadId":"63170","inReplyTo":"Z-FZObjvIN-qzvlj@pks.im","subject":"Re: [PATCH v4 7/8] refs: support rejection in batch updates during F/D checks","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2025-03-24T17:48:32Z","receivedAt":"2025-03-24T17:48:34Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> On Thu, Mar 20, 2025 at 12:44:02PM +0100, Karthik Nayak wrote:\n>> diff --git a/refs.c b/refs.c\n>> index b34ec198f5..f719046f47 100644\n>> --- a/refs.c\n>> +++ b/refs.c\n>> @@ -2540,6 +2540,7 @@ enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs\n>>  \t\t\t\t\t  const struct string_list *refnames,\n>>  \t\t\t\t\t  const struct string_list *extras,\n>>  \t\t\t\t\t  const struct string_list *skip,\n>> +\t\t\t\t\t  struct ref_transaction *transaction,\n>>  \t\t\t\t\t  unsigned int initial_transaction,\n>>  \t\t\t\t\t  struct strbuf *err)\n>>  {\n>> @@ -2599,12 +2601,26 @@ enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs\n>>  \t\t\tif (!initial_transaction &&\n>>  \t\t\t    !refs_read_raw_ref(refs, dirname.buf, &oid, &referent,\n>>  \t\t\t\t\t       &type, &ignore_errno)) {\n>> +\t\t\t\tif (transaction && ref_transaction_maybe_set_rejected(\n>> +\t\t\t\t\t    transaction, *update_idx,\n>> +\t\t\t\t\t    REF_TRANSACTION_ERROR_NAME_CONFLICT)) {\n>> +\t\t\t\t\tstrset_remove(&dirnames, dirname.buf);\n>> +\t\t\t\t\tcontinue;\n>> +\t\t\t\t}\n>> +\n>\n> Okay. We have to remove the dirname from `dirnames` again so that the\n> next reference that creates a reference in the same directory would also\n> be marked as conflicting. It does have the consequence that we now have\n> to read the dirname N times again, where N is the number of refs that\n> are created below that directory.\n>\n> We could probably improve this by using another map that contains the\n> conflicting names, right?\n>\n\nYes that's definitely possible, I will go ahead and add it!\n\n>> diff --git a/refs/files-backend.c b/refs/files-backend.c\n>> index be758ffff5..1d50d4013c 100644\n>> --- a/refs/files-backend.c\n>> +++ b/refs/files-backend.c\n>> @@ -864,7 +868,9 @@ static enum ref_transaction_error lock_raw_ref(struct files_ref_store *refs,\n>>  \t\t * make sure there is no existing packed ref that conflicts\n>>  \t\t * with refname. This check is deferred so that we can batch it.\n>>  \t\t */\n>> -\t\tstring_list_append(refnames_to_check, refname);\n>> +\t\titem = string_list_append(refnames_to_check, refname);\n>> +\t\titem->util = xmalloc(sizeof(update_idx));\n>> +\t\tmemcpy(item->util, &update_idx, sizeof(update_idx));\n>>  \t}\n>>\n>>  \tret = 0;\n>\n> Hm, so we have to allocate the `util` field now to store the update\n> index, which is a bit unfortunate because all of this is part of the hot\n> loop. We cannot store a direct pointer though because the array of\n> updates may be reallocated, which would invalidate any pointers pointing\n> into the array.\n>\n\nYes, your inference is on point.\n\n> I was wondering whether we could abuse an `uintptr_t` and use it to\n> store the update index as a pointer. It does feel somewhat dirty though.\n>\n\nWe can do something like this, it would be _clever_ but does feel very\nhacky.\n\nI did so some benchmarking here\n\nBenchmark 1: update-ref: create many refs (refformat = files, refcount\n= 100000, revision = master)\n  Time (mean ± σ):      7.396 s ±  0.175 s    [User: 0.962 s, System: 6.312 s]\n  Range (min … max):    7.145 s …  7.688 s    10 runs\n\nBenchmark 2: update-ref: create many refs (refformat = files, refcount\n= 100000, revision = b4/245-partially-atomic-ref-updates)\n  Time (mean ± σ):      7.514 s ±  0.144 s    [User: 0.919 s, System: 6.438 s]\n  Range (min … max):    7.297 s …  7.750 s    10 runs\n\nSummary\n  update-ref: create many refs (refformat = files, refcount = 100000,\nrevision = master) ran\n    1.02 ± 0.03 times faster than update-ref: create many refs\n(refformat = files, refcount = 100000, revision =\nb4/245-partially-atomic-ref-updates)\n\nOverall the perf degradation is very minimal. I also looked at the\nflamegraph to see if there is something. But most of the time seems to\nbe spent in IO (reading refs and creating locks).\n\nSo I think we should be ok here, wdyt?\n\n> Patrick\n"},{"id":"514960","messageId":"CAOLa=ZSjETz71mmeULYy74djOLZWfsixNCcAfONrAteHjdDdVA@mail.gmail.com","threadId":"63170","inReplyTo":"Z-FZQOYGPIuB2h-t@pks.im","subject":"Re: [PATCH v4 8/8] update-ref: add --batch-updates flag for stdin mode","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2025-03-24T17:51:20Z","receivedAt":"2025-03-24T17:51:21Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> On Thu, Mar 20, 2025 at 12:44:03PM +0100, Karthik Nayak wrote:\n>> diff --git a/Documentation/git-update-ref.adoc b/Documentation/git-update-ref.adoc\n>> index 9e6935d38d..5be2c16776 100644\n>> --- a/Documentation/git-update-ref.adoc\n>> +++ b/Documentation/git-update-ref.adoc\n>> @@ -57,6 +59,14 @@ performs all modifications together.  Specify commands of the form:\n>>  With `--create-reflog`, update-ref will create a reflog for each ref\n>>  even if one would not ordinarily be created.\n>>\n>> +With `--batch-updates`, update-ref executes the updates in a batch but allows\n>> +individual updates to fail due to invalid or incorrect user input, applying only\n>> +the successful updates. However, system-related errors—such as I/O failures or\n>> +memory issues—will result in a full failure of all batched updates. Any failed\n>> +updates will be reported in the following format:\n>> +\n>> +\trejected SP (<old-oid> | <old-target>) SP (<new-oid> | <new-target>) SP <rejection-reason> LF\n>> +\n>\n> Does this support NUL-terminated mode? It probably should, and if it\n> does we should also document the format.\n>\n\nIt only does for inputs. So there is nothing to be done for outputs.\nI actually added support for '-z' mode here, but there was an assumption\non my part that this is for both input/output.\n\nPhillip corrected my assumption in the first version of this series [1].\n\n[1]: https://lore.kernel.org/all/ceda422e-8c8e-4a1d-aaab-9a7a2fc009dd@gmail.com/\n\n>> diff --git a/builtin/update-ref.c b/builtin/update-ref.c\n>> index 1d541e13ad..97e14b279e 100644\n>> --- a/builtin/update-ref.c\n>> +++ b/builtin/update-ref.c\n>> @@ -735,6 +787,8 @@ int cmd_update_ref(int argc,\n>>  \t\tOPT_BOOL('z', NULL, &end_null, N_(\"stdin has NUL-terminated arguments\")),\n>>  \t\tOPT_BOOL( 0 , \"stdin\", &read_stdin, N_(\"read updates from stdin\")),\n>>  \t\tOPT_BOOL( 0 , \"create-reflog\", &create_reflog, N_(\"create a reflog\")),\n>> +\t\tOPT_BIT('0', \"batch-updates\", &flags, N_(\"batch reference updates\"),\n>> +\t\t\tREF_TRANSACTION_ALLOW_FAILURE),\n>>  \t\tOPT_END(),\n>>  \t};\n>>\n>> @@ -756,9 +810,10 @@ int cmd_update_ref(int argc,\n>>  \t\t\tusage_with_options(git_update_ref_usage, options);\n>>  \t\tif (end_null)\n>>  \t\t\tline_termination = '\\0';\n>> -\t\tupdate_refs_stdin();\n>> +\t\tupdate_refs_stdin(flags);\n>>  \t\treturn 0;\n>> -\t}\n>> +\t} else if (flags & REF_TRANSACTION_ALLOW_FAILURE)\n>> +\t\tdie(\"--batch-updates can only be used with --stdin\");\n>\n> Nit: formatting, the `else if` branch should have curly braces.\n\nAh! Thanks, will change!\n\n> Patrick\n"},{"id":"514994","messageId":"Z-KiEsx4ry3CX5eJ@pks.im","threadId":"63170","inReplyTo":"CAOLa=ZRM8EeeVXpZFpLU7Wg8k98_U+0hw1y63gQDjPEfR2P2cw@mail.gmail.com","subject":"Re: [PATCH v4 5/8] refs: introduce enum-based transaction error types","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-03-25T12:31:14Z","receivedAt":"2025-03-25T12:31:18Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Mon, Mar 24, 2025 at 02:50:56PM +0000, Karthik Nayak wrote:\n> Patrick Steinhardt <ps@pks.im> writes:\n> \n> > On Thu, Mar 20, 2025 at 12:44:00PM +0100, Karthik Nayak wrote:\n> >> diff --git a/refs.h b/refs.h\n> >> index 240e2d8537..dcd83e81e2 100644\n> >> --- a/refs.h\n> >> +++ b/refs.h\n> >> @@ -16,6 +16,29 @@ struct worktree;\n> >>  enum ref_storage_format ref_storage_format_by_name(const char *name);\n> >>  const char *ref_storage_format_to_name(enum ref_storage_format ref_storage_format);\n> >>\n> >> +/*\n> >> + * enum ref_transaction_error represents the following return codes:\n> >> + * REF_TRANSACTION_ERROR_GENERIC error_code: default error code.\n> >> + * REF_TRANSACTION_ERROR_NAME_CONFLICT error_code: ref name conflict like A vs A/B.\n> >> + * REF_TRANSACTION_ERROR_CREATE_EXISTS error_code: ref to be created already exists.\n> >> + * REF_TRANSACTION_ERROR_NONEXISTENT_REF error_code: ref expected but doesn't exist.\n> >> + * REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE error_code: provided old_oid or old_target of\n> >> + * reference doesn't match actual.\n> >> + * REF_TRANSACTION_ERROR_INVALID_NEW_VALUE error_code: provided new_oid or new_target is\n> >> + * invalid.\n> >> + * REF_TRANSACTION_ERROR_EXPECTED_SYMREF error_code: expected ref to be symref, but is a\n> >> + * regular ref.\n> >> + */\n> >> +enum ref_transaction_error {\n> >> +\tREF_TRANSACTION_ERROR_GENERIC = -1,\n> >> +\tREF_TRANSACTION_ERROR_NAME_CONFLICT = -2,\n> >> +\tREF_TRANSACTION_ERROR_CREATE_EXISTS = -3,\n> >> +\tREF_TRANSACTION_ERROR_NONEXISTENT_REF = -4,\n> >> +\tREF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE = -5,\n> >> +\tREF_TRANSACTION_ERROR_INVALID_NEW_VALUE = -6,\n> >> +\tREF_TRANSACTION_ERROR_EXPECTED_SYMREF = -7,\n> >> +};\n> >> +\n> >\n> > Tiny nit: I think it's generally preferable to document each specific\n> > right next to its definition.\n> >\n> \n> Idk about this, I based it off on `enum bisect_error` which is similar,\n> but I also see the same in `enum scld_error`.\n> \n> I'm okay to change this though, perhaps makes sense to document the\n> preferred style however.\n\nProbably would make sense, yes. The argument for why it's preferable to\ndo this inline is that it's harder for the list to grow stale. It's\ntrivial to see when a comment needs to be removed/updated.\n\nPatrick\n"},{"id":"514995","messageId":"Z-KiFeN0LXTR36lM@pks.im","threadId":"63170","inReplyTo":"CAOLa=ZRx8gt=_J2cRFEsUEi31ia7qNL7SZ-WBWuWSyU=8SRt+g@mail.gmail.com","subject":"Re: [PATCH v4 7/8] refs: support rejection in batch updates during F/D checks","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-03-25T12:31:17Z","receivedAt":"2025-03-25T12:31:20Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Mon, Mar 24, 2025 at 05:48:32PM +0000, Karthik Nayak wrote:\n> Patrick Steinhardt <ps@pks.im> writes:\n> > On Thu, Mar 20, 2025 at 12:44:02PM +0100, Karthik Nayak wrote:\n> >> diff --git a/refs/files-backend.c b/refs/files-backend.c\n> >> index be758ffff5..1d50d4013c 100644\n> >> --- a/refs/files-backend.c\n> >> +++ b/refs/files-backend.c\n> >> @@ -864,7 +868,9 @@ static enum ref_transaction_error lock_raw_ref(struct files_ref_store *refs,\n> >>  \t\t * make sure there is no existing packed ref that conflicts\n> >>  \t\t * with refname. This check is deferred so that we can batch it.\n> >>  \t\t */\n> >> -\t\tstring_list_append(refnames_to_check, refname);\n> >> +\t\titem = string_list_append(refnames_to_check, refname);\n> >> +\t\titem->util = xmalloc(sizeof(update_idx));\n> >> +\t\tmemcpy(item->util, &update_idx, sizeof(update_idx));\n> >>  \t}\n> >>\n> >>  \tret = 0;\n> >\n> > Hm, so we have to allocate the `util` field now to store the update\n> > index, which is a bit unfortunate because all of this is part of the hot\n> > loop. We cannot store a direct pointer though because the array of\n> > updates may be reallocated, which would invalidate any pointers pointing\n> > into the array.\n> >\n> \n> Yes, your inference is on point.\n> \n> > I was wondering whether we could abuse an `uintptr_t` and use it to\n> > store the update index as a pointer. It does feel somewhat dirty though.\n> >\n> \n> We can do something like this, it would be _clever_ but does feel very\n> hacky.\n> \n> I did so some benchmarking here\n> \n> Benchmark 1: update-ref: create many refs (refformat = files, refcount\n> = 100000, revision = master)\n>   Time (mean ± σ):      7.396 s ±  0.175 s    [User: 0.962 s, System: 6.312 s]\n>   Range (min … max):    7.145 s …  7.688 s    10 runs\n> \n> Benchmark 2: update-ref: create many refs (refformat = files, refcount\n> = 100000, revision = b4/245-partially-atomic-ref-updates)\n>   Time (mean ± σ):      7.514 s ±  0.144 s    [User: 0.919 s, System: 6.438 s]\n>   Range (min … max):    7.297 s …  7.750 s    10 runs\n> \n> Summary\n>   update-ref: create many refs (refformat = files, refcount = 100000,\n> revision = master) ran\n>     1.02 ± 0.03 times faster than update-ref: create many refs\n> (refformat = files, refcount = 100000, revision =\n> b4/245-partially-atomic-ref-updates)\n> \n> Overall the perf degradation is very minimal. I also looked at the\n> flamegraph to see if there is something. But most of the time seems to\n> be spent in IO (reading refs and creating locks).\n> \n> So I think we should be ok here, wdyt?\n\nOkay. I'm not a huge fan of using the `->util` pointer like this, but I\ndon't have a better idea either, and the performance regression seems\nacceptable. So let's roll with it until somebody has a better idea.\n\nThanks for doing the benchmark!\n\nPatrick\n"}]}