Continue looping over submodules with the “git submodule foreach” command after a non-zero exit

前端 未结 2 1801
傲寒
傲寒 2020-12-13 06:36

I have a project that contains many submodules. I want to loop over each submodule with the following command:

git submodule foreach npm install

2条回答
  •  盖世英雄少女心
    2020-12-13 06:50

    Just make your command always return a 0 code like so:

    git submodule foreach 'npm install || :'
    

    This is taken from the manual: git help 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. 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.
    
           As an example, git submodule foreach 'echo $path `git rev-parse
           HEAD`' will show the path and currently checked out commit for each
           submodule.
    

    The command : from help : in bash:

    :: :
        Null command.
    
        No effect; the command does nothing.
    
        Exit Status:
        Always succeeds.
    

    Always succeeds :)

提交回复
热议问题