4

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!

Sheng
  • 636
  • 7
  • 11

1 Answers1

3

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
Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • One quick question, if we establish the development workflow to be something like every project has static references to submodules and if you know you want to update the submodule commit you want to use, do it in your project and commit the submodule reference in the super project. If this is in place, I guess I don't need to ask the CI tool to track the latest automatically and the command below should be enough, right? `git submodule update --recursive` – Sheng Aug 06 '14 at 16:22
  • 1
    @Sheng yes, that should be enough – VonC Aug 06 '14 at 17:16
  • Thank you very much! Later on when we need to add symbolic submodule links in our projects, I will read through the links you posted here more carefully and I believe they are going to be very helpful. BTW, we just changed our repository from SVN to Git so it's been really not an easy switch from the use of external links to submodules. – Sheng Aug 11 '14 at 15:24
  • 1
    @Sheng Yes, I mentioned that case (svn external for Git) before: http://stackoverflow.com/a/18088319/6309. Before git 1.8.2, the two features were quite incompatible: http://stackoverflow.com/a/3132221/6309 – VonC Aug 11 '14 at 15:26