git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] update-ref --stdin: use skip_prefix()
@ 2018-06-03 14:36 SZEDER Gábor
  2018-06-04  3:39 ` Jeff King
  0 siblings, 1 reply; 2+ messages in thread
From: SZEDER Gábor @ 2018-06-03 14:36 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, SZEDER Gábor

Use skip_prefix() instead of starts_with() and strcmp() when parsing
'git update-ref's stdin to avoid a couple of magic numbers.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 builtin/update-ref.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/builtin/update-ref.c b/builtin/update-ref.c
index 4b4714b3fd..4fa3c0a86f 100644
--- a/builtin/update-ref.c
+++ b/builtin/update-ref.c
@@ -311,11 +311,12 @@ static const char *parse_cmd_verify(struct ref_transaction *transaction,
 
 static const char *parse_cmd_option(struct strbuf *input, const char *next)
 {
-	if (!strncmp(next, "no-deref", 8) && next[8] == line_termination)
+	const char *rest;
+	if (skip_prefix(next, "no-deref", &rest) && *rest == line_termination)
 		update_flags |= REF_NO_DEREF;
 	else
 		die("option unknown: %s", next);
-	return next + 8;
+	return rest;
 }
 
 static void update_refs_stdin(struct ref_transaction *transaction)
@@ -332,16 +333,16 @@ static void update_refs_stdin(struct ref_transaction *transaction)
 			die("empty command in input");
 		else if (isspace(*next))
 			die("whitespace before command: %s", next);
-		else if (starts_with(next, "update "))
-			next = parse_cmd_update(transaction, &input, next + 7);
-		else if (starts_with(next, "create "))
-			next = parse_cmd_create(transaction, &input, next + 7);
-		else if (starts_with(next, "delete "))
-			next = parse_cmd_delete(transaction, &input, next + 7);
-		else if (starts_with(next, "verify "))
-			next = parse_cmd_verify(transaction, &input, next + 7);
-		else if (starts_with(next, "option "))
-			next = parse_cmd_option(&input, next + 7);
+		else if (skip_prefix(next, "update ", &next))
+			next = parse_cmd_update(transaction, &input, next);
+		else if (skip_prefix(next, "create ", &next))
+			next = parse_cmd_create(transaction, &input, next);
+		else if (skip_prefix(next, "delete ", &next))
+			next = parse_cmd_delete(transaction, &input, next);
+		else if (skip_prefix(next, "verify ", &next))
+			next = parse_cmd_verify(transaction, &input, next);
+		else if (skip_prefix(next, "option ", &next))
+			next = parse_cmd_option(&input, next);
 		else
 			die("unknown command: %s", next);
 
-- 
2.18.0.rc0.207.ga6211da864


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] update-ref --stdin: use skip_prefix()
  2018-06-03 14:36 [PATCH] update-ref --stdin: use skip_prefix() SZEDER Gábor
@ 2018-06-04  3:39 ` Jeff King
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff King @ 2018-06-04  3:39 UTC (permalink / raw)
  To: SZEDER Gábor; +Cc: Junio C Hamano, git

On Sun, Jun 03, 2018 at 04:36:51PM +0200, SZEDER Gábor wrote:

> Use skip_prefix() instead of starts_with() and strcmp() when parsing
> 'git update-ref's stdin to avoid a couple of magic numbers.

I was coincidentally looking at this the other day also noticed these.
Thanks for cleaning it up (and your patch looks obviously correct).

I also found it funny that we read the whole input into a buffer and
parse from there, rather than using strbuf_getline(). But that's
intentional due to e23d84350a (update-ref --stdin: read the whole input
at once, 2014-04-07). I think the line-oriented protocol actually can be
easily read like that, but the "-z" format ends up having to do awkward
reads.

Anyway, sort of a tangent, but I didn't want anybody else looking at
this having to dig down the same hole I did. ;)

-Peff

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-06-04  3:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-03 14:36 [PATCH] update-ref --stdin: use skip_prefix() SZEDER Gábor
2018-06-04  3:39 ` Jeff King

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).