Bash:foreach的Git子模块?

问题描述 投票:9回答:3

我将sup别名为submodule foreach 'git co master; git up'coup分别为checkoutpull --rebase的别名。)。

如何添加条件,以便如果子模块名称为Libraries/JSONKit,它签出名为experimental的分支,而不是master

git bash foreach git-submodules
3个回答
14
投票

传递给git submodule的脚本passwd的工作目录位于给定子模块的顶部...因此您可以简单地查看pwd来查看您是否在特定的子模块中。但是,如果您花一些时间阅读git submodule文档,事实证明它甚至更容易:

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

因此您可以执行以下操作:

git submodule foreach '[ "$path" = "Libraries/JSONKit" ] \
  && branch=experimental \
  || branch=master; git co $branch'

1
投票

[larsksanswer使用:

git submodule foreach '[ "$path" = "Libraries/JSONKit" ]

但现在应该是(2020年)

git submodule foreach '[ "$sm_path" = "Libraries/JSONKit" ]

[使用Git 2.26(Q1 2020),大量的[git submodule foreach]已经用C重写,并且其文档也随之发展。

请参见git submodule foreach之前的commit fc1b924(2018年5月10日)和commit b6f7ac8commit f0fd0dccommit c033a2f(2018年5月9日)。((由Prathamesh Chavan (pratham-pc)合并于pratham-pc,2018年6月25日)>

[Junio C Hamano -- gitster --:文档'gitster',而不是'$ path']

由于大写问题,使用变量'$ path'可能对用户有害,请参阅commit ea27893(“submodule foreach.sh:请勿在submodule foreach字符串中使用$ path变量”,2012-04- 17,Git v1.7.11-rc0-$sm_path中列出的64394e3ae9)。调整文档以主张使用git submodule,该文档包含相同的值。我们仍然提供'git submodule'变量,并将其记录为'eval_gettext'的不赞成使用的同义词。

与讨论:Ramsay Jones

merge现在包括:

batch #4

在每个检出的子模块中评估一个任意的shell命令。

该命令可以访问变量$sm_pathpathsm_pathDocumentation/git-submodule#foreach

  • [Documentation/git-submodule#foreachforeach [--recursive] <command>中相关子模块节的名称。
  • $name是直接超级项目中记录的子模块的路径,
  • $sm_path是直接超级项目中记录的提交,而
  • $sha1是到直接超级项目顶层的绝对路径。
  • 注意,为避免与Windows上的'$toplevel'冲突,'$name'变量现在已不再使用'.gitmodules'变量的同义词


-1
投票

将以下内容添加到$sm_path

© www.soinside.com 2019 - 2024. All rights reserved.