credential: parse subcommands with OPT_SUBCOMMAND
PATCH 1/1 credential: parse subcommands with OPT_SUBCOMMAND
"git credential" dispatches its four subcommands (fill, approve, reject, capability) with a strcmp() chain after an argc check, instead of using the parse-options API used by other builtins.
Convert the dispatch to OPT_SUBCOMMAND(). Besides making the code shorter and more idiomatic, each subcommand gets its own usage string, so "git credential <subcommand> -h" shows a targeted help text, and the subcommand names are reported via --git-completion-helper.
Since the builtin now uses parse-options, drop NO_PARSEOPT from its git.c entry so "git --list-cmds=parseopt" includes it.
Two pre-existing inconsistencies are fixed while at it:
- The usage string did not mention "capability" even though it is a documented subcommand; it now lists all four.
- Documentation/git-credential.adoc marked its SYNOPSIS as a plain literal block, which t0450 cannot extract; mark it [verse] and drop "credential" from t0450/adoc-help-mismatches.
Behavior changes are limited to error paths: a missing or unknown subcommand is reported as such ("need a subcommand" / "unknown subcommand") before the usage text, instead of only showing the usage text, and "git credential <subcommand> -h" exits 0 after showing the subcommand's usage. Exit codes for failures are unchanged (129).
--- Documentation/git-credential.adoc | 3 +- builtin/credential.c | 148 ++++++++++++++++++++++++------ git.c | 2 +- t/t0300-credentials.sh | 18 ++++ t/t0450/adoc-help-mismatches | 1 - 5 files changed, 138 insertions(+), 34 deletions(-)
Documentation/git-credential.adoc +1 -2
@@ -7,9 +7,8 @@ git-credential - Retrieve and store user credentials SYNOPSIS -------- ------------------- +[verse] 'git credential' (fill|approve|reject|capability) ------------------- DESCRIPTION -----------
builtin/credential.c +118 -30
166 lines not on this page · raw message
git.c +1 -1
@@ -562,7 +562,7 @@ static struct cmd_struct commands[] = { { "commit-tree", cmd_commit_tree, RUN_SETUP }, { "config", cmd_config, RUN_SETUP_GENTLY | DELAY_PAGER_CONFIG }, { "count-objects", cmd_count_objects, RUN_SETUP }, - { "credential", cmd_credential, RUN_SETUP_GENTLY | NO_PARSEOPT }, + { "credential", cmd_credential, RUN_SETUP_GENTLY }, { "credential-cache", cmd_credential_cache }, { "credential-cache--daemon", cmd_credential_cache_daemon }, { "credential-store", cmd_credential_store },
t/t0300-credentials.sh +18 -0
@@ -1056,4 +1056,22 @@ test_expect_success 'interactive prompt is sanitized' ' EOF ' +test_expect_success 'credential subcommands -h show subcommand usage' ' + for sub in fill approve reject capability + do + git credential $sub -h >out && + test_grep "^usage: git credential $sub" out || return 1 + done +' + +test_expect_success 'credential requires a subcommand' ' + test_expect_code 129 git credential 2>err && + test_grep "need a subcommand" err +' + +test_expect_success 'credential rejects unknown subcommand' ' + test_expect_code 129 git credential foo 2>err && + test_grep "unknown subcommand" err +' + test_done
t/t0450/adoc-help-mismatches +0 -1
@@ -9,7 +9,6 @@ checkout checkout-index clone column -credential credential-cache credential-store fetch-pack
Signature · 1 line
2.53.0
Hi Edson,
Thanks for working on this, nice work adding subcommands completion for "git credential".
Quoted 2 lines · Wasswa Edson
Besides making the code
shorter and more idiomatic,
builtin/credential.c goes from 52 lines to 140, so maybe "shorter" could go.
Overall, the patch looks good, but most of the new lines are the same parse_options() call and check, repeated four times. Maybe a small helper would work here?
builtin/credential.c line 45
Quoted 2 lines
builtin/credential.c +2
-0+if (argc) +usage_with_options (credential_fill_usage, options);
One small thing: no test covers this check. When I removed it, t0300 still passed. Maybe something like this?
test_expect_success 'credential subcommands reject extra arguments' '
for sub in fill approve reject capability
do
test_expect_code 129 git credential $sub extra
</dev/null || return 1
done
'
Thanks,
Tanga
v2 cover credential: parse subcommands with OPT_SUBCOMMAND
In reply to Wasswa Edson"git credential" dispatches its subcommands by hand with strcmp(). This series converts it to OPT_SUBCOMMAND(), so that each subcommand gets its own "-h" usage and the subcommand names become available to completion.
Changes since v1:
- Split into three patches. The pre-existing mismatch between the "-h" output and the SYNOPSIS (1/3) and a refactor moving each subcommand into its own function (2/3) are now separate from the switch to OPT_SUBCOMMAND() (3/3).
- Factor the repeated parse_options() call and argument check into a helper, which rejects leftover arguments with "too many arguments" (Tanga).
- Add a test for the rejection of extra arguments (Tanga).
- Drop the claim that the code gets shorter (Tanga).
v1: https://lore.kernel.org/git/[email protected]/
Wasswa Edson (3):
credential: make -h output and SYNOPSIS agree
credential: move each subcommand into its own function
credential: parse subcommands with OPT_SUBCOMMAND
Documentation/git-credential.adoc | 3 +- builtin/credential.c | 143 +++++++++++++++++++++++------- git.c | 2 +- t/t0300-credentials.sh | 27 ++++++ t/t0450/adoc-help-mismatches | 1 - 5 files changed, 142 insertions(+), 34 deletions(-)
Range-diff against v1:
-: ---------- > 1: d1bffb83df credential: make -h output and SYNOPSIS agree
-: ---------- > 2: c7708ac31b credential: move each subcommand into its own function
1: 85e3f2ce94 ! 3: 7c9842bb9e credential: parse subcommands with OPT_SUBCOMMAND
@@ Metadata
## Commit message ##
credential: parse subcommands with OPT_SUBCOMMAND
- "git credential" dispatches its four subcommands (fill, approve,
- reject, capability) with a strcmp() chain after an argc check,
- instead of using the parse-options API used by other builtins.
+ Now that each subcommand of "git credential" lives in its own
+ function, switch the dispatch to OPT_SUBCOMMAND(). This gives each
+ subcommand its own usage string, so that "git credential <subcommand>
+ -h" shows help for that subcommand, and makes the subcommand names
+ available to the completion via --git-completion-helper.
- Convert the dispatch to OPT_SUBCOMMAND(). Besides making the code
- shorter and more idiomatic, each subcommand gets its own usage
- string, so "git credential <subcommand> -h" shows a targeted help
- text, and the subcommand names are reported via
- --git-completion-helper.
+ Each subcommand takes neither options nor arguments; reject any
+ with a common helper. Drop NO_PARSEOPT from the git.c entry, as the
+ command now uses parse-options.
- Since the builtin now uses parse-options, drop NO_PARSEOPT from its
- git.c entry so "git --list-cmds=parseopt" includes it.
+ This changes the behavior only on error paths:
- Two pre-existing inconsistencies are fixed while at it:
+ - A missing or unknown subcommand is reported as such ("need a
+ subcommand" / "unknown subcommand") before the usage text.
- - The usage string did not mention "capability" even though it is a
- documented subcommand; it now lists all four.
- - Documentation/git-credential.adoc marked its SYNOPSIS as a plain
- literal block, which t0450 cannot extract; mark it [verse] and drop
- "credential" from t0450/adoc-help-mismatches.
+ - Extra arguments are reported as "too many arguments" before the
+ usage text of the subcommand.
- Behavior changes are limited to error paths: a missing or unknown
- subcommand is reported as such ("need a subcommand" / "unknown
- subcommand") before the usage text, instead of only showing the
- usage text, and "git credential <subcommand> -h" exits 0 after
- showing the subcommand's usage. Exit codes for failures are
- unchanged (129).
+ - "git credential <subcommand> -h" shows the usage of that
+ subcommand and exits with 0, instead of failing with the usage of
+ the whole command.
- Signed-off-by: Wasswa Edson <[email protected]>
+ Failures still exit with 129.
- ## Documentation/git-credential.adoc ##
-@@ Documentation/git-credential.adoc: git-credential - Retrieve and store user credentials
-
- SYNOPSIS
- --------
--------------------
-+[verse]
- 'git credential' (fill|approve|reject|capability)
--------------------
-
- DESCRIPTION
- -----------
+ Helped-by: Tanga Joris <[email protected]>
+ Signed-off-by: Wasswa Edson <[email protected]>
## builtin/credential.c ##
@@
@@ builtin/credential.c
#include "config.h"
+#include "gettext.h"
+#include "parse-options.h"
-
--static const char usage_msg[] =
-- "git credential (fill|approve|reject)";
++
+static const char * const credential_usage[] = {
+ N_("git credential (fill|approve|reject|capability)"),
+ NULL,
+};
-
--int cmd_credential(int argc,
-- const char **argv,
-- const char *prefix UNUSED,
-- struct repository *repo UNUSED)
++
+static const char * const credential_fill_usage[] = {
+ N_("git credential fill"),
+ NULL,
@@ builtin/credential.c
+ NULL,
+};
+
++static void parse_no_args(int argc, const char **argv, const char *prefix,
++ const char * const *usagestr)
++{
++ struct option options[] = {
++ OPT_END()
++ };
+
+-static const char usage_msg[] =
+- "git credential (fill|approve|reject|capability)";
++ if (parse_options(argc, argv, prefix, options, usagestr, 0))
++ usage_msg_opt(_("too many arguments"), usagestr, options);
++}
+
+-static int cmd_credential_fill(int argc UNUSED, const char **argv UNUSED,
+- const char *prefix UNUSED,
+static int cmd_credential_fill(int argc, const char **argv, const char *prefix,
-+ struct repository *repo UNUSED)
+ struct repository *repo UNUSED)
{
-- const char *op;
-+ struct option options[] = { OPT_END() };
struct credential c = CREDENTIAL_INIT;
-- repo_config(the_repository, git_default_config, NULL);
-+ argc = parse_options(argc, argv, prefix, options,
-+ credential_fill_usage, 0);
-+ if (argc)
-+ usage_with_options(credential_fill_usage, options);
-+
-+ if (credential_read(&c, stdin, CREDENTIAL_OP_INITIAL) < 0)
-+ die("unable to read credential from stdin");
++ parse_no_args(argc, argv, prefix, credential_fill_usage);
+
-+ credential_fill(the_repository, &c, 0);
-+ credential_next_state(&c);
-+ credential_write(&c, stdout, CREDENTIAL_OP_RESPONSE);
-+
-+ credential_clear(&c);
-+ return 0;
-+}
+ if (credential_read(&c, stdin, CREDENTIAL_OP_INITIAL) < 0)
+ die("unable to read credential from stdin");
-- show_usage_if_asked(argc, argv, usage_msg);
-- if (argc != 2)
-- usage(usage_msg);
-- op = argv[1];
-+static int cmd_credential_approve(int argc, const char **argv,
-+ const char *prefix,
-+ struct repository *repo UNUSED)
-+{
-+ struct option options[] = { OPT_END() };
-+ struct credential c = CREDENTIAL_INIT;
+@@ builtin/credential.c: static int cmd_credential_fill(int argc UNUSED, const char **argv UNUSED,
+ return 0;
+ }
-- if (!strcmp(op, "capability")) {
-- credential_set_all_capabilities(&c, CREDENTIAL_OP_INITIAL);
-- credential_announce_capabilities(&c, stdout);
-- return 0;
-- }
-+ argc = parse_options(argc, argv, prefix, options,
-+ credential_approve_usage, 0);
-+ if (argc)
-+ usage_with_options(credential_approve_usage, options);
+-static int cmd_credential_approve(int argc UNUSED, const char **argv UNUSED,
+- const char *prefix UNUSED,
++static int cmd_credential_approve(int argc, const char **argv,
++ const char *prefix,
+ struct repository *repo UNUSED)
+ {
+ struct credential c = CREDENTIAL_INIT;
++ parse_no_args(argc, argv, prefix, credential_approve_usage);
++
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);
-- }
-+ credential_set_all_capabilities(&c, CREDENTIAL_OP_HELPER);
-+ credential_approve(the_repository, &c);
-
- credential_clear(&c);
+@@ builtin/credential.c: static int cmd_credential_approve(int argc UNUSED, const char **argv UNUSED,
return 0;
}
-+
+
+-static int cmd_credential_reject(int argc UNUSED, const char **argv UNUSED,
+- const char *prefix UNUSED,
+static int cmd_credential_reject(int argc, const char **argv,
+ const char *prefix,
-+ struct repository *repo UNUSED)
-+{
-+ struct option options[] = { OPT_END() };
-+ struct credential c = CREDENTIAL_INIT;
-+
-+ argc = parse_options(argc, argv, prefix, options,
-+ credential_reject_usage, 0);
-+ if (argc)
-+ usage_with_options(credential_reject_usage, options);
-+
-+ 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;
-+}
+ struct repository *repo UNUSED)
+ {
+ struct credential c = CREDENTIAL_INIT;
+
++ parse_no_args(argc, argv, prefix, credential_reject_usage);
+
+ if (credential_read(&c, stdin, CREDENTIAL_OP_INITIAL) < 0)
+ die("unable to read credential from stdin");
+
+@@ builtin/credential.c: static int cmd_credential_reject(int argc UNUSED, const char **argv UNUSED,
+ return 0;
+ }
+
+-static int cmd_credential_capability(int argc UNUSED,
+- const char **argv UNUSED,
+- const char *prefix UNUSED,
+static int cmd_credential_capability(int argc, const char **argv,
+ const char *prefix,
-+ struct repository *repo UNUSED)
-+{
-+ struct option options[] = { OPT_END() };
-+ struct credential c = CREDENTIAL_INIT;
-+
-+ argc = parse_options(argc, argv, prefix, options,
-+ credential_capability_usage, 0);
-+ if (argc)
-+ usage_with_options(credential_capability_usage, options);
-+
-+ credential_set_all_capabilities(&c, CREDENTIAL_OP_INITIAL);
-+ credential_announce_capabilities(&c, stdout);
-+
-+ credential_clear(&c);
-+ return 0;
-+}
+ struct repository *repo UNUSED)
+ {
+ struct credential c = CREDENTIAL_INIT;
+
++ parse_no_args(argc, argv, prefix, credential_capability_usage);
+
-+int cmd_credential(int argc,
-+ const char **argv,
-+ const char *prefix,
-+ struct repository *repo)
-+{
+ credential_set_all_capabilities(&c, CREDENTIAL_OP_INITIAL);
+ credential_announce_capabilities(&c, stdout);
+
+@@ builtin/credential.c: int cmd_credential(int argc,
+ const char *prefix,
+ struct repository *repo)
+ {
+- const char *op;
+ parse_opt_subcommand_fn *fn = NULL;
+ struct option options[] = {
+ OPT_SUBCOMMAND("fill", &fn, cmd_credential_fill),
@@ builtin/credential.c
+ OPT_SUBCOMMAND("capability", &fn, cmd_credential_capability),
+ OPT_END()
+ };
-+
-+ repo_config(the_repository, git_default_config, NULL);
-+
+
+ repo_config(the_repository, git_default_config, NULL);
+
+- show_usage_if_asked(argc, argv, usage_msg);
+- if (argc != 2)
+- usage(usage_msg);
+- op = argv[1];
+-
+- 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);
+-
+- usage(usage_msg);
+ argc = parse_options(argc, argv, prefix, options, credential_usage, 0);
+
+ return fn(argc, argv, prefix, repo);
-+}
+ }
## git.c ##
@@ git.c: static struct cmd_struct commands[] = {
@@ t/t0300-credentials.sh: test_expect_success 'interactive prompt is sanitized' '
+ test_expect_code 129 git credential foo 2>err &&
+ test_grep "unknown subcommand" err
+'
++
++test_expect_success 'credential subcommands reject extra arguments' '
++ for sub in fill approve reject capability
++ do
++ test_expect_code 129 git credential $sub extra \
++ </dev/null 2>err &&
++ test_grep "too many arguments" err || return 1
++ done
++'
+
test_done
-
- ## t/t0450/adoc-help-mismatches ##
-@@ t/t0450/adoc-help-mismatches: checkout
- checkout-index
- clone
- column
--credential
- credential-cache
- credential-store
- fetch-packbase-commit: 37b3475fb530c14109211faca33716a173db61ed
Signature · 1 line
2.53.0
PATCH v2 1/3 credential: make -h output and SYNOPSIS agree
The usage string of "git credential" does not mention the "capability" subcommand, even though it is documented and implemented. The SYNOPSIS in its documentation does list it, but is written as a plain literal block instead of using [verse], so t0450 cannot extract it, and "credential" has been listed as a known mismatch between the "-h" output and the documentation.
Add "capability" to the usage string, mark the SYNOPSIS as [verse], and drop "credential" from the list of known mismatches.
--- Documentation/git-credential.adoc | 3 +-- builtin/credential.c | 2 +- t/t0450/adoc-help-mismatches | 1 - 3 files changed, 2 insertions(+), 4 deletions(-)
Documentation/git-credential.adoc +1 -2
@@ -7,9 +7,8 @@ git-credential - Retrieve and store user credentials SYNOPSIS -------- ------------------- +[verse] 'git credential' (fill|approve|reject|capability) ------------------- DESCRIPTION -----------
builtin/credential.c +1 -1
@@ -7,7 +7,7 @@#include "config.h" static const char usage_msg[] = -"git credential (fill|approve|reject)" ; +"git credential (fill|approve|reject|capability)" ; int cmd_credential(int argc, const char **argv,
t/t0450/adoc-help-mismatches +0 -1
@@ -9,7 +9,6 @@ checkout checkout-index clone column -credential credential-cache credential-store fetch-pack
Signature · 1 line
2.53.0
PATCH v2 2/3 credential: move each subcommand into its own function
In reply to Wasswa Edsoncmd_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.
--- builtin/credential.c | 102 +++++++++++++++++++++++++++++++------------ 1 file changed, 75 insertions(+), 27 deletions(-)
builtin/credential.c +75 -27
@@ -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); }
Signature · 1 line
2.53.0
PATCH v2 3/3 credential: parse subcommands with OPT_SUBCOMMAND
In reply to Wasswa EdsonNow that each subcommand of "git credential" lives in its own function, switch the dispatch to OPT_SUBCOMMAND(). This gives each subcommand its own usage string, so that "git credential <subcommand> -h" shows help for that subcommand, and makes the subcommand names available to the completion via --git-completion-helper.
Each subcommand takes neither options nor arguments; reject any with a common helper. Drop NO_PARSEOPT from the git.c entry, as the command now uses parse-options.
This changes the behavior only on error paths:
- A missing or unknown subcommand is reported as such ("need a subcommand" / "unknown subcommand") before the usage text.
- Extra arguments are reported as "too many arguments" before the usage text of the subcommand.
- "git credential <subcommand> -h" shows the usage of that subcommand and exits with 0, instead of failing with the usage of the whole command.
Failures still exit with 129.
--- builtin/credential.c | 91 +++++++++++++++++++++++++++++------------- git.c | 2 +- t/t0300-credentials.sh | 27 +++++++++++++ 3 files changed, 91 insertions(+), 29 deletions(-)
builtin/credential.c +63 -28
@@ -5,16 +5,52 @@#include "builtin.h" #include "environment.h" #include "config.h" +#include "gettext.h" +#include "parse-options.h" + +static const char *const credential_usage[] = { +N_ ("git credential (fill|approve|reject|capability)" ), +NULL , +}; + +static const char *const credential_fill_usage[] = { +N_ ("git credential fill" ), +NULL , +}; + +static const char *const credential_approve_usage[] = { +N_ ("git credential approve" ), +NULL , +}; + +static const char *const credential_reject_usage[] = { +N_ ("git credential reject" ), +NULL , +}; + +static const char *const credential_capability_usage[] = { +N_ ("git credential capability" ), +NULL , +}; + +static void parse_no_args (int argc,const char **argv,const char *prefix, +const char *const *usagestr) +{ +struct option options[] = { +OPT_END () + }; -static const char usage_msg[] = -"git credential (fill|approve|reject|capability)" ; +if (parse_options (argc, argv, prefix, options, usagestr,0 )) +usage_msg_opt (_ ("too many arguments" ), usagestr, options); +} -static int cmd_credential_fill(int argc UNUSED, const char **argv UNUSED, - const char *prefix UNUSED, +static int cmd_credential_fill (int argc,const char **argv,const char *prefix, struct repository *repo UNUSED) {struct credential c =CREDENTIAL_INIT ; +parse_no_args (argc, argv, prefix, credential_fill_usage); +if (credential_read (&c, stdin,CREDENTIAL_OP_INITIAL ) <0 )die ("unable to read credential from stdin" );
@@ -26,12 +62,14 @@ static int cmd_credential_fill(int argc UNUSED, const char **argv UNUSED,return 0 ; } -static int cmd_credential_approve(int argc UNUSED, const char **argv UNUSED, - const char *prefix UNUSED, +static int cmd_credential_approve (int argc,const char **argv, +const char *prefix, struct repository *repo UNUSED) {struct credential c =CREDENTIAL_INIT ; +parse_no_args (argc, argv, prefix, credential_approve_usage); +if (credential_read (&c, stdin,CREDENTIAL_OP_INITIAL ) <0 )die ("unable to read credential from stdin" );
@@ -42,12 +80,14 @@ static int cmd_credential_approve(int argc UNUSED, const char **argv UNUSED,return 0 ; } -static int cmd_credential_reject(int argc UNUSED, const char **argv UNUSED, - const char *prefix UNUSED, +static int cmd_credential_reject (int argc,const char **argv, +const char *prefix, struct repository *repo UNUSED) {struct credential c =CREDENTIAL_INIT ; +parse_no_args (argc, argv, prefix, credential_reject_usage); +if (credential_read (&c, stdin,CREDENTIAL_OP_INITIAL ) <0 )die ("unable to read credential from stdin" );
@@ -58,13 +98,14 @@ static int cmd_credential_reject(int argc UNUSED, const char **argv UNUSED,return 0 ; } -static int cmd_credential_capability(int argc UNUSED, - const char **argv UNUSED, - const char *prefix UNUSED, +static int cmd_credential_capability (int argc,const char **argv, +const char *prefix, struct repository *repo UNUSED) {struct credential c =CREDENTIAL_INIT ; +parse_no_args (argc, argv, prefix, credential_capability_usage); +credential_set_all_capabilities (&c,CREDENTIAL_OP_INITIAL );credential_announce_capabilities (&c, stdout);
@@ -77,24 +118,18 @@ int cmd_credential(int argc,const char *prefix, struct repository *repo) { - const char *op; + parse_opt_subcommand_fn *fn = NULL; +struct option options[] = { +OPT_SUBCOMMAND ("fill" , &fn, cmd_credential_fill), +OPT_SUBCOMMAND ("approve" , &fn, cmd_credential_approve), +OPT_SUBCOMMAND ("reject" , &fn, cmd_credential_reject), +OPT_SUBCOMMAND ("capability" , &fn, cmd_credential_capability), +OPT_END () + };repo_config (the_repository, git_default_config,NULL ); -show_usage_if_asked (argc, argv, usage_msg); -if (argc !=2 ) -usage (usage_msg); - op = argv[1 ]; - -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); - -usage (usage_msg); + argc =parse_options (argc, argv, prefix, options, credential_usage,0 ); + +return fn (argc, argv, prefix, repo); }
git.c +1 -1
@@ -562,7 +562,7 @@ static struct cmd_struct commands[] = { { "commit-tree", cmd_commit_tree, RUN_SETUP }, { "config", cmd_config, RUN_SETUP_GENTLY | DELAY_PAGER_CONFIG }, { "count-objects", cmd_count_objects, RUN_SETUP }, - { "credential", cmd_credential, RUN_SETUP_GENTLY | NO_PARSEOPT }, + { "credential", cmd_credential, RUN_SETUP_GENTLY }, { "credential-cache", cmd_credential_cache }, { "credential-cache--daemon", cmd_credential_cache_daemon }, { "credential-store", cmd_credential_store },
t/t0300-credentials.sh +27 -0
@@ -1056,4 +1056,31 @@ test_expect_success 'interactive prompt is sanitized' ' EOF ' +test_expect_success 'credential subcommands -h show subcommand usage' ' + for sub in fill approve reject capability + do + git credential $sub -h >out && + test_grep "^usage: git credential $sub" out || return 1 + done +' + +test_expect_success 'credential requires a subcommand' ' + test_expect_code 129 git credential 2>err && + test_grep "need a subcommand" err +' + +test_expect_success 'credential rejects unknown subcommand' ' + test_expect_code 129 git credential foo 2>err && + test_grep "unknown subcommand" err +' + +test_expect_success 'credential subcommands reject extra arguments' ' + for sub in fill approve reject capability + do + test_expect_code 129 git credential $sub extra \ + </dev/null 2>err && + test_grep "too many arguments" err || return 1 + done +' + test_done
Signature · 1 line
2.53.0
Hello Edson,
Thanks for the v2. Your tests work well and catch the issue.
Quoted 1 line · Wasswa Edson
This changes the behavior only on error paths:
One more change: "git credential fill --" (and the same for the other subcommands) used to fail with the usage, and now runs the subcommand. It's harmless, so maybe add one more line to the list.
Otherwise it looks good to me.
Thanks,
Tanga
Quoted 1 line · Wasswa Edson
This changes the behavior only on error paths:
Not blocking, I gave my Reviewed-by on the cover letter. If you
reroll: "--end-of-options" does what "--" does now, the case Tanga mentioned:
$ git credential capability --end-of-options
version 0
capability authtype
capability stateQuoted 2 lines
-h" shows help for that subcommand, and makes the subcommand names available to the completion via --git-completion-helper.
Also not blocking: "git credential --git-completion-helper" lists them now, but bash completion still offers nothing for "git credential <TAB>", same as "git hook" today. Is a _git_credential meant as a follow-up?
Looks good to me, for all three patches. t0300, t0450 and t9902 pass for me.
Quoted 2 lines · Laveesh Bansal
Looks good to me, for all three patches. t0300, t0450 and t9902 pass for me.
Thanks for taking the time to review and test the series. It is much harder to write a positive review than to point out bugs, but encouraging the authors is always appreciated.
Outside of a trivially obvious fix or a quick nod from a known area expert (e.g., Dscho or J6t on a Windows-specific patch) or the original author of the code being touched, it is hard for bystanders to tell from a one-line "Looks good to me" how deeply the reviewer thought through the changes.
One trick I sometimes use when giving a positive review is to walk through the patch aloud, explaining in my own words what the old and new code do and why the tricky parts are handled correctly. That way, others can see that a second pair of eyes really understood and agreed with the approach (or the author can correct me if I misread something).
Thanks.
Quoted 5 lines · Junio C Hamano
Outside of a trivially obvious fix or a quick nod from a known area expert (e.g., Dscho or J6t on a Windows-specific patch) or the original author of the code being touched, it is hard for bystanders to tell from a one-line "Looks good to me" how deeply the reviewer thought through the changes.
Thanks Junio, that is a fair point and a helpful suggestion.
Quoted 5 lines
One trick I sometimes use when giving a positive review is to walk through the patch aloud, explaining in my own words what the old and new code do and why the tricky parts are handled correctly. That way, others can see that a second pair of eyes really understood and agreed with the approach (or the author can correct me if I misread something).
Here is what I did, so it is visible.
I applied the three patches on the base named in the cover letter (37b3475fb5) and built each commit on its own. t0300 and t0450 pass at each step (t0300: 56, 56 and 60 tests). On the final commit I ran t03* and t04*, t9902, and t0300 with GIT_TEST_DEFAULT_HASH=sha256. All pass.
To check that the tests catch the change, I ran the new t0300 tests against a build without the series (4 fail), and removed the "too many arguments" check (test 60 fails). Keeping "credential" in adoc-help-mismatches makes t0450 fail, so dropping that line is needed.
I compared an unpatched build with the patched one on about 50 command lines, including real input on stdin for fill, approve and reject. For valid input, output and exit codes match. The differences are in -h, "--", --git-completion-helper, and the error messages for a missing, unknown or extra argument. The two small points I found are in my reply to 3/3 [1]. One good result: "git credential bogus" blocks on stdin on the old build, and exits 129 right away now.
Why I think the approach is right:
1/3: "capability" was documented and implemented but missing from the usage string. Adding it, and marking the SYNOPSIS [verse] so that t0450 can extract it, makes -h and the man page agree. The "credential" line in adoc-help-mismatches then has to go, or t0450 reports the breakage as fixed.
2/3: each subcommand keeps the same steps as before: approve and reject read the credential and set the helper capabilities, fill reads and fills, and capability reads nothing. Only the order changes, so an unknown name is rejected before stdin is read. The old code read stdin first, which is why "git credential bogus" blocked. The functions keep using the_repository and leave the repo argument unused, which is right because "git credential" runs with RUN_SETUP_GENTLY and repo is NULL outside a repository.
3/3: OPT_SUBCOMMAND is used without the "optional subcommand" flag, so a missing subcommand is reported and the function pointer cannot be NULL. Each subcommand re-parses its own arguments with an empty option table, so -h, an unknown option and a leftover argument are each handled where the subcommand lives. The usage arrays are static, and the option table is only used before it goes out of scope. Calling parse_options() is what makes --git-completion-helper work, and dropping NO_PARSEOPT lists the command in --list-cmds=parseopt, which the completion script consults.
The remaining differences are that "--" and "--end-of-options" are consumed by the parser, so the subcommand then runs, as it does for "git refs verify --" and "git reflog list --". Reading the old and new code side by side, I did not find a case where input that was accepted before behaves differently.
I also thought about smaller versions. One shared usage string would be shorter, but "git credential fill -h" would then show the usage of the whole command, and the per-subcommand usage is how "git refs" and "git reflog" do it. Stopping after 2/3 would already fix the usage string and the stdin hang, but "git credential" would still have no per-subcommand -h, no "need a subcommand" or "unknown subcommand" errors, and no --git-completion-helper. I think the series as sent is the better balance.
I also searched the list archive for a competing series and found none.
As for how I review and respond: I usually keep my replies short when a patch looks right to me and works as expected, which is why my first reply was brief. You are right that a one-liner does not show what I checked. I am still new to this area, so I will explain my thinking in future reviews.
Thanks,
Laveesh
Quoted 5 lines · Laveesh Bansal
I applied the three patches on the base named in the cover letter (
37b3475fb5) and built each commit on its own. t0300 and t0450 pass at each step (t0300: 56, 56 and 60 tests). On the final commit I ran t03* and t04*, t9902, and t0300 with GIT_TEST_DEFAULT_HASH=sha256. All pass.
That is not a review. It is only proving what the test coverage touches. Testing is not pointless, especially if you are running it on minority platforms to catch portability issues, but the CI runs these tests on popular/common platforms once it hits 'seen' anyway, so it will not serve as a replacement of a proper review.
Quoted 10 lines
Why I think the approach is right:
...
I also searched the list archive for a competing series and found none.As for how I review and respond: I usually keep my replies short when a patch looks right to me and works as expected, which is why my first reply was brief. You are right that a one-liner does not show what I checked. I am still new to this area, so I will explain my thinking in future reviews.
Thanks! Your write-up above was really helpful.