@@ -22,6 +22,7 @@ static const char *const builtin_config_usage[] = {
22
22
N_ ("git config unset [<file-option>] [--all] [--value=<value>] [--fixed-value] <name> <value>" ),
23
23
N_ ("git config rename-section [<file-option>] <old-name> <new-name>" ),
24
24
N_ ("git config remove-section [<file-option>] <name>" ),
25
+ N_ ("git config edit [<file-option>]" ),
25
26
NULL
26
27
};
27
28
@@ -55,6 +56,11 @@ static const char *const builtin_config_remove_section_usage[] = {
55
56
NULL
56
57
};
57
58
59
+ static const char * const builtin_config_edit_usage [] = {
60
+ N_ ("git config edit [<file-option>]" ),
61
+ NULL
62
+ };
63
+
58
64
static char * key ;
59
65
static regex_t * key_regexp ;
60
66
static const char * value_pattern ;
@@ -1011,13 +1017,61 @@ static int cmd_config_remove_section(int argc, const char **argv, const char *pr
1011
1017
return 0 ;
1012
1018
}
1013
1019
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
+
1014
1067
static struct option builtin_subcommand_options [] = {
1015
1068
OPT_SUBCOMMAND ("list" , & subcommand , cmd_config_list ),
1016
1069
OPT_SUBCOMMAND ("get" , & subcommand , cmd_config_get ),
1017
1070
OPT_SUBCOMMAND ("set" , & subcommand , cmd_config_set ),
1018
1071
OPT_SUBCOMMAND ("unset" , & subcommand , cmd_config_unset ),
1019
1072
OPT_SUBCOMMAND ("rename-section" , & subcommand , cmd_config_rename_section ),
1020
1073
OPT_SUBCOMMAND ("remove-section" , & subcommand , cmd_config_remove_section ),
1074
+ OPT_SUBCOMMAND ("edit" , & subcommand , cmd_config_edit ),
1021
1075
OPT_END (),
1022
1076
};
1023
1077
@@ -1144,32 +1198,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
1144
1198
}
1145
1199
}
1146
1200
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 ();
1173
1202
}
1174
1203
else if (actions == ACTION_SET ) {
1175
1204
check_write ();
0 commit comments