git config 中的 Shell 变量扩展

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11262010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:09:26  来源:igfitidea点击:

Shell variable expansion in git config

gitshell

提问by SvenK

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.

我有一个 shell 变量,它指向我所有配置文件所在的目录。让我们假设变量是用export RC=$HOME/rc. 我在配置目录中有一个全局忽略文件:~/rc/globalgitignore.

My Question is, how can I expand the RCvariable in my .gitconfigfile?

我的问题是,如何扩展文件中的RC变量.gitconfig

I already tried the following:

我已经尝试了以下方法:

  • excludesfile = $RC/globalgitignore

  • excludesfile = !$RC/globalgitignore

  • excludesfile = !echo $RC/globalgitignore

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

  • 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 rcdirectory to a different location.

如果我输入完整路径excludesfile = ~/rc/globalgitignore,这是唯一有效的方法:,但是如果将我的rc目录移动到不同的位置,我必须更改路径。

采纳答案by lunaryorn

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

你不能。git-config(1)不支持环境变量扩展,只支持有限的类型转换和路径扩展:

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 specifieris passed, no checks or transformationsare performed on the value.

类型说明符可以是 --int 或 --bool,以使 git config 确保变量属于给定类型并将值转换为规范形式(int 的简单十进制数,“true”或bool 的 "false" 字符串) 或 --path,它会进行一些路径扩展(请参阅下面的 --path)。如果传递类型说明符,则不会对该值执行任何检查或转换

The documentation for --pathstates:

--path状态的文档:

--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).

- 小路

git-config 将扩展前导 ~ 到 $HOME 的值,并将 ~user 扩展到指定用户的主目录。此选项在设置值时无效(但您可以从命令行使用 git config bla ~/ 来让您的 shell 进行扩展)。

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?

术语“扩展”没有出现在git-config(1). 那么,鉴于没有任何地方记录此类功能,您是如何想到它应该这样做的呢?

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 $HOMEdirectory.

为了扩展环境变量,您必须自己预处理 Git 配置文件,即通过创建模板文件,并在将文件复制到您的$HOME目录之前使用脚本扩展变量。

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.

如果是关于 dotfile 管理,那么做所有人都做的事情:将它们放在一个目录中,然后将符号链接从您的$HOME.

回答by gospes

I use bash scripts in my config to enable variable expansion. Just export the variable you need in your .bashrc and use it in the scripts:

我在我的配置中使用 bash 脚本来启用变量扩展。只需在 .bashrc 中导出您需要的变量并在脚本中使用它:

In my ~/.bashrc:

在我的 ~/.bashrc 中:

export TESTVARIABLE="hello"

In my ~/.gitconfig:

在我的 ~/.gitconfig 中:

[alias]
    test = !bash -c '"echo \"Value: $TESTVARIABLE\";"'

At my bash prompt:

在我的 bash 提示符下:

bash> git test
    Value: hello 


The other way of doing it, is to add a git config command in your shell's rc. I for instance have in my .bashrc:

另一种方法是在你的 shell 的 rc 中添加一个 git config 命令。例如,我的 .bashrc 中有:

git config --global user.name "$USER@$HOSTNAME"

I have the same configuration on all my machines, and by adding this I can distinguish between commits from different machines. You could do the same and add to your shell rc:

我在所有机器上都有相同的配置,通过添加它,我可以区分来自不同机器的提交。你可以做同样的事情并添加到你的 shell rc:

export RC="$HOME/rc"
git config --global core.excludesfile "$RC/globalgitignore"