Skip to content

Commit a65b8ac

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bd021f3 commit a65b8ac

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

builtin/update-ref.c

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ static const char *parse_cmd_verify(struct ref_transaction *transaction,
310310
return next;
311311
}
312312

313-
static const char *parse_cmd_option(struct strbuf *input, const char *next)
313+
static const char *parse_cmd_option(struct ref_transaction *transaction,
314+
struct strbuf *input, const char *next)
314315
{
315316
const char *rest;
316317
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)
320321
return rest;
321322
}
322323

324+
static const struct parse_cmd {
325+
const char *prefix;
326+
const char *(*fn)(struct ref_transaction *, struct strbuf *, const char *);
327+
} command[] = {
328+
{ "update", parse_cmd_update },
329+
{ "create", parse_cmd_create },
330+
{ "delete", parse_cmd_delete },
331+
{ "verify", parse_cmd_verify },
332+
{ "option", parse_cmd_option },
333+
};
334+
323335
static void update_refs_stdin(struct ref_transaction *transaction)
324336
{
325337
struct strbuf input = STRBUF_INIT;
326338
const char *next;
339+
int i;
327340

328341
if (strbuf_read(&input, 0, 1000) < 0)
329342
die_errno("could not read from stdin");
330343
next = input.buf;
331344
/* Read each line dispatch its command */
332345
while (next < input.buf + input.len) {
346+
const struct parse_cmd *cmd = NULL;
347+
333348
if (*next == line_termination)
334349
die("empty command in input");
335350
else if (isspace(*next))
336351
die("whitespace before command: %s", next);
337-
else if (skip_prefix(next, "update ", &next))
338-
next = parse_cmd_update(transaction, &input, next);
339-
else if (skip_prefix(next, "create ", &next))
340-
next = parse_cmd_create(transaction, &input, next);
341-
else if (skip_prefix(next, "delete ", &next))
342-
next = parse_cmd_delete(transaction, &input, next);
343-
else if (skip_prefix(next, "verify ", &next))
344-
next = parse_cmd_verify(transaction, &input, next);
345-
else if (skip_prefix(next, "option ", &next))
346-
next = parse_cmd_option(&input, next);
347-
else
352+
353+
for (i = 0; i < ARRAY_SIZE(command); i++) {
354+
const char *prefix = command[i].prefix;
355+
356+
if (!skip_prefix(next, prefix, &next) ||
357+
!skip_prefix(next, " ", &next))
358+
continue;
359+
360+
cmd = &command[i];
361+
break;
362+
}
363+
if (!cmd)
348364
die("unknown command: %s", next);
349365

366+
next = cmd->fn(transaction, &input, next);
350367
next++;
351368
}
352369

0 commit comments

Comments
 (0)