Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add optional submodules in git?

When we have many (say 20) submodule in a git repository, we can install (and update) them like so:

git submodules update --init --recursive 

Git tries to download every submodule (recursively) after this command. What if we want to make some of the submodules optional (like a plugin)?

How can we make git skip downloading these optional submodules by default and handle as a usual submodule when we mark this submodule "okay, use this from now on"?

like image 749
ceremcem Avatar asked Mar 05 '18 12:03

ceremcem


People also ask

Does git pull include submodules?

Pulling with submodules. Once you have set up the submodules you can update the repository with fetch/pull like you would normally do. To pull everything including the submodules, use the --recurse-submodules and the --remote parameter in the git pull command .


1 Answers

One way to do this would be to :

  1. Get the list of submodules for the repo
  2. Get user input for each submodule (whether to update or not)
  3. Exclude the submodules selected by the user and,
  4. Update the submodules

To get the list of submodules and format it to display only submodule path :

git submodule--helper list | awk '{$1=$2=$3=""; print substr($0,4)}'

To exclude a submodule X :

git -c submodule."X".update=none submodule update --init --recursive

Combining these two commands with xargs and tr utilities and further using command substitutition,

git $(git submodule--helper list | awk '{$1=$2=$3=""; print substr($0,4)}' | xargs -pI % echo -c submodule.\"%\".update=none | tr '\n' ' '| xargs echo) submodule update --init --recursive

The interactive mode of xargs util allows user to select which submodule to update i.e by supplying input in (y/n) format. Input y indicates that submodule is excluded during the update. Input n indicates no action is taken.

Note:

  • The submodule name here is assumed to be the same as it's path. However, have a look here to get submodule names with diverging names and paths.

  • To exclude or include the submodule X (permanently), the attribute submodule."X".update should be added to the gitconfig file for the local repo. For exclusion set submodule."X".updateto none in the config file by using this command

     git config --local submodule."X".update none
    

    and for inclusion unset it by using this command

    git config --local --unset submodule."X".update
    
  • For excluding nested submodules, follow this

like image 142
Saurabh P Bhandari Avatar answered Sep 22 '22 02:09

Saurabh P Bhandari