{"thread":{"id":"48637","subject":"[PATCH] update-ref --stdin: use skip_prefix()","startedAt":"2018-06-03T14:36:58Z","lastAt":"2018-06-04T03:40:00Z","messageCount":2,"participants":["SZEDER Gábor","Jeff King"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"349147","messageId":"20180603143651.6844-1-szeder.dev@gmail.com","threadId":"48637","inReplyTo":null,"subject":"[PATCH] update-ref --stdin: use skip_prefix()","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2018-06-03T14:36:51Z","receivedAt":"2018-06-03T14:36:58Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"Use skip_prefix() instead of starts_with() and strcmp() when parsing\n'git update-ref's stdin to avoid a couple of magic numbers.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n builtin/update-ref.c | 25 +++++++++++++------------\n 1 file changed, 13 insertions(+), 12 deletions(-)\n\ndiff --git a/builtin/update-ref.c b/builtin/update-ref.c\nindex 4b4714b3fd..4fa3c0a86f 100644\n--- a/builtin/update-ref.c\n+++ b/builtin/update-ref.c\n@@ -311,11 +311,12 @@ static const char *parse_cmd_verify(struct ref_transaction *transaction,\n \n static const char *parse_cmd_option(struct strbuf *input, const char *next)\n {\n-\tif (!strncmp(next, \"no-deref\", 8) && next[8] == line_termination)\n+\tconst char *rest;\n+\tif (skip_prefix(next, \"no-deref\", &rest) && *rest == line_termination)\n \t\tupdate_flags |= REF_NO_DEREF;\n \telse\n \t\tdie(\"option unknown: %s\", next);\n-\treturn next + 8;\n+\treturn rest;\n }\n \n static void update_refs_stdin(struct ref_transaction *transaction)\n@@ -332,16 +333,16 @@ static void update_refs_stdin(struct ref_transaction *transaction)\n \t\t\tdie(\"empty command in input\");\n \t\telse if (isspace(*next))\n \t\t\tdie(\"whitespace before command: %s\", next);\n-\t\telse if (starts_with(next, \"update \"))\n-\t\t\tnext = parse_cmd_update(transaction, &input, next + 7);\n-\t\telse if (starts_with(next, \"create \"))\n-\t\t\tnext = parse_cmd_create(transaction, &input, next + 7);\n-\t\telse if (starts_with(next, \"delete \"))\n-\t\t\tnext = parse_cmd_delete(transaction, &input, next + 7);\n-\t\telse if (starts_with(next, \"verify \"))\n-\t\t\tnext = parse_cmd_verify(transaction, &input, next + 7);\n-\t\telse if (starts_with(next, \"option \"))\n-\t\t\tnext = parse_cmd_option(&input, next + 7);\n+\t\telse if (skip_prefix(next, \"update \", &next))\n+\t\t\tnext = parse_cmd_update(transaction, &input, next);\n+\t\telse if (skip_prefix(next, \"create \", &next))\n+\t\t\tnext = parse_cmd_create(transaction, &input, next);\n+\t\telse if (skip_prefix(next, \"delete \", &next))\n+\t\t\tnext = parse_cmd_delete(transaction, &input, next);\n+\t\telse if (skip_prefix(next, \"verify \", &next))\n+\t\t\tnext = parse_cmd_verify(transaction, &input, next);\n+\t\telse if (skip_prefix(next, \"option \", &next))\n+\t\t\tnext = parse_cmd_option(&input, next);\n \t\telse\n \t\t\tdie(\"unknown command: %s\", next);\n \n-- \n2.18.0.rc0.207.ga6211da864\n\n"},{"id":"349207","messageId":"20180604033955.GB14451@sigill.intra.peff.net","threadId":"48637","inReplyTo":"20180603143651.6844-1-szeder.dev@gmail.com","subject":"Re: [PATCH] update-ref --stdin: use skip_prefix()","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2018-06-04T03:39:55Z","receivedAt":"2018-06-04T03:40:00Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Sun, Jun 03, 2018 at 04:36:51PM +0200, SZEDER Gábor wrote:\n\n> Use skip_prefix() instead of starts_with() and strcmp() when parsing\n> 'git update-ref's stdin to avoid a couple of magic numbers.\n\nI was coincidentally looking at this the other day also noticed these.\nThanks for cleaning it up (and your patch looks obviously correct).\n\nI also found it funny that we read the whole input into a buffer and\nparse from there, rather than using strbuf_getline(). But that's\nintentional due to e23d84350a (update-ref --stdin: read the whole input\nat once, 2014-04-07). I think the line-oriented protocol actually can be\neasily read like that, but the \"-z\" format ends up having to do awkward\nreads.\n\nAnyway, sort of a tangent, but I didn't want anybody else looking at\nthis having to dig down the same hole I did. ;)\n\n-Peff\n"}]}