@@ -105,7 +105,7 @@ func (gs *gitSource) Identifier(scheme, ref string, attrs map[string]string, pla
105
105
}
106
106
107
107
// needs to be called with repo lock
108
- func (gs * gitSource ) mountRemote (ctx context.Context , remote string , authArgs []string , g session.Group ) (target string , release func () error , retErr error ) {
108
+ func (gs * gitSource ) mountRemote (ctx context.Context , remote string , authArgs []string , sha256 bool , g session.Group ) (target string , release func () error , retErr error ) {
109
109
sis , err := searchGitRemote (ctx , gs .cache , remote )
110
110
if err != nil {
111
111
return "" , nil , errors .Wrapf (err , "failed to search metadata for %s" , urlutil .RedactCredentials (remote ))
@@ -171,7 +171,11 @@ func (gs *gitSource) mountRemote(ctx context.Context, remote string, authArgs []
171
171
// implied default to suppress "hint:" output about not having a
172
172
// default initial branch name set which otherwise spams unit
173
173
// test logs.
174
- if _ , err := git .Run (ctx , "-c" , "init.defaultBranch=master" , "init" , "--bare" ); err != nil {
174
+ args := []string {"-c" , "init.defaultBranch=master" , "init" , "--bare" }
175
+ if sha256 {
176
+ args = append (args , "--object-format=sha256" )
177
+ }
178
+ if _ , err := git .Run (ctx , args ... ); err != nil {
175
179
return "" , nil , errors .Wrapf (err , "failed to init repo at %s" , dir )
176
180
}
177
181
@@ -198,6 +202,7 @@ type gitSourceHandler struct {
198
202
* gitSource
199
203
src GitIdentifier
200
204
cacheKey string
205
+ sha256 bool
201
206
sm * session.Manager
202
207
authArgs []string
203
208
}
@@ -376,29 +381,30 @@ func (gs *gitSourceHandler) CacheKey(ctx context.Context, g session.Group, index
376
381
if refCommitFullHash != "" {
377
382
cacheKey := gs .shaToCacheKey (refCommitFullHash , ref2 )
378
383
gs .cacheKey = cacheKey
384
+ gs .sha256 = len (refCommitFullHash ) == 64
379
385
// gs.src.Checksum is verified when checking out the commit
380
386
return cacheKey , refCommitFullHash , nil , true , nil
381
387
}
382
388
383
389
gs .getAuthToken (ctx , g )
384
390
385
- git , cleanup , err := gs .gitCli (ctx , g )
391
+ tmpGit , cleanup , err := gs .emptyGitCli (ctx , g )
386
392
if err != nil {
387
393
return "" , "" , nil , false , err
388
394
}
389
395
defer cleanup ()
390
396
391
397
ref := gs .src .Ref
392
398
if ref == "" {
393
- ref , err = getDefaultBranch (ctx , git , gs .src .Remote )
399
+ ref , err = getDefaultBranch (ctx , tmpGit , gs .src .Remote )
394
400
if err != nil {
395
401
return "" , "" , nil , false , err
396
402
}
397
403
}
398
404
399
405
// TODO: should we assume that remote tag is immutable? add a timer?
400
406
401
- buf , err := git .Run (ctx , "ls-remote" , "origin" , ref , ref + "^{}" )
407
+ buf , err := tmpGit .Run (ctx , "ls-remote" , gs . src . Remote , ref , ref + "^{}" )
402
408
if err != nil {
403
409
return "" , "" , nil , false , errors .Wrapf (err , "failed to fetch remote %s" , urlutil .RedactCredentials (remote ))
404
410
}
@@ -445,6 +451,7 @@ func (gs *gitSourceHandler) CacheKey(ctx context.Context, g session.Group, index
445
451
}
446
452
cacheKey := gs .shaToCacheKey (sha , usedRef )
447
453
gs .cacheKey = cacheKey
454
+ gs .sha256 = len (sha ) == 64
448
455
return cacheKey , sha , nil , true , nil
449
456
}
450
457
@@ -475,15 +482,18 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
475
482
gs .locker .Lock (gs .src .Remote )
476
483
defer gs .locker .Unlock (gs .src .Remote )
477
484
478
- git , cleanup , err := gs .gitCli (ctx , g )
485
+ git , cleanup , err := gs .emptyGitCli (ctx , g )
479
486
if err != nil {
480
487
return nil , err
481
488
}
482
489
defer cleanup ()
483
- gitDir , err := git .GitDir (ctx )
490
+
491
+ gitDir , unmountGitDir , err := gs .mountRemote (ctx , gs .src .Remote , gs .authArgs , gs .sha256 , g )
484
492
if err != nil {
485
493
return nil , err
486
494
}
495
+ defer unmountGitDir ()
496
+ git = git .New (gitutil .WithGitDir (gitDir ))
487
497
488
498
ref := gs .src .Ref
489
499
if ref == "" {
@@ -581,7 +591,11 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
581
591
return nil , err
582
592
}
583
593
checkoutGit := git .New (gitutil .WithWorkTree (checkoutDir ), gitutil .WithGitDir (checkoutDirGit ))
584
- _ , err = checkoutGit .Run (ctx , "-c" , "init.defaultBranch=master" , "init" )
594
+ args := []string {"-c" , "init.defaultBranch=master" , "init" }
595
+ if gs .sha256 {
596
+ args = append (args , "--object-format=sha256" )
597
+ }
598
+ _ , err = checkoutGit .Run (ctx , args ... )
585
599
if err != nil {
586
600
return nil , err
587
601
}
@@ -713,7 +727,7 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
713
727
return snap , nil
714
728
}
715
729
716
- func (gs * gitSourceHandler ) gitCli (ctx context.Context , g session.Group , opts ... gitutil.Option ) (* gitutil.GitCLI , func () error , error ) {
730
+ func (gs * gitSourceHandler ) emptyGitCli (ctx context.Context , g session.Group , opts ... gitutil.Option ) (* gitutil.GitCLI , func () error , error ) {
717
731
var cleanups []func () error
718
732
cleanup := func () error {
719
733
var err error
@@ -727,13 +741,6 @@ func (gs *gitSourceHandler) gitCli(ctx context.Context, g session.Group, opts ..
727
741
}
728
742
var err error
729
743
730
- gitDir , unmountGitDir , err := gs .mountRemote (ctx , gs .src .Remote , gs .authArgs , g )
731
- if err != nil {
732
- cleanup ()
733
- return nil , nil , err
734
- }
735
- cleanups = append (cleanups , unmountGitDir )
736
-
737
744
var sock string
738
745
if gs .src .MountSSHSock != "" {
739
746
var unmountSock func () error
@@ -757,7 +764,6 @@ func (gs *gitSourceHandler) gitCli(ctx context.Context, g session.Group, opts ..
757
764
}
758
765
759
766
opts = append ([]gitutil.Option {
760
- gitutil .WithGitDir (gitDir ),
761
767
gitutil .WithArgs (gs .authArgs ... ),
762
768
gitutil .WithSSHAuthSock (sock ),
763
769
gitutil .WithSSHKnownHosts (knownHosts ),
0 commit comments