Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell variable expansion in git config

Tags:

git

shell

I have a shell variable which points to the directory where all my configuration files are located. Let's assume the variable is created with export RC=$HOME/rc. I have a global ignore file in the configuration directory: ~/rc/globalgitignore.

My Question is, how can I expand the RC variable in my .gitconfig file?

I already tried the following:

  • excludesfile = $RC/globalgitignore

  • excludesfile = !$RC/globalgitignore

  • excludesfile = !echo $RC/globalgitignore

  • excludesfile = !$(echo $RC/globalgitignore)

None of these solutions work.

The only way this works if I enter the full path: excludesfile = ~/rc/globalgitignore, but then I have to change the path if move my rc directory to a different location.

like image 465
SvenK Avatar asked Jun 29 '12 12:06

SvenK


People also ask

Can I use environment variables in Gitconfig?

In order to expand environment variables you have to pre-process the Git config file yourself, i.e. by creating a template file, and expand variables with a script before copying the file to your $HOME directory.

What is ~/ Gitconfig?

gitconfig is used to store a per-user configuration as fallback values for the . git/config file. The file /etc/gitconfig can be used to store a system-wide default configuration. The configuration variables are used by both the Git plumbing and the porcelains.

How do I change my git config list?

Show global git config settings But at runtime, only the value set locally is used. If you would like to delete or edit a Git config value manually with a text editor, you can find the Git config file locations through the Git config list command's –show-origin switch.


1 Answers

You can't. git-config(1) does not support environment variable expansion, but only limited type conversion and path expansion:

The type specifier can be either --int or --bool, to make git config ensure that the variable(s) are of the given type and convert the value to the canonical form (simple decimal number for int, a "true" or "false" string for bool), or --path, which does some path expansion (see --path below). If no type specifier is passed, no checks or transformations are performed on the value.

The documentation for --path states:

--path

git-config will expand leading ~ to the value of $HOME, and ~user to the home directory for the specified user. This option has no effect when setting the value (but you can use git config bla ~/ from the command line to let your shell do the expansion).

The term "expansion" does not appear in any different context in git-config(1). So how did you even get the idea that it should, given that no such feature is documented anywhere?

In order to expand environment variables you have to pre-process the Git config file yourself, i.e. by creating a template file, and expand variables with a script before copying the file to your $HOME directory.

If it's about dotfile management, then do, what all people do: Put them in a directory, and add symlinks to this directory from your $HOME.

like image 104
lunaryorn Avatar answered Sep 24 '22 11:09

lunaryorn