@@ -22,6 +22,7 @@ static const char *const builtin_config_usage[] = {
2222 N_ ("git config unset [<file-option>] [--all] [--value=<value>] [--fixed-value] <name> <value>" ),
2323 N_ ("git config rename-section [<file-option>] <old-name> <new-name>" ),
2424 N_ ("git config remove-section [<file-option>] <name>" ),
25+ N_ ("git config edit [<file-option>]" ),
2526 NULL
2627};
2728
@@ -55,6 +56,11 @@ static const char *const builtin_config_remove_section_usage[] = {
5556 NULL
5657};
5758
59+ static const char * const builtin_config_edit_usage [] = {
60+ N_ ("git config edit [<file-option>]" ),
61+ NULL
62+ };
63+
5864static char * key ;
5965static regex_t * key_regexp ;
6066static const char * value_pattern ;
@@ -1011,13 +1017,61 @@ static int cmd_config_remove_section(int argc, const char **argv, const char *pr
10111017 return 0 ;
10121018}
10131019
1020+ static int show_editor (void )
1021+ {
1022+ char * config_file ;
1023+
1024+ if (!given_config_source .file && !startup_info -> have_repository )
1025+ die (_ ("not in a git directory" ));
1026+ if (given_config_source .use_stdin )
1027+ die (_ ("editing stdin is not supported" ));
1028+ if (given_config_source .blob )
1029+ die (_ ("editing blobs is not supported" ));
1030+ git_config (git_default_config , NULL );
1031+ config_file = given_config_source .file ?
1032+ xstrdup (given_config_source .file ) :
1033+ git_pathdup ("config" );
1034+ if (use_global_config ) {
1035+ int fd = open (config_file , O_CREAT | O_EXCL | O_WRONLY , 0666 );
1036+ if (fd >= 0 ) {
1037+ char * content = default_user_config ();
1038+ write_str_in_full (fd , content );
1039+ free (content );
1040+ close (fd );
1041+ }
1042+ else if (errno != EEXIST )
1043+ die_errno (_ ("cannot create configuration file %s" ), config_file );
1044+ }
1045+ launch_editor (config_file , NULL , NULL );
1046+ free (config_file );
1047+
1048+ return 0 ;
1049+ }
1050+
1051+ static int cmd_config_edit (int argc , const char * * argv , const char * prefix )
1052+ {
1053+ struct option opts [] = {
1054+ CONFIG_LOCATION_OPTIONS ,
1055+ OPT_END (),
1056+ };
1057+
1058+ argc = parse_options (argc , argv , prefix , opts , builtin_config_edit_usage , 0 );
1059+ check_write ();
1060+ check_argc (argc , 0 , 0 );
1061+
1062+ handle_config_location (prefix );
1063+
1064+ return show_editor ();
1065+ }
1066+
10141067static struct option builtin_subcommand_options [] = {
10151068 OPT_SUBCOMMAND ("list" , & subcommand , cmd_config_list ),
10161069 OPT_SUBCOMMAND ("get" , & subcommand , cmd_config_get ),
10171070 OPT_SUBCOMMAND ("set" , & subcommand , cmd_config_set ),
10181071 OPT_SUBCOMMAND ("unset" , & subcommand , cmd_config_unset ),
10191072 OPT_SUBCOMMAND ("rename-section" , & subcommand , cmd_config_rename_section ),
10201073 OPT_SUBCOMMAND ("remove-section" , & subcommand , cmd_config_remove_section ),
1074+ OPT_SUBCOMMAND ("edit" , & subcommand , cmd_config_edit ),
10211075 OPT_END (),
10221076};
10231077
@@ -1144,32 +1198,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
11441198 }
11451199 }
11461200 else if (actions == ACTION_EDIT ) {
1147- char * config_file ;
1148-
1149- check_argc (argc , 0 , 0 );
1150- if (!given_config_source .file && !startup_info -> have_repository )
1151- die (_ ("not in a git directory" ));
1152- if (given_config_source .use_stdin )
1153- die (_ ("editing stdin is not supported" ));
1154- if (given_config_source .blob )
1155- die (_ ("editing blobs is not supported" ));
1156- git_config (git_default_config , NULL );
1157- config_file = given_config_source .file ?
1158- xstrdup (given_config_source .file ) :
1159- git_pathdup ("config" );
1160- if (use_global_config ) {
1161- int fd = open (config_file , O_CREAT | O_EXCL | O_WRONLY , 0666 );
1162- if (fd >= 0 ) {
1163- char * content = default_user_config ();
1164- write_str_in_full (fd , content );
1165- free (content );
1166- close (fd );
1167- }
1168- else if (errno != EEXIST )
1169- die_errno (_ ("cannot create configuration file %s" ), config_file );
1170- }
1171- launch_editor (config_file , NULL , NULL );
1172- free (config_file );
1201+ ret = show_editor ();
11731202 }
11741203 else if (actions == ACTION_SET ) {
11751204 check_write ();
0 commit comments