How to add optional submodules in git?
Asked Answered
C

1

13

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"?

Cowherb answered 5/3, 2018 at 12:53 Comment(0)
A
1

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

Alderney answered 9/12, 2019 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.