Skip to content

Commit 3cbace5

Browse files
pks-tgitster
authored andcommitted
builtin/config: introduce "edit" subcommand
Introduce a new "edit" subcommand to git-config(1). Please refer to preceding commits regarding the motivation behind this change. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 15dad20 commit 3cbace5

File tree

3 files changed

+68
-36
lines changed

3 files changed

+68
-36
lines changed

Documentation/git-config.txt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ SYNOPSIS
1515
'git config unset' [<file-option>] [--all] [--value=<value>] [--fixed-value] <name> <value>
1616
'git config rename-section' [<file-option>] <old-name> <new-name>
1717
'git config remove-section' [<file-option>] <name>
18+
'git config edit' [<file-option>]
1819
'git config' [<file-option>] --get-colorbool <name> [<stdout-is-tty>]
19-
'git config' [<file-option>] -e | --edit
2020

2121
DESCRIPTION
2222
-----------
@@ -98,6 +98,11 @@ rename-section::
9898
remove-section::
9999
Remove the given section from the configuration file.
100100

101+
edit::
102+
Opens an editor to modify the specified config file; either
103+
`--system`, `--global`, `--local` (default), `--worktree`, or
104+
`--file <config-file>`.
105+
101106
[[OPTIONS]]
102107
OPTIONS
103108
-------
@@ -274,12 +279,6 @@ Valid `<type>`'s include:
274279
When the color setting for `name` is undefined, the command uses
275280
`color.ui` as fallback.
276281

277-
-e::
278-
--edit::
279-
Opens an editor to modify the specified config file; either
280-
`--system`, `--global`, `--local` (default), `--worktree`, or
281-
`--file <config-file>`.
282-
283282
--[no-]includes::
284283
Respect `include.*` directives in config files when looking up
285284
values. Defaults to `off` when a specific file is given (e.g.,
@@ -336,6 +335,10 @@ recommended to migrate to the new syntax.
336335
--remove-section <name>::
337336
Replaced by `git config remove-section <name>`.
338337

338+
-e::
339+
--edit::
340+
Replaced by `git config edit`.
341+
339342
CONFIGURATION
340343
-------------
341344
`pager.config` is only respected when listing configuration, i.e., when

builtin/config.c

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
5864
static char *key;
5965
static regex_t *key_regexp;
6066
static 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+
10141067
static 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();

t/t1300-config.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ test_expect_success 'setting a value in stdin is an error' '
666666
'
667667

668668
test_expect_success 'editing stdin is an error' '
669-
test_must_fail git config --file - --edit
669+
test_must_fail git config ${mode_prefix}edit --file -
670670
'
671671

672672
test_expect_success 'refer config from subdirectory' '
@@ -1768,7 +1768,7 @@ test_expect_success 'command line overrides environment config' '
17681768
test_expect_success 'git config --edit works' '
17691769
git config -f tmp test.value no &&
17701770
echo test.value=yes >expect &&
1771-
GIT_EDITOR="echo [test]value=yes >" git config -f tmp --edit &&
1771+
GIT_EDITOR="echo [test]value=yes >" git config ${mode_prefix}edit -f tmp &&
17721772
git config ${mode_prefix}list -f tmp >actual &&
17731773
test_cmp expect actual
17741774
'
@@ -1777,7 +1777,7 @@ test_expect_success 'git config --edit respects core.editor' '
17771777
git config -f tmp test.value no &&
17781778
echo test.value=yes >expect &&
17791779
test_config core.editor "echo [test]value=yes >" &&
1780-
git config -f tmp --edit &&
1780+
git config ${mode_prefix}edit -f tmp &&
17811781
git config ${mode_prefix}list -f tmp >actual &&
17821782
test_cmp expect actual
17831783
'

0 commit comments

Comments
 (0)