Skip to content

Commit 9830534

Browse files
moygitster
authored andcommitted
config --global --edit: create a template file if needed
When the user has no ~/.gitconfig file, git config --global --edit used to launch an editor on an nonexistant file name. Instead, create a file with a default content before launching the editor. The template contains only commented-out entries, to save a few keystrokes for the user. If the values are guessed properly, the user will only have to uncomment the entries. Advanced users teaching newbies can create a minimalistic configuration faster for newbies. Beginners reading a tutorial advising to run "git config --global --edit" as a first step will be slightly more guided for their first contact with Git. Signed-off-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 740c281 commit 9830534

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

builtin/config.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,20 @@ static int get_urlmatch(const char *var, const char *url)
458458
return 0;
459459
}
460460

461+
static char *default_user_config(void)
462+
{
463+
struct strbuf buf = STRBUF_INIT;
464+
strbuf_addf(&buf,
465+
_("# This is Git's per-user configuration file.\n"
466+
"[core]\n"
467+
"# Please adapt and uncomment the following lines:\n"
468+
"# user = %s\n"
469+
"# email = %s\n"),
470+
ident_default_name(),
471+
ident_default_email());
472+
return strbuf_detach(&buf, NULL);
473+
}
474+
461475
int cmd_config(int argc, const char **argv, const char *prefix)
462476
{
463477
int nongit = !startup_info->have_repository;
@@ -564,6 +578,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
564578
}
565579
}
566580
else if (actions == ACTION_EDIT) {
581+
const char *config_file = given_config_source.file ?
582+
given_config_source.file : git_path("config");
567583
check_argc(argc, 0, 0);
568584
if (!given_config_source.file && nongit)
569585
die("not in a git directory");
@@ -572,9 +588,18 @@ int cmd_config(int argc, const char **argv, const char *prefix)
572588
if (given_config_source.blob)
573589
die("editing blobs is not supported");
574590
git_config(git_default_config, NULL);
575-
launch_editor(given_config_source.file ?
576-
given_config_source.file : git_path("config"),
577-
NULL, NULL);
591+
if (use_global_config) {
592+
int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
593+
if (fd) {
594+
char *content = default_user_config();
595+
write_str_in_full(fd, content);
596+
free(content);
597+
close(fd);
598+
}
599+
else if (errno != EEXIST)
600+
die_errno(_("cannot create configuration file %s"), config_file);
601+
}
602+
launch_editor(config_file, NULL, NULL);
578603
}
579604
else if (actions == ACTION_SET) {
580605
int ret;

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,7 @@ extern const char *git_author_info(int);
10251025
extern const char *git_committer_info(int);
10261026
extern const char *fmt_ident(const char *name, const char *email, const char *date_str, int);
10271027
extern const char *fmt_name(const char *name, const char *email);
1028+
extern const char *ident_default_name(void);
10281029
extern const char *ident_default_email(void);
10291030
extern const char *git_editor(void);
10301031
extern const char *git_pager(int stdout_is_tty);

ident.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static void copy_email(const struct passwd *pw, struct strbuf *email)
102102
add_domainname(email);
103103
}
104104

105-
static const char *ident_default_name(void)
105+
const char *ident_default_name(void)
106106
{
107107
if (!git_default_name.len) {
108108
copy_gecos(xgetpwuid_self(), &git_default_name);

0 commit comments

Comments
 (0)