Using Git submodules to share modified Drupal modules across sites
This reference is currently somewhat specific to Agaric's setup. We currently use Drush to update modules rapidly on a per-site basis (rather than updating a module in a central location and pushing it to the sites that use it), but certain modules have special needs (extra files, a patch that needs to be applied and hasn't been committed to Drupal.org version control yet) and when these modules have to be used on multiple sites, we can save time by bringing them in as submodules. Here are the notes from the first time we did this.
On submoduling, we need our copy of special module to include the excellent modification patch, so I have that working locally for superb client but not committed. I need to commit it somewhere and make it a submodule for both superb client site one and superb client site two
Reading the submodule documentation made me significantly less sure of what i should be doing
Stefan Freudenberg to the rescue.
(Last step first, to add a repo as a submodule it's git submodule add <repo> <workdir path>
.)
First step is creating the repo. Should I take the files out of the repo they are in before doing anything with them? (creating the submodule repo), I ask, but Stefan says that is not necessary. You can start a subproject by running git init in a workdir subdirectory.
The special module is committed to superb client site one but the excellent modification patch, a Drupal "sub"module, is not, but it is physically in my local superb client one working copy.
For regular use (bringing the module into new projects) scroll down to "Pulling a shared module into your site project."
Adding a module for centralized distribution
So still first, create a repository for special module: run git init in the special module directory, within the project. It's possible.
cd /path/to/drupal/sites/all/modules/special_module
git init
Initialized empty Git repository in /path/to/drupal/sites/all/modules/special_module/.git/
Also create a repo on the server, just initialize a bare repository where you can push:
mkdir /srv/git/drupal-modules/specialmodule.git
cd /srv/git/drupal-modules/specialmodule.git
git init --bare
After doing so
git remote add origin git.agariclabs.com:/srv/git/drupal-modules/special_module.git
Try to push. Here we failed a couple times with a wrong path and then still no refs in common until using push origin master.
git commit first
we have initiliazed but not added content ot the repo.
don't forget to git add .
try to git fetch
the remote end hung up unexpectedly
and what is the error message?
that is it
preceded by "fatal:"
Can you try to push again, please.
git push origin master
from your special module directory
if that does not work run git branch
to see what branch you are in
failed also. the git remote add origin path must be wrong?
- master
git.agariclabs.com:/srv/git/drupal-modules/special_module.git
git push origin master worked
now you can try to make it a submodule
got to yourcontaining workdir root
and try git submodule add git.agariclabs.com:/srv/git/drupal-modules/special_module.git
such as drupal/sites/all/modules/special_module
git submodule add git.agariclabs.com:/srv/git/drupal-modules/salesforce.git drupal/sites/default/modules/salesforce
"Adding existing repo"
now check in special_module if you are still on a branch
in your superproject run git submodule status
does the hash value have a plus or minus sign?
... a minus
then run git submodule init
from the main project root
registered path
and now it has no sign
perfect.
it will show a plus if it's not in sync
now enter the special_module directory and run git branch
- master
good so you can still work there. if you check out a new submodule it will have no branch.
you can try to add that submodule in another project now.
Pulling a shared module into your site project
Maybe it will work for you.
cd /var/www/theproject
git submodule add git.agariclabs.com:/srv/git/drupal-modules/special_module.git drupal/sites/default/modules/special_module
git submodule status
git submodule init
git submodule update
cd drupal/sites/default/modules/special_module
git checkout master
cd --
git status
git add .gitmodules
git add drupal/sites/default/modules/special_module/
git commit -m "Installed centrally maintained version of special_module as a submodule."
git push
Comments
Git Submodule Problems
Hi, Thanks for the great post.
The problem I cant understand with Git Submodules, is that firstly, if you clone down a project with submodules included and then: git submodule init, git submodule update, you get them on (no branch) which is annoying as you cant update them and need to switch to master or a branch if you want to make a change and push.
Secondly, if you commit your parent project then it attaches a state of the submodules tag with the commit.
What we find sometimes is that another developer hasnt noticed or been told a submodule has been updated, so his next commits will push the submodules apparent folder version back and the next commit from a developer with the newer version will do the same thing back.
That just seems fail to me.
So I want to know is there a better way to manage these submodules, so that users get the master branch by default, and perhaps Tortoise Git is able to be given the option to update submodules with parent updates at the same time.
Or is there a command thats like Git pull, but includes submodules as well all in one?
I guess we could write a script but it seems like this is something that should already exist.
Looking forwards to your feedback! :)
Cheers,
Madhava Jay
Kintek.com.au
Post new comment