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

[PATCH v2 7/8] update-ref: support multiple simultaneous updates

From
Brad King <brad.king@kitware.com>
Date
Aug 30, 2013, 18:12 UTC
Message-ID
<ba564b6566b54d780a24355ca893294d814d8d24.1377885441.git.brad.king@kitware.com>
In-Reply-To
<cover.1377885441.git.brad.king@kitware.com>

Add a --stdin signature to read update instructions from standard input and apply multiple ref updates together. Use an input format that supports any update that could be specified via the command-line, including object names like 'branch:path with space'.

Signed-off-by: Brad King <brad.king@kitware.com>
---
 Documentation/git-update-ref.txt |   21 ++++++-
 builtin/update-ref.c             |  121 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 140 insertions(+), 2 deletions(-)
diff --git a/Documentation/git-update-ref.txt b/Documentation/git-update-ref.txt
index 0df13ff..295d0bb 100644
--- a/Documentation/git-update-ref.txt
+++ b/Documentation/git-update-ref.txt
@@ -8,7 +8,7 @@ git-update-ref - Update the object name stored in a ref safely
 SYNOPSIS
 --------
 [verse]
-'git update-ref' [-m <reason>] (-d <ref> [<oldvalue>] | [--no-deref] <ref> <newvalue> [<oldvalue>])
+'git update-ref' [-m <reason>] (-d <ref> [<oldvalue>] | [--no-deref] <ref> <newvalue> [<oldvalue>] | --stdin)
 
 DESCRIPTION
 -----------
@@ -58,6 +58,25 @@ archive by creating a symlink tree).
 With `-d` flag, it deletes the named <ref> after verifying it
 still contains <oldvalue>.
 
+With `--stdin`, update-ref reads instructions from standard input and
+performs all modifications together.  Empty lines are ignored.
+Each non-empty line is parsed as whitespace-separated arguments.
+Use single-quotes to enclose whitespace and backslashes and an
+unquoted backslash to escape a single quote.  Specify updates with
+lines of the form:
+
+	[--no-deref] [--] <ref> <newvalue> [<oldvalue>]
+
+Lines of any other format or a repeated <ref> produce an error.
+Specify a zero <newvalue> to delete a ref and/or a zero <oldvalue>
+to make sure that a ref not exist.  Use either 40 "0" or the
+empty string (written as '') to specify a zero value.
+
+If all <ref>s can be locked with matching <oldvalue>s
+simultaneously all modifications are performed.  Otherwise, no
+modifications are performed.  Note that while each individual
+<ref> is updated or deleted atomically, a concurrent reader may
+still see a subset of the modifications.
 
 Logging Updates
 ---------------
diff --git a/builtin/update-ref.c b/builtin/update-ref.c
index 51d2684..eb8db85 100644
--- a/builtin/update-ref.c
+++ b/builtin/update-ref.c
@@ -6,19 +6,129 @@
 static const char * const git_update_ref_usage[] = {
 	N_("git update-ref [options] -d <refname> [<oldval>]"),
 	N_("git update-ref [options]    <refname> <newval> [<oldval>]"),
+	N_("git update-ref [options] --stdin"),
 	NULL
 };
 
+static const char blank[] = " \t\r\n";
+
+static int updates_size;
+static int updates_count;
+static struct ref_update *updates;
+
+static const char* update_refs_stdin_next_arg(const char* next,
+					      struct strbuf *arg)
+{
+	/* Skip leading whitespace: */
+	while (isspace(*next))
+		++next;
+
+	/* Return NULL when no argument is found: */
+	if (!*next)
+		return NULL;
+
+	/* Parse the argument: */
+	strbuf_reset(arg);
+	for (;;) {
+		char c = *next;
+		if (!c || isspace(c))
+			break;
+		++next;
+		if (c == '\'') {
+			size_t len = strcspn(next, "'");
+			if (!next[len])
+				die("unterminated single-quote: '%s", next);
+			strbuf_add(arg, next, len);
+			next += len + 1;
+			continue;
+		}
+		if (c == '\\') {
+			if (*next == '\'')
+				c = *next++;
+			else
+				die("unquoted backslash not escaping "
+				    "single-quote: \\%s", next);
+		}
+		strbuf_addch(arg, c);
+	}
+	return next;
+}
+
+static void update_refs_stdin(const char *line)
+{
+	int options = 1, flags = 0, argc = 0;
+	char *argv[3] = {0, 0, 0};
+	struct strbuf arg = STRBUF_INIT;
+	struct ref_update *update;
+	const char *next = line;
+
+	/* Skip blank lines: */
+	if (!line[0])
+		return;
+
+	/* Parse arguments on this line: */
+	while ((next = update_refs_stdin_next_arg(next, &arg)) != NULL) {
+		if (options && arg.buf[0] == '-')
+			if (!strcmp(arg.buf, "--no-deref"))
+				flags |= REF_NODEREF;
+			else if (!strcmp(arg.buf, "--"))
+				options = 0;
+			else
+				die("unknown option %s", arg.buf);
+		else if (argc >= 3)
+			die("too many arguments on line: %s", line);
+		else {
+			argv[argc++] = xstrdup(arg.buf);
+			options = 0;
+		}
+	}
+	strbuf_release(&arg);
+
+	/* Allocate and zero-init a struct ref_update: */
+	if (updates_count == updates_size) {
+		updates_size = updates_size ? (updates_size * 2) : 16;
+		updates = xrealloc(updates, sizeof(*updates) * updates_size);
+		memset(updates + updates_count, 0,
+		       sizeof(*updates) * (updates_size - updates_count));
+	}
+	update = &updates[updates_count++];
+	update->flags = flags;
+
+	/* Set the update ref_name: */
+	if (!argv[0])
+		die("no ref on line: %s", line);
+	if (check_refname_format(argv[0], REFNAME_ALLOW_ONELEVEL))
+		die("invalid ref format on line: %s", line);
+	update->ref_name = argv[0];
+	argv[0] = 0;
+
+	/* Set the update new_sha1 and, if specified, old_sha1: */
+	if (!argv[1])
+		die("missing new value on line: %s", line);
+	if (*argv[1] && get_sha1(argv[1], update->new_sha1))
+		die("invalid new value on line: %s", line);
+	if (argv[2]) {
+		update->have_old = 1;
+		if (*argv[2] && get_sha1(argv[2], update->old_sha1))
+			die("invalid old value on line: %s", line);
+	}
+
+	while (argc > 0)
+		free(argv[--argc]);
+}
+
 int cmd_update_ref(int argc, const char **argv, const char *prefix)
 {
 	const char *refname, *oldval, *msg = NULL;
 	unsigned char sha1[20], oldsha1[20];
-	int delete = 0, no_deref = 0, flags = 0;
+	int delete = 0, no_deref = 0, read_stdin = 0, flags = 0;
+	struct strbuf line = STRBUF_INIT;
 	struct option options[] = {
 		OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")),
 		OPT_BOOLEAN('d', NULL, &delete, N_("delete the reference")),
 		OPT_BOOLEAN( 0 , "no-deref", &no_deref,
 					N_("update <refname> not the one it points to")),
+		OPT_BOOLEAN( 0 , "stdin", &read_stdin, N_("read updates from stdin")),
 		OPT_END(),
 	};
 
@@ -28,6 +138,15 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 	if (msg && !*msg)
 		die("Refusing to perform update with empty message.");
 
+	if (read_stdin) {
+		if (delete || no_deref || argc > 0)
+			usage_with_options(git_update_ref_usage, options);
+		while (strbuf_getline(&line, stdin, '\n') != EOF)
+			update_refs_stdin(line.buf);
+		strbuf_release(&line);
+		return update_refs(msg, updates, updates_count, DIE_ON_ERR);
+	}
+
 	if (delete) {
 		if (argc < 1 || argc > 2)
 			usage_with_options(git_update_ref_usage, options);
-- 
1.7.10.4
Previous: Brad KingNext: Junio C Hamano
Message 47 of 106 in “Multiple simultaneously locked ref updates”
  1. 0/7 Multiple simultaneously locked ref updatesBrad King, Aug 29, 2013
  2. 1/7 reset: rename update_refs to reset_refsBrad King, Aug 29, 2013
  3. Junio C HamanoAug 29, 2013
  4. Brad KingAug 29, 2013
  5. 2/7 refs: report ref type from lock_any_ref_for_updateBrad King, Aug 29, 2013
  6. Junio C HamanoAug 29, 2013
  7. Brad KingAug 29, 2013
  8. 3/7 refs: factor update_ref steps into helpersBrad King, Aug 29, 2013
  9. 4/7 refs: factor delete_ref loose ref step into a helperBrad King, Aug 29, 2013
  10. Junio C HamanoAug 29, 2013
  11. Brad KingAug 29, 2013
  12. 5/7 refs: add function to repack without multiple refsBrad King, Aug 29, 2013
  13. Junio C HamanoAug 29, 2013
  14. Brad KingAug 29, 2013
  15. 6/7 refs: add update_refs for multiple simultaneous updatesBrad King, Aug 29, 2013
  16. Junio C HamanoAug 29, 2013
  17. Brad KingAug 29, 2013
  18. Junio C HamanoAug 29, 2013
  19. Brad KingAug 29, 2013
  20. Brad KingAug 29, 2013
  21. 7/7 update-ref: support multiple simultaneous updatesBrad King, Aug 29, 2013
  22. Junio C HamanoAug 29, 2013
  23. Brad KingAug 29, 2013
  24. Martin FickAug 29, 2013
  25. Brad KingAug 29, 2013
  26. Junio C HamanoAug 29, 2013
  27. Brad KingAug 29, 2013
  28. Junio C HamanoAug 29, 2013
  29. Brad KingAug 29, 2013
  30. 0/8 Multiple simultaneously locked ref updatesBrad King, Aug 30, 2013
  31. 1/8 reset: rename update_refs to reset_refsBrad King, Aug 30, 2013
  32. 2/8 refs: report ref type from lock_any_ref_for_updateBrad King, Aug 30, 2013
  33. 3/8 refs: factor update_ref steps into helpersBrad King, Aug 30, 2013
  34. Junio C HamanoSep 1, 2013
  35. Brad KingSep 2, 2013
  36. 4/8 refs: factor delete_ref loose ref step into a helperBrad King, Aug 30, 2013
  37. Michael HaggertyAug 31, 2013
  38. Brad KingSep 2, 2013
  39. 5/8 refs: add function to repack without multiple refsBrad King, Aug 30, 2013
  40. 6/8 refs: add update_refs for multiple simultaneous updatesBrad King, Aug 30, 2013
  41. Michael HaggertyAug 31, 2013
  42. Brad KingSep 2, 2013
  43. Junio C HamanoSep 1, 2013
  44. Brad KingSep 2, 2013
  45. Michael HaggertySep 3, 2013
  46. Brad KingSep 3, 2013
  47. 7/8 update-ref: support multiple simultaneous updatesBrad King, Aug 30, 2013
  48. Junio C HamanoAug 30, 2013
  49. Brad KingSep 2, 2013
  50. Michael HaggertyAug 31, 2013
  51. Brad KingSep 2, 2013
  52. 8/8 update-ref: add test cases covering --stdin signatureBrad King, Aug 30, 2013
  53. Eric SunshineSep 1, 2013
  54. Brad KingSep 2, 2013
  55. Michael HaggertyAug 31, 2013
  56. 0/8 Multiple simultaneously locked ref updatesBrad King, Sep 2, 2013
  57. 1/8 reset: rename update_refs to reset_refsBrad King, Sep 2, 2013
  58. 2/8 refs: report ref type from lock_any_ref_for_updateBrad King, Sep 2, 2013
  59. 3/8 refs: factor update_ref steps into helpersBrad King, Sep 2, 2013
  60. 4/8 refs: factor delete_ref loose ref step into a helperBrad King, Sep 2, 2013
  61. 5/8 refs: add function to repack without multiple refsBrad King, Sep 2, 2013
  62. 6/8 refs: add update_refs for multiple simultaneous updatesBrad King, Sep 2, 2013
  63. 7/8 update-ref: support multiple simultaneous updatesBrad King, Sep 2, 2013
  64. Brad KingSep 2, 2013
  65. 8/8 update-ref: add test cases covering --stdin signatureBrad King, Sep 2, 2013
  66. Eric SunshineSep 3, 2013
  67. Brad KingSep 3, 2013
  68. 0/8 Multiple simultaneously locked ref updatesBrad King, Sep 4, 2013
  69. 1/8 reset: rename update_refs to reset_refsBrad King, Sep 4, 2013
  70. 2/8 refs: report ref type from lock_any_ref_for_updateBrad King, Sep 4, 2013
  71. 3/8 refs: factor update_ref steps into helpersBrad King, Sep 4, 2013
  72. 4/8 refs: factor delete_ref loose ref step into a helperBrad King, Sep 4, 2013
  73. 5/8 refs: add function to repack without multiple refsBrad King, Sep 4, 2013
  74. 6/8 refs: add update_refs for multiple simultaneous updatesBrad King, Sep 4, 2013
  75. 7/8 update-ref: support multiple simultaneous updatesBrad King, Sep 4, 2013
  76. Junio C HamanoSep 4, 2013
  77. Brad KingSep 4, 2013
  78. Junio C HamanoSep 4, 2013
  79. Brad KingSep 5, 2013
  80. Junio C HamanoSep 5, 2013
  81. Brad KingSep 5, 2013
  82. Junio C HamanoSep 4, 2013
  83. Brad KingSep 4, 2013
  84. 8/8 update-ref: add test cases covering --stdin signatureBrad King, Sep 4, 2013
  85. 0/8 Multiple simultaneously locked ref updatesBrad King, Sep 9, 2013
  86. 7/8 update-ref: support multiple simultaneous updatesBrad King, Sep 9, 2013
  87. 8/8 update-ref: add test cases covering --stdin signatureBrad King, Sep 9, 2013
  88. 0/8 Multiple simultaneously locked ref updatesBrad King, Sep 10, 2013
  89. 1/8 reset: rename update_refs to reset_refsBrad King, Sep 10, 2013
  90. Ramkumar RamachandraSep 10, 2013
  91. 2/8 refs: report ref type from lock_any_ref_for_updateBrad King, Sep 10, 2013
  92. 3/8 refs: factor update_ref steps into helpersBrad King, Sep 10, 2013
  93. 4/8 refs: factor delete_ref loose ref step into a helperBrad King, Sep 10, 2013
  94. 5/8 refs: add function to repack without multiple refsBrad King, Sep 10, 2013
  95. 6/8 refs: add update_refs for multiple simultaneous updatesBrad King, Sep 10, 2013
  96. 7/8 update-ref: support multiple simultaneous updatesBrad King, Sep 10, 2013
  97. Eric SunshineSep 10, 2013
  98. Brad KingSep 11, 2013
  99. Eric SunshineSep 11, 2013
  100. 8/8 update-ref: add test cases covering --stdin signatureBrad King, Sep 10, 2013
  101. Eric SunshineSep 10, 2013
  102. Junio C HamanoSep 10, 2013
  103. 8/8 update-ref: add test cases covering --stdin signatureBrad King, Sep 11, 2013
  104. Junio C HamanoSep 10, 2013
  105. Brad KingSep 10, 2013
  106. Junio C HamanoSep 10, 2013

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.