在git config中使用shell变量扩展 [英] Shell variable expansion in git config

查看:133
本文介绍了在git config中使用shell变量扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我的问题是我可以在 .gitconfig 文件中展开 RC 变量吗?



我已经尝试了以下方法:


  • excludesfile = $ RC / globalgitignore


  • excludesfile =!$ RC / globalgitignore


  • excludesfile =!echo $ RC / globalgitignore


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




无这些解决方案的工作。



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

解决方案

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


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


- path 的文档声明:


- 路径



git-config将扩展至$ HOME的值,并将〜user扩展至指定用户的主目录。这个选项在设置值时不起作用(但是你可以在命令行中使用git config bla〜/让shell执行扩展)。

术语扩展不出现在 git-config(1)中的任何不同的上下文中。那么你是怎么想到它应该这样做的,因为没有这样的功能被记录在任何地方?

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



如果是关于dotfile管理,那么做,所有人都做:将它们放在一个目录中,并从 $ HOME 中添加符号链接到这个目录。


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.

解决方案

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.

这篇关于在git config中使用shell变量扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆