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 <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthieu Moy committed Jul 25, 2014 at 21:11 UTC 9830534e40bd15231357965441d4fe02a6a4810e
3 files changed +30 -4
builtin/config.c
+28 -3
@@ -458,6 +458,20 @@ static int get_urlmatch(const char *var, const char *url)
458 return 0;
459 }
460
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 +
475 int cmd_config(int argc, const char **argv, const char *prefix)
476 {
477 int nongit = !startup_info->have_repository;
@@ -564,6 +578,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
578 }
579 }
580 else if (actions == ACTION_EDIT) {
581 + const char *config_file = given_config_source.file ?
582 + given_config_source.file : git_path("config");
583 check_argc(argc, 0, 0);
584 if (!given_config_source.file && nongit)
585 die("not in a git directory");
@@ -572,9 +588,18 @@ int cmd_config(int argc, const char **argv, const char *prefix)
588 if (given_config_source.blob)
589 die("editing blobs is not supported");
590 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);
603 }
604 else if (actions == ACTION_SET) {
605 int ret;
cache.h
+1
@@ -1025,6 +1025,7 @@ extern const char *git_author_info(int);
1025 extern const char *git_committer_info(int);
1026 extern const char *fmt_ident(const char *name, const char *email, const char *date_str, int);
1027 extern const char *fmt_name(const char *name, const char *email);
1028 +extern const char *ident_default_name(void);
1029 extern const char *ident_default_email(void);
1030 extern const char *git_editor(void);
1031 extern const char *git_pager(int stdout_is_tty);
ident.c
+1 -1
@@ -102,7 +102,7 @@ static void copy_email(const struct passwd *pw, struct strbuf *email)
102 add_domainname(email);
103 }
104
105 -static const char *ident_default_name(void)
105 +const char *ident_default_name(void)
106 {
107 if (!git_default_name.len) {
108 copy_gecos(xgetpwuid_self(), &git_default_name);