git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: Kristoffer Haugsbakk <code@khaugsbakk.name>
Cc: git@vger.kernel.org, stolee@gmail.com
Subject: Re: [PATCH v1 3/4] config: factor out global config file retrieval
Date: Mon, 23 Oct 2023 11:58:01 +0200	[thread overview]
Message-ID: <ZTZDqToqcsDiS5AP@tanuki> (raw)
In-Reply-To: <147c767443c35b3b4a5516bf40557f41bb201078.1697660181.git.code@khaugsbakk.name>

[-- Attachment #1: Type: text/plain, Size: 3656 bytes --]

On Wed, Oct 18, 2023 at 10:28:40PM +0200, Kristoffer Haugsbakk wrote:
> 
> Factor out code that retrieves the global config file so that we can use
> it in `gc.c` as well.
> 
> Use the old name from the previous commit since this function acts
> functionally the same as `git_system_config` but for “global”.
> 
> Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name>
> ---
>  builtin/config.c | 25 ++-----------------------
>  config.c         | 24 ++++++++++++++++++++++++
>  config.h         |  1 +
>  3 files changed, 27 insertions(+), 23 deletions(-)
> 
> diff --git a/builtin/config.c b/builtin/config.c
> index 6fff2655816..df06b766fad 100644
> --- a/builtin/config.c
> +++ b/builtin/config.c
> @@ -708,30 +708,9 @@ int cmd_config(int argc, const char **argv, const char *prefix)
>  	}
>  
>  	if (use_global_config) {
> -		char *user_config, *xdg_config;
> -
> -		git_global_config_paths(&user_config, &xdg_config);
> -		if (!user_config)
> -			/*
> -			 * It is unknown if HOME/.gitconfig exists, so
> -			 * we do not know if we should write to XDG
> -			 * location; error out even if XDG_CONFIG_HOME
> -			 * is set and points at a sane location.
> -			 */
> -			die(_("$HOME not set"));
> -
> +		given_config_source.file = git_global_config();
>  		given_config_source.scope = CONFIG_SCOPE_GLOBAL;
> -
> -		if (access_or_warn(user_config, R_OK, 0) &&
> -		    xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
> -			given_config_source.file = xdg_config;
> -			free(user_config);
> -		} else {
> -			given_config_source.file = user_config;
> -			free(xdg_config);
> -		}
> -	}
> -	else if (use_system_config) {
> +	} else if (use_system_config) {
>  		given_config_source.file = git_system_config();
>  		given_config_source.scope = CONFIG_SCOPE_SYSTEM;
>  	} else if (use_local_config) {
> diff --git a/config.c b/config.c
> index d2cdda96edd..2ff766c56ff 100644
> --- a/config.c
> +++ b/config.c
> @@ -2111,6 +2111,30 @@ char *git_system_config(void)
>  	return system_config;
>  }
>  
> +char *git_global_config(void)
> +{
> +	char *user_config, *xdg_config;
> +
> +	git_global_config_paths(&user_config, &xdg_config);
> +	if (!user_config)
> +		/*
> +		 * It is unknown if HOME/.gitconfig exists, so
> +		 * we do not know if we should write to XDG

Nit: we don't know about the intent of the caller, so they may not want
to write to the file but only read it.

> +		 * location; error out even if XDG_CONFIG_HOME
> +		 * is set and points at a sane location.
> +		 */
> +		die(_("$HOME not set"));

Is it sensible to `die()` here in this new function that behaves more
like a library function? I imagine it would be more sensible to indicate
the error to the user and let them handle it accordingly.

Patrick

> +
> +	if (access_or_warn(user_config, R_OK, 0) && xdg_config &&
> +	    !access_or_warn(xdg_config, R_OK, 0)) {
> +		free(user_config);
> +		return xdg_config;
> +	} else {
> +		free(xdg_config);
> +		return user_config;
> +	}
> +}
> +
>  void git_global_config_paths(char **user_out, char **xdg_out)
>  {
>  	char *user_config = xstrdup_or_null(getenv("GIT_CONFIG_GLOBAL"));
> diff --git a/config.h b/config.h
> index 9f04de8ee3e..5cf961b548d 100644
> --- a/config.h
> +++ b/config.h
> @@ -394,6 +394,7 @@ int config_error_nonbool(const char *);
>  #endif
>  
>  char *git_system_config(void);
> +char *git_global_config(void);
>  void git_global_config_paths(char **user, char **xdg);
>  
>  int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
> -- 
> 2.42.0.2.g879ad04204
> 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2023-10-23  9:58 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-18 20:28 [PATCH v1 0/4] maintenance: use XDG config if it exists Kristoffer Haugsbakk
2023-10-18 20:28 ` [PATCH v1 1/4] config: format newlines Kristoffer Haugsbakk
2023-10-18 20:28 ` [PATCH v1 2/4] config: rename global config function Kristoffer Haugsbakk
2023-10-18 20:28 ` [PATCH v1 3/4] config: factor out global config file retrieval Kristoffer Haugsbakk
2023-10-23  9:58   ` Patrick Steinhardt [this message]
2023-10-23 17:40     ` [PATCH v1 3/4] config: factor out global config file retrievalync-mailbox> Taylor Blau
2023-10-24 13:23       ` Kristoffer Haugsbakk
2023-10-25  5:38         ` Patrick Steinhardt
2023-10-25  7:33           ` Kristoffer Haugsbakk
2023-10-25  8:05             ` Patrick Steinhardt
2023-10-27 15:54               ` Junio C Hamano
2023-10-18 20:28 ` [PATCH v1 4/4] maintenance: use XDG config if it exists Kristoffer Haugsbakk
2023-10-23  9:58   ` Patrick Steinhardt
2023-10-23 11:39     ` Eric Sunshine
2024-01-14 21:43 ` [PATCH v2 0/4] " Kristoffer Haugsbakk
2024-01-14 21:43   ` [PATCH v2 1/4] config: format newlines Kristoffer Haugsbakk
2024-01-14 21:43   ` [PATCH v2 2/4] config: rename global config function Kristoffer Haugsbakk
2024-01-14 21:43   ` [PATCH v2 3/4] config: factor out global config file retrieval Kristoffer Haugsbakk
2024-01-16 21:39     ` Junio C Hamano
2024-01-16 21:46       ` Kristoffer Haugsbakk
2024-01-19  6:18     ` Patrick Steinhardt
2024-01-19  7:40       ` Kristoffer Haugsbakk
2024-01-19  7:59         ` Patrick Steinhardt
2024-01-19 23:04           ` Junio C Hamano
2024-01-19 18:36         ` Junio C Hamano
2024-01-19 18:59           ` rsbecker
2024-01-14 21:43   ` [PATCH v2 4/4] maintenance: use XDG config if it exists Kristoffer Haugsbakk
2024-01-16 21:52     ` Junio C Hamano
2024-01-18 16:12 ` [PATCH v3 0/4] " Kristoffer Haugsbakk
2024-01-18 16:12   ` [PATCH v3 1/4] config: format newlines Kristoffer Haugsbakk
2024-01-18 16:12   ` [PATCH v3 2/4] config: rename global config function Kristoffer Haugsbakk
2024-01-18 16:12   ` [PATCH v3 3/4] config: factor out global config file retrieval Kristoffer Haugsbakk
2024-01-18 16:12   ` [PATCH v3 4/4] maintenance: use XDG config if it exists Kristoffer Haugsbakk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZTZDqToqcsDiS5AP@tanuki \
    --to=ps@pks.im \
    --cc=code@khaugsbakk.name \
    --cc=git@vger.kernel.org \
    --cc=stolee@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).