1717
1818static const char * const builtin_config_usage [] = {
1919 N_ ("git config list [<file-option>] [<display-option>] [--includes]" ),
20- N_ ("git config [<options>] " ),
20+ N_ ("git config get [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<value>] [--fixed-value] [--default=<default>] <name> " ),
2121 NULL
2222};
2323
@@ -26,6 +26,11 @@ static const char *const builtin_config_list_usage[] = {
2626 NULL
2727};
2828
29+ static const char * const builtin_config_get_usage [] = {
30+ N_ ("git config get [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<value>] [--fixed-value] [--default=<default>] <name>" ),
31+ NULL
32+ };
33+
2934static char * key ;
3035static regex_t * key_regexp ;
3136static const char * value_pattern ;
@@ -722,6 +727,16 @@ static void handle_nul(void) {
722727 OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")), \
723728 OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object"))
724729
730+ #define CONFIG_TYPE_OPTIONS \
731+ OPT_GROUP(N_("Type")), \
732+ OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type), \
733+ OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL), \
734+ OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT), \
735+ OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT), \
736+ OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR), \
737+ OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH), \
738+ OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE)
739+
725740#define CONFIG_DISPLAY_OPTIONS \
726741 OPT_GROUP(N_("Display options")), \
727742 OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")), \
@@ -746,14 +761,7 @@ static struct option builtin_config_options[] = {
746761 OPT_CMDMODE ('e' , "edit" , & actions , N_ ("open an editor" ), ACTION_EDIT ),
747762 OPT_CMDMODE (0 , "get-color" , & actions , N_ ("find the color configured: slot [<default>]" ), ACTION_GET_COLOR ),
748763 OPT_CMDMODE (0 , "get-colorbool" , & actions , N_ ("find the color setting: slot [<stdout-is-tty>]" ), ACTION_GET_COLORBOOL ),
749- OPT_GROUP (N_ ("Type" )),
750- OPT_CALLBACK ('t' , "type" , & type , N_ ("type" ), N_ ("value is given this type" ), option_parse_type ),
751- OPT_CALLBACK_VALUE (0 , "bool" , & type , N_ ("value is \"true\" or \"false\"" ), TYPE_BOOL ),
752- OPT_CALLBACK_VALUE (0 , "int" , & type , N_ ("value is decimal number" ), TYPE_INT ),
753- OPT_CALLBACK_VALUE (0 , "bool-or-int" , & type , N_ ("value is --bool or --int" ), TYPE_BOOL_OR_INT ),
754- OPT_CALLBACK_VALUE (0 , "bool-or-str" , & type , N_ ("value is --bool or string" ), TYPE_BOOL_OR_STR ),
755- OPT_CALLBACK_VALUE (0 , "path" , & type , N_ ("value is a path (file or directory name)" ), TYPE_PATH ),
756- OPT_CALLBACK_VALUE (0 , "expiry-date" , & type , N_ ("value is an expiry date" ), TYPE_EXPIRY_DATE ),
764+ CONFIG_TYPE_OPTIONS ,
757765 CONFIG_DISPLAY_OPTIONS ,
758766 OPT_GROUP (N_ ("Other" )),
759767 OPT_STRING (0 , "default" , & default_value , N_ ("value" ), N_ ("with --get, use default value when missing entry" )),
@@ -799,8 +807,51 @@ static int cmd_config_list(int argc, const char **argv, const char *prefix)
799807 return 0 ;
800808}
801809
810+ static int cmd_config_get (int argc , const char * * argv , const char * prefix )
811+ {
812+ const char * value_pattern = NULL , * url = NULL ;
813+ int flags = 0 ;
814+ struct option opts [] = {
815+ CONFIG_LOCATION_OPTIONS ,
816+ CONFIG_TYPE_OPTIONS ,
817+ OPT_GROUP (N_ ("Filter options" )),
818+ OPT_BOOL (0 , "all" , & do_all , N_ ("return all values for multi-valued config options" )),
819+ OPT_BOOL (0 , "regexp" , & use_key_regexp , N_ ("interpret the name as a regular expression" )),
820+ OPT_STRING (0 , "value" , & value_pattern , N_ ("pattern" ), N_ ("show config with values matching the pattern" )),
821+ OPT_BIT (0 , "fixed-value" , & flags , N_ ("use string equality when comparing values to value pattern" ), CONFIG_FLAGS_FIXED_VALUE ),
822+ OPT_STRING (0 , "url" , & url , N_ ("URL" ), N_ ("show config matching the given URL" )),
823+ CONFIG_DISPLAY_OPTIONS ,
824+ OPT_BOOL (0 , "show-names" , & show_keys , N_ ("show config keys in addition to their values" )),
825+ OPT_GROUP (N_ ("Other" )),
826+ OPT_BOOL (0 , "includes" , & respect_includes_opt , N_ ("respect include directives on lookup" )),
827+ OPT_STRING (0 , "default" , & default_value , N_ ("value" ), N_ ("use default value when missing entry" )),
828+ OPT_END (),
829+ };
830+
831+ argc = parse_options (argc , argv , prefix , opts , builtin_config_get_usage ,
832+ PARSE_OPT_STOP_AT_NON_OPTION );
833+ check_argc (argc , 1 , 1 );
834+
835+ if ((flags & CONFIG_FLAGS_FIXED_VALUE ) && !value_pattern )
836+ die (_ ("--fixed-value only applies with 'value-pattern'" ));
837+ if (default_value && (do_all || url ))
838+ die (_ ("--default= cannot be used with --all or --url=" ));
839+ if (url && (do_all || use_key_regexp || value_pattern ))
840+ die (_ ("--url= cannot be used with --all, --regexp or --value" ));
841+
842+ handle_config_location (prefix );
843+ handle_nul ();
844+
845+ setup_auto_pager ("config" , 1 );
846+
847+ if (url )
848+ return get_urlmatch (argv [0 ], url );
849+ return get_value (argv [0 ], value_pattern , flags );
850+ }
851+
802852static struct option builtin_subcommand_options [] = {
803853 OPT_SUBCOMMAND ("list" , & subcommand , cmd_config_list ),
854+ OPT_SUBCOMMAND ("get" , & subcommand , cmd_config_get ),
804855 OPT_END (),
805856};
806857
0 commit comments