我有我的dotfiles设置,我有一件事是.gitconfig。问题出在我的OSX机器上,希望助手为osxkeychain
,但是对于我的Linux机器,我想要cache --timeout 3600
。有没有办法在gitconfig文件中进行操作系统检测?也许像这样:
[credential os=apple]
helper = osxkeychain
[credential os=linux]
helper = cache --timeout 3600
您可以在全局conditional include文件.gitconfig
中定义一个since Git 2.13。
但是它仅在存储库的路径上现在适用。
因此,如果您可以在两台不同的计算机上的两条不同路径中设置存储库,则可以基于这些路径来关联一个包括相关帮助程序的全局设置。
; include for all repositories inside /path/to/group/osx
[includeIf "gitdir:/path/to/group/osx"]
helper = osxkeychain
; include for all repositories inside /path/to/group/linux
[includeIf "gitdir:/path/to/group/linux"]
helper = cache --timeout 3600
替代方法是设置setting per branch(自Git 2.23起,但是与OS的概念并不吻合(分支“ linux”,分支“ OSx”:没有太大意义)]
[无法在Git配置中进行OS配置。但是,有两种可能的方法。
一个是为了利用每个用户的配置文件~/.gitconfig
和~/.config/git/config
这两个事实(尽管如果设置了$XDG_CONFIG_HOME
,后者可能会有所不同)。我将常用配置存储在前者中,将任何自定义的每台计算机配置(不受Git管理)存储在后者中,例如凭据助手和签名子项。如果您确实使用Git管理文件,则可能在您的dotfile文件中有一个脚本可以将适当的文件复制到后者中。
另一个选项是将凭据助手指定为shell命令,它当然具有shell的全部功能。因此,您可以执行以下操作:
[credential]
helper = "!f() { if [ \"$(uname -s)\" = Linux ]; then git credential-cache --timeout 3600 \"$@\"; else git credential-osxkeychain \"$@\"; fi; };f"
这将根据您所运行的操作系统调用适当的帮助程序。