@@ -266,3 +266,62 @@ func TestFromSubdir(t *testing.T) {
266266 )
267267 require .NoError (t , err , "scanning repository" )
268268}
269+
270+ func TestSubmodule (t * testing.T ) {
271+ t .Parallel ()
272+ path , err := ioutil .TempDir ("" , "submodule" )
273+ require .NoError (t , err , "creating temporary directory" )
274+
275+ defer func () {
276+ os .RemoveAll (path )
277+ }()
278+
279+ timestamp := time .Unix (1112911993 , 0 )
280+
281+ submPath := filepath .Join (path , "subm" )
282+ cmd := exec .Command ("git" , "init" , submPath )
283+ require .NoError (t , cmd .Run (), "initializing subm repo" )
284+ submRepo , err := git .NewRepository (submPath )
285+ require .NoError (t , err , "initializing subm Repository object" )
286+ addFile (t , submPath , submRepo , "submfile1.txt" , "Hello, submodule!\n " )
287+ addFile (t , submPath , submRepo , "submfile2.txt" , "Hello again, submodule!\n " )
288+ addFile (t , submPath , submRepo , "submfile3.txt" , "Hello again, submodule!\n " )
289+
290+ cmd = gitCommand (t , submRepo , "commit" , "-m" , "main initial" )
291+ addAuthorInfo (cmd , & timestamp )
292+ require .NoError (t , cmd .Run (), "creating subm commit" )
293+
294+ mainPath := filepath .Join (path , "main" )
295+ cmd = exec .Command ("git" , "init" , mainPath )
296+ require .NoError (t , cmd .Run (), "initializing main repo" )
297+ mainRepo , err := git .NewRepository (mainPath )
298+ require .NoError (t , err , "initializing main Repository object" )
299+ addFile (t , mainPath , mainRepo , "mainfile.txt" , "Hello, main!\n " )
300+
301+ cmd = gitCommand (t , mainRepo , "commit" , "-m" , "subm initial" )
302+ addAuthorInfo (cmd , & timestamp )
303+ require .NoError (t , cmd .Run (), "creating main commit" )
304+
305+ // Make subm a submodule of main:
306+ cmd = gitCommand (t , mainRepo , "submodule" , "add" , submPath , "sub" )
307+ cmd .Dir = mainPath
308+ require .NoError (t , cmd .Run (), "adding submodule" )
309+
310+ // Analyze the main repo:
311+ h , err := sizes .ScanRepositoryUsingGraph (
312+ mainRepo , git .AllReferencesFilter , sizes .NameStyleNone , false ,
313+ )
314+ require .NoError (t , err , "scanning repository" )
315+ assert .Equal (t , counts .Count32 (1 ), h .UniqueBlobCount , "unique blob count" )
316+ assert .Equal (t , counts .Count32 (1 ), h .MaxExpandedBlobCount , "max expanded blob count" )
317+
318+ // Analyze the submodule:
319+ submRepo2 , err := git .NewRepository (filepath .Join (mainPath , "sub" ))
320+ require .NoError (t , err , "creating Repository object in submodule" )
321+ h , err = sizes .ScanRepositoryUsingGraph (
322+ submRepo2 , git .AllReferencesFilter , sizes .NameStyleNone , false ,
323+ )
324+ require .NoError (t , err , "scanning repository" )
325+ assert .Equal (t , counts .Count32 (2 ), h .UniqueBlobCount , "unique blob count" )
326+ assert .Equal (t , counts .Count32 (3 ), h .MaxExpandedBlobCount , "max expanded blob count" )
327+ }
0 commit comments