@@ -378,6 +378,169 @@ describe('Test fetchDepth and fetchTags options', () => {
378378 } )
379379} )
380380
381+ describe ( 'repository object format' , ( ) => {
382+ beforeEach ( async ( ) => {
383+ jest . spyOn ( fshelper , 'fileExistsSync' ) . mockImplementation ( jest . fn ( ) )
384+ jest . spyOn ( fshelper , 'directoryExistsSync' ) . mockImplementation ( jest . fn ( ) )
385+ } )
386+
387+ afterEach ( ( ) => {
388+ jest . restoreAllMocks ( )
389+ } )
390+
391+ it ( 'detects SHA-256 from a 64-character HEAD oid' , async ( ) => {
392+ mockExec . mockImplementation ( ( path , args , options ) => {
393+ if ( args . includes ( 'version' ) ) {
394+ options . listeners . stdout ( Buffer . from ( 'git version 2.50.1' ) )
395+ }
396+
397+ if ( args . includes ( 'ls-remote' ) ) {
398+ options . listeners . stdout (
399+ Buffer . from (
400+ 'ref: refs/heads/main\tHEAD\n' +
401+ '9422233ca7ee1b17f1e905d0e141faf0c401556c41cdc6acd71c6bd685da2e92\tHEAD\n'
402+ )
403+ )
404+ }
405+
406+ return 0
407+ } )
408+ jest . spyOn ( exec , 'exec' ) . mockImplementation ( mockExec )
409+
410+ git = await commandManager . createCommandManager ( 'test' , false , false )
411+
412+ const objectFormat = await git . tryGetObjectFormat (
413+ 'https://github.com/example/repo'
414+ )
415+
416+ expect ( objectFormat ) . toEqual ( { format : 'sha256' , succeeded : true } )
417+ expect ( mockExec ) . toHaveBeenCalledWith (
418+ expect . any ( String ) ,
419+ [
420+ '-c' ,
421+ 'protocol.version=2' ,
422+ 'ls-remote' ,
423+ '--quiet' ,
424+ '--exit-code' ,
425+ '--symref' ,
426+ 'https://github.com/example/repo' ,
427+ 'HEAD'
428+ ] ,
429+ expect . objectContaining ( {
430+ ignoreReturnCode : true ,
431+ silent : true
432+ } )
433+ )
434+ } )
435+
436+ it ( 'detects SHA-1 from a 40-character HEAD oid' , async ( ) => {
437+ mockExec . mockImplementation ( ( path , args , options ) => {
438+ if ( args . includes ( 'version' ) ) {
439+ options . listeners . stdout ( Buffer . from ( 'git version 2.50.1' ) )
440+ }
441+
442+ if ( args . includes ( 'ls-remote' ) ) {
443+ options . listeners . stdout (
444+ Buffer . from (
445+ 'ref: refs/heads/main\tHEAD\n' +
446+ 'c988866043f035e6a46509872215f91d879044c9\tHEAD\n'
447+ )
448+ )
449+ }
450+
451+ return 0
452+ } )
453+ jest . spyOn ( exec , 'exec' ) . mockImplementation ( mockExec )
454+
455+ git = await commandManager . createCommandManager ( 'test' , false , false )
456+
457+ await expect (
458+ git . tryGetObjectFormat ( 'https://github.com/example/repo' )
459+ ) . resolves . toEqual ( { format : 'sha1' , succeeded : true } )
460+ } )
461+
462+ it ( 'returns unsuccessful when HEAD does not resolve to a recognized object id' , async ( ) => {
463+ mockExec . mockImplementation ( ( path , args , options ) => {
464+ if ( args . includes ( 'version' ) ) {
465+ options . listeners . stdout ( Buffer . from ( 'git version 2.50.1' ) )
466+ }
467+
468+ if ( args . includes ( 'ls-remote' ) ) {
469+ options . listeners . stdout ( Buffer . from ( 'ref: refs/heads/main\tHEAD\n' ) )
470+ }
471+
472+ return 0
473+ } )
474+ jest . spyOn ( exec , 'exec' ) . mockImplementation ( mockExec )
475+
476+ git = await commandManager . createCommandManager ( 'test' , false , false )
477+
478+ await expect (
479+ git . tryGetObjectFormat ( 'https://github.com/example/repo' )
480+ ) . resolves . toEqual ( { format : '' , succeeded : false } )
481+ } )
482+
483+ it ( 'returns unsuccessful when object format detection cannot reach the remote' , async ( ) => {
484+ mockExec . mockImplementation ( ( path , args , options ) => {
485+ if ( args . includes ( 'version' ) ) {
486+ options . listeners . stdout ( Buffer . from ( 'git version 2.50.1' ) )
487+ return 0
488+ }
489+
490+ return 128
491+ } )
492+ jest . spyOn ( exec , 'exec' ) . mockImplementation ( mockExec )
493+
494+ git = await commandManager . createCommandManager ( 'test' , false , false )
495+
496+ await expect (
497+ git . tryGetObjectFormat ( 'https://github.com/example/repo' )
498+ ) . resolves . toEqual ( { format : '' , succeeded : false } )
499+ } )
500+
501+ it ( 'initializes SHA-256 repositories with the matching object format' , async ( ) => {
502+ mockExec . mockImplementation ( ( path , args , options ) => {
503+ if ( args . includes ( 'version' ) ) {
504+ options . listeners . stdout ( Buffer . from ( 'git version 2.50.1' ) )
505+ }
506+
507+ return 0
508+ } )
509+ jest . spyOn ( exec , 'exec' ) . mockImplementation ( mockExec )
510+
511+ git = await commandManager . createCommandManager ( 'test' , false , false )
512+
513+ await git . init ( 'sha256' )
514+
515+ expect ( mockExec ) . toHaveBeenCalledWith (
516+ expect . any ( String ) ,
517+ [ 'init' , '--object-format=sha256' , 'test' ] ,
518+ expect . any ( Object )
519+ )
520+ } )
521+
522+ it ( 'initializes SHA-1 repositories with existing default arguments' , async ( ) => {
523+ mockExec . mockImplementation ( ( path , args , options ) => {
524+ if ( args . includes ( 'version' ) ) {
525+ options . listeners . stdout ( Buffer . from ( 'git version 2.50.1' ) )
526+ }
527+
528+ return 0
529+ } )
530+ jest . spyOn ( exec , 'exec' ) . mockImplementation ( mockExec )
531+
532+ git = await commandManager . createCommandManager ( 'test' , false , false )
533+
534+ await git . init ( 'sha1' )
535+
536+ expect ( mockExec ) . toHaveBeenCalledWith (
537+ expect . any ( String ) ,
538+ [ 'init' , 'test' ] ,
539+ expect . any ( Object )
540+ )
541+ } )
542+ } )
543+
381544describe ( 'git user-agent with orchestration ID' , ( ) => {
382545 beforeEach ( async ( ) => {
383546 jest . spyOn ( fshelper , 'fileExistsSync' ) . mockImplementation ( jest . fn ( ) )
0 commit comments