Git development
 help / color / mirror / Atom feed
From: Wasswa Edson <edsonwasswa54@gmail.com>
To: git@vger.kernel.org
Cc: tangajoris@gmail.com, Wasswa Edson <edsonwasswa54@gmail.com>
Subject: [PATCH v2 2/3] credential: move each subcommand into its own function
Date: Sat, 14 Nov 2026 21:47:51 +0300	[thread overview]
Message-ID: <20261114184752.1417-3-edsonwasswa54@gmail.com> (raw)
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 <edsonwasswa54@gmail.com>
---
 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


  parent reply	other threads:[~2026-11-14 18:48 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-04 20:22 [PATCH] credential: parse subcommands with OPT_SUBCOMMAND Wasswa Edson
2026-11-08 22:28 ` Tanga Joris
2026-11-14 18:47 ` [PATCH v2 0/3] " Wasswa Edson
2026-11-14 18:47   ` [PATCH v2 1/3] credential: make -h output and SYNOPSIS agree Wasswa Edson
2026-11-14 18:47   ` Wasswa Edson [this message]
2026-11-14 18:47   ` [PATCH v2 3/3] credential: parse subcommands with OPT_SUBCOMMAND Wasswa Edson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261114184752.1417-3-edsonwasswa54@gmail.com \
    --to=edsonwasswa54@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=tangajoris@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox