Skip to content

Instantly share code, notes, and snippets.

@Anmo
Last active May 30, 2024 13:04
Show Gist options
  • Save Anmo/02c021bdd159f5962963 to your computer and use it in GitHub Desktop.
Save Anmo/02c021bdd159f5962963 to your computer and use it in GitHub Desktop.
Use of submodules and sparse-checkout
#!/bin/bash
#First create a repo
mkdir A && cd A && git init && touch a.dev && touch a.prod && git add -A && git commit -m 'init A' && cd ../
#Lets create another repo that will use A as submodule with sparse-checkout
mkdir B && cd B && git init && touch b && git add -A && git commit -m 'init B'
#Now we will clone A as submodule of B and will say that file/dir we only want to use in B
git submodule add ../A/ A && cd A && git config core.sparsecheckout true && echo a.prod >> ../.git/modules/A/info/sparse-checkout && git read-tree -mu HEAD && cd ../ && git add -A && git commit -m 'add A as submodule/sparse-checkout' && cd ../
#The problem, when cloning B, is that sparse is not applied
git clone --recursive B/ C
#you can check that the submodule A was all checkout
#To accomplished the sparse checkout you can run the 3rd command (without the mkdir parte) or,
#you can have a script like sparseCO.js and put it in the scripts.prepublish in package.json
cd B && echo '{"scripts":{"prepublish":"node sparseCO.js"}}' >> package.json && echo '!function(){var a={A:["a.prod"]},b="echo ",c=" && ",d="git config core.sparsecheckout true",e="git read-tree -mu HEAD",f=[];for(var g in a){var h=[" >> ../.git/modules/",g,"/info/sparse-checkout"].join(""),i=b+a[g].join([h,b].join(c))+h;f.push([g,d,i,e,"cd ../"].join(c))}require("child_process").exec("cd "+f.join(""),function(){})}();' >> sparseCO.js && git add -A && git commit -m 'create package.json and sparseCO.js' && cd ../
#and then, run this clone command:
git clone --recursive B/ D && cd D && npm install && cd ../
#NOTE: --recursive is just to checkout recursively, this mean, checkout submodules
{
"name": "B",
"version": "0.0.1",
"description": "B with submodule A with sparse-checkout",
"main": "b",
"scripts": {
"prepublish": "node sparseCO.js"
},
"repository": {
"type": "git",
"url": "git@github.com:user/B.git"
},
"keywords": [
"Submodule",
"Sparse checkout"
],
"author": "user <mail> (https://github.com/user/)",
"license": "MIT"
}
;(function( ) {
//Were you can define what submodule do you have and each sparse you want to checkout
var subMod_sparse = {
A : [ 'a.prod' ]
};
var e = 'echo ';
var aa = ' && ';
var gsct = 'git config core.sparsecheckout true';
var grth = 'git read-tree -mu HEAD';
var cmd = [ ];
for ( var sM in subMod_sparse ) {
var path = [ ' >> ../.git/modules/' , sM , '/info/sparse-checkout' ].join( '' );
var sparse = e + subMod_sparse[ sM ].join( [ path , e ].join( aa ) ) + path;
cmd.push( [ sM , gsct , sparse , grth , 'cd ../' ].join( aa ) );
}
require( 'child_process' ).exec( 'cd ' + cmd.join( '' ) , function( error , stdout , stderr ) { });
})( );
#!/bin/bash
#last, to update submodules you can just do
#git submodule foreach git pull origin master
#change A
cd A && echo 'test1' >> a.prod && git commit -am 'change a.prod' && cd ../
#update submodule
cd B && git submodule foreach git pull origin master && git commit -am 'update submodule A' && cd ../
#NOTE: if change other things in B, and then in D do git pull, you will not get the changes in A
#for update submodule A in clone D you have to do the submodule foreach git pull command
@James-E-Adams
Copy link

This helped me a lot. Thanks for posting it! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment