git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RFC PATCH 1/3] config --global --edit: create a template file if needed

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Jul 25, 2014, 15:37 UTC
Message-ID
<CAPig+cSyLtUmkxAcO+iatKSS2t2T5byXRrJYq=MSuBoZ3m=hcw@mail.gmail.com>
In-Reply-To
<1406295891-7316-1-git-send-email-Matthieu.Moy@imag.fr>
On Fri, Jul 25, 2014 at 9:44 AM, Matthieu Moy <Matthieu.Moy@imag.fr> wrote:
Show 37 quoted lines
> 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>
> ---
>  builtin/config.c | 30 +++++++++++++++++++++++++++---
>  cache.h          |  1 +
>  ident.c          |  2 +-
>  3 files changed, 29 insertions(+), 4 deletions(-)
>
> diff --git a/builtin/config.c b/builtin/config.c
> index fcd8474..3821697 100644
> --- a/builtin/config.c
> +++ b/builtin/config.c
> @@ -445,6 +445,20 @@ static int get_urlmatch(const char *var, const char *url)
>         return 0;
>  }
>
> +static char *default_user_config()
> +{
> +       struct strbuf buf = STRBUF_INIT;
> +       strbuf_addf(&buf,
> +                   _("# This is Git's user-wide configuration file.\n"
> +                     "[core]\n"
> +                     "# Please, adapt and uncomment the following lines:\n"
> +                     "#        user = %s\n"
> +                     "#        email = %s\n"),

"[core]", "user =", "email =" should not be translated. Would it make sense to keep these outside of _()?

Show 29 quoted lines
> +                   ident_default_name(),
> +                   ident_default_email());
> +       return strbuf_detach(&buf, NULL);
> +}
> +
>  int cmd_config(int argc, const char **argv, const char *prefix)
>  {
>         int nongit = !startup_info->have_repository;
> @@ -551,6 +565,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
>                 }
>         }
>         else if (actions == ACTION_EDIT) {
> +               const char *config_file = given_config_source.file ?
> +                       given_config_source.file : git_path("config");
>                 check_argc(argc, 0, 0);
>                 if (!given_config_source.file && nongit)
>                         die("not in a git directory");
> @@ -559,9 +575,17 @@ int cmd_config(int argc, const char **argv, const char *prefix)
>                 if (given_config_source.blob)
>                         die("editing blobs is not supported");
>                 git_config(git_default_config, NULL);
> -               launch_editor(given_config_source.file ?
> -                             given_config_source.file : git_path("config"),
> -                             NULL, NULL);
> +               if (use_global_config) {
> +                       int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
> +                       if (fd) {
> +                               char *content = default_user_config();
> +                               write_str_in_full(fd, content);
close(fd);
> +                               free(content);
> +                       }
> +                       else if (errno != EEXIST)
> +                               die_errno(_("Cannot create configuration file %s"), config_file);

Other error messages in this file (including those just above this block) begin with a lowercase letter.

Show 32 quoted lines
> +               }
> +               launch_editor(config_file, NULL, NULL);
>         }
>         else if (actions == ACTION_SET) {
>                 int ret;
> diff --git a/cache.h b/cache.h
> index 2f63fd1..8c79c0c 100644
> --- a/cache.h
> +++ b/cache.h
> @@ -1061,6 +1061,7 @@ extern const char *git_author_info(int);
>  extern const char *git_committer_info(int);
>  extern const char *fmt_ident(const char *name, const char *email, const char *date_str, int);
>  extern const char *fmt_name(const char *name, const char *email);
> +extern const char *ident_default_name(void);
>  extern const char *ident_default_email(void);
>  extern const char *git_editor(void);
>  extern const char *git_pager(int stdout_is_tty);
> diff --git a/ident.c b/ident.c
> index 1d9b6e7..77bc882 100644
> --- a/ident.c
> +++ b/ident.c
> @@ -102,7 +102,7 @@ static void copy_email(const struct passwd *pw, struct strbuf *email)
>         add_domainname(email);
>  }
>
> -static const char *ident_default_name(void)
> +const char *ident_default_name(void)
>  {
>         if (!git_default_name.len) {
>                 copy_gecos(xgetpwuid_self(), &git_default_name);
> --
> 2.0.2.737.gfb43bde
Previous: Eric SunshineNext: Matthieu Moy
Message 8 of 13 in “config --global --edit: create a template file if needed”
  1. 1/3 config --global --edit: create a template file if neededMatthieu Moy, Jul 25, 2014
  2. 2/3 home_config_path: allow NULL xdg parameterMatthieu Moy, Jul 25, 2014
  3. Tanay AbhraJul 25, 2014
  4. Junio C HamanoJul 25, 2014
  5. Matthieu MoyJul 25, 2014
  6. 3/3 commit: advertize config --global --edit on guessed identityMatthieu Moy, Jul 25, 2014
  7. Eric SunshineJul 25, 2014
  8. Eric SunshineJul 25, 2014
  9. Matthieu MoyJul 25, 2014
  10. Eric SunshineJul 25, 2014
  11. Junio C HamanoJul 25, 2014
  12. Junio C HamanoJul 25, 2014
  13. Matthieu MoyJul 25, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.