gpt4 book ai didi

bash - 如何将变量注入(inject) git submodule foreach?

转载 作者:行者123 更新时间:2023-12-04 14:23:51 25 4
gpt4 key购买 nike

我有 2 个问题,(1) 如何将变量注入(inject)这个 foreach环形?在下面的例子中,$user_branch无法在第 2 行访问。

USER_BRANCH="foo"
git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; [ "$branch" = "" ] && git checkout $USER_BRANCH || git checkout $branch' –

(2) 有没有办法让这个 foreach进入常规循环?,类似于:
foreach $(git submodules --recursive); do
#do something
#more
#and more
done

最佳答案

解决您的第一个问题,问题是您使用的是单引号,因此您的变量不会被扩展。切换到双引号应该可以修复该部分。

git submodule foreach -q --recursive "git checkout $user_branch"

对于第二个问题, documentation建议您可以在每个模块中运行任意 shell 命令,因此虽然您不能像 bash for 循环那样构造它,但您可以运行 shell 脚本或由 ; 连接的多个命令。 .

foreach [--recursive]

Evaluates an arbitrary shell command in each checked out submodule. The command has access to the variables $name, $path, $sha1 and $toplevel: $name is the name of the relevant submodule section in .gitmodules, $path is the name of the submodule directory relative to the superproject, $sha1 is the commit as recorded in the superproject, and $toplevel is the absolute path to the top-level of the superproject. Any submodules defined in the superproject but not checked out are ignored by this command. Unless given --quiet, foreach prints the name of each submodule before evaluating the command. If --recursive is given, submodules are traversed recursively (i.e. the given shell command is evaluated in nested submodules as well). A non-zero return from the command in any submodule causes the processing to terminate. This can be overridden by adding || : to the end of the command.

关于bash - 如何将变量注入(inject) git submodule foreach?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49929938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com