简体   繁体   English

通过shell变量将配置选项传递给git clone

[英]Passing config options via shell variable to git clone

I'm trying to pass dynamically created configuration options to git clone via an environment variable on bash. 我正在尝试通过bash上的环境变量将动态创建的配置选项传递给git clone

Passing them directly works, but it does not work via the env variable: 直接传递它们有效,但不能通过env变量起作用:

$ git clone -c 'url.foo.insteadof=bar' git://git.cweiske.de/psist.git
... all fine

$ export PARAMS="-c 'url.foo.insteadof=bar'"; git clone $PARAMS git://git.cweiske.de/psist.git
error: invalid key: 'url.foo.insteadof

What can I do to make git recognize the options? 如何使git识别选项?

That's because in the first example the quotes are syntactic: 这是因为在第一个示例中, 引号是句法上的:

$ (set -o xtrace; git clone -c 'url.foo.insteadof=bar' git://invalid) 2>&1 | grep 'git clone'
+ git clone -c url.foo.insteadof=bar git://invalid

while in the second they are literal: 而在第二个中,它们是原义的:

$ (set -o xtrace; export PARAMS="-c 'url.foo.insteadof=bar'" && git clone $PARAMS git://invalid) 2>&1 | grep 'git clone'
+ git clone -c ''\''url.foo.insteadof=bar'\''' git://invalid

You can use arrays to reliably pass arguments using variables : 您可以使用数组来可靠地使用变量传递参数

$ (set -o xtrace; export PARAMS=('-c' 'url.foo.insteadof=bar') && git clone "${PARAMS[@]}" git://invalid) 2>&1 | grep 'git clone'
+ git clone -c url.foo.insteadof=bar git://invalid

You could use eval to get the parameters passed correctly. 您可以使用eval来正确传递参数。

export PARAMS="-c 'url.foo.insteadof=bar'"; 
eval git clone $PARAMS git://git.cweiske.de/psist.git

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM