Search code examples
gitcontinuous-integrationgit-submodules

Add conditional logic after "git submodule foreach"


I am working on a script to update submodules for various projects in a CI environment. The single script needs to take care following three cases:

  1. Project that always needs master branch of the submodule.
  2. Project that has reference to specific branch of the submodule.
  3. Project that has reference to specific commit of the submodule.

Is it possible to create this single script to handle the submodule update from the parent project for all these different cases?

git submodule foreach git pull would handle case 1 and 2 but not 3.

git submodule foreach git pull origin master would handle 1 but also 3 in some sense as it won't fail.

Is there anyway to add conditional logic after foreach so I can get all submodules properly updated? Or am I trying to fix the problem in a wrong way? Any help would be appreciated!


Solution

  • You don't need to pull explicitly: you can configure a submodule to mention the branch it needs to update.
    See "git submodule tracking latest".

    And you can convert a existing submodule in order to add that branch information.
    See "Git submodules: Specify a branch/tag".

    From there, a simple command would take care of the pull for you:

    git submodule update --recursive --remote