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

[PATCH v4 6/9] update-ref: handle rejections while adding updates

From
Karthik Nayak <karthik.188@gmail.com>
Date
May 4, 2026, 17:44 UTC
Message-ID
<20260504-refs-move-to-generic-layer-v4-6-936ac2f0b1a3@gmail.com>
In-Reply-To
<20260504-refs-move-to-generic-layer-v4-0-936ac2f0b1a3@gmail.com>

When using git-update-ref(1) with the '--batch-updates' flag, updates rejected by the reference backend are displayed to the user while other updates are applied. This only applies during the commit phase of the transaction.

In the following commits, we'll also extend `ref_transaction_update()` to reject updates before a transaction is prepared/committed. In preparation, modify the code in update-ref to also handle non-generic rejections from `ref_transaction_update()`. This involves propagating information to each of the commands on whether updates are allowed to be rejected, and also checking for rejections and only dying for generic failures.

Errors encountered during updates will be shown to the user immediately unlike other errors encountered only when the transaction is prepared/committed. As the verification of object IDs and peeled tag objects will move into `ref_transaction_update()` in the following commit, this means that those errors will be shown to the user before other errors, this changes the order of errors, but the functionality remains the same.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 builtin/update-ref.c | 137 ++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 98 insertions(+), 39 deletions(-)
diff --git a/builtin/update-ref.c b/builtin/update-ref.c
index 5259cc7226..6355c3dd3e 100644
--- a/builtin/update-ref.c
+++ b/builtin/update-ref.c
@@ -25,6 +25,15 @@ static unsigned int default_flags;
 static unsigned create_reflog_flag;
 static const char *msg;
 
+struct command_options {
+	/*
+	 * Individual updates are allowed to fail without causing
+	 * update-ref to exit. This is set when using the
+	 * '--batch-updates' flag.
+	 */
+	bool allow_update_failures;
+};
+
 /*
  * Parse one whitespace- or NUL-terminated, possibly C-quoted argument
  * and append the result to arg.  Return a pointer to the terminator.
@@ -257,6 +266,31 @@ static void print_rejected_refs(const char *refname,
 	strbuf_release(&sb);
 }
 
+/*
+ * Handle transaction errors. If we're using batches updates, we want to only
+ * die for generic errors and print the remaining to the user.
+ */
+static void handle_ref_transaction_error(const char *refname,
+					 struct object_id *new_oid,
+					 struct object_id *old_oid,
+					 const char *new_target,
+					 const char *old_target,
+					 enum ref_transaction_error tx_err,
+					 struct strbuf *err,
+					 struct command_options *opts)
+{
+	if (!tx_err)
+		return;
+
+	if (tx_err != REF_TRANSACTION_ERROR_GENERIC && opts->allow_update_failures) {
+		print_rejected_refs(refname, old_oid, new_oid, old_target,
+				    new_target, tx_err, err->buf, NULL);
+		return;
+	}
+
+	die("%s", err->buf);
+}
+
 /*
  * The following five parse_cmd_*() functions parse the corresponding
  * command.  In each case, next points at the character following the
@@ -268,11 +302,13 @@ static void print_rejected_refs(const char *refname,
  */
 
 static void parse_cmd_update(struct ref_transaction *transaction,
-			     const char *next, const char *end)
+			     const char *next, const char *end,
+			     struct command_options *opts)
 {
 	struct strbuf err = STRBUF_INIT;
 	char *refname;
 	struct object_id new_oid, old_oid;
+	enum ref_transaction_error tx_err;
 	int have_old;
 
 	refname = parse_refname(&next);
@@ -289,12 +325,14 @@ static void parse_cmd_update(struct ref_transaction *transaction,
 	if (*next != line_termination)
 		die("update %s: extra input: %s", refname, next);
 
-	if (ref_transaction_update(transaction, refname,
-				   &new_oid, have_old ? &old_oid : NULL,
-				   NULL, NULL,
-				   update_flags | create_reflog_flag,
-				   msg, &err))
-		die("%s", err.buf);
+	tx_err = ref_transaction_update(transaction, refname,
+					&new_oid, have_old ? &old_oid : NULL,
+					NULL, NULL,
+					update_flags | create_reflog_flag,
+					msg, &err);
+	handle_ref_transaction_error(refname, &new_oid, have_old ? &old_oid : NULL,
+				     NULL, NULL, tx_err, &err, opts);
+
 
 	update_flags = default_flags;
 	free(refname);
@@ -302,9 +340,11 @@ static void parse_cmd_update(struct ref_transaction *transaction,
 }
 
 static void parse_cmd_symref_update(struct ref_transaction *transaction,
-				    const char *next, const char *end UNUSED)
+				    const char *next, const char *end UNUSED,
+				    struct command_options *opts)
 {
 	char *refname, *new_target, *old_arg;
+	enum ref_transaction_error tx_err;
 	char *old_target = NULL;
 	struct strbuf err = STRBUF_INIT;
 	struct object_id old_oid;
@@ -341,13 +381,15 @@ static void parse_cmd_symref_update(struct ref_transaction *transaction,
 	if (*next != line_termination)
 		die("symref-update %s: extra input: %s", refname, next);
 
-	if (ref_transaction_update(transaction, refname, NULL,
-				   have_old_oid ? &old_oid : NULL,
-				   new_target,
-				   have_old_oid ? NULL : old_target,
-				   update_flags | create_reflog_flag,
-				   msg, &err))
-		die("%s", err.buf);
+	tx_err = ref_transaction_update(transaction, refname, NULL,
+					have_old_oid ? &old_oid : NULL,
+					new_target,
+					have_old_oid ? NULL : old_target,
+					update_flags | create_reflog_flag,
+					msg, &err);
+	handle_ref_transaction_error(refname, NULL, have_old_oid ? &old_oid : NULL,
+				     new_target, have_old_oid ? NULL : old_target,
+				     tx_err, &err, opts);
 
 	update_flags = default_flags;
 	free(refname);
@@ -358,11 +400,13 @@ static void parse_cmd_symref_update(struct ref_transaction *transaction,
 }
 
 static void parse_cmd_create(struct ref_transaction *transaction,
-			     const char *next, const char *end)
+			     const char *next, const char *end,
+			     struct command_options *opts)
 {
 	struct strbuf err = STRBUF_INIT;
 	char *refname;
 	struct object_id new_oid;
+	enum ref_transaction_error tx_err;
 
 	refname = parse_refname(&next);
 	if (!refname)
@@ -377,22 +421,24 @@ static void parse_cmd_create(struct ref_transaction *transaction,
 	if (*next != line_termination)
 		die("create %s: extra input: %s", refname, next);
 
-	if (ref_transaction_create(transaction, refname, &new_oid, NULL,
-				   update_flags | create_reflog_flag,
-				   msg, &err))
-		die("%s", err.buf);
+	tx_err = ref_transaction_create(transaction, refname, &new_oid, NULL,
+					update_flags | create_reflog_flag,
+					msg, &err);
+	handle_ref_transaction_error(refname, &new_oid, NULL, NULL, NULL, tx_err,
+				     &err, opts);
 
 	update_flags = default_flags;
 	free(refname);
 	strbuf_release(&err);
 }
 
-
 static void parse_cmd_symref_create(struct ref_transaction *transaction,
-				    const char *next, const char *end UNUSED)
+				    const char *next, const char *end UNUSED,
+				    struct command_options *opts)
 {
 	struct strbuf err = STRBUF_INIT;
 	char *refname, *new_target;
+	enum ref_transaction_error tx_err;
 
 	refname = parse_refname(&next);
 	if (!refname)
@@ -405,10 +451,11 @@ static void parse_cmd_symref_create(struct ref_transaction *transaction,
 	if (*next != line_termination)
 		die("symref-create %s: extra input: %s", refname, next);
 
-	if (ref_transaction_create(transaction, refname, NULL, new_target,
-				   update_flags | create_reflog_flag,
-				   msg, &err))
-		die("%s", err.buf);
+	tx_err = ref_transaction_create(transaction, refname, NULL, new_target,
+					update_flags | create_reflog_flag,
+					msg, &err);
+	handle_ref_transaction_error(refname, NULL, NULL, new_target, NULL,
+				     tx_err, &err, opts);
 
 	update_flags = default_flags;
 	free(refname);
@@ -417,7 +464,8 @@ static void parse_cmd_symref_create(struct ref_transaction *transaction,
 }
 
 static void parse_cmd_delete(struct ref_transaction *transaction,
-			     const char *next, const char *end)
+			     const char *next, const char *end,
+			     struct command_options *opts UNUSED)
 {
 	struct strbuf err = STRBUF_INIT;
 	char *refname;
@@ -450,9 +498,9 @@ static void parse_cmd_delete(struct ref_transaction *transaction,
 	strbuf_release(&err);
 }
 
-
 static void parse_cmd_symref_delete(struct ref_transaction *transaction,
-				    const char *next, const char *end UNUSED)
+				    const char *next, const char *end UNUSED,
+				    struct command_options *opts UNUSED)
 {
 	struct strbuf err = STRBUF_INIT;
 	char *refname, *old_target;
@@ -479,9 +527,9 @@ static void parse_cmd_symref_delete(struct ref_transaction *transaction,
 	strbuf_release(&err);
 }
 
-
 static void parse_cmd_verify(struct ref_transaction *transaction,
-			     const char *next, const char *end)
+			     const char *next, const char *end,
+			     struct command_options *opts UNUSED)
 {
 	struct strbuf err = STRBUF_INIT;
 	char *refname;
@@ -508,7 +556,8 @@ static void parse_cmd_verify(struct ref_transaction *transaction,
 }
 
 static void parse_cmd_symref_verify(struct ref_transaction *transaction,
-				    const char *next, const char *end UNUSED)
+				    const char *next, const char *end UNUSED,
+				    struct command_options *opts UNUSED)
 {
 	struct strbuf err = STRBUF_INIT;
 	struct object_id old_oid;
@@ -550,7 +599,8 @@ static void report_ok(const char *command)
 }
 
 static void parse_cmd_option(struct ref_transaction *transaction UNUSED,
-			     const char *next, const char *end UNUSED)
+			     const char *next, const char *end UNUSED,
+			     struct command_options *opts UNUSED)
 {
 	const char *rest;
 	if (skip_prefix(next, "no-deref", &rest) && *rest == line_termination)
@@ -560,7 +610,8 @@ static void parse_cmd_option(struct ref_transaction *transaction UNUSED,
 }
 
 static void parse_cmd_start(struct ref_transaction *transaction UNUSED,
-			    const char *next, const char *end UNUSED)
+			    const char *next, const char *end UNUSED,
+			    struct command_options *opts UNUSED)
 {
 	if (*next != line_termination)
 		die("start: extra input: %s", next);
@@ -568,7 +619,8 @@ static void parse_cmd_start(struct ref_transaction *transaction UNUSED,
 }
 
 static void parse_cmd_prepare(struct ref_transaction *transaction,
-			      const char *next, const char *end UNUSED)
+			      const char *next, const char *end UNUSED,
+			      struct command_options *opts UNUSED)
 {
 	struct strbuf error = STRBUF_INIT;
 	if (*next != line_termination)
@@ -579,7 +631,8 @@ static void parse_cmd_prepare(struct ref_transaction *transaction,
 }
 
 static void parse_cmd_abort(struct ref_transaction *transaction,
-			    const char *next, const char *end UNUSED)
+			    const char *next, const char *end UNUSED,
+			    struct command_options *opts UNUSED)
 {
 	struct strbuf error = STRBUF_INIT;
 	if (*next != line_termination)
@@ -590,7 +643,8 @@ static void parse_cmd_abort(struct ref_transaction *transaction,
 }
 
 static void parse_cmd_commit(struct ref_transaction *transaction,
-			     const char *next, const char *end UNUSED)
+			     const char *next, const char *end UNUSED,
+			     struct command_options *opts UNUSED)
 {
 	struct strbuf error = STRBUF_INIT;
 	if (*next != line_termination)
@@ -618,7 +672,8 @@ enum update_refs_state {
 
 static const struct parse_cmd {
 	const char *prefix;
-	void (*fn)(struct ref_transaction *, const char *, const char *);
+	void (*fn)(struct ref_transaction *, const char *, const char *,
+		   struct command_options *);
 	unsigned args;
 	enum update_refs_state state;
 } command[] = {
@@ -644,6 +699,10 @@ static void update_refs_stdin(unsigned int flags)
 	struct ref_transaction *transaction;
 	int i, j;
 
+	struct command_options opts = {
+		.allow_update_failures = flags & REF_TRANSACTION_ALLOW_FAILURE,
+	};
+
 	transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
 						  flags, &err);
 	if (!transaction)
@@ -721,7 +780,7 @@ static void update_refs_stdin(unsigned int flags)
 		}
 
 		cmd->fn(transaction, input.buf + strlen(cmd->prefix) + !!cmd->args,
-			input.buf + input.len);
+			input.buf + input.len, &opts);
 	}
 
 	switch (state) {
-- 
2.53.GIT
Previous: Karthik NayakNext: Patrick Steinhardt
Message 62 of 68 in “refs: move some of the generic logic out of the backends”
  1. 0/8 refs: move some of the generic logic out of the backendsKarthik Nayak, Apr 20, 2026
  2. 1/8 refs: remove unused typedef 'ref_transaction_commit_fn'Karthik Nayak, Apr 20, 2026
  3. Patrick SteinhardtApr 22, 2026
  4. Karthik NayakApr 22, 2026
  5. 2/8 refs: extract out reflog config to generic layerKarthik Nayak, Apr 20, 2026
  6. Patrick SteinhardtApr 22, 2026
  7. Karthik NayakApr 22, 2026
  8. 3/8 refs: return `ref_transaction_error` from `ref_transaction_update()`Karthik Nayak, Apr 20, 2026
  9. Patrick SteinhardtApr 22, 2026
  10. Karthik NayakApr 22, 2026
  11. 4/8 update-ref: move `print_rejected_refs()` upKarthik Nayak, Apr 20, 2026
  12. Patrick SteinhardtApr 22, 2026
  13. Karthik NayakApr 22, 2026
  14. 5/8 update-ref: handle rejections while adding updatesKarthik Nayak, Apr 20, 2026
  15. Patrick SteinhardtApr 22, 2026
  16. Karthik NayakApr 22, 2026
  17. 6/8 refs: move object parsing to the generic layerKarthik Nayak, Apr 20, 2026
  18. Patrick SteinhardtApr 22, 2026
  19. Karthik NayakApr 22, 2026
  20. 8/8 refs: use peeled tag values in reference backendsKarthik Nayak, Apr 20, 2026
  21. 7/8 refs: add peeled object ID to the `ref_update` structKarthik Nayak, Apr 20, 2026
  22. 0/9 refs: move some of the generic logic out of the backendsKarthik Nayak, Apr 23, 2026
  23. 1/9 refs: remove unused typedef 'ref_transaction_commit_fn'Karthik Nayak, Apr 23, 2026
  24. 2/9 refs: introduce `ref_store_init_options`Karthik Nayak, Apr 23, 2026
  25. Patrick SteinhardtApr 23, 2026
  26. Karthik NayakApr 24, 2026
  27. 3/9 refs: extract out reflog config to generic layerKarthik Nayak, Apr 23, 2026
  28. Patrick SteinhardtApr 23, 2026
  29. 4/9 refs: return `ref_transaction_error` from `ref_transaction_update()`Karthik Nayak, Apr 23, 2026
  30. Toon ClaesApr 24, 2026
  31. 5/9 update-ref: move `print_rejected_refs()` upKarthik Nayak, Apr 23, 2026
  32. 6/9 update-ref: handle rejections while adding updatesKarthik Nayak, Apr 23, 2026
  33. Patrick SteinhardtApr 23, 2026
  34. Karthik NayakApr 24, 2026
  35. Toon ClaesApr 24, 2026
  36. Karthik NayakApr 27, 2026
  37. 7/9 refs: move object parsing to the generic layerKarthik Nayak, Apr 23, 2026
  38. Toon ClaesApr 24, 2026
  39. Karthik NayakApr 27, 2026
  40. 8/9 refs: add peeled object ID to the `ref_update` structKarthik Nayak, Apr 23, 2026
  41. Toon ClaesApr 24, 2026
  42. Karthik NayakApr 27, 2026
  43. 9/9 refs: use peeled tag values in reference backendsKarthik Nayak, Apr 23, 2026
  44. 0/9 refs: move some of the generic logic out of the backendsKarthik Nayak, Apr 27, 2026
  45. 1/9 refs: remove unused typedef 'ref_transaction_commit_fn'Karthik Nayak, Apr 27, 2026
  46. 2/9 refs: introduce `ref_store_init_options`Karthik Nayak, Apr 27, 2026
  47. 3/9 refs: extract out reflog config to generic layerKarthik Nayak, Apr 27, 2026
  48. 4/9 refs: return `ref_transaction_error` from `ref_transaction_update()`Karthik Nayak, Apr 27, 2026
  49. 5/9 update-ref: move `print_rejected_refs()` upKarthik Nayak, Apr 27, 2026
  50. 6/9 update-ref: handle rejections while adding updatesKarthik Nayak, Apr 27, 2026
  51. Toon ClaesApr 29, 2026
  52. Karthik NayakApr 30, 2026
  53. 7/9 refs: move object parsing to the generic layerKarthik Nayak, Apr 27, 2026
  54. 8/9 refs: add peeled object ID to the `ref_update` structKarthik Nayak, Apr 27, 2026
  55. 9/9 refs: use peeled tag values in reference backendsKarthik Nayak, Apr 27, 2026
  56. 0/9 refs: move some of the generic logic out of the backendsKarthik Nayak, May 4, 2026
  57. 1/9 refs: remove unused typedef 'ref_transaction_commit_fn'Karthik Nayak, May 4, 2026
  58. 2/9 refs: introduce `ref_store_init_options`Karthik Nayak, May 4, 2026
  59. 3/9 refs: extract out reflog config to generic layerKarthik Nayak, May 4, 2026
  60. 4/9 refs: return `ref_transaction_error` from `ref_transaction_update()`Karthik Nayak, May 4, 2026
  61. 5/9 update-ref: move `print_rejected_refs()` upKarthik Nayak, May 4, 2026
  62. 6/9 update-ref: handle rejections while adding updatesKarthik Nayak, May 4, 2026
  63. Patrick SteinhardtMay 5, 2026
  64. Karthik NayakMay 5, 2026
  65. Toon ClaesMay 6, 2026
  66. 7/9 refs: move object parsing to the generic layerKarthik Nayak, May 4, 2026
  67. 8/9 refs: add peeled object ID to the `ref_update` structKarthik Nayak, May 4, 2026
  68. 9/9 refs: use peeled tag values in reference backendsKarthik Nayak, May 4, 2026

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

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