I have a git project with several sub-modules (a choice I somewhat regret in retrospect). It uses gnu make
Currently I expect people to issue by hand a git submodule update --init
1 before building the project for the first time and also after pulling any change that updated a submodule reference.
However, I'd like for the Makefile to issue these commands automatically when they are needed. It's OK if they issue the commands sometimes when they aren't needed (spurious update) - but it shouldn't happen regularly.
For the initial init
it seems enough to have a rule like (for a submodule that lives in directory module1
:
module1/.git:
git submodule update --init
And here the choice of .git
as the file to "represent" the submodule is fairly arbitrary, it could be some other file instead.
However this doesn't work that well to update the submodule when the reference has been updated. I guess I could make the submodule depend on the root .gitmodules
file which I guess should change when a submodule reference gets updated, something like:
module1/.git: .gitmodules
git submodule update --init
Here the use of .git
seems wrong though: presumably that directly won't necessarily be updated when update
is run (especially if there was no update to this particular submodule), which will leave the update command running every time.
Looking for a cleaner solution here.
1 Or possibly use the --recursive
argument on the initial clone
, which has the same effect.
I've crafted something like this:
.PHONY: check-and-reinit-submodules
check-and-reinit-submodules:
@if git submodule status | egrep -q '^[-]|^[+]' ; then \
echo "INFO: Need to reinitialize git submodules"; \
git submodule update --init; \
fi
It is using git submodule status
, to figure out if submodule is not initialized (-
at the beginning) or somehow modified/outdated (+
at the beginning). This make goal will run always, but git submodule update --init
will run only when needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With