From patchwork Thu Apr 2 07:09:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 11470187 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 38CB41874 for ; Thu, 2 Apr 2020 07:09:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0D10220784 for ; Thu, 2 Apr 2020 07:09:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="aW3KugoF"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="XEmbdwRe" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387502AbgDBHJf (ORCPT ); Thu, 2 Apr 2020 03:09:35 -0400 Received: from out3-smtp.messagingengine.com ([66.111.4.27]:47495 "EHLO out3-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387447AbgDBHJf (ORCPT ); Thu, 2 Apr 2020 03:09:35 -0400 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 291B25C0281; Thu, 2 Apr 2020 03:09:34 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Thu, 02 Apr 2020 03:09:34 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=date :from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm3; bh=yM9X7I4cWYrAHEkC6K+XmEnQGrD DWu/uule9ZBR7Oa8=; b=aW3KugoFs6DJDM0IFJPCGwAciiU66LWyGHmLKO8MJWv /pU/WcpP9at/SS3BwFTFy0XnQZqQwxIWi3omtUngq5TJqI098w3bWYEobj53z94C CcdaYTFPGzjQpLDxm+iwe596dOCdnYKAZv76QXqeb3etoYim2HxjQog8jikPi/rm b4QY1cUwg4384yZwIt4qwIJRwrl/Hn/TwI9bYu8f+PxaHjlgs0jKvnNx5G/Rl5RI SSW/p/INJsQEY6YrEZQUdOVFNFMl2zgjAL/e8AT3zJePu+BzPHliqxKlHueJYfga tRqzatGsco33Fq6DjtAMPVqUfpwz8ymZsGvsFwrKJgQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=yM9X7I 4cWYrAHEkC6K+XmEnQGrDDWu/uule9ZBR7Oa8=; b=XEmbdwReoZubDZJ66/E2Er VZXgf8v1yv77bfxid8/hvMKseo+jUdkvTCuzk/3XBBmjLOCpo+3NeAcWr2OuNfEv yT0ejOdtUlZpRCwnEWpV6UQcUdh6nPukVWhcxLo10dpRUi71q6P1/aZb4HXq2h7q U4d9xVR91dl+pe3Rh9OpKuYIBJ0yU7z3iSJ69Zv3+Wj1YJ1wyuudAVORdkAgZ4U0 ljqWPWLJGynT1V61cqBsCsKXs92YoKpSDEP3gQG2z+Pdhyg+mrN/DOiP4VFTjyvB WuBm2txQqS07qofTGByyEMZc69m2uc3o/AwbkjCVzu9YiTcW1KAIk3z3w54yRRtw == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduhedrtdefgdduudeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvffukfhfgggtuggjsehgtderredttddvnecuhfhrohhmpefrrghtrhhi tghkucfuthgvihhnhhgrrhguthcuoehpshesphhkshdrihhmqeenucfkphepkeelrdduvd drleegrddvkeenucevlhhushhtvghrufhiiigvpeegnecurfgrrhgrmhepmhgrihhlfhhr ohhmpehpshesphhkshdrihhm X-ME-Proxy: Received: from vm-mail.pks.im (x590c5e1c.dyn.telefonica.de [89.12.94.28]) by mail.messagingengine.com (Postfix) with ESMTPA id 6F973306CD75; Thu, 2 Apr 2020 03:09:33 -0400 (EDT) Received: from localhost (ncase [10.192.0.11]) by vm-mail.pks.im (OpenSMTPD) with ESMTPSA id e063f603 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 2 Apr 2020 07:09:31 +0000 (UTC) Date: Thu, 2 Apr 2020 09:09:38 +0200 From: Patrick Steinhardt To: git Cc: Christian Couder , Junio C Hamano Subject: [PATCH v3 4/9] update-ref: organize commands in an array Message-ID: <0f881d49366fe9d9806b08af35319475ca07abc3.1585811013.git.ps@pks.im> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org We currently manually wire up all commands known to `git-update-ref --stdin`, making it harder than necessary to preprocess arguments after the command is determined. To make this more extensible, let's refactor the code to use an array of known commands instead. While this doesn't add a lot of value now, it is a preparatory step to implement line-wise reading of commands. As we're going to introduce commands without trailing spaces, this commit also moves whitespace parsing into the respective commands. Signed-off-by: Patrick Steinhardt --- builtin/update-ref.c | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/builtin/update-ref.c b/builtin/update-ref.c index 2d8f7f0578..a6ff88b95b 100644 --- a/builtin/update-ref.c +++ b/builtin/update-ref.c @@ -310,7 +310,8 @@ static const char *parse_cmd_verify(struct ref_transaction *transaction, return next; } -static const char *parse_cmd_option(struct strbuf *input, const char *next) +static const char *parse_cmd_option(struct ref_transaction *transaction, + struct strbuf *input, const char *next) { const char *rest; if (skip_prefix(next, "no-deref", &rest) && *rest == line_termination) @@ -320,33 +321,49 @@ static const char *parse_cmd_option(struct strbuf *input, const char *next) return rest; } +static const struct parse_cmd { + const char *prefix; + const char *(*fn)(struct ref_transaction *, struct strbuf *, const char *); +} command[] = { + { "update", parse_cmd_update }, + { "create", parse_cmd_create }, + { "delete", parse_cmd_delete }, + { "verify", parse_cmd_verify }, + { "option", parse_cmd_option }, +}; + static void update_refs_stdin(struct ref_transaction *transaction) { struct strbuf input = STRBUF_INIT; const char *next; + int i; if (strbuf_read(&input, 0, 1000) < 0) die_errno("could not read from stdin"); next = input.buf; /* Read each line dispatch its command */ while (next < input.buf + input.len) { + const struct parse_cmd *cmd = NULL; + if (*next == line_termination) die("empty command in input"); else if (isspace(*next)) die("whitespace before command: %s", next); - 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 + + for (i = 0; i < ARRAY_SIZE(command); i++) { + const char *prefix = command[i].prefix; + + if (!skip_prefix(next, prefix, &next) || + !skip_prefix(next, " ", &next)) + continue; + + cmd = &command[i]; + break; + } + if (!cmd) die("unknown command: %s", next); + next = cmd->fn(transaction, &input, next); next++; }