From: Wasswa Edson Date: Sat, 14 Nov 2026 18:47:51 GMT Subject: [PATCH v2 2/3] credential: move each subcommand into its own function Message-ID: <20261114184752.1417-3-edsonwasswa54@gmail.com> In-Reply-To: <20261114184752.1417-1-edsonwasswa54@gmail.com> cmd_credential() handles all of its subcommands inline, sharing a single "struct credential" and dispatching with a chain of strcmp() calls. Move each subcommand into its own function with the signature expected by parse-options' subcommand callbacks, in preparation for switching the dispatch to OPT_SUBCOMMAND() in the next commit. The only change in behavior is that an unknown subcommand is now rejected before reading a credential from the standard input. Signed-off-by: Wasswa Edson --- builtin/credential.c | 102 +++++++++++++++++++++++++++++++------------ 1 file changed, 75 insertions(+), 27 deletions(-) diff --git a/builtin/credential.c b/builtin/credential.c index 72e7b2af4e..d6ba21a2cb 100644 --- a/builtin/credential.c +++ b/builtin/credential.c @@ -9,13 +9,75 @@ static const char usage_msg[] = "git credential (fill|approve|reject|capability)"; +static int cmd_credential_fill(int argc UNUSED, const char **argv UNUSED, + const char *prefix UNUSED, + struct repository *repo UNUSED) +{ + struct credential c = CREDENTIAL_INIT; + + if (credential_read(&c, stdin, CREDENTIAL_OP_INITIAL) < 0) + die("unable to read credential from stdin"); + + credential_fill(the_repository, &c, 0); + credential_next_state(&c); + credential_write(&c, stdout, CREDENTIAL_OP_RESPONSE); + + credential_clear(&c); + return 0; +} + +static int cmd_credential_approve(int argc UNUSED, const char **argv UNUSED, + const char *prefix UNUSED, + struct repository *repo UNUSED) +{ + struct credential c = CREDENTIAL_INIT; + + if (credential_read(&c, stdin, CREDENTIAL_OP_INITIAL) < 0) + die("unable to read credential from stdin"); + + credential_set_all_capabilities(&c, CREDENTIAL_OP_HELPER); + credential_approve(the_repository, &c); + + credential_clear(&c); + return 0; +} + +static int cmd_credential_reject(int argc UNUSED, const char **argv UNUSED, + const char *prefix UNUSED, + struct repository *repo UNUSED) +{ + struct credential c = CREDENTIAL_INIT; + + if (credential_read(&c, stdin, CREDENTIAL_OP_INITIAL) < 0) + die("unable to read credential from stdin"); + + credential_set_all_capabilities(&c, CREDENTIAL_OP_HELPER); + credential_reject(the_repository, &c); + + credential_clear(&c); + return 0; +} + +static int cmd_credential_capability(int argc UNUSED, + const char **argv UNUSED, + const char *prefix UNUSED, + struct repository *repo UNUSED) +{ + struct credential c = CREDENTIAL_INIT; + + credential_set_all_capabilities(&c, CREDENTIAL_OP_INITIAL); + credential_announce_capabilities(&c, stdout); + + credential_clear(&c); + return 0; +} + int cmd_credential(int argc, const char **argv, - const char *prefix UNUSED, - struct repository *repo UNUSED) + const char *prefix, + struct repository *repo) { const char *op; - struct credential c = CREDENTIAL_INIT; repo_config(the_repository, git_default_config, NULL); @@ -24,29 +86,15 @@ int cmd_credential(int argc, usage(usage_msg); op = argv[1]; - if (!strcmp(op, "capability")) { - credential_set_all_capabilities(&c, CREDENTIAL_OP_INITIAL); - credential_announce_capabilities(&c, stdout); - return 0; - } - - if (credential_read(&c, stdin, CREDENTIAL_OP_INITIAL) < 0) - die("unable to read credential from stdin"); - - if (!strcmp(op, "fill")) { - credential_fill(the_repository, &c, 0); - credential_next_state(&c); - credential_write(&c, stdout, CREDENTIAL_OP_RESPONSE); - } else if (!strcmp(op, "approve")) { - credential_set_all_capabilities(&c, CREDENTIAL_OP_HELPER); - credential_approve(the_repository, &c); - } else if (!strcmp(op, "reject")) { - credential_set_all_capabilities(&c, CREDENTIAL_OP_HELPER); - credential_reject(the_repository, &c); - } else { - usage(usage_msg); - } + if (!strcmp(op, "fill")) + return cmd_credential_fill(argc - 1, argv + 1, prefix, repo); + if (!strcmp(op, "approve")) + return cmd_credential_approve(argc - 1, argv + 1, prefix, repo); + if (!strcmp(op, "reject")) + return cmd_credential_reject(argc - 1, argv + 1, prefix, repo); + if (!strcmp(op, "capability")) + return cmd_credential_capability(argc - 1, argv + 1, prefix, + repo); - credential_clear(&c); - return 0; + usage(usage_msg); } -- 2.53.0