【问题标题】:Bash: Git submodule foreach?Bash:Git 子模块 foreach?
【发布时间】:2011-12-03 01:25:15
【问题描述】:

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

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

【问题讨论】:

    标签: git bash foreach git-submodules


    【解决方案1】:

    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'
    

    【讨论】:

    • 对于任何 google 用户,我将其改编为嵌套子模块 git submodule foreach 'git submodule init && git submodule update' - 发现它使具有大量插件的项目更容易生活;)
    • submodule foreach 也支持--recursive,因此您也可以使用相同的 shell 命令遍历嵌套的子模块。
    【解决方案2】:

    larsksanswer 使用:

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

    但现在(2020 年)应该是

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

    随着 Git 2.26(2020 年第一季度),大部分“git submodule foreach”已用 C 重写,其文档也在不断发展。

    参见commit fc1b924(2018 年 5 月 10 日)和commit b6f7ac8commit f0fd0dccommit c033a2f(2018 年 5 月 9 日)Prathamesh Chavan (pratham-pc)
    (由@987654330 中的Junio C Hamano -- gitster -- 合并@,2018 年 6 月 25 日)

    submodule foreach:文档 '$sm_path' 而不是 '$path'

    由于大小写问题,使用变量 '$path' 可能对用户有害,请参阅64394e3ae9 ("git submodule.sh: Don't use $path variable in eval_gettext string", 2012-04 -17,Git v1.7.11-rc0 -- merge 列在batch #4)。
    调整文档以提倡使用$sm_path,其中包含相同的值。
    我们仍然使“path”变量可用,并将其记录为“sm_path”的已弃用同义词

    讨论者:拉姆齐·琼斯

    Documentation/git-submodule#foreach 现在包括:

    foreach [--recursive] <command>:

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

    该命令可以访问变量$name$sm_path$sha1$toplevel

    • $name.gitmodules 中相关子模块部分的名称。
    • $sm_path 是直接超级项目中记录的子模块的路径,
    • $sha1 是直接超级项目中记录的提交,并且
    • $toplevel 是直接超级项目顶层的绝对路径。

    请注意,为避免在 Windows 上与“$PATH”发生冲突,“$path”变量现在是“$sm_path”变量的已弃用同义词

    【讨论】:

      【解决方案3】:

      将以下内容添加到.git/config

      [alias]
          sup = "submodule foreach 'if [ $name == \"Libraries/JSONKit\" ]; then git co experimental; else git co master; fi; git up'"
      

      【讨论】:

      • 尽管它是经过验证的答案,但为什么会被否决?我自己没有尝试过确切的命令,尽管在我的情况下添加别名非常有用,可以在 git foreach 中轻松启动复杂的 LFS 命令:[alias] track-large = !"git st --porcelain --ignore -submodules | grep -v \"D \" | awk '{ l=length($0); s=substr($0,4,l-1); print s}' | sed -n 's/\(\( .* -> *\)\\|\)\(.*\)/\\3/p' | xargs -I{} find {} -size +300k | xargs -I{} git lfs track \"{ }\""
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多