git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 2/3] credential: move each subcommand into its own function

From
WEWasswa Edson <edsonwasswa54@gmail.com>
Date
Nov 14, 2026, 18:47 UTC
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 <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
Previous: Wasswa EdsonNext: Wasswa Edson
Message 5 of 6 in “credential: parse subcommands with OPT_SUBCOMMAND”
  1. credential: parse subcommands with OPT_SUBCOMMANDWasswa Edson, Nov 4, 2026
  2. Tanga JorisNov 8, 2026
  3. 0/3 credential: parse subcommands with OPT_SUBCOMMANDWasswa Edson, Nov 14, 2026
  4. 1/3 credential: make -h output and SYNOPSIS agreeWasswa Edson, Nov 14, 2026
  5. 2/3 credential: move each subcommand into its own functionWasswa Edson, Nov 14, 2026
  6. 3/3 credential: parse subcommands with OPT_SUBCOMMANDWasswa Edson, Nov 14, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.