threads / discuss / 31655

Configuring the location of ~/.gitconfig

Subject: Configuring the location of ~/.gitconfig

## tl;dr

14 messages between Sep 26, 2012 and Sep 28, 2012.

replies: 13people: 6as markdown or json

Ramkumar Ramachandra· Sep 26, 2012, 14:14 UTC · lore
Hi,

I'd like to configure the location of ~/.gitconfig through an environment variable. My usecase is a simple enough: I have a repository with all my dotfiles, and I don't want to symlink ~/dotfiles/.gitconfig from $HOME after cloning it. Does anyone else think the feature will be useful?

A couple of similar examples:
1. The git templates directory is configurable via the
GIT_TEMPLATE_DIR variable.
2. The location of ~/.zshrc, ~/.zlogin etc is configurable via the
ZDOTDIR variable in ZSH.
Ram
David Aguilar· Sep 26, 2012, 18:34 UTC · re: Ramkumar Ramachandra · lore

Re: Configuring the location of ~/.gitconfig

On Wed, Sep 26, 2012 at 7:14 AM, Ramkumar Ramachandra <artagnon@gmail.com> wrote:

Show 13 quoted lines
> Hi,
>
> I'd like to configure the location of ~/.gitconfig through an
> environment variable.  My usecase is a simple enough: I have a
> repository with all my dotfiles, and I don't want to symlink
> ~/dotfiles/.gitconfig from $HOME after cloning it.  Does anyone else
> think the feature will be useful?
>
> A couple of similar examples:
> 1. The git templates directory is configurable via the
> GIT_TEMPLATE_DIR variable.
> 2. The location of ~/.zshrc, ~/.zlogin etc is configurable via the
> ZDOTDIR variable in ZSH.
There was some work recently to teach git about the XDG_CONFIG_HOME variable.
Would that help, or are the XDG variables too global for your usage?
-- 
David
Ramkumar Ramachandra· Sep 27, 2012, 14:46 UTC · re: Ramkumar Ramachandra · lore

[PATCH] config: introduce GIT_GLOBAL_CONFIG to override ~/.gitconfig

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 Documentation/git-config.txt |    3 +++
 path.c                       |    5 +++++
 t/t1306-xdg-files.sh         |    8 ++++++++
 3 files changed, 16 insertions(+), 0 deletions(-)
diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt
index eaea079..c8db03f 100644
--- a/Documentation/git-config.txt
+++ b/Documentation/git-config.txt
@@ -205,6 +205,9 @@ $GIT_DIR/config::
 	User-specific configuration file. Also called "global"
 	configuration file.
 
+$GIT_GLOBAL_CONFIG::
+	Overrides the path of the global configuration file.
+
 $XDG_CONFIG_HOME/git/config::
 	Second user-specific configuration file. If $XDG_CONFIG_HOME is not set
 	or empty, $HOME/.config/git/config will be used. Any single-valued
diff --git a/path.c b/path.c
index cbbdf7d..9b09cee 100644
--- a/path.c
+++ b/path.c
@@ -131,10 +131,15 @@ char *git_path(const char *fmt, ...)
 
 void home_config_paths(char **global, char **xdg, char *file)
 {
+	char *global_config = getenv("GIT_GLOBAL_CONFIG");
 	char *xdg_home = getenv("XDG_CONFIG_HOME");
 	char *home = getenv("HOME");
 	char *to_free = NULL;
 
+	if (global_config) {
+		*global = mkpathdup("%s", global_config);
+		return;
+	}
 	if (!home) {
 		if (global)
 			*global = NULL;
diff --git a/t/t1306-xdg-files.sh b/t/t1306-xdg-files.sh
index 8b14ab1..5b0e08e 100755
--- a/t/t1306-xdg-files.sh
+++ b/t/t1306-xdg-files.sh
@@ -28,6 +28,14 @@ test_expect_success 'read config: xdg file exists and ~/.gitconfig exists' '
 	test_cmp expected actual
 '
 
+test_expect_success 'read config: $GIT_GLOBAL_CONFIG is set and ~/.gitconfig exists' '
+	>.gitconfig &&
+	echo "[alias]" >.gittestconfig &&
+	echo "	myalias = !echo in_gitconfig" >>.gittestconfig &&
+	echo in_gitconfig >expected &&
+	GIT_GLOBAL_CONFIG=~/.gittestconfig git myalias >actual &&
+	test_cmp expected actual
+'
 
 test_expect_success 'read with --get: xdg file exists and ~/.gitconfig doesn'\''t' '
 	rm .gitconfig &&
-- 
1.7.8.1.362.g5d6df.dirty
Matthieu Moy· Sep 27, 2012, 16:57 UTC · re: Ramkumar Ramachandra · lore

Re: [PATCH] config: introduce GIT_GLOBAL_CONFIG to override ~/.gitconfig

Ramkumar Ramachandra <artagnon@gmail.com> writes:
Show 11 quoted lines
> diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt
> index eaea079..c8db03f 100644
> --- a/Documentation/git-config.txt
> +++ b/Documentation/git-config.txt
> @@ -205,6 +205,9 @@ $GIT_DIR/config::
>  	User-specific configuration file. Also called "global"
>  	configuration file.
>  
> +$GIT_GLOBAL_CONFIG::
> +	Overrides the path of the global configuration file.
> +

I'm not particularly in favor of introducing another environment variable, but if you are to introduce it, why just override the configuration file, and not $HOME completely (e.g. to override $HOME/.git-credentials too).

There was a patch proposing that here ($GIT_HOME to override $HOME):
  http://thread.gmane.org/gmane.comp.version-control.git/135447/focus=135494

I don't remember exactly what happened to the patch, I can't find an explicit reason to reject it in the thread, but it seems it didn't make its way to git.git.

Show 16 quoted lines
> index cbbdf7d..9b09cee 100644
> --- a/path.c
> +++ b/path.c
> @@ -131,10 +131,15 @@ char *git_path(const char *fmt, ...)
>  
>  void home_config_paths(char **global, char **xdg, char *file)
>  {
> +	char *global_config = getenv("GIT_GLOBAL_CONFIG");
>  	char *xdg_home = getenv("XDG_CONFIG_HOME");
>  	char *home = getenv("HOME");
>  	char *to_free = NULL;
>  
> +	if (global_config) {
> +		*global = mkpathdup("%s", global_config);
> +		return;
> +	}

If you return here, haven't you completely broken the XDG stuff, since *xdg is set a few lines below in the function?

Also, I guess home_config_paths(..., "ignore") will return the path to the configuration file instead of the ignore file?

Show 14 quoted lines
> --- a/t/t1306-xdg-files.sh
> +++ b/t/t1306-xdg-files.sh
> @@ -28,6 +28,14 @@ test_expect_success 'read config: xdg file exists and ~/.gitconfig exists' '
>  	test_cmp expected actual
>  '
>  
> +test_expect_success 'read config: $GIT_GLOBAL_CONFIG is set and ~/.gitconfig exists' '
> +	>.gitconfig &&
> +	echo "[alias]" >.gittestconfig &&
> +	echo "	myalias = !echo in_gitconfig" >>.gittestconfig &&
> +	echo in_gitconfig >expected &&
> +	GIT_GLOBAL_CONFIG=~/.gittestconfig git myalias >actual &&
> +	test_cmp expected actual
> +'

You should check that "git config --set" works too, as the codepath for writing to configuration is relatively different from the one to read. For example, I *think* that "git config --global" will write to $GIT_GLOBAL_CONFIG and "git config" without --global will ignore it, but a test would be welcome.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Junio C Hamano· Sep 27, 2012, 17:50 UTC · re: Matthieu Moy · lore

Re: [PATCH] config: introduce GIT_GLOBAL_CONFIG to override ~/.gitconfig

Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
Show 20 quoted lines
> Ramkumar Ramachandra <artagnon@gmail.com> writes:
>
>> diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt
>> index eaea079..c8db03f 100644
>> --- a/Documentation/git-config.txt
>> +++ b/Documentation/git-config.txt
>> @@ -205,6 +205,9 @@ $GIT_DIR/config::
>>  	User-specific configuration file. Also called "global"
>>  	configuration file.
>>  
>> +$GIT_GLOBAL_CONFIG::
>> +	Overrides the path of the global configuration file.
>> +
>
> I'm not particularly in favor of introducing another environment
> variable, but if you are to introduce it, why just override the
> configuration file, and not $HOME completely (e.g. to override
> $HOME/.git-credentials too).
>
> There was a patch proposing that here ($GIT_HOME to override $HOME):

I think both of these are at the entrance of slippery slope to insanity I'd rather not to venture into.

If somebody hates ~/.dotmanyfiles so much, why not do HOME=~/dots/, instead of having to set GIT_HOME to move ~/.gitconfig, FROTZ_HOME to move ~/.frotzconfig, MAIL_HOME to move ~/.mailrc, etc.?

I wouldn't be surprised if some _other_ things break with your HOME pointing at a directory inside your home directory, but then it may be better to fix that other thing instead.

Junio C Hamano· Sep 27, 2012, 17:27 UTC · re: Ramkumar Ramachandra · lore

Re: [PATCH] config: introduce GIT_GLOBAL_CONFIG to override ~/.gitconfig

Ramkumar Ramachandra <artagnon@gmail.com> writes:
Show 18 quoted lines
> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
> ---
>  Documentation/git-config.txt |    3 +++
>  path.c                       |    5 +++++
>  t/t1306-xdg-files.sh         |    8 ++++++++
>  3 files changed, 16 insertions(+), 0 deletions(-)
>
> diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt
> index eaea079..c8db03f 100644
> --- a/Documentation/git-config.txt
> +++ b/Documentation/git-config.txt
> @@ -205,6 +205,9 @@ $GIT_DIR/config::
>  	User-specific configuration file. Also called "global"
>  	configuration file.
>  
> +$GIT_GLOBAL_CONFIG::
> +	Overrides the path of the global configuration file.
> +
Why is this even a good idea?
  - Does it make sense not to read $HOME/.gitconfig or XDG stuff
    when this is set?  Why skip them?  If it makes sense to skip
    them, why doesn't it also skip $GIT_DIR/config and/or
    /etc/gitconfig?
  - Why is it not "the third user-specific configuration file" instead?
  - Why is it not "a list of paths to read configurations from"?
  - Where does it end?
Not overly impressed, I'd have to say.
Show 34 quoted lines
> diff --git a/path.c b/path.c
> index cbbdf7d..9b09cee 100644
> --- a/path.c
> +++ b/path.c
> @@ -131,10 +131,15 @@ char *git_path(const char *fmt, ...)
>  
>  void home_config_paths(char **global, char **xdg, char *file)
>  {
> +	char *global_config = getenv("GIT_GLOBAL_CONFIG");
>  	char *xdg_home = getenv("XDG_CONFIG_HOME");
>  	char *home = getenv("HOME");
>  	char *to_free = NULL;
>  
> +	if (global_config) {
> +		*global = mkpathdup("%s", global_config);
> +		return;
> +	}
>  	if (!home) {
>  		if (global)
>  			*global = NULL;
> diff --git a/t/t1306-xdg-files.sh b/t/t1306-xdg-files.sh
> index 8b14ab1..5b0e08e 100755
> --- a/t/t1306-xdg-files.sh
> +++ b/t/t1306-xdg-files.sh
> @@ -28,6 +28,14 @@ test_expect_success 'read config: xdg file exists and ~/.gitconfig exists' '
>  	test_cmp expected actual
>  '
>  
> +test_expect_success 'read config: $GIT_GLOBAL_CONFIG is set and ~/.gitconfig exists' '
> +	>.gitconfig &&
> +	echo "[alias]" >.gittestconfig &&
> +	echo "	myalias = !echo in_gitconfig" >>.gittestconfig &&
> +	echo in_gitconfig >expected &&
> +	GIT_GLOBAL_CONFIG=~/.gittestconfig git myalias >actual &&
How is this tilde expanded and by whom?
Jeff King· Sep 27, 2012, 17:35 UTC · re: Ramkumar Ramachandra · lore

Re: [PATCH] config: introduce GIT_GLOBAL_CONFIG to override ~/.gitconfig

On Thu, Sep 27, 2012 at 08:16:11PM +0530, Ramkumar Ramachandra wrote:
Show 11 quoted lines
> diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt
> index eaea079..c8db03f 100644
> --- a/Documentation/git-config.txt
> +++ b/Documentation/git-config.txt
> @@ -205,6 +205,9 @@ $GIT_DIR/config::
>  	User-specific configuration file. Also called "global"
>  	configuration file.
>  
> +$GIT_GLOBAL_CONFIG::
> +	Overrides the path of the global configuration file.
> +

Like the other reviews, I am not overly enthused. If we are going to add a new variable, I think $GIT_HOME makes a lot more sense. But it really sounds like using $XDG_CONFIG_HOME would be even simpler.

Also, have you considered using a config include? Like:
  $ echo '[include]path = ~/my-dotfiles/gitconfig' >~/.gitconfig

It's a one-time setup, and then you get updates inside my-dotfiles forever. The one-time setup is annoying, but you have to bootstrap somehow (e.g., you're going to have to copy a .profile or similar to get the GIT_GLOBAL_CONFIG variable set).

-Peff
Junio C Hamano· Sep 27, 2012, 19:57 UTC · re: Jeff King · lore

Re: [PATCH] config: introduce GIT_GLOBAL_CONFIG to override ~/.gitconfig

Jeff King <peff@peff.net> writes:
> Also, have you considered using a config include? Like:
>
>   $ echo '[include]path = ~/my-dotfiles/gitconfig' >~/.gitconfig
Very good suggestion.
> ... you have to bootstrap
> somehow (e.g., you're going to have to copy a .profile or similar to get
> the GIT_GLOBAL_CONFIG variable set).
Exactly my thought ;-)
Matthieu Moy· Sep 27, 2012, 20:51 UTC · re: Jeff King · lore

Re: [PATCH] config: introduce GIT_GLOBAL_CONFIG to override ~/.gitconfig

Jeff King <peff@peff.net> writes:
Show 8 quoted lines
> Also, have you considered using a config include? Like:
>
>   $ echo '[include]path = ~/my-dotfiles/gitconfig' >~/.gitconfig
>
> It's a one-time setup, and then you get updates inside my-dotfiles
> forever. The one-time setup is annoying, but you have to bootstrap
> somehow (e.g., you're going to have to copy a .profile or similar to get
> the GIT_GLOBAL_CONFIG variable set).

I also strongly prefer symlinks or include over environment variables. Relying on environment variables makes the setup a bit fragile, partly because I often mess things up with my shell (e.g. I once had different shell and environment variables in ~/.xsession and in actual shells, hence different configuration depending on whether I launch stuff from my window-manager or from a shell).

I could understand using environment variables for one-shot configuration change (e.g. HOME=/tmp/ git whatever), but then $HOME is sufficient in general.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Ramkumar Ramachandra· Sep 28, 2012, 07:05 UTC · re: Jeff King · lore

Re: [PATCH] config: introduce GIT_GLOBAL_CONFIG to override ~/.gitconfig

Jeff King wrote:
Show 7 quoted lines
> Like the other reviews, I am not overly enthused. If we are going to add
> a new variable, I think $GIT_HOME makes a lot more sense. But it really
> sounds like using $XDG_CONFIG_HOME would be even simpler.
>
> Also, have you considered using a config include? Like:
>
>   $ echo '[include]path = ~/my-dotfiles/gitconfig' >~/.gitconfig

Sounds like this is the way to go. Or I could pick up Junio's suggestion to use a Makefile. I'll discontinue working on the patch.

Thanks.
Ram
Junio C Hamano· Sep 27, 2012, 06:09 UTC · re: Ramkumar Ramachandra · lore

Re: Configuring the location of ~/.gitconfig

Ramkumar Ramachandra <artagnon@gmail.com> writes:
Show 5 quoted lines
> I'd like to configure the location of ~/.gitconfig through an
> environment variable.  My usecase is a simple enough: I have a
> repository with all my dotfiles, and I don't want to symlink
> ~/dotfiles/.gitconfig from $HOME after cloning it.  Does anyone else
> think the feature will be useful?

Not me. For that particular use case, my approach (long before I switched the vcs that controls my dotfiles to git) have always been to have ~/src that is version controlled, with a Makefile that builds/adjusts dotfiles appropriately for each box and installs them in the proper place.

Of course, if .gitconfig _is_ the same across all boxes I use, that Makefile is likely to "install" by making a symlink ~/.gitconfig that points at src/dot/gitconfig without "building".

Anurag Priyam· Sep 27, 2012, 06:40 UTC · re: Junio C Hamano · lore

Re: Configuring the location of ~/.gitconfig

On Thu, Sep 27, 2012 at 11:39 AM, Junio C Hamano <gitster@pobox.com> wrote:
Show 13 quoted lines
> Ramkumar Ramachandra <artagnon@gmail.com> writes:
>
>> I'd like to configure the location of ~/.gitconfig through an
>> environment variable.  My usecase is a simple enough: I have a
>> repository with all my dotfiles, and I don't want to symlink
>> ~/dotfiles/.gitconfig from $HOME after cloning it.  Does anyone else
>> think the feature will be useful?
>
> Not me. For that particular use case, my approach (long before I
> switched the vcs that controls my dotfiles to git) have always been
> to have ~/src that is version controlled, with a Makefile that
> builds/adjusts dotfiles appropriately for each box and installs them
> in the proper place.

Makefile is what I wanted to avoid when I suggested Ram that maybe Git could _optionally_ read the location of global gitconfig from an environment variable that can be exported in zshenv or bash_profile.

I don't think either way is the best way of managing dotfiles. Just a matter of preference. The environment variable approach doesn't require you to run `make` everytime you sync your dotfiles across different machines, and that is what I like.

Also, Git allows configuring the location of template directory via an environment variable (GIT_TEMPLATE_DIR). Since Git has already fixed the location of global gitconfig, it might as well read the location of template directory from there (init.templatedir). Why the added flexibility? Well, I have been exploiting the feature to manage git templates through my zsh configuration.

-- 
Anurag Priyam
Junio C Hamano· Sep 27, 2012, 17:16 UTC · re: Anurag Priyam · lore

Re: Configuring the location of ~/.gitconfig

Anurag Priyam <anurag08priyam@gmail.com> writes:
Show 9 quoted lines
>> Not me. For that particular use case, my approach (long before I
>> switched the vcs that controls my dotfiles to git) have always been
>> to have ~/src that is version controlled, with a Makefile that
>> builds/adjusts dotfiles appropriately for each box and installs them
>> in the proper place.
>
> Makefile is what I wanted to avoid when I suggested Ram that maybe Git
> could _optionally_ read the location of global gitconfig from an
> environment variable that can be exported in zshenv or bash_profile.

Where exactly do the "zshenv" and "bash_profile" you mention reside, and how are their contents kept up to date? I am guessing it is /net/srv1/home/ram/.bash_profile on machines that mount user home directories from srv1 NFS server and it is to have a string like "export GITCONFIG_FILE=/net/srv1/home/ram/dot/gitconfig" or on those machines. The contents of ~ram/dot/gitconfig may be able to stay the same across boxes, but how is ~ram/.bash_profile is set up? Manually?

Again, I am not personally interested; I think aversion of Make is the root cause of the disease.

← back to recent threads