summaryrefslogtreecommitdiff
path: root/builtin
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2020-04-02 07:09:38 (GMT)
committerJunio C Hamano <gitster@pobox.com>2020-04-02 18:09:48 (GMT)
commita65b8ac291d919c8f0c89f7397e03662020344ab (patch)
treea3b21e6052938c734d40e9ec5232db31d4a376d3 /builtin
parentbd021f39103fab4c5ae53a4d1a1756f8970d00db (diff)
downloadgit-a65b8ac291d919c8f0c89f7397e03662020344ab.zip
git-a65b8ac291d919c8f0c89f7397e03662020344ab.tar.gz
git-a65b8ac291d919c8f0c89f7397e03662020344ab.tar.bz2
update-ref: organize commands in an array
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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin')
-rw-r--r--builtin/update-ref.c41
1 files changed, 29 insertions, 12 deletions
diff --git a/builtin/update-ref.c b/builtin/update-ref.c
index 2d8f7f0..a6ff88b 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++;
}